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:
5CheckIncludeFiles
6-----------------
7
8Provides a macro to check if a list of one or more header files can
9be 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
27The following variables may be set before calling this macro to modify
28the 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
47See modules :module:`CheckIncludeFile` and :module:`CheckIncludeFileCXX`
48to check for a single header file in ``C`` or ``CXX`` languages.
49#]=======================================================================]
50
51include_guard(GLOBAL)
52
53macro(CHECK_INCLUDE_FILES INCLUDE VARIABLE)
54  if(NOT DEFINED "${VARIABLE}")
55    set(CMAKE_CONFIGURABLE_FILE_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 ${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)
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 CMAKE_CONFIGURABLE_FILE_CONTENT
90        "#include <${FILE}>\n")
91    endforeach()
92    string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
93      "\n\nint main(void){return 0;}\n")
94    configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
95      "${src}" @ONLY)
96
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}")
103    else()
104      set(_description "include file ${_INCLUDE}")
105    endif()
106
107    set(_CIF_LINK_OPTIONS)
108    if(CMAKE_REQUIRED_LINK_OPTIONS)
109      set(_CIF_LINK_OPTIONS LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
110    endif()
111
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
116        )
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."
126          "\n"
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."
130          )
131      endif()
132      unset(_CIF_CMP0075)
133    endif()
134
135    if(NOT CMAKE_REQUIRED_QUIET)
136      message(CHECK_START "Looking for ${_description}")
137    endif()
138    try_compile(${VARIABLE}
139      ${CMAKE_BINARY_DIR}
140      ${src}
141      COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
142      ${_CIF_LINK_OPTIONS}
143      ${_CIF_LINK_LIBRARIES}
144      CMAKE_FLAGS
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)
150    if(${VARIABLE})
151      if(NOT CMAKE_REQUIRED_QUIET)
152        message(CHECK_PASS "found")
153      endif()
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"
158        "${OUTPUT}\n\n")
159    else()
160      if(NOT CMAKE_REQUIRED_QUIET)
161        message(CHECK_FAIL "not found")
162      endif()
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")
168    endif()
169  endif()
170endmacro()
171