2023-11-01 06:11:15 +08:00
|
|
|
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
# file Copyright.txt or https://cmake.org/licensing for details.
|
|
|
|
|
|
|
|
#[=======================================================================[.rst:
|
|
|
|
CheckLanguage
|
|
|
|
-------------
|
|
|
|
|
|
|
|
Check if a language can be enabled
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
check_language(<lang>)
|
|
|
|
|
2023-11-01 06:20:22 +08:00
|
|
|
where ``<lang>`` is a language that may be passed to :command:`enable_language`
|
|
|
|
such as ``Fortran``. If :variable:`CMAKE_<LANG>_COMPILER` is already defined
|
|
|
|
the check does nothing. Otherwise it tries enabling the language in a
|
|
|
|
test project. The result is cached in :variable:`CMAKE_<LANG>_COMPILER`
|
|
|
|
as the compiler that was found, or ``NOTFOUND`` if the language cannot be
|
|
|
|
enabled. For CUDA which can have an explicit host compiler, the cache
|
|
|
|
:variable:`CMAKE_CUDA_HOST_COMPILER` variable will be set if it was required
|
2023-11-01 08:44:24 +08:00
|
|
|
for compilation (and cleared if it was not).
|
2023-11-01 06:11:15 +08:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
check_language(Fortran)
|
|
|
|
if(CMAKE_Fortran_COMPILER)
|
|
|
|
enable_language(Fortran)
|
|
|
|
else()
|
|
|
|
message(STATUS "No Fortran support")
|
|
|
|
endif()
|
|
|
|
#]=======================================================================]
|
|
|
|
|
|
|
|
include_guard(GLOBAL)
|
|
|
|
|
2023-11-01 08:44:24 +08:00
|
|
|
block(SCOPE_FOR POLICIES)
|
|
|
|
cmake_policy(SET CMP0126 NEW)
|
|
|
|
|
2023-11-01 06:11:15 +08:00
|
|
|
macro(check_language lang)
|
|
|
|
if(NOT DEFINED CMAKE_${lang}_COMPILER)
|
|
|
|
set(_desc "Looking for a ${lang} compiler")
|
2023-11-01 06:34:31 +08:00
|
|
|
message(CHECK_START "${_desc}")
|
2023-11-01 06:11:15 +08:00
|
|
|
file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang})
|
2023-11-01 06:20:22 +08:00
|
|
|
|
|
|
|
set(extra_compiler_variables)
|
2023-11-01 08:44:24 +08:00
|
|
|
if(${lang} STREQUAL CUDA AND NOT CMAKE_GENERATOR MATCHES "Visual Studio")
|
2023-11-01 06:20:22 +08:00
|
|
|
set(extra_compiler_variables "set(CMAKE_CUDA_HOST_COMPILER \\\"\${CMAKE_CUDA_HOST_COMPILER}\\\")")
|
|
|
|
endif()
|
|
|
|
|
2023-11-01 06:36:59 +08:00
|
|
|
set(_cl_content
|
2023-11-01 06:11:15 +08:00
|
|
|
"cmake_minimum_required(VERSION ${CMAKE_VERSION})
|
|
|
|
project(Check${lang} ${lang})
|
|
|
|
file(WRITE \"\${CMAKE_CURRENT_BINARY_DIR}/result.cmake\"
|
|
|
|
\"set(CMAKE_${lang}_COMPILER \\\"\${CMAKE_${lang}_COMPILER}\\\")\\n\"
|
2023-11-01 06:20:22 +08:00
|
|
|
\"${extra_compiler_variables}\\n\"
|
|
|
|
)"
|
|
|
|
)
|
|
|
|
|
|
|
|
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}/CMakeLists.txt"
|
2023-11-01 06:36:59 +08:00
|
|
|
"${_cl_content}")
|
2023-11-01 06:11:15 +08:00
|
|
|
if(CMAKE_GENERATOR_INSTANCE)
|
|
|
|
set(_D_CMAKE_GENERATOR_INSTANCE "-DCMAKE_GENERATOR_INSTANCE:INTERNAL=${CMAKE_GENERATOR_INSTANCE}")
|
|
|
|
else()
|
|
|
|
set(_D_CMAKE_GENERATOR_INSTANCE "")
|
|
|
|
endif()
|
2023-11-01 06:34:31 +08:00
|
|
|
if(CMAKE_GENERATOR MATCHES "^(Xcode$|Green Hills MULTI$|Visual Studio)")
|
|
|
|
set(_D_CMAKE_MAKE_PROGRAM "")
|
|
|
|
else()
|
|
|
|
set(_D_CMAKE_MAKE_PROGRAM "-DCMAKE_MAKE_PROGRAM:FILEPATH=${CMAKE_MAKE_PROGRAM}")
|
|
|
|
endif()
|
2023-11-01 08:44:24 +08:00
|
|
|
if(CMAKE_TOOLCHAIN_FILE)
|
|
|
|
set(_D_CMAKE_TOOLCHAIN_FILE "-DCMAKE_TOOLCHAIN_FILE:FILEPATH=${CMAKE_TOOLCHAIN_FILE}")
|
|
|
|
else()
|
|
|
|
set(_D_CMAKE_TOOLCHAIN_FILE "")
|
|
|
|
endif()
|
2023-11-01 06:11:15 +08:00
|
|
|
execute_process(
|
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}
|
|
|
|
COMMAND ${CMAKE_COMMAND} . -G ${CMAKE_GENERATOR}
|
|
|
|
-A "${CMAKE_GENERATOR_PLATFORM}"
|
|
|
|
-T "${CMAKE_GENERATOR_TOOLSET}"
|
|
|
|
${_D_CMAKE_GENERATOR_INSTANCE}
|
2023-11-01 06:34:31 +08:00
|
|
|
${_D_CMAKE_MAKE_PROGRAM}
|
2023-11-01 08:44:24 +08:00
|
|
|
${_D_CMAKE_TOOLCHAIN_FILE}
|
2023-11-01 06:36:59 +08:00
|
|
|
OUTPUT_VARIABLE _cl_output
|
|
|
|
ERROR_VARIABLE _cl_output
|
|
|
|
RESULT_VARIABLE _cl_result
|
2023-11-01 06:11:15 +08:00
|
|
|
)
|
|
|
|
include(${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Check${lang}/result.cmake OPTIONAL)
|
2023-11-01 06:36:59 +08:00
|
|
|
if(CMAKE_${lang}_COMPILER AND "${_cl_result}" STREQUAL "0")
|
2023-11-01 08:44:24 +08:00
|
|
|
message(CONFIGURE_LOG
|
2023-11-01 06:11:15 +08:00
|
|
|
"${_desc} passed with the following output:\n"
|
2023-11-01 06:36:59 +08:00
|
|
|
"${_cl_output}\n")
|
2023-11-01 06:34:31 +08:00
|
|
|
set(_CHECK_COMPILER_STATUS CHECK_PASS)
|
2023-11-01 06:11:15 +08:00
|
|
|
else()
|
|
|
|
set(CMAKE_${lang}_COMPILER NOTFOUND)
|
2023-11-01 06:34:31 +08:00
|
|
|
set(_CHECK_COMPILER_STATUS CHECK_FAIL)
|
2023-11-01 08:44:24 +08:00
|
|
|
message(CONFIGURE_LOG
|
2023-11-01 06:11:15 +08:00
|
|
|
"${_desc} failed with the following output:\n"
|
2023-11-01 06:36:59 +08:00
|
|
|
"${_cl_output}\n")
|
2023-11-01 06:11:15 +08:00
|
|
|
endif()
|
2023-11-01 06:34:31 +08:00
|
|
|
message(${_CHECK_COMPILER_STATUS} "${CMAKE_${lang}_COMPILER}")
|
2023-11-01 06:11:15 +08:00
|
|
|
set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER}" CACHE FILEPATH "${lang} compiler")
|
|
|
|
mark_as_advanced(CMAKE_${lang}_COMPILER)
|
2023-11-01 06:20:22 +08:00
|
|
|
|
|
|
|
if(CMAKE_${lang}_HOST_COMPILER)
|
|
|
|
message(STATUS "Looking for a ${lang} host compiler - ${CMAKE_${lang}_HOST_COMPILER}")
|
|
|
|
set(CMAKE_${lang}_HOST_COMPILER "${CMAKE_${lang}_HOST_COMPILER}" CACHE FILEPATH "${lang} host compiler")
|
|
|
|
mark_as_advanced(CMAKE_${lang}_HOST_COMPILER)
|
|
|
|
endif()
|
|
|
|
|
2023-11-01 06:11:15 +08:00
|
|
|
endif()
|
|
|
|
endmacro()
|
2023-11-01 08:44:24 +08:00
|
|
|
|
|
|
|
endblock()
|