cmIfCommand.cxx 6.67 KB
Newer Older
1
2
3
4
5
6
7
8
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

Will Schroeder's avatar
Will Schroeder committed
9
10
  Copyright (c) 2002 Insight Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11

Will Schroeder's avatar
Will Schroeder committed
12
13
14
     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.
15
16
17
18
19
20

=========================================================================*/
#include "cmIfCommand.h"

bool cmIfFunctionBlocker::
IsFunctionBlocked(const char *name, const std::vector<std::string> &args, 
21
                  cmMakefile &mf)
22
{
23
24
25
26
27
28
29
  // always let if statements through
  if (!strcmp(name,"IF"))
    {
    return false;
    }
  
  // watch for our ELSE or ENDIF
Ken Martin's avatar
Ken Martin committed
30
  if (!strcmp(name,"ELSE") || !strcmp(name,"ENDIF"))
31
    {
Ken Martin's avatar
Ken Martin committed
32
    if (args == m_Args)
Ken Martin's avatar
Ken Martin committed
33
      {
Ken Martin's avatar
Ken Martin committed
34
35
36
37
38
39
40
      // if it was an else statement then we should change state
      // and block this Else Command
      if (!strcmp(name,"ELSE"))
        {
        m_IsBlocking = !m_IsBlocking;
        return true;
        }
41
42
43
44
      // otherwise it must be an ENDIF statement, in that case remove the
      // function blocker
      mf.RemoveFunctionBlocker("ENDIF",args);
      return true;
Ken Martin's avatar
Ken Martin committed
45
      }
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    else if(args.empty())
      {
      std::string err = "Empty arguments for ";
      err += name;
      err += ".  Did you mean ";
      err += name;
      err += "( ";
      for(std::vector<std::string>::const_iterator a = m_Args.begin();
          a != m_Args.end();++a)
        {
        err += *a;
        err += " ";
        }
      err += ")?";
      cmSystemTools::Error(err.c_str());
      }
62
    }
63
  return m_IsBlocking;
64
65
66
67
}

bool cmIfFunctionBlocker::
ShouldRemove(const char *name, const std::vector<std::string> &args, 
Ken Martin's avatar
Ken Martin committed
68
             cmMakefile &) 
69
{
70
71
72
73
74
75
76
77
  if (!strcmp(name,"ELSE") || !strcmp(name,"ENDIF"))
    {
    if (args == m_Args)
      {
      return true;
      }
    }
  return false;
78
79
}

Ken Martin's avatar
Ken Martin committed
80
void cmIfFunctionBlocker::
Ken Martin's avatar
Ken Martin committed
81
ScopeEnded(cmMakefile &mf)
Ken Martin's avatar
Ken Martin committed
82
{
Brad King's avatar
Brad King committed
83
84
85
86
87
88
89
  const char* versionValue
    = mf.GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  if (!versionValue || (atof(versionValue) <= 1.4))
    {
    return;
    }
  
Ken Martin's avatar
Ken Martin committed
90
91
92
93
94
95
96
97
98
99
  std::string errmsg = "The end of a CMakeLists file was reached with an IF statement that was not closed properly.\nWithin the directory: ";
  errmsg += mf.GetCurrentDirectory();
  errmsg += "\nThe arguments are: ";
  for(std::vector<std::string>::const_iterator j = m_Args.begin();
      j != m_Args.end(); ++j)
    {   
    errmsg += *j;
    errmsg += " ";
    }
  cmSystemTools::Error(errmsg.c_str());
Ken Martin's avatar
Ken Martin committed
100
101
}

102
bool cmIfCommand::InitialPass(std::vector<std::string> const& args)
103
{
104
105
106
107
  bool isValid;
  bool isTrue = cmIfCommand::IsTrue(args,isValid,m_Makefile);
  
  if (!isValid)
108
    {
109
    this->SetError("An IF command had incorrect arguments");
110
111
    return false;
    }
112
  
113
114
115
116
117
118
119
  cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
  // if is isn't true block the commands
  f->m_IsBlocking = !isTrue;
  for(std::vector<std::string>::const_iterator j = args.begin();
      j != args.end(); ++j)
    {   
    f->m_Args.push_back(*j);
120
    }
121
  m_Makefile->AddFunctionBlocker(f);
122
123
124
  
  return true;
}
Ken Martin's avatar
Ken Martin committed
125

126
127
128
bool cmIfCommand::IsTrue(const std::vector<std::string> &args, bool &isValid,
                         const cmMakefile *makefile)
{
Ken Martin's avatar
Ken Martin committed
129
  // check for the different signatures
130
131
  bool isTrue = true;
  isValid = false;
Ken Martin's avatar
Ken Martin committed
132
  const char *def;
Ken Martin's avatar
Ken Martin committed
133
134
  const char *def2;

135
136
  if(args.size() < 1 )
    {
137
    isValid = true;
138
139
140
    return false;
    }

Ken Martin's avatar
Ken Martin committed
141
142
  if (args.size() == 1)
    {
143
    def = makefile->GetDefinition(args[0].c_str());
Ken Martin's avatar
Ken Martin committed
144
145
    if(cmSystemTools::IsOff(def))
      {
146
      isTrue = false;
Ken Martin's avatar
Ken Martin committed
147
      }
148
    isValid = true;
Ken Martin's avatar
Ken Martin committed
149
150
    }

Ken Martin's avatar
Ken Martin committed
151
  if (args.size() == 2 && (args[0] == "NOT"))
152
    {
153
    def = makefile->GetDefinition(args[1].c_str());
Ken Martin's avatar
Ken Martin committed
154
155
    if(!cmSystemTools::IsOff(def))
      {
156
      isTrue = false;
Ken Martin's avatar
Ken Martin committed
157
      }
158
159
    isValid = true;

Ken Martin's avatar
Ken Martin committed
160
161
    }

162
163
  if (args.size() == 2 && (args[0] == "COMMAND"))
    {
164
    if(!makefile->CommandExists(args[1].c_str()))
165
      {
166
      isTrue = false;
167
      }
168
    isValid = true;
169
170
    }

171
172
  if (args.size() == 2 && (args[0] == "EXISTS"))
    {
173
    if(!cmSystemTools::FileExists(args[1].c_str()))
174
      {
175
      isTrue = false;
176
      }
177
    isValid = true;
178
179
    }

Ken Martin's avatar
Ken Martin committed
180
181
  if (args.size() == 3 && (args[1] == "AND"))
    {
182
183
    def = makefile->GetDefinition(args[0].c_str());
    def2 = makefile->GetDefinition(args[2].c_str());
Berk Geveci's avatar
Berk Geveci committed
184
    if(cmSystemTools::IsOff(def) || cmSystemTools::IsOff(def2))
Ken Martin's avatar
Ken Martin committed
185
      {
186
      isTrue = false;
Ken Martin's avatar
Ken Martin committed
187
      }
188
    isValid = true;
189
    }
Ken Martin's avatar
Ken Martin committed
190
191
  
  if (args.size() == 3 && (args[1] == "OR"))
192
    {
193
194
    def = makefile->GetDefinition(args[0].c_str());
    def2 = makefile->GetDefinition(args[2].c_str());
Berk Geveci's avatar
Berk Geveci committed
195
    if(cmSystemTools::IsOff(def) && cmSystemTools::IsOff(def2))
Ken Martin's avatar
Ken Martin committed
196
      {
197
      isTrue = false;
Ken Martin's avatar
Ken Martin committed
198
      }
199
    isValid = true;
Ken Martin's avatar
Ken Martin committed
200
201
202
203
    }

  if (args.size() == 3 && (args[1] == "MATCHES"))
    {
204
    def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
Ken Martin's avatar
Ken Martin committed
205
206
207
    cmRegularExpression regEntry(args[2].c_str());
    
    // check for black line or comment
208
    if (!regEntry.find(def))
Ken Martin's avatar
Ken Martin committed
209
      {
210
      isTrue = false;
Ken Martin's avatar
Ken Martin committed
211
      }
212
    isValid = true;
Ken Martin's avatar
Ken Martin committed
213
214
    }
  
Ken Martin's avatar
Ken Martin committed
215
216
  if (args.size() == 3 && (args[1] == "LESS"))
    {
217
218
    def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
    def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
Ken Martin's avatar
Ken Martin committed
219
220
221
222
223
224
225
226
227
228
    if (!def)
      {
      def = args[0].c_str();
      }
    if (!def2)
      {
      def2 = args[2].c_str();
      }    
    if(atof(def) >= atof(def2))
      {
229
      isTrue = false;
Ken Martin's avatar
Ken Martin committed
230
      }
231
    isValid = true;
Ken Martin's avatar
Ken Martin committed
232
233
234
235
    }

  if (args.size() == 3 && (args[1] == "GREATER"))
    {
236
237
    def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
    def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
Ken Martin's avatar
Ken Martin committed
238
239
    if(atof(def) <= atof(def2))
      {
240
      isTrue = false;
Ken Martin's avatar
Ken Martin committed
241
      }
242
    isValid = true;
Ken Martin's avatar
Ken Martin committed
243
244
245
246
    }

  if (args.size() == 3 && (args[1] == "STRLESS"))
    {
247
248
    def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
    def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
Ken Martin's avatar
Ken Martin committed
249
250
    if(strcmp(def,def2) >= 0)
      {
251
      isTrue = false;
Ken Martin's avatar
Ken Martin committed
252
      }
253
    isValid = true;
Ken Martin's avatar
Ken Martin committed
254
255
256
257
    }

  if (args.size() == 3 && (args[1] == "STRGREATER"))
    {
258
259
    def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
    def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
Ken Martin's avatar
Ken Martin committed
260
261
    if(strcmp(def,def2) <= 0)
      {
262
      isTrue = false;
Ken Martin's avatar
Ken Martin committed
263
      }
264
    isValid = true;
Ken Martin's avatar
Ken Martin committed
265
266
    }

267
  return isTrue;
268
}
269
270
271
272
273
274
275
276
277
278
279

const char* cmIfCommand::GetVariableOrString(const char* str,
                                             const cmMakefile* mf)
{
  const char* def = mf->GetDefinition(str);
  if(!def)
    {
    def = str;
    }
  return def;
}