cmake/Source/cmWhileCommand.cxx

145 lines
4.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 "cmWhileCommand.h"
2016-07-09 11:21:54 +02: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"
#include "cmMakefile.h"
#include "cmSystemTools.h"
#include "cmake.h"
2018-01-26 17:06:56 +01:00
#include <memory> // IWYU pragma: keep
2016-07-09 11:21:54 +02:00
cmWhileFunctionBlocker::cmWhileFunctionBlocker(cmMakefile* mf)
: Makefile(mf)
, Depth(0)
2015-08-17 11:37:30 +02:00
{
this->Makefile->PushLoopBlock();
}
cmWhileFunctionBlocker::~cmWhileFunctionBlocker()
{
this->Makefile->PopLoopBlock();
}
2016-07-09 11:21:54 +02:00
bool cmWhileFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff,
cmMakefile& mf,
cmExecutionStatus& inStatus)
{
// at end of for each execute recorded commands
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::Strucmp(lff.Name.c_str(), "while")) {
// record the number of while commands past this one
this->Depth++;
2016-07-09 11:21:54 +02:00
} else if (!cmSystemTools::Strucmp(lff.Name.c_str(), "endwhile")) {
// if this is the endwhile for this while loop then execute
2016-07-09 11:21:54 +02:00
if (!this->Depth) {
// Remove the function blocker for this scope or bail.
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmFunctionBlocker> fb(
mf.RemoveFunctionBlocker(this, lff));
2016-07-09 11:21:54 +02:00
if (!fb.get()) {
return false;
}
std::string errorString;
2012-04-19 19:04:21 +03:00
2015-04-27 22:25:09 +02:00
std::vector<cmExpandedCommandArgument> expandedArguments;
mf.ExpandArguments(this->Args, expandedArguments);
2009-10-04 10:30:41 +03:00
cmake::MessageType messageType;
2015-04-27 22:25:09 +02:00
2015-11-17 17:22:37 +01:00
cmListFileContext execContext = this->GetStartingContext();
cmCommandContext commandContext;
commandContext.Line = execContext.Line;
commandContext.Name = execContext.Name;
2016-07-09 11:21:54 +02:00
cmConditionEvaluator conditionEvaluator(mf, this->GetStartingContext(),
mf.GetBacktrace(commandContext));
2015-11-17 17:22:37 +01:00
2016-07-09 11:21:54 +02:00
bool isTrue =
conditionEvaluator.IsTrue(expandedArguments, errorString, messageType);
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
while (isTrue) {
if (!errorString.empty()) {
2009-10-04 10:30:41 +03:00
std::string err = "had incorrect arguments: ";
2018-01-26 17:06:56 +01:00
for (cmListFileArgument const& arg : this->Args) {
err += (arg.Delim ? "\"" : "");
err += arg.Value;
err += (arg.Delim ? "\"" : "");
2009-10-04 10:30:41 +03:00
err += " ";
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
err += "(";
err += errorString;
err += ").";
mf.IssueMessage(messageType, err);
2016-07-09 11:21:54 +02:00
if (messageType == cmake::FATAL_ERROR) {
2009-10-04 10:30:41 +03:00
cmSystemTools::SetFatalErrorOccured();
return true;
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// Invoke all the functions that were collected in the block.
2018-01-26 17:06:56 +01:00
for (cmListFileFunction const& fn : this->Functions) {
cmExecutionStatus status;
2018-01-26 17:06:56 +01:00
mf.ExecuteCommand(fn, status);
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()) {
return true;
2016-07-09 11:21:54 +02:00
}
if (status.GetContinueInvoked()) {
2015-04-27 22:25:09 +02:00
break;
2016-07-09 11:21:54 +02:00
}
if (cmSystemTools::GetFatalErrorOccured()) {
2009-10-04 10:30:41 +03:00
return true;
}
2016-07-09 11:21:54 +02:00
}
expandedArguments.clear();
mf.ExpandArguments(this->Args, expandedArguments);
2016-07-09 11:21:54 +02:00
isTrue = conditionEvaluator.IsTrue(expandedArguments, errorString,
messageType);
}
2016-07-09 11:21:54 +02:00
return true;
}
2016-10-30 18:24:19 +01:00
// decrement for each nested while that ends
this->Depth--;
2016-07-09 11:21:54 +02:00
}
// record the command
this->Functions.push_back(lff);
2012-04-19 19:04:21 +03:00
// always return true
return true;
}
2016-07-09 11:21:54 +02:00
bool cmWhileFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
cmMakefile&)
{
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::Strucmp(lff.Name.c_str(), "endwhile")) {
// if the endwhile has arguments, then make sure
// they match the arguments of the matching while
2016-07-09 11:21:54 +02:00
if (lff.Arguments.empty() || lff.Arguments == this->Args) {
return true;
}
2016-07-09 11:21:54 +02:00
}
return false;
}
2016-07-09 11:21:54 +02:00
bool cmWhileCommand::InvokeInitialPass(
const std::vector<cmListFileArgument>& args, 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
}
2012-04-19 19:04:21 +03:00
// create a function blocker
2016-07-09 11:21:54 +02:00
cmWhileFunctionBlocker* f = new cmWhileFunctionBlocker(this->Makefile);
f->Args = args;
this->Makefile->AddFunctionBlocker(f);
2012-04-19 19:04:21 +03:00
return true;
}