cmake/Source/cmIfCommand.cxx

220 lines
7.2 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
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 "cmOutputConverter.h"
#include "cmSystemTools.h"
#include "cm_auto_ptr.hxx"
#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 ";
2016-07-09 11:21:54 +02:00
for (std::vector<cmExpandedCommandArgument>::const_iterator i = args.begin();
i != args.end(); ++i) {
2009-11-06 22:07:41 +02:00
err += " ";
2015-11-17 17:22:37 +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;
}
//=========================================================================
2016-07-09 11:21:54 +02:00
bool cmIfFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff,
cmMakefile& mf,
cmExecutionStatus& inStatus)
{
// we start by recording all the functions
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::Strucmp(lff.Name.c_str(), "if")) {
this->ScopeDepth++;
2016-07-09 11:21:54 +02:00
} else if (!cmSystemTools::Strucmp(lff.Name.c_str(), "endif")) {
this->ScopeDepth--;
// if this is the endif for this if statement, then start executing
2016-07-09 11:21:54 +02:00
if (!this->ScopeDepth) {
// Remove the function blocker for this scope or bail.
2016-10-30 18:24:19 +01:00
CM_AUTO_PTR<cmFunctionBlocker> fb(mf.RemoveFunctionBlocker(this, lff));
2016-07-09 11:21:54 +02:00
if (!fb.get()) {
return false;
}
// execute the functions for the true parts of the if statement
cmExecutionStatus status;
int scopeDepth = 0;
2016-07-09 11:21:54 +02:00
for (unsigned int c = 0; c < this->Functions.size(); ++c) {
// keep track of scope depth
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(), "if")) {
scopeDepth++;
2016-07-09 11:21:54 +02:00
}
if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),
"endif")) {
scopeDepth--;
2016-07-09 11:21:54 +02:00
}
// watch for our state change
if (scopeDepth == 0 &&
2016-07-09 11:21:54 +02:00
!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(), "else")) {
2017-07-20 19:35:53 +02:00
if (this->ElseSeen) {
cmListFileBacktrace bt = mf.GetBacktrace(this->Functions[c]);
mf.GetCMakeInstance()->IssueMessage(
cmake::FATAL_ERROR,
"A duplicate ELSE command was found inside an IF block.", bt);
cmSystemTools::SetFatalErrorOccured();
return true;
}
this->IsBlocking = this->HasRun;
this->HasRun = true;
2017-07-20 19:35:53 +02:00
this->ElseSeen = true;
2012-06-27 20:52:58 +03:00
// if trace is enabled, print a (trivially) evaluated "else"
// statement
2016-07-09 11:21:54 +02:00
if (!this->IsBlocking && mf.GetCMakeInstance()->GetTrace()) {
2012-06-27 20:52:58 +03:00
mf.PrintCommandTrace(this->Functions[c]);
}
2016-07-09 11:21:54 +02:00
} else if (scopeDepth == 0 &&
!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),
"elseif")) {
2017-07-20 19:35:53 +02:00
if (this->ElseSeen) {
cmListFileBacktrace bt = mf.GetBacktrace(this->Functions[c]);
mf.GetCMakeInstance()->IssueMessage(
cmake::FATAL_ERROR,
"An ELSEIF command was found after an ELSE command.", bt);
cmSystemTools::SetFatalErrorOccured();
return true;
}
2016-07-09 11:21:54 +02:00
if (this->HasRun) {
this->IsBlocking = true;
2016-07-09 11:21:54 +02:00
} else {
2012-06-27 20:52:58 +03:00
// if trace is enabled, print the evaluated "elseif" statement
2016-07-09 11:21:54 +02:00
if (mf.GetCMakeInstance()->GetTrace()) {
2012-06-27 20:52:58 +03:00
mf.PrintCommandTrace(this->Functions[c]);
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
std::string errorString;
2009-10-04 10:30:41 +03:00
2015-04-27 22:25:09 +02:00
std::vector<cmExpandedCommandArgument> expandedArguments;
2009-10-04 10:30:41 +03:00
mf.ExpandArguments(this->Functions[c].Arguments,
expandedArguments);
2009-10-04 10:30:41 +03:00
cmake::MessageType messType;
2015-11-17 17:22:37 +01:00
cmListFileContext conditionContext =
2016-07-09 11:21:54 +02:00
cmListFileContext::FromCommandContext(
this->Functions[c], this->GetStartingContext().FilePath);
2015-11-17 17:22:37 +01:00
cmConditionEvaluator conditionEvaluator(
2016-07-09 11:21:54 +02:00
mf, conditionContext, mf.GetBacktrace(this->Functions[c]));
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
bool isTrue = conditionEvaluator.IsTrue(expandedArguments,
errorString, messType);
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (!errorString.empty()) {
2015-08-17 11:37:30 +02:00
std::string err = cmIfCommandError(expandedArguments);
err += errorString;
2015-11-17 17:22:37 +01:00
cmListFileBacktrace bt = mf.GetBacktrace(this->Functions[c]);
mf.GetCMakeInstance()->IssueMessage(messType, err, bt);
2016-07-09 11:21:54 +02:00
if (messType == 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
2016-07-09 11:21:54 +02:00
if (isTrue) {
this->IsBlocking = false;
this->HasRun = true;
}
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// should we execute?
2016-07-09 11:21:54 +02:00
else if (!this->IsBlocking) {
status.Clear();
2016-07-09 11:21:54 +02:00
mf.ExecuteCommand(this->Functions[c], status);
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()) {
2017-07-20 19:35:53 +02:00
inStatus.SetBreakInvoked();
return true;
2016-07-09 11:21:54 +02:00
}
if (status.GetContinueInvoked()) {
2017-07-20 19:35:53 +02:00
inStatus.SetContinueInvoked();
2015-04-27 22:25:09 +02:00
return true;
}
}
}
2016-07-09 11:21:54 +02:00
return true;
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// record the command
this->Functions.push_back(lff);
2009-10-04 10:30:41 +03:00
// always return true
return true;
}
//=========================================================================
bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
cmMakefile&)
{
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::Strucmp(lff.Name.c_str(), "endif")) {
// if the endif has arguments, then make sure
// they match the arguments of the matching if
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 cmIfCommand::InvokeInitialPass(
const std::vector<cmListFileArgument>& args, cmExecutionStatus&)
{
std::string errorString;
2009-10-04 10:30:41 +03:00
2015-04-27 22:25:09 +02:00
std::vector<cmExpandedCommandArgument> expandedArguments;
this->Makefile->ExpandArguments(args, expandedArguments);
2009-10-04 10:30:41 +03:00
cmake::MessageType status;
2015-11-17 17:22:37 +01:00
cmConditionEvaluator conditionEvaluator(
2016-07-09 11:21:54 +02:00
*(this->Makefile), this->Makefile->GetExecutionContext(),
this->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()) {
2016-10-30 18:24:19 +01:00
std::string err = "if " + cmIfCommandError(expandedArguments);
err += errorString;
2016-07-09 11:21:54 +02:00
if (status == cmake::FATAL_ERROR) {
2016-10-30 18:24:19 +01:00
this->Makefile->IssueMessage(cmake::FATAL_ERROR, err);
2009-10-04 10:30:41 +03:00
cmSystemTools::SetFatalErrorOccured();
2016-10-30 18:24:19 +01:00
return true;
}
2016-10-30 18:24:19 +01:00
this->Makefile->IssueMessage(status, err);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
cmIfFunctionBlocker* f = new cmIfFunctionBlocker();
// if is isn't true block the commands
f->ScopeDepth = 1;
f->IsBlocking = !isTrue;
2016-07-09 11:21:54 +02:00
if (isTrue) {
f->HasRun = true;
2016-07-09 11:21:54 +02:00
}
f->Args = args;
this->Makefile->AddFunctionBlocker(f);
2009-10-04 10:30:41 +03:00
return true;
}