cmMakefile: Simplify detail:::Add{Custom,Utility}Command

Note 1: `detail::AddCustomCommandToTarget()` resets cc,
since cc is not moved away.

Note 2: In `detail::AddUtilityCommand()`, a few vars are preserved
before using. Their refs will be alive in most cases, but cc might
be destroyed in the future.
This commit is contained in:
NAKAMURA Takumi
2021-11-14 17:22:33 +09:00
committed by Brad King
parent 90e1206f25
commit e37511ae7e
4 changed files with 180 additions and 185 deletions

View File

@@ -1072,11 +1072,22 @@ cmTarget* cmLocalGenerator::AddCustomCommandToTarget(
return nullptr;
}
detail::AddCustomCommandToTarget(
*this, this->DirectoryBacktrace, cmCommandOrigin::Generator, t, byproducts,
depends, commandLines, type, comment, workingDir, escapeOldStyle,
uses_terminal, depfile, job_pool, command_expand_lists, stdPipesUTF8,
cmp0116);
auto cc = cm::make_unique<cmCustomCommand>();
cc->SetBacktrace(this->DirectoryBacktrace);
cc->SetByproducts(byproducts);
cc->SetDepends(depends);
cc->SetCommandLines(commandLines);
cc->SetComment(comment);
cc->SetWorkingDirectory(workingDir);
cc->SetEscapeOldStyle(escapeOldStyle);
cc->SetUsesTerminal(uses_terminal);
cc->SetDepfile(depfile);
cc->SetJobPool(job_pool);
cc->SetCommandExpandLists(command_expand_lists);
cc->SetStdPipesUTF8(stdPipesUTF8);
cc->SetCMP0116Status(cmp0116);
detail::AddCustomCommandToTarget(*this, cmCommandOrigin::Generator, t, type,
std::move(cc));
return t;
}
@@ -1113,11 +1124,25 @@ cmSourceFile* cmLocalGenerator::AddCustomCommandToOutput(
return nullptr;
}
return detail::AddCustomCommandToOutput(
*this, this->DirectoryBacktrace, cmCommandOrigin::Generator, outputs,
byproducts, depends, main_dependency, implicit_depends, commandLines,
comment, workingDir, replace, escapeOldStyle, uses_terminal,
command_expand_lists, depfile, job_pool, stdPipesUTF8, cmp0116);
auto cc = cm::make_unique<cmCustomCommand>();
cc->SetBacktrace(this->DirectoryBacktrace);
cc->SetOutputs(outputs);
cc->SetByproducts(byproducts);
cc->SetDepends(depends);
cc->SetImplicitDepends(implicit_depends);
cc->SetCommandLines(commandLines);
cc->SetComment(comment);
cc->SetWorkingDirectory(workingDir);
cc->SetEscapeOldStyle(escapeOldStyle);
cc->SetUsesTerminal(uses_terminal);
cc->SetCommandExpandLists(command_expand_lists);
cc->SetDepfile(depfile);
cc->SetJobPool(job_pool);
cc->SetStdPipesUTF8(stdPipesUTF8);
cc->SetCMP0116Status(cmp0116);
return detail::AddCustomCommandToOutput(*this, cmCommandOrigin::Generator,
main_dependency, std::move(cc),
replace);
}
cmTarget* cmLocalGenerator::AddUtilityCommand(
@@ -1136,10 +1161,21 @@ cmTarget* cmLocalGenerator::AddUtilityCommand(
return target;
}
detail::AddUtilityCommand(
*this, this->DirectoryBacktrace, cmCommandOrigin::Generator, target,
workingDir, byproducts, depends, commandLines, escapeOldStyle, comment,
uses_terminal, command_expand_lists, job_pool, stdPipesUTF8, cmp0116);
auto cc = cm::make_unique<cmCustomCommand>();
cc->SetBacktrace(this->DirectoryBacktrace);
cc->SetWorkingDirectory(workingDir);
cc->SetByproducts(byproducts);
cc->SetDepends(depends);
cc->SetCommandLines(commandLines);
cc->SetEscapeOldStyle(escapeOldStyle);
cc->SetComment(comment);
cc->SetUsesTerminal(uses_terminal);
cc->SetCommandExpandLists(command_expand_lists);
cc->SetJobPool(job_pool);
cc->SetStdPipesUTF8(stdPipesUTF8);
cc->SetCMP0116Status(cmp0116);
detail::AddUtilityCommand(*this, cmCommandOrigin::Generator, target,
std::move(cc));
return target;
}
@@ -4051,19 +4087,16 @@ std::string ComputeCustomCommandRuleFileName(cmLocalGenerator& lg,
h.HashString(output).substr(0, 16));
}
cmSourceFile* AddCustomCommand(
cmLocalGenerator& lg, const cmListFileBacktrace& lfbt,
cmCommandOrigin origin, const std::vector<std::string>& outputs,
const std::vector<std::string>& byproducts,
const std::vector<std::string>& depends, const std::string& main_dependency,
const cmImplicitDependsList& implicit_depends,
const cmCustomCommandLines& commandLines, const char* comment,
const char* workingDir, bool replace, bool escapeOldStyle,
bool uses_terminal, bool command_expand_lists, const std::string& depfile,
const std::string& job_pool, bool stdPipesUTF8,
cmPolicies::PolicyStatus cmp0116)
cmSourceFile* AddCustomCommand(cmLocalGenerator& lg, cmCommandOrigin origin,
const std::string& main_dependency,
std::unique_ptr<cmCustomCommand> cc,
bool replace)
{
cmMakefile* mf = lg.GetMakefile();
const auto& lfbt = cc->GetBacktrace();
const auto& outputs = cc->GetOutputs();
const auto& byproducts = cc->GetByproducts();
const auto& commandLines = cc->GetCommandLines();
// Choose a source file on which to store the custom command.
cmSourceFile* file = nullptr;
@@ -4118,34 +4151,18 @@ cmSourceFile* AddCustomCommand(
// Attach the custom command to the file.
if (file) {
// Construct a complete list of dependencies.
std::vector<std::string> depends2(depends);
if (!main_dependency.empty()) {
depends2.push_back(main_dependency);
cc->AppendDepends({ main_dependency });
}
std::unique_ptr<cmCustomCommand> cc = cm::make_unique<cmCustomCommand>();
cc->SetByproducts(byproducts);
cc->SetDepends(std::move(depends2));
cc->SetOutputs(outputs);
cc->SetCommandLines(commandLines);
cc->SetComment(comment);
cc->SetBacktrace(lfbt);
cc->SetWorkingDirectory(workingDir);
cc->SetStdPipesUTF8(stdPipesUTF8);
cc->SetEscapeOldStyle(escapeOldStyle);
cc->SetEscapeAllowMakeVars(true);
cc->SetImplicitDepends(implicit_depends);
cc->SetUsesTerminal(uses_terminal);
cc->SetCommandExpandLists(command_expand_lists);
cc->SetDepfile(depfile);
cc->SetJobPool(job_pool);
cc->SetCMP0116Status(cmp0116);
file->SetCustomCommand(std::move(cc));
lg.AddSourceOutputs(file, outputs, cmLocalGenerator::OutputRole::Primary,
lfbt, origin);
lg.AddSourceOutputs(file, byproducts,
cmLocalGenerator::OutputRole::Byproduct, lfbt, origin);
file->SetCustomCommand(std::move(cc));
}
return file;
}
@@ -4174,68 +4191,39 @@ bool AnyTargetCommandOutputMatches(
}
namespace detail {
void AddCustomCommandToTarget(cmLocalGenerator& lg,
const cmListFileBacktrace& lfbt,
cmCommandOrigin origin, cmTarget* target,
const std::vector<std::string>& byproducts,
const std::vector<std::string>& depends,
const cmCustomCommandLines& commandLines,
cmCustomCommandType type, const char* comment,
const char* workingDir, bool escapeOldStyle,
bool uses_terminal, const std::string& depfile,
const std::string& job_pool,
bool command_expand_lists, bool stdPipesUTF8,
cmPolicies::PolicyStatus cmp0116)
void AddCustomCommandToTarget(cmLocalGenerator& lg, cmCommandOrigin origin,
cmTarget* target, cmCustomCommandType type,
std::unique_ptr<cmCustomCommand> cc)
{
// Add the command to the appropriate build step for the target.
cmCustomCommand cc;
cc.SetByproducts(byproducts);
cc.SetDepends(depends);
cc.SetCommandLines(commandLines);
cc.SetComment(comment);
cc.SetBacktrace(lfbt);
cc.SetWorkingDirectory(workingDir);
cc.SetStdPipesUTF8(stdPipesUTF8);
cc.SetEscapeOldStyle(escapeOldStyle);
cc.SetEscapeAllowMakeVars(true);
cc.SetUsesTerminal(uses_terminal);
cc.SetCommandExpandLists(command_expand_lists);
cc.SetDepfile(depfile);
cc.SetJobPool(job_pool);
cc.SetCMP0116Status(cmp0116);
cc.SetTarget(target->GetName());
cc->SetEscapeAllowMakeVars(true);
cc->SetTarget(target->GetName());
lg.AddTargetByproducts(target, cc->GetByproducts(), cc->GetBacktrace(),
origin);
switch (type) {
case cmCustomCommandType::PRE_BUILD:
target->AddPreBuildCommand(std::move(cc));
target->AddPreBuildCommand(std::move(*cc));
break;
case cmCustomCommandType::PRE_LINK:
target->AddPreLinkCommand(std::move(cc));
target->AddPreLinkCommand(std::move(*cc));
break;
case cmCustomCommandType::POST_BUILD:
target->AddPostBuildCommand(std::move(cc));
target->AddPostBuildCommand(std::move(*cc));
break;
}
lg.AddTargetByproducts(target, byproducts, lfbt, origin);
cc.reset();
}
cmSourceFile* AddCustomCommandToOutput(
cmLocalGenerator& lg, const cmListFileBacktrace& lfbt,
cmCommandOrigin origin, const std::vector<std::string>& outputs,
const std::vector<std::string>& byproducts,
const std::vector<std::string>& depends, const std::string& main_dependency,
const cmImplicitDependsList& implicit_depends,
const cmCustomCommandLines& commandLines, const char* comment,
const char* workingDir, bool replace, bool escapeOldStyle,
bool uses_terminal, bool command_expand_lists, const std::string& depfile,
const std::string& job_pool, bool stdPipesUTF8,
cmPolicies::PolicyStatus cmp0116)
cmSourceFile* AddCustomCommandToOutput(cmLocalGenerator& lg,
cmCommandOrigin origin,
const std::string& main_dependency,
std::unique_ptr<cmCustomCommand> cc,
bool replace)
{
return AddCustomCommand(lg, lfbt, origin, outputs, byproducts, depends,
main_dependency, implicit_depends, commandLines,
comment, workingDir, replace, escapeOldStyle,
uses_terminal, command_expand_lists, depfile,
job_pool, stdPipesUTF8, cmp0116);
return AddCustomCommand(lg, origin, main_dependency, std::move(cc), replace);
}
void AppendCustomCommandToOutput(cmLocalGenerator& lg,
@@ -4278,33 +4266,27 @@ void AppendCustomCommandToOutput(cmLocalGenerator& lg,
lfbt);
}
void AddUtilityCommand(cmLocalGenerator& lg, const cmListFileBacktrace& lfbt,
cmCommandOrigin origin, cmTarget* target,
const char* workingDir,
const std::vector<std::string>& byproducts,
const std::vector<std::string>& depends,
const cmCustomCommandLines& commandLines,
bool escapeOldStyle, const char* comment,
bool uses_terminal, bool command_expand_lists,
const std::string& job_pool, bool stdPipesUTF8,
cmPolicies::PolicyStatus cmp0116)
void AddUtilityCommand(cmLocalGenerator& lg, cmCommandOrigin origin,
cmTarget* target, std::unique_ptr<cmCustomCommand> cc)
{
// They might be moved away
auto byproducts = cc->GetByproducts();
auto lfbt = cc->GetBacktrace();
// Use an empty comment to avoid generation of default comment.
if (!comment) {
comment = "";
if (!cc->GetComment()) {
cc->SetComment("");
}
// Create the generated symbolic output name of the utility target.
std::string output =
lg.CreateUtilityOutput(target->GetName(), byproducts, lfbt);
cc->SetOutputs(output);
std::string no_main_dependency;
cmImplicitDependsList no_implicit_depends;
cmSourceFile* rule = AddCustomCommand(
lg, lfbt, origin, { output }, byproducts, depends, no_main_dependency,
no_implicit_depends, commandLines, comment, workingDir,
/*replace=*/false, escapeOldStyle, uses_terminal, command_expand_lists,
/*depfile=*/"", job_pool, stdPipesUTF8, cmp0116);
cmSourceFile* rule =
AddCustomCommand(lg, origin, no_main_dependency, std::move(cc),
/*replace=*/false);
if (rule) {
lg.AddTargetByproducts(target, byproducts, lfbt, origin);
}

View File

@@ -713,30 +713,15 @@ bool cmLocalGeneratorCheckObjectName(std::string& objName,
#endif
namespace detail {
void AddCustomCommandToTarget(cmLocalGenerator& lg,
const cmListFileBacktrace& lfbt,
cmCommandOrigin origin, cmTarget* target,
const std::vector<std::string>& byproducts,
const std::vector<std::string>& depends,
const cmCustomCommandLines& commandLines,
cmCustomCommandType type, const char* comment,
const char* workingDir, bool escapeOldStyle,
bool uses_terminal, const std::string& depfile,
const std::string& job_pool,
bool command_expand_lists, bool stdPipesUTF8,
cmPolicies::PolicyStatus cmp0116);
void AddCustomCommandToTarget(cmLocalGenerator& lg, cmCommandOrigin origin,
cmTarget* target, cmCustomCommandType type,
std::unique_ptr<cmCustomCommand> cc);
cmSourceFile* AddCustomCommandToOutput(
cmLocalGenerator& lg, const cmListFileBacktrace& lfbt,
cmCommandOrigin origin, const std::vector<std::string>& outputs,
const std::vector<std::string>& byproducts,
const std::vector<std::string>& depends, const std::string& main_dependency,
const cmImplicitDependsList& implicit_depends,
const cmCustomCommandLines& commandLines, const char* comment,
const char* workingDir, bool replace, bool escapeOldStyle,
bool uses_terminal, bool command_expand_lists, const std::string& depfile,
const std::string& job_pool, bool stdPipesUTF8,
cmPolicies::PolicyStatus cmp0116);
cmSourceFile* AddCustomCommandToOutput(cmLocalGenerator& lg,
cmCommandOrigin origin,
const std::string& main_dependency,
std::unique_ptr<cmCustomCommand> cc,
bool replace);
void AppendCustomCommandToOutput(cmLocalGenerator& lg,
const cmListFileBacktrace& lfbt,
@@ -745,16 +730,8 @@ void AppendCustomCommandToOutput(cmLocalGenerator& lg,
const cmImplicitDependsList& implicit_depends,
const cmCustomCommandLines& commandLines);
void AddUtilityCommand(cmLocalGenerator& lg, const cmListFileBacktrace& lfbt,
cmCommandOrigin origin, cmTarget* target,
const char* workingDir,
const std::vector<std::string>& byproducts,
const std::vector<std::string>& depends,
const cmCustomCommandLines& commandLines,
bool escapeOldStyle, const char* comment,
bool uses_terminal, bool command_expand_lists,
const std::string& job_pool, bool stdPipesUTF8,
cmPolicies::PolicyStatus cmp0116);
void AddUtilityCommand(cmLocalGenerator& lg, cmCommandOrigin origin,
cmTarget* target, std::unique_ptr<cmCustomCommand> cc);
std::vector<std::string> ComputeISPCObjectSuffixes(cmGeneratorTarget* target);
std::vector<std::string> ComputeISPCExtraObjects(

View File

@@ -900,7 +900,12 @@ void cmMakefile::AddGeneratorAction(GeneratorAction&& action)
void cmMakefile::GeneratorAction::operator()(cmLocalGenerator& lg,
const cmListFileBacktrace& lfbt)
{
Action(lg, lfbt);
if (cc) {
CCAction(lg, lfbt, std::move(cc));
} else {
assert(Action);
Action(lg, lfbt);
}
}
void cmMakefile::DoGenerate(cmLocalGenerator& lg)
@@ -962,19 +967,6 @@ private:
cmListFileBacktrace& Backtrace;
cmListFileBacktrace Previous;
};
cm::optional<std::string> MakeOptionalString(const char* str)
{
if (str) {
return str;
}
return cm::nullopt;
}
const char* GetCStrOrNull(const cm::optional<std::string>& str)
{
return str ? str->c_str() : nullptr;
}
}
bool cmMakefile::ValidateCustomCommand(
@@ -1082,19 +1074,29 @@ cmTarget* cmMakefile::AddCustomCommandToTarget(
auto cmp0116 = this->GetPolicyStatus(cmPolicies::CMP0116);
// Strings could be moved into the callback function with C++14.
cm::optional<std::string> commentStr = MakeOptionalString(comment);
cm::optional<std::string> workingStr = MakeOptionalString(workingDir);
auto cc = cm::make_unique<cmCustomCommand>();
cc->SetByproducts(byproducts);
cc->SetDepends(depends);
cc->SetCommandLines(commandLines);
cc->SetComment(comment);
cc->SetWorkingDirectory(workingDir);
cc->SetEscapeOldStyle(escapeOldStyle);
cc->SetUsesTerminal(uses_terminal);
cc->SetDepfile(depfile);
cc->SetJobPool(job_pool);
cc->SetCommandExpandLists(command_expand_lists);
cc->SetStdPipesUTF8(stdPipesUTF8);
cc->SetCMP0116Status(cmp0116);
// Dispatch command creation to allow generator expressions in outputs.
this->AddGeneratorAction(
[=](cmLocalGenerator& lg, const cmListFileBacktrace& lfbt) {
std::move(cc),
[=](cmLocalGenerator& lg, const cmListFileBacktrace& lfbt,
std::unique_ptr<cmCustomCommand> tcc) {
BacktraceGuard guard(this->Backtrace, lfbt);
detail::AddCustomCommandToTarget(
lg, lfbt, cmCommandOrigin::Project, t, byproducts, depends,
commandLines, type, GetCStrOrNull(commentStr),
GetCStrOrNull(workingStr), escapeOldStyle, uses_terminal, depfile,
job_pool, command_expand_lists, stdPipesUTF8, cmp0116);
tcc->SetBacktrace(lfbt);
detail::AddCustomCommandToTarget(lg, cmCommandOrigin::Project, t, type,
std::move(tcc));
});
return t;
@@ -1143,20 +1145,32 @@ void cmMakefile::AddCustomCommandToOutput(
auto cmp0116 = this->GetPolicyStatus(cmPolicies::CMP0116);
// Strings could be moved into the callback function with C++14.
cm::optional<std::string> commentStr = MakeOptionalString(comment);
cm::optional<std::string> workingStr = MakeOptionalString(workingDir);
auto cc = cm::make_unique<cmCustomCommand>();
cc->SetOutputs(outputs);
cc->SetByproducts(byproducts);
cc->SetDepends(depends);
cc->SetImplicitDepends(implicit_depends);
cc->SetCommandLines(commandLines);
cc->SetComment(comment);
cc->SetWorkingDirectory(workingDir);
cc->SetEscapeOldStyle(escapeOldStyle);
cc->SetUsesTerminal(uses_terminal);
cc->SetCommandExpandLists(command_expand_lists);
cc->SetDepfile(depfile);
cc->SetJobPool(job_pool);
cc->SetStdPipesUTF8(stdPipesUTF8);
cc->SetCMP0116Status(cmp0116);
// Dispatch command creation to allow generator expressions in outputs.
this->AddGeneratorAction(
[=](cmLocalGenerator& lg, const cmListFileBacktrace& lfbt) {
std::move(cc),
[=](cmLocalGenerator& lg, const cmListFileBacktrace& lfbt,
std::unique_ptr<cmCustomCommand> tcc) {
BacktraceGuard guard(this->Backtrace, lfbt);
tcc->SetBacktrace(lfbt);
cmSourceFile* sf = detail::AddCustomCommandToOutput(
lg, lfbt, cmCommandOrigin::Project, outputs, byproducts, depends,
main_dependency, implicit_depends, commandLines,
GetCStrOrNull(commentStr), GetCStrOrNull(workingStr), replace,
escapeOldStyle, uses_terminal, command_expand_lists, depfile, job_pool,
stdPipesUTF8, cmp0116);
lg, cmCommandOrigin::Project, main_dependency, std::move(tcc),
replace);
if (callback && sf) {
callback(sf);
}
@@ -1264,19 +1278,28 @@ cmTarget* cmMakefile::AddUtilityCommand(
auto cmp0116 = this->GetPolicyStatus(cmPolicies::CMP0116);
// Strings could be moved into the callback function with C++14.
cm::optional<std::string> commentStr = MakeOptionalString(comment);
cm::optional<std::string> workingStr = MakeOptionalString(workingDir);
auto cc = cm::make_unique<cmCustomCommand>();
cc->SetWorkingDirectory(workingDir);
cc->SetByproducts(byproducts);
cc->SetDepends(depends);
cc->SetCommandLines(commandLines);
cc->SetEscapeOldStyle(escapeOldStyle);
cc->SetComment(comment);
cc->SetUsesTerminal(uses_terminal);
cc->SetCommandExpandLists(command_expand_lists);
cc->SetJobPool(job_pool);
cc->SetStdPipesUTF8(stdPipesUTF8);
cc->SetCMP0116Status(cmp0116);
// Dispatch command creation to allow generator expressions in outputs.
this->AddGeneratorAction(
[=](cmLocalGenerator& lg, const cmListFileBacktrace& lfbt) {
std::move(cc),
[=](cmLocalGenerator& lg, const cmListFileBacktrace& lfbt,
std::unique_ptr<cmCustomCommand> tcc) {
BacktraceGuard guard(this->Backtrace, lfbt);
detail::AddUtilityCommand(
lg, lfbt, cmCommandOrigin::Project, target, GetCStrOrNull(workingStr),
byproducts, depends, commandLines, escapeOldStyle,
GetCStrOrNull(commentStr), uses_terminal, command_expand_lists,
job_pool, stdPipesUTF8, cmp0116);
tcc->SetBacktrace(lfbt);
detail::AddUtilityCommand(lg, cmCommandOrigin::Project, target,
std::move(tcc));
});
return target;

View File

@@ -24,6 +24,7 @@
#include "cm_sys_stat.h"
#include "cmAlgorithms.h"
#include "cmCustomCommand.h"
#include "cmCustomCommandTypes.h"
#include "cmListFileCache.h"
#include "cmMessageType.h"
@@ -50,7 +51,6 @@ class cmExportBuildFileGenerator;
class cmFunctionBlocker;
class cmGeneratorExpressionEvaluationFile;
class cmGlobalGenerator;
class cmImplicitDependsList;
class cmInstallGenerator;
class cmLocalGenerator;
class cmMessenger;
@@ -144,6 +144,9 @@ public:
{
using ActionT =
std::function<void(cmLocalGenerator&, const cmListFileBacktrace&)>;
using CCActionT =
std::function<void(cmLocalGenerator&, const cmListFileBacktrace&,
std::unique_ptr<cmCustomCommand> cc)>;
public:
GeneratorAction(ActionT&& action)
@@ -151,10 +154,20 @@ public:
{
}
GeneratorAction(std::unique_ptr<cmCustomCommand> tcc, CCActionT&& action)
: CCAction(std::move(action))
, cc(std::move(tcc))
{
}
void operator()(cmLocalGenerator& lg, const cmListFileBacktrace& lfbt);
private:
ActionT Action;
// FIXME: Use std::variant
CCActionT CCAction;
std::unique_ptr<cmCustomCommand> cc;
};
/**