cmake/Source/cmIfCommand.cxx

211 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 "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
{
2020-02-01 23:06:01 +01:00
return lff.Arguments.empty() || lff.Arguments == this->Args;
}
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
if (func.Name.Lower == "if") {
scopeDepth++;
}
if (func.Name.Lower == "endif") {
scopeDepth--;
}
// watch for our state change
if (scopeDepth == 0 && func.Name.Lower == "else") {
if (this->ElseSeen) {
cmListFileBacktrace bt = mf.GetBacktrace(func);
mf.GetCMakeInstance()->IssueMessage(
MessageType::FATAL_ERROR,
"A duplicate ELSE command was found inside an IF block.", bt);
cmSystemTools::SetFatalErrorOccured();
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()) {
mf.PrintCommandTrace(func);
}
} else if (scopeDepth == 0 && func.Name.Lower == "elseif") {
if (this->ElseSeen) {
cmListFileBacktrace bt = mf.GetBacktrace(func);
mf.GetCMakeInstance()->IssueMessage(
MessageType::FATAL_ERROR,
"An ELSEIF command was found after an ELSE command.", bt);
cmSystemTools::SetFatalErrorOccured();
return true;
}
if (this->HasRun) {
this->IsBlocking = true;
} else {
// if trace is enabled, print the evaluated "elseif" statement
if (mf.GetCMakeInstance()->GetTrace()) {
mf.PrintCommandTrace(func);
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;
mf.ExpandArguments(func.Arguments, expandedArguments);
MessageType messType;
cmListFileContext conditionContext =
cmListFileContext::FromCommandContext(
func, this->GetStartingContext().FilePath);
cmConditionEvaluator conditionEvaluator(mf, conditionContext,
mf.GetBacktrace(func));
bool isTrue =
conditionEvaluator.IsTrue(expandedArguments, errorString, messType);
if (!errorString.empty()) {
std::string err =
cmStrCat(cmIfCommandError(expandedArguments), errorString);
cmListFileBacktrace bt = mf.GetBacktrace(func);
mf.GetCMakeInstance()->IssueMessage(messType, err, bt);
if (messType == MessageType::FATAL_ERROR) {
2017-07-20 19:35:53 +02:00
cmSystemTools::SetFatalErrorOccured();
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()) {
inStatus.SetReturnInvoked();
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
2015-11-17 17:22:37 +01:00
cmConditionEvaluator conditionEvaluator(
2020-02-01 23:06:01 +01:00
makefile, makefile.GetExecutionContext(), 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);
2009-10-04 10:30:41 +03:00
cmSystemTools::SetFatalErrorOccured();
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;
}