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"
|
2007-08-29 11:58:38 -04:00
|
|
|
#include "cmStringCommand.h"
|
|
|
|
|
2014-09-11 19:50:51 +02:00
|
|
|
#include "cmConditionEvaluator.h"
|
|
|
|
|
2003-05-24 10:07:58 -04:00
|
|
|
#include <stdlib.h> // required for atof
|
2004-06-11 15:07:17 -04:00
|
|
|
#include <list>
|
2003-06-23 14:10:12 -04:00
|
|
|
#include <cmsys/RegularExpression.hxx>
|
|
|
|
|
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 ";
|
2014-09-04 20:21:28 +02:00
|
|
|
for(std::vector<cmExpandedCommandArgument>::const_iterator i = args.begin();
|
2009-10-27 09:01:33 -04:00
|
|
|
i != args.end(); ++i)
|
|
|
|
{
|
|
|
|
err += " ";
|
2015-05-16 00:55:21 +02:00
|
|
|
err += cmOutputConverter::EscapeForCMake(i->GetValue());
|
2009-10-27 09:01:33 -04:00
|
|
|
}
|
|
|
|
err += "\n";
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2008-06-26 13:01:35 -04:00
|
|
|
//=========================================================================
|
2001-04-19 17:39:03 -04:00
|
|
|
bool cmIfFunctionBlocker::
|
2009-06-12 10:07:05 -04:00
|
|
|
IsFunctionBlocked(const cmListFileFunction& lff,
|
2008-06-26 13:01:35 -04:00
|
|
|
cmMakefile &mf,
|
2008-01-23 10:28:26 -05:00
|
|
|
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
|
|
|
|
if (!cmSystemTools::Strucmp(lff.Name.c_str(),"if"))
|
2006-12-12 10:07:20 -05:00
|
|
|
{
|
2008-01-23 10:28:26 -05:00
|
|
|
this->ScopeDepth++;
|
2006-12-12 10:07:20 -05:00
|
|
|
}
|
2015-02-22 13:48:30 +01:00
|
|
|
else if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
|
2001-04-19 17:39:03 -04:00
|
|
|
{
|
2008-01-23 10:28:26 -05:00
|
|
|
this->ScopeDepth--;
|
|
|
|
// if this is the endif for this if statement, then start executing
|
2009-06-12 10:07:05 -04:00
|
|
|
if (!this->ScopeDepth)
|
2006-09-22 11:23:51 -04:00
|
|
|
{
|
2009-01-20 14:36:18 -05:00
|
|
|
// Remove the function blocker for this scope or bail.
|
2009-01-21 09:49:00 -05:00
|
|
|
cmsys::auto_ptr<cmFunctionBlocker>
|
|
|
|
fb(mf.RemoveFunctionBlocker(this, lff));
|
2009-01-20 14:36:18 -05:00
|
|
|
if(!fb.get()) { return false; }
|
|
|
|
|
2008-01-23 10:28:26 -05:00
|
|
|
// execute the functions for the true parts of the if statement
|
|
|
|
cmExecutionStatus status;
|
|
|
|
int scopeDepth = 0;
|
|
|
|
for(unsigned int c = 0; c < this->Functions.size(); ++c)
|
2002-08-09 12:00:49 -04:00
|
|
|
{
|
2008-01-23 10:28:26 -05:00
|
|
|
// keep track of scope depth
|
|
|
|
if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),"if"))
|
|
|
|
{
|
|
|
|
scopeDepth++;
|
|
|
|
}
|
|
|
|
if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),"endif"))
|
|
|
|
{
|
|
|
|
scopeDepth--;
|
|
|
|
}
|
|
|
|
// watch for our state change
|
|
|
|
if (scopeDepth == 0 &&
|
|
|
|
!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),"else"))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
if(!this->IsBlocking && mf.GetCMakeInstance()->GetTrace())
|
|
|
|
{
|
|
|
|
mf.PrintCommandTrace(this->Functions[c]);
|
|
|
|
}
|
2008-01-23 10:28:26 -05:00
|
|
|
}
|
|
|
|
else if (scopeDepth == 0 && !cmSystemTools::Strucmp
|
|
|
|
(this->Functions[c].Name.c_str(),"elseif"))
|
2006-09-22 11:23:51 -04:00
|
|
|
{
|
2008-01-23 10:28:26 -05:00
|
|
|
if (this->HasRun)
|
2006-09-22 11:23:51 -04:00
|
|
|
{
|
2008-01-23 10:28:26 -05:00
|
|
|
this->IsBlocking = true;
|
2006-09-22 11:23:51 -04:00
|
|
|
}
|
2008-01-23 10:28:26 -05:00
|
|
|
else
|
|
|
|
{
|
2012-05-14 18:50:30 -04:00
|
|
|
// if trace is enabled, print the evaluated "elseif" statement
|
|
|
|
if(mf.GetCMakeInstance()->GetTrace())
|
|
|
|
{
|
|
|
|
mf.PrintCommandTrace(this->Functions[c]);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
cmConditionEvaluator conditionEvaluator(mf);
|
|
|
|
|
|
|
|
bool isTrue = conditionEvaluator.IsTrue(
|
|
|
|
expandedArguments, errorString, messType);
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2015-01-15 23:04:33 +01:00
|
|
|
if (!errorString.empty())
|
2008-01-23 10:28:26 -05:00
|
|
|
{
|
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);
|
2009-06-12 11:10:26 -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
|
|
|
}
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2008-01-23 10:28:26 -05:00
|
|
|
if (isTrue)
|
|
|
|
{
|
|
|
|
this->IsBlocking = false;
|
|
|
|
this->HasRun = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2008-01-23 10:28:26 -05:00
|
|
|
// should we execute?
|
|
|
|
else if (!this->IsBlocking)
|
2006-09-22 11:23:51 -04:00
|
|
|
{
|
2008-01-23 10:28:26 -05:00
|
|
|
status.Clear();
|
|
|
|
mf.ExecuteCommand(this->Functions[c],status);
|
|
|
|
if (status.GetReturnInvoked())
|
|
|
|
{
|
|
|
|
inStatus.SetReturnInvoked(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (status.GetBreakInvoked())
|
|
|
|
{
|
|
|
|
inStatus.SetBreakInvoked(true);
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-29 17:56:18 +01:00
|
|
|
if (status.GetContinueInvoked())
|
|
|
|
{
|
|
|
|
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
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2006-05-31 11:19:39 -04:00
|
|
|
if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
|
2002-07-10 11:38:38 -04:00
|
|
|
{
|
2008-02-29 12:18:11 -05:00
|
|
|
// if the endif has arguments, then make sure
|
|
|
|
// they match the arguments of the matching if
|
2015-01-15 00:31:49 +01:00
|
|
|
if (lff.Arguments.empty() ||
|
2008-02-29 12:18:11 -05:00
|
|
|
lff.Arguments == this->Args)
|
2002-07-10 11:38:38 -04:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
//=========================================================================
|
2006-05-11 15:50:11 -04:00
|
|
|
bool cmIfCommand
|
2009-06-12 10:07:05 -04:00
|
|
|
::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
|
2008-01-23 10:28:26 -05:00
|
|
|
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
|
|
|
|
|
|
|
cmConditionEvaluator conditionEvaluator(*(this->Makefile));
|
|
|
|
|
|
|
|
bool isTrue = conditionEvaluator.IsTrue(
|
|
|
|
expandedArguments, errorString, status);
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2015-01-15 23:04:33 +01:00
|
|
|
if (!errorString.empty())
|
2001-04-19 17:39:03 -04:00
|
|
|
{
|
2015-05-05 21:50:39 +02:00
|
|
|
std::string err = cmIfCommandError(expandedArguments);
|
2004-08-03 08:13:54 -04:00
|
|
|
err += errorString;
|
2009-06-12 10:07:05 -04:00
|
|
|
if (status == cmake::FATAL_ERROR)
|
|
|
|
{
|
2014-03-11 00:04:11 +01:00
|
|
|
this->SetError(err);
|
2009-06-12 10:07:05 -04:00
|
|
|
cmSystemTools::SetFatalErrorOccured();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->Makefile->IssueMessage(status, err);
|
|
|
|
}
|
2001-04-19 17:39:03 -04:00
|
|
|
}
|
2009-06-12 10:07:05 -04:00
|
|
|
|
2002-07-10 11:38:38 -04:00
|
|
|
cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
|
|
|
|
// 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;
|
2006-09-22 11:23:51 -04:00
|
|
|
if (isTrue)
|
|
|
|
{
|
|
|
|
f->HasRun = true;
|
|
|
|
}
|
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;
|
|
|
|
}
|