cmIfCommand.cxx 7.26 KB
Newer Older
1 2 3
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4

5 6
  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.
7

8 9 10 11
  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.
============================================================================*/
12
#include "cmIfCommand.h"
Alexander Neundorf's avatar
 
Alexander Neundorf committed
13 14
#include "cmStringCommand.h"

15 16
#include "cmConditionEvaluator.h"

17
#include <stdlib.h> // required for atof
18
#include <list>
19 20
#include <cmsys/RegularExpression.hxx>

21 22

static std::string cmIfCommandError(
23
  std::vector<cmExpandedCommandArgument> const& args)
24 25
{
  std::string err = "given arguments:\n ";
26
  for(std::vector<cmExpandedCommandArgument>::const_iterator i = args.begin();
27 28 29
      i != args.end(); ++i)
    {
    err += " ";
30
    err += cmOutputConverter::EscapeForCMake(i->GetValue());
31 32 33 34 35
    }
  err += "\n";
  return err;
}

36
//=========================================================================
37
bool cmIfFunctionBlocker::
38
IsFunctionBlocked(const cmListFileFunction& lff,
39
                  cmMakefile &mf,
40
                  cmExecutionStatus &inStatus)
41
{
42 43
  // we start by recording all the functions
  if (!cmSystemTools::Strucmp(lff.Name.c_str(),"if"))
44
    {
45
    this->ScopeDepth++;
46
    }
47
  else if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
48
    {
49 50
    this->ScopeDepth--;
    // if this is the endif for this if statement, then start executing
51
    if (!this->ScopeDepth)
Ken Martin's avatar
Ken Martin committed
52
      {
53
      // Remove the function blocker for this scope or bail.
54 55
      cmsys::auto_ptr<cmFunctionBlocker>
        fb(mf.RemoveFunctionBlocker(this, lff));
56 57
      if(!fb.get()) { return false; }

58 59 60 61
      // 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)
Ken Martin's avatar
Ken Martin committed
62
        {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        // 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;
78 79 80 81 82 83 84

          // if trace is enabled, print a (trivially) evaluated "else"
          // statement
          if(!this->IsBlocking && mf.GetCMakeInstance()->GetTrace())
            {
            mf.PrintCommandTrace(this->Functions[c]);
            }
85 86 87
          }
        else if (scopeDepth == 0 && !cmSystemTools::Strucmp
                 (this->Functions[c].Name.c_str(),"elseif"))
Ken Martin's avatar
Ken Martin committed
88
          {
89
          if (this->HasRun)
Ken Martin's avatar
Ken Martin committed
90
            {
91
            this->IsBlocking = true;
Ken Martin's avatar
Ken Martin committed
92
            }
93 94
          else
            {
95 96 97 98 99 100
            // if trace is enabled, print the evaluated "elseif" statement
            if(mf.GetCMakeInstance()->GetTrace())
              {
              mf.PrintCommandTrace(this->Functions[c]);
              }

101
            std::string errorString;
102

103
            std::vector<cmExpandedCommandArgument> expandedArguments;
104
            mf.ExpandArguments(this->Functions[c].Arguments,
105
                               expandedArguments);
106

Ken Martin's avatar
Ken Martin committed
107
            cmake::MessageType messType;
108

109 110 111 112 113 114 115 116
            cmListFileContext conditionContext =
                cmConditionEvaluator::GetConditionContext(
                  &mf, this->Functions[c],
                  this->GetStartingContext().FilePath);

            cmConditionEvaluator conditionEvaluator(
                  mf, conditionContext,
                  mf.GetBacktrace(this->Functions[c]));
117 118 119

            bool isTrue = conditionEvaluator.IsTrue(
              expandedArguments, errorString, messType);
120

121
            if (!errorString.empty())
122
              {
123
              std::string err = cmIfCommandError(expandedArguments);
124
              err += errorString;
125 126
              cmListFileBacktrace bt = mf.GetBacktrace(this->Functions[c]);
              mf.GetCMakeInstance()->IssueMessage(messType, err, bt);
Ken Martin's avatar
Ken Martin committed
127
              if (messType == cmake::FATAL_ERROR)
128 129 130 131
                {
                cmSystemTools::SetFatalErrorOccured();
                return true;
                }
132
              }
133

134 135 136 137 138 139 140
            if (isTrue)
              {
              this->IsBlocking = false;
              this->HasRun = true;
              }
            }
          }
141

142 143
        // should we execute?
        else if (!this->IsBlocking)
Ken Martin's avatar
Ken Martin committed
144
          {
145 146 147 148 149 150 151 152 153 154 155 156
          status.Clear();
          mf.ExecuteCommand(this->Functions[c],status);
          if (status.GetReturnInvoked())
            {
            inStatus.SetReturnInvoked(true);
            return true;
            }
          if (status.GetBreakInvoked())
            {
            inStatus.SetBreakInvoked(true);
            return true;
            }
157 158 159 160 161
          if (status.GetContinueInvoked())
            {
            inStatus.SetContinueInvoked(true);
            return true;
            }
Ken Martin's avatar
Ken Martin committed
162
          }
Ken Martin's avatar
Ken Martin committed
163
        }
Ken Martin's avatar
Ken Martin committed
164 165 166
      return true;
      }
    }
167

168 169
  // record the command
  this->Functions.push_back(lff);
170

171 172
  // always return true
  return true;
173 174
}

175
//=========================================================================
176
bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
Bill Hoffman's avatar
Bill Hoffman committed
177
                                       cmMakefile&)
178
{
179
  if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
180
    {
181 182
    // if the endif has arguments, then make sure
    // they match the arguments of the matching if
183
    if (lff.Arguments.empty() ||
184
        lff.Arguments == this->Args)
185 186 187 188
      {
      return true;
      }
    }
Ken Martin's avatar
Ken Martin committed
189

190
  return false;
191 192
}

193
//=========================================================================
Ken Martin's avatar
Ken Martin committed
194
bool cmIfCommand
195
::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
196
                    cmExecutionStatus &)
197
{
198
  std::string errorString;
199

200
  std::vector<cmExpandedCommandArgument> expandedArguments;
Ken Martin's avatar
Ken Martin committed
201
  this->Makefile->ExpandArguments(args, expandedArguments);
202 203

  cmake::MessageType status;
204

205 206 207 208 209 210 211 212 213 214
  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());
215 216 217

  bool isTrue = conditionEvaluator.IsTrue(
    expandedArguments, errorString, status);
218

219
  if (!errorString.empty())
220
    {
221
    std::string err = cmIfCommandError(expandedArguments);
222
    err += errorString;
223 224
    if (status == cmake::FATAL_ERROR)
      {
Stephen Kelly's avatar
Stephen Kelly committed
225
      this->SetError(err);
226 227 228 229 230 231 232
      cmSystemTools::SetFatalErrorOccured();
      return false;
      }
    else
      {
      this->Makefile->IssueMessage(status, err);
      }
233
    }
234

235 236
  cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
  // if is isn't true block the commands
237
  f->ScopeDepth = 1;
Ken Martin's avatar
Ken Martin committed
238
  f->IsBlocking = !isTrue;
Ken Martin's avatar
Ken Martin committed
239 240 241 242
  if (isTrue)
    {
    f->HasRun = true;
    }
Ken Martin's avatar
Ken Martin committed
243 244
  f->Args = args;
  this->Makefile->AddFunctionBlocker(f);
245

246 247
  return true;
}