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:
8 Provides a macro to check if a list of one or more header files can
11 .. command:: CHECK_INCLUDE_FILES
15 CHECK_INCLUDE_FILES("<includes>" <variable> [LANGUAGE <language>])
17 Check if the given ``<includes>`` list may be included together
18 in a source file and store the result in an internal cache
19 entry named ``<variable>``. Specify the ``<includes>`` argument
20 as a :ref:`;-list <CMake Language Lists>` of header file names.
22 If ``LANGUAGE`` is set, the specified compiler will be used to perform the
23 check. Acceptable values are ``C`` and ``CXX``. If not set, the C compiler
24 will be used if enabled. If the C compiler is not enabled, the C++
25 compiler will be used if enabled.
27 The following variables may be set before calling this macro to modify
28 the way the check is run:
30 ``CMAKE_REQUIRED_FLAGS``
31 string of compile command line flags.
32 ``CMAKE_REQUIRED_DEFINITIONS``
33 a :ref:`;-list <CMake Language Lists>` of macros to define (-DFOO=bar).
34 ``CMAKE_REQUIRED_INCLUDES``
35 a :ref:`;-list <CMake Language Lists>` of header search paths to pass to
37 ``CMAKE_REQUIRED_LINK_OPTIONS``
38 .. versionadded:: 3.14
39 a :ref:`;-list <CMake Language Lists>` of options to add to the link command.
40 ``CMAKE_REQUIRED_LIBRARIES``
41 a :ref:`;-list <CMake Language Lists>` of libraries to add to the link
42 command. See policy :policy:`CMP0075`.
43 ``CMAKE_REQUIRED_QUIET``
45 execute quietly without messages.
47 See modules :module:`CheckIncludeFile` and :module:`CheckIncludeFileCXX`
48 to check for a single header file in ``C`` or ``CXX`` languages.
49 #]=======================================================================]
53 macro(CHECK_INCLUDE_FILES INCLUDE VARIABLE)
54 if(NOT DEFINED "${VARIABLE}")
55 set(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
57 if("x${ARGN}" STREQUAL "x")
58 if(CMAKE_C_COMPILER_LOADED)
60 elseif(CMAKE_CXX_COMPILER_LOADED)
63 message(FATAL_ERROR "CHECK_INCLUDE_FILES needs either C or CXX language enabled.\n")
65 elseif("x${ARGN}" MATCHES "^xLANGUAGE;([a-zA-Z]+)$")
66 set(_lang "${CMAKE_MATCH_1}")
67 elseif("x${ARGN}" MATCHES "^xLANGUAGE$")
68 message(FATAL_ERROR "No languages listed for LANGUAGE option.\nSupported languages: C, CXX.\n")
70 message(FATAL_ERROR "Unknown arguments:\n ${ARGN}\n")
73 if(_lang STREQUAL "C")
74 set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckIncludeFiles/${VARIABLE}.c)
75 elseif(_lang STREQUAL "CXX")
76 set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckIncludeFiles/${VARIABLE}.cpp)
78 message(FATAL_ERROR "Unknown language:\n ${_lang}\nSupported languages: C, CXX.\n")
81 if(CMAKE_REQUIRED_INCLUDES)
82 set(CHECK_INCLUDE_FILES_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
84 set(CHECK_INCLUDE_FILES_INCLUDE_DIRS)
86 set(CHECK_INCLUDE_FILES_CONTENT "/* */\n")
87 set(MACRO_CHECK_INCLUDE_FILES_FLAGS ${CMAKE_REQUIRED_FLAGS})
88 foreach(FILE ${INCLUDE})
89 string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
90 "#include <${FILE}>\n")
92 string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
93 "\n\nint main(void){return 0;}\n")
94 configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
97 set(_INCLUDE ${INCLUDE}) # remove empty elements
98 if("${_INCLUDE}" MATCHES "^([^;]+);.+;([^;]+)$")
99 list(LENGTH _INCLUDE _INCLUDE_LEN)
100 set(_description "${_INCLUDE_LEN} include files ${CMAKE_MATCH_1}, ..., ${CMAKE_MATCH_2}")
101 elseif("${_INCLUDE}" MATCHES "^([^;]+);([^;]+)$")
102 set(_description "include files ${CMAKE_MATCH_1}, ${CMAKE_MATCH_2}")
104 set(_description "include file ${_INCLUDE}")
107 set(_CIF_LINK_OPTIONS)
108 if(CMAKE_REQUIRED_LINK_OPTIONS)
109 set(_CIF_LINK_OPTIONS LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
112 set(_CIF_LINK_LIBRARIES "")
113 if(CMAKE_REQUIRED_LIBRARIES)
114 cmake_policy(GET CMP0075 _CIF_CMP0075
115 PARENT_SCOPE # undocumented, do not use outside of CMake
117 if("x${_CIF_CMP0075}x" STREQUAL "xNEWx")
118 set(_CIF_LINK_LIBRARIES LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
119 elseif("x${_CIF_CMP0075}x" STREQUAL "xOLDx")
120 elseif(NOT _CIF_CMP0075_WARNED)
121 set(_CIF_CMP0075_WARNED 1)
122 message(AUTHOR_WARNING
123 "Policy CMP0075 is not set: Include file check macros honor CMAKE_REQUIRED_LIBRARIES. "
124 "Run \"cmake --help-policy CMP0075\" for policy details. "
125 "Use the cmake_policy command to set the policy and suppress this warning."
127 "CMAKE_REQUIRED_LIBRARIES is set to:\n"
128 " ${CMAKE_REQUIRED_LIBRARIES}\n"
129 "For compatibility with CMake 3.11 and below this check is ignoring it."
135 if(NOT CMAKE_REQUIRED_QUIET)
136 message(CHECK_START "Looking for ${_description}")
138 try_compile(${VARIABLE}
141 COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
143 ${_CIF_LINK_LIBRARIES}
145 -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILES_FLAGS}
146 "${CHECK_INCLUDE_FILES_INCLUDE_DIRS}"
147 OUTPUT_VARIABLE OUTPUT)
148 unset(_CIF_LINK_OPTIONS)
149 unset(_CIF_LINK_LIBRARIES)
151 if(NOT CMAKE_REQUIRED_QUIET)
152 message(CHECK_PASS "found")
154 set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
155 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
156 "Determining if files ${INCLUDE} "
157 "exist passed with the following output:\n"
160 if(NOT CMAKE_REQUIRED_QUIET)
161 message(CHECK_FAIL "not found")
163 set(${VARIABLE} "" CACHE INTERNAL "Have includes ${INCLUDE}")
164 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
165 "Determining if files ${INCLUDE} "
166 "exist failed with the following output:\n"
167 "${OUTPUT}\nSource:\n${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")