2016-09-27 15:01:08 -04:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2001-04-19 17:39:03 -04:00
|
|
|
#include "cmIfCommand.h"
|
2016-04-29 09:40:20 -04:00
|
|
|
|
2016-10-25 20:35:04 +02:00
|
|
|
#include "cmConditionEvaluator.h"
|
|
|
|
#include "cmExecutionStatus.h"
|
|
|
|
#include "cmExpandedCommandArgument.h"
|
|
|
|
#include "cmMakefile.h"
|
2015-10-05 20:57:47 +02:00
|
|
|
#include "cmOutputConverter.h"
|
2016-10-19 22:30:58 +02:00
|
|
|
#include "cmSystemTools.h"
|
2016-10-25 20:35:04 +02:00
|
|
|
#include "cmake.h"
|
2003-06-23 14:10:12 -04:00
|
|
|
|
2017-09-21 23:06:05 +02:00
|
|
|
#include <memory> // IWYU pragma: keep
|
|
|
|
|
2009-10-27 09:01:33 -04:00
|
|
|
static std::string cmIfCommandError(
|
2015-05-05 21:50:39 +02:00
|
|
|
std::vector<cmExpandedCommandArgument> const& args)
|
2009-10-27 09:01:33 -04:00
|
|
|
{
|
|
|
|
std::string err = "given arguments:\n ";
|
2017-09-11 13:40:26 +03:00
|
|
|
for (cmExpandedCommandArgument const& i : args) {
|
2009-10-27 09:01:33 -04:00
|
|
|
err += " ";
|
2017-09-11 13:40:26 +03:00
|
|
|
err += cmOutputConverter::EscapeForCMake(i.GetValue());
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
2009-10-27 09:01:33 -04:00
|
|
|
err += "\n";
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2008-06-26 13:01:35 -04:00
|
|
|
//=========================================================================
|
2016-05-16 10:34:04 -04:00
|
|
|
bool cmIfFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff,
|
|
|
|
cmMakefile& mf,
|
|
|
|
cmExecutionStatus& inStatus)
|
2001-04-19 17:39:03 -04:00
|
|
|
{
|
2008-01-23 10:28:26 -05:00
|
|
|
// we start by recording all the functions
|
2018-05-01 16:17:31 +02:00
|
|
|
if (lff.Name.Lower == "if") {
|
2008-01-23 10:28:26 -05:00
|
|
|
this->ScopeDepth++;
|
2018-05-01 16:17:31 +02:00
|
|
|
} else if (lff.Name.Lower == "endif") {
|
2008-01-23 10:28:26 -05:00
|
|
|
this->ScopeDepth--;
|
|
|
|
// if this is the endif for this if statement, then start executing
|
2016-05-16 10:34:04 -04:00
|
|
|
if (!this->ScopeDepth) {
|
2009-01-20 14:36:18 -05:00
|
|
|
// Remove the function blocker for this scope or bail.
|
2017-09-21 23:06:05 +02:00
|
|
|
std::unique_ptr<cmFunctionBlocker> fb(
|
|
|
|
mf.RemoveFunctionBlocker(this, lff));
|
2016-05-16 10:34:04 -04:00
|
|
|
if (!fb.get()) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-20 14:36:18 -05:00
|
|
|
|
2008-01-23 10:28:26 -05:00
|
|
|
// execute the functions for the true parts of the if statement
|
|
|
|
cmExecutionStatus status;
|
|
|
|
int scopeDepth = 0;
|
2017-09-11 13:40:26 +03:00
|
|
|
for (cmListFileFunction const& func : this->Functions) {
|
2008-01-23 10:28:26 -05:00
|
|
|
// keep track of scope depth
|
2018-05-01 16:17:31 +02:00
|
|
|
if (func.Name.Lower == "if") {
|
2008-01-23 10:28:26 -05:00
|
|
|
scopeDepth++;
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
2018-05-01 16:17:31 +02:00
|
|
|
if (func.Name.Lower == "endif") {
|
2008-01-23 10:28:26 -05:00
|
|
|
scopeDepth--;
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
2008-01-23 10:28:26 -05:00
|
|
|
// watch for our state change
|
2018-05-01 16:17:31 +02:00
|
|
|
if (scopeDepth == 0 && func.Name.Lower == "else") {
|
2017-03-20 21:49:59 +01:00
|
|
|
|
|
|
|
if (this->ElseSeen) {
|
2017-09-11 13:40:26 +03:00
|
|
|
cmListFileBacktrace bt = mf.GetBacktrace(func);
|
2017-03-20 21:49:59 +01:00
|
|
|
mf.GetCMakeInstance()->IssueMessage(
|
|
|
|
cmake::FATAL_ERROR,
|
|
|
|
"A duplicate ELSE command was found inside an IF block.", bt);
|
|
|
|
cmSystemTools::SetFatalErrorOccured();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-01-23 10:28:26 -05:00
|
|
|
this->IsBlocking = this->HasRun;
|
|
|
|
this->HasRun = true;
|
2017-03-20 21:49:59 +01:00
|
|
|
this->ElseSeen = true;
|
2012-05-14 18:50:30 -04:00
|
|
|
|
|
|
|
// if trace is enabled, print a (trivially) evaluated "else"
|
|
|
|
// statement
|
2016-05-16 10:34:04 -04:00
|
|
|
if (!this->IsBlocking && mf.GetCMakeInstance()->GetTrace()) {
|
2017-09-11 13:40:26 +03:00
|
|
|
mf.PrintCommandTrace(func);
|
2008-01-23 10:28:26 -05:00
|
|
|
}
|
2018-05-01 16:17:31 +02:00
|
|
|
} else if (scopeDepth == 0 && func.Name.Lower == "elseif") {
|
2017-03-20 21:49:59 +01:00
|
|
|
if (this->ElseSeen) {
|
2017-09-11 13:40:26 +03:00
|
|
|
cmListFileBacktrace bt = mf.GetBacktrace(func);
|
2017-03-20 21:49:59 +01:00
|
|
|
mf.GetCMakeInstance()->IssueMessage(
|
|
|
|
cmake::FATAL_ERROR,
|
|
|
|
"An ELSEIF command was found after an ELSE command.", bt);
|
|
|
|
cmSystemTools::SetFatalErrorOccured();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 10:34:04 -04:00
|
|
|
if (this->HasRun) {
|
2008-01-23 10:28:26 -05:00
|
|
|
this->IsBlocking = true;
|
2016-05-16 10:34:04 -04:00
|
|
|
} else {
|
2012-05-14 18:50:30 -04:00
|
|
|
// if trace is enabled, print the evaluated "elseif" statement
|
2016-05-16 10:34:04 -04:00
|
|
|
if (mf.GetCMakeInstance()->GetTrace()) {
|
2017-09-11 13:40:26 +03:00
|
|
|
mf.PrintCommandTrace(func);
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
2012-05-14 18:50:30 -04:00
|
|
|
|
2008-06-28 11:16:36 -04:00
|
|
|
std::string errorString;
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2014-09-04 20:21:28 +02:00
|
|
|
std::vector<cmExpandedCommandArgument> expandedArguments;
|
2017-09-11 13:40:26 +03:00
|
|
|
mf.ExpandArguments(func.Arguments, expandedArguments);
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2009-06-12 11:10:26 -04:00
|
|
|
cmake::MessageType messType;
|
2014-09-11 19:50:51 +02:00
|
|
|
|
2015-10-20 19:13:52 +02:00
|
|
|
cmListFileContext conditionContext =
|
2016-05-16 10:34:04 -04:00
|
|
|
cmListFileContext::FromCommandContext(
|
2017-09-11 13:40:26 +03:00
|
|
|
func, this->GetStartingContext().FilePath);
|
2015-10-20 19:13:52 +02:00
|
|
|
|
2017-09-11 13:40:26 +03:00
|
|
|
cmConditionEvaluator conditionEvaluator(mf, conditionContext,
|
|
|
|
mf.GetBacktrace(func));
|
2014-09-11 19:50:51 +02:00
|
|
|
|
2016-05-16 10:34:04 -04:00
|
|
|
bool isTrue = conditionEvaluator.IsTrue(expandedArguments,
|
|
|
|
errorString, messType);
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2016-05-16 10:34:04 -04:00
|
|
|
if (!errorString.empty()) {
|
2015-05-05 21:50:39 +02:00
|
|
|
std::string err = cmIfCommandError(expandedArguments);
|
2008-01-23 10:28:26 -05:00
|
|
|
err += errorString;
|
2017-09-11 13:40:26 +03:00
|
|
|
cmListFileBacktrace bt = mf.GetBacktrace(func);
|
2015-05-29 01:14:19 +02:00
|
|
|
mf.GetCMakeInstance()->IssueMessage(messType, err, bt);
|
2016-05-16 10:34:04 -04:00
|
|
|
if (messType == cmake::FATAL_ERROR) {
|
2009-06-12 10:07:05 -04:00
|
|
|
cmSystemTools::SetFatalErrorOccured();
|
|
|
|
return true;
|
2008-01-23 10:28:26 -05:00
|
|
|
}
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2016-05-16 10:34:04 -04:00
|
|
|
if (isTrue) {
|
2008-01-23 10:28:26 -05:00
|
|
|
this->IsBlocking = false;
|
|
|
|
this->HasRun = true;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2008-01-23 10:28:26 -05:00
|
|
|
// should we execute?
|
2016-05-16 10:34:04 -04:00
|
|
|
else if (!this->IsBlocking) {
|
2008-01-23 10:28:26 -05:00
|
|
|
status.Clear();
|
2017-09-11 13:40:26 +03:00
|
|
|
mf.ExecuteCommand(func, status);
|
2016-05-16 10:34:04 -04:00
|
|
|
if (status.GetReturnInvoked()) {
|
2016-12-26 10:30:31 +01:00
|
|
|
inStatus.SetReturnInvoked();
|
2008-01-23 10:28:26 -05:00
|
|
|
return true;
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
|
|
|
if (status.GetBreakInvoked()) {
|
2016-12-26 10:30:31 +01:00
|
|
|
inStatus.SetBreakInvoked();
|
2008-01-23 10:28:26 -05:00
|
|
|
return true;
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
|
|
|
if (status.GetContinueInvoked()) {
|
2016-12-26 10:30:31 +01:00
|
|
|
inStatus.SetContinueInvoked();
|
2014-11-29 17:56:18 +01:00
|
|
|
return true;
|
2006-09-22 11:23:51 -04:00
|
|
|
}
|
2002-08-09 12:00:49 -04:00
|
|
|
}
|
2006-09-22 11:23:51 -04:00
|
|
|
}
|
2016-05-16 10:34:04 -04:00
|
|
|
return true;
|
2006-09-22 11:23:51 -04:00
|
|
|
}
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2008-01-23 10:28:26 -05:00
|
|
|
// record the command
|
|
|
|
this->Functions.push_back(lff);
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2008-01-23 10:28:26 -05:00
|
|
|
// always return true
|
|
|
|
return true;
|
2001-04-19 17:39:03 -04:00
|
|
|
}
|
|
|
|
|
2008-06-26 13:01:35 -04:00
|
|
|
//=========================================================================
|
2002-12-11 18:13:33 -05:00
|
|
|
bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
|
2008-02-29 21:33:33 -05:00
|
|
|
cmMakefile&)
|
2001-04-19 17:39:03 -04:00
|
|
|
{
|
2018-05-01 16:17:31 +02:00
|
|
|
if (lff.Name.Lower == "endif") {
|
2008-02-29 12:18:11 -05:00
|
|
|
// if the endif has arguments, then make sure
|
|
|
|
// they match the arguments of the matching if
|
2016-05-16 10:34:04 -04:00
|
|
|
if (lff.Arguments.empty() || lff.Arguments == this->Args) {
|
2002-07-10 11:38:38 -04:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
2006-05-18 13:50:01 -04:00
|
|
|
|
2002-07-10 11:38:38 -04:00
|
|
|
return false;
|
2001-04-19 17:39:03 -04:00
|
|
|
}
|
|
|
|
|
2008-06-26 13:01:35 -04:00
|
|
|
//=========================================================================
|
2016-05-16 10:34:04 -04:00
|
|
|
bool cmIfCommand::InvokeInitialPass(
|
|
|
|
const std::vector<cmListFileArgument>& args, cmExecutionStatus&)
|
2001-04-19 17:39:03 -04:00
|
|
|
{
|
2008-06-28 11:16:36 -04:00
|
|
|
std::string errorString;
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2014-09-04 20:21:28 +02:00
|
|
|
std::vector<cmExpandedCommandArgument> expandedArguments;
|
2006-03-15 11:02:08 -05:00
|
|
|
this->Makefile->ExpandArguments(args, expandedArguments);
|
2009-06-12 10:07:05 -04:00
|
|
|
|
|
|
|
cmake::MessageType status;
|
2014-09-11 19:50:51 +02:00
|
|
|
|
2015-10-20 19:13:52 +02:00
|
|
|
cmConditionEvaluator conditionEvaluator(
|
2016-05-16 10:34:04 -04:00
|
|
|
*(this->Makefile), this->Makefile->GetExecutionContext(),
|
|
|
|
this->Makefile->GetBacktrace());
|
2014-09-11 19:50:51 +02:00
|
|
|
|
2016-05-16 10:34:04 -04:00
|
|
|
bool isTrue =
|
|
|
|
conditionEvaluator.IsTrue(expandedArguments, errorString, status);
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2016-05-16 10:34:04 -04:00
|
|
|
if (!errorString.empty()) {
|
2016-01-28 22:10:29 +01:00
|
|
|
std::string err = "if " + cmIfCommandError(expandedArguments);
|
2004-08-03 08:13:54 -04:00
|
|
|
err += errorString;
|
2016-05-16 10:34:04 -04:00
|
|
|
if (status == cmake::FATAL_ERROR) {
|
2016-01-28 22:10:29 +01:00
|
|
|
this->Makefile->IssueMessage(cmake::FATAL_ERROR, err);
|
2009-06-12 10:07:05 -04:00
|
|
|
cmSystemTools::SetFatalErrorOccured();
|
2016-01-28 22:10:29 +01:00
|
|
|
return true;
|
2001-04-19 17:39:03 -04:00
|
|
|
}
|
2016-09-16 22:45:24 +02:00
|
|
|
this->Makefile->IssueMessage(status, err);
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2016-05-16 10:34:04 -04:00
|
|
|
cmIfFunctionBlocker* f = new cmIfFunctionBlocker();
|
2002-07-10 11:38:38 -04:00
|
|
|
// if is isn't true block the commands
|
2008-01-23 10:28:26 -05:00
|
|
|
f->ScopeDepth = 1;
|
2006-03-15 11:02:08 -05:00
|
|
|
f->IsBlocking = !isTrue;
|
2016-05-16 10:34:04 -04:00
|
|
|
if (isTrue) {
|
2006-09-22 11:23:51 -04:00
|
|
|
f->HasRun = true;
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
2006-03-15 11:02:08 -05:00
|
|
|
f->Args = args;
|
|
|
|
this->Makefile->AddFunctionBlocker(f);
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2002-07-01 08:49:36 -04:00
|
|
|
return true;
|
|
|
|
}
|