cmGeneratorExpressionEvaluator.cxx 38.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
48
49
50
51
                        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
{
  virtual ~cmGeneratorExpressionNode() {}

  virtual bool GeneratesContent() const { return true; }

52
53
  virtual bool RequiresLiteralInput() const { return false; }

54
55
56
57
58
59
60
  virtual bool AcceptsSingleArbitraryContentParameter() const
    { return false; }

  virtual int NumExpectedParameters() const { return 1; }

  virtual std::string Evaluate(const std::vector<std::string> &parameters,
                               cmGeneratorExpressionContext *context,
61
62
                               const GeneratorExpressionContent *content,
                               cmGeneratorExpressionDAGChecker *dagChecker
63
64
65
66
67
68
69
70
71
72
                              ) const = 0;
};

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

  virtual bool GeneratesContent() const { return false; }

73
74
  virtual bool AcceptsSingleArbitraryContentParameter() const { return true; }

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

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

  virtual bool AcceptsSingleArbitraryContentParameter() const { return true; }

  std::string Evaluate(const std::vector<std::string> &,
                       cmGeneratorExpressionContext *,
94
95
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
96
97
98
99
100
101
  {
    // Unreachable
    return std::string();
  }
} oneNode;

102
103
104
105
106
107
//----------------------------------------------------------------------------
static const struct OneNode buildInterfaceNode;

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

108
109
110
111
112
113
114
115
116
117
//----------------------------------------------------------------------------
#define BOOLEAN_OP_NODE(OPNAME, OP, SUCCESS_VALUE, FAILURE_VALUE) \
static const struct OP ## Node : public cmGeneratorExpressionNode \
{ \
  OP ## Node () {} \
/* We let -1 carry the meaning 'at least one' */ \
  virtual int NumExpectedParameters() const { return -1; } \
 \
  std::string Evaluate(const std::vector<std::string> &parameters, \
                       cmGeneratorExpressionContext *context, \
118
119
                       const GeneratorExpressionContent *content, \
                       cmGeneratorExpressionDAGChecker *) const \
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
  { \
    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() {}
149

150
151
  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *context,
152
153
                       const GeneratorExpressionContent *content,
                       cmGeneratorExpressionDAGChecker *) const
154
155
156
157
158
159
160
161
162
163
164
  {
    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;

165
166
167
168
169
170
171
172
173
//----------------------------------------------------------------------------
static const struct BoolNode : public cmGeneratorExpressionNode
{
  BoolNode() {}

  virtual int NumExpectedParameters() const { return 1; }

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *,
174
175
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  {
    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 *,
190
191
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
192
  {
193
    return *parameters.begin() == parameters[1] ? "1" : "0";
194
195
196
197
198
199
200
201
202
203
204
205
  }
} 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 *,
206
207
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  {
    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 *,
222
223
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
224
225
226
227
228
  {
    return ",";
  }
} commaNode;

229
230
231
232
//----------------------------------------------------------------------------
static const struct ConfigurationNode : public cmGeneratorExpressionNode
{
  ConfigurationNode() {}
233

234
235
236
237
  virtual int NumExpectedParameters() const { return 0; }

  std::string Evaluate(const std::vector<std::string> &,
                       cmGeneratorExpressionContext *context,
238
239
                       const GeneratorExpressionContent *,
                       cmGeneratorExpressionDAGChecker *) const
240
  {
241
    context->HadContextSensitiveCondition = true;
242
243
244
245
246
247
248
249
250
251
252
253
254
    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,
255
256
                       const GeneratorExpressionContent *content,
                       cmGeneratorExpressionDAGChecker *) const
257
258
259
260
261
262
263
264
265
  {
    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();
      }
266
    context->HadContextSensitiveCondition = true;
267
268
269
270
271
    if (!context->Config)
      {
      return parameters.front().empty() ? "1" : "0";
      }

272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    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;
      return context->CurrentTarget->GetMappedConfig(context->Config,
                                                  &loc,
                                                  &imp,
                                                  suffix) ? "1" : "0";
      }
    return "0";
290
291
292
  }
} configurationTestNode;

293
294
295
296
297
298
299

//----------------------------------------------------------------------------
static const char* targetPropertyTransitiveWhitelist[] = {
    "INTERFACE_INCLUDE_DIRECTORIES"
  , "INTERFACE_COMPILE_DEFINITIONS"
};

300
//----------------------------------------------------------------------------
301
static const struct TargetPropertyNode : public cmGeneratorExpressionNode
302
303
304
305
306
307
308
309
{
  TargetPropertyNode() {}

  // This node handles errors on parameter count itself.
  virtual int NumExpectedParameters() const { return -1; }

  std::string Evaluate(const std::vector<std::string> &parameters,
                       cmGeneratorExpressionContext *context,
310
                       const GeneratorExpressionContent *content,
311
312
                       cmGeneratorExpressionDAGChecker *dagCheckerParent
                      ) const
313
314
315
316
317
318
319
  {
    if (parameters.size() != 1 && parameters.size() != 2)
      {
      reportError(context, content->GetOriginalExpression(),
          "$<TARGET_PROPERTY:...> expression requires one or two parameters");
      return std::string();
      }
320
321
    cmsys::RegularExpression propertyNameValidator;
    propertyNameValidator.compile("^[A-Za-z0-9_]+$");
322

323
    cmTarget* target = context->HeadTarget;
324
    std::string propertyName = *parameters.begin();
325
326
327
328
329
330
331
332
333
334
335

    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();
      }

336
337
    if (parameters.size() == 2)
      {
338
      if (parameters.begin()->empty() && parameters[1].empty())
339
340
341
342
343
344
345
346
347
348
349
350
351
352
        {
        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();
        }

353
      std::string targetName = parameters.front();
354
      propertyName = parameters[1];
355
      if (!cmGeneratorExpression::IsValidTargetName(targetName))
356
        {
357
        if (!propertyNameValidator.find(propertyName.c_str()))
358
359
360
361
362
363
364
365
366
          {
          ::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();
        }
367
      target = context->Makefile->FindTargetToUse(
368
                                                targetName.c_str());
369
370
371
372
373

      if (!target)
        {
        cmOStringStream e;
        e << "Target \""
374
          << targetName
375
376
          << "\" not found.";
        reportError(context, content->GetOriginalExpression(), e.str());
377
        return std::string();
378
        }
379
380
      }

381
382
383
384
    if (target == context->HeadTarget)
      {
      // Keep track of the properties seen while processing.
      // The evaluation of the LINK_LIBRARIES generator expressions
385
386
      // will check this to ensure that properties have one consistent
      // value for all evaluations.
387
388
389
      context->SeenTargetProperties.insert(propertyName);
      }

390
391
392
393
394
395
396
397
    if (propertyName.empty())
      {
      reportError(context, content->GetOriginalExpression(),
          "$<TARGET_PROPERTY:...> expression requires a non-empty property "
          "name.");
      return std::string();
      }

398
    if (!propertyNameValidator.find(propertyName.c_str()))
399
400
401
402
      {
      ::reportError(context, content->GetOriginalExpression(),
                    "Property name not supported.");
      return std::string();
403
      }
404

405
406
    assert(target);

407
408
409
410
411
412
    cmGeneratorExpressionDAGChecker dagChecker(context->Backtrace,
                                               target->GetName(),
                                               propertyName,
                                               content,
                                               dagCheckerParent);

413
    switch (dagChecker.check())
414
      {
415
    case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
416
417
      dagChecker.reportError(context, content->GetOriginalExpression());
      return std::string();
418
419
420
    case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
      // No error. We just skip cyclic references.
      return std::string();
421
    case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
422
423
424
425
426
427
428
429
430
431
432
      for (size_t i = 0;
          i < (sizeof(targetPropertyTransitiveWhitelist) /
                sizeof(*targetPropertyTransitiveWhitelist));
          ++i)
        {
        if (targetPropertyTransitiveWhitelist[i] == propertyName)
          {
          // No error. We're not going to find anything new here.
          return std::string();
          }
        }
433
434
    case cmGeneratorExpressionDAGChecker::DAG:
      break;
435
436
      }

437
    const char *prop = target->GetProperty(propertyName.c_str());
438
439
440
441

    std::string linkedTargetsContent;

    if (dagCheckerParent)
442
      {
443
      if (dagCheckerParent->EvaluatingLinkLibraries())
444
        {
445
446
447
448
        if(!prop)
          {
          return std::string();
          }
449
        }
450
      else
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
        assert(dagCheckerParent->EvaluatingIncludeDirectories()
            || dagCheckerParent->EvaluatingCompileDefinitions());

        if (propertyName == "INTERFACE_INCLUDE_DIRECTORIES"
            || propertyName == "INTERFACE_COMPILE_DEFINITIONS")
          {
          const cmTarget::LinkInterface *iface = target->GetLinkInterface(
                                                        context->Config,
                                                        context->HeadTarget);
          if(iface)
            {
            cmGeneratorExpression ge(context->Backtrace);

            std::string sep;
            std::string depString;
            for (std::vector<std::string>::const_iterator
                it = iface->Libraries.begin();
                it != iface->Libraries.end(); ++it)
              {
              if (context->Makefile->FindTargetToUse(it->c_str()))
                {
                depString +=
                  sep + "$<TARGET_PROPERTY:" + *it + "," + propertyName + ">";
                sep = ";";
                }
              }
            cmsys::auto_ptr<cmCompiledGeneratorExpression> cge =
                                                          ge.Parse(depString);
            linkedTargetsContent = cge->Evaluate(context->Makefile,
                                context->Config,
                                context->Quiet,
                                context->HeadTarget,
                                target,
                                &dagChecker);
            if (cge->GetHadContextSensitiveCondition())
              {
              context->HadContextSensitiveCondition = true;
              }
            }
          }
        }
      }

    if (!prop)
      {
      if (target->IsImported())
        {
        return linkedTargetsContent;
500
501
502
503
        }
      if (target->IsLinkInterfaceDependentBoolProperty(propertyName,
                                                       context->Config))
        {
504
        context->HadContextSensitiveCondition = true;
505
506
507
508
        return target->GetLinkInterfaceDependentBoolProperty(
                                                propertyName,
                                                context->Config) ? "1" : "0";
        }
509
510
511
      if (target->IsLinkInterfaceDependentStringProperty(propertyName,
                                                         context->Config))
        {
512
        context->HadContextSensitiveCondition = true;
513
514
        const char *propContent =
                              target->GetLinkInterfaceDependentStringProperty(
515
516
                                                propertyName,
                                                context->Config);
517
        return propContent ? propContent : "";
518
        }
519

520
      return linkedTargetsContent;
521
522
523
524
525
526
527
528
529
530
      }

    for (size_t i = 0;
         i < (sizeof(targetPropertyTransitiveWhitelist) /
              sizeof(*targetPropertyTransitiveWhitelist));
         ++i)
      {
      if (targetPropertyTransitiveWhitelist[i] == propertyName)
        {
        cmGeneratorExpression ge(context->Backtrace);
531
532
        cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
        std::string result = cge->Evaluate(context->Makefile,
533
534
535
536
537
                            context->Config,
                            context->Quiet,
                            context->HeadTarget,
                            target,
                            &dagChecker);
538
539
540
541
542

        if (cge->GetHadContextSensitiveCondition())
          {
          context->HadContextSensitiveCondition = true;
          }
543
544
545
546
        if (!linkedTargetsContent.empty())
          {
          result += (result.empty() ? "" : ";") + linkedTargetsContent;
          }
547
        return result;
548
549
550
        }
      }
    return prop;
551
552
553
  }
} targetPropertyNode;

554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//----------------------------------------------------------------------------
static const struct TargetNameNode : public cmGeneratorExpressionNode
{
  TargetNameNode() {}

  virtual bool GeneratesContent() const { return true; }

  virtual bool AcceptsSingleArbitraryContentParameter() const { return true; }
  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;

576
577
578
579
580
//----------------------------------------------------------------------------
static const char* targetPolicyWhitelist[] = {
    "CMP0003"
  , "CMP0004"
  , "CMP0008"
581
  , "CMP0020"
582
583
584
585
586
587
588
589
590
591
592
593
594
};

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

  RETURN_POLICY(CMP0003)
  RETURN_POLICY(CMP0004)
  RETURN_POLICY(CMP0008)
595
  RETURN_POLICY(CMP0020)
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613

#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; \
  } \

  RETURN_POLICY_ID(CMP0003)
  RETURN_POLICY_ID(CMP0004)
  RETURN_POLICY_ID(CMP0008)
614
  RETURN_POLICY_ID(CMP0020)
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

#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();
      }
641
642
643

    context->HadContextSensitiveCondition = true;

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
    for (size_t i = 0;
         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 "
670
671
      "policies.  Currently it may be used with policies CMP0003, CMP0004, "
      "CMP0008 and CMP0020."
672
673
674
675
676
677
      );
    return std::string();
  }

} targetPolicyNode;

Stephen Kelly's avatar
Stephen Kelly committed
678
679
680
681
682
//----------------------------------------------------------------------------
static const struct InstallPrefixNode : public cmGeneratorExpressionNode
{
  InstallPrefixNode() {}

683
  virtual bool GeneratesContent() const { return true; }
Stephen Kelly's avatar
Stephen Kelly committed
684
685
686
  virtual int NumExpectedParameters() const { return 0; }

  std::string Evaluate(const std::vector<std::string> &,
687
688
                       cmGeneratorExpressionContext *context,
                       const GeneratorExpressionContent *content,
Stephen Kelly's avatar
Stephen Kelly committed
689
690
                       cmGeneratorExpressionDAGChecker *) const
  {
691
692
693
    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
694
695
696
697
698
    return std::string();
  }

} installPrefixNode;

699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
//----------------------------------------------------------------------------
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,
813
814
                       const GeneratorExpressionContent *content,
                       cmGeneratorExpressionDAGChecker *) const
815
816
817
818
  {
    // Lookup the referenced target.
    std::string name = *parameters.begin();

819
    if (!cmGeneratorExpression::IsValidTargetName(name))
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
      {
      ::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();
      }
    if(target->GetType() >= cmTarget::UTILITY &&
      target->GetType() != cmTarget::UNKNOWN_LIBRARY)
      {
      ::reportError(context, content->GetOriginalExpression(),
                  "Target \"" + name + "\" is not an executable or library.");
      return std::string();
      }
    context->Targets.insert(target);

    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;
881
  else if (identifier == "1")
882
    return &oneNode;
883
  else if (identifier == "AND")
884
    return &andNode;
885
  else if (identifier == "OR")
886
    return &orNode;
887
  else if (identifier == "NOT")
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
    return &notNode;
  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;
911
912
913
914
915
916
917
918
  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;
919
920
  else if (identifier == "TARGET_PROPERTY")
    return &targetPropertyNode;
921
922
  else if (identifier == "TARGET_NAME")
    return &targetNameNode;
923
924
  else if (identifier == "TARGET_POLICY")
    return &targetPolicyNode;
925
926
927
928
  else if (identifier == "BUILD_INTERFACE")
    return &buildInterfaceNode;
  else if (identifier == "INSTALL_INTERFACE")
    return &installInterfaceNode;
Stephen Kelly's avatar
Stephen Kelly committed
929
930
  else if (identifier == "INSTALL_PREFIX")
    return &installPrefixNode;
931
  return 0;
932

933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
}

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

}

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

//----------------------------------------------------------------------------
std::string GeneratorExpressionContent::Evaluate(
952
953
                            cmGeneratorExpressionContext *context,
                            cmGeneratorExpressionDAGChecker *dagChecker) const
954
955
956
957
958
959
960
961
962
{
  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)
    {
963
    identifier += (*it)->Evaluate(context, dagChecker);
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
    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())
    {
982
983
984
985
986
987
988
989
990
991
992
993
994
995
    if (node->AcceptsSingleArbitraryContentParameter())
      {
      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);
      }
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
    return std::string();
    }

  if (node->AcceptsSingleArbitraryContentParameter())
    {
    std::string result;
    std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
                                        pit = this->ParamChildren.begin();
    const
    std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
                                        pend = this->ParamChildren.end();
    for ( ; pit != pend; ++pit)
      {
      if (!result.empty())
        {
        result += ",";
        }

      std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
                                                              = pit->begin();
      const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
                                                                = pit->end();
      for ( ; it != end; ++it)
        {
1020
1021
1022
1023
1024
1025
1026
1027
1028
        if (node->RequiresLiteralInput())
          {
          if ((*it)->GetType() != cmGeneratorExpressionEvaluator::Text)
            {
            reportError(context, this->GetOriginalExpression(),
                  "$<" + identifier + "> expression requires literal input.");
            return std::string();
            }
          }
1029
        result += (*it)->Evaluate(context, dagChecker);
1030
1031
1032
1033
1034
1035
        if (context->HadError)
          {
          return std::string();
          }
        }
      }
1036
1037
1038
1039
1040
1041
    if (node->RequiresLiteralInput())
      {
      std::vector<std::string> parameters;
      parameters.push_back(result);
      return node->Evaluate(parameters, context, this, dagChecker);
      }
1042
1043
1044
1045
    return result;
    }

  std::vector<std::string> parameters;
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
  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
{
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
  {
  std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
                                        pit = this->ParamChildren.begin();
  const
  std::vector<std::vector<cmGeneratorExpressionEvaluator*> >::const_iterator
                                        pend = this->ParamChildren.end();
  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)
      {
1078
      parameter += (*it)->Evaluate(context, dagChecker);
1079
1080
1081
1082
1083
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
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
      if (context->HadError)
        {
        return std::string();
        }
      }
    parameters.push_back(parameter);
    }
  }

  int numExpected = node->NumExpectedParameters();
  if ((numExpected != -1 && (unsigned int)numExpected != parameters.size()))
    {
    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, this->GetOriginalExpression(), e.str());
      }
    return std::string();
    }

  if (numExpected == -1 && parameters.empty())
    {
    reportError(context, this->GetOriginalExpression(), "$<" + identifier
                      + "> expression requires at least one parameter.");
    }
1119
  return std::string();
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
}

//----------------------------------------------------------------------------
static void deleteAll(const std::vector<cmGeneratorExpressionEvaluator*> &c)
{
  std::vector<cmGeneratorExpressionEvaluator*>::const_iterator it
                                                  = c.begin();
  const std::vector<cmGeneratorExpressionEvaluator*>::const_iterator end
                                                  = c.end();
  for ( ; it != end; ++it)
    {
    delete *it;
    }
}

//----------------------------------------------------------------------------
GeneratorExpressionContent::~GeneratorExpressionContent()
{
  deleteAll(this->IdentifierChildren);

  typedef std::vector<cmGeneratorExpressionEvaluator*> EvaluatorVector;
  typedef std::vector<cmGeneratorExpressionToken> TokenVector;
  std::vector<EvaluatorVector>::const_iterator pit =
                                                  this->ParamChildren.begin();
  const std::vector<EvaluatorVector>::const_iterator pend =
                                                  this->ParamChildren.end();
  for ( ; pit != pend; ++pit)
    {
    deleteAll(*pit);
    }
}