From 3e15419bd459a4bdca5195fd3868f1dae2bef680 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Thu, 9 Jan 2025 16:23:46 +0100 Subject: [PATCH] target_sources: Restore toleration of duplicate CXX_MODULES sources In commit 9c0491a3e4 (cmDyndepCollation: write out scanned source information too, 2024-03-25) via !9708, the `sf_map` gained a new job of also being used to track non-file set sources which could import modules. This was implemented by removing processed `FILE_SET TYPE CXX_MODULES` sources from the map and working with the sources remaining. When a `FILE_SET TYPE CXX_MODULES` source appeared multiple times, this would then erroneously complain that it "was not scheduled for compilation". Use a set of source paths to track sources that have already been processed. If duplicates are found, trigger an author warning and skip the duplicate file. Fixes: #26549 --- Source/cmDyndepCollation.cxx | 13 +++++++++++++ .../examples/duplicate-sources-stderr.txt | 8 ++++++++ 2 files changed, 21 insertions(+) create mode 100644 Tests/RunCMake/CXXModules/examples/duplicate-sources-stderr.txt diff --git a/Source/cmDyndepCollation.cxx b/Source/cmDyndepCollation.cxx index 1c05f25fe0..f42136f7ea 100644 --- a/Source/cmDyndepCollation.cxx +++ b/Source/cmDyndepCollation.cxx @@ -127,11 +127,23 @@ TdiSourceInfo CollationInformationSources(cmGeneratorTarget const* gt, } } + // Detect duplicate sources. + std::set visited_sources; + for (auto const& files_per_dir : files_per_dirs) { for (auto const& file : files_per_dir.second) { auto const full_file = cmSystemTools::CollapseFullPath(file); auto lookup = sf_map.find(full_file); if (lookup == sf_map.end()) { + if (visited_sources.count(full_file)) { + // Duplicate source; raise an author warning. + gt->Makefile->IssueMessage( + MessageType::AUTHOR_WARNING, + cmStrCat( + "Target \"", tgt->GetName(), "\" has source file\n ", file, + "\nin a \"FILE_SET TYPE CXX_MODULES\" multiple times.")); + continue; + } gt->Makefile->IssueMessage( MessageType::FATAL_ERROR, cmStrCat("Target \"", tgt->GetName(), "\" has source file\n ", @@ -140,6 +152,7 @@ TdiSourceInfo CollationInformationSources(cmGeneratorTarget const* gt, "scheduled for compilation.")); continue; } + visited_sources.insert(full_file); auto const* sf = lookup->second.first; CompileType const ct = lookup->second.second; diff --git a/Tests/RunCMake/CXXModules/examples/duplicate-sources-stderr.txt b/Tests/RunCMake/CXXModules/examples/duplicate-sources-stderr.txt new file mode 100644 index 0000000000..bf964dd73c --- /dev/null +++ b/Tests/RunCMake/CXXModules/examples/duplicate-sources-stderr.txt @@ -0,0 +1,8 @@ +CMake Warning \(dev\) in CMakeLists.txt: + Target "duplicate_sources" has source file + + [^ +]*/Tests/RunCMake/CXXModules/examples/duplicate-sources/duplicate.cxx + + in a "FILE_SET TYPE CXX_MODULES" multiple times. +This warning is for project developers. Use -Wno-dev to suppress it.