1 # Copied from master branch of CMake (commit SHA 34a49dea) and
2 # modified to use CheckIncludeFileCXX instead of CheckIncludeFile
3 # when the LANGUAGE is CXX. Modified the try_compile call to
4 # not pass any LINK_LIBRARIES as this option is only supported by
5 # CMake since version 2.8.11
16 # CHECK_TYPE_SIZE(TYPE VARIABLE [BUILTIN_TYPES_ONLY]
17 # [LANGUAGE <language>])
19 # Check if the type exists and determine its size. On return,
20 # "HAVE_${VARIABLE}" holds the existence of the type, and "${VARIABLE}"
21 # holds one of the following:
25 # <size> = type has non-zero size <size>
26 # "0" = type has arch-dependent size (see below)
27 # "" = type does not exist
29 # Furthermore, the variable "${VARIABLE}_CODE" holds C preprocessor code
30 # to define the macro "${VARIABLE}" to the size of the type, or leave
31 # the macro undefined if the type does not exist.
33 # The variable "${VARIABLE}" may be "0" when CMAKE_OSX_ARCHITECTURES has
34 # multiple architectures for building OS X universal binaries. This
35 # indicates that the type size varies across architectures. In this
36 # case "${VARIABLE}_CODE" contains C preprocessor tests mapping from
37 # each architecture macro to the corresponding type size. The list of
38 # architecture macros is stored in "${VARIABLE}_KEYS", and the value for
39 # each key is stored in "${VARIABLE}-${KEY}".
41 # If the BUILTIN_TYPES_ONLY option is not given, the macro checks for
42 # headers <sys/types.h>, <stdint.h>, and <stddef.h>, and saves results
43 # in HAVE_SYS_TYPES_H, HAVE_STDINT_H, and HAVE_STDDEF_H. The type size
44 # check automatically includes the available headers, thus supporting
45 # checks of types defined in the headers.
47 # If LANGUAGE is set, the specified compiler will be used to perform the
48 # check. Acceptable values are C and CXX
50 # Despite the name of the macro you may use it to check the size of more
51 # complex expressions, too. To check e.g. for the size of a struct
52 # member you can do something like this:
56 # check_type_size("((struct something*)0)->member" SIZEOF_MEMBER)
60 # The following variables may be set before calling this macro to modify
61 # the way the check is run:
65 # CMAKE_REQUIRED_FLAGS = string of compile command line flags
66 # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
67 # CMAKE_REQUIRED_INCLUDES = list of include directories
68 # CMAKE_EXTRA_INCLUDE_FILES = list of extra headers to include
70 #=============================================================================
71 # Copyright 2002-2009 Kitware, Inc.
73 # Distributed under the OSI-approved BSD License (the "License");
74 # see accompanying file Copyright.txt for details.
76 # This software is distributed WITHOUT ANY WARRANTY; without even the
77 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
78 # See the License for more information.
79 #=============================================================================
80 # (To distribute this file outside of CMake, substitute the full
81 # License text for the above reference.)
83 include(CheckIncludeFile)
84 include(CheckIncludeFileCXX)
87 cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
89 get_filename_component(__check_type_size_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
91 #-----------------------------------------------------------------------------
92 # Helper function. DO NOT CALL DIRECTLY.
93 function(__check_type_size_impl type var map builtin language)
94 message(STATUS "Check size of ${type}")
96 # Include header files.
100 set(headers "${headers}#include <sys/types.h>\n")
103 set(headers "${headers}#include <stdint.h>\n")
106 set(headers "${headers}#include <stddef.h>\n")
109 foreach(h ${CMAKE_EXTRA_INCLUDE_FILES})
110 set(headers "${headers}#include \"${h}\"\n")
115 if("${language}" STREQUAL "C")
116 set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.c)
117 elseif("${language}" STREQUAL "CXX")
118 set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.cpp)
120 message(FATAL_ERROR "Unknown language:\n ${language}\nSupported languages: C, CXX.\n")
122 set(bin ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.bin)
123 configure_file(${__check_type_size_dir}/CheckTypeSize.c.in ${src} @ONLY)
124 try_compile(HAVE_${var} ${CMAKE_BINARY_DIR} ${src}
125 COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
127 "-DCOMPILE_DEFINITIONS:STRING=${CMAKE_REQUIRED_FLAGS}"
128 "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}"
129 OUTPUT_VARIABLE output
134 # The check compiled. Load information from the binary.
135 file(STRINGS ${bin} strings LIMIT_COUNT 10 REGEX "INFO:size")
137 # Parse the information strings.
138 set(regex_size ".*INFO:size\\[0*([^]]*)\\].*")
139 set(regex_key " key\\[([^]]*)\\]")
144 foreach(info ${strings})
145 if("${info}" MATCHES "${regex_size}")
147 string(REGEX REPLACE "${regex_size}" "\\1" size "${info}")
150 elseif(NOT "${size}" STREQUAL "${${var}}")
155 # Get the architecture map key.
156 string(REGEX MATCH "${regex_key}" key "${info}")
157 string(REGEX REPLACE "${regex_key}" "\\1" key "${key}")
159 set(code "${code}\nset(${var}-${key} \"${size}\")")
160 list(APPEND keys ${key})
165 # Update the architecture-to-size map.
166 if(mismatch AND keys)
167 configure_file(${__check_type_size_dir}/CheckTypeSizeMap.cmake.in ${map} @ONLY)
173 if(mismatch AND NOT keys)
174 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 !")
177 message(STATUS "Check size of ${type} - done")
178 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
179 "Determining size of ${type} passed with the following output:\n${output}\n\n")
180 set(${var} "${${var}}" CACHE INTERNAL "CHECK_TYPE_SIZE: sizeof(${type})")
182 # The check failed to compile.
183 message(STATUS "Check size of ${type} - failed")
184 file(READ ${src} content)
185 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
186 "Determining size of ${type} failed with the following output:\n${output}\n${src}:\n${content}\n\n")
187 set(${var} "" CACHE INTERNAL "CHECK_TYPE_SIZE: ${type} unknown")
192 #-----------------------------------------------------------------------------
193 macro(CHECK_TYPE_SIZE TYPE VARIABLE)
197 if("x${arg}" STREQUAL "xBUILTIN_TYPES_ONLY")
198 set(_CHECK_TYPE_SIZE_${arg} 1)
200 elseif("x${arg}" STREQUAL "xLANGUAGE") # change to MATCHES for more keys
202 set(_CHECK_TYPE_SIZE_${doing} "")
203 elseif("x${doing}" STREQUAL "xLANGUAGE")
204 set(_CHECK_TYPE_SIZE_${doing} "${arg}")
207 message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
210 if("x${doing}" MATCHES "^x(LANGUAGE)$")
211 message(FATAL_ERROR "Missing argument:\n ${doing} arguments requires a value\n")
213 if(DEFINED _CHECK_TYPE_SIZE_LANGUAGE)
214 if(NOT "x${_CHECK_TYPE_SIZE_LANGUAGE}" MATCHES "^x(C|CXX)$")
215 message(FATAL_ERROR "Unknown language:\n ${_CHECK_TYPE_SIZE_LANGUAGE}.\nSupported languages: C, CXX.\n")
217 set(_language ${_CHECK_TYPE_SIZE_LANGUAGE})
222 # Optionally check for standard headers.
223 if(_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)
227 if ("x${_CHECK_TYPE_SIZE_LANGUAGE}" STREQUAL "xCXX")
228 check_include_file_cxx(sys/types.h HAVE_SYS_TYPES_H)
229 check_include_file_cxx(stdint.h HAVE_STDINT_H)
230 check_include_file_cxx(stddef.h HAVE_STDDEF_H)
232 check_include_file(sys/types.h HAVE_SYS_TYPES_H)
233 check_include_file(stdint.h HAVE_STDINT_H)
234 check_include_file(stddef.h HAVE_STDDEF_H)
237 unset(_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)
238 unset(_CHECK_TYPE_SIZE_LANGUAGE)
240 # Compute or load the size or size map.
241 set(${VARIABLE}_KEYS)
242 set(_map_file ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${VARIABLE}.cmake)
243 if(NOT DEFINED HAVE_${VARIABLE})
244 __check_type_size_impl(${TYPE} ${VARIABLE} ${_map_file} ${_builtin} ${_language})
246 include(${_map_file} OPTIONAL)
250 # Create preprocessor code.
252 set(${VARIABLE}_CODE)
254 foreach(key ${${VARIABLE}_KEYS})
255 set(${VARIABLE}_CODE "${${VARIABLE}_CODE}#${_if} defined(${key})\n# define ${VARIABLE} ${${VARIABLE}-${key}}\n")
258 set(${VARIABLE}_CODE "${${VARIABLE}_CODE}#else\n# error ${VARIABLE} unknown\n#endif")
261 set(${VARIABLE}_CODE "#define ${VARIABLE} ${${VARIABLE}}")
263 set(${VARIABLE}_CODE "/* #undef ${VARIABLE} */")
267 #-----------------------------------------------------------------------------