From 1610aa22f4cf626a3fe7b0a4dd306f3673d43870 Mon Sep 17 00:00:00 2001 From: Sharadh Rajaraman Date: Sat, 4 Jul 2026 22:41:17 +0100 Subject: [PATCH] Clang: Add support for `import std` when targeting the MSVC ABI When Clang targets the MSVC ABI, discover the MSVC standard library's import-std metadata discovery through the MSVC module logic. Also extend the latter to use `CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES` and honor `CMAKE_CXX_STDLIB_MODULES_JSON`. Closes: #26761 Signed-off-by: Sharadh Rajaraman --- .../ci/configure_windows_clang_ninja.cmake | 2 +- Modules/Compiler/Clang-CXX-CXXImportStd.cmake | 7 ++++ Modules/Compiler/MSVC-CXX-CXXImportStd.cmake | 38 +++++++++---------- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/.gitlab/ci/configure_windows_clang_ninja.cmake b/.gitlab/ci/configure_windows_clang_ninja.cmake index c92d9b6256..5c87bac892 100644 --- a/.gitlab/ci/configure_windows_clang_ninja.cmake +++ b/.gitlab/ci/configure_windows_clang_ninja.cmake @@ -1,4 +1,4 @@ -set(CMake_TEST_MODULE_COMPILATION "named,compile_commands,collation,partitions,internal_partitions,export_bmi,install_bmi,shared,bmionly,build_database" CACHE STRING "") +set(CMake_TEST_MODULE_COMPILATION "named,compile_commands,collation,partitions,internal_partitions,export_bmi,install_bmi,shared,bmionly,build_database,import_std23" CACHE STRING "") if("$ENV{CMAKE_CI_BUILD_NAME}" MATCHES "(^|_)cl(_|$)") set(CMAKE_Fortran_COMPILER "flang" CACHE STRING "") set(CMAKE_Fortran_COMPILER_ID "LLVMFlang" CACHE STRING "") diff --git a/Modules/Compiler/Clang-CXX-CXXImportStd.cmake b/Modules/Compiler/Clang-CXX-CXXImportStd.cmake index 5d637b7a05..c8f94bfac0 100644 --- a/Modules/Compiler/Clang-CXX-CXXImportStd.cmake +++ b/Modules/Compiler/Clang-CXX-CXXImportStd.cmake @@ -1,3 +1,10 @@ +if(CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC" OR + CMAKE_CXX_STANDARD_LIBRARY STREQUAL "msvc" OR + CMAKE_CXX_COMPILER_TARGET MATCHES "(^|-)windows-msvc($|-)") + include(Compiler/MSVC-CXX-CXXImportStd) + return() +endif() + function (_cmake_cxx_find_modules_json) if (NOT CMAKE_CXX_STDLIB_MODULES_JSON) if (CMAKE_CXX_STANDARD_LIBRARY STREQUAL "libc++") diff --git a/Modules/Compiler/MSVC-CXX-CXXImportStd.cmake b/Modules/Compiler/MSVC-CXX-CXXImportStd.cmake index b7bd5254e5..d31c4c39a2 100644 --- a/Modules/Compiler/MSVC-CXX-CXXImportStd.cmake +++ b/Modules/Compiler/MSVC-CXX-CXXImportStd.cmake @@ -1,24 +1,24 @@ function (_cmake_cxx_find_modules_json) if (CMAKE_CXX_STDLIB_MODULES_JSON) - return () - endif () - - find_file(_msvc_modules_json_file - NAME modules.json - HINTS - "$ENV{VCToolsInstallDir}/modules" - PATHS - "$ENV{INCLUDE}" - "${CMAKE_CXX_COMPILER}/../../.." - "${CMAKE_CXX_COMPILER}/../.." # msvc-wine layout - PATH_SUFFIXES - ../modules - NO_CACHE) - - # Without this file, we do not have modules installed. - if (NOT EXISTS "${_msvc_modules_json_file}") - set(CMAKE_CXX_COMPILER_IMPORT_STD_ERROR_MESSAGE "Could not find `modules.json` resource" PARENT_SCOPE) - return () + set(_msvc_modules_json_file "${CMAKE_CXX_STDLIB_MODULES_JSON}") + else () + find_file(_msvc_modules_json_file + NAME modules.json + HINTS + "$ENV{VCToolsInstallDir}/modules" + PATHS + "$ENV{INCLUDE}" + ${CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES} + "${CMAKE_CXX_COMPILER}/../../.." + "${CMAKE_CXX_COMPILER}/../.." # msvc-wine layout + PATH_SUFFIXES + ../modules + NO_CACHE) + # Without this file, we do not have modules installed. + if (NOT EXISTS "${_msvc_modules_json_file}") + set(CMAKE_CXX_COMPILER_IMPORT_STD_ERROR_MESSAGE "Could not find `modules.json` resource" PARENT_SCOPE) + return () + endif () endif () file(READ "${_msvc_modules_json_file}" _msvc_modules_json)