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(_src_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 ${VARIABLE}.c)
75 elseif(_lang STREQUAL "CXX")
76 set(src ${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 _src_content
90 "#include <${FILE}>\n")
92 string(APPEND _src_content
93 "\n\nint main(void){return 0;}\n")
95 set(_INCLUDE ${INCLUDE}) # remove empty elements
96 if("${_INCLUDE}" MATCHES "^([^;]+);.+;([^;]+)$")
97 list(LENGTH _INCLUDE _INCLUDE_LEN)
98 set(_description "${_INCLUDE_LEN} include files ${CMAKE_MATCH_1}, ..., ${CMAKE_MATCH_2}")
99 elseif("${_INCLUDE}" MATCHES "^([^;]+);([^;]+)$")
100 set(_description "include files ${CMAKE_MATCH_1}, ${CMAKE_MATCH_2}")
102 set(_description "include file ${_INCLUDE}")
105 set(_CIF_LINK_OPTIONS)
106 if(CMAKE_REQUIRED_LINK_OPTIONS)
107 set(_CIF_LINK_OPTIONS LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
110 set(_CIF_LINK_LIBRARIES "")
111 if(CMAKE_REQUIRED_LIBRARIES)
112 cmake_policy(GET CMP0075 _CIF_CMP0075
113 PARENT_SCOPE # undocumented, do not use outside of CMake
115 if("x${_CIF_CMP0075}x" STREQUAL "xNEWx")
116 set(_CIF_LINK_LIBRARIES LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
117 elseif("x${_CIF_CMP0075}x" STREQUAL "xOLDx")
118 elseif(NOT _CIF_CMP0075_WARNED)
119 set(_CIF_CMP0075_WARNED 1)
120 message(AUTHOR_WARNING
121 "Policy CMP0075 is not set: Include file check macros honor CMAKE_REQUIRED_LIBRARIES. "
122 "Run \"cmake --help-policy CMP0075\" for policy details. "
123 "Use the cmake_policy command to set the policy and suppress this warning."
125 "CMAKE_REQUIRED_LIBRARIES is set to:\n"
126 " ${CMAKE_REQUIRED_LIBRARIES}\n"
127 "For compatibility with CMake 3.11 and below this check is ignoring it."
133 if(NOT CMAKE_REQUIRED_QUIET)
134 message(CHECK_START "Looking for ${_description}")
136 try_compile(${VARIABLE}
137 SOURCE_FROM_VAR "${src}" _src_content
138 COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
140 ${_CIF_LINK_LIBRARIES}
142 -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILES_FLAGS}
143 "${CHECK_INCLUDE_FILES_INCLUDE_DIRS}"
144 OUTPUT_VARIABLE OUTPUT)
145 unset(_CIF_LINK_OPTIONS)
146 unset(_CIF_LINK_LIBRARIES)
148 if(NOT CMAKE_REQUIRED_QUIET)
149 message(CHECK_PASS "found")
151 set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
152 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
153 "Determining if files ${INCLUDE} "
154 "exist passed with the following output:\n"
157 if(NOT CMAKE_REQUIRED_QUIET)
158 message(CHECK_FAIL "not found")
160 set(${VARIABLE} "" CACHE INTERNAL "Have includes ${INCLUDE}")
161 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
162 "Determining if files ${INCLUDE} "
163 "exist failed with the following output:\n"
164 "${OUTPUT}\nSource:\n${_src_content}\n")