1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
10 .. command:: check_type_size
14 check_type_size(<type> <variable> [BUILTIN_TYPES_ONLY]
15 [LANGUAGE <language>])
17 Check if the type exists and determine its size. Results are reported
18 in the following variables:
21 Holds a true or false value indicating whether the type exists.
24 Holds one of the following values:
27 Type has non-zero size ``<size>``.
30 Type has architecture-dependent size. This may occur when
31 :variable:`CMAKE_OSX_ARCHITECTURES` has multiple architectures.
32 In this case ``<variable>_CODE`` contains C preprocessor tests
33 mapping from each architecture macro to the corresponding type size.
34 The list of architecture macros is stored in ``<variable>_KEYS``,
35 and the value for each key is stored in ``<variable>-<key>``.
41 Holds C preprocessor code to define the macro ``<variable>`` to the size
42 of the type, or to leave the macro undefined if the type does not exist.
46 ``BUILTIN_TYPES_ONLY``
48 Support only compiler-builtin types. If *not* given, the macro checks
49 for headers ``<sys/types.h>``, ``<stdint.h>``, and ``<stddef.h>``, and
50 saves results in ``HAVE_SYS_TYPES_H``, ``HAVE_STDINT_H``, and
51 ``HAVE_STDDEF_H``. The type size check automatically includes the
52 available headers, thus supporting checks of types defined in the headers.
54 ``LANGUAGE <language>``
55 Use the ``<language>`` compiler to perform the check.
56 Acceptable values are ``C`` and ``CXX``.
58 Despite the name of the macro you may use it to check the size of more
59 complex expressions, too. To check e.g. for the size of a struct
60 member you can do something like this:
64 check_type_size("((struct something*)0)->member" SIZEOF_MEMBER)
67 The following variables may be set before calling this macro to modify
68 the way the check is run:
70 ``CMAKE_REQUIRED_FLAGS``
71 string of compile command line flags.
72 ``CMAKE_REQUIRED_DEFINITIONS``
73 list of macros to define (-DFOO=bar).
74 ``CMAKE_REQUIRED_INCLUDES``
75 list of include directories.
76 ``CMAKE_REQUIRED_LINK_OPTIONS``
77 .. versionadded:: 3.14
78 list of options to pass to link command.
79 ``CMAKE_REQUIRED_LIBRARIES``
80 list of libraries to link.
81 ``CMAKE_REQUIRED_QUIET``
83 execute quietly without messages.
84 ``CMAKE_EXTRA_INCLUDE_FILES``
85 list of extra headers to include.
86 #]=======================================================================]
88 include(CheckIncludeFile)
89 include(CheckIncludeFileCXX)
91 get_filename_component(__check_type_size_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
96 cmake_policy(SET CMP0054 NEW)
98 #-----------------------------------------------------------------------------
99 # Helper function. DO NOT CALL DIRECTLY.
100 function(__check_type_size_impl type var map builtin language)
101 if(NOT CMAKE_REQUIRED_QUIET)
102 message(CHECK_START "Check size of ${type}")
105 # Perform language check
106 if(language STREQUAL "C")
108 elseif(language STREQUAL "CXX")
111 message(FATAL_ERROR "Unknown language:\n ${language}\nSupported languages: C, CXX.\n")
114 # Include header files.
117 if(language STREQUAL "CXX" AND type MATCHES "^std::")
119 string(APPEND headers "#include <sys/types.h>\n")
122 string(APPEND headers "#include <cstdint>\n")
125 string(APPEND headers "#include <cstddef>\n")
129 string(APPEND headers "#include <sys/types.h>\n")
132 string(APPEND headers "#include <stdint.h>\n")
135 string(APPEND headers "#include <stddef.h>\n")
139 foreach(h ${CMAKE_EXTRA_INCLUDE_FILES})
140 string(APPEND headers "#include \"${h}\"\n")
144 set(bin ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.bin)
145 file(READ ${__check_type_size_dir}/CheckTypeSize.c.in src_content)
146 string(CONFIGURE "${src_content}" src_content @ONLY)
147 try_compile(HAVE_${var} SOURCE_FROM_VAR "${src}" src_content
148 COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
149 LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS}
150 LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}
152 "-DCOMPILE_DEFINITIONS:STRING=${CMAKE_REQUIRED_FLAGS}"
153 "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}"
154 OUTPUT_VARIABLE output
159 # The check compiled. Load information from the binary.
160 file(STRINGS ${bin} strings LIMIT_COUNT 10 REGEX "INFO:size")
162 # Parse the information strings.
163 set(regex_size ".*INFO:size\\[0*([^]]*)\\].*")
164 set(regex_key " key\\[([^]]*)\\]")
169 foreach(info ${strings})
170 if("${info}" MATCHES "${regex_size}")
172 set(size "${CMAKE_MATCH_1}")
175 elseif(NOT "${size}" STREQUAL "${${var}}")
180 # Get the architecture map key.
181 string(REGEX MATCH "${regex_key}" key "${info}")
182 string(REGEX REPLACE "${regex_key}" "\\1" key "${key}")
184 string(APPEND code "\nset(${var}-${key} \"${size}\")")
185 list(APPEND keys ${key})
190 # Update the architecture-to-size map.
191 if(mismatch AND keys)
192 configure_file(${__check_type_size_dir}/CheckTypeSizeMap.cmake.in ${map} @ONLY)
198 if(mismatch AND NOT keys)
199 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 !")
202 if(NOT CMAKE_REQUIRED_QUIET)
203 message(CHECK_PASS "done")
205 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
206 "Determining size of ${type} passed with the following output:\n${output}\n\n")
207 set(${var} "${${var}}" CACHE INTERNAL "CHECK_TYPE_SIZE: sizeof(${type})")
209 # The check failed to compile.
210 if(NOT CMAKE_REQUIRED_QUIET)
211 message(CHECK_FAIL "failed")
213 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
214 "Determining size of ${type} failed with the following output:\n${output}\n${src}:\n${src_content}\n\n")
215 set(${var} "" CACHE INTERNAL "CHECK_TYPE_SIZE: ${type} unknown")
220 #-----------------------------------------------------------------------------
221 macro(CHECK_TYPE_SIZE TYPE VARIABLE)
225 if("x${arg}" STREQUAL "xBUILTIN_TYPES_ONLY")
226 set(_CHECK_TYPE_SIZE_${arg} 1)
228 elseif("x${arg}" STREQUAL "xLANGUAGE") # change to MATCHES for more keys
230 set(_CHECK_TYPE_SIZE_${doing} "")
231 elseif("x${doing}" STREQUAL "xLANGUAGE")
232 set(_CHECK_TYPE_SIZE_${doing} "${arg}")
235 message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
238 if("x${doing}" MATCHES "^x(LANGUAGE)$")
239 message(FATAL_ERROR "Missing argument:\n ${doing} arguments requires a value\n")
241 if(DEFINED _CHECK_TYPE_SIZE_LANGUAGE)
242 if(NOT "x${_CHECK_TYPE_SIZE_LANGUAGE}" MATCHES "^x(C|CXX)$")
243 message(FATAL_ERROR "Unknown language:\n ${_CHECK_TYPE_SIZE_LANGUAGE}.\nSupported languages: C, CXX.\n")
245 set(_language ${_CHECK_TYPE_SIZE_LANGUAGE})
250 # Optionally check for standard headers.
251 if(_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)
255 if(_language STREQUAL "C")
256 check_include_file(sys/types.h HAVE_SYS_TYPES_H)
257 check_include_file(stdint.h HAVE_STDINT_H)
258 check_include_file(stddef.h HAVE_STDDEF_H)
259 elseif(_language STREQUAL "CXX")
260 check_include_file_cxx(sys/types.h HAVE_SYS_TYPES_H)
261 if("${TYPE}" MATCHES "^std::")
262 check_include_file_cxx(cstdint HAVE_CSTDINT)
263 check_include_file_cxx(cstddef HAVE_CSTDDEF)
265 check_include_file_cxx(stdint.h HAVE_STDINT_H)
266 check_include_file_cxx(stddef.h HAVE_STDDEF_H)
270 unset(_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)
271 unset(_CHECK_TYPE_SIZE_LANGUAGE)
273 # Compute or load the size or size map.
274 set(${VARIABLE}_KEYS)
275 set(_map_file ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${VARIABLE}.cmake)
276 if(NOT DEFINED HAVE_${VARIABLE})
277 __check_type_size_impl(${TYPE} ${VARIABLE} ${_map_file} ${_builtin} ${_language})
279 include(${_map_file} OPTIONAL)
283 # Create preprocessor code.
285 set(${VARIABLE}_CODE)
287 foreach(key ${${VARIABLE}_KEYS})
288 string(APPEND ${VARIABLE}_CODE "#${_if} defined(${key})\n# define ${VARIABLE} ${${VARIABLE}-${key}}\n")
291 string(APPEND ${VARIABLE}_CODE "#else\n# error ${VARIABLE} unknown\n#endif")
294 set(${VARIABLE}_CODE "#define ${VARIABLE} ${${VARIABLE}}")
296 set(${VARIABLE}_CODE "/* #undef ${VARIABLE} */")
300 #-----------------------------------------------------------------------------