From 83c1657ff7fda3dc86b49bc9039f59449f2d8ae4 Mon Sep 17 00:00:00 2001
From: Brad King <brad.king@kitware.com>
Date: Thu, 3 Oct 2019 13:34:42 -0400
Subject: Unity build: Generate sources during Compute step

The unity build sources need to be added for all generators.  Create
them during `cmGlobalGenerator::Compute` to avoid duplicating the calls
in every generator.  We already handle Qt autogen there too.

Issue: #19789
---
 Source/cmGlobalGenerator.cxx                   | 18 ++++++++++++++++++
 Source/cmGlobalGenerator.h                     |  2 ++
 Source/cmGlobalXCodeGenerator.cxx              |  1 -
 Source/cmLocalGenerator.cxx                    |  9 +++++++--
 Source/cmLocalGenerator.h                      |  2 +-
 Source/cmLocalVisualStudio7Generator.cxx       |  1 -
 Source/cmMakefileExecutableTargetGenerator.cxx |  1 -
 Source/cmMakefileLibraryTargetGenerator.cxx    |  1 -
 Source/cmMakefileUtilityTargetGenerator.cxx    |  1 -
 Source/cmNinjaNormalTargetGenerator.cxx        |  1 -
 Source/cmVisualStudio10TargetGenerator.cxx     |  1 -
 11 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx
index 018037c..ee9ea3c 100644
--- a/Source/cmGlobalGenerator.cxx
+++ b/Source/cmGlobalGenerator.cxx
@@ -1381,6 +1381,11 @@ bool cmGlobalGenerator::Compute()
     return false;
   }
 
+  // Add automatically generated sources (e.g. unity build).
+  if (!this->AddAutomaticSources()) {
+    return false;
+  }
+
   // Add generator specific helper commands
   for (cmLocalGenerator* localGen : this->LocalGenerators) {
     localGen->AddHelperCommands();
@@ -1548,6 +1553,19 @@ bool cmGlobalGenerator::QtAutoGen()
 #endif
 }
 
+bool cmGlobalGenerator::AddAutomaticSources()
+{
+  for (cmLocalGenerator* lg : this->LocalGenerators) {
+    for (cmGeneratorTarget* gt : lg->GetGeneratorTargets()) {
+      if (gt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
+        continue;
+      }
+      lg->AddUnityBuild(gt);
+    }
+  }
+  return true;
+}
+
 cmLinkLineComputer* cmGlobalGenerator::CreateLinkLineComputer(
   cmOutputConverter* outputConverter, cmStateDirectory const& stateDir) const
 {
diff --git a/Source/cmGlobalGenerator.h b/Source/cmGlobalGenerator.h
index b7a8ac7..9e30b8f 100644
--- a/Source/cmGlobalGenerator.h
+++ b/Source/cmGlobalGenerator.h
@@ -503,6 +503,8 @@ protected:
   /// @return true on success
   bool QtAutoGen();
 
+  bool AddAutomaticSources();
+
   std::string SelectMakeProgram(const std::string& makeProgram,
                                 const std::string& makeDefault = "") const;
 
diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx
index f23e28d..8ae1e12 100644
--- a/Source/cmGlobalXCodeGenerator.cxx
+++ b/Source/cmGlobalXCodeGenerator.cxx
@@ -2827,7 +2827,6 @@ bool cmGlobalXCodeGenerator::CreateGroups(
         continue;
       }
 
-      generator->AddUnityBuild(gtgt, "");
       generator->AddPchDependencies(gtgt, "");
 
       auto addSourceToGroup = [this, mf, gtgt,
diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx
index 154d509..fc1426d 100644
--- a/Source/cmLocalGenerator.cxx
+++ b/Source/cmLocalGenerator.cxx
@@ -2437,13 +2437,18 @@ void cmLocalGenerator::AddPchDependencies(cmGeneratorTarget* target,
   }
 }
 
-void cmLocalGenerator::AddUnityBuild(cmGeneratorTarget* target,
-                                     const std::string& config)
+void cmLocalGenerator::AddUnityBuild(cmGeneratorTarget* target)
 {
   if (!target->GetPropertyAsBool("UNITY_BUILD")) {
     return;
   }
 
+  // FIXME: Handle all configurations in multi-config generators.
+  std::string config;
+  if (!this->GetGlobalGenerator()->IsMultiConfig()) {
+    config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
+  }
+
   const std::string buildType = cmSystemTools::UpperCase(config);
 
   std::string filename_base =
diff --git a/Source/cmLocalGenerator.h b/Source/cmLocalGenerator.h
index f150733..14d05ad 100644
--- a/Source/cmLocalGenerator.h
+++ b/Source/cmLocalGenerator.h
@@ -128,7 +128,7 @@ public:
                                 const std::string& rawFlag) const;
   void AddPchDependencies(cmGeneratorTarget* target,
                           const std::string& config);
-  void AddUnityBuild(cmGeneratorTarget* target, const std::string& config);
+  void AddUnityBuild(cmGeneratorTarget* target);
   void AppendIPOLinkerFlags(std::string& flags, cmGeneratorTarget* target,
                             const std::string& config,
                             const std::string& lang);
diff --git a/Source/cmLocalVisualStudio7Generator.cxx b/Source/cmLocalVisualStudio7Generator.cxx
index 6b0f802..e16e851 100644
--- a/Source/cmLocalVisualStudio7Generator.cxx
+++ b/Source/cmLocalVisualStudio7Generator.cxx
@@ -1313,7 +1313,6 @@ void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
                                                     const std::string& libName,
                                                     cmGeneratorTarget* target)
 {
-  this->AddUnityBuild(target, "");
   this->AddPchDependencies(target, "");
 
   std::vector<std::string> configs;
diff --git a/Source/cmMakefileExecutableTargetGenerator.cxx b/Source/cmMakefileExecutableTargetGenerator.cxx
index 91bd47e..0b225cb 100644
--- a/Source/cmMakefileExecutableTargetGenerator.cxx
+++ b/Source/cmMakefileExecutableTargetGenerator.cxx
@@ -41,7 +41,6 @@ cmMakefileExecutableTargetGenerator::cmMakefileExecutableTargetGenerator(
     cm::make_unique<cmOSXBundleGenerator>(target, this->ConfigName);
   this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
 
-  this->LocalGenerator->AddUnityBuild(target, this->ConfigName);
   this->LocalGenerator->AddPchDependencies(target, this->ConfigName);
 }
 
diff --git a/Source/cmMakefileLibraryTargetGenerator.cxx b/Source/cmMakefileLibraryTargetGenerator.cxx
index faa0d67..cf09374 100644
--- a/Source/cmMakefileLibraryTargetGenerator.cxx
+++ b/Source/cmMakefileLibraryTargetGenerator.cxx
@@ -43,7 +43,6 @@ cmMakefileLibraryTargetGenerator::cmMakefileLibraryTargetGenerator(
     cm::make_unique<cmOSXBundleGenerator>(target, this->ConfigName);
   this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
 
-  this->LocalGenerator->AddUnityBuild(target, this->ConfigName);
   this->LocalGenerator->AddPchDependencies(target, this->ConfigName);
 }
 
diff --git a/Source/cmMakefileUtilityTargetGenerator.cxx b/Source/cmMakefileUtilityTargetGenerator.cxx
index 47e2665..516f098 100644
--- a/Source/cmMakefileUtilityTargetGenerator.cxx
+++ b/Source/cmMakefileUtilityTargetGenerator.cxx
@@ -26,7 +26,6 @@ cmMakefileUtilityTargetGenerator::cmMakefileUtilityTargetGenerator(
     cm::make_unique<cmOSXBundleGenerator>(target, this->ConfigName);
   this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
 
-  this->LocalGenerator->AddUnityBuild(target, this->ConfigName);
   this->LocalGenerator->AddPchDependencies(target, this->ConfigName);
 }
 
diff --git a/Source/cmNinjaNormalTargetGenerator.cxx b/Source/cmNinjaNormalTargetGenerator.cxx
index 38ccaa3..3f362fc 100644
--- a/Source/cmNinjaNormalTargetGenerator.cxx
+++ b/Source/cmNinjaNormalTargetGenerator.cxx
@@ -62,7 +62,6 @@ cmNinjaNormalTargetGenerator::cmNinjaNormalTargetGenerator(
     cm::make_unique<cmOSXBundleGenerator>(target, this->GetConfigName());
   this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
 
-  GetLocalGenerator()->AddUnityBuild(target, this->GetConfigName());
   GetLocalGenerator()->AddPchDependencies(target, this->GetConfigName());
 }
 
diff --git a/Source/cmVisualStudio10TargetGenerator.cxx b/Source/cmVisualStudio10TargetGenerator.cxx
index d5f0c61..34556f9 100644
--- a/Source/cmVisualStudio10TargetGenerator.cxx
+++ b/Source/cmVisualStudio10TargetGenerator.cxx
@@ -251,7 +251,6 @@ cmVisualStudio10TargetGenerator::cmVisualStudio10TargetGenerator(
   this->InSourceBuild = (this->Makefile->GetCurrentSourceDirectory() ==
                          this->Makefile->GetCurrentBinaryDirectory());
 
-  this->LocalGenerator->AddUnityBuild(target, "");
   this->LocalGenerator->AddPchDependencies(target, "");
 }
 
-- 
cgit v0.12