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