try_compile: Do not require module sources in the source directory

Derive the base directory of each module file set from the module source
instead of using the source directory of the calling project. This
allows module sources to reside anywhere, e.g., copies created by
SOURCE_FROM_FILE in the scratch directory or installed module
interfaces.

Fixes: #28206
This commit is contained in:
Sergiu Deitsch committed 2026-10-25 00:16:03 +02:00
1 parent a42e96a28c
commit 517a62f593
4 files changed
+56 -9

No files matched your search

@@ -0,0 +1,7 @@
try_compile-module-base-dirs
----------------------------
* The :command:`try_compile` and :command:`try_run` commands now accept
``CXX_MODULE`` sources outside of the calling project's source directory,
including copies made by ``SOURCE_FROM_CONTENT``, ``SOURCE_FROM_FILE``
and ``SOURCE_FROM_VAR``. Previously such sources caused an error.
+11 -9
View File
@@ -957,15 +957,17 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
}
} break;
case Arguments::SourceType::CxxModule: {
if (!in_file_set) {
file_set_name += 'a';
fprintf(fout,
" PRIVATE FILE_SET %s TYPE CXX_MODULES BASE_DIRS \"%s\" "
"FILES\n",
file_set_name.c_str(),
this->Makefile->GetCurrentSourceDirectory().c_str());
in_file_set = true;
}
// Module sources may reside anywhere, including the scratch
// directory for copied sources. Each one gets its own file set
// whose base directory contains it.
std::string const baseDir = cmSystemTools::GetFilenamePath(
cmSystemTools::CollapseFullPath(si, this->BinaryDirectory));
file_set_name += 'a';
fprintf(fout,
" PRIVATE FILE_SET %s TYPE CXX_MODULES BASE_DIRS \"%s\" "
"FILES\n",
file_set_name.c_str(), baseDir.c_str());
in_file_set = true;
} break;
case Arguments::SourceType::Directory:
/* Handled elsewhere. */
@@ -233,6 +233,7 @@ if ("named" IN_LIST CMake_TEST_MODULE_COMPILATION)
run_cxx_module_test(imp-from-object)
run_cxx_module_test(circular)
run_cxx_module_test(try-compile)
run_cxx_module_test(try-compile-ext)
run_cxx_module_test(try-run)
unset(RunCMake_CXXModules_NO_TEST)
run_cxx_module_test(same-src-name)
@@ -0,0 +1,37 @@
cmake_minimum_required(VERSION 3.24...3.28)
project(cxx_modules_try_compile_ext CXX)
include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
# The module sources reside outside of the project's source directory.
set(srcdir "${CMAKE_CURRENT_LIST_DIR}/../try-compile")
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
try_compile(can_use_modules_sources
SOURCES_TYPE CXX_MODULE
SOURCES
"${srcdir}/importable.cxx"
SOURCES_TYPE NORMAL
SOURCES
"${srcdir}/use_importable.cxx"
CXX_STANDARD 20)
if (NOT can_use_modules_sources)
message(FATAL_ERROR
"`try_compile` could not compile module sources given by `SOURCES`.")
endif ()
try_compile(can_use_modules_copied
SOURCES_TYPE CXX_MODULE
SOURCE_FROM_FILE
importable.cxx "${srcdir}/importable.cxx"
SOURCES_TYPE NORMAL
SOURCE_FROM_FILE
use_importable.cxx "${srcdir}/use_importable.cxx"
CXX_STANDARD 20)
if (NOT can_use_modules_copied)
message(FATAL_ERROR
"`try_compile` could not compile module sources given by "
"`SOURCE_FROM_FILE`.")
endif ()