CheckTypeSize.cmake 9.1 KB

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