cmake/Modules/CheckIncludeFile.cmake

90 lines
3.1 KiB
CMake
Raw Normal View History

2016-10-30 18:24:19 +01:00
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
2014-08-03 19:52:23 +02:00
#.rst:
# CheckIncludeFile
# ----------------
#
2015-08-17 11:37:30 +02:00
# Provides a macro to check if a header file can be included in ``C``.
2014-08-03 19:52:23 +02:00
#
2015-08-17 11:37:30 +02:00
# .. command:: CHECK_INCLUDE_FILE
2013-03-16 19:13:01 +02:00
#
2015-08-17 11:37:30 +02:00
# ::
2014-08-03 19:52:23 +02:00
#
2015-08-17 11:37:30 +02:00
# CHECK_INCLUDE_FILE(<include> <variable> [<flags>])
2014-08-03 19:52:23 +02:00
#
2015-08-17 11:37:30 +02:00
# 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).
#
2014-08-03 19:52:23 +02:00
# The following variables may be set before calling this macro to modify
# the way the check is run:
#
2015-08-17 11:37:30 +02:00
# ``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.
2009-10-04 10:30:41 +03:00
2013-03-16 19:13:01 +02:00
macro(CHECK_INCLUDE_FILE INCLUDE VARIABLE)
2015-04-27 22:25:09 +02:00
if(NOT DEFINED "${VARIABLE}")
2013-03-16 19:13:01 +02:00
if(CMAKE_REQUIRED_INCLUDES)
set(CHECK_INCLUDE_FILE_C_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
else()
set(CHECK_INCLUDE_FILE_C_INCLUDE_DIRS)
endif()
set(MACRO_CHECK_INCLUDE_FILE_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CHECK_INCLUDE_FILE_VAR ${INCLUDE})
configure_file(${CMAKE_ROOT}/Modules/CheckIncludeFile.c.in
2014-08-03 19:52:23 +02:00
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.c)
2015-04-27 22:25:09 +02:00
if(NOT CMAKE_REQUIRED_QUIET)
message(STATUS "Looking for ${INCLUDE}")
endif()
2013-03-16 19:13:01 +02:00
if(${ARGC} EQUAL 3)
set(CMAKE_C_FLAGS_SAVE ${CMAKE_C_FLAGS})
2016-10-30 18:24:19 +01:00
string(APPEND CMAKE_C_FLAGS " ${ARGV2}")
2013-03-16 19:13:01 +02:00
endif()
2013-03-16 19:13:01 +02:00
try_compile(${VARIABLE}
${CMAKE_BINARY_DIR}
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.c
COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
2013-03-16 19:13:01 +02:00
CMAKE_FLAGS
-DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILE_FLAGS}
"${CHECK_INCLUDE_FILE_C_INCLUDE_DIRS}"
2013-03-16 19:13:01 +02:00
OUTPUT_VARIABLE OUTPUT)
2013-03-16 19:13:01 +02:00
if(${ARGC} EQUAL 3)
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_SAVE})
endif()
2013-03-16 19:13:01 +02:00
if(${VARIABLE})
2015-04-27 22:25:09 +02:00
if(NOT CMAKE_REQUIRED_QUIET)
message(STATUS "Looking for ${INCLUDE} - found")
endif()
2013-03-16 19:13:01 +02:00
set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
"Determining if the include file ${INCLUDE} "
"exists passed with the following output:\n"
"${OUTPUT}\n\n")
2013-03-16 19:13:01 +02:00
else()
2015-04-27 22:25:09 +02:00
if(NOT CMAKE_REQUIRED_QUIET)
message(STATUS "Looking for ${INCLUDE} - not found")
endif()
2013-03-16 19:13:01 +02:00
set(${VARIABLE} "" CACHE INTERNAL "Have include ${INCLUDE}")
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Determining if the include file ${INCLUDE} "
"exists failed with the following output:\n"
"${OUTPUT}\n\n")
2013-03-16 19:13:01 +02:00
endif()
endif()
endmacro()