瀏覽代碼

Refactoring cmRulePlaceHolderExpander: propagate cmBuildStep information

Marc Chevrier 1 年之前
父節點
當前提交
2bd4c06c26

+ 35 - 0
Source/cmGeneratorOptions.h

@@ -0,0 +1,35 @@
+/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
+   file Copyright.txt or https://cmake.org/licensing for details.  */
+#pragma once
+
+#include "cmConfigure.h" // IWYU pragma: keep
+
+/** Flag if byproducts shall also be considered.  */
+enum class cmSourceOutputKind
+{
+  OutputOnly,
+  OutputOrByproduct
+};
+
+/** What scanner to use for dependencies lookup.  */
+enum class cmDependencyScannerKind
+{
+  CMake,
+  Compiler
+};
+
+/** What to compute language flags for */
+enum class cmBuildStep
+{
+  Compile,
+  Link
+};
+
+/** What compilation mode the swift files are in */
+enum class cmSwiftCompileMode
+{
+  Wholemodule,
+  Incremental,
+  Singlefile,
+  Unknown,
+};

+ 1 - 0
Source/cmGeneratorTarget.cxx

@@ -30,6 +30,7 @@
 #include "cmGeneratedFileStream.h"
 #include "cmGeneratorExpression.h"
 #include "cmGeneratorExpressionDAGChecker.h"
+#include "cmGeneratorOptions.h"
 #include "cmGlobalGenerator.h"
 #include "cmList.h"
 #include "cmLocalGenerator.h"

+ 1 - 0
Source/cmGhsMultiTargetGenerator.cxx

@@ -15,6 +15,7 @@
 #include "cmCustomCommand.h"
 #include "cmCustomCommandGenerator.h"
 #include "cmGeneratedFileStream.h"
+#include "cmGeneratorOptions.h"
 #include "cmGeneratorTarget.h"
 #include "cmGlobalGhsMultiGenerator.h"
 #include "cmLinkLineComputer.h" // IWYU pragma: keep

+ 1 - 0
Source/cmGlobalXCodeGenerator.cxx

@@ -27,6 +27,7 @@
 #include "cmCustomCommandTypes.h"
 #include "cmGeneratedFileStream.h"
 #include "cmGeneratorExpression.h"
+#include "cmGeneratorOptions.h"
 #include "cmGeneratorTarget.h"
 #include "cmGlobalGeneratorFactory.h"
 #include "cmLinkItem.h"

+ 2 - 2
Source/cmLocalGenerator.cxx

@@ -208,10 +208,10 @@ cmLocalGenerator::cmLocalGenerator(cmGlobalGenerator* gg, cmMakefile* makefile)
 }
 
 std::unique_ptr<cmRulePlaceholderExpander>
-cmLocalGenerator::CreateRulePlaceholderExpander() const
+cmLocalGenerator::CreateRulePlaceholderExpander(cmBuildStep buildStep) const
 {
   return cm::make_unique<cmRulePlaceholderExpander>(
-    this->Compilers, this->VariableMappings, this->CompilerSysroot,
+    buildStep, this->Compilers, this->VariableMappings, this->CompilerSysroot,
     this->LinkerSysroot);
 }
 

+ 3 - 31
Source/cmLocalGenerator.h

@@ -19,6 +19,7 @@
 #include <cm3p/kwiml/int.h>
 
 #include "cmCustomCommandTypes.h"
+#include "cmGeneratorOptions.h"
 #include "cmGeneratorTarget.h"
 #include "cmListFileCache.h"
 #include "cmMessageType.h" // IWYU pragma: keep
@@ -46,36 +47,6 @@ class cmake;
 template <typename Iter>
 class cmRange;
 
-/** Flag if byproducts shall also be considered.  */
-enum class cmSourceOutputKind
-{
-  OutputOnly,
-  OutputOrByproduct
-};
-
-/** What scanner to use for dependencies lookup.  */
-enum class cmDependencyScannerKind
-{
-  CMake,
-  Compiler
-};
-
-/** What to compute language flags for */
-enum class cmBuildStep
-{
-  Compile,
-  Link
-};
-
-/** What compilation mode the swift files are in */
-enum class cmSwiftCompileMode
-{
-  Wholemodule,
-  Incremental,
-  Singlefile,
-  Unknown,
-};
-
 /** Target and source file which have a specific output.  */
 struct cmSourcesWithOutput
 {
@@ -147,7 +118,8 @@ public:
   }
 
   virtual std::unique_ptr<cmRulePlaceholderExpander>
-  CreateRulePlaceholderExpander() const;
+  CreateRulePlaceholderExpander(
+    cmBuildStep buildStep = cmBuildStep::Compile) const;
 
   std::string GetLinkLibsCMP0065(std::string const& linkLanguage,
                                  cmGeneratorTarget& tgt) const;

+ 3 - 2
Source/cmLocalNinjaGenerator.cxx

@@ -50,9 +50,10 @@ cmLocalNinjaGenerator::cmLocalNinjaGenerator(cmGlobalGenerator* gg,
 // Virtual public methods.
 
 std::unique_ptr<cmRulePlaceholderExpander>
-cmLocalNinjaGenerator::CreateRulePlaceholderExpander() const
+cmLocalNinjaGenerator::CreateRulePlaceholderExpander(
+  cmBuildStep buildStep) const
 {
-  auto ret = this->cmLocalGenerator::CreateRulePlaceholderExpander();
+  auto ret = this->cmLocalGenerator::CreateRulePlaceholderExpander(buildStep);
   ret->SetTargetImpLib("$TARGET_IMPLIB");
   return std::unique_ptr<cmRulePlaceholderExpander>(std::move(ret));
 }

+ 3 - 2
Source/cmLocalNinjaGenerator.h

@@ -11,6 +11,7 @@
 #include <string>
 #include <vector>
 
+#include "cmGeneratorOptions.h"
 #include "cmLocalCommonGenerator.h"
 #include "cmNinjaTypes.h"
 #include "cmOutputConverter.h"
@@ -42,8 +43,8 @@ public:
 
   void Generate() override;
 
-  std::unique_ptr<cmRulePlaceholderExpander> CreateRulePlaceholderExpander()
-    const override;
+  std::unique_ptr<cmRulePlaceholderExpander> CreateRulePlaceholderExpander(
+    cmBuildStep buildStep = cmBuildStep::Compile) const override;
 
   std::string GetTargetDirectory(
     cmGeneratorTarget const* target) const override;

+ 1 - 1
Source/cmLocalUnixMakefileGenerator3.h

@@ -12,8 +12,8 @@
 #include <vector>
 
 #include "cmDepends.h"
+#include "cmGeneratorOptions.h"
 #include "cmLocalCommonGenerator.h"
-#include "cmLocalGenerator.h"
 
 class cmCustomCommand;
 class cmCustomCommandGenerator;

+ 1 - 0
Source/cmLocalVisualStudio7Generator.cxx

@@ -25,6 +25,7 @@
 #include "cmCustomCommandLines.h"
 #include "cmGeneratedFileStream.h"
 #include "cmGeneratorExpression.h"
+#include "cmGeneratorOptions.h"
 #include "cmGeneratorTarget.h"
 #include "cmGlobalGenerator.h"
 #include "cmGlobalVisualStudio7Generator.h"

+ 3 - 2
Source/cmMakefileExecutableTargetGenerator.cxx

@@ -12,6 +12,7 @@
 #include <cmext/algorithm>
 
 #include "cmGeneratedFileStream.h"
+#include "cmGeneratorOptions.h"
 #include "cmGeneratorTarget.h"
 #include "cmGlobalUnixMakefileGenerator3.h"
 #include "cmLinkLineComputer.h"
@@ -233,7 +234,7 @@ void cmMakefileExecutableTargetGenerator::WriteNvidiaDeviceExecutableRule(
     }
 
     auto rulePlaceholderExpander =
-      this->LocalGenerator->CreateRulePlaceholderExpander();
+      this->LocalGenerator->CreateRulePlaceholderExpander(cmBuildStep::Link);
 
     // Expand placeholders in the commands.
     rulePlaceholderExpander->SetTargetImpLib(targetOutput);
@@ -603,7 +604,7 @@ void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink)
     }
 
     auto rulePlaceholderExpander =
-      this->LocalGenerator->CreateRulePlaceholderExpander();
+      this->LocalGenerator->CreateRulePlaceholderExpander(cmBuildStep::Link);
 
     // Expand placeholders in the commands.
     rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);

+ 3 - 2
Source/cmMakefileLibraryTargetGenerator.cxx

@@ -12,6 +12,7 @@
 #include <cmext/algorithm>
 
 #include "cmGeneratedFileStream.h"
+#include "cmGeneratorOptions.h"
 #include "cmGeneratorTarget.h"
 #include "cmGlobalUnixMakefileGenerator3.h"
 #include "cmLinkLineComputer.h"
@@ -385,7 +386,7 @@ void cmMakefileLibraryTargetGenerator::WriteNvidiaDeviceLibraryRules(
     }
 
     auto rulePlaceholderExpander =
-      this->LocalGenerator->CreateRulePlaceholderExpander();
+      this->LocalGenerator->CreateRulePlaceholderExpander(cmBuildStep::Link);
 
     // Construct the main link rule and expand placeholders.
     rulePlaceholderExpander->SetTargetImpLib(targetOutput);
@@ -710,7 +711,7 @@ void cmMakefileLibraryTargetGenerator::WriteLibraryRules(
 
   // Expand the rule variables.
   auto rulePlaceholderExpander =
-    this->LocalGenerator->CreateRulePlaceholderExpander();
+    this->LocalGenerator->CreateRulePlaceholderExpander(cmBuildStep::Link);
   bool useWatcomQuote =
     this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
   cmList real_link_commands;

+ 3 - 1
Source/cmMakefileTargetGenerator.cxx

@@ -25,6 +25,7 @@
 #include "cmFileSet.h"
 #include "cmGeneratedFileStream.h"
 #include "cmGeneratorExpression.h"
+#include "cmGeneratorOptions.h"
 #include "cmGeneratorTarget.h"
 #include "cmGlobalUnixMakefileGenerator3.h"
 #include "cmLinkLineComputer.h" // IWYU pragma: keep
@@ -1707,7 +1708,8 @@ void cmMakefileTargetGenerator::WriteDeviceLinkRule(
   vars.Flags = flags.c_str();
 
   std::string compileCmd = this->GetLinkRule("CMAKE_CUDA_DEVICE_LINK_COMPILE");
-  auto rulePlaceholderExpander = localGen->CreateRulePlaceholderExpander();
+  auto rulePlaceholderExpander =
+    localGen->CreateRulePlaceholderExpander(cmBuildStep::Link);
   rulePlaceholderExpander->ExpandRuleVariables(localGen, compileCmd, vars);
 
   commands.emplace_back(compileCmd);

+ 7 - 3
Source/cmNinjaNormalTargetGenerator.cxx

@@ -18,6 +18,7 @@
 #include "cmCustomCommand.h" // IWYU pragma: keep
 #include "cmCustomCommandGenerator.h"
 #include "cmGeneratedFileStream.h"
+#include "cmGeneratorOptions.h"
 #include "cmGeneratorTarget.h"
 #include "cmGlobalNinjaGenerator.h"
 #include "cmLinkLineComputer.h"
@@ -350,7 +351,8 @@ void cmNinjaNormalTargetGenerator::WriteNvidiaDeviceLinkRule(
     }
 
     auto rulePlaceholderExpander =
-      this->GetLocalGenerator()->CreateRulePlaceholderExpander();
+      this->GetLocalGenerator()->CreateRulePlaceholderExpander(
+        cmBuildStep::Link);
 
     // Rule for linking library/executable.
     std::vector<std::string> linkCmds = this->ComputeDeviceLinkCmd();
@@ -413,7 +415,8 @@ void cmNinjaNormalTargetGenerator::WriteDeviceLinkRules(
   std::string compileCmd = this->GetMakefile()->GetRequiredDefinition(
     "CMAKE_CUDA_DEVICE_LINK_COMPILE");
   auto rulePlaceholderExpander =
-    this->GetLocalGenerator()->CreateRulePlaceholderExpander();
+    this->GetLocalGenerator()->CreateRulePlaceholderExpander(
+      cmBuildStep::Link);
   rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
                                                compileCmd, vars);
 
@@ -566,7 +569,8 @@ void cmNinjaNormalTargetGenerator::WriteLinkRule(bool useResponseFile,
     }
 
     auto rulePlaceholderExpander =
-      this->GetLocalGenerator()->CreateRulePlaceholderExpander();
+      this->GetLocalGenerator()->CreateRulePlaceholderExpander(
+        cmBuildStep::Link);
 
     // Rule for linking library/executable.
     std::vector<std::string> linkCmds = this->ComputeLinkCmd(config);

+ 1 - 0
Source/cmNinjaTargetGenerator.cxx

@@ -29,6 +29,7 @@
 #include "cmFileSet.h"
 #include "cmGeneratedFileStream.h"
 #include "cmGeneratorExpression.h"
+#include "cmGeneratorOptions.h"
 #include "cmGeneratorTarget.h"
 #include "cmGlobalCommonGenerator.h"
 #include "cmGlobalNinjaGenerator.h"

+ 5 - 5
Source/cmRulePlaceholderExpander.cxx

@@ -9,10 +9,11 @@
 #include "cmSystemTools.h"
 
 cmRulePlaceholderExpander::cmRulePlaceholderExpander(
-  std::map<std::string, std::string> compilers,
+  cmBuildStep buildStep, std::map<std::string, std::string> compilers,
   std::map<std::string, std::string> variableMappings,
   std::string compilerSysroot, std::string linkerSysroot)
-  : Compilers(std::move(compilers))
+  : BuildStep(buildStep)
+  , Compilers(std::move(compilers))
   , VariableMappings(std::move(variableMappings))
   , CompilerSysroot(std::move(compilerSysroot))
   , LinkerSysroot(std::move(linkerSysroot))
@@ -320,9 +321,8 @@ std::string cmRulePlaceholderExpander::ExpandVariable(
     }
     std::string sysroot;
     // Some platforms may use separate sysroots for compiling and linking.
-    // If we detect link flags, then we pass the link sysroot instead.
-    // FIXME: Use a more robust way to detect link line expansion.
-    if (this->ReplaceValues->LinkFlags) {
+    // When the build step is link, pass the link sysroot instead.
+    if (this->BuildStep == cmBuildStep::Link) {
       sysroot = this->LinkerSysroot;
     } else {
       sysroot = this->CompilerSysroot;

+ 3 - 1
Source/cmRulePlaceholderExpander.h

@@ -8,6 +8,7 @@
 #include <map>
 #include <string>
 
+#include "cmGeneratorOptions.h"
 #include "cmPlaceholderExpander.h"
 
 class cmOutputConverter;
@@ -16,7 +17,7 @@ class cmRulePlaceholderExpander : public cmPlaceholderExpander
 {
 public:
   cmRulePlaceholderExpander(
-    std::map<std::string, std::string> compilers,
+    cmBuildStep buildStep, std::map<std::string, std::string> compilers,
     std::map<std::string, std::string> variableMappings,
     std::string compilerSysroot, std::string linkerSysroot);
 
@@ -84,6 +85,7 @@ private:
 
   std::string TargetImpLib;
 
+  cmBuildStep BuildStep = cmBuildStep::Compile;
   std::map<std::string, std::string> Compilers;
   std::map<std::string, std::string> VariableMappings;
   std::string CompilerSysroot;

+ 1 - 0
Source/cmVisualStudio10TargetGenerator.cxx

@@ -30,6 +30,7 @@
 #include "cmFileSet.h"
 #include "cmGeneratedFileStream.h"
 #include "cmGeneratorExpression.h"
+#include "cmGeneratorOptions.h"
 #include "cmGeneratorTarget.h"
 #include "cmGlobalGenerator.h"
 #include "cmGlobalVisualStudio10Generator.h"