CheckTypeSize.cmake 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt 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. On return,
  12. ``HAVE_${VARIABLE}`` holds the existence of the type, and ``${VARIABLE}``
  13. holds one of the following:
  14. ::
  15. <size> = type has non-zero size <size>
  16. "0" = type has arch-dependent size (see below)
  17. "" = type does not exist
  18. Both ``HAVE_${VARIABLE}`` and ``${VARIABLE}`` will be created as internal
  19. cache variables.
  20. Furthermore, the variable ``${VARIABLE}_CODE`` holds C preprocessor code
  21. to define the macro ``${VARIABLE}`` to the size of the type, or leave
  22. the macro undefined if the type does not exist.
  23. The variable ``${VARIABLE}`` may be ``0`` when
  24. :variable:`CMAKE_OSX_ARCHITECTURES` has multiple architectures for building
  25. OS X universal binaries. This indicates that the type size varies across
  26. architectures. In this case ``${VARIABLE}_CODE`` contains C preprocessor
  27. tests mapping from each architecture macro to the corresponding type size.
  28. The list of architecture macros is stored in ``${VARIABLE}_KEYS``, and the
  29. value for each key is stored in ``${VARIABLE}-${KEY}``.
  30. If the ``BUILTIN_TYPES_ONLY`` option is not given, the macro checks for
  31. headers ``<sys/types.h>``, ``<stdint.h>``, and ``<stddef.h>``, and saves
  32. results in ``HAVE_SYS_TYPES_H``, ``HAVE_STDINT_H``, and ``HAVE_STDDEF_H``.
  33. The type size check automatically includes the available headers, thus
  34. supporting checks of types defined in the headers.
  35. If ``LANGUAGE`` is set, the specified compiler will be used to perform the
  36. check. Acceptable values are ``C`` and ``CXX``.
  37. Despite the name of the macro you may use it to check the size of more
  38. complex expressions, too. To check e.g. for the size of a struct
  39. member you can do something like this:
  40. .. code-block:: cmake
  41. check_type_size("((struct something*)0)->member" SIZEOF_MEMBER)
  42. The following variables may be set before calling this macro to modify
  43. the way the check is run:
  44. ::
  45. CMAKE_REQUIRED_FLAGS = string of compile command line flags
  46. CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  47. CMAKE_REQUIRED_INCLUDES = list of include directories
  48. CMAKE_REQUIRED_LINK_OPTIONS = list of options to pass to link command
  49. CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  50. CMAKE_REQUIRED_QUIET = execute quietly without messages
  51. CMAKE_EXTRA_INCLUDE_FILES = list of extra headers to include
  52. #]=======================================================================]
  53. include(CheckIncludeFile)
  54. include(CheckIncludeFileCXX)
  55. get_filename_component(__check_type_size_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
  56. include_guard(GLOBAL)
  57. cmake_policy(PUSH)
  58. cmake_policy(SET CMP0054 NEW)
  59. #-----------------------------------------------------------------------------
  60. # Helper function. DO NOT CALL DIRECTLY.
  61. function(__check_type_size_impl type var map builtin language)
  62. if(NOT CMAKE_REQUIRED_QUIET)
  63. message(CHECK_START "Check size of ${type}")
  64. endif()
  65. # Perform language check
  66. if(language STREQUAL "C")
  67. set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.c)
  68. elseif(language STREQUAL "CXX")
  69. set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.cpp)
  70. else()
  71. message(FATAL_ERROR "Unknown language:\n ${language}\nSupported languages: C, CXX.\n")
  72. endif()
  73. # Include header files.
  74. set(headers)
  75. if(builtin)
  76. if(language STREQUAL "CXX" AND type MATCHES "^std::")
  77. if(HAVE_SYS_TYPES_H)
  78. string(APPEND headers "#include <sys/types.h>\n")
  79. endif()
  80. if(HAVE_CSTDINT)
  81. string(APPEND headers "#include <cstdint>\n")
  82. endif()
  83. if(HAVE_CSTDDEF)
  84. string(APPEND headers "#include <cstddef>\n")
  85. endif()
  86. else()
  87. if(HAVE_SYS_TYPES_H)
  88. string(APPEND headers "#include <sys/types.h>\n")
  89. endif()
  90. if(HAVE_STDINT_H)
  91. string(APPEND headers "#include <stdint.h>\n")
  92. endif()
  93. if(HAVE_STDDEF_H)
  94. string(APPEND headers "#include <stddef.h>\n")
  95. endif()
  96. endif()
  97. endif()
  98. foreach(h ${CMAKE_EXTRA_INCLUDE_FILES})
  99. string(APPEND headers "#include \"${h}\"\n")
  100. endforeach()
  101. # Perform the check.
  102. set(bin ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.bin)
  103. configure_file(${__check_type_size_dir}/CheckTypeSize.c.in ${src} @ONLY)
  104. try_compile(HAVE_${var} ${CMAKE_BINARY_DIR} ${src}
  105. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  106. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS}
  107. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}
  108. CMAKE_FLAGS
  109. "-DCOMPILE_DEFINITIONS:STRING=${CMAKE_REQUIRED_FLAGS}"
  110. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}"
  111. OUTPUT_VARIABLE output
  112. COPY_FILE ${bin}
  113. )
  114. if(HAVE_${var})
  115. # The check compiled. Load information from the binary.
  116. file(STRINGS ${bin} strings LIMIT_COUNT 10 REGEX "INFO:size")
  117. # Parse the information strings.
  118. set(regex_size ".*INFO:size\\[0*([^]]*)\\].*")
  119. set(regex_key " key\\[([^]]*)\\]")
  120. set(keys)
  121. set(code)
  122. set(mismatch)
  123. set(first 1)
  124. foreach(info ${strings})
  125. if("${info}" MATCHES "${regex_size}")
  126. # Get the type size.
  127. set(size "${CMAKE_MATCH_1}")
  128. if(first)
  129. set(${var} ${size})
  130. elseif(NOT "${size}" STREQUAL "${${var}}")
  131. set(mismatch 1)
  132. endif()
  133. set(first 0)
  134. # Get the architecture map key.
  135. string(REGEX MATCH "${regex_key}" key "${info}")
  136. string(REGEX REPLACE "${regex_key}" "\\1" key "${key}")
  137. if(key)
  138. string(APPEND code "\nset(${var}-${key} \"${size}\")")
  139. list(APPEND keys ${key})
  140. endif()
  141. endif()
  142. endforeach()
  143. # Update the architecture-to-size map.
  144. if(mismatch AND keys)
  145. configure_file(${__check_type_size_dir}/CheckTypeSizeMap.cmake.in ${map} @ONLY)
  146. set(${var} 0)
  147. else()
  148. file(REMOVE ${map})
  149. endif()
  150. if(mismatch AND NOT keys)
  151. 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 !")
  152. endif()
  153. if(NOT CMAKE_REQUIRED_QUIET)
  154. message(CHECK_PASS "done")
  155. endif()
  156. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  157. "Determining size of ${type} passed with the following output:\n${output}\n\n")
  158. set(${var} "${${var}}" CACHE INTERNAL "CHECK_TYPE_SIZE: sizeof(${type})")
  159. else()
  160. # The check failed to compile.
  161. if(NOT CMAKE_REQUIRED_QUIET)
  162. message(CHECK_FAIL "failed")
  163. endif()
  164. file(READ ${src} content)
  165. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  166. "Determining size of ${type} failed with the following output:\n${output}\n${src}:\n${content}\n\n")
  167. set(${var} "" CACHE INTERNAL "CHECK_TYPE_SIZE: ${type} unknown")
  168. file(REMOVE ${map})
  169. endif()
  170. endfunction()
  171. #-----------------------------------------------------------------------------
  172. macro(CHECK_TYPE_SIZE TYPE VARIABLE)
  173. # parse arguments
  174. unset(doing)
  175. foreach(arg ${ARGN})
  176. if("x${arg}" STREQUAL "xBUILTIN_TYPES_ONLY")
  177. set(_CHECK_TYPE_SIZE_${arg} 1)
  178. unset(doing)
  179. elseif("x${arg}" STREQUAL "xLANGUAGE") # change to MATCHES for more keys
  180. set(doing "${arg}")
  181. set(_CHECK_TYPE_SIZE_${doing} "")
  182. elseif("x${doing}" STREQUAL "xLANGUAGE")
  183. set(_CHECK_TYPE_SIZE_${doing} "${arg}")
  184. unset(doing)
  185. else()
  186. message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
  187. endif()
  188. endforeach()
  189. if("x${doing}" MATCHES "^x(LANGUAGE)$")
  190. message(FATAL_ERROR "Missing argument:\n ${doing} arguments requires a value\n")
  191. endif()
  192. if(DEFINED _CHECK_TYPE_SIZE_LANGUAGE)
  193. if(NOT "x${_CHECK_TYPE_SIZE_LANGUAGE}" MATCHES "^x(C|CXX)$")
  194. message(FATAL_ERROR "Unknown language:\n ${_CHECK_TYPE_SIZE_LANGUAGE}.\nSupported languages: C, CXX.\n")
  195. endif()
  196. set(_language ${_CHECK_TYPE_SIZE_LANGUAGE})
  197. else()
  198. set(_language C)
  199. endif()
  200. # Optionally check for standard headers.
  201. if(_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)
  202. set(_builtin 0)
  203. else()
  204. set(_builtin 1)
  205. if(_language STREQUAL "C")
  206. check_include_file(sys/types.h HAVE_SYS_TYPES_H)
  207. check_include_file(stdint.h HAVE_STDINT_H)
  208. check_include_file(stddef.h HAVE_STDDEF_H)
  209. elseif(_language STREQUAL "CXX")
  210. check_include_file_cxx(sys/types.h HAVE_SYS_TYPES_H)
  211. if("${TYPE}" MATCHES "^std::")
  212. check_include_file_cxx(cstdint HAVE_CSTDINT)
  213. check_include_file_cxx(cstddef HAVE_CSTDDEF)
  214. else()
  215. check_include_file_cxx(stdint.h HAVE_STDINT_H)
  216. check_include_file_cxx(stddef.h HAVE_STDDEF_H)
  217. endif()
  218. endif()
  219. endif()
  220. unset(_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)
  221. unset(_CHECK_TYPE_SIZE_LANGUAGE)
  222. # Compute or load the size or size map.
  223. set(${VARIABLE}_KEYS)
  224. set(_map_file ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${VARIABLE}.cmake)
  225. if(NOT DEFINED HAVE_${VARIABLE})
  226. __check_type_size_impl(${TYPE} ${VARIABLE} ${_map_file} ${_builtin} ${_language})
  227. endif()
  228. include(${_map_file} OPTIONAL)
  229. set(_map_file)
  230. set(_builtin)
  231. # Create preprocessor code.
  232. if(${VARIABLE}_KEYS)
  233. set(${VARIABLE}_CODE)
  234. set(_if if)
  235. foreach(key ${${VARIABLE}_KEYS})
  236. string(APPEND ${VARIABLE}_CODE "#${_if} defined(${key})\n# define ${VARIABLE} ${${VARIABLE}-${key}}\n")
  237. set(_if elif)
  238. endforeach()
  239. string(APPEND ${VARIABLE}_CODE "#else\n# error ${VARIABLE} unknown\n#endif")
  240. set(_if)
  241. elseif(${VARIABLE})
  242. set(${VARIABLE}_CODE "#define ${VARIABLE} ${${VARIABLE}}")
  243. else()
  244. set(${VARIABLE}_CODE "/* #undef ${VARIABLE} */")
  245. endif()
  246. endmacro()
  247. #-----------------------------------------------------------------------------
  248. cmake_policy(POP)