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.
This commit is contained in:
Kyle Edwards
2023-02-06 10:25:45 -05:00
parent dad1234ba1
commit 480b363724
12 changed files with 34 additions and 33 deletions

View File

@@ -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

View File

@@ -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
};

View File

@@ -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(),

View File

@@ -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 =

View File

@@ -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 =

View File

@@ -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);

View File

@@ -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()) {

View File

@@ -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);

View File

@@ -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(

View File

@@ -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
*

View File

@@ -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));

View File

@@ -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);