CheckTypeSize.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckTypeSize
  5. -------------
  6. Check sizeof a type
  7. .. command:: check_type_size
  8. .. code-block:: cmake
  9. check_type_size(<type> <variable> [BUILTIN_TYPES_ONLY]
  10. [LANGUAGE <language>])
  11. Check if the type exists and determine its size. Results are reported
  12. in the following variables:
  13. ``HAVE_<variable>``
  14. Holds a true or false value indicating whether the type exists.
  15. ``<variable>``
  16. Holds one of the following values:
  17. ``<size>``
  18. Type has non-zero size ``<size>``.
  19. ``0``
  20. Type has architecture-dependent size. This may occur when
  21. :variable:`CMAKE_OSX_ARCHITECTURES` has multiple architectures.
  22. In this case ``<variable>_CODE`` contains C preprocessor tests
  23. mapping from each architecture macro to the corresponding type size.
  24. The list of architecture macros is stored in ``<variable>_KEYS``,
  25. and the value for each key is stored in ``<variable>-<key>``.
  26. "" (empty string)
  27. Type does not exist.
  28. ``<variable>_CODE``
  29. Holds C preprocessor code to define the macro ``<variable>`` to the size
  30. of the type, or to leave the macro undefined if the type does not exist.
  31. The options are:
  32. ``BUILTIN_TYPES_ONLY``
  33. Support only compiler-builtin types. If *not* given, the macro checks
  34. for headers ``<sys/types.h>``, ``<stdint.h>``, and ``<stddef.h>``, and
  35. saves results in ``HAVE_SYS_TYPES_H``, ``HAVE_STDINT_H``, and
  36. ``HAVE_STDDEF_H``. The type size check automatically includes the
  37. available headers, thus supporting checks of types defined in the headers.
  38. ``LANGUAGE <language>``
  39. Use the ``<language>`` compiler to perform the check.
  40. Acceptable values are ``C`` and ``CXX``.
  41. If not specified, it defaults to ``C``.
  42. Despite the name of the macro you may use it to check the size of more
  43. complex expressions, too. To check e.g. for the size of a struct
  44. member you can do something like this:
  45. .. code-block:: cmake
  46. check_type_size("((struct something*)0)->member" SIZEOF_MEMBER)
  47. The following variables may be set before calling this macro to modify
  48. the way the check is run:
  49. .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst
  50. .. include:: /module/include/CMAKE_REQUIRED_DEFINITIONS.rst
  51. .. include:: /module/include/CMAKE_REQUIRED_INCLUDES.rst
  52. .. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
  53. .. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
  54. .. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
  55. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst
  56. ``CMAKE_EXTRA_INCLUDE_FILES``
  57. list of extra headers to include.
  58. Examples
  59. ^^^^^^^^
  60. Consider the code:
  61. .. code-block:: cmake
  62. include(CheckTypeSize)
  63. # Check for size of long.
  64. check_type_size(long SIZEOF_LONG)
  65. message("HAVE_SIZEOF_LONG: ${HAVE_SIZEOF_LONG}")
  66. message("SIZEOF_LONG: ${SIZEOF_LONG}")
  67. message("SIZEOF_LONG_CODE: ${SIZEOF_LONG_CODE}")
  68. On a 64-bit architecture, the output may look something like this::
  69. HAVE_SIZEOF_LONG: TRUE
  70. SIZEOF_LONG: 8
  71. SIZEOF_LONG_CODE: #define SIZEOF_LONG 8
  72. On Apple platforms, when :variable:`CMAKE_OSX_ARCHITECTURES` has multiple
  73. architectures, types may have architecture-dependent sizes.
  74. For example, with the code
  75. .. code-block:: cmake
  76. include(CheckTypeSize)
  77. check_type_size(long SIZEOF_LONG)
  78. message("HAVE_SIZEOF_LONG: ${HAVE_SIZEOF_LONG}")
  79. message("SIZEOF_LONG: ${SIZEOF_LONG}")
  80. foreach(key IN LISTS SIZE_OF_LONG_KEYS)
  81. message("key: ${key}")
  82. message("value: ${SIZE_OF_LONG-${key}}")
  83. endforeach()
  84. message("SIZEOF_LONG_CODE:
  85. ${SIZEOF_LONG_CODE}")
  86. the result may be::
  87. HAVE_SIZEOF_LONG: TRUE
  88. SIZEOF_LONG: 0
  89. key: __i386
  90. value: 4
  91. key: __x86_64
  92. value: 8
  93. SIZEOF_LONG_CODE:
  94. #if defined(__i386)
  95. # define SIZE_OF_LONG 4
  96. #elif defined(__x86_64)
  97. # define SIZE_OF_LONG 8
  98. #else
  99. # error SIZE_OF_LONG unknown
  100. #endif
  101. #]=======================================================================]
  102. include(CheckIncludeFile)
  103. include(CheckIncludeFileCXX)
  104. get_filename_component(__check_type_size_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
  105. include_guard(GLOBAL)
  106. block(SCOPE_FOR POLICIES)
  107. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  108. #-----------------------------------------------------------------------------
  109. # Helper function. DO NOT CALL DIRECTLY.
  110. function(__check_type_size_impl type var map builtin language)
  111. if(NOT CMAKE_REQUIRED_QUIET)
  112. message(CHECK_START "Check size of ${type}")
  113. endif()
  114. # Perform language check
  115. string(MAKE_C_IDENTIFIER ${var} _var_escaped)
  116. if(language STREQUAL "C")
  117. set(src ${_var_escaped}.c)
  118. elseif(language STREQUAL "CXX")
  119. set(src ${_var_escaped}.cpp)
  120. else()
  121. message(FATAL_ERROR "Unknown language:\n ${language}\nSupported languages: C, CXX.\n")
  122. endif()
  123. # Include header files.
  124. set(headers)
  125. if(builtin)
  126. if(language STREQUAL "CXX" AND type MATCHES "^std::")
  127. if(HAVE_SYS_TYPES_H)
  128. string(APPEND headers "#include <sys/types.h>\n")
  129. endif()
  130. if(HAVE_CSTDINT)
  131. string(APPEND headers "#include <cstdint>\n")
  132. endif()
  133. if(HAVE_CSTDDEF)
  134. string(APPEND headers "#include <cstddef>\n")
  135. endif()
  136. else()
  137. if(HAVE_SYS_TYPES_H)
  138. string(APPEND headers "#include <sys/types.h>\n")
  139. endif()
  140. if(HAVE_STDINT_H)
  141. string(APPEND headers "#include <stdint.h>\n")
  142. endif()
  143. if(HAVE_STDDEF_H)
  144. string(APPEND headers "#include <stddef.h>\n")
  145. endif()
  146. endif()
  147. endif()
  148. foreach(h ${CMAKE_EXTRA_INCLUDE_FILES})
  149. string(APPEND headers "#include \"${h}\"\n")
  150. endforeach()
  151. if(CMAKE_REQUIRED_LINK_DIRECTORIES)
  152. set(_CTS_LINK_DIRECTORIES
  153. "-DLINK_DIRECTORIES:STRING=${CMAKE_REQUIRED_LINK_DIRECTORIES}")
  154. else()
  155. set(_CTS_LINK_DIRECTORIES)
  156. endif()
  157. # Perform the check.
  158. set(bin ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.bin)
  159. file(READ ${__check_type_size_dir}/CheckTypeSize.c.in src_content)
  160. string(CONFIGURE "${src_content}" src_content @ONLY)
  161. try_compile(HAVE_${var} SOURCE_FROM_VAR "${src}" src_content
  162. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  163. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS}
  164. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}
  165. CMAKE_FLAGS
  166. "-DCOMPILE_DEFINITIONS:STRING=${CMAKE_REQUIRED_FLAGS}"
  167. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}"
  168. "${_CTS_LINK_DIRECTORIES}"
  169. COPY_FILE ${bin}
  170. )
  171. unset(_CTS_LINK_DIRECTORIES)
  172. if(HAVE_${var})
  173. # The check compiled. Load information from the binary.
  174. file(STRINGS ${bin} strings LIMIT_COUNT 10 REGEX "INFO:size")
  175. # Parse the information strings.
  176. set(regex_size ".*INFO:size\\[0*([^]]*)\\].*")
  177. set(regex_key " key\\[([^]]*)\\]")
  178. set(keys)
  179. set(code)
  180. set(mismatch)
  181. set(first 1)
  182. foreach(info ${strings})
  183. if("${info}" MATCHES "${regex_size}")
  184. # Get the type size.
  185. set(size "${CMAKE_MATCH_1}")
  186. if(first)
  187. set(${var} ${size})
  188. elseif(NOT "${size}" STREQUAL "${${var}}")
  189. set(mismatch 1)
  190. endif()
  191. set(first 0)
  192. # Get the architecture map key.
  193. string(REGEX MATCH "${regex_key}" key "${info}")
  194. string(REGEX REPLACE "${regex_key}" "\\1" key "${key}")
  195. if(key)
  196. string(APPEND code "\nset(${var}-${key} \"${size}\")")
  197. list(APPEND keys ${key})
  198. endif()
  199. endif()
  200. endforeach()
  201. # Update the architecture-to-size map.
  202. if(mismatch AND keys)
  203. configure_file(${__check_type_size_dir}/CheckTypeSizeMap.cmake.in ${map} @ONLY)
  204. set(${var} 0)
  205. else()
  206. file(REMOVE ${map})
  207. endif()
  208. if(mismatch AND NOT keys)
  209. message(SEND_ERROR "CHECK_TYPE_SIZE found different results, consider setting CMAKE_OSX_ARCHITECTURES or CMAKE_TRY_COMPILE_OSX_ARCHITECTURES to one or no architecture !")
  210. endif()
  211. if(NOT CMAKE_REQUIRED_QUIET)
  212. message(CHECK_PASS "done")
  213. endif()
  214. set(${var} "${${var}}" CACHE INTERNAL "CHECK_TYPE_SIZE: sizeof(${type})")
  215. else()
  216. # The check failed to compile.
  217. if(NOT CMAKE_REQUIRED_QUIET)
  218. message(CHECK_FAIL "failed")
  219. endif()
  220. set(${var} "" CACHE INTERNAL "CHECK_TYPE_SIZE: ${type} unknown")
  221. file(REMOVE ${map})
  222. endif()
  223. endfunction()
  224. #-----------------------------------------------------------------------------
  225. macro(CHECK_TYPE_SIZE TYPE VARIABLE)
  226. # parse arguments
  227. unset(doing)
  228. foreach(arg ${ARGN})
  229. if("x${arg}" STREQUAL "xBUILTIN_TYPES_ONLY")
  230. set(_CHECK_TYPE_SIZE_${arg} 1)
  231. unset(doing)
  232. elseif("x${arg}" STREQUAL "xLANGUAGE") # change to MATCHES for more keys
  233. set(doing "${arg}")
  234. set(_CHECK_TYPE_SIZE_${doing} "")
  235. elseif("x${doing}" STREQUAL "xLANGUAGE")
  236. set(_CHECK_TYPE_SIZE_${doing} "${arg}")
  237. unset(doing)
  238. else()
  239. message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
  240. endif()
  241. endforeach()
  242. if("x${doing}" MATCHES "^x(LANGUAGE)$")
  243. message(FATAL_ERROR "Missing argument:\n ${doing} arguments requires a value\n")
  244. endif()
  245. if(DEFINED _CHECK_TYPE_SIZE_LANGUAGE)
  246. if(NOT "x${_CHECK_TYPE_SIZE_LANGUAGE}" MATCHES "^x(C|CXX)$")
  247. message(FATAL_ERROR "Unknown language:\n ${_CHECK_TYPE_SIZE_LANGUAGE}.\nSupported languages: C, CXX.\n")
  248. endif()
  249. set(_language ${_CHECK_TYPE_SIZE_LANGUAGE})
  250. else()
  251. set(_language C)
  252. endif()
  253. # Optionally check for standard headers.
  254. if(_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)
  255. set(_builtin 0)
  256. else()
  257. set(_builtin 1)
  258. if(_language STREQUAL "C")
  259. check_include_file(sys/types.h HAVE_SYS_TYPES_H)
  260. check_include_file(stdint.h HAVE_STDINT_H)
  261. check_include_file(stddef.h HAVE_STDDEF_H)
  262. elseif(_language STREQUAL "CXX")
  263. check_include_file_cxx(sys/types.h HAVE_SYS_TYPES_H)
  264. if("${TYPE}" MATCHES "^std::")
  265. check_include_file_cxx(cstdint HAVE_CSTDINT)
  266. check_include_file_cxx(cstddef HAVE_CSTDDEF)
  267. else()
  268. check_include_file_cxx(stdint.h HAVE_STDINT_H)
  269. check_include_file_cxx(stddef.h HAVE_STDDEF_H)
  270. endif()
  271. endif()
  272. endif()
  273. unset(_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)
  274. unset(_CHECK_TYPE_SIZE_LANGUAGE)
  275. # Compute or load the size or size map.
  276. set(${VARIABLE}_KEYS)
  277. set(_map_file ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${VARIABLE}.cmake)
  278. if(NOT DEFINED HAVE_${VARIABLE})
  279. __check_type_size_impl(${TYPE} ${VARIABLE} ${_map_file} ${_builtin} ${_language})
  280. endif()
  281. include(${_map_file} OPTIONAL)
  282. set(_map_file)
  283. set(_builtin)
  284. # Create preprocessor code.
  285. if(${VARIABLE}_KEYS)
  286. set(${VARIABLE}_CODE)
  287. set(_if if)
  288. foreach(key ${${VARIABLE}_KEYS})
  289. string(APPEND ${VARIABLE}_CODE "#${_if} defined(${key})\n# define ${VARIABLE} ${${VARIABLE}-${key}}\n")
  290. set(_if elif)
  291. endforeach()
  292. string(APPEND ${VARIABLE}_CODE "#else\n# error ${VARIABLE} unknown\n#endif")
  293. set(_if)
  294. elseif(${VARIABLE})
  295. set(${VARIABLE}_CODE "#define ${VARIABLE} ${${VARIABLE}}")
  296. else()
  297. set(${VARIABLE}_CODE "/* #undef ${VARIABLE} */")
  298. endif()
  299. endmacro()
  300. #-----------------------------------------------------------------------------
  301. endblock()