浏览代码

Merge topic 'cxxmodules-import-std-user-detect'

678717f3e4 cxxmodules: provide a detection variable for `import std`

Acked-by: Kitware Robot <[email protected]>
Acked-by: huangqinjin <[email protected]>
Merge-request: !9442
Brad King 1 年之前
父节点
当前提交
b99bdf5a4f

+ 3 - 0
Help/manual/cmake-cxxmodules.7.rst

@@ -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

+ 1 - 0
Help/manual/cmake-variables.7.rst

@@ -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

+ 14 - 0
Help/variable/CMAKE_CXX_COMPILER_IMPORT_STD.rst

@@ -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.

+ 1 - 0
Modules/CMakeCXXCompiler.cmake.in

@@ -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@

+ 3 - 0
Modules/Compiler/CMakeCommonCompilerMacros.cmake

@@ -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()

+ 15 - 4
Tests/RunCMake/CXXModules/Inspect.cmake

@@ -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 "\