CheckTypeSize.cmake 7.3 KB

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