cmGeneratorExpressionEvaluator.cxx 56.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2012 Stephen Kelly <steveire@gmail.com>

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

  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.
============================================================================*/
#include "cmMakefile.h"

#include "cmGeneratorExpressionEvaluator.h"
#include "cmGeneratorExpressionParser.h"
16
#include "cmGeneratorExpressionDAGChecker.h"
17
#include "cmGeneratorExpression.h"
18

19
20
#include <cmsys/String.h>

21
22
#include <assert.h>

23
//----------------------------------------------------------------------------
24
25
26
27
#if !defined(__SUNPRO_CC) || __SUNPRO_CC > 0x510
static
#endif
void reportError(cmGeneratorExpressionContext *context,
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
                        const std::string &expr, const std::string &result)
{
  context->HadError = true;
  if (context->Quiet)
    {
    return;
    }

  cmOStringStream e;
  e << "Error evaluating generator expression:\n"
    << "  " << expr << "\n"
    << result;
  context->Makefile->GetCMakeInstance()
    ->IssueMessage(cmake::FATAL_ERROR, e.str().c_str(),
                    context->Backtrace);
}

//----------------------------------------------------------------------------
struct cmGeneratorExpressionNode
{
48
49
50
51
52
  enum {
    DynamicParameters = 0,
    OneOrMoreParameters = -1,
    ZeroOrMoreParameters = -2
  };
53
54
55
56
  virtual ~cmGeneratorExpressionNode() {}

  virtual bool GeneratesContent() const { return true; }

57
58
  virtual bool RequiresLiteralInput() const { return false; }

59
  virtual bool AcceptsArbitraryContentParameter() const
60
61
62
63
64
65
    { return false; }

  virtual int NumExpectedParameters() const { return 1; }

  virtual std::string Evaluate(const std::vector<std::string> &parameters,
                               cmGeneratorExpressionContext *context,
66
67
                               const GeneratorExpressionContent *content,
                               cmGeneratorExpressionDAGChecker *dagChecker
68
69
70
71
72
73
74
75
76
77
                              ) const = 0;
};

//----------------------------------------------------------------------------
static const struct ZeroNode : public cmGeneratorExpressionNode
{
  ZeroNode() {}

  virtual bool GeneratesContent() const { return false; }

78
  virtual bool AcceptsArbitraryContentParameter() const { return true; }
79

80
81
  std::string Evaluate(const std::vector<std::string> &,
                       cmGeneratorExpressionContext *,
82
83
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
84
85
86
87
88
89
90
91
92
93
94
  {
    // Unreachable
    return std::string();
  }
} zeroNode;

//----------------------------------------------------------------------------
static const struct OneNode : public cmGeneratorExpressionNode
{
  OneNode() {}

95
  virtual bool AcceptsArbitraryContentParameter() const { return true; }
96
97
98

  std::string Evaluate(const std::vector<std::string> &,
                       cmGeneratorExpressionContext *,
99
100
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
101
102
103
104
105
106
  {
    // Unreachable
    return std::string();
  }
} oneNode;

107
108
109
110
111
112
//----------------------------------------------------------------------------
static const struct OneNode buildInterfaceNode;

//----------------------------------------------------------------------------
static const struct ZeroNode installInterfaceNode;

113
114
115
116
117
//----------------------------------------------------------------------------
#define BOOLEAN_OP_NODE(OPNAME, OP, SUCCESS_VALUE, FAILURE_VALUE) \
static const struct OP ## Node : public cmGeneratorExpressionNode \
{ \
  OP ## Node () {} \
118
  virtual int NumExpectedParameters() const { return OneOrMoreParameters; } \
119
120
121
 \
  std::string Evaluate(const std::vector<std::string> &parameters, \
                       cmGeneratorExpressionContext *context, \
122
123
                       const GeneratorExpressionContent *content, \
                       cmGeneratorExpressionDAGChecker *) const \
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
  { \
    std::vector<std::string>::const_iterator it = parameters.begin(); \
    const std::vector<std::string>::const_iterator end = parameters.end(); \
    for ( ; it != end; ++it) \
      { \
      if (*it == #FAILURE_VALUE) \
        { \
        return #FAILURE_VALUE; \
        } \
      else if (*it != #SUCCESS_VALUE) \
        { \
        reportError(context, content->GetOriginalExpression(), \
        "Parameters to $<" #OP "> must resolve to either '0' or '1'."); \
        return std::string(); \
        } \
      } \
    return #SUCCESS_VALUE; \
  } \
} OPNAME;

BOOLEAN_OP_NODE(andNode, AND, 1, 0)
BOOLEAN_OP_NODE(orNode, OR, 0, 1)

#undef BOOLEAN_OP_NODE

//----------------------------------------------------------------------------
static const struct NotNode : public cmGeneratorExpressionNode
{
  NotNode() {}
153

154
155
  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *context,
156
157
                       const GeneratorExpressionContent *content,
                       cmGeneratorExpressionDAGChecker *) const
158
159
160
161
162
163
164
165
166
167
168
  {
    if (*parameters.begin() != "0" && *parameters.begin() != "1")
      {
      reportError(context, content->GetOriginalExpression(),
            "$<NOT> parameter must resolve to exactly one '0' or '1' value.");
      return std::string();
      }
    return *parameters.begin() == "0" ? "1" : "0";
  }
} notNode;

169
170
171
172
173
174
175
176
177
//----------------------------------------------------------------------------
static const struct BoolNode : public cmGeneratorExpressionNode
{
  BoolNode() {}

  virtual int NumExpectedParameters() const { return 1; }

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *,
178
179
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  {
    return !cmSystemTools::IsOff(parameters.begin()->c_str()) ? "1" : "0";
  }
} boolNode;

//----------------------------------------------------------------------------
static const struct StrEqualNode : public cmGeneratorExpressionNode
{
  StrEqualNode() {}

  virtual int NumExpectedParameters() const { return 2; }

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *,
194
195
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
196
  {
197
    return *parameters.begin() == parameters[1] ? "1" : "0";
198
199
200
201
202
203
204
205
206
207
208
209
  }
} strEqualNode;

//----------------------------------------------------------------------------
static const struct Angle_RNode : public cmGeneratorExpressionNode
{
  Angle_RNode() {}

  virtual int NumExpectedParameters() const { return 0; }

  std::string Evaluate(const std::vector<std::string> &,
                       cmGeneratorExpressionContext *,
210
211
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  {
    return ">";
  }
} angle_rNode;

//----------------------------------------------------------------------------
static const struct CommaNode : public cmGeneratorExpressionNode
{
  CommaNode() {}

  virtual int NumExpectedParameters() const { return 0; }

  std::string Evaluate(const std::vector<std::string> &,
                       cmGeneratorExpressionContext *,
226
227
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
228
229
230
231
232
  {
    return ",";
  }
} commaNode;

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//----------------------------------------------------------------------------
static const struct SemicolonNode : public cmGeneratorExpressionNode
{
  SemicolonNode() {}

  virtual int NumExpectedParameters() const { return 0; }

  std::string Evaluate(const std::vector<std::string> &,
                       cmGeneratorExpressionContext *,
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
  {
    return ";";
  }
} semicolonNode;

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
//----------------------------------------------------------------------------
struct CompilerIdNode : public cmGeneratorExpressionNode
{
  CompilerIdNode() {}

  virtual int NumExpectedParameters() const { return ZeroOrMoreParameters; }

  std::string EvaluateWithLanguage(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *context,
                       const GeneratorExpressionContent *content,
                       cmGeneratorExpressionDAGChecker *,
                       const std::string &lang) const
  {
    const char *compilerId = context->Makefile ?
                              context->Makefile->GetSafeDefinition((
                              "CMAKE_" + lang + "_COMPILER_ID").c_str()) : "";
    if (parameters.size() == 0)
      {
      return compilerId ? compilerId : "";
      }
    cmsys::RegularExpression compilerIdValidator;
    compilerIdValidator.compile("^[A-Za-z0-9_]*$");
    if (!compilerIdValidator.find(parameters.begin()->c_str()))
      {
      reportError(context, content->GetOriginalExpression(),
                  "Expression syntax not recognized.");
      return std::string();
      }
    if (!compilerId)
      {
      return parameters.front().empty() ? "1" : "0";
      }

    if (cmsysString_strcasecmp(parameters.begin()->c_str(), compilerId) == 0)
      {
      return "1";
      }
    return "0";
  }
};

//----------------------------------------------------------------------------
static const struct CCompilerIdNode : public CompilerIdNode
{
  CCompilerIdNode() {}

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *context,
                       const GeneratorExpressionContent *content,
                       cmGeneratorExpressionDAGChecker *dagChecker) const
  {
    if (parameters.size() != 0 && parameters.size() != 1)
      {
      reportError(context, content->GetOriginalExpression(),
          "$<C_COMPILER_ID> expression requires one or two parameters");
      return std::string();
      }
    if (!context->HeadTarget)
      {
      reportError(context, content->GetOriginalExpression(),
          "$<C_COMPILER_ID> may only be used with targets.  It may not "
          "be used with add_custom_command.");
      }
    return this->EvaluateWithLanguage(parameters, context, content,
                                      dagChecker, "C");
  }
} cCompilerIdNode;

//----------------------------------------------------------------------------
static const struct CXXCompilerIdNode : public CompilerIdNode
{
  CXXCompilerIdNode() {}

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *context,
                       const GeneratorExpressionContent *content,
                       cmGeneratorExpressionDAGChecker *dagChecker) const
  {
    if (parameters.size() != 0 && parameters.size() != 1)
      {
      reportError(context, content->GetOriginalExpression(),
          "$<CXX_COMPILER_ID> expression requires one or two parameters");
      return std::string();
      }
    if (!context->HeadTarget)
      {
      reportError(context, content->GetOriginalExpression(),
          "$<CXX_COMPILER_ID> may only be used with targets.  It may not "
          "be used with add_custom_command.");
      }
    return this->EvaluateWithLanguage(parameters, context, content,
                                      dagChecker, "CXX");
  }
} cxxCompilerIdNode;

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
//----------------------------------------------------------------------------
struct CompilerVersionNode : public cmGeneratorExpressionNode
{
  CompilerVersionNode() {}

  virtual int NumExpectedParameters() const { return ZeroOrMoreParameters; }

  std::string EvaluateWithLanguage(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *context,
                       const GeneratorExpressionContent *content,
                       cmGeneratorExpressionDAGChecker *,
                       const std::string &lang) const
  {
    const char *compilerVersion = context->Makefile ?
                              context->Makefile->GetSafeDefinition((
                        "CMAKE_" + lang + "_COMPILER_VERSION").c_str()) : "";
    if (parameters.size() == 0)
      {
      return compilerVersion ? compilerVersion : "";
      }

    cmsys::RegularExpression compilerIdValidator;
    compilerIdValidator.compile("^[0-9\\.]*$");
    if (!compilerIdValidator.find(parameters.begin()->c_str()))
      {
      reportError(context, content->GetOriginalExpression(),
                  "Expression syntax not recognized.");
      return std::string();
      }
    if (!compilerVersion)
      {
      return parameters.front().empty() ? "1" : "0";
      }

    return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
                                      parameters.begin()->c_str(),
                                      compilerVersion) ? "1" : "0";
  }
};

//----------------------------------------------------------------------------
static const struct CCompilerVersionNode : public CompilerVersionNode
{
  CCompilerVersionNode() {}

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *context,
                       const GeneratorExpressionContent *content,
                       cmGeneratorExpressionDAGChecker *dagChecker) const
  {
    if (parameters.size() != 0 && parameters.size() != 1)
      {
      reportError(context, content->GetOriginalExpression(),
          "$<C_COMPILER_VERSION> expression requires one or two parameters");
      return std::string();
      }
    if (!context->HeadTarget)
      {
      reportError(context, content->GetOriginalExpression(),
          "$<C_COMPILER_VERSION> may only be used with targets.  It may not "
          "be used with add_custom_command.");
      }
    return this->EvaluateWithLanguage(parameters, context, content,
                                      dagChecker, "C");
  }
} cCompilerVersionNode;

//----------------------------------------------------------------------------
static const struct CxxCompilerVersionNode : public CompilerVersionNode
{
  CxxCompilerVersionNode() {}

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *context,
                       const GeneratorExpressionContent *content,
                       cmGeneratorExpressionDAGChecker *dagChecker) const
  {
    if (parameters.size() != 0 && parameters.size() != 1)
      {
      reportError(context, content->GetOriginalExpression(),
          "$<CXX_COMPILER_VERSION> expression requires one or two "
          "parameters");
      return std::string();
      }
    if (!context->HeadTarget)
      {
      reportError(context, content->GetOriginalExpression(),
          "$<CXX_COMPILER_VERSION> may only be used with targets.  It may "
          "not be used with add_custom_command.");
      }
    return this->EvaluateWithLanguage(parameters, context, content,
                                      dagChecker, "CXX");
  }
} cxxCompilerVersionNode;


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
//----------------------------------------------------------------------------
static const struct VersionGreaterNode : public cmGeneratorExpressionNode
{
  VersionGreaterNode() {}

  virtual int NumExpectedParameters() const { return 2; }

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *,
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
  {
    return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER,
                                         parameters.front().c_str(),
                                         parameters[1].c_str()) ? "1" : "0";
  }
} versionGreaterNode;

//----------------------------------------------------------------------------
static const struct VersionLessNode : public cmGeneratorExpressionNode
{
  VersionLessNode() {}

  virtual int NumExpectedParameters() const { return 2; }

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *,
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
  {
    return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
                                         parameters.front().c_str(),
                                         parameters[1].c_str()) ? "1" : "0";
  }
} versionLessNode;

//----------------------------------------------------------------------------
static const struct VersionEqualNode : public cmGeneratorExpressionNode
{
  VersionEqualNode() {}

  virtual int NumExpectedParameters() const { return 2; }

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *,
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
  {
    return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
                                         parameters.front().c_str(),
                                         parameters[1].c_str()) ? "1" : "0";
  }
} versionEqualNode;

494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//----------------------------------------------------------------------------
static const struct LinkOnlyNode : public cmGeneratorExpressionNode
{
  LinkOnlyNode() {}

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *,
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *dagChecker) const
  {
    if(!dagChecker->GetTransitivePropertiesOnly())
      {
      return parameters.front();
      }
    return "";
  }
} linkOnlyNode;

512
513
514
515
//----------------------------------------------------------------------------
static const struct ConfigurationNode : public cmGeneratorExpressionNode
{
  ConfigurationNode() {}
516

517
518
519
520
  virtual int NumExpectedParameters() const { return 0; }

  std::string Evaluate(const std::vector<std::string> &,
                       cmGeneratorExpressionContext *context,
521
522
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
523
  {
524
    context->HadContextSensitiveCondition = true;
525
526
527
528
529
530
531
532
533
534
535
536
537
    return context->Config ? context->Config : "";
  }
} configurationNode;

//----------------------------------------------------------------------------
static const struct ConfigurationTestNode : public cmGeneratorExpressionNode
{
  ConfigurationTestNode() {}

  virtual int NumExpectedParameters() const { return 1; }

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *context,
538
539
                       const GeneratorExpressionContent *content,
                       cmGeneratorExpressionDAGChecker *) const
540
541
542
543
544
545
546
547
548
  {
    cmsys::RegularExpression configValidator;
    configValidator.compile("^[A-Za-z0-9_]*$");
    if (!configValidator.find(parameters.begin()->c_str()))
      {
      reportError(context, content->GetOriginalExpression(),
                  "Expression syntax not recognized.");
      return std::string();
      }
549
    context->HadContextSensitiveCondition = true;
550
551
552
553
554
    if (!context->Config)
      {
      return parameters.front().empty() ? "1" : "0";
      }

555
556
557
558
559
560
561
562
563
564
565
566
    if (cmsysString_strcasecmp(parameters.begin()->c_str(),
                                  context->Config) == 0)
      {
      return "1";
      }

    if (context->CurrentTarget
        && context->CurrentTarget->IsImported())
      {
      const char* loc = 0;
      const char* imp = 0;
      std::string suffix;
567
      if (context->CurrentTarget->GetMappedConfig(context->Config,
568
569
                                                  &loc,
                                                  &imp,
570
571
572
573
574
575
576
                                                  suffix))
        {
        // This imported target has an appropriate location
        // for this (possibly mapped) config.
        // Check if there is a proper config mapping for the tested config.
        std::vector<std::string> mappedConfigs;
        std::string mapProp = "MAP_IMPORTED_CONFIG_";
577
        mapProp += cmSystemTools::UpperCase(context->Config);
578
579
580
581
582
583
        if(const char* mapValue =
                        context->CurrentTarget->GetProperty(mapProp.c_str()))
          {
          cmSystemTools::ExpandListArgument(cmSystemTools::UpperCase(mapValue),
                                            mappedConfigs);
          return std::find(mappedConfigs.begin(), mappedConfigs.end(),
584
585
                           cmSystemTools::UpperCase(parameters.front()))
              != mappedConfigs.end() ? "1" : "0";
586
587
          }
        }
588
589
      }
    return "0";
590
591
592
  }
} configurationTestNode;

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
static const struct JoinNode : public cmGeneratorExpressionNode
{
  JoinNode() {}

  virtual int NumExpectedParameters() const { return 2; }

  virtual bool AcceptsArbitraryContentParameter() const { return true; }

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *,
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
  {
    std::string result;

    std::vector<std::string> list;
    cmSystemTools::ExpandListArgument(parameters.front(), list);
    std::string sep;
    for(std::vector<std::string>::const_iterator li = list.begin();
      li != list.end(); ++li)
      {
      result += sep + *li;
      sep = parameters[1];
      }
    return result;
  }
} joinNode;

621
622
623
#define TRANSITIVE_PROPERTY_NAME(PROPERTY) \
  , #PROPERTY

624
625
//----------------------------------------------------------------------------
static const char* targetPropertyTransitiveWhitelist[] = {
626
627
  0
  CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(TRANSITIVE_PROPERTY_NAME)
628
629
};

630
631
std::string getLinkedTargetsContent(const std::vector<std::string> &libraries,
                                  cmTarget *target,
632
                                  cmTarget *headTarget,
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
                                  cmGeneratorExpressionContext *context,
                                  cmGeneratorExpressionDAGChecker *dagChecker,
                                  const std::string &interfacePropertyName)
{
  cmGeneratorExpression ge(context->Backtrace);

  std::string sep;
  std::string depString;
  for (std::vector<std::string>::const_iterator
      it = libraries.begin();
      it != libraries.end(); ++it)
    {
    if (*it == target->GetName())
      {
      // Broken code can have a target in its own link interface.
      // Don't follow such link interface entries so as not to create a
      // self-referencing loop.
      continue;
      }
    if (context->Makefile->FindTargetToUse(it->c_str()))
      {
      depString +=
        sep + "$<TARGET_PROPERTY:" + *it + "," + interfacePropertyName + ">";
      sep = ";";
      }
    }
  cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(depString);
  std::string linkedTargetsContent = cge->Evaluate(context->Makefile,
                      context->Config,
                      context->Quiet,
663
                      headTarget,
664
665
666
667
668
669
670
671
672
                      target,
                      dagChecker);
  if (cge->GetHadContextSensitiveCondition())
    {
    context->HadContextSensitiveCondition = true;
    }
  return linkedTargetsContent;
}

673
674
675
676
677
678
679
680
681
682
683
//----------------------------------------------------------------------------
struct TransitiveWhitelistCompare
{
  explicit TransitiveWhitelistCompare(const std::string &needle)
    : Needle(needle) {}
  bool operator() (const char *item)
  { return strcmp(item, this->Needle.c_str()) == 0; }
private:
  std::string Needle;
};

684
//----------------------------------------------------------------------------
685
static const struct TargetPropertyNode : public cmGeneratorExpressionNode
686
687
688
689
{
  TargetPropertyNode() {}

  // This node handles errors on parameter count itself.
690
  virtual int NumExpectedParameters() const { return OneOrMoreParameters; }
691
692
693

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *context,
694
                       const GeneratorExpressionContent *content,
695
696
                       cmGeneratorExpressionDAGChecker *dagCheckerParent
                      ) const
697
698
699
700
701
702
703
  {
    if (parameters.size() != 1 && parameters.size() != 2)
      {
      reportError(context, content->GetOriginalExpression(),
          "$<TARGET_PROPERTY:...> expression requires one or two parameters");
      return std::string();
      }
704
705
    cmsys::RegularExpression propertyNameValidator;
    propertyNameValidator.compile("^[A-Za-z0-9_]+$");
706

707
    cmTarget* target = context->HeadTarget;
708
    std::string propertyName = *parameters.begin();
709
710
711
712
713
714
715
716
717
718
719

    if (!target && parameters.size() == 1)
      {
      reportError(context, content->GetOriginalExpression(),
          "$<TARGET_PROPERTY:prop> may only be used with targets.  It may not "
          "be used with add_custom_command.  Specify the target to read a "
          "property from using the $<TARGET_PROPERTY:tgt,prop> signature "
          "instead.");
      return std::string();
      }

720
721
    if (parameters.size() == 2)
      {
722
      if (parameters.begin()->empty() && parameters[1].empty())
723
724
725
726
727
728
729
730
731
732
733
734
735
736
        {
        reportError(context, content->GetOriginalExpression(),
            "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
            "target name and property name.");
        return std::string();
        }
      if (parameters.begin()->empty())
        {
        reportError(context, content->GetOriginalExpression(),
            "$<TARGET_PROPERTY:tgt,prop> expression requires a non-empty "
            "target name.");
        return std::string();
        }

737
      std::string targetName = parameters.front();
738
      propertyName = parameters[1];
739
      if (!cmGeneratorExpression::IsValidTargetName(targetName))
740
        {
741
        if (!propertyNameValidator.find(propertyName.c_str()))
742
743
744
745
746
747
748
749
750
          {
          ::reportError(context, content->GetOriginalExpression(),
                        "Target name and property name not supported.");
          return std::string();
          }
        ::reportError(context, content->GetOriginalExpression(),
                      "Target name not supported.");
        return std::string();
        }
751
752
753
754
755
756
757
758
759
760
761
762
      if(propertyName == "ALIASED_TARGET")
        {
        if(context->Makefile->IsAlias(targetName.c_str()))
          {
          if(cmTarget* tgt =
                      context->Makefile->FindTargetToUse(targetName.c_str()))
            {
            return tgt->GetName();
            }
          }
        return "";
        }
763
      target = context->Makefile->FindTargetToUse(
764
                                                targetName.c_str());
765
766
767
768
769

      if (!target)
        {
        cmOStringStream e;
        e << "Target \""
770
          << targetName
771
772
          << "\" not found.";
        reportError(context, content->GetOriginalExpression(), e.str());
773
        return std::string();
774
        }
775
        context->AllTargets.insert(target);
776
777
      }

778
779
780
781
    if (target == context->HeadTarget)
      {
      // Keep track of the properties seen while processing.
      // The evaluation of the LINK_LIBRARIES generator expressions
782
783
      // will check this to ensure that properties have one consistent
      // value for all evaluations.
784
785
786
      context->SeenTargetProperties.insert(propertyName);
      }

787
788
789
790
791
792
793
794
    if (propertyName.empty())
      {
      reportError(context, content->GetOriginalExpression(),
          "$<TARGET_PROPERTY:...> expression requires a non-empty property "
          "name.");
      return std::string();
      }

795
    if (!propertyNameValidator.find(propertyName.c_str()))
796
797
798
799
      {
      ::reportError(context, content->GetOriginalExpression(),
                    "Property name not supported.");
      return std::string();
800
      }
801

802
803
    assert(target);

804
805
    if (propertyName == "LINKER_LANGUAGE")
      {
806
807
      if (target->LinkLanguagePropagatesToDependents() &&
          dagCheckerParent && dagCheckerParent->EvaluatingLinkLibraries())
808
809
810
        {
        reportError(context, content->GetOriginalExpression(),
            "LINKER_LANGUAGE target property can not be used while evaluating "
811
            "link libraries for a static library");
812
813
814
815
816
817
        return std::string();
        }
      const char *lang = target->GetLinkerLanguage(context->Config);
      return lang ? lang : "";
      }

818
819
820
821
822
823
    cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
                                               target->GetName(),
                                               propertyName,
                                               content,
                                               dagCheckerParent);

824
    switch (dagChecker.check())
825
      {
826
    case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
827
828
      dagChecker.reportError(context, content->GetOriginalExpression());
      return std::string();
829
830
831
    case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
      // No error. We just skip cyclic references.
      return std::string();
832
    case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
833
      for (size_t i = 1;
834
835
836
837
838
839
840
841
842
843
          i < (sizeof(targetPropertyTransitiveWhitelist) /
                sizeof(*targetPropertyTransitiveWhitelist));
          ++i)
        {
        if (targetPropertyTransitiveWhitelist[i] == propertyName)
          {
          // No error. We're not going to find anything new here.
          return std::string();
          }
        }
844
845
    case cmGeneratorExpressionDAGChecker::DAG:
      break;
846
847
      }

848
    const char *prop = target->GetProperty(propertyName.c_str());
849
850

    if (dagCheckerParent)
851
      {
852
      if (dagCheckerParent->EvaluatingLinkLibraries())
853
        {
854
855
856
857
        if(!prop)
          {
          return std::string();
          }
858
        }
859
      else
860
        {
861
862
863
864
865
866
867
#define ASSERT_TRANSITIVE_PROPERTY_METHOD(METHOD) \
  dagCheckerParent->METHOD () ||

        assert(
          CM_FOR_EACH_TRANSITIVE_PROPERTY_METHOD(
                                            ASSERT_TRANSITIVE_PROPERTY_METHOD)
          false);
868
869
870
871
872
873
        }
      }

    std::string linkedTargetsContent;

    std::string interfacePropertyName;
874

875
876
877
878
879
    if (propertyName == "INTERFACE_INCLUDE_DIRECTORIES"
        || propertyName == "INCLUDE_DIRECTORIES")
      {
      interfacePropertyName = "INTERFACE_INCLUDE_DIRECTORIES";
      }
880
881
882
883
    else if (propertyName == "INTERFACE_SYSTEM_INCLUDE_DIRECTORIES")
      {
      interfacePropertyName = "INTERFACE_SYSTEM_INCLUDE_DIRECTORIES";
      }
884
885
886
887
888
889
    else if (propertyName == "INTERFACE_COMPILE_DEFINITIONS"
        || propertyName == "COMPILE_DEFINITIONS"
        || strncmp(propertyName.c_str(), "COMPILE_DEFINITIONS_", 20) == 0)
      {
      interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
      }
890
891
892
893
894
    else if (propertyName == "INTERFACE_COMPILE_OPTIONS"
        || propertyName == "COMPILE_OPTIONS")
      {
      interfacePropertyName = "INTERFACE_COMPILE_OPTIONS";
      }
895

896
897
    cmTarget *headTarget = context->HeadTarget ? context->HeadTarget : target;

898
    const char **transBegin = targetPropertyTransitiveWhitelist + 1;
899
900
901
902
    const char **transEnd = targetPropertyTransitiveWhitelist
              + (sizeof(targetPropertyTransitiveWhitelist) /
              sizeof(*targetPropertyTransitiveWhitelist));
    if (std::find_if(transBegin, transEnd,
903
              TransitiveWhitelistCompare(propertyName)) != transEnd)
904
      {
905
906
907
908
909

      std::vector<std::string> libs;
      target->GetTransitivePropertyLinkLibraries(context->Config,
                                                 headTarget, libs);
      if (!libs.empty())
910
        {
911
        linkedTargetsContent =
912
                  getLinkedTargetsContent(libs, target,
913
                                          headTarget,
914
915
                                          context, &dagChecker,
                                          interfacePropertyName);
916
917
        }
      }
918
919
920
921
922
    else if (std::find_if(transBegin, transEnd,
              TransitiveWhitelistCompare(interfacePropertyName)) != transEnd)
      {
      const cmTarget::LinkImplementation *impl = target->GetLinkImplementation(
                                                    context->Config,
923
                                                    headTarget);
924
925
926
927
      if(impl)
        {
        linkedTargetsContent =
                  getLinkedTargetsContent(impl->Libraries, target,
928
                                          headTarget,
929
930
                                          context, &dagChecker,
                                          interfacePropertyName);
931
932
933
        }
      }

934
935
936
    linkedTargetsContent =
          cmGeneratorExpression::StripEmptyListElements(linkedTargetsContent);

937
938
939
940
941
    if (!prop)
      {
      if (target->IsImported())
        {
        return linkedTargetsContent;
942
943
944
945
        }
      if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
                                                       context->Config))
        {
946
        context->HadContextSensitiveCondition = true;
947
948
949
950
        return target->GetLinkInterfaceDependentBoolProperty(
                                                propertyName,
                                                context->Config) ? "1" : "0";
        }
951
952
953
      if (target->IsLinkInterfaceDependentStringProperty(propertyName,
                                                         context->Config))
        {
954
        context->HadContextSensitiveCondition = true;
955
956
        const char *propContent =
                              target->GetLinkInterfaceDependentStringProperty(
957
958
                                                propertyName,
                                                context->Config);
959
        return propContent ? propContent : "";
960
        }
961

962
      return linkedTargetsContent;
963
964
      }

965
    for (size_t i = 1;
966
967
968
969
         i < (sizeof(targetPropertyTransitiveWhitelist) /
              sizeof(*targetPropertyTransitiveWhitelist));
         ++i)
      {
970
      if (targetPropertyTransitiveWhitelist[i] == interfacePropertyName)
971
972
        {
        cmGeneratorExpression ge(context->Backtrace);
973
974
        cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
        std::string result = cge->Evaluate(context->Makefile,
975
976
                            context->Config,
                            context->Quiet,
977
                            headTarget,
978
979
                            target,
                            &dagChecker);
980
981
982
983
984

        if (cge->GetHadContextSensitiveCondition())
          {
          context->HadContextSensitiveCondition = true;
          }
985
986
987
988
        if (!linkedTargetsContent.empty())
          {
          result += (result.empty() ? "" : ";") + linkedTargetsContent;
          }
989
        return result;
990
991
992
        }
      }
    return prop;
993
994
995
  }
} targetPropertyNode;

996
997
998
999
1000
1001
1002
//----------------------------------------------------------------------------
static const struct TargetNameNode : public cmGeneratorExpressionNode
{
  TargetNameNode() {}

  virtual bool GeneratesContent() const { return true; }

1003
  virtual bool AcceptsArbitraryContentParameter() const { return true; }
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
  virtual bool RequiresLiteralInput() const { return true; }

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *,
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
  {
    return parameters.front();
  }

  virtual int NumExpectedParameters() const { return 1; }

} targetNameNode;

1018
1019
//----------------------------------------------------------------------------
static const char* targetPolicyWhitelist[] = {
1020
1021
1022
1023
1024
1025
1026
  0
#define TARGET_POLICY_STRING(POLICY) \
  , #POLICY

  CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_STRING)

#undef TARGET_POLICY_STRING
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
};

cmPolicies::PolicyStatus statusForTarget(cmTarget *tgt, const char *policy)
{
#define RETURN_POLICY(POLICY) \
  if (strcmp(policy, #POLICY) == 0) \
  { \
    return tgt->GetPolicyStatus ## POLICY (); \
  } \

1037
  CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY)
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052

#undef RETURN_POLICY

  assert("!Unreachable code. Not a valid policy");
  return cmPolicies::WARN;
}

cmPolicies::PolicyID policyForString(const char *policy_id)
{
#define RETURN_POLICY_ID(POLICY_ID) \
  if (strcmp(policy_id, #POLICY_ID) == 0) \
  { \
    return cmPolicies:: POLICY_ID; \
  } \

1053
  CM_FOR_EACH_TARGET_POLICY(RETURN_POLICY_ID)
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079

#undef RETURN_POLICY_ID

  assert("!Unreachable code. Not a valid policy");
  return cmPolicies::CMP0002;
}

//----------------------------------------------------------------------------
static const struct TargetPolicyNode : public cmGeneratorExpressionNode
{
  TargetPolicyNode() {}

  virtual int NumExpectedParameters() const { return 1; }

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *context ,
                       const GeneratorExpressionContent *content,
                       cmGeneratorExpressionDAGChecker *) const
  {
    if (!context->HeadTarget)
      {
      reportError(context, content->GetOriginalExpression(),
          "$<TARGET_POLICY:prop> may only be used with targets.  It may not "
          "be used with add_custom_command.");
      return std::string();
      }
1080
1081
1082

    context->HadContextSensitiveCondition = true;

1083
    for (size_t i = 1;
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
         i < (sizeof(targetPolicyWhitelist) /
              sizeof(*targetPolicyWhitelist));
         ++i)
      {
      const char *policy = targetPolicyWhitelist[i];
      if (parameters.front() == policy)
        {
        cmMakefile *mf = context->HeadTarget->GetMakefile();
        switch(statusForTarget(context->HeadTarget, policy))
          {
          case cmPolicies::WARN:
            mf->IssueMessage(cmake::AUTHOR_WARNING,
                             mf->GetPolicies()->
                             GetPolicyWarning(policyForString(policy)));
          case cmPolicies::REQUIRED_IF_USED:
          case cmPolicies::REQUIRED_ALWAYS:
          case cmPolicies::OLD:
            return "0";
          case cmPolicies::NEW:
            return "1";
          }
        }
      }
    reportError(context, content->GetOriginalExpression(),
      "$<TARGET_POLICY:prop> may only be used with a limited number of "
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
      "policies.  Currently it may be used with the following policies:\n"

#define STRINGIFY_HELPER(X) #X
#define STRINGIFY(X) STRINGIFY_HELPER(X)

#define TARGET_POLICY_LIST_ITEM(POLICY) \
      " * " STRINGIFY(POLICY) "\n"

      CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_LIST_ITEM)

#undef TARGET_POLICY_LIST_ITEM
1120
1121
1122
1123
1124
1125
      );
    return std::string();
  }

} targetPolicyNode;

Stephen Kelly's avatar
Stephen Kelly committed
1126
1127
1128
1129
1130
//----------------------------------------------------------------------------
static const struct InstallPrefixNode : public cmGeneratorExpressionNode
{
  InstallPrefixNode() {}

1131
  virtual bool GeneratesContent() const { return true; }
Stephen Kelly's avatar
Stephen Kelly committed
1132
1133
1134
  virtual int NumExpectedParameters() const { return 0; }

  std::string Evaluate(const std::vector<std::string> &,
1135
1136
                       cmGeneratorExpressionContext *context,
                       const GeneratorExpressionContent *content,
Stephen Kelly's avatar
Stephen Kelly committed
1137
1138
                       cmGeneratorExpressionDAGChecker *) const
  {
1139
1140
1141
    reportError(context, content->GetOriginalExpression(),
                "INSTALL_PREFIX is a marker for install(EXPORT) only.  It "
                "should never be evaluated.");
Stephen Kelly's avatar
Stephen Kelly committed
1142
1143
1144
1145
1146
    return std::string();
  }

} installPrefixNode;

1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
//----------------------------------------------------------------------------
template<bool linker, bool soname>
struct TargetFilesystemArtifactResultCreator
{
  static std::string Create(cmTarget* target,
                            cmGeneratorExpressionContext *context,
                            const GeneratorExpressionContent *content);
};

//----------------------------------------------------------------------------
template<>
struct TargetFilesystemArtifactResultCreator<false, true>
{
  static std::string Create(cmTarget* target,
                            cmGeneratorExpressionContext *context,
                            const GeneratorExpressionContent *content)
  {
    // The target soname file (.so.1).
    if(target->IsDLLPlatform())
      {
      ::reportError(context, content->GetOriginalExpression(),
                    "TARGET_SONAME_FILE is not allowed "
                    "for DLL target platforms.");
      return std::string();
      }
    if(target->GetType() != cmTarget::SHARED_LIBRARY)
      {
      ::reportError(context, content->GetOriginalExpression(),
                    "TARGET_SONAME_FILE is allowed only for "
                    "SHARED libraries.");
      return std::string();
      }
    std::string result = target->GetDirectory(context->Config);
    result += "/";
    result += target->GetSOName(context->Config);
    return result;
  }
};

//----------------------------------------------------------------------------
template<>
struct TargetFilesystemArtifactResultCreator<true, false>
{
  static std::string Create(cmTarget* target,
                            cmGeneratorExpressionContext *context,
                            const GeneratorExpressionContent *content)
  {
    // The file used to link to the target (.so, .lib, .a).
    if(!target->IsLinkable())
      {
      ::reportError(context, content->GetOriginalExpression(),
                    "TARGET_LINKER_FILE is allowed only for libraries and "
                    "executables with ENABLE_EXPORTS.");
      return std::string();
      }
    return target->GetFullPath(context->Config,
                               target->HasImportLibrary());
  }
};

//----------------------------------------------------------------------------
template<>
struct TargetFilesystemArtifactResultCreator<false, false>
{
  static std::string Create(cmTarget* target,
                            cmGeneratorExpressionContext *context,
                            const GeneratorExpressionContent *)
  {
    return target->GetFullPath(context->Config, false, true);
  }
};


//----------------------------------------------------------------------------
template<bool dirQual, bool nameQual>
struct TargetFilesystemArtifactResultGetter
{
  static std::string Get(const std::string &result);
};

//----------------------------------------------------------------------------
template<>
struct TargetFilesystemArtifactResultGetter<false, true>
{
  static std::string Get(const std::string &result)
  { return cmSystemTools::GetFilenameName(result); }
};

//----------------------------------------------------------------------------
template<>
struct TargetFilesystemArtifactResultGetter<true, false>
{
  static std::string Get(const std::string &result)
  { return cmSystemTools::GetFilenamePath(result); }
};

//----------------------------------------------------------------------------
template<>
struct TargetFilesystemArtifactResultGetter<false, false>
{
  static std::string Get(const std::string &result)
  { return result; }
};

//----------------------------------------------------------------------------
template<bool linker, bool soname, bool dirQual, bool nameQual>
struct TargetFilesystemArtifact : public cmGeneratorExpressionNode
{
  TargetFilesystemArtifact() {}

  virtual int NumExpectedParameters() const { return 1; }

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *context,
1261
                       const GeneratorExpressionContent *content,
1262
                       cmGeneratorExpressionDAGChecker *dagChecker) const
1263
1264
1265
1266
  {
    // Lookup the referenced target.
    std::string name = *parameters.begin();

1267
    if (!cmGeneratorExpression::IsValidTargetName(name))
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
      {
      ::reportError(context, content->GetOriginalExpression(),
                    "Expression syntax not recognized.");
      return std::string();
      }
    cmTarget* target = context->Makefile->FindTargetToUse(name.c_str());
    if(!target)
      {
      ::reportError(context, content->GetOriginalExpression(),
                    "No target \"" + name + "\"");
      return std::string();
      }
1280
    if(target->GetType() >= cmTarget::OBJECT_LIBRARY &&
1281
1282
1283
1284
1285
1286
      target->GetType() != cmTarget::UNKNOWN_LIBRARY)
      {
      ::reportError(context, content->GetOriginalExpression(),
                  "Target \"" + name + "\" is not an executable or library.");
      return std::string();
      }
1287
1288
1289
1290
1291
1292
1293
    if (dagChecker && dagChecker->EvaluatingLinkLibraries(name.c_str()))
      {
      ::reportError(context, content->GetOriginalExpression(),
                    "Expressions which require the linker language may not "
                    "be used while evaluating link libraries");
      return std::string();
      }
1294
1295
    context->DependTargets.insert(target);
    context->AllTargets.insert(target);
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336

    std::string result =
                TargetFilesystemArtifactResultCreator<linker, soname>::Create(
                          target,
                          context,
                          content);
    if (context->HadError)
      {
      return std::string();
      }
    return
        TargetFilesystemArtifactResultGetter<dirQual, nameQual>::Get(result);
  }
};

//----------------------------------------------------------------------------
static const
TargetFilesystemArtifact<false, false, false, false> targetFileNode;
static const
TargetFilesystemArtifact<true, false, false, false> targetLinkerFileNode;
static const
TargetFilesystemArtifact<false, true, false, false> targetSoNameFileNode;
static const
TargetFilesystemArtifact<false, false, false, true> targetFileNameNode;
static const
TargetFilesystemArtifact<true, false, false, true> targetLinkerFileNameNode;
static const
TargetFilesystemArtifact<false, true, false, true> targetSoNameFileNameNode;
static const
TargetFilesystemArtifact<false, false, true, false> targetFileDirNode;
static const
TargetFilesystemArtifact<true, false, true, false> targetLinkerFileDirNode;
static const
TargetFilesystemArtifact<false, true, true, false> targetSoNameFileDirNode;

//----------------------------------------------------------------------------
static const
cmGeneratorExpressionNode* GetNode(const std::string &identifier)
{
  if (identifier == "0")
    return &zeroNode;
1337
  else if (identifier == "1")
1338
    return &oneNode;
1339
  else if (identifier == "AND")
1340
    return &andNode;
1341
  else if (identifier == "OR")
1342
    return &orNode;
1343
  else if (identifier == "NOT")
1344
    return &notNode;
1345
1346
1347
1348
  else if (identifier == "C_COMPILER_ID")
    return &cCompilerIdNode;
  else if (identifier == "CXX_COMPILER_ID")
    return &cxxCompilerIdNode;
1349
1350
1351
1352
1353
1354
  else if (identifier == "VERSION_GREATER")
    return &versionGreaterNode;
  else if (identifier == "VERSION_LESS")
    return &versionLessNode;
  else if (identifier == "VERSION_EQUAL")
    return &versionEqualNode;
1355
1356
1357
1358
  else if (identifier == "C_COMPILER_VERSION")
    return &cCompilerVersionNode;
  else if (identifier == "CXX_COMPILER_VERSION")
    return &cxxCompilerVersionNode;
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
  else if (identifier == "CONFIGURATION")
    return &configurationNode;
  else if (identifier == "CONFIG")
    return &configurationTestNode;
  else if (identifier == "TARGET_FILE")
    return &targetFileNode;
  else if (identifier == "TARGET_LINKER_FILE")
    return &targetLinkerFileNode;
  else if (identifier == "TARGET_SONAME_FILE")
    return &targetSoNameFileNode;
  else if (identifier == "TARGET_FILE_NAME")
    return &targetFileNameNode;
  else if (identifier == "TARGET_LINKER_FILE_NAME")
    return &targetLinkerFileNameNode;
  else if (identifier == "TARGET_SONAME_FILE_NAME")
    return &targetSoNameFileNameNode;
  else if (identifier == "TARGET_FILE_DIR")
    return &targetFileDirNode;
  else if (identifier == "TARGET_LINKER_FILE_DIR")
    return &targetLinkerFileDirNode;
  else if (identifier == "TARGET_SONAME_FILE_DIR")
    return &targetSoNameFileDirNode;
1381
1382
1383
1384
1385
1386
1387
1388
  else if (identifier == "STREQUAL")
    return &strEqualNode;
  else if (identifier == "BOOL")
    return &boolNode;
  else if (identifier == "ANGLE-R")
    return &angle_rNode;
  else if (identifier == "COMMA")
    return &commaNode;
1389
1390
  else if (identifier == "SEMICOLON")
    return &semicolonNode;
1391
1392
  else if (identifier == "TARGET_PROPERTY")
    return &targetPropertyNode;
1393
1394
  else if (identifier == "TARGET_NAME")
    return &targetNameNode;
1395
1396
  else if (identifier == "TARGET_POLICY")
    return &targetPolicyNode;
1397
1398
1399
1400
  else if (identifier == "BUILD_INTERFACE")
    return &buildInterfaceNode;
  else if (identifier == "INSTALL_INTERFACE")
    return &installInterfaceNode;
Stephen Kelly's avatar
Stephen Kelly committed
1401
1402
  else if (identifier == "INSTALL_PREFIX")
    return &installPrefixNode;
1403
1404
  else if (identifier == "JOIN")
    return &joinNode;
1405
1406
  else if (identifier == "LINK_ONLY")
    return &linkOnlyNode;
1407
  return 0;
1408

1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
}

//----------------------------------------------------------------------------
GeneratorExpressionContent::GeneratorExpressionContent(
                                                    const char *startContent,
                                                    unsigned int length)
  : StartContent(startContent), ContentLength(length)
{

}

//----------------------------------------------------------------------------
std::string GeneratorExpressionContent::GetOriginalExpression() const
{
  return std::string(this->StartContent, this->ContentLength);
}

1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
//----------------------------------------------------------------------------
std::string GeneratorExpressionContent::ProcessArbitraryContent(
    const cmGeneratorExpressionNode *node,
    const std::string &identifier,
    cmGeneratorExpressionContext *context,
    cmGeneratorExpressionDAGChecker *dagChecker,
    std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
    pit) const
{
  std::string result;

  const
  std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
                                      pend = this->ParamChildren.end();
  for ( ; pit != pend; ++pit)
    {
    std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
                                                            = pit->begin();
    const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
                                                              = pit->end();
    for ( ; it != end; ++it)
      {
      if (node->RequiresLiteralInput())
        {
        if ((*it)->GetType() != cmGeneratorExpressionEvaluator::Text)
          {
          reportError(context, this->GetOriginalExpression(),
                "$<" + identifier + "> expression requires literal input.");
          return std::string();
          }
        }
      result += (*it)->Evaluate(context, dagChecker);
      if (context->HadError)
        {
        return std::string();
        }
      }
      if ((pit + 1) != pend)
        {
        result += ",";
        }
    }
  if (node->RequiresLiteralInput())
    {
    std::vector<std::string> parameters;
    parameters.push_back(result);
    return node->Evaluate(parameters, context, this, dagChecker);
    }
  return result;
}

1477
1478
//----------------------------------------------------------------------------
std::string GeneratorExpressionContent::Evaluate(
1479
1480
                            cmGeneratorExpressionContext *context,
                            cmGeneratorExpressionDAGChecker *dagChecker) const
1481
1482
1483
1484
1485
1486
1487
1488
1489
{
  std::string identifier;
  {
  std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
                                          = this->IdentifierChildren.begin();
  const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
                                          = this->IdentifierChildren.end();
  for ( ; it != end; ++it)
    {
1490
    identifier += (*it)->Evaluate(context, dagChecker);
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
    if (context->HadError)
      {
      return std::string();
      }
    }
  }

  const cmGeneratorExpressionNode *node = GetNode(identifier);

  if (!node)
    {
    reportError(context, this->GetOriginalExpression(),
              "Expression did not evaluate to a known generator expression");
    return std::string();
    }

  if (!node->GeneratesContent())
    {
1509
1510
    if (node->NumExpectedParameters() == 1
        && node->AcceptsArbitraryContentParameter())
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
      {
      if (this->ParamChildren.empty())
        {
        reportError(context, this->GetOriginalExpression(),
                  "$<" + identifier + "> expression requires a parameter.");
        }
      }
    else
      {
      std::vector<std::string> parameters;
      this->EvaluateParameters(node, identifier, context, dagChecker,
                               parameters);
      }
1524
1525
1526
    return std::string();
    }

1527
1528
  if (node->NumExpectedParameters() == 1
        && node->AcceptsArbitraryContentParameter())
1529
    {
1530
1531
1532
    return this->ProcessArbitraryContent(node, identifier, context,
                                         dagChecker,
                                         this->ParamChildren.begin());
1533
1534
1535
    }

  std::vector<std::string> parameters;
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
  this->EvaluateParameters(node, identifier, context, dagChecker, parameters);
  if (context->HadError)
    {
    return std::string();
    }

  return node->Evaluate(parameters, context, this, dagChecker);
}

//----------------------------------------------------------------------------
std::string GeneratorExpressionContent::EvaluateParameters(
                                const cmGeneratorExpressionNode *node,
                                const std::string &identifier,
                                cmGeneratorExpressionContext *context,
                                cmGeneratorExpressionDAGChecker *dagChecker,
                                std::vector<std::string> &parameters) const
{
1553
  const int numExpected = node->NumExpectedParameters();
1554
1555
1556
1557
1558
1559
  {
  std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
                                        pit = this->ParamChildren.begin();
  const
  std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
                                        pend = this->ParamChildren.end();
1560
1561
  const bool acceptsArbitraryContent
                                  = node->AcceptsArbitraryContentParameter();
1562
1563
1564
1565
1566
1567
1568
1569
1570
  for ( ; pit != pend; ++pit)
    {
    std::string parameter;
    std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it =
                                                              pit->begin();
    const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end =
                                                              pit->end();
    for ( ; it != end; ++it)
      {
1571
      parameter += (*it)->Evaluate(context, dagChecker);
1572
1573
1574
1575
1576
1577
      if (context->HadError)
        {
        return std::string();
        }
      }
    parameters.push_back(parameter);
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
    if (acceptsArbitraryContent
        && parameters.size() == (unsigned int)numExpected - 1)
      {
      assert(pit != pend);
      std::string lastParam = this->ProcessArbitraryContent(node, identifier,
                                                            context,
                                                            dagChecker,
                                                            pit + 1);
      parameters.push_back(lastParam);
      return std::string();
      }
1589
1590
1591
    }
  }

1592
1593
  if ((numExpected > cmGeneratorExpressionNode::DynamicParameters
      && (unsigned int)numExpected != parameters.size()))
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
    {
    if (numExpected == 0)
      {
      reportError(context, this->GetOriginalExpression(),
                  "$<" + identifier + "> expression requires no parameters.");
      }
    else if (numExpected == 1)
      {
      reportError(context, this->GetOriginalExpression(),
                  "$<" + identifier + "> expression requires "
                  "exactly one parameter.");
      }
    else
      {
      cmOStringStream e;
      e << "$<" + identifier + "> expression requires "
        << numExpected
        << " comma separated parameters, but got "
        << parameters.size() << " instead.";
      reportError(context,<