瀏覽代碼

Merge topic 'vs-cxxmodules-same-src-name'

b9c99830c5 VS: Fix C++ modules in source files with the same name

Acked-by: Kitware Robot <[email protected]>
Acked-by: buildbot <[email protected]>
Merge-request: !8596
Brad King 2 年之前
父節點
當前提交
4938a0cbdd

+ 11 - 0
Source/cmVisualStudio10TargetGenerator.cxx

@@ -2766,6 +2766,8 @@ void cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
     }
   }
 
+  bool isCppModule = false;
+
   for (std::string const& config : this->Configurations) {
     this->GeneratorTarget->NeedCxxModuleSupport(lang, config);
 
@@ -2789,6 +2791,7 @@ void cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
     if (fs && fs->GetType() == "CXX_MODULES"_s) {
       if (lang == "CXX"_s) {
         if (fs->GetType() == "CXX_MODULES"_s) {
+          isCppModule = true;
           if (shouldScanForModules &&
               this->GlobalGenerator->IsScanDependenciesSupported()) {
             // ScanSourceforModuleDependencies uses 'cl /scanDependencies' and
@@ -2947,6 +2950,14 @@ void cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
       oh.OutputPreprocessorDefinitions(lang);
     }
   }
+
+  if (isCppModule && !objectName.empty()) {
+    std::string baseName = cmStrCat("$(IntDir)/", objectName);
+    cmStripSuffixIfExists(baseName, ".obj");
+    e2.Element("ModuleOutputFile", cmStrCat(baseName, ".ifc"));
+    e2.Element("ModuleDependenciesFile", cmStrCat(baseName, ".module.json"));
+  }
+
   if (this->IsXamlSource(source->GetFullPath())) {
     const std::string& fileName = source->GetFullPath();
     e2.Element("DependentUpon",

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

@@ -148,6 +148,7 @@ if ("named" IN_LIST CMake_TEST_MODULE_COMPILATION)
   set(RunCMake_CXXModules_NO_TEST 1)
   run_cxx_module_test(circular)
   unset(RunCMake_CXXModules_NO_TEST)
+  run_cxx_module_test(same-src-name)
   run_cxx_module_test(scan_properties)
 endif ()
 

+ 4 - 0
Tests/RunCMake/CXXModules/examples/same-src-name-stderr.txt

@@ -0,0 +1,4 @@
+CMake Warning \(dev\) at CMakeLists.txt:7 \(target_sources\):
+  CMake's C\+\+ module support is experimental.  It is meant only for
+  experimentation and feedback to CMake developers.
+This warning is for project developers.  Use -Wno-dev to suppress it.

+ 20 - 0
Tests/RunCMake/CXXModules/examples/same-src-name/CMakeLists.txt

@@ -0,0 +1,20 @@
+cmake_minimum_required(VERSION 3.27)
+project(cxx_modules_same_src_name CXX)
+
+include("${CMAKE_SOURCE_DIR}/../cxx-modules-rules.cmake")
+
+add_executable(same_src_name)
+target_sources(same_src_name
+  PRIVATE
+    main.cxx
+  PRIVATE
+    FILE_SET CXX_MODULES
+      BASE_DIRS
+        "${CMAKE_CURRENT_SOURCE_DIR}"
+      FILES
+        a/same.cxx
+        b/same.cxx
+  )
+target_compile_features(same_src_name PUBLIC cxx_std_20)
+
+add_test(NAME same_src_name COMMAND same_src_name)

+ 5 - 0
Tests/RunCMake/CXXModules/examples/same-src-name/a/same.cxx

@@ -0,0 +1,5 @@
+export module a.same;
+export int a_same()
+{
+  return 0;
+}

+ 5 - 0
Tests/RunCMake/CXXModules/examples/same-src-name/b/same.cxx

@@ -0,0 +1,5 @@
+export module b.same;
+export int b_same()
+{
+  return 0;
+}

+ 7 - 0
Tests/RunCMake/CXXModules/examples/same-src-name/main.cxx

@@ -0,0 +1,7 @@
+import a.same;
+import b.same;
+
+int main()
+{
+  return a_same() + b_same();
+}