CMake/Source/cmIfCommand.cxx

249 lines
7.3 KiB
C++
Raw Normal View History

/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
2001-04-20 01:39:03 +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
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"
#include "cmStringCommand.h"
#include "cmOutputConverter.h"
#include "cmConditionEvaluator.h"
#include <stdlib.h> // required for atof
#include <list>
#include <cmsys/RegularExpression.hxx>
static std::string cmIfCommandError(
std::vector<cmExpandedCommandArgument> const& args)
{
std::string err = "given arguments:\n ";
for(std::vector<cmExpandedCommandArgument>::const_iterator i = args.begin();
i != args.end(); ++i)
{
err += " ";
err += cmOutputConverter::EscapeForCMake(i->GetValue());
}
err += "\n";
return err;
}
//=========================================================================
2001-04-20 01:39:03 +04:00
bool cmIfFunctionBlocker::
IsFunctionBlocked(const cmListFileFunction& lff,
cmMakefile &mf,
cmExecutionStatus &inStatus)
2001-04-20 01:39:03 +04:00
{
// we start by recording all the functions
if (!cmSystemTools::Strucmp(lff.Name.c_str(),"if"))
{
this->ScopeDepth++;
}
else if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
2001-04-20 01:39:03 +04:00
{
this->ScopeDepth--;
// if this is the endif for this if statement, then start executing
if (!this->ScopeDepth)
2006-09-22 19:23:51 +04:00
{
// Remove the function blocker for this scope or bail.
cmsys::auto_ptr<cmFunctionBlocker>
fb(mf.RemoveFunctionBlocker(this, lff));
if(!fb.get()) { return false; }
// 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
{
// 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;
// if trace is enabled, print a (trivially) evaluated "else"
// statement
if(!this->IsBlocking && mf.GetCMakeInstance()->GetTrace())
{
mf.PrintCommandTrace(this->Functions[c]);
}
}
else if (scopeDepth == 0 && !cmSystemTools::Strucmp
(this->Functions[c].Name.c_str(),"elseif"))
2006-09-22 19:23:51 +04:00
{
if (this->HasRun)
2006-09-22 19:23:51 +04:00
{
this->IsBlocking = true;
2006-09-22 19:23:51 +04:00
}
else
{
// if trace is enabled, print the evaluated "elseif" statement
if(mf.GetCMakeInstance()->GetTrace())
{
mf.PrintCommandTrace(this->Functions[c]);
}
std::string errorString;
std::vector<cmExpandedCommandArgument> expandedArguments;
mf.ExpandArguments(this->Functions[c].Arguments,
expandedArguments);
2009-06-12 19:10:26 +04:00
cmake::MessageType messType;
cmListFileContext conditionContext =
cmConditionEvaluator::GetConditionContext(
&mf, this->Functions[c],
this->GetStartingContext().FilePath);
cmConditionEvaluator conditionEvaluator(
mf, conditionContext,
mf.GetBacktrace(this->Functions[c]));
bool isTrue = conditionEvaluator.IsTrue(
expandedArguments, errorString, messType);
if (!errorString.empty())
{
std::string err = cmIfCommandError(expandedArguments);
err += errorString;
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)
{
cmSystemTools::SetFatalErrorOccured();
return true;
}
}
if (isTrue)
{
this->IsBlocking = false;
this->HasRun = true;
}
}
}
// should we execute?
else if (!this->IsBlocking)
2006-09-22 19:23:51 +04: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;
}
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;
}
}
// record the command
this->Functions.push_back(lff);
// always return true
return true;
2001-04-20 01:39:03 +04:00
}
//=========================================================================
bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
2008-03-01 05:33:33 +03:00
cmMakefile&)
2001-04-20 01:39:03 +04: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
if (lff.Arguments.empty() ||
lff.Arguments == this->Args)
{
return true;
}
}
2006-05-18 21:50:01 +04:00
return false;
2001-04-20 01:39:03 +04:00
}
//=========================================================================
2006-05-11 23:50:11 +04:00
bool cmIfCommand
::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
cmExecutionStatus &)
2001-04-20 01:39:03 +04:00
{
std::string errorString;
std::vector<cmExpandedCommandArgument> expandedArguments;
2006-03-15 19:02:08 +03:00
this->Makefile->ExpandArguments(args, expandedArguments);
cmake::MessageType status;
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());
bool isTrue = conditionEvaluator.IsTrue(
expandedArguments, errorString, status);
if (!errorString.empty())
2001-04-20 01:39:03 +04:00
{
std::string err = cmIfCommandError(expandedArguments);
err += errorString;
if (status == cmake::FATAL_ERROR)
{
this->SetError(err);
cmSystemTools::SetFatalErrorOccured();
return false;
}
else
{
this->Makefile->IssueMessage(status, err);
}
2001-04-20 01:39:03 +04:00
}
cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
// if is isn't true block the commands
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);
return true;
}