cmake/Source/cmMacroCommand.cxx

198 lines
6.4 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmMacroCommand.h"
2020-02-01 23:06:01 +01:00
#include <cstdio>
2018-04-23 21:13:27 +02:00
#include <utility>
2017-04-14 19:02:05 +02:00
2020-02-01 23:06:01 +01:00
#include <cm/memory>
#include <cm/string_view>
2020-08-30 11:54:41 +02:00
#include <cmext/algorithm>
#include <cmext/string_view>
2020-02-01 23:06:01 +01:00
2017-04-14 19:02:05 +02:00
#include "cmExecutionStatus.h"
2020-02-01 23:06:01 +01:00
#include "cmFunctionBlocker.h"
#include "cmListFileCache.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
#include "cmPolicies.h"
2019-11-11 23:01:05 +01:00
#include "cmRange.h"
2017-04-14 19:02:05 +02:00
#include "cmState.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2020-02-01 23:06:01 +01:00
namespace {
// define the class for macro commands
2020-02-01 23:06:01 +01:00
class cmMacroHelperCommand
{
public:
/**
* This is called when the command is first encountered in
* the CMakeLists.txt file.
*/
2020-02-01 23:06:01 +01:00
bool operator()(std::vector<cmListFileArgument> const& args,
cmExecutionStatus& inStatus) const;
std::vector<std::string> Args;
std::vector<cmListFileFunction> Functions;
cmPolicies::PolicyMap Policies;
2011-02-07 16:37:25 +01:00
std::string FilePath;
};
2020-02-01 23:06:01 +01:00
bool cmMacroHelperCommand::operator()(
std::vector<cmListFileArgument> const& args,
cmExecutionStatus& inStatus) const
{
2020-02-01 23:06:01 +01:00
cmMakefile& makefile = inStatus.GetMakefile();
// Expand the argument list to the macro.
std::vector<std::string> expandedArgs;
2020-02-01 23:06:01 +01:00
makefile.ExpandArguments(args, expandedArgs);
// make sure the number of arguments passed is at least the number
// required by the signature
2016-07-09 11:21:54 +02:00
if (expandedArgs.size() < this->Args.size() - 1) {
std::string errorMsg =
2020-02-01 23:06:01 +01:00
cmStrCat("Macro invoked with incorrect arguments for macro named: ",
this->Args[0]);
inStatus.SetError(errorMsg);
return false;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
cmMakefile::MacroPushPop macroScope(&makefile, this->FilePath,
2015-11-17 17:22:37 +01:00
this->Policies);
// set the value of argc
2020-02-01 23:06:01 +01:00
std::string argcDef = std::to_string(expandedArgs.size());
2020-02-01 23:06:01 +01:00
auto eit = expandedArgs.begin() + (this->Args.size() - 1);
2015-11-17 17:22:37 +01:00
std::string expandedArgn = cmJoin(cmMakeRange(eit, expandedArgs.end()), ";");
2015-08-17 11:37:30 +02:00
std::string expandedArgv = cmJoin(expandedArgs, ";");
std::vector<std::string> variables;
variables.reserve(this->Args.size() - 1);
2016-07-09 11:21:54 +02:00
for (unsigned int j = 1; j < this->Args.size(); ++j) {
2015-08-17 11:37:30 +02:00
variables.push_back("${" + this->Args[j] + "}");
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
std::vector<std::string> argVs;
argVs.reserve(expandedArgs.size());
char argvName[60];
2016-07-09 11:21:54 +02:00
for (unsigned int j = 0; j < expandedArgs.size(); ++j) {
2022-03-29 21:10:50 +02:00
snprintf(argvName, sizeof(argvName), "${ARGV%u}", j);
2019-11-11 23:01:05 +01:00
argVs.emplace_back(argvName);
2016-07-09 11:21:54 +02:00
}
// Invoke all the functions that were collected in the block.
// for each function
2018-01-26 17:06:56 +01:00
for (cmListFileFunction const& func : this->Functions) {
// Replace the formal arguments and then invoke the command.
2021-09-14 00:13:48 +02:00
std::vector<cmListFileArgument> newLFFArgs;
newLFFArgs.reserve(func.Arguments().size());
// for each argument of the current function
2021-09-14 00:13:48 +02:00
for (cmListFileArgument const& k : func.Arguments()) {
2015-08-17 11:37:30 +02:00
cmListFileArgument arg;
2018-01-26 17:06:56 +01:00
arg.Value = k.Value;
if (k.Delim != cmListFileArgument::Bracket) {
2014-08-03 19:52:23 +02:00
// replace formal arguments
2016-07-09 11:21:54 +02:00
for (unsigned int j = 0; j < variables.size(); ++j) {
2016-03-13 13:35:51 +01:00
cmSystemTools::ReplaceString(arg.Value, variables[j],
expandedArgs[j]);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// replace argc
2016-03-13 13:35:51 +01:00
cmSystemTools::ReplaceString(arg.Value, "${ARGC}", argcDef);
2014-08-03 19:52:23 +02:00
2016-03-13 13:35:51 +01:00
cmSystemTools::ReplaceString(arg.Value, "${ARGN}", expandedArgn);
cmSystemTools::ReplaceString(arg.Value, "${ARGV}", expandedArgv);
2014-08-03 19:52:23 +02:00
// if the current argument of the current function has ${ARGV in it
// then try replacing ARGV values
2016-07-09 11:21:54 +02:00
if (arg.Value.find("${ARGV") != std::string::npos) {
for (unsigned int t = 0; t < expandedArgs.size(); ++t) {
cmSystemTools::ReplaceString(arg.Value, argVs[t], expandedArgs[t]);
}
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
arg.Delim = k.Delim;
arg.Line = k.Line;
2021-09-14 00:13:48 +02:00
newLFFArgs.push_back(std::move(arg));
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
cmListFileFunction newLFF{ func.OriginalName(), func.Line(),
2022-08-04 22:12:04 +02:00
func.LineEnd(), std::move(newLFFArgs) };
2020-02-01 23:06:01 +01:00
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.
2015-11-17 17:22:37 +01:00
macroScope.Quiet();
2017-07-20 19:35:53 +02:00
inStatus.SetNestedError();
return false;
2016-07-09 11:21:54 +02:00
}
if (status.GetReturnInvoked()) {
2017-07-20 19:35:53 +02:00
inStatus.SetReturnInvoked();
return true;
2016-07-09 11:21:54 +02:00
}
if (status.GetBreakInvoked()) {
2017-07-20 19:35:53 +02:00
inStatus.SetBreakInvoked();
return true;
}
2016-07-09 11:21:54 +02:00
}
return true;
}
2020-02-01 23:06:01 +01:00
class cmMacroFunctionBlocker : public cmFunctionBlocker
{
2020-02-01 23:06:01 +01:00
public:
cm::string_view StartCommandName() const override { return "macro"_s; }
cm::string_view EndCommandName() const override { return "endmacro"_s; }
2020-02-01 23:06:01 +01:00
bool ArgumentsMatch(cmListFileFunction const&,
cmMakefile& mf) const override;
bool Replay(std::vector<cmListFileFunction> functions,
cmExecutionStatus& status) override;
2020-02-01 23:06:01 +01:00
std::vector<std::string> Args;
};
bool cmMacroFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff,
cmMakefile& mf) const
{
2020-02-01 23:06:01 +01:00
std::vector<std::string> expandedArguments;
2021-09-14 00:13:48 +02:00
mf.ExpandArguments(lff.Arguments(), expandedArguments);
2020-02-01 23:06:01 +01:00
return expandedArguments.empty() || expandedArguments[0] == this->Args[0];
}
2020-02-01 23:06:01 +01:00
bool cmMacroFunctionBlocker::Replay(std::vector<cmListFileFunction> functions,
cmExecutionStatus& status)
{
cmMakefile& mf = status.GetMakefile();
2020-08-30 11:54:41 +02:00
mf.AppendProperty("MACROS", this->Args[0]);
2020-02-01 23:06:01 +01:00
// create a new command and add it to cmake
cmMacroHelperCommand f;
f.Args = this->Args;
f.Functions = std::move(functions);
f.FilePath = this->GetStartingContext().FilePath;
mf.RecordPolicies(f.Policies);
2021-09-14 00:13:48 +02:00
return mf.GetState()->AddScriptedCommand(
this->Args[0],
BT<cmState::Command>(std::move(f),
mf.GetBacktrace().Push(this->GetStartingContext())),
mf);
2020-02-01 23:06:01 +01:00
}
}
2020-02-01 23:06:01 +01:00
bool cmMacroCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
// create a function blocker
2020-02-01 23:06:01 +01:00
{
auto fb = cm::make_unique<cmMacroFunctionBlocker>();
2020-08-30 11:54:41 +02:00
cm::append(fb->Args, args);
2020-02-01 23:06:01 +01:00
status.GetMakefile().AddFunctionBlocker(std::move(fb));
}
return true;
}