cmake/Source/cmFunctionCommand.cxx

190 lines
6.1 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 "cmFunctionCommand.h"
2020-02-01 23:06:01 +01:00
#include <utility>
#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"
2020-08-30 11:54:41 +02:00
#include "cmSystemTools.h"
2020-02-01 23:06:01 +01:00
namespace {
2020-08-30 11:54:41 +02:00
std::string const ARGC = "ARGC";
std::string const ARGN = "ARGN";
std::string const ARGV = "ARGV";
std::string const CMAKE_CURRENT_FUNCTION = "CMAKE_CURRENT_FUNCTION";
std::string const CMAKE_CURRENT_FUNCTION_LIST_FILE =
"CMAKE_CURRENT_FUNCTION_LIST_FILE";
std::string const CMAKE_CURRENT_FUNCTION_LIST_DIR =
"CMAKE_CURRENT_FUNCTION_LIST_DIR";
std::string const CMAKE_CURRENT_FUNCTION_LIST_LINE =
"CMAKE_CURRENT_FUNCTION_LIST_LINE";
// define the class for function commands
2020-02-01 23:06:01 +01:00
class cmFunctionHelperCommand
{
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;
2015-11-17 17:22:37 +01:00
std::string FilePath;
2020-08-30 11:54:41 +02:00
long Line;
};
2020-02-01 23:06:01 +01:00
bool cmFunctionHelperCommand::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 function.
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) {
2020-08-30 11:54:41 +02:00
auto const errorMsg = cmStrCat(
2020-02-01 23:06:01 +01:00
"Function invoked with incorrect arguments for function named: ",
2020-08-30 11:54:41 +02:00
this->Args.front());
2020-02-01 23:06:01 +01:00
inStatus.SetError(errorMsg);
return false;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
cmMakefile::FunctionPushPop functionScope(&makefile, this->FilePath,
2015-11-17 17:22:37 +01:00
this->Policies);
// set the value of argc
2020-08-30 11:54:41 +02:00
makefile.AddDefinition(ARGC, std::to_string(expandedArgs.size()));
makefile.MarkVariableAsUsed(ARGC);
// set the values for ARGV0 ARGV1 ...
2020-08-30 11:54:41 +02:00
for (auto t = 0u; t < expandedArgs.size(); ++t) {
auto const value = cmStrCat(ARGV, std::to_string(t));
makefile.AddDefinition(value, expandedArgs[t]);
makefile.MarkVariableAsUsed(value);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
// define the formal arguments
2020-08-30 11:54:41 +02:00
for (auto j = 1u; j < this->Args.size(); ++j) {
2020-02-01 23:06:01 +01:00
makefile.AddDefinition(this->Args[j], expandedArgs[j - 1]);
2016-07-09 11:21:54 +02:00
}
// define ARGV and ARGN
2020-08-30 11:54:41 +02:00
auto const argvDef = cmJoin(expandedArgs, ";");
auto const eit = expandedArgs.begin() + (this->Args.size() - 1);
auto const argnDef = cmJoin(cmMakeRange(eit, expandedArgs.end()), ";");
makefile.AddDefinition(ARGV, argvDef);
makefile.MarkVariableAsUsed(ARGV);
makefile.AddDefinition(ARGN, argnDef);
makefile.MarkVariableAsUsed(ARGN);
makefile.AddDefinition(CMAKE_CURRENT_FUNCTION, this->Args.front());
makefile.MarkVariableAsUsed(CMAKE_CURRENT_FUNCTION);
makefile.AddDefinition(CMAKE_CURRENT_FUNCTION_LIST_FILE, this->FilePath);
makefile.MarkVariableAsUsed(CMAKE_CURRENT_FUNCTION_LIST_FILE);
makefile.AddDefinition(CMAKE_CURRENT_FUNCTION_LIST_DIR,
cmSystemTools::GetFilenamePath(this->FilePath));
makefile.MarkVariableAsUsed(CMAKE_CURRENT_FUNCTION_LIST_DIR);
makefile.AddDefinition(CMAKE_CURRENT_FUNCTION_LIST_LINE,
std::to_string(this->Line));
makefile.MarkVariableAsUsed(CMAKE_CURRENT_FUNCTION_LIST_LINE);
// 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) {
2020-02-01 23:06:01 +01:00
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.
2015-11-17 17:22:37 +01:00
functionScope.Quiet();
2017-07-20 19:35:53 +02:00
inStatus.SetNestedError();
return false;
2016-07-09 11:21:54 +02:00
}
if (status.GetReturnInvoked()) {
2020-08-30 11:54:41 +02:00
break;
}
2016-07-09 11:21:54 +02:00
}
// pop scope on the makefile
return true;
}
2020-02-01 23:06:01 +01:00
class cmFunctionFunctionBlocker : public cmFunctionBlocker
{
2020-02-01 23:06:01 +01:00
public:
cm::string_view StartCommandName() const override { return "function"_s; }
cm::string_view EndCommandName() const override { return "endfunction"_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 cmFunctionFunctionBlocker::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-08-30 11:54:41 +02:00
return expandedArguments.empty() ||
expandedArguments.front() == this->Args.front();
2020-02-01 23:06:01 +01:00
}
2020-02-01 23:06:01 +01:00
bool cmFunctionFunctionBlocker::Replay(
std::vector<cmListFileFunction> functions, cmExecutionStatus& status)
{
cmMakefile& mf = status.GetMakefile();
// create a new command and add it to cmake
cmFunctionHelperCommand f;
f.Args = this->Args;
f.Functions = std::move(functions);
f.FilePath = this->GetStartingContext().FilePath;
2020-08-30 11:54:41 +02:00
f.Line = this->GetStartingContext().Line;
2020-02-01 23:06:01 +01:00
mf.RecordPolicies(f.Policies);
2021-09-14 00:13:48 +02:00
return mf.GetState()->AddScriptedCommand(
this->Args.front(),
BT<cmState::Command>(std::move(f),
mf.GetBacktrace().Push(this->GetStartingContext())),
mf);
}
2020-08-30 11:54:41 +02:00
} // anonymous namespace
2020-02-01 23:06:01 +01:00
bool cmFunctionCommand(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-08-30 11:54:41 +02:00
auto fb = cm::make_unique<cmFunctionFunctionBlocker>();
cm::append(fb->Args, args);
status.GetMakefile().AddFunctionBlocker(std::move(fb));
return true;
}