2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2001-04-20 01:39:03 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2001-04-20 01:39:03 +04:00
|
|
|
|
2009-09-28 19: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-20 01:39:03 +04:00
|
|
|
#include "cmIfCommand.h"
|
2016-04-29 16:40:20 +03:00
|
|
|
|
2015-10-05 21:57:47 +03:00
|
|
|
#include "cmOutputConverter.h"
|
2016-04-29 17:53:13 +03:00
|
|
|
#include "cmStringCommand.h"
|
2007-08-29 19:58:38 +04:00
|
|
|
|
2014-09-11 21:50:51 +04:00
|
|
|
#include "cmConditionEvaluator.h"
|
|
|
|
|
2003-06-23 22:10:12 +04:00
|
|
|
#include <cmsys/RegularExpression.hxx>
|
2016-04-29 17:53:13 +03:00
|
|
|
#include <list>
|
|
|
|
#include <stdlib.h> // required for atof
|
2003-06-23 22:10:12 +04:00
|
|
|
|
2009-10-27 16:01:33 +03:00
|
|
|
static std::string cmIfCommandError(
|
2015-05-05 22:50:39 +03:00
|
|
|
std::vector<cmExpandedCommandArgument> const& args)
|
2009-10-27 16:01:33 +03:00
|
|
|
{
|
|
|
|
std::string err = "given arguments:\n ";
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<cmExpandedCommandArgument>::const_iterator i = args.begin();
|
|
|
|
i != args.end(); ++i) {
|
2009-10-27 16:01:33 +03:00
|
|
|
err += " ";
|
2015-05-16 01:55:21 +03:00
|
|
|
err += cmOutputConverter::EscapeForCMake(i->GetValue());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-10-27 16:01:33 +03:00
|
|
|
err += "\n";
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2008-06-26 21:01:35 +04:00
|
|
|
//=========================================================================
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmIfFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff,
|
|
|
|
cmMakefile& mf,
|
|
|
|
cmExecutionStatus& inStatus)
|
2001-04-20 01:39:03 +04:00
|
|
|
{
|
2008-01-23 18:28:26 +03:00
|
|
|
// we start by recording all the functions
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!cmSystemTools::Strucmp(lff.Name.c_str(), "if")) {
|
2008-01-23 18:28:26 +03:00
|
|
|
this->ScopeDepth++;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (!cmSystemTools::Strucmp(lff.Name.c_str(), "endif")) {
|
2008-01-23 18:28:26 +03:00
|
|
|
this->ScopeDepth--;
|
|
|
|
// if this is the endif for this if statement, then start executing
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->ScopeDepth) {
|
2009-01-20 22:36:18 +03:00
|
|
|
// Remove the function blocker for this scope or bail.
|
2016-06-28 17:17:52 +03:00
|
|
|
CM_AUTO_PTR<cmFunctionBlocker> fb(mf.RemoveFunctionBlocker(this, lff));
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!fb.get()) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-20 22:36:18 +03:00
|
|
|
|
2008-01-23 18:28:26 +03:00
|
|
|
// execute the functions for the true parts of the if statement
|
|
|
|
cmExecutionStatus status;
|
|
|
|
int scopeDepth = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (unsigned int c = 0; c < this->Functions.size(); ++c) {
|
2008-01-23 18:28:26 +03:00
|
|
|
// keep track of scope depth
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(), "if")) {
|
2008-01-23 18:28:26 +03:00
|
|
|
scopeDepth++;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),
|
|
|
|
"endif")) {
|
2008-01-23 18:28:26 +03:00
|
|
|
scopeDepth--;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-01-23 18:28:26 +03:00
|
|
|
// watch for our state change
|
|
|
|
if (scopeDepth == 0 &&
|
2016-05-16 17:34:04 +03:00
|
|
|
!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(), "else")) {
|
2008-01-23 18:28:26 +03:00
|
|
|
this->IsBlocking = this->HasRun;
|
|
|
|
this->HasRun = true;
|
2012-05-15 02:50:30 +04:00
|
|
|
|
|
|
|
// if trace is enabled, print a (trivially) evaluated "else"
|
|
|
|
// statement
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->IsBlocking && mf.GetCMakeInstance()->GetTrace()) {
|
2012-05-15 02:50:30 +04:00
|
|
|
mf.PrintCommandTrace(this->Functions[c]);
|
2008-01-23 18:28:26 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (scopeDepth == 0 &&
|
|
|
|
!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),
|
|
|
|
"elseif")) {
|
|
|
|
if (this->HasRun) {
|
2008-01-23 18:28:26 +03:00
|
|
|
this->IsBlocking = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2012-05-15 02:50:30 +04:00
|
|
|
// if trace is enabled, print the evaluated "elseif" statement
|
2016-05-16 17:34:04 +03:00
|
|
|
if (mf.GetCMakeInstance()->GetTrace()) {
|
2012-05-15 02:50:30 +04:00
|
|
|
mf.PrintCommandTrace(this->Functions[c]);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-05-15 02:50:30 +04:00
|
|
|
|
2008-06-28 19:16:36 +04:00
|
|
|
std::string errorString;
|
2009-06-12 18:07:05 +04:00
|
|
|
|
2014-09-04 22:21:28 +04:00
|
|
|
std::vector<cmExpandedCommandArgument> expandedArguments;
|
2009-06-12 18:07:05 +04:00
|
|
|
mf.ExpandArguments(this->Functions[c].Arguments,
|
2008-01-23 18:28:26 +03:00
|
|
|
expandedArguments);
|
2009-06-12 18:07:05 +04:00
|
|
|
|
2009-06-12 19:10:26 +04:00
|
|
|
cmake::MessageType messType;
|
2014-09-11 21:50:51 +04:00
|
|
|
|
2015-10-20 20:13:52 +03:00
|
|
|
cmListFileContext conditionContext =
|
2016-05-16 17:34:04 +03:00
|
|
|
cmListFileContext::FromCommandContext(
|
|
|
|
this->Functions[c], this->GetStartingContext().FilePath);
|
2015-10-20 20:13:52 +03:00
|
|
|
|
|
|
|
cmConditionEvaluator conditionEvaluator(
|
2016-05-16 17:34:04 +03:00
|
|
|
mf, conditionContext, mf.GetBacktrace(this->Functions[c]));
|
2014-09-11 21:50:51 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool isTrue = conditionEvaluator.IsTrue(expandedArguments,
|
|
|
|
errorString, messType);
|
2009-06-12 18:07:05 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!errorString.empty()) {
|
2015-05-05 22:50:39 +03:00
|
|
|
std::string err = cmIfCommandError(expandedArguments);
|
2008-01-23 18:28:26 +03:00
|
|
|
err += errorString;
|
2015-05-29 02:14:19 +03:00
|
|
|
cmListFileBacktrace bt = mf.GetBacktrace(this->Functions[c]);
|
|
|
|
mf.GetCMakeInstance()->IssueMessage(messType, err, bt);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (messType == cmake::FATAL_ERROR) {
|
2009-06-12 18:07:05 +04:00
|
|
|
cmSystemTools::SetFatalErrorOccured();
|
|
|
|
return true;
|
2008-01-23 18:28:26 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-06-12 18:07:05 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (isTrue) {
|
2008-01-23 18:28:26 +03:00
|
|
|
this->IsBlocking = false;
|
|
|
|
this->HasRun = true;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-06-12 18:07:05 +04:00
|
|
|
|
2008-01-23 18:28:26 +03:00
|
|
|
// should we execute?
|
2016-05-16 17:34:04 +03:00
|
|
|
else if (!this->IsBlocking) {
|
2008-01-23 18:28:26 +03:00
|
|
|
status.Clear();
|
2016-05-16 17:34:04 +03:00
|
|
|
mf.ExecuteCommand(this->Functions[c], status);
|
|
|
|
if (status.GetReturnInvoked()) {
|
2008-01-23 18:28:26 +03:00
|
|
|
inStatus.SetReturnInvoked(true);
|
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (status.GetBreakInvoked()) {
|
2008-01-23 18:28:26 +03:00
|
|
|
inStatus.SetBreakInvoked(true);
|
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (status.GetContinueInvoked()) {
|
2014-11-29 19:56:18 +03:00
|
|
|
inStatus.SetContinueInvoked(true);
|
|
|
|
return true;
|
2006-09-22 19:23:51 +04:00
|
|
|
}
|
2002-08-09 20:00:49 +04:00
|
|
|
}
|
2006-09-22 19:23:51 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return true;
|
2006-09-22 19:23:51 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-06-12 18:07:05 +04:00
|
|
|
|
2008-01-23 18:28:26 +03:00
|
|
|
// record the command
|
|
|
|
this->Functions.push_back(lff);
|
2009-06-12 18:07:05 +04:00
|
|
|
|
2008-01-23 18:28:26 +03:00
|
|
|
// always return true
|
|
|
|
return true;
|
2001-04-20 01:39:03 +04:00
|
|
|
}
|
|
|
|
|
2008-06-26 21:01:35 +04:00
|
|
|
//=========================================================================
|
2002-12-12 02:13:33 +03:00
|
|
|
bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
|
2008-03-01 05:33:33 +03:00
|
|
|
cmMakefile&)
|
2001-04-20 01:39:03 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!cmSystemTools::Strucmp(lff.Name.c_str(), "endif")) {
|
2008-02-29 20:18:11 +03:00
|
|
|
// if the endif has arguments, then make sure
|
|
|
|
// they match the arguments of the matching if
|
2016-05-16 17:34:04 +03:00
|
|
|
if (lff.Arguments.empty() || lff.Arguments == this->Args) {
|
2002-07-10 19:38:38 +04:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2006-05-18 21:50:01 +04:00
|
|
|
|
2002-07-10 19:38:38 +04:00
|
|
|
return false;
|
2001-04-20 01:39:03 +04:00
|
|
|
}
|
|
|
|
|
2008-06-26 21:01:35 +04:00
|
|
|
//=========================================================================
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmIfCommand::InvokeInitialPass(
|
|
|
|
const std::vector<cmListFileArgument>& args, cmExecutionStatus&)
|
2001-04-20 01:39:03 +04:00
|
|
|
{
|
2008-06-28 19:16:36 +04:00
|
|
|
std::string errorString;
|
2009-06-12 18:07:05 +04:00
|
|
|
|
2014-09-04 22:21:28 +04:00
|
|
|
std::vector<cmExpandedCommandArgument> expandedArguments;
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Makefile->ExpandArguments(args, expandedArguments);
|
2009-06-12 18:07:05 +04:00
|
|
|
|
|
|
|
cmake::MessageType status;
|
2014-09-11 21:50:51 +04:00
|
|
|
|
2015-10-20 20:13:52 +03:00
|
|
|
cmConditionEvaluator conditionEvaluator(
|
2016-05-16 17:34:04 +03:00
|
|
|
*(this->Makefile), this->Makefile->GetExecutionContext(),
|
|
|
|
this->Makefile->GetBacktrace());
|
2014-09-11 21:50:51 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool isTrue =
|
|
|
|
conditionEvaluator.IsTrue(expandedArguments, errorString, status);
|
2009-06-12 18:07:05 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!errorString.empty()) {
|
2016-01-29 00:10:29 +03:00
|
|
|
std::string err = "if " + cmIfCommandError(expandedArguments);
|
2004-08-03 16:13:54 +04:00
|
|
|
err += errorString;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (status == cmake::FATAL_ERROR) {
|
2016-01-29 00:10:29 +03:00
|
|
|
this->Makefile->IssueMessage(cmake::FATAL_ERROR, err);
|
2009-06-12 18:07:05 +04:00
|
|
|
cmSystemTools::SetFatalErrorOccured();
|
2016-01-29 00:10:29 +03:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2009-06-12 18:07:05 +04:00
|
|
|
this->Makefile->IssueMessage(status, err);
|
2001-04-20 01:39:03 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-06-12 18:07:05 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmIfFunctionBlocker* f = new cmIfFunctionBlocker();
|
2002-07-10 19:38:38 +04:00
|
|
|
// if is isn't true block the commands
|
2008-01-23 18:28:26 +03:00
|
|
|
f->ScopeDepth = 1;
|
2006-03-15 19:02:08 +03:00
|
|
|
f->IsBlocking = !isTrue;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (isTrue) {
|
2006-09-22 19:23:51 +04:00
|
|
|
f->HasRun = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2006-03-15 19:02:08 +03:00
|
|
|
f->Args = args;
|
|
|
|
this->Makefile->AddFunctionBlocker(f);
|
2009-06-12 18:07:05 +04:00
|
|
|
|
2002-07-01 16:49:36 +04:00
|
|
|
return true;
|
|
|
|
}
|