cxxmodules: provide a detection variable for import std

This commit is contained in:
Ben Boeckel
2024-04-17 11:09:08 -04:00
parent 9bf79a7964
commit 678717f3e4
6 changed files with 37 additions and 4 deletions

View File

@@ -96,6 +96,9 @@ library combinations:
* MSVC toolset 14.36 and newer (provided with Visual Studio 17.6 Preview 2 and
newer)
The :variable:`CMAKE_CXX_COMPILER_IMPORT_STD` variable may be used to detect
support for a standard level with the active C++ toolchain.
.. note ::
This support is provided only when experimental support for

View File

@@ -580,6 +580,7 @@ Variables for Languages
/variable/CMAKE_CUDA_STANDARD_REQUIRED
/variable/CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES
/variable/CMAKE_CXX_COMPILE_FEATURES
/variable/CMAKE_CXX_COMPILER_IMPORT_STD
/variable/CMAKE_CXX_EXTENSIONS
/variable/CMAKE_CXX_STANDARD
/variable/CMAKE_CXX_STANDARD_REQUIRED

View File

@@ -0,0 +1,14 @@
CMAKE_CXX_COMPILER_IMPORT_STD
-----------------------------
.. versionadded:: 3.30
A list of C++ standard levels for which ``import std`` support exists for the
current C++ toolchain. Support for C++\<NN\> may be detected using a
``<NN> IN_LIST CMAKE_CXX_COMPILER_IMPORT_STD`` predicate with the
:command:`if` command.
.. note ::
This variable is meaningful only when experimental support for ``import
std;`` has been enabled by the ``CMAKE_EXPERIMENTAL_CXX_IMPORT_STD`` gate.

View File

@@ -92,4 +92,5 @@ set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "@CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES@")
set(CMAKE_CXX_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "@CMAKE_CXX_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES@")
set(CMAKE_CXX_COMPILER_CLANG_RESOURCE_DIR "@CMAKE_CXX_COMPILER_CLANG_RESOURCE_DIR@")
set(CMAKE_CXX_COMPILER_IMPORT_STD "")
@CMAKE_CXX_IMPORT_STD@

View File

@@ -244,6 +244,9 @@ function(cmake_create_cxx_import_std std variable)
string(CONCAT guarded_target_definition
"if (NOT TARGET \"__CMAKE::CXX${std}\")\n"
"${target_definition}"
"endif ()\n"
"if (TARGET \"__CMAKE::CXX${std}\")\n"
" list(APPEND CMAKE_CXX_COMPILER_IMPORT_STD \"${std}\")\n"
"endif ()\n")
set("${variable}" "${guarded_target_definition}" PARENT_SCOPE)
endfunction()

View File

@@ -15,10 +15,21 @@ if (CMAKE_CXX_FLAGS MATCHES "-std=")
set(forced_cxx_standard 1)
endif ()
set(have_cxx23_import_std 0)
if (TARGET "__CMAKE::CXX23")
set(have_cxx23_import_std 1)
endif ()
macro (cxx_check_import_std version)
set(have_cxx${version}_import_std 0)
if ("${version}" IN_LIST CMAKE_CXX_COMPILER_IMPORT_STD)
set(have_cxx${version}_import_std 1)
endif ()
if (TARGET "__CMAKE:CXX${version}" AND NOT have_cxx${version}_import_std)
message(FATAL_ERROR
"The toolchain's C++${version} target exists, but the user variable does "
"not indicate it.")
endif ()
endmacro ()
cxx_check_import_std(23)
cxx_check_import_std(26)
# Forward information about the C++ compile features.
string(APPEND info "\