CheckIncludeFile.cmake 3.6 KB
Newer Older
1 2 3 4
#.rst:
# CheckIncludeFile
# ----------------
#
5
# Provides a macro to check if a header file can be included in ``C``.
6
#
7
# .. command:: CHECK_INCLUDE_FILE
8
#
9
#   ::
10
#
11
#     CHECK_INCLUDE_FILE(<include> <variable> [<flags>])
12
#
13 14 15 16
#   Check if the given ``<include>`` file may be included in a ``C``
#   source file and store the result in an internal cache entry named
#   ``<variable>``.  The optional third argument may be used to add
#   compilation flags to the check (or use ``CMAKE_REQUIRED_FLAGS`` below).
Ken Martin's avatar
Ken Martin committed
17
#
18 19
# The following variables may be set before calling this macro to modify
# the way the check is run:
20
#
21 22 23 24 25 26 27 28 29 30 31 32
# ``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
# ``CMAKE_REQUIRED_QUIET``
#   execute quietly without messages
#
# See the :module:`CheckIncludeFiles` module to check for multiple headers
# at once.  See the :module:`CheckIncludeFileCXX` module to check for headers
# using the ``CXX`` language.
33 34 35 36 37 38 39 40 41 42 43

#=============================================================================
# Copyright 2002-2009 Kitware, Inc.
#
# 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.
#=============================================================================
44
# (To distribute this file outside of CMake, substitute the full
45 46
#  License text for the above reference.)

47
macro(CHECK_INCLUDE_FILE INCLUDE VARIABLE)
48
  if(NOT DEFINED "${VARIABLE}")
49 50
    if(CMAKE_REQUIRED_INCLUDES)
      set(CHECK_INCLUDE_FILE_C_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
51
    else()
52
      set(CHECK_INCLUDE_FILE_C_INCLUDE_DIRS)
53
    endif()
54 55 56
    set(MACRO_CHECK_INCLUDE_FILE_FLAGS ${CMAKE_REQUIRED_FLAGS})
    set(CHECK_INCLUDE_FILE_VAR ${INCLUDE})
    configure_file(${CMAKE_ROOT}/Modules/CheckIncludeFile.c.in
57
      ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.c)
58 59 60
    if(NOT CMAKE_REQUIRED_QUIET)
      message(STATUS "Looking for ${INCLUDE}")
    endif()
61 62 63
    if(${ARGC} EQUAL 3)
      set(CMAKE_C_FLAGS_SAVE ${CMAKE_C_FLAGS})
      set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${ARGV2}")
64
    endif()
65

66
    try_compile(${VARIABLE}
67
      ${CMAKE_BINARY_DIR}
68
      ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.c
69
      COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
70
      CMAKE_FLAGS
71
      -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILE_FLAGS}
72
      "${CHECK_INCLUDE_FILE_C_INCLUDE_DIRS}"
73
      OUTPUT_VARIABLE OUTPUT)
74

75 76
    if(${ARGC} EQUAL 3)
      set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_SAVE})
77
    endif()
78

79
    if(${VARIABLE})
80 81 82
      if(NOT CMAKE_REQUIRED_QUIET)
        message(STATUS "Looking for ${INCLUDE} - found")
      endif()
83 84
      set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
85 86 87
        "Determining if the include file ${INCLUDE} "
        "exists passed with the following output:\n"
        "${OUTPUT}\n\n")
88
    else()
89 90 91
      if(NOT CMAKE_REQUIRED_QUIET)
        message(STATUS "Looking for ${INCLUDE} - not found")
      endif()
92 93
      set(${VARIABLE} "" CACHE INTERNAL "Have include ${INCLUDE}")
      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
94 95
        "Determining if the include file ${INCLUDE} "
        "exists failed with the following output:\n"
96
        "${OUTPUT}\n\n")
97 98 99
    endif()
  endif()
endmacro()