Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / CheckIncludeFiles.cmake
1 # - Check if the files can be included
2 #
3 # CHECK_INCLUDE_FILES(INCLUDE VARIABLE)
4 #
5 #  INCLUDE  - list of files to include
6 #  VARIABLE - variable to return result
7 #
8 # The following variables may be set before calling this macro to
9 # modify the way the check is run:
10 #
11 #  CMAKE_REQUIRED_FLAGS = string of compile command line flags
12 #  CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
13 #  CMAKE_REQUIRED_INCLUDES = list of include directories
14
15 #=============================================================================
16 # Copyright 2003-2012 Kitware, Inc.
17 #
18 # Distributed under the OSI-approved BSD License (the "License");
19 # see accompanying file Copyright.txt for details.
20 #
21 # This software is distributed WITHOUT ANY WARRANTY; without even the
22 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 # See the License for more information.
24 #=============================================================================
25 # (To distribute this file outside of CMake, substitute the full
26 #  License text for the above reference.)
27
28 macro(CHECK_INCLUDE_FILES INCLUDE VARIABLE)
29   if("${VARIABLE}" MATCHES "^${VARIABLE}$")
30     set(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
31     if(CMAKE_REQUIRED_INCLUDES)
32       set(CHECK_INCLUDE_FILES_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
33     else()
34       set(CHECK_INCLUDE_FILES_INCLUDE_DIRS)
35     endif()
36     set(CHECK_INCLUDE_FILES_CONTENT "/* */\n")
37     set(MACRO_CHECK_INCLUDE_FILES_FLAGS ${CMAKE_REQUIRED_FLAGS})
38     foreach(FILE ${INCLUDE})
39       set(CMAKE_CONFIGURABLE_FILE_CONTENT
40         "${CMAKE_CONFIGURABLE_FILE_CONTENT}#include <${FILE}>\n")
41     endforeach()
42     set(CMAKE_CONFIGURABLE_FILE_CONTENT
43       "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n\nint main(){return 0;}\n")
44     configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
45       "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFiles.c" @ONLY IMMEDIATE)
46
47     set(_INCLUDE ${INCLUDE}) # remove empty elements
48     if("${_INCLUDE}" MATCHES "^([^;]+);.+;([^;]+)$")
49       list(LENGTH _INCLUDE _INCLUDE_LEN)
50       set(_description "${_INCLUDE_LEN} include files ${CMAKE_MATCH_1}, ..., ${CMAKE_MATCH_2}")
51     elseif("${_INCLUDE}" MATCHES "^([^;]+);([^;]+)$")
52       set(_description "include files ${CMAKE_MATCH_1}, ${CMAKE_MATCH_2}")
53     else()
54       set(_description "include file ${_INCLUDE}")
55     endif()
56
57     message(STATUS "Looking for ${_description}")
58     try_compile(${VARIABLE}
59       ${CMAKE_BINARY_DIR}
60       ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFiles.c
61       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
62       CMAKE_FLAGS
63       -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILES_FLAGS}
64       "${CHECK_INCLUDE_FILES_INCLUDE_DIRS}"
65       OUTPUT_VARIABLE OUTPUT)
66     if(${VARIABLE})
67       message(STATUS "Looking for ${_description} - found")
68       set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
69       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
70         "Determining if files ${INCLUDE} "
71         "exist passed with the following output:\n"
72         "${OUTPUT}\n\n")
73     else()
74       message(STATUS "Looking for ${_description} - not found")
75       set(${VARIABLE} "" CACHE INTERNAL "Have includes ${INCLUDE}")
76       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
77         "Determining if files ${INCLUDE} "
78         "exist failed with the following output:\n"
79         "${OUTPUT}\nSource:\n${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
80     endif()
81   endif()
82 endmacro()