cmake/Source/cmWhileCommand.cxx

145 lines
4.3 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"
2021-11-20 13:41:27 +01:00
#include "cmOutputConverter.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2021-09-14 00:13:48 +02:00
#include "cmake.h"
2020-02-01 23:06:01 +01:00
class cmWhileFunctionBlocker : public cmFunctionBlocker
{
public:
2021-11-20 13:41:27 +01:00
cmWhileFunctionBlocker(cmMakefile* mf, std::vector<cmListFileArgument> args);
2020-02-01 23:06:01 +01:00
~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;
private:
cmMakefile* Makefile;
2021-11-20 13:41:27 +01:00
std::vector<cmListFileArgument> Args;
2020-02-01 23:06:01 +01:00
};
2018-01-26 17:06:56 +01:00
2021-11-20 13:41:27 +01:00
cmWhileFunctionBlocker::cmWhileFunctionBlocker(
cmMakefile* const mf, std::vector<cmListFileArgument> args)
: Makefile{ mf }
, Args{ std::move(args) }
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
{
2021-09-14 00:13:48 +02:00
return lff.Arguments().empty() || lff.Arguments() == this->Args;
2020-02-01 23:06:01 +01:00
}
2020-02-01 23:06:01 +01:00
bool cmWhileFunctionBlocker::Replay(std::vector<cmListFileFunction> functions,
cmExecutionStatus& inStatus)
{
2021-11-20 13:41:27 +01:00
auto& mf = inStatus.GetMakefile();
2012-04-19 19:04:21 +03:00
2021-09-14 00:13:48 +02:00
cmListFileBacktrace whileBT =
mf.GetBacktrace().Push(this->GetStartingContext());
2020-02-01 23:06:01 +01:00
2021-11-20 13:41:27 +01:00
std::vector<cmExpandedCommandArgument> expandedArguments;
// At least same size expected for `expandedArguments` as `Args`
expandedArguments.reserve(this->Args.size());
auto expandArgs = [&mf](std::vector<cmListFileArgument> const& args,
std::vector<cmExpandedCommandArgument>& out)
-> std::vector<cmExpandedCommandArgument>& {
out.clear();
mf.ExpandArguments(args, out);
return out;
};
2022-03-29 21:10:50 +02:00
// FIXME(#23296): For compatibility with older versions of CMake, we
// tolerate condition errors that evaluate to false. We should add
// a policy to enforce such errors.
bool enforceError = true;
2021-11-20 13:41:27 +01:00
std::string errorString;
MessageType messageType;
for (cmConditionEvaluator conditionEvaluator(mf, whileBT);
2022-03-29 21:10:50 +02:00
(enforceError = /* enforce condition errors that evaluate to true */
conditionEvaluator.IsTrue(expandArgs(this->Args, expandedArguments),
errorString, messageType));) {
2020-02-01 23:06:01 +01:00
// 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;
}
}
2016-07-09 11:21:54 +02:00
}
2021-11-20 13:41:27 +01:00
2022-03-29 21:10:50 +02:00
if (!errorString.empty() && enforceError) {
2021-11-20 13:41:27 +01:00
std::string err = "had incorrect arguments:\n ";
for (auto const& i : expandedArguments) {
err += " ";
err += cmOutputConverter::EscapeForCMake(i.GetValue());
}
err += "\n";
err += errorString;
mf.GetCMakeInstance()->IssueMessage(messageType, err, whileBT);
if (messageType == MessageType::FATAL_ERROR) {
cmSystemTools::SetFatalErrorOccured();
}
}
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
2021-11-20 13:41:27 +01:00
auto& makefile = status.GetMakefile();
makefile.AddFunctionBlocker(
cm::make_unique<cmWhileFunctionBlocker>(&makefile, args));
return true;
}