mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-19 19:43:23 +08:00
Refactor: Modernize function
command
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
file Copyright.txt or https://cmake.org/licensing for details. */
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
||||||
#include "cmFunctionCommand.h"
|
#include "cmFunctionCommand.h"
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include <cm/memory>
|
#include <cm/memory>
|
||||||
@@ -21,6 +20,10 @@
|
|||||||
#include "cmStringAlgorithms.h"
|
#include "cmStringAlgorithms.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
std::string const ARGC = "ARGC";
|
||||||
|
std::string const ARGN = "ARGN";
|
||||||
|
std::string const ARGV = "ARGV";
|
||||||
|
|
||||||
// define the class for function commands
|
// define the class for function commands
|
||||||
class cmFunctionHelperCommand
|
class cmFunctionHelperCommand
|
||||||
{
|
{
|
||||||
@@ -37,7 +40,6 @@ public:
|
|||||||
cmPolicies::PolicyMap Policies;
|
cmPolicies::PolicyMap Policies;
|
||||||
std::string FilePath;
|
std::string FilePath;
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
bool cmFunctionHelperCommand::operator()(
|
bool cmFunctionHelperCommand::operator()(
|
||||||
std::vector<cmListFileArgument> const& args,
|
std::vector<cmListFileArgument> const& args,
|
||||||
@@ -52,9 +54,9 @@ bool cmFunctionHelperCommand::operator()(
|
|||||||
// make sure the number of arguments passed is at least the number
|
// make sure the number of arguments passed is at least the number
|
||||||
// required by the signature
|
// required by the signature
|
||||||
if (expandedArgs.size() < this->Args.size() - 1) {
|
if (expandedArgs.size() < this->Args.size() - 1) {
|
||||||
std::string errorMsg = cmStrCat(
|
auto const errorMsg = cmStrCat(
|
||||||
"Function invoked with incorrect arguments for function named: ",
|
"Function invoked with incorrect arguments for function named: ",
|
||||||
this->Args[0]);
|
this->Args.front());
|
||||||
inStatus.SetError(errorMsg);
|
inStatus.SetError(errorMsg);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -63,30 +65,29 @@ bool cmFunctionHelperCommand::operator()(
|
|||||||
this->Policies);
|
this->Policies);
|
||||||
|
|
||||||
// set the value of argc
|
// set the value of argc
|
||||||
makefile.AddDefinition("ARGC", std::to_string(expandedArgs.size()));
|
makefile.AddDefinition(ARGC, std::to_string(expandedArgs.size()));
|
||||||
makefile.MarkVariableAsUsed("ARGC");
|
makefile.MarkVariableAsUsed(ARGC);
|
||||||
|
|
||||||
// set the values for ARGV0 ARGV1 ...
|
// set the values for ARGV0 ARGV1 ...
|
||||||
for (unsigned int t = 0; t < expandedArgs.size(); ++t) {
|
for (auto t = 0u; t < expandedArgs.size(); ++t) {
|
||||||
std::ostringstream tmpStream;
|
auto const value = cmStrCat(ARGV, std::to_string(t));
|
||||||
tmpStream << "ARGV" << t;
|
makefile.AddDefinition(value, expandedArgs[t]);
|
||||||
makefile.AddDefinition(tmpStream.str(), expandedArgs[t]);
|
makefile.MarkVariableAsUsed(value);
|
||||||
makefile.MarkVariableAsUsed(tmpStream.str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// define the formal arguments
|
// define the formal arguments
|
||||||
for (unsigned int j = 1; j < this->Args.size(); ++j) {
|
for (auto j = 1u; j < this->Args.size(); ++j) {
|
||||||
makefile.AddDefinition(this->Args[j], expandedArgs[j - 1]);
|
makefile.AddDefinition(this->Args[j], expandedArgs[j - 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// define ARGV and ARGN
|
// define ARGV and ARGN
|
||||||
std::string argvDef = cmJoin(expandedArgs, ";");
|
auto const argvDef = cmJoin(expandedArgs, ";");
|
||||||
auto eit = expandedArgs.begin() + (this->Args.size() - 1);
|
auto const eit = expandedArgs.begin() + (this->Args.size() - 1);
|
||||||
std::string argnDef = cmJoin(cmMakeRange(eit, expandedArgs.end()), ";");
|
auto const argnDef = cmJoin(cmMakeRange(eit, expandedArgs.end()), ";");
|
||||||
makefile.AddDefinition("ARGV", argvDef);
|
makefile.AddDefinition(ARGV, argvDef);
|
||||||
makefile.MarkVariableAsUsed("ARGV");
|
makefile.MarkVariableAsUsed(ARGV);
|
||||||
makefile.AddDefinition("ARGN", argnDef);
|
makefile.AddDefinition(ARGN, argnDef);
|
||||||
makefile.MarkVariableAsUsed("ARGN");
|
makefile.MarkVariableAsUsed(ARGN);
|
||||||
|
|
||||||
// Invoke all the functions that were collected in the block.
|
// Invoke all the functions that were collected in the block.
|
||||||
// for each function
|
// for each function
|
||||||
@@ -100,7 +101,7 @@ bool cmFunctionHelperCommand::operator()(
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (status.GetReturnInvoked()) {
|
if (status.GetReturnInvoked()) {
|
||||||
return true;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,7 +130,8 @@ bool cmFunctionFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff,
|
|||||||
std::vector<std::string> expandedArguments;
|
std::vector<std::string> expandedArguments;
|
||||||
mf.ExpandArguments(lff.Arguments, expandedArguments,
|
mf.ExpandArguments(lff.Arguments, expandedArguments,
|
||||||
this->GetStartingContext().FilePath.c_str());
|
this->GetStartingContext().FilePath.c_str());
|
||||||
return expandedArguments.empty() || expandedArguments[0] == this->Args[0];
|
return expandedArguments.empty() ||
|
||||||
|
expandedArguments.front() == this->Args.front();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool cmFunctionFunctionBlocker::Replay(
|
bool cmFunctionFunctionBlocker::Replay(
|
||||||
@@ -142,10 +144,12 @@ bool cmFunctionFunctionBlocker::Replay(
|
|||||||
f.Functions = std::move(functions);
|
f.Functions = std::move(functions);
|
||||||
f.FilePath = this->GetStartingContext().FilePath;
|
f.FilePath = this->GetStartingContext().FilePath;
|
||||||
mf.RecordPolicies(f.Policies);
|
mf.RecordPolicies(f.Policies);
|
||||||
mf.GetState()->AddScriptedCommand(this->Args[0], std::move(f));
|
mf.GetState()->AddScriptedCommand(this->Args.front(), std::move(f));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // anonymous namespace
|
||||||
|
|
||||||
bool cmFunctionCommand(std::vector<std::string> const& args,
|
bool cmFunctionCommand(std::vector<std::string> const& args,
|
||||||
cmExecutionStatus& status)
|
cmExecutionStatus& status)
|
||||||
{
|
{
|
||||||
@@ -155,10 +159,9 @@ bool cmFunctionCommand(std::vector<std::string> const& args,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create a function blocker
|
// create a function blocker
|
||||||
{
|
|
||||||
auto fb = cm::make_unique<cmFunctionFunctionBlocker>();
|
auto fb = cm::make_unique<cmFunctionFunctionBlocker>();
|
||||||
cmAppend(fb->Args, args);
|
cmAppend(fb->Args, args);
|
||||||
status.GetMakefile().AddFunctionBlocker(std::move(fb));
|
status.GetMakefile().AddFunctionBlocker(std::move(fb));
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user