cmake/Source/cmIfCommand.cxx

210 lines
6.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 "cmIfCommand.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 "cmOutputConverter.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
#include "cmake.h"
2009-11-06 22:07:41 +02:00
static std::string cmIfCommandError(
2015-08-17 11:37:30 +02:00
std::vector<cmExpandedCommandArgument> const& args)
2009-11-06 22:07:41 +02:00
{
std::string err = "given arguments:\n ";
2018-01-26 17:06:56 +01:00
for (cmExpandedCommandArgument const& i : args) {
2009-11-06 22:07:41 +02:00
err += " ";
2018-01-26 17:06:56 +01:00
err += cmOutputConverter::EscapeForCMake(i.GetValue());
2016-07-09 11:21:54 +02:00
}
2009-11-06 22:07:41 +02:00
err += "\n";
return err;
}
2020-02-01 23:06:01 +01:00
class cmIfFunctionBlocker : public cmFunctionBlocker
{
public:
cm::string_view StartCommandName() const override { return "if"_s; }
cm::string_view EndCommandName() const override { return "endif"_s; }
bool ArgumentsMatch(cmListFileFunction const& lff,
cmMakefile&) const override;
bool Replay(std::vector<cmListFileFunction> functions,
cmExecutionStatus& inStatus) override;
std::vector<cmListFileArgument> Args;
bool IsBlocking;
bool HasRun = false;
bool ElseSeen = false;
};
bool cmIfFunctionBlocker::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
}
bool cmIfFunctionBlocker::Replay(std::vector<cmListFileFunction> functions,
cmExecutionStatus& inStatus)
{
cmMakefile& mf = inStatus.GetMakefile();
// execute the functions for the true parts of the if statement
int scopeDepth = 0;
for (cmListFileFunction const& func : functions) {
// keep track of scope depth
2021-09-14 00:13:48 +02:00
if (func.LowerCaseName() == "if") {
2020-02-01 23:06:01 +01:00
scopeDepth++;
}
2021-09-14 00:13:48 +02:00
if (func.LowerCaseName() == "endif") {
2020-02-01 23:06:01 +01:00
scopeDepth--;
}
// watch for our state change
2021-09-14 00:13:48 +02:00
if (scopeDepth == 0 && func.LowerCaseName() == "else") {
2022-08-04 22:12:04 +02:00
cmListFileBacktrace elseBT = mf.GetBacktrace().Push(
cmListFileContext{ func.OriginalName(),
this->GetStartingContext().FilePath, func.Line() });
2020-02-01 23:06:01 +01:00
if (this->ElseSeen) {
mf.GetCMakeInstance()->IssueMessage(
MessageType::FATAL_ERROR,
2021-09-14 00:13:48 +02:00
"A duplicate ELSE command was found inside an IF block.", elseBT);
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
2020-02-01 23:06:01 +01:00
return true;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
this->IsBlocking = this->HasRun;
this->HasRun = true;
this->ElseSeen = true;
// if trace is enabled, print a (trivially) evaluated "else"
// statement
if (!this->IsBlocking && mf.GetCMakeInstance()->GetTrace()) {
2022-08-04 22:12:04 +02:00
mf.PrintCommandTrace(func, elseBT,
cmMakefile::CommandMissingFromStack::Yes);
2020-02-01 23:06:01 +01:00
}
2021-09-14 00:13:48 +02:00
} else if (scopeDepth == 0 && func.LowerCaseName() == "elseif") {
cmListFileBacktrace elseifBT = mf.GetBacktrace().Push(
cmListFileContext{ func.OriginalName(),
this->GetStartingContext().FilePath, func.Line() });
2020-02-01 23:06:01 +01:00
if (this->ElseSeen) {
mf.GetCMakeInstance()->IssueMessage(
MessageType::FATAL_ERROR,
2021-09-14 00:13:48 +02:00
"An ELSEIF command was found after an ELSE command.", elseifBT);
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
2020-02-01 23:06:01 +01:00
return true;
}
if (this->HasRun) {
this->IsBlocking = true;
} else {
// if trace is enabled, print the evaluated "elseif" statement
if (mf.GetCMakeInstance()->GetTrace()) {
2022-08-04 22:12:04 +02:00
mf.PrintCommandTrace(func, elseifBT,
cmMakefile::CommandMissingFromStack::Yes);
2016-07-09 11:21:54 +02:00
}
2017-07-20 19:35:53 +02:00
2020-02-01 23:06:01 +01:00
std::string errorString;
2012-06-27 20:52:58 +03:00
2020-02-01 23:06:01 +01:00
std::vector<cmExpandedCommandArgument> expandedArguments;
2021-09-14 00:13:48 +02:00
mf.ExpandArguments(func.Arguments(), expandedArguments);
2020-02-01 23:06:01 +01:00
MessageType messType;
2021-09-14 00:13:48 +02:00
cmConditionEvaluator conditionEvaluator(mf, elseifBT);
2020-02-01 23:06:01 +01:00
bool isTrue =
conditionEvaluator.IsTrue(expandedArguments, errorString, messType);
if (!errorString.empty()) {
std::string err =
cmStrCat(cmIfCommandError(expandedArguments), errorString);
2021-09-14 00:13:48 +02:00
mf.GetCMakeInstance()->IssueMessage(messType, err, elseifBT);
2020-02-01 23:06:01 +01:00
if (messType == MessageType::FATAL_ERROR) {
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
2017-07-20 19:35:53 +02:00
return true;
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2020-02-01 23:06:01 +01:00
if (isTrue) {
this->IsBlocking = false;
this->HasRun = true;
}
}
}
2009-10-04 10:30:41 +03:00
2020-02-01 23:06:01 +01:00
// should we execute?
else if (!this->IsBlocking) {
cmExecutionStatus status(mf);
mf.ExecuteCommand(func, status);
if (status.GetReturnInvoked()) {
2022-11-16 20:14:03 +01:00
inStatus.SetReturnInvoked(status.GetReturnVariables());
2020-02-01 23:06:01 +01:00
return true;
}
if (status.GetBreakInvoked()) {
inStatus.SetBreakInvoked();
return true;
}
if (status.GetContinueInvoked()) {
inStatus.SetContinueInvoked();
return true;
}
}
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 cmIfCommand(std::vector<cmListFileArgument> const& args,
cmExecutionStatus& inStatus)
{
2020-02-01 23:06:01 +01:00
cmMakefile& makefile = inStatus.GetMakefile();
std::string errorString;
2009-10-04 10:30:41 +03:00
2015-04-27 22:25:09 +02:00
std::vector<cmExpandedCommandArgument> expandedArguments;
2020-02-01 23:06:01 +01:00
makefile.ExpandArguments(args, expandedArguments);
2009-10-04 10:30:41 +03:00
2019-11-11 23:01:05 +01:00
MessageType status;
2009-10-04 10:30:41 +03:00
2021-09-14 00:13:48 +02:00
cmConditionEvaluator conditionEvaluator(makefile, makefile.GetBacktrace());
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
bool isTrue =
conditionEvaluator.IsTrue(expandedArguments, errorString, status);
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (!errorString.empty()) {
2020-02-01 23:06:01 +01:00
std::string err =
cmStrCat("if ", cmIfCommandError(expandedArguments), errorString);
2019-11-11 23:01:05 +01:00
if (status == MessageType::FATAL_ERROR) {
2020-02-01 23:06:01 +01:00
makefile.IssueMessage(MessageType::FATAL_ERROR, err);
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
2016-10-30 18:24:19 +01:00
return true;
}
2020-02-01 23:06:01 +01:00
makefile.IssueMessage(status, err);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2020-02-01 23:06:01 +01:00
{
auto fb = cm::make_unique<cmIfFunctionBlocker>();
// if is isn't true block the commands
fb->IsBlocking = !isTrue;
if (isTrue) {
fb->HasRun = true;
}
fb->Args = args;
makefile.AddFunctionBlocker(std::move(fb));
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
return true;
}