浏览代码

cmLocalGenerator: De-duplicate unity source file generation

Brad King 4 年之前
父节点
当前提交
0b56f92576
共有 2 个文件被更改,包括 40 次插入40 次删除
  1. 32 40
      Source/cmLocalGenerator.cxx
  2. 8 0
      Source/cmLocalGenerator.h

+ 32 - 40
Source/cmLocalGenerator.cxx

@@ -37,6 +37,7 @@
 #include "cmInstallTargetGenerator.h"
 #include "cmLinkLineComputer.h"
 #include "cmMakefile.h"
+#include "cmRange.h"
 #include "cmRulePlaceholderExpander.h"
 #include "cmSourceFile.h"
 #include "cmSourceFileLocation.h"
@@ -2806,6 +2807,29 @@ inline void RegisterUnitySources(cmGeneratorTarget* target, cmSourceFile* sf,
 }
 }
 
+std::string cmLocalGenerator::WriteUnitySource(
+  cmGeneratorTarget* target,
+  cmRange<std::vector<cmSourceFile*>::const_iterator> sources,
+  cmValue beforeInclude, cmValue afterInclude, std::string filename) const
+{
+  cmValue uniqueIdName = target->GetProperty("UNITY_BUILD_UNIQUE_ID");
+  const std::string filename_tmp = cmStrCat(filename, ".tmp");
+  {
+    cmGeneratedFileStream file(
+      filename_tmp, false,
+      target->GetGlobalGenerator()->GetMakefileEncoding());
+    file << "/* generated by CMake */\n\n";
+
+    for (cmSourceFile* sf : sources) {
+      RegisterUnitySources(target, sf, filename);
+      IncludeFileInUnitySources(file, sf->ResolveFullPath(), beforeInclude,
+                                afterInclude, uniqueIdName);
+    }
+  }
+  cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
+  return filename;
+}
+
 void cmLocalGenerator::IncludeFileInUnitySources(
   cmGeneratedFileStream& unity_file, std::string const& sf_full_path,
   cmValue beforeInclude, cmValue afterInclude, cmValue uniqueIdName) const
@@ -2856,8 +2880,6 @@ std::vector<std::string> cmLocalGenerator::AddUnityFilesModeAuto(
     batchSize = filtered_sources.size();
   }
 
-  cmValue uniqueIdName = target->GetProperty("UNITY_BUILD_UNIQUE_ID");
-
   std::vector<std::string> unity_files;
   for (size_t itemsLeft = filtered_sources.size(), chunk, batch = 0;
        itemsLeft > 0; itemsLeft -= chunk, ++batch) {
@@ -2866,26 +2888,11 @@ std::vector<std::string> cmLocalGenerator::AddUnityFilesModeAuto(
 
     std::string filename = cmStrCat(filename_base, "unity_", batch,
                                     (lang == "C") ? "_c.c" : "_cxx.cxx");
-
-    const std::string filename_tmp = cmStrCat(filename, ".tmp");
-    {
-      size_t begin = batch * batchSize;
-      size_t end = begin + chunk;
-
-      cmGeneratedFileStream file(
-        filename_tmp, false,
-        target->GetGlobalGenerator()->GetMakefileEncoding());
-      file << "/* generated by CMake */\n\n";
-
-      for (; begin != end; ++begin) {
-        cmSourceFile* sf = filtered_sources[begin];
-        RegisterUnitySources(target, sf, filename);
-        IncludeFileInUnitySources(file, sf->ResolveFullPath(), beforeInclude,
-                                  afterInclude, uniqueIdName);
-      }
-    }
-    cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
-    unity_files.emplace_back(std::move(filename));
+    auto const begin = filtered_sources.begin() + batch * batchSize;
+    auto const end = begin + chunk;
+    unity_files.emplace_back(
+      this->WriteUnitySource(target, cmMakeRange(begin, end), beforeInclude,
+                             afterInclude, std::move(filename)));
   }
   return unity_files;
 }
@@ -2912,28 +2919,13 @@ std::vector<std::string> cmLocalGenerator::AddUnityFilesModeGroup(
     }
   }
 
-  cmValue uniqueIdName = target->GetProperty("UNITY_BUILD_UNIQUE_ID");
-
   for (auto const& item : explicit_mapping) {
     auto const& name = item.first;
     std::string filename = cmStrCat(filename_base, "unity_", name,
                                     (lang == "C") ? "_c.c" : "_cxx.cxx");
-
-    const std::string filename_tmp = cmStrCat(filename, ".tmp");
-    {
-      cmGeneratedFileStream file(
-        filename_tmp, false,
-        target->GetGlobalGenerator()->GetMakefileEncoding());
-      file << "/* generated by CMake */\n\n";
-
-      for (cmSourceFile* sf : item.second) {
-        RegisterUnitySources(target, sf, filename);
-        IncludeFileInUnitySources(file, sf->ResolveFullPath(), beforeInclude,
-                                  afterInclude, uniqueIdName);
-      }
-    }
-    cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
-    unity_files.emplace_back(std::move(filename));
+    unity_files.emplace_back(
+      this->WriteUnitySource(target, cmMakeRange(item.second), beforeInclude,
+                             afterInclude, std::move(filename)));
   }
 
   return unity_files;

+ 8 - 0
Source/cmLocalGenerator.h

@@ -40,6 +40,9 @@ class cmState;
 class cmTarget;
 class cmake;
 
+template <typename Iter>
+class cmRange;
+
 /** Flag if byproducts shall also be considered.  */
 enum class cmSourceOutputKind
 {
@@ -657,6 +660,11 @@ private:
                          const std::string& ReuseFrom,
                          cmGeneratorTarget* reuseTarget,
                          std::vector<std::string> const& extensions);
+
+  std::string WriteUnitySource(
+    cmGeneratorTarget* target,
+    cmRange<std::vector<cmSourceFile*>::const_iterator> sources,
+    cmValue beforeInclude, cmValue afterInclude, std::string filename) const;
   void IncludeFileInUnitySources(cmGeneratedFileStream& unity_file,
                                  std::string const& sf_full_path,
                                  cmValue beforeInclude, cmValue afterInclude,