CheckIncludeFiles.cmake 3.6 KB
Newer Older
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
9
10
11
12
13
#
# The following variables may be set before calling this macro to
# modify the way the check is run:
#
#  CMAKE_REQUIRED_FLAGS = string of compile command line flags
#  CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
#  CMAKE_REQUIRED_INCLUDES = list of include directories
14

15
#=============================================================================
16
# Copyright 2003-2012 Kitware, Inc.
17
18
19
20
21
22
23
24
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
25
# (To distribute this file outside of CMake, substitute the full
26
27
#  License text for the above reference.)

28
29
MACRO(CHECK_INCLUDE_FILES INCLUDE VARIABLE)
  IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
30
    SET(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
31
32
33
34
35
    IF(CMAKE_REQUIRED_INCLUDES)
      SET(CHECK_INCLUDE_FILES_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
    ELSE(CMAKE_REQUIRED_INCLUDES)
      SET(CHECK_INCLUDE_FILES_INCLUDE_DIRS)
    ENDIF(CMAKE_REQUIRED_INCLUDES)
36
    SET(CHECK_INCLUDE_FILES_CONTENT "/* */\n")
Andy Cedilnik's avatar
Andy Cedilnik committed
37
    SET(MACRO_CHECK_INCLUDE_FILES_FLAGS ${CMAKE_REQUIRED_FLAGS})
38
    FOREACH(FILE ${INCLUDE})
39
40
      SET(CMAKE_CONFIGURABLE_FILE_CONTENT
        "${CMAKE_CONFIGURABLE_FILE_CONTENT}#include <${FILE}>\n")
41
    ENDFOREACH(FILE)
42
43
44
    SET(CMAKE_CONFIGURABLE_FILE_CONTENT
      "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n\nint main(){return 0;}\n")
    CONFIGURE_FILE("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
45
      "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFiles.c" @ONLY IMMEDIATE)
46

47
48
49
50
51
52
53
54
55
56
57
    SET(_INCLUDE ${INCLUDE}) # remove empty elements
    IF("${_INCLUDE}" MATCHES "^([^;]+);.+;([^;]+)$")
      LIST(LENGTH _INCLUDE _INCLUDE_LEN)
      SET(_description "${_INCLUDE_LEN} include files ${CMAKE_MATCH_1}, ..., ${CMAKE_MATCH_2}")
    ELSEIF("${_INCLUDE}" MATCHES "^([^;]+);([^;]+)$")
      SET(_description "include files ${CMAKE_MATCH_1}, ${CMAKE_MATCH_2}")
    ELSE()
      SET(_description "include file ${_INCLUDE}")
    ENDIF()

    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
63
      CMAKE_FLAGS 
      -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
71
72
        "Determining if files ${INCLUDE} "
        "exist passed with the following output:\n"
        "${OUTPUT}\n\n")
73
    ELSE(${VARIABLE})
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
78
        "Determining if files ${INCLUDE} "
        "exist failed with the following output:\n"
79
        "${OUTPUT}\nSource:\n${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
80
81
82
    ENDIF(${VARIABLE})
  ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
ENDMACRO(CHECK_INCLUDE_FILES)