CheckTypeSize.cmake 9.5 KB

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