1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-19 19:43:23 +08:00

CheckIncludeFiles: extend to allow a LANGUAGE argument

Allows CheckIncludeFiles to work for C or C++.
This commit is contained in:
David Adam
2017-11-08 23:01:42 +08:00
parent a7347c022e
commit 7669695a74

View File

@@ -12,13 +12,16 @@
# #
# :: # ::
# #
# CHECK_INCLUDE_FILES("<includes>" <variable>) # CHECK_INCLUDE_FILES("<includes>" <variable> [LANGUAGE <language>])
# #
# Check if the given ``<includes>`` list may be included together # Check if the given ``<includes>`` list may be included together
# in a ``C`` source file and store the result in an internal cache # in a ``C`` source file and store the result in an internal cache
# entry named ``<variable>``. Specify the ``<includes>`` argument # entry named ``<variable>``. Specify the ``<includes>`` argument
# as a :ref:`;-list <CMake Language Lists>` of header file names. # as a :ref:`;-list <CMake Language Lists>` of header file names.
# #
# If LANGUAGE is set, the specified compiler will be used to perform the
# check. Acceptable values are C and CXX.
#
# The following variables may be set before calling this macro to modify # The following variables may be set before calling this macro to modify
# the way the check is run: # the way the check is run:
# #
@@ -37,6 +40,29 @@
macro(CHECK_INCLUDE_FILES INCLUDE VARIABLE) macro(CHECK_INCLUDE_FILES INCLUDE VARIABLE)
if(NOT DEFINED "${VARIABLE}") if(NOT DEFINED "${VARIABLE}")
set(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n") set(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
if("x${ARGN}" STREQUAL "x")
if(CMAKE_C_COMPILER_LOADED)
set(_lang C)
elseif(CMAKE_CXX_COMPILER_LOADED)
set(_lang CXX)
else()
message(FATAL_ERROR "CHECK_INCLUDE_FILES needs either C or CXX language enabled")
endif()
elseif("x${ARGN}" MATCHES "^xLANGUAGE;([a-zA-Z]+)$")
set(_lang "${CMAKE_MATCH_1}")
else()
message(FATAL_ERROR "Unknown arguments:\n ${ARGN}\n")
endif()
if(_lang STREQUAL "C")
set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckIncludeFiles/${var}.c)
elseif(_lang STREQUAL "CXX")
set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckIncludeFiles/${var}.cpp)
else()
message(FATAL_ERROR "Unknown language:\n ${_lang}\nSupported languages: C, CXX.\n")
endif()
if(CMAKE_REQUIRED_INCLUDES) if(CMAKE_REQUIRED_INCLUDES)
set(CHECK_INCLUDE_FILES_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}") set(CHECK_INCLUDE_FILES_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
else() else()
@@ -51,7 +77,7 @@ macro(CHECK_INCLUDE_FILES INCLUDE VARIABLE)
string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
"\n\nint main(void){return 0;}\n") "\n\nint main(void){return 0;}\n")
configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in" configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFiles.c" @ONLY) "${src}" @ONLY)
set(_INCLUDE ${INCLUDE}) # remove empty elements set(_INCLUDE ${INCLUDE}) # remove empty elements
if("${_INCLUDE}" MATCHES "^([^;]+);.+;([^;]+)$") if("${_INCLUDE}" MATCHES "^([^;]+);.+;([^;]+)$")
@@ -68,7 +94,7 @@ macro(CHECK_INCLUDE_FILES INCLUDE VARIABLE)
endif() endif()
try_compile(${VARIABLE} try_compile(${VARIABLE}
${CMAKE_BINARY_DIR} ${CMAKE_BINARY_DIR}
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFiles.c ${src}
COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
CMAKE_FLAGS CMAKE_FLAGS
-DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILES_FLAGS} -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILES_FLAGS}