CheckTypeSize.cmake 9.5 KB

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