2009-09-28 11:43:28 -04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2001-04-19 17:39:03 -04:00
|
|
|
|
2009-09-28 11:43:28 -04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2001-04-19 17:39:03 -04:00
|
|
|
|
2009-09-28 11:43:28 -04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2001-04-19 17:39:03 -04:00
|
|
|
#include "cmIfCommand.h"
|
2016-04-29 09:40:20 -04:00
|
|
|
|
2015-10-05 20:57:47 +02:00
|
|
|
#include "cmOutputConverter.h"
|
2016-04-29 10:53:13 -04:00
|
|
|
#include "cmStringCommand.h"
|
2007-08-29 11:58:38 -04:00
|
|
|
|
2014-09-11 19:50:51 +02:00
|
|
|
#include "cmConditionEvaluator.h"
|
|
|
|
|
2003-06-23 14:10:12 -04:00
|
|
|
#include <cmsys/RegularExpression.hxx>
|
2016-04-29 10:53:13 -04:00
|
|
|
#include <list>
|
|
|
|
#include <stdlib.h> // required for atof
|
2003-06-23 14:10:12 -04:00
|
|
|
|
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 ";
|
2016-05-16 10:34:04 -04:00
|
|
|
for (std::vector<cmExpandedCommandArgument>::const_iterator i = args.begin();
|
|
|
|
i != args.end(); ++i) {
|
2009-10-27 09:01:33 -04:00
|
|
|
err += " ";
|
2015-05-16 00:55:21 +02: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
|
2016-05-16 10:34:04 -04:00
|
|
|
if (!cmSystemTools::Strucmp(lff.Name.c_str(), "if")) {
|
2008-01-23 10:28:26 -05:00
|
|
|
this->ScopeDepth++;
|
2016-05-16 10:34:04 -04:00
|
|
|
} else if (!cmSystemTools::Strucmp(lff.Name.c_str(), "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.
|
2016-06-28 10:17:52 -04:00
|
|
|
CM_AUTO_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;
|
2016-05-16 10:34:04 -04:00
|
|
|
for (unsigned int c = 0; c < this->Functions.size(); ++c) {
|
2008-01-23 10:28:26 -05:00
|
|
|
// keep track of scope depth
|
2016-05-16 10:34:04 -04:00
|
|
|
if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(), "if")) {
|
2008-01-23 10:28:26 -05:00
|
|
|
scopeDepth++;
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
|
|
|
if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),
|
|
|
|
"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
|
|
|
|
if (scopeDepth == 0 &&
|
2016-05-16 10:34:04 -04:00
|
|
|
!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(), "else")) {
|
2008-01-23 10:28:26 -05:00
|
|
|
this->IsBlocking = this->HasRun;
|
|
|
|
this->HasRun = 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()) {
|
2012-05-14 18:50:30 -04:00
|
|
|
mf.PrintCommandTrace(this->Functions[c]);
|
2008-01-23 10:28:26 -05:00
|
|
|
}
|
2016-05-16 10:34:04 -04:00
|
|
|
} else if (scopeDepth == 0 &&
|
|
|
|
!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),
|
|
|
|
"elseif")) {
|
|
|
|
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()) {
|
2012-05-14 18:50:30 -04:00
|
|
|
mf.PrintCommandTrace(this->Functions[c]);
|
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;
|
2009-06-12 10:07:05 -04:00
|
|
|
mf.ExpandArguments(this->Functions[c].Arguments,
|
2008-01-23 10:28:26 -05:00
|
|
|
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(
|
|
|
|
this->Functions[c], this->GetStartingContext().FilePath);
|
2015-10-20 19:13:52 +02:00
|
|
|
|
|
|
|
cmConditionEvaluator conditionEvaluator(
|
2016-05-16 10:34:04 -04:00
|
|
|
mf, conditionContext, mf.GetBacktrace(this->Functions[c]));
|
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;
|
2015-05-29 01:14:19 +02:00
|
|
|
cmListFileBacktrace bt = mf.GetBacktrace(this->Functions[c]);
|
|
|
|
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();
|
2016-05-16 10:34:04 -04:00
|
|
|
mf.ExecuteCommand(this->Functions[c], status);
|
|
|
|
if (status.GetReturnInvoked()) {
|
2008-01-23 10:28:26 -05:00
|
|
|
inStatus.SetReturnInvoked(true);
|
|
|
|
return true;
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
|
|
|
if (status.GetBreakInvoked()) {
|
2008-01-23 10:28:26 -05:00
|
|
|
inStatus.SetBreakInvoked(true);
|
|
|
|
return true;
|
2016-05-16 10:34:04 -04:00
|
|
|
}
|
|
|
|
if (status.GetContinueInvoked()) {
|
2014-11-29 17:56:18 +01:00
|
|
|
inStatus.SetContinueInvoked(true);
|
|
|
|
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
|
|
|
{
|
2016-05-16 10:34:04 -04:00
|
|
|
if (!cmSystemTools::Strucmp(lff.Name.c_str(), "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;
|
|
|
|
}
|