1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
|
/*=========================================================================
Program: CMake - Cross-Platform Makefile Generator
Module: $RCSfile$
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
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.
=========================================================================*/
#include "cmIfCommand.h"
#include "cmStringCommand.h"
#include <stdlib.h> // required for atof
#include <list>
#include <cmsys/RegularExpression.hxx>
bool cmIfFunctionBlocker::
IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
cmExecutionStatus &inStatus)
{
// Prevent recusion and don't let this blocker block its own
// commands.
if (this->Executing)
{
return false;
}
// we start by recording all the functions
if (!cmSystemTools::Strucmp(lff.Name.c_str(),"if"))
{
this->ScopeDepth++;
}
if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
{
this->ScopeDepth--;
// if this is the endif for this if statement, then start executing
if (!this->ScopeDepth)
{
// execute the functions for the true parts of the if statement
this->Executing = true;
cmExecutionStatus status;
int scopeDepth = 0;
for(unsigned int c = 0; c < this->Functions.size(); ++c)
{
// 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;
}
else if (scopeDepth == 0 && !cmSystemTools::Strucmp
(this->Functions[c].Name.c_str(),"elseif"))
{
if (this->HasRun)
{
this->IsBlocking = true;
}
else
{
char* errorString = 0;
std::vector<std::string> expandedArguments;
mf.ExpandArguments(this->Functions[c].Arguments,
expandedArguments);
bool isTrue =
cmIfCommand::IsTrue(expandedArguments,&errorString,&mf);
if (errorString)
{
std::string err = "had incorrect arguments: ";
unsigned int i;
for(i =0; i < this->Functions[c].Arguments.size(); ++i)
{
err += (this->Functions[c].Arguments[i].Quoted?"\"":"");
err += this->Functions[c].Arguments[i].Value;
err += (this->Functions[c].Arguments[i].Quoted?"\"":"");
err += " ";
}
err += "(";
err += errorString;
err += ").";
cmSystemTools::Error(err.c_str());
delete [] errorString;
return false;
}
if (isTrue)
{
this->IsBlocking = false;
this->HasRun = true;
}
}
}
// should we execute?
else if (!this->IsBlocking)
{
status.Clear();
mf.ExecuteCommand(this->Functions[c],status);
if (status.GetReturnInvoked())
{
inStatus.SetReturnInvoked(true);
mf.RemoveFunctionBlocker(lff);
return true;
}
if (status.GetBreakInvoked())
{
inStatus.SetBreakInvoked(true);
mf.RemoveFunctionBlocker(lff);
return true;
}
}
}
mf.RemoveFunctionBlocker(lff);
return true;
}
}
// record the command
this->Functions.push_back(lff);
// always return true
return true;
}
bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
cmMakefile& mf)
{
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.size() == 0 ||
lff.Arguments == this->Args)
{
return true;
}
}
return false;
}
void cmIfFunctionBlocker::
ScopeEnded(cmMakefile &mf)
{
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<cmListFileArgument>::const_iterator j = this->Args.begin();
j != this->Args.end(); ++j)
{
errmsg += (j->Quoted?"\"":"");
errmsg += j->Value;
errmsg += (j->Quoted?"\"":"");
errmsg += " ";
}
cmSystemTools::Message(errmsg.c_str(), "Warning");
}
bool cmIfCommand
::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
cmExecutionStatus &)
{
char* errorString = 0;
std::vector<std::string> expandedArguments;
this->Makefile->ExpandArguments(args, expandedArguments);
bool isTrue =
cmIfCommand::IsTrue(expandedArguments,&errorString,this->Makefile);
if (errorString)
{
std::string err = "had incorrect arguments: ";
unsigned int i;
for(i =0; i < args.size(); ++i)
{
err += (args[i].Quoted?"\"":"");
err += args[i].Value;
err += (args[i].Quoted?"\"":"");
err += " ";
}
err += "(";
err += errorString;
err += ").";
this->SetError(err.c_str());
delete [] errorString;
return false;
}
cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
// if is isn't true block the commands
f->ScopeDepth = 1;
f->IsBlocking = !isTrue;
if (isTrue)
{
f->HasRun = true;
}
f->Args = args;
this->Makefile->AddFunctionBlocker(f);
return true;
}
namespace
{
void IncrementArguments(std::list<std::string> &newArgs,
std::list<std::string>::iterator &argP1,
std::list<std::string>::iterator &argP2)
{
if (argP1 != newArgs.end())
{
argP1++;
argP2 = argP1;
if (argP1 != newArgs.end())
{
argP2++;
}
}
}
}
// order of operations,
// IS_DIRECTORY EXISTS COMMAND DEFINED
// MATCHES LESS GREATER EQUAL STRLESS STRGREATER STREQUAL
// AND OR
//
// There is an issue on whether the arguments should be values of references,
// for example IF (FOO AND BAR) should that compare the strings FOO and BAR
// or should it really do IF (${FOO} AND ${BAR}) Currently IS_DIRECTORY
// EXISTS COMMAND and DEFINED all take values. EQUAL, LESS and GREATER can
// take numeric values or variable names. STRLESS and STRGREATER take
// variable names but if the variable name is not found it will use the name
// directly. AND OR take variables or the values 0 or 1.
bool cmIfCommand::IsTrue(const std::vector<std::string> &args,
char **errorString, cmMakefile *makefile)
{
// check for the different signatures
const char *def;
const char *def2;
const char* msg = "Unknown arguments specified";
*errorString = new char[strlen(msg) + 1];
strcpy(*errorString, msg);
// handle empty invocation
if (args.size() < 1)
{
delete [] *errorString;
*errorString = 0;
return false;
}
// store the reduced args in this vector
std::list<std::string> newArgs;
int reducible;
unsigned int i;
// copy to the list structure
for(i = 0; i < args.size(); ++i)
{
newArgs.push_back(args[i]);
}
std::list<std::string>::iterator argP1;
std::list<std::string>::iterator argP2;
// now loop through the arguments and see if we can reduce any of them
// we do this multiple times. Once for each level of precedence
do
{
reducible = 0;
std::list<std::string>::iterator arg = newArgs.begin();
while (arg != newArgs.end())
{
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
// does a file exist
if (*arg == "EXISTS" && argP1 != newArgs.end())
{
if(cmSystemTools::FileExists((argP1)->c_str()))
{
*arg = "1";
}
else
{
*arg = "0";
}
newArgs.erase(argP1);
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
reducible = 1;
}
// does a directory with this name exist
if (*arg == "IS_DIRECTORY" && argP1 != newArgs.end())
{
if(cmSystemTools::FileIsDirectory((argP1)->c_str()))
{
*arg = "1";
}
else
{
*arg = "0";
}
newArgs.erase(argP1);
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
reducible = 1;
}
// is the given path an absolute path ?
if (*arg == "IS_ABSOLUTE" && argP1 != newArgs.end())
{
if(cmSystemTools::FileIsFullPath((argP1)->c_str()))
{
*arg = "1";
}
else
{
*arg = "0";
}
newArgs.erase(argP1);
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
reducible = 1;
}
// does a command exist
if (*arg == "COMMAND" && argP1 != newArgs.end())
{
if(makefile->CommandExists((argP1)->c_str()))
{
*arg = "1";
}
else
{
*arg = "0";
}
newArgs.erase(argP1);
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
reducible = 1;
}
// is a variable defined
if (*arg == "DEFINED" && argP1 != newArgs.end())
{
size_t argP1len = argP1->size();
bool bdef = false;
if(argP1len > 4 && argP1->substr(0, 4) == "ENV{" &&
argP1->operator[](argP1len-1) == '}')
{
std::string env = argP1->substr(4, argP1len-5);
bdef = cmSystemTools::GetEnv(env.c_str())?true:false;
}
else
{
bdef = makefile->IsDefinitionSet((argP1)->c_str());
}
if(bdef)
{
*arg = "1";
}
else
{
*arg = "0";
}
newArgs.erase(argP1);
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
reducible = 1;
}
++arg;
}
}
while (reducible);
// now loop through the arguments and see if we can reduce any of them
// we do this multiple times. Once for each level of precedence
do
{
reducible = 0;
std::list<std::string>::iterator arg = newArgs.begin();
while (arg != newArgs.end())
{
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
*(argP1) == "MATCHES")
{
def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
const char* rex = (argP2)->c_str();
cmStringCommand::ClearMatches(makefile);
cmsys::RegularExpression regEntry;
if ( !regEntry.compile(rex) )
{
cmOStringStream error;
error << "Regular expression \"" << rex << "\" cannot compile";
delete [] *errorString;
*errorString = new char[error.str().size() + 1];
strcpy(*errorString, error.str().c_str());
return false;
}
if (regEntry.find(def))
{
cmStringCommand::StoreMatches(makefile, regEntry);
*arg = "1";
}
else
{
*arg = "0";
}
newArgs.erase(argP2);
newArgs.erase(argP1);
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
reducible = 1;
}
if (argP1 != newArgs.end() && *arg == "MATCHES")
{
*arg = "0";
newArgs.erase(argP1);
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
reducible = 1;
}
if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
(*(argP1) == "LESS" || *(argP1) == "GREATER" ||
*(argP1) == "EQUAL"))
{
def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(), makefile);
double lhs;
double rhs;
if(sscanf(def, "%lg", &lhs) != 1 ||
sscanf(def2, "%lg", &rhs) != 1)
{
*arg = "0";
}
else if (*(argP1) == "LESS")
{
if(lhs < rhs)
{
*arg = "1";
}
else
{
*arg = "0";
}
}
else if (*(argP1) == "GREATER")
{
if(lhs > rhs)
{
*arg = "1";
}
else
{
*arg = "0";
}
}
else
{
if(lhs == rhs)
{
*arg = "1";
}
else
{
*arg = "0";
}
}
newArgs.erase(argP2);
newArgs.erase(argP1);
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
reducible = 1;
}
if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
(*(argP1) == "STRLESS" ||
*(argP1) == "STREQUAL" ||
*(argP1) == "STRGREATER"))
{
def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(), makefile);
int val = strcmp(def,def2);
int result;
if (*(argP1) == "STRLESS")
{
result = (val < 0);
}
else if (*(argP1) == "STRGREATER")
{
result = (val > 0);
}
else // strequal
{
result = (val == 0);
}
if(result)
{
*arg = "1";
}
else
{
*arg = "0";
}
newArgs.erase(argP2);
newArgs.erase(argP1);
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
reducible = 1;
}
// is file A newer than file B
if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
*(argP1) == "IS_NEWER_THAN")
{
int fileIsNewer=0;
bool success=cmSystemTools::FileTimeCompare(arg->c_str(),
(argP2)->c_str(),
&fileIsNewer);
if(success==false || fileIsNewer==1 || fileIsNewer==0)
{
*arg = "1";
}
else
{
*arg = "0";
}
newArgs.erase(argP2);
newArgs.erase(argP1);
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
reducible = 1;
}
++arg;
}
}
while (reducible);
// now loop through the arguments and see if we can reduce any of them
// we do this multiple times. Once for each level of precedence
do
{
reducible = 0;
std::list<std::string>::iterator arg = newArgs.begin();
while (arg != newArgs.end())
{
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
if (argP1 != newArgs.end() && *arg == "NOT")
{
def = cmIfCommand::GetVariableOrNumber((argP1)->c_str(), makefile);
if(!cmSystemTools::IsOff(def))
{
*arg = "0";
}
else
{
*arg = "1";
}
newArgs.erase(argP1);
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
reducible = 1;
}
++arg;
}
}
while (reducible);
// now loop through the arguments and see if we can reduce any of them
// we do this multiple times. Once for each level of precedence
do
{
reducible = 0;
std::list<std::string>::iterator arg = newArgs.begin();
while (arg != newArgs.end())
{
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
if (argP1 != newArgs.end() && *(argP1) == "AND" &&
argP2 != newArgs.end())
{
def = cmIfCommand::GetVariableOrNumber(arg->c_str(), makefile);
def2 = cmIfCommand::GetVariableOrNumber((argP2)->c_str(), makefile);
if(cmSystemTools::IsOff(def) || cmSystemTools::IsOff(def2))
{
*arg = "0";
}
else
{
*arg = "1";
}
newArgs.erase(argP2);
newArgs.erase(argP1);
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
reducible = 1;
}
if (argP1 != newArgs.end() && *(argP1) == "OR" &&
argP2 != newArgs.end())
{
def = cmIfCommand::GetVariableOrNumber(arg->c_str(), makefile);
def2 = cmIfCommand::GetVariableOrNumber((argP2)->c_str(), makefile);
if(cmSystemTools::IsOff(def) && cmSystemTools::IsOff(def2))
{
*arg = "0";
}
else
{
*arg = "1";
}
newArgs.erase(argP2);
newArgs.erase(argP1);
argP1 = arg;
IncrementArguments(newArgs,argP1,argP2);
reducible = 1;
}
++arg;
}
}
while (reducible);
// now at the end there should only be one argument left
if (newArgs.size() == 1)
{
delete [] *errorString;
*errorString = 0;
if (*newArgs.begin() == "0")
{
return false;
}
if (*newArgs.begin() == "1")
{
return true;
}
def = makefile->GetDefinition(args[0].c_str());
if(cmSystemTools::IsOff(def))
{
return false;
}
}
return true;
}
const char* cmIfCommand::GetVariableOrString(const char* str,
const cmMakefile* mf)
{
const char* def = mf->GetDefinition(str);
if(!def)
{
def = str;
}
return def;
}
const char* cmIfCommand::GetVariableOrNumber(const char* str,
const cmMakefile* mf)
{
const char* def = mf->GetDefinition(str);
if(!def)
{
if (atoi(str))
{
def = str;
}
}
return def;
}
|