cmState: Add scripted commands by value

This commit is contained in:
Regina Pfeifer 2019-04-07 21:27:41 +02:00
parent 0101ace131
commit de77d355ac
5 changed files with 50 additions and 89 deletions

View File

@ -5,8 +5,6 @@
#include <sstream>
#include <utility>
#include "cm_memory.hxx"
#include "cmAlgorithms.h"
#include "cmExecutionStatus.h"
#include "cmMakefile.h"
@ -15,35 +13,15 @@
#include "cmState.h"
// define the class for function commands
class cmFunctionHelperCommand : public cmCommand
class cmFunctionHelperCommand
{
public:
/**
* This is a virtual constructor for the command.
*/
std::unique_ptr<cmCommand> Clone() override
{
auto newC = cm::make_unique<cmFunctionHelperCommand>();
// we must copy when we clone
newC->Args = this->Args;
newC->Functions = this->Functions;
newC->Policies = this->Policies;
newC->FilePath = this->FilePath;
return std::unique_ptr<cmCommand>(std::move(newC));
}
/**
* This is called when the command is first encountered in
* the CMakeLists.txt file.
*/
bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
cmExecutionStatus&) override;
bool InitialPass(std::vector<std::string> const&,
cmExecutionStatus&) override
{
return false;
}
bool operator()(std::vector<cmListFileArgument> const& args,
cmExecutionStatus& inStatus) const;
std::vector<std::string> Args;
std::vector<cmListFileFunction> Functions;
@ -51,12 +29,15 @@ public:
std::string FilePath;
};
bool cmFunctionHelperCommand::InvokeInitialPass(
const std::vector<cmListFileArgument>& args, cmExecutionStatus& inStatus)
bool cmFunctionHelperCommand::operator()(
std::vector<cmListFileArgument> const& args,
cmExecutionStatus& inStatus) const
{
cmMakefile& makefile = inStatus.GetMakefile();
// Expand the argument list to the function.
std::vector<std::string> expandedArgs;
this->Makefile->ExpandArguments(args, expandedArgs);
makefile.ExpandArguments(args, expandedArgs);
// make sure the number of arguments passed is at least the number
// required by the signature
@ -64,30 +45,30 @@ bool cmFunctionHelperCommand::InvokeInitialPass(
std::string errorMsg =
"Function invoked with incorrect arguments for function named: ";
errorMsg += this->Args[0];
this->SetError(errorMsg);
inStatus.SetError(errorMsg);
return false;
}
cmMakefile::FunctionPushPop functionScope(this->Makefile, this->FilePath,
cmMakefile::FunctionPushPop functionScope(&makefile, this->FilePath,
this->Policies);
// set the value of argc
std::ostringstream strStream;
strStream << expandedArgs.size();
this->Makefile->AddDefinition("ARGC", strStream.str().c_str());
this->Makefile->MarkVariableAsUsed("ARGC");
makefile.AddDefinition("ARGC", strStream.str().c_str());
makefile.MarkVariableAsUsed("ARGC");
// set the values for ARGV0 ARGV1 ...
for (unsigned int t = 0; t < expandedArgs.size(); ++t) {
std::ostringstream tmpStream;
tmpStream << "ARGV" << t;
this->Makefile->AddDefinition(tmpStream.str(), expandedArgs[t].c_str());
this->Makefile->MarkVariableAsUsed(tmpStream.str());
makefile.AddDefinition(tmpStream.str(), expandedArgs[t].c_str());
makefile.MarkVariableAsUsed(tmpStream.str());
}
// define the formal arguments
for (unsigned int j = 1; j < this->Args.size(); ++j) {
this->Makefile->AddDefinition(this->Args[j], expandedArgs[j - 1].c_str());
makefile.AddDefinition(this->Args[j], expandedArgs[j - 1].c_str());
}
// define ARGV and ARGN
@ -95,17 +76,16 @@ bool cmFunctionHelperCommand::InvokeInitialPass(
std::vector<std::string>::const_iterator eit =
expandedArgs.begin() + (this->Args.size() - 1);
std::string argnDef = cmJoin(cmMakeRange(eit, expandedArgs.end()), ";");
this->Makefile->AddDefinition("ARGV", argvDef.c_str());
this->Makefile->MarkVariableAsUsed("ARGV");
this->Makefile->AddDefinition("ARGN", argnDef.c_str());
this->Makefile->MarkVariableAsUsed("ARGN");
makefile.AddDefinition("ARGV", argvDef.c_str());
makefile.MarkVariableAsUsed("ARGV");
makefile.AddDefinition("ARGN", argnDef.c_str());
makefile.MarkVariableAsUsed("ARGN");
// Invoke all the functions that were collected in the block.
// for each function
for (cmListFileFunction const& func : this->Functions) {
cmExecutionStatus status(*this->GetMakefile());
if (!this->Makefile->ExecuteCommand(func, status) ||
status.GetNestedError()) {
cmExecutionStatus status(makefile);
if (!makefile.ExecuteCommand(func, status) || status.GetNestedError()) {
// The error message should have already included the call stack
// so we do not need to report an error here.
functionScope.Quiet();
@ -132,11 +112,11 @@ bool cmFunctionFunctionBlocker::IsFunctionBlocked(
// if this is the endfunction for this function then execute
if (!this->Depth) {
// create a new command and add it to cmake
auto f = cm::make_unique<cmFunctionHelperCommand>();
f->Args = this->Args;
f->Functions = this->Functions;
f->FilePath = this->GetStartingContext().FilePath;
mf.RecordPolicies(f->Policies);
cmFunctionHelperCommand f;
f.Args = this->Args;
f.Functions = this->Functions;
f.FilePath = this->GetStartingContext().FilePath;
mf.RecordPolicies(f.Policies);
mf.GetState()->AddScriptedCommand(this->Args[0], std::move(f));
// remove the function blocker now that the function is defined
mf.RemoveFunctionBlocker(this, lff);

View File

@ -247,7 +247,8 @@ bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& args,
// function blocker
if (initFunction) {
this->Makefile->GetState()->AddScriptedCommand(
args[0], cm::make_unique<cmLoadedCommand>(initFunction));
args[0],
cmLegacyCommandWrapper(cm::make_unique<cmLoadedCommand>(initFunction)));
return true;
}
this->SetError("Attempt to load command failed. "

View File

@ -17,35 +17,15 @@
#include "cmSystemTools.h"
// define the class for macro commands
class cmMacroHelperCommand : public cmCommand
class cmMacroHelperCommand
{
public:
/**
* This is a virtual constructor for the command.
*/
std::unique_ptr<cmCommand> Clone() override
{
auto newC = cm::make_unique<cmMacroHelperCommand>();
// we must copy when we clone
newC->Args = this->Args;
newC->Functions = this->Functions;
newC->FilePath = this->FilePath;
newC->Policies = this->Policies;
return std::unique_ptr<cmCommand>(std::move(newC));
}
/**
* This is called when the command is first encountered in
* the CMakeLists.txt file.
*/
bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
cmExecutionStatus&) override;
bool InitialPass(std::vector<std::string> const&,
cmExecutionStatus&) override
{
return false;
}
bool operator()(std::vector<cmListFileArgument> const& args,
cmExecutionStatus& inStatus) const;
std::vector<std::string> Args;
std::vector<cmListFileFunction> Functions;
@ -53,12 +33,15 @@ public:
std::string FilePath;
};
bool cmMacroHelperCommand::InvokeInitialPass(
const std::vector<cmListFileArgument>& args, cmExecutionStatus& inStatus)
bool cmMacroHelperCommand::operator()(
std::vector<cmListFileArgument> const& args,
cmExecutionStatus& inStatus) const
{
cmMakefile& makefile = inStatus.GetMakefile();
// Expand the argument list to the macro.
std::vector<std::string> expandedArgs;
this->Makefile->ExpandArguments(args, expandedArgs);
makefile.ExpandArguments(args, expandedArgs);
// make sure the number of arguments passed is at least the number
// required by the signature
@ -66,11 +49,11 @@ bool cmMacroHelperCommand::InvokeInitialPass(
std::string errorMsg =
"Macro invoked with incorrect arguments for macro named: ";
errorMsg += this->Args[0];
this->SetError(errorMsg);
inStatus.SetError(errorMsg);
return false;
}
cmMakefile::MacroPushPop macroScope(this->Makefile, this->FilePath,
cmMakefile::MacroPushPop macroScope(&makefile, this->FilePath,
this->Policies);
// set the value of argc
@ -132,9 +115,8 @@ bool cmMacroHelperCommand::InvokeInitialPass(
arg.Line = k.Line;
newLFF.Arguments.push_back(std::move(arg));
}
cmExecutionStatus status(*this->GetMakefile());
if (!this->Makefile->ExecuteCommand(newLFF, status) ||
status.GetNestedError()) {
cmExecutionStatus status(makefile);
if (!makefile.ExecuteCommand(newLFF, status) || status.GetNestedError()) {
// The error message should have already included the call stack
// so we do not need to report an error here.
macroScope.Quiet();
@ -166,11 +148,11 @@ bool cmMacroFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff,
if (!this->Depth) {
mf.AppendProperty("MACROS", this->Args[0].c_str());
// create a new command and add it to cmake
auto f = cm::make_unique<cmMacroHelperCommand>();
f->Args = this->Args;
f->Functions = this->Functions;
f->FilePath = this->GetStartingContext().FilePath;
mf.RecordPolicies(f->Policies);
cmMacroHelperCommand f;
f.Args = this->Args;
f.Functions = this->Functions;
f.FilePath = this->GetStartingContext().FilePath;
mf.RecordPolicies(f.Policies);
mf.GetState()->AddScriptedCommand(this->Args[0], std::move(f));
// remove the function blocker now that the macro is defined
mf.RemoveFunctionBlocker(this, lff);

View File

@ -458,8 +458,7 @@ void cmState::AddUnexpectedCommand(std::string const& name, const char* error)
});
}
void cmState::AddScriptedCommand(std::string const& name,
std::unique_ptr<cmCommand> command)
void cmState::AddScriptedCommand(std::string const& name, Command command)
{
std::string sName = cmSystemTools::LowerCase(name);
@ -468,7 +467,7 @@ void cmState::AddScriptedCommand(std::string const& name,
this->ScriptedCommands["_" + sName] = oldCmd;
}
this->ScriptedCommands[sName] = cmLegacyCommandWrapper(std::move(command));
this->ScriptedCommands[sName] = std::move(command);
}
cmState::Command cmState::GetCommand(std::string const& name) const

View File

@ -158,8 +158,7 @@ public:
std::unique_ptr<cmCommand> command,
cmPolicies::PolicyID policy, const char* message);
void AddUnexpectedCommand(std::string const& name, const char* error);
void AddScriptedCommand(std::string const& name,
std::unique_ptr<cmCommand> command);
void AddScriptedCommand(std::string const& name, Command command);
void RemoveBuiltinCommand(std::string const& name);
void RemoveUserDefinedCommands();
std::vector<std::string> GetCommandNames() const;