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
This commit is contained in:
Ben Boeckel
2025-01-09 16:23:46 +01:00
parent 5cfb8ae790
commit 3e15419bd4
2 changed files with 21 additions and 0 deletions

View File

@@ -127,11 +127,23 @@ TdiSourceInfo CollationInformationSources(cmGeneratorTarget const* gt,
}
}
// Detect duplicate sources.
std::set<std::string> 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;

View File

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