CheckIncludeFileCXX.cmake 3.66 KB
Newer Older
1 2 3
#.rst:
# CheckIncludeFileCXX
# -------------------
4
#
5
# Provides a macro to check if a header file can be included in ``CXX``.
6
#
7
# .. command:: CHECK_INCLUDE_FILE_CXX
8
#
9
#   ::
10
#
11
#     CHECK_INCLUDE_FILE_CXX(<include> <variable> [<flags>])
12
#
13 14 15 16
#   Check if the given ``<include>`` file may be included in a ``CXX``
#   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).
17 18 19 20
#
# The following variables may be set before calling this macro to modify
# the way the check is run:
#
21 22 23 24 25 26 27 28 29 30 31
# ``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 modules :module:`CheckIncludeFile` and :module:`CheckIncludeFiles`
# to check for one or more ``C`` headers.
32 33 34 35 36 37 38 39 40 41 42

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

46
macro(CHECK_INCLUDE_FILE_CXX INCLUDE VARIABLE)
47
  if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
48 49
    if(CMAKE_REQUIRED_INCLUDES)
      set(CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
50
    else()
51
      set(CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS)
52
    endif()
53 54 55
    set(MACRO_CHECK_INCLUDE_FILE_FLAGS ${CMAKE_REQUIRED_FLAGS})
    set(CHECK_INCLUDE_FILE_VAR ${INCLUDE})
    configure_file(${CMAKE_ROOT}/Modules/CheckIncludeFile.cxx.in
56
      ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.cxx)
57 58 59
    if(NOT CMAKE_REQUIRED_QUIET)
      message(STATUS "Looking for C++ include ${INCLUDE}")
    endif()
60 61 62
    if(${ARGC} EQUAL 3)
      set(CMAKE_CXX_FLAGS_SAVE ${CMAKE_CXX_FLAGS})
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ARGV2}")
63
    endif()
64

65
    try_compile(${VARIABLE}
66
      ${CMAKE_BINARY_DIR}
67
      ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.cxx
68
      COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
69
      CMAKE_FLAGS
Andy Cedilnik's avatar
Andy Cedilnik committed
70
      -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILE_FLAGS}
71
      "${CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS}"
72
      OUTPUT_VARIABLE OUTPUT)
73

74 75
    if(${ARGC} EQUAL 3)
      set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_SAVE})
76
    endif()
77

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