Browse Source

cmCustomCommand: Refactor custom command-specific policy values

Many custom commands are created by CMake itself rather than by
the user. These custom commands should always have their policies
set to NEW, and user-created custom commands should have their
policy values set only from the state snapshot. In addition, we
want to genericize the mechanism of recording a policy at the time
of custom command creation.

Add a CM_FOR_EACH_CUSTOM_COMMAND_POLICY macro to genericize
custom command policies. Use this to define all custom command
policies. Make all such policies NEW instead of WARN by default.
Remove individual policy modifier methods and add a single method
that records relevant values from a cmStateSnapshot. Remove the
no longer needed explicit policy settings from synthesized custom
commands.
Kyle Edwards 2 years ago
parent
commit
480b363724

+ 13 - 6
Source/cmCustomCommand.cxx

@@ -7,6 +7,8 @@
 
 #include <cmext/algorithm>
 
+#include "cmStateSnapshot.h"
+
 const std::vector<std::string>& cmCustomCommand::GetOutputs() const
 {
   return this->Outputs;
@@ -182,14 +184,19 @@ void cmCustomCommand::SetJobPool(const std::string& job_pool)
   this->JobPool = job_pool;
 }
 
-cmPolicies::PolicyStatus cmCustomCommand::GetCMP0116Status() const
-{
-  return this->CMP0116Status;
-}
+#define DEFINE_CC_POLICY_ACCESSOR(P)                                          \
+  cmPolicies::PolicyStatus cmCustomCommand::Get##P##Status() const            \
+  {                                                                           \
+    return this->P##Status;                                                   \
+  }
+CM_FOR_EACH_CUSTOM_COMMAND_POLICY(DEFINE_CC_POLICY_ACCESSOR)
+#undef DEFINE_CC_POLICY_ACCESSOR
 
-void cmCustomCommand::SetCMP0116Status(cmPolicies::PolicyStatus cmp0116)
+void cmCustomCommand::RecordPolicyValues(const cmStateSnapshot& snapshot)
 {
-  this->CMP0116Status = cmp0116;
+#define SET_CC_POLICY(P) this->P##Status = snapshot.GetPolicy(cmPolicies::P);
+  CM_FOR_EACH_CUSTOM_COMMAND_POLICY(SET_CC_POLICY)
+#undef SET_CC_POLICY
 }
 
 const std::string& cmCustomCommand::GetTarget() const

+ 16 - 4
Source/cmCustomCommand.h

@@ -17,6 +17,8 @@ class cmImplicitDependsList
 {
 };
 
+class cmStateSnapshot;
+
 /** \class cmCustomCommand
  * \brief A class to encapsulate a custom command
  *
@@ -108,9 +110,13 @@ public:
   const std::string& GetJobPool() const;
   void SetJobPool(const std::string& job_pool);
 
-  /** Set/Get the CMP0116 status (used by the Ninja generator) */
-  cmPolicies::PolicyStatus GetCMP0116Status() const;
-  void SetCMP0116Status(cmPolicies::PolicyStatus cmp0116);
+#define DECLARE_CC_POLICY_ACCESSOR(P)                                         \
+  cmPolicies::PolicyStatus Get##P##Status() const;
+  CM_FOR_EACH_CUSTOM_COMMAND_POLICY(DECLARE_CC_POLICY_ACCESSOR)
+#undef DECLARE_CC_POLICY_ACCESSOR
+
+  /** Record policy values from state snapshot */
+  void RecordPolicyValues(const cmStateSnapshot& snapshot);
 
   /** Set/Get the associated target */
   const std::string& GetTarget() const;
@@ -135,5 +141,11 @@ private:
   bool CommandExpandLists = false;
   bool StdPipesUTF8 = false;
   bool HasMainDependency_ = false;
-  cmPolicies::PolicyStatus CMP0116Status = cmPolicies::WARN;
+
+// Policies are NEW for synthesized custom commands, and set by cmMakefile for
+// user-created custom commands.
+#define DECLARE_CC_POLICY_FIELD(P)                                            \
+  cmPolicies::PolicyStatus P##Status = cmPolicies::NEW;
+  CM_FOR_EACH_CUSTOM_COMMAND_POLICY(DECLARE_CC_POLICY_FIELD)
+#undef DECLARE_CC_POLICY_FIELD
 };

+ 0 - 3
Source/cmGlobalGhsMultiGenerator.cxx

@@ -22,7 +22,6 @@
 #include "cmLocalGhsMultiGenerator.h"
 #include "cmMakefile.h"
 #include "cmMessageType.h"
-#include "cmPolicies.h"
 #include "cmSourceFile.h"
 #include "cmState.h"
 #include "cmStateTypes.h"
@@ -717,7 +716,6 @@ bool cmGlobalGhsMultiGenerator::AddCheckTarget()
     cc->SetDepends(listFiles);
     cc->SetCommandLines(commandLines);
     cc->SetComment("Checking Build System");
-    cc->SetCMP0116Status(cmPolicies::NEW);
     cc->SetEscapeOldStyle(false);
     cc->SetStdPipesUTF8(true);
 
@@ -747,7 +745,6 @@ void cmGlobalGhsMultiGenerator::AddAllTarget()
       // Use no actual command lines so that the target itself is not
       // considered always out of date.
       auto cc = cm::make_unique<cmCustomCommand>();
-      cc->SetCMP0116Status(cmPolicies::NEW);
       cc->SetEscapeOldStyle(false);
       cc->SetComment("Build all projects");
       cmTarget* allBuild = gen[0]->AddUtilityCommand(this->GetAllTargetName(),

+ 0 - 3
Source/cmGlobalVisualStudio8Generator.cxx

@@ -169,7 +169,6 @@ bool cmGlobalVisualStudio8Generator::AddCheckTarget()
     cm::static_reference_cast<cmLocalVisualStudio7Generator>(generators[0]);
 
   auto cc = cm::make_unique<cmCustomCommand>();
-  cc->SetCMP0116Status(cmPolicies::NEW);
   cmTarget* tgt = lg.AddUtilityCommand(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false,
                                        std::move(cc));
 
@@ -225,7 +224,6 @@ bool cmGlobalVisualStudio8Generator::AddCheckTarget()
       cc->SetByproducts(byproducts);
       cc->SetCommandLines(verifyCommandLines);
       cc->SetComment("Checking File Globs");
-      cc->SetCMP0116Status(cmPolicies::NEW);
       cc->SetStdPipesUTF8(stdPipesUTF8);
       lg.AddCustomCommandToTarget(CMAKE_CHECK_BUILD_SYSTEM_TARGET,
                                   cmCustomCommandType::PRE_BUILD,
@@ -260,7 +258,6 @@ bool cmGlobalVisualStudio8Generator::AddCheckTarget()
     cc->SetDepends(listFiles);
     cc->SetCommandLines(commandLines);
     cc->SetComment("Checking Build System");
-    cc->SetCMP0116Status(cmPolicies::NEW);
     cc->SetEscapeOldStyle(false);
     cc->SetStdPipesUTF8(stdPipesUTF8);
     if (cmSourceFile* file =

+ 0 - 1
Source/cmGlobalVisualStudioGenerator.cxx

@@ -201,7 +201,6 @@ void cmGlobalVisualStudioGenerator::AddExtraIDETargets()
       // Use no actual command lines so that the target itself is not
       // considered always out of date.
       auto cc = cm::make_unique<cmCustomCommand>();
-      cc->SetCMP0116Status(cmPolicies::NEW);
       cc->SetEscapeOldStyle(false);
       cc->SetComment("Build all projects");
       cmTarget* allBuild =

+ 0 - 3
Source/cmGlobalXCodeGenerator.cxx

@@ -615,7 +615,6 @@ void cmGlobalXCodeGenerator::AddExtraTargets(
   auto cc = cm::make_unique<cmCustomCommand>();
   cc->SetCommandLines(
     cmMakeSingleCommandLine({ "echo", "Build all projects" }));
-  cc->SetCMP0116Status(cmPolicies::NEW);
   cmTarget* allbuild =
     root->AddUtilityCommand("ALL_BUILD", true, std::move(cc));
 
@@ -655,7 +654,6 @@ void cmGlobalXCodeGenerator::AddExtraTargets(
     cmSystemTools::ReplaceString(file, "\\ ", " ");
     cc = cm::make_unique<cmCustomCommand>();
     cc->SetCommandLines(cmMakeSingleCommandLine({ "make", "-f", file }));
-    cc->SetCMP0116Status(cmPolicies::NEW);
     cmTarget* check = root->AddUtilityCommand(CMAKE_CHECK_BUILD_SYSTEM_TARGET,
                                               true, std::move(cc));
 
@@ -687,7 +685,6 @@ void cmGlobalXCodeGenerator::AddExtraTargets(
         cc->SetCommandLines(legacyDependHelperCommandLines);
         cc->SetComment("Depend check for xcode");
         cc->SetWorkingDirectory(legacyDependHelperDir.c_str());
-        cc->SetCMP0116Status(cmPolicies::NEW);
         gen->AddCustomCommandToTarget(
           target->GetName(), cmCustomCommandType::POST_BUILD, std::move(cc),
           cmObjectLibraryCommands::Accept);

+ 0 - 1
Source/cmLocalGenerator.cxx

@@ -2841,7 +2841,6 @@ void cmLocalGenerator::CopyPchCompilePdb(
   auto cc = cm::make_unique<cmCustomCommand>();
   cc->SetCommandLines(commandLines);
   cc->SetComment(no_message);
-  cc->SetCMP0116Status(cmPolicies::NEW);
   cc->SetStdPipesUTF8(true);
 
   if (this->GetGlobalGenerator()->IsVisualStudio()) {

+ 0 - 2
Source/cmLocalVisualStudio7Generator.cxx

@@ -141,7 +141,6 @@ void cmLocalVisualStudio7Generator::FixGlobalTargets()
       cc->SetOutputs(force);
       cc->SetCommandLines(force_commands);
       cc->SetComment(" ");
-      cc->SetCMP0116Status(cmPolicies::NEW);
       if (cmSourceFile* file =
             this->AddCustomCommandToOutput(std::move(cc), true)) {
         l->AddSource(file->ResolveFullPath());
@@ -269,7 +268,6 @@ cmSourceFile* cmLocalVisualStudio7Generator::CreateVCProjBuildRule()
   cc->SetDepends(listFiles);
   cc->SetCommandLines(commandLines);
   cc->SetComment(comment.c_str());
-  cc->SetCMP0116Status(cmPolicies::NEW);
   cc->SetEscapeOldStyle(false);
   cc->SetStdPipesUTF8(true);
   this->AddCustomCommandToOutput(std::move(cc), true);

+ 3 - 3
Source/cmMakefile.cxx

@@ -1117,7 +1117,7 @@ cmTarget* cmMakefile::AddCustomCommandToTarget(
   // Always create the byproduct sources and mark them generated.
   this->CreateGeneratedOutputs(byproducts);
 
-  cc->SetCMP0116Status(this->GetPolicyStatus(cmPolicies::CMP0116));
+  cc->RecordPolicyValues(this->GetStateSnapshot());
 
   // Dispatch command creation to allow generator expressions in outputs.
   this->AddGeneratorAction(
@@ -1156,7 +1156,7 @@ void cmMakefile::AddCustomCommandToOutput(
   this->CreateGeneratedOutputs(outputs);
   this->CreateGeneratedOutputs(byproducts);
 
-  cc->SetCMP0116Status(this->GetPolicyStatus(cmPolicies::CMP0116));
+  cc->RecordPolicyValues(this->GetStateSnapshot());
 
   // Dispatch command creation to allow generator expressions in outputs.
   this->AddGeneratorAction(
@@ -1274,7 +1274,7 @@ cmTarget* cmMakefile::AddUtilityCommand(const std::string& utilityName,
   // Always create the byproduct sources and mark them generated.
   this->CreateGeneratedOutputs(byproducts);
 
-  cc->SetCMP0116Status(this->GetPolicyStatus(cmPolicies::CMP0116));
+  cc->RecordPolicyValues(this->GetStateSnapshot());
 
   // Dispatch command creation to allow generator expressions in outputs.
   this->AddGeneratorAction(

+ 2 - 0
Source/cmPolicies.h

@@ -474,6 +474,8 @@ class cmMakefile;
   F(CMP0131)                                                                  \
   F(CMP0142)
 
+#define CM_FOR_EACH_CUSTOM_COMMAND_POLICY(F) F(CMP0116)
+
 /** \class cmPolicies
  * \brief Handles changes in CMake behavior and policies
  *

+ 0 - 2
Source/cmQtAutoGenGlobalInitializer.cxx

@@ -13,7 +13,6 @@
 #include "cmLocalGenerator.h"
 #include "cmMakefile.h"
 #include "cmMessageType.h"
-#include "cmPolicies.h"
 #include "cmProcessOutput.h"
 #include "cmQtAutoGen.h"
 #include "cmQtAutoGenInitializer.h"
@@ -173,7 +172,6 @@ void cmQtAutoGenGlobalInitializer::GetOrCreateGlobalTarget(
     // Create utility target
     auto cc = cm::make_unique<cmCustomCommand>();
     cc->SetWorkingDirectory(makefile->GetHomeOutputDirectory().c_str());
-    cc->SetCMP0116Status(cmPolicies::NEW);
     cc->SetEscapeOldStyle(false);
     cc->SetComment(comment.c_str());
     cmTarget* target = localGen->AddUtilityCommand(name, true, std::move(cc));

+ 0 - 5
Source/cmQtAutoGenInitializer.cxx

@@ -1238,7 +1238,6 @@ bool cmQtAutoGenInitializer::InitAutogenTarget()
       cc->SetDepends(uicDependencies);
       cc->SetComment("");
       cc->SetWorkingDirectory(this->Dir.Work.c_str());
-      cc->SetCMP0116Status(cmPolicies::NEW);
       cc->SetEscapeOldStyle(false);
       cc->SetStdPipesUTF8(stdPipesUTF8);
       this->LocalGen->AddCustomCommandToOutput(std::move(cc));
@@ -1332,7 +1331,6 @@ bool cmQtAutoGenInitializer::InitAutogenTarget()
       cc->SetByproducts(timestampTargetProvides);
       cc->SetDepends(dependencies);
       cc->SetCommandLines(timestampTargetCommandLines);
-      cc->SetCMP0116Status(cmPolicies::NEW);
       cc->SetEscapeOldStyle(false);
       cmTarget* timestampTarget = this->LocalGen->AddUtilityCommand(
         timestampTargetName, true, std::move(cc));
@@ -1371,7 +1369,6 @@ bool cmQtAutoGenInitializer::InitAutogenTarget()
       cc->SetCommandLines(commandLines);
       cc->SetComment(autogenComment.c_str());
       cc->SetWorkingDirectory(this->Dir.Work.c_str());
-      cc->SetCMP0116Status(cmPolicies::NEW);
       cc->SetEscapeOldStyle(false);
       cc->SetDepfile(this->AutogenTarget.DepFile);
       cc->SetStdPipesUTF8(stdPipesUTF8);
@@ -1391,7 +1388,6 @@ bool cmQtAutoGenInitializer::InitAutogenTarget()
     cc->SetByproducts(autogenByproducts);
     cc->SetDepends(dependencies);
     cc->SetCommandLines(commandLines);
-    cc->SetCMP0116Status(cmPolicies::NEW);
     cc->SetEscapeOldStyle(false);
     cc->SetComment(autogenComment.c_str());
     cmTarget* autogenTarget = this->LocalGen->AddUtilityCommand(
@@ -1472,7 +1468,6 @@ bool cmQtAutoGenInitializer::InitRccTargets()
     auto cc = cm::make_unique<cmCustomCommand>();
     cc->SetWorkingDirectory(this->Dir.Work.c_str());
     cc->SetCommandLines(commandLines);
-    cc->SetCMP0116Status(cmPolicies::NEW);
     cc->SetComment(ccComment.c_str());
     cc->SetStdPipesUTF8(true);