CheckTypeSize.cmake 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. cmake_policy(PUSH)
  54. cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
  55. get_filename_component(__check_type_size_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
  56. #-----------------------------------------------------------------------------
  57. # Helper function. DO NOT CALL DIRECTLY.
  58. function(__check_type_size_impl type var map builtin)
  59. message(STATUS "Check size of ${type}")
  60. # Include header files.
  61. set(headers)
  62. if(builtin)
  63. if(HAVE_SYS_TYPES_H)
  64. set(headers "${headers}#include <sys/types.h>\n")
  65. endif()
  66. if(HAVE_STDINT_H)
  67. set(headers "${headers}#include <stdint.h>\n")
  68. endif()
  69. if(HAVE_STDDEF_H)
  70. set(headers "${headers}#include <stddef.h>\n")
  71. endif()
  72. endif()
  73. foreach(h ${CMAKE_EXTRA_INCLUDE_FILES})
  74. set(headers "${headers}#include \"${h}\"\n")
  75. endforeach()
  76. # Perform the check.
  77. set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.c)
  78. set(bin ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.bin)
  79. configure_file(${__check_type_size_dir}/CheckTypeSize.c.in ${src} @ONLY)
  80. try_compile(HAVE_${var} ${CMAKE_BINARY_DIR} ${src}
  81. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  82. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}
  83. CMAKE_FLAGS
  84. "-DCOMPILE_DEFINITIONS:STRING=${CMAKE_REQUIRED_FLAGS}"
  85. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}"
  86. OUTPUT_VARIABLE output
  87. COPY_FILE ${bin}
  88. )
  89. if(HAVE_${var})
  90. # The check compiled. Load information from the binary.
  91. file(STRINGS ${bin} strings LIMIT_COUNT 10 REGEX "INFO:size")
  92. # Parse the information strings.
  93. set(regex_size ".*INFO:size\\[0*([^]]*)\\].*")
  94. set(regex_key " key\\[([^]]*)\\]")
  95. set(keys)
  96. set(code)
  97. set(mismatch)
  98. set(first 1)
  99. foreach(info ${strings})
  100. if("${info}" MATCHES "${regex_size}")
  101. # Get the type size.
  102. string(REGEX REPLACE "${regex_size}" "\\1" size "${info}")
  103. if(first)
  104. set(${var} ${size})
  105. elseif(NOT "${size}" STREQUAL "${${var}}")
  106. set(mismatch 1)
  107. endif()
  108. set(first 0)
  109. # Get the architecture map key.
  110. string(REGEX MATCH "${regex_key}" key "${info}")
  111. string(REGEX REPLACE "${regex_key}" "\\1" key "${key}")
  112. if(key)
  113. set(code "${code}\nset(${var}-${key} \"${size}\")")
  114. list(APPEND keys ${key})
  115. endif()
  116. endif()
  117. endforeach()
  118. # Update the architecture-to-size map.
  119. if(mismatch AND keys)
  120. configure_file(${__check_type_size_dir}/CheckTypeSizeMap.cmake.in ${map} @ONLY)
  121. set(${var} 0)
  122. else()
  123. file(REMOVE ${map})
  124. endif()
  125. if(mismatch AND NOT keys)
  126. 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 !")
  127. endif()
  128. message(STATUS "Check size of ${type} - done")
  129. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  130. "Determining size of ${type} passed with the following output:\n${output}\n\n")
  131. set(${var} "${${var}}" CACHE INTERNAL "CHECK_TYPE_SIZE: sizeof(${type})")
  132. else()
  133. # The check failed to compile.
  134. message(STATUS "Check size of ${type} - failed")
  135. file(READ ${src} content)
  136. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  137. "Determining size of ${type} failed with the following output:\n${output}\n${src}:\n${content}\n\n")
  138. set(${var} "" CACHE INTERNAL "CHECK_TYPE_SIZE: ${type} unknown")
  139. file(REMOVE ${map})
  140. endif()
  141. endfunction()
  142. #-----------------------------------------------------------------------------
  143. macro(CHECK_TYPE_SIZE TYPE VARIABLE)
  144. # Optionally check for standard headers.
  145. if("${ARGV2}" STREQUAL "BUILTIN_TYPES_ONLY")
  146. set(_builtin 0)
  147. else()
  148. set(_builtin 1)
  149. check_include_file(sys/types.h HAVE_SYS_TYPES_H)
  150. check_include_file(stdint.h HAVE_STDINT_H)
  151. check_include_file(stddef.h HAVE_STDDEF_H)
  152. endif()
  153. # Compute or load the size or size map.
  154. set(${VARIABLE}_KEYS)
  155. set(_map_file ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${VARIABLE}.cmake)
  156. if(NOT DEFINED HAVE_${VARIABLE})
  157. __check_type_size_impl(${TYPE} ${VARIABLE} ${_map_file} ${_builtin})
  158. endif()
  159. include(${_map_file} OPTIONAL)
  160. set(_map_file)
  161. set(_builtin)
  162. # Create preprocessor code.
  163. if(${VARIABLE}_KEYS)
  164. set(${VARIABLE}_CODE)
  165. set(_if if)
  166. foreach(key ${${VARIABLE}_KEYS})
  167. set(${VARIABLE}_CODE "${${VARIABLE}_CODE}#${_if} defined(${key})\n# define ${VARIABLE} ${${VARIABLE}-${key}}\n")
  168. set(_if elif)
  169. endforeach()
  170. set(${VARIABLE}_CODE "${${VARIABLE}_CODE}#else\n# error ${VARIABLE} unknown\n#endif")
  171. set(_if)
  172. elseif(${VARIABLE})
  173. set(${VARIABLE}_CODE "#define ${VARIABLE} ${${VARIABLE}}")
  174. else()
  175. set(${VARIABLE}_CODE "/* #undef ${VARIABLE} */")
  176. endif()
  177. endmacro()
  178. #-----------------------------------------------------------------------------
  179. cmake_policy(POP)