cmake/Source/cmMacroCommand.cxx

223 lines
7.2 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"
2017-04-14 19:02:05 +02:00
#include <sstream>
#include <stdio.h>
2018-04-23 21:13:27 +02:00
#include <utility>
2017-04-14 19:02:05 +02:00
2015-08-17 11:37:30 +02:00
#include "cmAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmExecutionStatus.h"
#include "cmMakefile.h"
#include "cmPolicies.h"
#include "cmState.h"
#include "cmSystemTools.h"
// define the class for macro commands
class cmMacroHelperCommand : public cmCommand
{
public:
cmMacroHelperCommand() {}
///! clean up any memory allocated by the macro
2018-01-26 17:06:56 +01:00
~cmMacroHelperCommand() override {}
/**
* This is a virtual constructor for the command.
*/
2018-01-26 17:06:56 +01:00
cmCommand* Clone() override
{
2016-07-09 11:21:54 +02:00
cmMacroHelperCommand* newC = new cmMacroHelperCommand;
// we must copy when we clone
newC->Args = this->Args;
newC->Functions = this->Functions;
2011-02-07 16:37:25 +01:00
newC->FilePath = this->FilePath;
newC->Policies = this->Policies;
return newC;
}
/**
* This is called when the command is first encountered in
* the CMakeLists.txt file.
*/
2016-10-30 18:24:19 +01:00
bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
2018-01-26 17:06:56 +01:00
cmExecutionStatus&) override;
2016-10-30 18:24:19 +01:00
bool InitialPass(std::vector<std::string> const&,
2018-01-26 17:06:56 +01:00
cmExecutionStatus&) override
2016-07-09 11:21:54 +02:00
{
return false;
}
std::vector<std::string> Args;
std::vector<cmListFileFunction> Functions;
cmPolicies::PolicyMap Policies;
2011-02-07 16:37:25 +01:00
std::string FilePath;
};
2016-07-09 11:21:54 +02:00
bool cmMacroHelperCommand::InvokeInitialPass(
const std::vector<cmListFileArgument>& args, cmExecutionStatus& inStatus)
{
// Expand the argument list to the macro.
std::vector<std::string> expandedArgs;
this->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 =
"Macro invoked with incorrect arguments for macro named: ";
errorMsg += this->Args[0];
2015-04-27 22:25:09 +02:00
this->SetError(errorMsg);
return false;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
cmMakefile::MacroPushPop macroScope(this->Makefile, this->FilePath,
2015-11-17 17:22:37 +01:00
this->Policies);
// set the value of argc
2015-04-27 22:25:09 +02:00
std::ostringstream argcDefStream;
argcDefStream << expandedArgs.size();
std::string argcDef = argcDefStream.str();
2016-07-09 11:21:54 +02:00
std::vector<std::string>::const_iterator 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) {
sprintf(argvName, "${ARGV%i}", j);
2015-08-17 11:37:30 +02:00
argVs.push_back(argvName);
2016-07-09 11:21:54 +02:00
}
// Invoke all the functions that were collected in the block.
cmListFileFunction newLFF;
// 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.
newLFF.Arguments.clear();
2018-01-26 17:06:56 +01:00
newLFF.Arguments.reserve(func.Arguments.size());
newLFF.Name = func.Name;
newLFF.Line = func.Line;
// for each argument of the current function
2018-01-26 17:06:56 +01: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;
2018-04-23 21:13:27 +02:00
newLFF.Arguments.push_back(std::move(arg));
2016-07-09 11:21:54 +02:00
}
cmExecutionStatus status;
2016-07-09 11:21:54 +02:00
if (!this->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;
}
2016-07-09 11:21:54 +02:00
bool cmMacroFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff,
cmMakefile& mf,
cmExecutionStatus&)
{
// record commands until we hit the ENDMACRO
// at the ENDMACRO call we shift gears and start looking for invocations
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::Strucmp(lff.Name.c_str(), "macro")) {
this->Depth++;
2016-07-09 11:21:54 +02:00
} else if (!cmSystemTools::Strucmp(lff.Name.c_str(), "endmacro")) {
// if this is the endmacro for this macro then execute
2016-07-09 11:21:54 +02:00
if (!this->Depth) {
2015-11-17 17:22:37 +01:00
mf.AppendProperty("MACROS", this->Args[0].c_str());
// create a new command and add it to cmake
2016-07-09 11:21:54 +02:00
cmMacroHelperCommand* f = new cmMacroHelperCommand();
f->Args = this->Args;
f->Functions = this->Functions;
2015-11-17 17:22:37 +01:00
f->FilePath = this->GetStartingContext().FilePath;
mf.RecordPolicies(f->Policies);
2017-07-20 19:35:53 +02:00
mf.GetState()->AddScriptedCommand(this->Args[0], f);
// remove the function blocker now that the macro is defined
mf.RemoveFunctionBlocker(this, lff);
return true;
}
2016-10-30 18:24:19 +01:00
// decrement for each nested macro that ends
this->Depth--;
2016-07-09 11:21:54 +02:00
}
// if it wasn't an endmacro and we are not executing then we must be
// recording
this->Functions.push_back(lff);
return true;
}
2016-07-09 11:21:54 +02:00
bool cmMacroFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
cmMakefile& mf)
{
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::Strucmp(lff.Name.c_str(), "endmacro")) {
std::vector<std::string> expandedArguments;
2015-11-17 17:22:37 +01:00
mf.ExpandArguments(lff.Arguments, expandedArguments,
this->GetStartingContext().FilePath.c_str());
// if the endmacro has arguments make sure they
// match the arguments of the macro
if ((expandedArguments.empty() ||
2016-07-09 11:21:54 +02:00
(expandedArguments[0] == this->Args[0]))) {
return true;
}
2016-07-09 11:21:54 +02:00
}
return false;
}
bool cmMacroCommand::InitialPass(std::vector<std::string> const& args,
2016-07-09 11:21:54 +02:00
cmExecutionStatus&)
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
this->SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
// create a function blocker
2016-07-09 11:21:54 +02:00
cmMacroFunctionBlocker* f = new cmMacroFunctionBlocker();
2015-04-27 22:25:09 +02:00
f->Args.insert(f->Args.end(), args.begin(), args.end());
this->Makefile->AddFunctionBlocker(f);
return true;
}