CheckTypeSize.cmake 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. # The following variables may be set before calling this macro to
  28. # modify the way the check is run:
  29. #
  30. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  31. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  32. # CMAKE_REQUIRED_INCLUDES = list of include directories
  33. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  34. # CMAKE_EXTRA_INCLUDE_FILES = list of extra headers to include
  35. #=============================================================================
  36. # Copyright 2002-2009 Kitware, Inc.
  37. #
  38. # Distributed under the OSI-approved BSD License (the "License");
  39. # see accompanying file Copyright.txt for details.
  40. #
  41. # This software is distributed WITHOUT ANY WARRANTY; without even the
  42. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  43. # See the License for more information.
  44. #=============================================================================
  45. # (To distribute this file outside of CMake, substitute the full
  46. # License text for the above reference.)
  47. include(CheckIncludeFile)
  48. include("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
  49. cmake_policy(PUSH)
  50. cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
  51. get_filename_component(__check_type_size_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
  52. #-----------------------------------------------------------------------------
  53. # Helper function. DO NOT CALL DIRECTLY.
  54. function(__check_type_size_impl type var map builtin)
  55. message(STATUS "Check size of ${type}")
  56. # Include header files.
  57. set(headers)
  58. if(builtin)
  59. if(HAVE_SYS_TYPES_H)
  60. set(headers "${headers}#include <sys/types.h>\n")
  61. endif()
  62. if(HAVE_STDINT_H)
  63. set(headers "${headers}#include <stdint.h>\n")
  64. endif()
  65. if(HAVE_STDDEF_H)
  66. set(headers "${headers}#include <stddef.h>\n")
  67. endif()
  68. endif()
  69. foreach(h ${CMAKE_EXTRA_INCLUDE_FILES})
  70. set(headers "${headers}#include \"${h}\"\n")
  71. endforeach()
  72. # Perform the check.
  73. # this one translates potentially used imported library targets to their files on disk
  74. cmake_expand_imported_targets(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
  75. set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.c)
  76. set(bin ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.bin)
  77. configure_file(${__check_type_size_dir}/CheckTypeSize.c.in ${src} @ONLY)
  78. try_compile(HAVE_${var} ${CMAKE_BINARY_DIR} ${src}
  79. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  80. CMAKE_FLAGS
  81. "-DCOMPILE_DEFINITIONS:STRING=${CMAKE_REQUIRED_FLAGS}"
  82. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}"
  83. "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}"
  84. OUTPUT_VARIABLE output
  85. COPY_FILE ${bin}
  86. )
  87. if(HAVE_${var})
  88. # The check compiled. Load information from the binary.
  89. file(STRINGS ${bin} strings LIMIT_COUNT 10 REGEX "INFO:size")
  90. # Parse the information strings.
  91. set(regex_size ".*INFO:size\\[0*([^]]*)\\].*")
  92. set(regex_key " key\\[([^]]*)\\]")
  93. set(keys)
  94. set(code)
  95. set(mismatch)
  96. set(first 1)
  97. foreach(info ${strings})
  98. if("${info}" MATCHES "${regex_size}")
  99. # Get the type size.
  100. string(REGEX REPLACE "${regex_size}" "\\1" size "${info}")
  101. if(first)
  102. set(${var} ${size})
  103. elseif(NOT "${size}" STREQUAL "${${var}}")
  104. set(mismatch 1)
  105. endif()
  106. set(first 0)
  107. # Get the architecture map key.
  108. string(REGEX MATCH "${regex_key}" key "${info}")
  109. string(REGEX REPLACE "${regex_key}" "\\1" key "${key}")
  110. if(key)
  111. set(code "${code}\nset(${var}-${key} \"${size}\")")
  112. list(APPEND keys ${key})
  113. endif()
  114. endif()
  115. endforeach()
  116. # Update the architecture-to-size map.
  117. if(mismatch AND keys)
  118. configure_file(${__check_type_size_dir}/CheckTypeSizeMap.cmake.in ${map} @ONLY)
  119. set(${var} 0)
  120. else()
  121. file(REMOVE ${map})
  122. endif()
  123. if(mismatch AND NOT keys)
  124. 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 !")
  125. endif()
  126. message(STATUS "Check size of ${type} - done")
  127. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  128. "Determining size of ${type} passed with the following output:\n${output}\n\n")
  129. set(${var} "${${var}}" CACHE INTERNAL "CHECK_TYPE_SIZE: sizeof(${type})")
  130. else(HAVE_${var})
  131. # The check failed to compile.
  132. message(STATUS "Check size of ${type} - failed")
  133. file(READ ${src} content)
  134. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  135. "Determining size of ${type} failed with the following output:\n${output}\n${src}:\n${content}\n\n")
  136. set(${var} "" CACHE INTERNAL "CHECK_TYPE_SIZE: ${type} unknown")
  137. file(REMOVE ${map})
  138. endif(HAVE_${var})
  139. endfunction()
  140. #-----------------------------------------------------------------------------
  141. macro(CHECK_TYPE_SIZE TYPE VARIABLE)
  142. # Optionally check for standard headers.
  143. if("${ARGV2}" STREQUAL "BUILTIN_TYPES_ONLY")
  144. set(_builtin 0)
  145. else()
  146. set(_builtin 1)
  147. check_include_file(sys/types.h HAVE_SYS_TYPES_H)
  148. check_include_file(stdint.h HAVE_STDINT_H)
  149. check_include_file(stddef.h HAVE_STDDEF_H)
  150. endif()
  151. # Compute or load the size or size map.
  152. set(${VARIABLE}_KEYS)
  153. set(_map_file ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${VARIABLE}.cmake)
  154. if(NOT DEFINED HAVE_${VARIABLE})
  155. __check_type_size_impl(${TYPE} ${VARIABLE} ${_map_file} ${_builtin})
  156. endif()
  157. include(${_map_file} OPTIONAL)
  158. set(_map_file)
  159. set(_builtin)
  160. # Create preprocessor code.
  161. if(${VARIABLE}_KEYS)
  162. set(${VARIABLE}_CODE)
  163. set(_if if)
  164. foreach(key ${${VARIABLE}_KEYS})
  165. set(${VARIABLE}_CODE "${${VARIABLE}_CODE}#${_if} defined(${key})\n# define ${VARIABLE} ${${VARIABLE}-${key}}\n")
  166. set(_if elif)
  167. endforeach()
  168. set(${VARIABLE}_CODE "${${VARIABLE}_CODE}#else\n# error ${VARIABLE} unknown\n#endif")
  169. set(_if)
  170. elseif(${VARIABLE})
  171. set(${VARIABLE}_CODE "#define ${VARIABLE} ${${VARIABLE}}")
  172. else()
  173. set(${VARIABLE}_CODE "/* #undef ${VARIABLE} */")
  174. endif()
  175. endmacro()
  176. #-----------------------------------------------------------------------------
  177. cmake_policy(POP)