Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Modules / CheckIncludeFiles.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #[=======================================================================[.rst:
5 CheckIncludeFiles
6 -----------------
7
8 Provides a macro to check if a list of one or more header files can
9 be included together.
10
11 .. command:: CHECK_INCLUDE_FILES
12
13   .. code-block:: cmake
14
15     CHECK_INCLUDE_FILES("<includes>" <variable> [LANGUAGE <language>])
16
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.
21
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.
26
27 The following variables may be set before calling this macro to modify
28 the way the check is run:
29
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
36   the compiler.
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``
44   .. versionadded:: 3.1
45     execute quietly without messages.
46
47 See modules :module:`CheckIncludeFile` and :module:`CheckIncludeFileCXX`
48 to check for a single header file in ``C`` or ``CXX`` languages.
49 #]=======================================================================]
50
51 include_guard(GLOBAL)
52
53 macro(CHECK_INCLUDE_FILES INCLUDE VARIABLE)
54   if(NOT DEFINED "${VARIABLE}")
55     set(_src_content "/* */\n")
56
57     if("x${ARGN}" STREQUAL "x")
58        if(CMAKE_C_COMPILER_LOADED)
59          set(_lang C)
60        elseif(CMAKE_CXX_COMPILER_LOADED)
61          set(_lang CXX)
62        else()
63          message(FATAL_ERROR "CHECK_INCLUDE_FILES needs either C or CXX language enabled.\n")
64        endif()
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")
69     else()
70       message(FATAL_ERROR "Unknown arguments:\n  ${ARGN}\n")
71     endif()
72
73     if(_lang STREQUAL "C")
74       set(src ${VARIABLE}.c)
75     elseif(_lang STREQUAL "CXX")
76       set(src ${VARIABLE}.cpp)
77     else()
78       message(FATAL_ERROR "Unknown language:\n  ${_lang}\nSupported languages: C, CXX.\n")
79     endif()
80
81     if(CMAKE_REQUIRED_INCLUDES)
82       set(CHECK_INCLUDE_FILES_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
83     else()
84       set(CHECK_INCLUDE_FILES_INCLUDE_DIRS)
85     endif()
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")
91     endforeach()
92     string(APPEND _src_content
93       "\n\nint main(void){return 0;}\n")
94
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}")
101     else()
102       set(_description "include file ${_INCLUDE}")
103     endif()
104
105     set(_CIF_LINK_OPTIONS)
106     if(CMAKE_REQUIRED_LINK_OPTIONS)
107       set(_CIF_LINK_OPTIONS LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
108     endif()
109
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
114         )
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."
124           "\n"
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."
128           )
129       endif()
130       unset(_CIF_CMP0075)
131     endif()
132
133     if(NOT CMAKE_REQUIRED_QUIET)
134       message(CHECK_START "Looking for ${_description}")
135     endif()
136     try_compile(${VARIABLE}
137       SOURCE_FROM_VAR "${src}" _src_content
138       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
139       ${_CIF_LINK_OPTIONS}
140       ${_CIF_LINK_LIBRARIES}
141       CMAKE_FLAGS
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)
147     if(${VARIABLE})
148       if(NOT CMAKE_REQUIRED_QUIET)
149         message(CHECK_PASS "found")
150       endif()
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"
155         "${OUTPUT}\n\n")
156     else()
157       if(NOT CMAKE_REQUIRED_QUIET)
158         message(CHECK_FAIL "not found")
159       endif()
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")
165     endif()
166   endif()
167 endmacro()