Răsfoiți Sursa

Merge topic 'duplicate-source-behavior' into release-3.31

3e15419bd4 target_sources: Restore toleration of duplicate CXX_MODULES sources
5cfb8ae790 Tests/CXXModules: add a test with duplicate sources

Acked-by: Kitware Robot <[email protected]>
Tested-by: buildbot <[email protected]>
Merge-request: !10155
Brad King 10 luni în urmă
părinte
comite
92b260387d

+ 13 - 0
Source/cmDyndepCollation.cxx

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

+ 1 - 0
Tests/RunCMake/CXXModules/RunCMakeTest.cmake

@@ -321,6 +321,7 @@ endif ()
 
 # Tests which require collation work.
 if ("collation" IN_LIST CMake_TEST_MODULE_COMPILATION)
+  run_cxx_module_test(duplicate-sources)
   run_cxx_module_test(public-req-private)
   set(RunCMake_CXXModules_NO_TEST 1)
   run_cxx_module_test(req-private-other-target)

+ 8 - 0
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.

+ 19 - 0
Tests/RunCMake/CXXModules/examples/duplicate-sources/CMakeLists.txt

@@ -0,0 +1,19 @@
+cmake_minimum_required(VERSION 3.31)
+project(cxx_modules_duplicate_sources CXX)
+
+include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
+
+add_executable(duplicate_sources)
+target_sources(duplicate_sources
+  PRIVATE
+    main.cxx
+  PRIVATE
+    FILE_SET CXX_MODULES
+      BASE_DIRS
+        "${CMAKE_CURRENT_SOURCE_DIR}"
+      FILES
+        duplicate.cxx
+        duplicate.cxx)
+target_compile_features(duplicate_sources PRIVATE cxx_std_20)
+
+add_test(NAME duplicate_sources COMMAND duplicate_sources)

+ 8 - 0
Tests/RunCMake/CXXModules/examples/duplicate-sources/duplicate.cxx

@@ -0,0 +1,8 @@
+module;
+
+export module duplicate;
+
+export int from_import()
+{
+  return 0;
+}

+ 6 - 0
Tests/RunCMake/CXXModules/examples/duplicate-sources/main.cxx

@@ -0,0 +1,6 @@
+import duplicate;
+
+int main()
+{
+  return from_import();
+}