CheckTypeSize.cmake 9.1 KB

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