Updates will be applied on October 27th between 12pm - 12:45pm EDT (UTC-0400). Gitlab may be slow during the maintenance window.

cmGeneratorTarget.cxx 218 KB
Newer Older
1
2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
4
#include "cmGeneratorTarget.h"

5
#include "cmsys/RegularExpression.hxx"
6
7
8
9
#include <algorithm>
#include <assert.h>
#include <errno.h>
#include <iterator>
10
#include <memory>
11
12
13
14
15
#include <queue>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
16
#include <unordered_set>
wahikihiki's avatar
wahikihiki committed
17
#include <utility>
18

19
#include "cmAlgorithms.h"
20
#include "cmComputeLinkInformation.h"
21
#include "cmCustomCommand.h"
22
#include "cmCustomCommandGenerator.h"
23
#include "cmCustomCommandLines.h"
24
#include "cmGeneratorExpression.h"
25
#include "cmGeneratorExpressionContext.h"
26
#include "cmGeneratorExpressionDAGChecker.h"
27
#include "cmGeneratorExpressionNode.h"
28
29
30
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
31
#include "cmMessageType.h"
32
#include "cmPropertyMap.h"
33
#include "cmRange.h"
34
#include "cmSourceFile.h"
35
#include "cmSourceFileLocation.h"
36
#include "cmState.h"
37
#include "cmStringAlgorithms.h"
38
#include "cmSystemTools.h"
39
#include "cmTarget.h"
40
#include "cmTargetLinkLibraryType.h"
41
#include "cmTargetPropertyComputer.h"
42
#include "cmake.h"
43

44
class cmMessenger;
45

46
47
48
49
50
51
52
53
54
template <>
const char* cmTargetPropertyComputer::GetSources<cmGeneratorTarget>(
  cmGeneratorTarget const* tgt, cmMessenger* /* messenger */,
  cmListFileBacktrace const& /* context */)
{
  return tgt->GetSourcesProperty();
}

template <>
55
56
57
const char*
cmTargetPropertyComputer::ComputeLocationForBuild<cmGeneratorTarget>(
  cmGeneratorTarget const* tgt)
58
59
60
61
62
63
64
65
66
67
68
{
  return tgt->GetLocation("");
}

template <>
const char* cmTargetPropertyComputer::ComputeLocation<cmGeneratorTarget>(
  cmGeneratorTarget const* tgt, const std::string& config)
{
  return tgt->GetLocation(config);
}

69
70
class cmGeneratorTarget::TargetPropertyEntry
{
71
protected:
72
  static cmLinkImplItem NoLinkImplItem;
73

74
public:
75
76
  TargetPropertyEntry(cmLinkImplItem const& item)
    : LinkImplItem(item)
77
78
  {
  }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  virtual ~TargetPropertyEntry() = default;

  virtual const std::string& Evaluate(
    cmLocalGenerator* lg, const std::string& config, bool quiet = false,
    cmGeneratorTarget const* headTarget = nullptr,
    cmGeneratorTarget const* currentTarget = nullptr,
    cmGeneratorExpressionDAGChecker* dagChecker = nullptr,
    std::string const& language = std::string()) const = 0;
  virtual const std::string& Evaluate(
    cmLocalGenerator* lg, const std::string& config, bool quiet,
    cmGeneratorTarget const* headTarget,
    cmGeneratorExpressionDAGChecker* dagChecker,
    std::string const& language = std::string()) const = 0;

  virtual cmListFileBacktrace GetBacktrace() const = 0;
  virtual std::string const& GetInput() const = 0;
  virtual bool GetHadContextSensitiveCondition() const { return false; }

97
98
99
100
  cmLinkImplItem const& LinkImplItem;
};
cmLinkImplItem cmGeneratorTarget::TargetPropertyEntry::NoLinkImplItem;

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
class TargetPropertyEntryGenex : public cmGeneratorTarget::TargetPropertyEntry
{
public:
  TargetPropertyEntryGenex(std::unique_ptr<cmCompiledGeneratorExpression> cge,
                           cmLinkImplItem const& item = NoLinkImplItem)
    : cmGeneratorTarget::TargetPropertyEntry(item)
    , ge(std::move(cge))
  {
  }

  const std::string& Evaluate(
    cmLocalGenerator* lg, const std::string& config, bool quiet = false,
    cmGeneratorTarget const* headTarget = nullptr,
    cmGeneratorTarget const* currentTarget = nullptr,
    cmGeneratorExpressionDAGChecker* dagChecker = nullptr,
    std::string const& language = std::string()) const override
  {
    return this->ge->Evaluate(lg, config, quiet, headTarget, currentTarget,
                              dagChecker, language);
  }
  const std::string& Evaluate(
    cmLocalGenerator* lg, const std::string& config, bool quiet,
    cmGeneratorTarget const* headTarget,
    cmGeneratorExpressionDAGChecker* dagChecker,
    std::string const& language = std::string()) const override
  {
    return this->ge->Evaluate(lg, config, quiet, headTarget, dagChecker,
                              language);
  }

  cmListFileBacktrace GetBacktrace() const override
  {
    return this->ge->GetBacktrace();
  }

  std::string const& GetInput() const override { return this->ge->GetInput(); }

  bool GetHadContextSensitiveCondition() const override
  {
    return this->ge->GetHadContextSensitiveCondition();
  }

private:
  const std::unique_ptr<cmCompiledGeneratorExpression> ge;
};

class TargetPropertyEntryString : public cmGeneratorTarget::TargetPropertyEntry
{
public:
  TargetPropertyEntryString(std::string propertyValue,
                            cmListFileBacktrace backtrace,
                            cmLinkImplItem const& item = NoLinkImplItem)
    : cmGeneratorTarget::TargetPropertyEntry(item)
    , PropertyValue(std::move(propertyValue))
    , Backtrace(std::move(backtrace))
  {
  }

  const std::string& Evaluate(cmLocalGenerator*, const std::string&, bool,
                              cmGeneratorTarget const*,
                              cmGeneratorTarget const*,
                              cmGeneratorExpressionDAGChecker*,
                              std::string const&) const override
  {
    return this->PropertyValue;
  }
  const std::string& Evaluate(cmLocalGenerator*, const std::string&, bool,
                              cmGeneratorTarget const*,
                              cmGeneratorExpressionDAGChecker*,
                              std::string const&) const override
  {
    return this->PropertyValue;
  }

  cmListFileBacktrace GetBacktrace() const override { return this->Backtrace; }
  std::string const& GetInput() const override { return this->PropertyValue; }

private:
  std::string PropertyValue;
  cmListFileBacktrace Backtrace;
};

cmGeneratorTarget::TargetPropertyEntry* CreateTargetPropertyEntry(
  const std::string& propertyValue,
  cmListFileBacktrace backtrace = cmListFileBacktrace(),
  bool evaluateForBuildsystem = false)
{
  if (cmGeneratorExpression::Find(propertyValue) != std::string::npos) {
    cmGeneratorExpression ge(std::move(backtrace));
    std::unique_ptr<cmCompiledGeneratorExpression> cge =
      ge.Parse(propertyValue);
    cge->SetEvaluateForBuildsystem(evaluateForBuildsystem);
    return new TargetPropertyEntryGenex(std::move(cge));
  }

  return new TargetPropertyEntryString(propertyValue, backtrace);
}

199
void CreatePropertyGeneratorExpressions(
200
  cmStringRange entries, cmBacktraceRange backtraces,
201
202
  std::vector<cmGeneratorTarget::TargetPropertyEntry*>& items,
  bool evaluateForBuildsystem = false)
203
204
205
{
  std::vector<cmListFileBacktrace>::const_iterator btIt = backtraces.begin();
  for (std::vector<std::string>::const_iterator it = entries.begin();
206
       it != entries.end(); ++it, ++btIt) {
207
    items.push_back(
208
      CreateTargetPropertyEntry(*it, *btIt, evaluateForBuildsystem));
209
  }
210
211
}

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
namespace {
// Represent a target property entry after evaluating generator expressions
// and splitting up lists.
struct EvaluatedTargetPropertyEntry
{
  EvaluatedTargetPropertyEntry(cmLinkImplItem const& item,
                               cmListFileBacktrace bt)
    : LinkImplItem(item)
    , Backtrace(std::move(bt))
  {
  }

  // Move-only.
  EvaluatedTargetPropertyEntry(EvaluatedTargetPropertyEntry&&) = default;
  EvaluatedTargetPropertyEntry(EvaluatedTargetPropertyEntry const&) = delete;
  EvaluatedTargetPropertyEntry& operator=(EvaluatedTargetPropertyEntry&&) =
    delete;
  EvaluatedTargetPropertyEntry& operator=(
    EvaluatedTargetPropertyEntry const&) = delete;

  cmLinkImplItem const& LinkImplItem;
  cmListFileBacktrace Backtrace;
  std::vector<std::string> Values;
  bool ContextDependent = false;
};

EvaluatedTargetPropertyEntry EvaluateTargetPropertyEntry(
  cmGeneratorTarget const* thisTarget, std::string const& config,
  std::string const& lang, cmGeneratorExpressionDAGChecker* dagChecker,
  cmGeneratorTarget::TargetPropertyEntry* entry)
{
  EvaluatedTargetPropertyEntry ee(entry->LinkImplItem, entry->GetBacktrace());
  cmSystemTools::ExpandListArgument(
    entry->Evaluate(thisTarget->GetLocalGenerator(), config, false, thisTarget,
                    dagChecker, lang),
    ee.Values);
  if (entry->GetHadContextSensitiveCondition()) {
    ee.ContextDependent = true;
  }
  return ee;
}

std::vector<EvaluatedTargetPropertyEntry> EvaluateTargetPropertyEntries(
  cmGeneratorTarget const* thisTarget, std::string const& config,
  std::string const& lang, cmGeneratorExpressionDAGChecker* dagChecker,
  std::vector<cmGeneratorTarget::TargetPropertyEntry*> const& in)
{
  std::vector<EvaluatedTargetPropertyEntry> out;
  out.reserve(in.size());
  for (cmGeneratorTarget::TargetPropertyEntry* entry : in) {
    out.emplace_back(EvaluateTargetPropertyEntry(thisTarget, config, lang,
                                                 dagChecker, entry));
  }
  return out;
}
}

269
cmGeneratorTarget::cmGeneratorTarget(cmTarget* t, cmLocalGenerator* lg)
270
  : Target(t)
271
  , FortranModuleDirectoryCreated(false)
272
273
  , SourceFileFlagsConstructed(false)
  , PolicyWarnedCMP0022(false)
274
  , PolicyReportedCMP0069(false)
275
276
277
278
  , DebugIncludesDone(false)
  , DebugCompileOptionsDone(false)
  , DebugCompileFeaturesDone(false)
  , DebugCompileDefinitionsDone(false)
279
  , DebugLinkOptionsDone(false)
280
  , DebugLinkDirectoriesDone(false)
281
282
283
  , DebugSourcesDone(false)
  , LinkImplementationLanguageIsContextDependent(true)
  , UtilityItemsDone(false)
284
285
{
  this->Makefile = this->Target->GetMakefile();
286
  this->LocalGenerator = lg;
287
  this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
288

289
290
  this->GlobalGenerator->ComputeTargetObjectDirectory(this);

291
292
293
  CreatePropertyGeneratorExpressions(t->GetIncludeDirectoriesEntries(),
                                     t->GetIncludeDirectoriesBacktraces(),
                                     this->IncludeDirectoriesEntries);
294

295
296
297
  CreatePropertyGeneratorExpressions(t->GetCompileOptionsEntries(),
                                     t->GetCompileOptionsBacktraces(),
                                     this->CompileOptionsEntries);
298

299
300
301
  CreatePropertyGeneratorExpressions(t->GetCompileFeaturesEntries(),
                                     t->GetCompileFeaturesBacktraces(),
                                     this->CompileFeaturesEntries);
302

303
304
305
  CreatePropertyGeneratorExpressions(t->GetCompileDefinitionsEntries(),
                                     t->GetCompileDefinitionsBacktraces(),
                                     this->CompileDefinitionsEntries);
306

307
308
309
310
  CreatePropertyGeneratorExpressions(t->GetLinkOptionsEntries(),
                                     t->GetLinkOptionsBacktraces(),
                                     this->LinkOptionsEntries);

311
312
313
314
  CreatePropertyGeneratorExpressions(t->GetLinkDirectoriesEntries(),
                                     t->GetLinkDirectoriesBacktraces(),
                                     this->LinkDirectoriesEntries);

315
316
317
  CreatePropertyGeneratorExpressions(t->GetSourceEntries(),
                                     t->GetSourceBacktraces(),
                                     this->SourceEntries, true);
318

319
  this->PolicyMap = t->GetPolicyMap();
320
321
}

322
323
cmGeneratorTarget::~cmGeneratorTarget()
{
324
  cmDeleteAll(this->IncludeDirectoriesEntries);
325
  cmDeleteAll(this->CompileOptionsEntries);
326
  cmDeleteAll(this->CompileFeaturesEntries);
327
  cmDeleteAll(this->CompileDefinitionsEntries);
328
  cmDeleteAll(this->LinkOptionsEntries);
329
  cmDeleteAll(this->LinkDirectoriesEntries);
330
  cmDeleteAll(this->SourceEntries);
331
332
333
  cmDeleteAll(this->LinkInformation);
}

334
335
336
const char* cmGeneratorTarget::GetSourcesProperty() const
{
  std::vector<std::string> values;
337
  for (TargetPropertyEntry* se : this->SourceEntries) {
338
    values.push_back(se->GetInput());
339
340
341
  }
  static std::string value;
  value.clear();
342
  value = cmJoin(values, ";");
343
344
345
  return value.c_str();
}

346
347
348
349
350
cmGlobalGenerator* cmGeneratorTarget::GetGlobalGenerator() const
{
  return this->GetLocalGenerator()->GetGlobalGenerator();
}

351
352
353
354
355
cmLocalGenerator* cmGeneratorTarget::GetLocalGenerator() const
{
  return this->LocalGenerator;
}

356
cmStateEnums::TargetType cmGeneratorTarget::GetType() const
357
358
359
360
{
  return this->Target->GetType();
}

361
const std::string& cmGeneratorTarget::GetName() const
362
363
364
365
{
  return this->Target->GetName();
}

366
367
std::string cmGeneratorTarget::GetExportName() const
{
368
  const char* exportName = this->GetProperty("EXPORT_NAME");
369

370
371
  if (exportName && *exportName) {
    if (!cmGeneratorExpression::IsValidTargetName(exportName)) {
372
373
374
      std::ostringstream e;
      e << "EXPORT_NAME property \"" << exportName << "\" for \""
        << this->GetName() << "\": is not valid.";
375
      cmSystemTools::Error(e.str());
376
377
      return "";
    }
378
379
    return exportName;
  }
380
381
382
  return this->GetName();
}

383
const char* cmGeneratorTarget::GetProperty(const std::string& prop) const
384
{
385
386
387
  if (!cmTargetPropertyComputer::PassesWhitelist(
        this->GetType(), prop, this->Makefile->GetMessenger(),
        this->GetBacktrace())) {
Daniel Pfeifer's avatar
Daniel Pfeifer committed
388
    return nullptr;
389
390
391
392
393
394
  }
  if (const char* result = cmTargetPropertyComputer::GetProperty(
        this, prop, this->Makefile->GetMessenger(), this->GetBacktrace())) {
    return result;
  }
  if (cmSystemTools::GetFatalErrorOccured()) {
Daniel Pfeifer's avatar
Daniel Pfeifer committed
395
    return nullptr;
396
  }
397
398
399
  return this->Target->GetProperty(prop);
}

400
401
402
403
404
405
406
407
408
const char* cmGeneratorTarget::GetSafeProperty(const std::string& prop) const
{
  const char* ret = this->GetProperty(prop);
  if (!ret) {
    return "";
  }
  return ret;
}

409
410
const char* cmGeneratorTarget::GetOutputTargetType(
  cmStateEnums::ArtifactType artifact) const
411
{
412
  switch (this->GetType()) {
413
    case cmStateEnums::SHARED_LIBRARY:
414
      if (this->IsDLLPlatform()) {
415
416
417
418
419
420
421
        switch (artifact) {
          case cmStateEnums::RuntimeBinaryArtifact:
            // A DLL shared library is treated as a runtime target.
            return "RUNTIME";
          case cmStateEnums::ImportLibraryArtifact:
            // A DLL import library is treated as an archive target.
            return "ARCHIVE";
422
        }
423
      } else {
424
425
426
        // For non-DLL platforms shared libraries are treated as
        // library targets.
        return "LIBRARY";
427
      }
428
      break;
429
    case cmStateEnums::STATIC_LIBRARY:
430
431
      // Static libraries are always treated as archive targets.
      return "ARCHIVE";
432
    case cmStateEnums::MODULE_LIBRARY:
433
434
      switch (artifact) {
        case cmStateEnums::RuntimeBinaryArtifact:
435
          // Module libraries are always treated as library targets.
436
437
          return "LIBRARY";
        case cmStateEnums::ImportLibraryArtifact:
438
          // Module import libraries are treated as archive targets.
439
          return "ARCHIVE";
440
      }
441
      break;
442
443
444
    case cmStateEnums::OBJECT_LIBRARY:
      // Object libraries are always treated as object targets.
      return "OBJECT";
445
    case cmStateEnums::EXECUTABLE:
446
447
448
449
450
451
452
      switch (artifact) {
        case cmStateEnums::RuntimeBinaryArtifact:
          // Executables are always treated as runtime targets.
          return "RUNTIME";
        case cmStateEnums::ImportLibraryArtifact:
          // Executable import libraries are treated as archive targets.
          return "ARCHIVE";
453
      }
454
      break;
455
456
    default:
      break;
457
  }
458
459
460
  return "";
}

461
462
std::string cmGeneratorTarget::GetOutputName(
  const std::string& config, cmStateEnums::ArtifactType artifact) const
463
{
464
  // Lookup/compute/cache the output name for this configuration.
465
  OutputNameKey key(config, artifact);
466
467
  cmGeneratorTarget::OutputNameMapType::iterator i =
    this->OutputNameMap.find(key);
468
  if (i == this->OutputNameMap.end()) {
469
470
471
    // Add empty name in map to detect potential recursion.
    OutputNameMapType::value_type entry(key, "");
    i = this->OutputNameMap.insert(entry).first;
472

473
474
    // Compute output name.
    std::vector<std::string> props;
475
    std::string type = this->GetOutputTargetType(artifact);
476
    std::string configUpper = cmSystemTools::UpperCase(config);
477
    if (!type.empty() && !configUpper.empty()) {
478
479
      // <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME_<CONFIG>
      props.push_back(type + "_OUTPUT_NAME_" + configUpper);
480
481
    }
    if (!type.empty()) {
482
483
      // <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME
      props.push_back(type + "_OUTPUT_NAME");
484
485
    }
    if (!configUpper.empty()) {
486
487
488
489
      // OUTPUT_NAME_<CONFIG>
      props.push_back("OUTPUT_NAME_" + configUpper);
      // <CONFIG>_OUTPUT_NAME
      props.push_back(configUpper + "_OUTPUT_NAME");
490
    }
491
    // OUTPUT_NAME
wahikihiki's avatar
wahikihiki committed
492
    props.emplace_back("OUTPUT_NAME");
493

494
    std::string outName;
495
496
    for (std::string const& p : props) {
      if (const char* outNameProp = this->GetProperty(p)) {
497
498
499
        outName = outNameProp;
        break;
      }
500
    }
501

502
    if (outName.empty()) {
503
      outName = this->GetName();
504
    }
505
506
507

    // Now evaluate genex and update the previously-prepared map entry.
    cmGeneratorExpression ge;
508
    std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(outName);
509
    i->second = cge->Evaluate(this->LocalGenerator, config);
510
  } else if (i->second.empty()) {
511
512
    // An empty map entry indicates we have been called recursively
    // from the above block.
513
    this->LocalGenerator->GetCMakeInstance()->IssueMessage(
514
      MessageType::FATAL_ERROR,
515
      "Target '" + this->GetName() + "' OUTPUT_NAME depends on itself.",
516
      this->GetBacktrace());
517
  }
518
  return i->second;
519
520
}

521
522
523
524
std::string cmGeneratorTarget::GetFilePrefix(
  const std::string& config, cmStateEnums::ArtifactType artifact) const
{
  if (this->IsImported()) {
525
    const char* prefix = this->GetFilePrefixInternal(config, artifact);
526
527
528
529
530
531
532
533
534
535
536
537

    return prefix ? prefix : std::string();
  }

  std::string prefix, suffix, base;
  this->GetFullNameInternal(config, artifact, prefix, base, suffix);
  return prefix;
}
std::string cmGeneratorTarget::GetFileSuffix(
  const std::string& config, cmStateEnums::ArtifactType artifact) const
{
  if (this->IsImported()) {
538
    const char* suffix = this->GetFileSuffixInternal(config, artifact);
539
540
541
542
543
544
545
546
547

    return suffix ? suffix : std::string();
  }

  std::string prefix, suffix, base;
  this->GetFullNameInternal(config, artifact, prefix, base, suffix);
  return suffix;
}

548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
std::string cmGeneratorTarget::GetFilePostfix(const std::string& config) const
{
  const char* postfix = nullptr;
  if (!config.empty()) {
    std::string configProp = cmSystemTools::UpperCase(config);
    configProp += "_POSTFIX";
    postfix = this->GetProperty(configProp);
    // Mac application bundles and frameworks have no postfix.
    if (!this->IsImported() && postfix &&
        (this->IsAppBundleOnApple() || this->IsFrameworkOnApple())) {
      postfix = nullptr;
    }
  }
  return postfix ? postfix : std::string();
}

564
const char* cmGeneratorTarget::GetFilePrefixInternal(
565
566
  std::string const& config, cmStateEnums::ArtifactType artifact,
  const std::string& language) const
567
568
569
570
571
572
573
574
575
576
577
578
579
580
{
  // no prefix for non-main target types.
  if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
      this->GetType() != cmStateEnums::SHARED_LIBRARY &&
      this->GetType() != cmStateEnums::MODULE_LIBRARY &&
      this->GetType() != cmStateEnums::EXECUTABLE) {
    return nullptr;
  }

  const bool isImportedLibraryArtifact =
    (artifact == cmStateEnums::ImportLibraryArtifact);

  // Return an empty prefix for the import library if this platform
  // does not support import libraries.
581
  if (isImportedLibraryArtifact && !this->NeedImportLibraryName(config)) {
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
    return nullptr;
  }

  // The implib option is only allowed for shared libraries, module
  // libraries, and executables.
  if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
      this->GetType() != cmStateEnums::MODULE_LIBRARY &&
      this->GetType() != cmStateEnums::EXECUTABLE) {
    artifact = cmStateEnums::RuntimeBinaryArtifact;
  }

  // Compute prefix value.
  const char* targetPrefix =
    (isImportedLibraryArtifact ? this->GetProperty("IMPORT_PREFIX")
                               : this->GetProperty("PREFIX"));

  if (!targetPrefix) {
    const char* prefixVar = this->Target->GetPrefixVariableInternal(artifact);
    if (!language.empty() && prefixVar && *prefixVar) {
      std::string langPrefix = prefixVar + std::string("_") + language;
      targetPrefix = this->Makefile->GetDefinition(langPrefix);
    }

    // if there is no prefix on the target nor specific language
    // use the cmake definition.
    if (!targetPrefix && prefixVar) {
      targetPrefix = this->Makefile->GetDefinition(prefixVar);
    }
  }

  return targetPrefix;
}
const char* cmGeneratorTarget::GetFileSuffixInternal(
615
616
  std::string const& config, cmStateEnums::ArtifactType artifact,
  const std::string& language) const
617
618
619
620
621
622
623
624
625
626
627
628
629
630
{
  // no suffix for non-main target types.
  if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
      this->GetType() != cmStateEnums::SHARED_LIBRARY &&
      this->GetType() != cmStateEnums::MODULE_LIBRARY &&
      this->GetType() != cmStateEnums::EXECUTABLE) {
    return nullptr;
  }

  const bool isImportedLibraryArtifact =
    (artifact == cmStateEnums::ImportLibraryArtifact);

  // Return an empty suffix for the import library if this platform
  // does not support import libraries.
631
  if (isImportedLibraryArtifact && !this->NeedImportLibraryName(config)) {
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
    return nullptr;
  }

  // The implib option is only allowed for shared libraries, module
  // libraries, and executables.
  if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
      this->GetType() != cmStateEnums::MODULE_LIBRARY &&
      this->GetType() != cmStateEnums::EXECUTABLE) {
    artifact = cmStateEnums::RuntimeBinaryArtifact;
  }

  // Compute suffix value.
  const char* targetSuffix =
    (isImportedLibraryArtifact ? this->GetProperty("IMPORT_SUFFIX")
                               : this->GetProperty("SUFFIX"));

  if (!targetSuffix) {
    const char* suffixVar = this->Target->GetSuffixVariableInternal(artifact);
    if (!language.empty() && suffixVar && *suffixVar) {
      std::string langSuffix = suffixVar + std::string("_") + language;
      targetSuffix = this->Makefile->GetDefinition(langSuffix);
    }

    // if there is no suffix on the target nor specific language
    // use the cmake definition.
    if (!targetSuffix && suffixVar) {
      targetSuffix = this->Makefile->GetDefinition(suffixVar);
    }
  }

  return targetSuffix;
}

665
666
667
668
669
670
671
void cmGeneratorTarget::ClearSourcesCache()
{
  this->KindedSourcesMap.clear();
  this->LinkImplementationLanguageIsContextDependent = true;
  this->Objects.clear();
}

672
void cmGeneratorTarget::AddSourceCommon(const std::string& src, bool before)
673
{
674
675
676
  this->SourceEntries.insert(
    before ? this->SourceEntries.begin() : this->SourceEntries.end(),
    CreateTargetPropertyEntry(src, this->Makefile->GetBacktrace(), true));
677
  this->ClearSourcesCache();
678
679
}

680
void cmGeneratorTarget::AddSource(const std::string& src, bool before)
681
{
682
683
  this->Target->AddSource(src, before);
  this->AddSourceCommon(src, before);
684
685
}

686
687
688
void cmGeneratorTarget::AddTracedSources(std::vector<std::string> const& srcs)
{
  this->Target->AddTracedSources(srcs);
689
  if (!srcs.empty()) {
690
    this->AddSourceCommon(cmJoin(srcs, ";"));
691
  }
692
693
}

694
695
696
697
void cmGeneratorTarget::AddIncludeDirectory(const std::string& src,
                                            bool before)
{
  this->Target->InsertInclude(src, this->Makefile->GetBacktrace(), before);
698
  this->IncludeDirectoriesEntries.insert(
699
700
    before ? this->IncludeDirectoriesEntries.begin()
           : this->IncludeDirectoriesEntries.end(),
701
    CreateTargetPropertyEntry(src, this->Makefile->GetBacktrace(), true));
702
703
}

704
705
std::vector<cmSourceFile*> const* cmGeneratorTarget::GetSourceDepends(
  cmSourceFile const* sf) const
706
{
707
  SourceEntriesType::const_iterator i = this->SourceDepends.find(sf);
708
  if (i != this->SourceDepends.end()) {
709
    return &i->second.Depends;
710
  }
Daniel Pfeifer's avatar
Daniel Pfeifer committed
711
  return nullptr;
712
713
}

714
715
716
717
718
719
720
721
namespace {
void handleSystemIncludesDep(cmLocalGenerator* lg,
                             cmGeneratorTarget const* depTgt,
                             const std::string& config,
                             cmGeneratorTarget const* headTarget,
                             cmGeneratorExpressionDAGChecker* dagChecker,
                             std::vector<std::string>& result,
                             bool excludeImported, std::string const& language)
722
723
{
  if (const char* dirs =
724
        depTgt->GetProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES")) {
725
    cmGeneratorExpression ge;
726
727
    cmSystemTools::ExpandListArgument(
      ge.Parse(dirs)->Evaluate(lg, config, false, headTarget, depTgt,
728
                               dagChecker, language),
729
730
731
      result);
  }
  if (!depTgt->IsImported() || excludeImported) {
732
    return;
733
  }
734
735

  if (const char* dirs =
736
        depTgt->GetProperty("INTERFACE_INCLUDE_DIRECTORIES")) {
737
    cmGeneratorExpression ge;
738
739
    cmSystemTools::ExpandListArgument(
      ge.Parse(dirs)->Evaluate(lg, config, false, headTarget, depTgt,
740
                               dagChecker, language),
741
742
      result);
  }
743
}
744
}
745

746
/* clang-format off */
747
#define IMPLEMENT_VISIT(KIND)                                                 \
748
  do {                                                                        \
749
    KindedSources const& kinded = this->GetKindedSources(config);             \
750
751
    for (SourceAndKind const& s : kinded.Sources) {                           \
      if (s.Kind == KIND) {                                                   \
752
        data.push_back(s.Source.Value);                                       \
753
      }                                                                       \
754
    }                                                                         \
755
  } while (false)
756
/* clang-format on */
757

758
759
void cmGeneratorTarget::GetObjectSources(
  std::vector<cmSourceFile const*>& data, const std::string& config) const
760
{
761
  IMPLEMENT_VISIT(SourceKindObjectSource);
762

763
  if (!this->Objects.empty()) {
764
    return;
765
  }
766

767
768
  for (cmSourceFile const* it : data) {
    this->Objects[it];
769
  }
770
771

  this->LocalGenerator->ComputeObjectFilenames(this->Objects, this);
772
773
}

774
void cmGeneratorTarget::ComputeObjectMapping()
775
{
776
  if (!this->Objects.empty()) {
777
    return;
778
  }
779
780
781

  std::vector<std::string> configs;
  this->Makefile->GetConfigurations(configs);
782
  if (configs.empty()) {
783
    configs.emplace_back();
784
  }
785
  for (std::string const& c : configs) {
786
    std::vector<cmSourceFile const*> sourceFiles;
787
    this->GetObjectSources(sourceFiles, c);
788
  }
789
790
}

791
792
793
const char* cmGeneratorTarget::GetFeature(const std::string& feature,
                                          const std::string& config) const
{
794
  if (!config.empty()) {
795
796
797
    std::string featureConfig = feature;
    featureConfig += "_";
    featureConfig += cmSystemTools::UpperCase(config);
798
    if (const char* value = this->GetProperty(featureConfig)) {
799
800
      return value;
    }
801
802
  }
  if (const char* value = this->GetProperty(feature)) {
803
    return value;
804
  }
805
  return this->LocalGenerator->GetFeature(feature, config);
806
807
}

808
809
810
811
812
813
814
815
816
817
818
819
820
const char* cmGeneratorTarget::GetLinkPIEProperty(
  const std::string& config) const
{
  static std::string PICValue;

  PICValue = this->GetLinkInterfaceDependentStringAsBoolProperty(
    "POSITION_INDEPENDENT_CODE", config);

  if (PICValue == "(unset)") {
    // POSITION_INDEPENDENT_CODE is not set
    return nullptr;
  }

821
822
823
824
  auto status = this->GetPolicyStatusCMP0083();
  return (status != cmPolicies::WARN && status != cmPolicies::OLD)
    ? PICValue.c_str()
    : nullptr;
825
826
}

827
828
bool cmGeneratorTarget::IsIPOEnabled(std::string const& lang,
                                     std::string const& config) const
829
{
830
  const char* feature = "INTERPROCEDURAL_OPTIMIZATION";
831
832
833
834
835
836
837
  const bool result = cmSystemTools::IsOn(this->GetFeature(feature, config));

  if (!result) {
    // 'INTERPROCEDURAL_OPTIMIZATION' is off, no need to check policies
    return false;
  }

838
839
840
841
842
  if (lang != "C" && lang != "CXX" && lang != "Fortran") {
    // We do not define IPO behavior for other languages.
    return false;
  }

843
844
845
  cmPolicies::PolicyStatus cmp0069 = this->GetPolicyStatusCMP0069();

  if (cmp0069 == cmPolicies::OLD || cmp0069 == cmPolicies::WARN) {
846
    if (this->Makefile->IsOn("_CMAKE_" + lang + "_IPO_LEGACY_BEHAVIOR")) {
847
848
849
850
851
852
      return true;
    }
    if (this->PolicyReportedCMP0069) {
      // problem is already reported, no need to issue a message
      return false;
    }
853
854
855
    const bool in_try_compile =
      this->LocalGenerator->GetCMakeInstance()->GetIsInTryCompile();
    if (cmp0069 == cmPolicies::WARN && !in_try_compile) {
856
857
858
859
860
      std::ostringstream w;
      w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0069) << "\n";
      w << "INTERPROCEDURAL_OPTIMIZATION property will be ignored for target "
        << "'" << this->GetName() << "'.";
      this->LocalGenerator->GetCMakeInstance()->IssueMessage(
861
        MessageType::AUTHOR_WARNING, w.str(), this->GetBacktrace());
862
863
864
865
866
867
868

      this->PolicyReportedCMP0069 = true;
    }
    return false;
  }

  // Note: check consistency with messages from CheckIPOSupported
Daniel Pfeifer's avatar
Daniel Pfeifer committed
869
  const char* message = nullptr;
870
  if (!this->Makefile->IsOn("_CMAKE_" + lang + "_IPO_SUPPORTED_BY_CMAKE")) {
871
    message = "CMake doesn't support IPO for current compiler";
872
873
  } else if (!this->Makefile->IsOn("_CMAKE_" + lang +
                                   "_IPO_MAY_BE_SUPPORTED_BY_COMPILER")) {
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
    message = "Compiler doesn't support IPO";
  } else if (!this->GlobalGenerator->IsIPOSupported()) {
    message = "CMake doesn't support IPO for current generator";
  }

  if (!message) {
    // No error/warning messages
    return true;
  }

  if (this->PolicyReportedCMP0069) {
    // problem is already reported, no need to issue a message
    return false;
  }

  this->PolicyReportedCMP0069 = true;

  this->LocalGenerator->GetCMakeInstance()->IssueMessage(
892
    MessageType::FATAL_ERROR, message, this->GetBacktrace());
893
  return false;
894
895
}

896
const std::string& cmGeneratorTarget::GetObjectName(cmSourceFile const* file)
897
{
898
899
  this->ComputeObjectMapping();
  return this->Objects[file];
900
901
}

902
903
904
905
906
907
908
909
910
const char* cmGeneratorTarget::GetCustomObjectExtension() const
{
  static std::string extension;
  const bool has_ptx_extension =
    this->GetPropertyAsBool("CUDA_PTX_COMPILATION");
  if (has_ptx_extension) {
    extension = ".ptx";
    return extension.c_str();
  }
Daniel Pfeifer's avatar
Daniel Pfeifer committed
911
  return nullptr;
912
913
}

914
void cmGeneratorTarget::AddExplicitObjectName(cmSourceFile const* sf)
915
916
917
918
919
920
{
  this->ExplicitObjectName.insert(sf);
}

bool cmGeneratorTarget::HasExplicitObjectName(cmSourceFile const* file) const
{
921
  const_cast<cmGeneratorTarget*>(this)->ComputeObjectMapping();
922
923
  std::set<cmSourceFile const*>::const_iterator it =
    this->ExplicitObjectName.find(file);
924
925
926
  return it != this->ExplicitObjectName.end();
}

927
928
929
void cmGeneratorTarget::GetModuleDefinitionSources(
  std::vector<cmSourceFile const*>& data, const std::string& config) const
{
930
  IMPLEMENT_VISIT(SourceKindModuleDefinition);
931
932
}

933
934
void cmGeneratorTarget::GetHeaderSources(
  std::vector<cmSourceFile const*>& data, const std::string& config) const
935
{
936
  IMPLEMENT_VISIT(SourceKindHeader);
937
938
}

939
940
void cmGeneratorTarget::GetExtraSources(std::vector<cmSourceFile const*>& data,
                                        const std::string& config) const
941
{
942
  IMPLEMENT_VISIT(SourceKindExtra);
943
944
}

945
946
void cmGeneratorTarget::GetCustomCommands(
  std::vector<cmSourceFile const*>& data, const std::string& config) const
947
{
948
  IMPLEMENT_VISIT(SourceKindCustomCommand);
949
950
}

951
952
void cmGeneratorTarget::GetExternalObjects(
  std::vector<cmSourceFile const*>& data, const std::string& config) const
953
{
954
  IMPLEMENT_VISIT(SourceKindExternalObject);
955
956
}

957
void cmGeneratorTarget::GetExpectedResxHeaders(std::set<std::string>& headers,
958
                                               const std::string& config) const
959
{
960
961
  KindedSources const& kinded = this->GetKindedSources(config);
  headers = kinded.ExpectedResxHeaders;
962
963
}

964
void cmGeneratorTarget::GetResxSources(std::vector<cmSourceFile const*>& data,
965
                                       const std::string& config) const
966
{
967
  IMPLEMENT_VISIT(SourceKindResx);
968
969
}

970
971
void cmGeneratorTarget::GetAppManifest(std::vector<cmSourceFile const*>& data,
                                       const std::string& config) const
972
{
973
  IMPLEMENT_VISIT(SourceKindAppManifest);
974
975
}

976
977
void cmGeneratorTarget::GetManifests(std::vector<cmSourceFile const*>& data,
                                     const std::string& config) const
978
{
979
  IMPLEMENT_VISIT(SourceKindManifest);
980
981
}

982
983
void cmGeneratorTarget::GetCertificates(std::vector<cmSourceFile const*>& data,
                                        const std::string& config) const
984
{
985
  IMPLEMENT_VISIT(SourceKindCertificate);
986
987
}

988
989
void cmGeneratorTarget::GetExpectedXamlHeaders(std::set<std::string>& headers,
                                               const std::string& config) const
990
{
991
992
  KindedSources const& kinded = this->GetKindedSources(config);
  headers = kinded.ExpectedXamlHeaders;
993
994
}

995
996
void cmGeneratorTarget::GetExpectedXamlSources(std::set<std::string>& srcs,
                                               const std::string& config) const
997
{
998
999
  KindedSources const& kinded = this->GetKindedSources(config);
  srcs = kinded.ExpectedXamlSources;
1000
1001
1002
1003
}

std::set<cmLinkItem> const& cmGeneratorTarget::GetUtilityItems() const
{
1004
  if (!this->UtilityItemsDone) {
1005
    this->UtilityItemsDone = true;
1006
1007
    std::set<BT<std::string>> const& utilities = this->GetUtilities();
    for (BT<std::string> const& i : utilities) {
1008
      if (cmGeneratorTarget* gt =
1009
1010
            this->LocalGenerator->FindGeneratorTargetToUse(i.Value)) {
        this->UtilityItems.insert(cmLinkItem(gt, i.Backtrace));
1011
      } else {
1012
        this->UtilityItems.insert(cmLinkItem(i.Value, i.Backtrace));
1013
      }
1014
    }
1015
  }
1016
  return this->UtilityItems;
1017
1018
}

1019
void cmGeneratorTarget::GetXamlSources(std::vector<cmSourceFile const*>& data,
1020
                                       const std::string& config) const
1021
{
1022
  IMPLEMENT_VISIT(SourceKindXaml);
1023
1024
}

1025
1026
1027
const char* cmGeneratorTarget::GetLocation(const std::string& config) const
{
  static std::string location;
1028
  if (this->IsImported()) {
1029
1030
    location = this->Target->ImportedGetFullPath(
      config, cmStateEnums::RuntimeBinaryArtifact);
1031
  } else {
1032
    location = this->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
1033
  }
1034
1035
1036
  return location.c_str();
}

1037
1038
std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPreBuildCommands()
  const
1039
1040
1041
1042
{
  return this->Target->GetPreBuildCommands();
}

1043
1044
std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPreLinkCommands()
  const
1045
1046
1047
1048
{
  return this->Target->GetPreLinkCommands();
}

1049
1050
std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPostBuildCommands()
  const
1051
1052
1053
1054
{
  return this->Target->GetPostBuildCommands();
}

1055
1056
1057
1058
1059
bool cmGeneratorTarget::IsImported() const
{
  return this->Target->IsImported();
}

1060
1061
1062
1063
1064
bool cmGeneratorTarget::IsImportedGloballyVisible() const
{
  return this->Target->IsImportedGloballyVisible();
}

1065
1066