cmake/Source/cmWhileCommand.cxx

143 lines
3.9 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 "cmWhileCommand.h"
2016-07-09 11:21:54 +02:00
2020-02-01 23:06:01 +01:00
#include <string>
#include <utility>
#include <cm/memory>
#include <cm/string_view>
2020-08-30 11:54:41 +02:00
#include <cmext/string_view>
2020-02-01 23:06:01 +01:00
2015-04-27 22:25:09 +02:00
#include "cmConditionEvaluator.h"
2017-04-14 19:02:05 +02:00
#include "cmExecutionStatus.h"
#include "cmExpandedCommandArgument.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"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2020-02-01 23:06:01 +01:00
class cmWhileFunctionBlocker : public cmFunctionBlocker
{
public:
cmWhileFunctionBlocker(cmMakefile* mf);
~cmWhileFunctionBlocker() override;
cm::string_view StartCommandName() const override { return "while"_s; }
cm::string_view EndCommandName() const override { return "endwhile"_s; }
bool ArgumentsMatch(cmListFileFunction const& lff,
cmMakefile& mf) const override;
bool Replay(std::vector<cmListFileFunction> functions,
cmExecutionStatus& inStatus) override;
std::vector<cmListFileArgument> Args;
private:
cmMakefile* Makefile;
};
2018-01-26 17:06:56 +01:00
2016-07-09 11:21:54 +02:00
cmWhileFunctionBlocker::cmWhileFunctionBlocker(cmMakefile* mf)
: Makefile(mf)
2015-08-17 11:37:30 +02:00
{
this->Makefile->PushLoopBlock();
}
cmWhileFunctionBlocker::~cmWhileFunctionBlocker()
{
this->Makefile->PopLoopBlock();
}
2020-02-01 23:06:01 +01:00
bool cmWhileFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff,
cmMakefile&) const
{
2020-02-01 23:06:01 +01:00
return lff.Arguments.empty() || lff.Arguments == this->Args;
}
2020-02-01 23:06:01 +01:00
bool cmWhileFunctionBlocker::Replay(std::vector<cmListFileFunction> functions,
cmExecutionStatus& inStatus)
{
cmMakefile& mf = inStatus.GetMakefile();
std::string errorString;
2020-02-01 23:06:01 +01:00
std::vector<cmExpandedCommandArgument> expandedArguments;
mf.ExpandArguments(this->Args, expandedArguments);
MessageType messageType;
2012-04-19 19:04:21 +03:00
2020-02-01 23:06:01 +01:00
cmListFileContext execContext = this->GetStartingContext();
2020-02-01 23:06:01 +01:00
cmCommandContext commandContext;
commandContext.Line = execContext.Line;
commandContext.Name = execContext.Name;
cmConditionEvaluator conditionEvaluator(mf, this->GetStartingContext(),
mf.GetBacktrace(commandContext));
bool isTrue =
conditionEvaluator.IsTrue(expandedArguments, errorString, messageType);
while (isTrue) {
if (!errorString.empty()) {
std::string err = "had incorrect arguments: ";
for (cmListFileArgument const& arg : this->Args) {
err += (arg.Delim ? "\"" : "");
err += arg.Value;
err += (arg.Delim ? "\"" : "");
err += " ";
}
err += "(";
err += errorString;
err += ").";
mf.IssueMessage(messageType, err);
if (messageType == MessageType::FATAL_ERROR) {
cmSystemTools::SetFatalErrorOccured();
return true;
}
}
// Invoke all the functions that were collected in the block.
for (cmListFileFunction const& fn : functions) {
cmExecutionStatus status(mf);
mf.ExecuteCommand(fn, status);
if (status.GetReturnInvoked()) {
inStatus.SetReturnInvoked();
return true;
}
if (status.GetBreakInvoked()) {
return true;
}
if (status.GetContinueInvoked()) {
break;
}
if (cmSystemTools::GetFatalErrorOccured()) {
return true;
}
}
2020-02-01 23:06:01 +01:00
expandedArguments.clear();
mf.ExpandArguments(this->Args, expandedArguments);
isTrue =
conditionEvaluator.IsTrue(expandedArguments, errorString, messageType);
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
return true;
}
2020-02-01 23:06:01 +01:00
bool cmWhileCommand(std::vector<cmListFileArgument> 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
}
2012-04-19 19:04:21 +03:00
// create a function blocker
2020-02-01 23:06:01 +01:00
{
cmMakefile& makefile = status.GetMakefile();
auto fb = cm::make_unique<cmWhileFunctionBlocker>(&makefile);
fb->Args = args;
makefile.AddFunctionBlocker(std::move(fb));
}
return true;
}