From ea8c37b759cfcf78ddeab9b296e874ee8a5212e1 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 4 Dec 2023 07:20:36 -0500 Subject: Tests/CXXModules: add a test which scans a PCH-using source This tests that PCH usage works with scanning logic. --- Tests/RunCMake/CXXModules/RunCMakeTest.cmake | 2 ++ .../CXXModules/examples/scan-with-pch/CMakeLists.txt | 13 +++++++++++++ Tests/RunCMake/CXXModules/examples/scan-with-pch/main.cxx | 8 ++++++++ Tests/RunCMake/CXXModules/examples/scan-with-pch/pch.h | 1 + 4 files changed, 24 insertions(+) create mode 100644 Tests/RunCMake/CXXModules/examples/scan-with-pch/CMakeLists.txt create mode 100644 Tests/RunCMake/CXXModules/examples/scan-with-pch/main.cxx create mode 100644 Tests/RunCMake/CXXModules/examples/scan-with-pch/pch.h diff --git a/Tests/RunCMake/CXXModules/RunCMakeTest.cmake b/Tests/RunCMake/CXXModules/RunCMakeTest.cmake index 781c708..0c0f1b4 100644 --- a/Tests/RunCMake/CXXModules/RunCMakeTest.cmake +++ b/Tests/RunCMake/CXXModules/RunCMakeTest.cmake @@ -165,6 +165,8 @@ if (RunCMake_GENERATOR MATCHES "Ninja") run_cxx_module_test_target(ninja-cmp0154 "${ninja_cmp0154_target}") endif () +run_cxx_module_test(scan-with-pch) + # Tests which use named modules. if ("named" IN_LIST CMake_TEST_MODULE_COMPILATION) run_cxx_module_test(simple) diff --git a/Tests/RunCMake/CXXModules/examples/scan-with-pch/CMakeLists.txt b/Tests/RunCMake/CXXModules/examples/scan-with-pch/CMakeLists.txt new file mode 100644 index 0000000..e3e7561 --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/scan-with-pch/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.28) +project(cxx_modules_scan_with_pch CXX) + +include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake") + +add_executable(simple) +target_sources(simple + PRIVATE + main.cxx) +target_compile_features(simple PUBLIC cxx_std_20) +target_precompile_headers(simple PRIVATE pch.h) + +add_test(NAME simple COMMAND simple) diff --git a/Tests/RunCMake/CXXModules/examples/scan-with-pch/main.cxx b/Tests/RunCMake/CXXModules/examples/scan-with-pch/main.cxx new file mode 100644 index 0000000..33bd41e --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/scan-with-pch/main.cxx @@ -0,0 +1,8 @@ +#ifndef from_pch +# error "pch not present" +#endif + +int main(int argc, char* argv[]) +{ + return 0; +} diff --git a/Tests/RunCMake/CXXModules/examples/scan-with-pch/pch.h b/Tests/RunCMake/CXXModules/examples/scan-with-pch/pch.h new file mode 100644 index 0000000..2841ee9 --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/scan-with-pch/pch.h @@ -0,0 +1 @@ +#define from_pch -- cgit v0.12 From f61c64cd1cac7c9a6ec93d63647f332a5915da13 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 4 Dec 2023 08:43:25 -0500 Subject: cmLocalGenerator: prevent scanning of PCH source files --- Source/cmLocalGenerator.cxx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index fe8d502..caac535 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -2706,6 +2706,9 @@ void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target) auto* pch_sf = this->Makefile->GetOrCreateSource( pchSource, false, cmSourceFileLocationKind::Known); + // PCH sources should never be scanned as they cannot contain C++ + // module references. + pch_sf->SetProperty("CXX_SCAN_FOR_MODULES", "0"); if (!this->GetGlobalGenerator()->IsXcode()) { if (!ReuseFrom) { -- cgit v0.12 From 40dc13b24292354d07b9720d636fa48ab3eee219 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 4 Dec 2023 07:21:37 -0500 Subject: cmNinjaTargetGenerator: PCH files do not need dyndep Fixes: #24209 --- Source/cmNinjaTargetGenerator.cxx | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Source/cmNinjaTargetGenerator.cxx b/Source/cmNinjaTargetGenerator.cxx index 2283923..4025918 100644 --- a/Source/cmNinjaTargetGenerator.cxx +++ b/Source/cmNinjaTargetGenerator.cxx @@ -1367,7 +1367,10 @@ void cmNinjaTargetGenerator::WriteObjectBuildStatement( !(language == "RC" || (language == "CUDA" && !flag)); int const commandLineLengthLimit = ((lang_supports_response && this->ForceResponseFile())) ? -1 : 0; - bool const needDyndep = + cmValue pchExtension = + this->GetMakefile()->GetDefinition("CMAKE_PCH_EXTENSION"); + bool const isPch = cmHasSuffix(objectFileName, pchExtension); + bool const needDyndep = !isPch && this->GeneratorTarget->NeedDyndepForSource(language, config, source); cmNinjaBuild objBuild(this->LanguageCompilerRule( @@ -1435,13 +1438,9 @@ void cmNinjaTargetGenerator::WriteObjectBuildStatement( } objBuild.Outputs.push_back(objectFileName); - if (firstForConfig) { - cmValue pchExtension = - this->GetMakefile()->GetDefinition("CMAKE_PCH_EXTENSION"); - if (!cmHasSuffix(objectFileName, pchExtension)) { - // Add this object to the list of object files. - this->Configs[config].Objects.push_back(objectFileName); - } + if (firstForConfig && !isPch) { + // Add this object to the list of object files. + this->Configs[config].Objects.push_back(objectFileName); } objBuild.ExplicitDeps.push_back(sourceFilePath); -- cgit v0.12