CheckTypeSize.cmake 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # - Check sizeof a type
  2. # CHECK_TYPE_SIZE(TYPE VARIABLE [BUILTIN_TYPES_ONLY])
  3. # Check if the type exists and determine its size.
  4. # On return, "HAVE_${VARIABLE}" holds the existence of the type,
  5. # and "${VARIABLE}" holds one of the following:
  6. # <size> = type has non-zero size <size>
  7. # "0" = type has arch-dependent size (see below)
  8. # "" = type does not exist
  9. # Furthermore, the variable "${VARIABLE}_CODE" holds C preprocessor
  10. # code to define the macro "${VARIABLE}" to the size of the type, or
  11. # leave the macro undefined if the type does not exist.
  12. #
  13. # The variable "${VARIABLE}" may be "0" when CMAKE_OSX_ARCHITECTURES
  14. # has multiple architectures for building OS X universal binaries.
  15. # This indicates that the type size varies across architectures.
  16. # In this case "${VARIABLE}_CODE" contains C preprocessor tests
  17. # mapping from each architecture macro to the corresponding type size.
  18. # The list of architecture macros is stored in "${VARIABLE}_KEYS", and
  19. # the value for each key is stored in "${VARIABLE}-${KEY}".
  20. #
  21. # If the BUILTIN_TYPES_ONLY option is not given, the macro checks for
  22. # headers <sys/types.h>, <stdint.h>, and <stddef.h>, and saves results
  23. # in HAVE_SYS_TYPES_H, HAVE_STDINT_H, and HAVE_STDDEF_H. The type
  24. # size check automatically includes the available headers, thus
  25. # supporting checks of types defined in the headers.
  26. #
  27. # Despite the name of the macro you may use it to check the size of
  28. # more complex expressions, too. To check e.g. for the size of a struct
  29. # member you can do something like this:
  30. # check_type_size("((struct something*)0)->member" SIZEOF_MEMBER)
  31. #
  32. # The following variables may be set before calling this macro to
  33. # modify the way the check is run:
  34. #
  35. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  36. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  37. # CMAKE_REQUIRED_INCLUDES = list of include directories
  38. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  39. # CMAKE_EXTRA_INCLUDE_FILES = list of extra headers to include
  40. #=============================================================================
  41. # Copyright 2002-2009 Kitware, Inc.
  42. #
  43. # Distributed under the OSI-approved BSD License (the "License");
  44. # see accompanying file Copyright.txt for details.
  45. #
  46. # This software is distributed WITHOUT ANY WARRANTY; without even the
  47. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  48. # See the License for more information.
  49. #=============================================================================
  50. # (To distribute this file outside of CMake, substitute the full
  51. # License text for the above reference.)
  52. include(CheckIncludeFile)
  53. include("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
  54. cmake_policy(PUSH)
  55. cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
  56. get_filename_component(__check_type_size_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
  57. #-----------------------------------------------------------------------------
  58. # Helper function. DO NOT CALL DIRECTLY.
  59. function(__check_type_size_impl type var map builtin)
  60. message(STATUS "Check size of ${type}")
  61. # Include header files.
  62. set(headers)
  63. if(builtin)
  64. if(HAVE_SYS_TYPES_H)
  65. set(headers "${headers}#include <sys/types.h>\n")
  66. endif()
  67. if(HAVE_STDINT_H)
  68. set(headers "${headers}#include <stdint.h>\n")
  69. endif()
  70. if(HAVE_STDDEF_H)
  71. set(headers "${headers}#include <stddef.h>\n")
  72. endif()
  73. endif()
  74. foreach(h ${CMAKE_EXTRA_INCLUDE_FILES})
  75. set(headers "${headers}#include \"${h}\"\n")
  76. endforeach()
  77. # Perform the check.
  78. # this one translates potentially used imported library targets to their files on disk
  79. cmake_expand_imported_targets(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
  80. set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.c)
  81. set(bin ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.bin)
  82. configure_file(${__check_type_size_dir}/CheckTypeSize.c.in ${src} @ONLY)
  83. try_compile(HAVE_${var} ${CMAKE_BINARY_DIR} ${src}
  84. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  85. CMAKE_FLAGS
  86. "-DCOMPILE_DEFINITIONS:STRING=${CMAKE_REQUIRED_FLAGS}"
  87. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}"
  88. "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}"
  89. OUTPUT_VARIABLE output
  90. COPY_FILE ${bin}
  91. )
  92. if(HAVE_${var})
  93. # The check compiled. Load information from the binary.
  94. file(STRINGS ${bin} strings LIMIT_COUNT 10 REGEX "INFO:size")
  95. # Parse the information strings.
  96. set(regex_size ".*INFO:size\\[0*([^]]*)\\].*")
  97. set(regex_key " key\\[([^]]*)\\]")
  98. set(keys)
  99. set(code)
  100. set(mismatch)
  101. set(first 1)
  102. foreach(info ${strings})
  103. if("${info}" MATCHES "${regex_size}")
  104. # Get the type size.
  105. string(REGEX REPLACE "${regex_size}" "\\1" size "${info}")
  106. if(first)
  107. set(${var} ${size})
  108. elseif(NOT "${size}" STREQUAL "${${var}}")
  109. set(mismatch 1)
  110. endif()
  111. set(first 0)
  112. # Get the architecture map key.
  113. string(REGEX MATCH "${regex_key}" key "${info}")
  114. string(REGEX REPLACE "${regex_key}" "\\1" key "${key}")
  115. if(key)
  116. set(code "${code}\nset(${var}-${key} \"${size}\")")
  117. list(APPEND keys ${key})
  118. endif()
  119. endif()
  120. endforeach()
  121. # Update the architecture-to-size map.
  122. if(mismatch AND keys)
  123. configure_file(${__check_type_size_dir}/CheckTypeSizeMap.cmake.in ${map} @ONLY)
  124. set(${var} 0)
  125. else()
  126. file(REMOVE ${map})
  127. endif()
  128. if(mismatch AND NOT keys)
  129. 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 !")
  130. endif()
  131. message(STATUS "Check size of ${type} - done")
  132. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  133. "Determining size of ${type} passed with the following output:\n${output}\n\n")
  134. set(${var} "${${var}}" CACHE INTERNAL "CHECK_TYPE_SIZE: sizeof(${type})")
  135. else()
  136. # The check failed to compile.
  137. message(STATUS "Check size of ${type} - failed")
  138. file(READ ${src} content)
  139. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  140. "Determining size of ${type} failed with the following output:\n${output}\n${src}:\n${content}\n\n")
  141. set(${var} "" CACHE INTERNAL "CHECK_TYPE_SIZE: ${type} unknown")
  142. file(REMOVE ${map})
  143. endif()
  144. endfunction()
  145. #-----------------------------------------------------------------------------
  146. macro(CHECK_TYPE_SIZE TYPE VARIABLE)
  147. # Optionally check for standard headers.
  148. if("${ARGV2}" STREQUAL "BUILTIN_TYPES_ONLY")
  149. set(_builtin 0)
  150. else()
  151. set(_builtin 1)
  152. check_include_file(sys/types.h HAVE_SYS_TYPES_H)
  153. check_include_file(stdint.h HAVE_STDINT_H)
  154. check_include_file(stddef.h HAVE_STDDEF_H)
  155. endif()
  156. # Compute or load the size or size map.
  157. set(${VARIABLE}_KEYS)
  158. set(_map_file ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${VARIABLE}.cmake)
  159. if(NOT DEFINED HAVE_${VARIABLE})
  160. __check_type_size_impl(${TYPE} ${VARIABLE} ${_map_file} ${_builtin})
  161. endif()
  162. include(${_map_file} OPTIONAL)
  163. set(_map_file)
  164. set(_builtin)
  165. # Create preprocessor code.
  166. if(${VARIABLE}_KEYS)
  167. set(${VARIABLE}_CODE)
  168. set(_if if)
  169. foreach(key ${${VARIABLE}_KEYS})
  170. set(${VARIABLE}_CODE "${${VARIABLE}_CODE}#${_if} defined(${key})\n# define ${VARIABLE} ${${VARIABLE}-${key}}\n")
  171. set(_if elif)
  172. endforeach()
  173. set(${VARIABLE}_CODE "${${VARIABLE}_CODE}#else\n# error ${VARIABLE} unknown\n#endif")
  174. set(_if)
  175. elseif(${VARIABLE})
  176. set(${VARIABLE}_CODE "#define ${VARIABLE} ${${VARIABLE}}")
  177. else()
  178. set(${VARIABLE}_CODE "/* #undef ${VARIABLE} */")
  179. endif()
  180. endmacro()
  181. #-----------------------------------------------------------------------------
  182. cmake_policy(POP)