Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmGeneratorTarget.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#include "cmGeneratorTarget.h"
4
5
#include <algorithm>
6
#include <array>
7
#include <cassert>
8
#include <cctype>
9
#include <cstddef>
10
#include <cstdio>
11
#include <cstring>
12
#include <initializer_list>
13
#include <sstream>
14
#include <unordered_set>
15
#include <utility>
16
17
#include <cm/memory>
18
#include <cm/optional>
19
#include <cm/string_view>
20
#include <cmext/algorithm>
21
#include <cmext/string_view>
22
23
#include "cmAlgorithms.h"
24
#include "cmComputeLinkInformation.h" // IWYU pragma: keep
25
#include "cmCryptoHash.h"
26
#include "cmCxxModuleMetadata.h"
27
#include "cmCxxModuleUsageEffects.h"
28
#include "cmExperimental.h"
29
#include "cmFileSet.h"
30
#include "cmFileTimes.h"
31
#include "cmGenExContext.h"
32
#include "cmGeneratedFileStream.h"
33
#include "cmGeneratorExpression.h"
34
#include "cmGeneratorExpressionDAGChecker.h"
35
#include "cmGeneratorOptions.h"
36
#include "cmGlobalGenerator.h"
37
#include "cmList.h"
38
#include "cmLocalGenerator.h"
39
#include "cmMakefile.h"
40
#include "cmMessageType.h"
41
#include "cmOutputConverter.h"
42
#include "cmPropertyMap.h"
43
#include "cmRulePlaceholderExpander.h"
44
#include "cmSourceFile.h"
45
#include "cmSourceFileLocation.h"
46
#include "cmSourceFileLocationKind.h"
47
#include "cmStandardLevel.h"
48
#include "cmStandardLevelResolver.h"
49
#include "cmState.h"
50
#include "cmStateTypes.h"
51
#include "cmStringAlgorithms.h"
52
#include "cmSyntheticTargetCache.h"
53
#include "cmSystemTools.h"
54
#include "cmTarget.h"
55
#include "cmTargetLinkLibraryType.h"
56
#include "cmTargetPropertyComputer.h"
57
#include "cmTargetTraceDependencies.h"
58
#include "cmake.h"
59
60
namespace {
61
using UseTo = cmGeneratorTarget::UseTo;
62
}
63
64
template <>
65
cmValue cmTargetPropertyComputer::GetSources<cmGeneratorTarget>(
66
  cmGeneratorTarget const* tgt)
67
0
{
68
0
  return tgt->GetSourcesProperty();
69
0
}
70
71
template <>
72
std::string const&
73
cmTargetPropertyComputer::ImportedLocation<cmGeneratorTarget>(
74
  cmGeneratorTarget const* tgt, std::string const& config)
75
0
{
76
0
  return tgt->GetLocation(config);
77
0
}
78
79
static void CreatePropertyGeneratorExpressions(
80
  cmake& cmakeInstance, cmBTStringRange entries,
81
  std::vector<std::unique_ptr<cmGeneratorTarget::TargetPropertyEntry>>& items,
82
  bool evaluateForBuildsystem = false)
83
0
{
84
0
  for (auto const& entry : entries) {
85
0
    items.emplace_back(cmGeneratorTarget::TargetPropertyEntry::Create(
86
0
      cmakeInstance, entry, evaluateForBuildsystem));
87
0
  }
88
0
}
89
90
cmGeneratorTarget::cmGeneratorTarget(cmTarget* t, cmLocalGenerator* lg)
91
0
  : Target(t)
92
0
{
93
0
  this->Makefile = this->Target->GetMakefile();
94
0
  this->LocalGenerator = lg;
95
0
  this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
96
97
0
  this->GlobalGenerator->ComputeTargetObjectDirectory(this);
98
99
0
  CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
100
0
                                     t->GetIncludeDirectoriesEntries(),
101
0
                                     this->IncludeDirectoriesEntries);
102
103
0
  CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
104
0
                                     t->GetCompileOptionsEntries(),
105
0
                                     this->CompileOptionsEntries);
106
107
0
  CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
108
0
                                     t->GetCompileFeaturesEntries(),
109
0
                                     this->CompileFeaturesEntries);
110
111
0
  CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
112
0
                                     t->GetCompileDefinitionsEntries(),
113
0
                                     this->CompileDefinitionsEntries);
114
115
0
  CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
116
0
                                     t->GetLinkOptionsEntries(),
117
0
                                     this->LinkOptionsEntries);
118
119
0
  CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
120
0
                                     t->GetLinkDirectoriesEntries(),
121
0
                                     this->LinkDirectoriesEntries);
122
123
0
  CreatePropertyGeneratorExpressions(*lg->GetCMakeInstance(),
124
0
                                     t->GetPrecompileHeadersEntries(),
125
0
                                     this->PrecompileHeadersEntries);
126
127
0
  CreatePropertyGeneratorExpressions(
128
0
    *lg->GetCMakeInstance(), t->GetSourceEntries(), this->SourceEntries, true);
129
130
0
  this->PolicyMap = t->GetPolicyMap();
131
132
  // Get hard-coded linker language
133
0
  if (this->Target->GetProperty("HAS_CXX")) {
134
0
    this->LinkerLanguage = "CXX";
135
0
  } else {
136
0
    this->LinkerLanguage = this->Target->GetSafeProperty("LINKER_LANGUAGE");
137
0
  }
138
139
0
  auto configs =
140
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::ExcludeEmptyConfig);
141
0
  std::string build_db_languages[] = { "CXX" };
142
0
  for (auto const& language : build_db_languages) {
143
0
    for (auto const& config : configs) {
144
0
      auto bdb_path = this->BuildDatabasePath(language, config);
145
0
      if (!bdb_path.empty()) {
146
0
        this->Makefile->GetOrCreateGeneratedSource(bdb_path);
147
0
        this->GetGlobalGenerator()->AddBuildDatabaseFile(language, config,
148
0
                                                         bdb_path);
149
0
      }
150
0
    }
151
0
  }
152
0
}
153
154
0
cmGeneratorTarget::~cmGeneratorTarget() = default;
155
156
cmValue cmGeneratorTarget::GetSourcesProperty() const
157
0
{
158
0
  std::vector<std::string> values;
159
0
  for (auto const& se : this->SourceEntries) {
160
0
    values.push_back(se->GetInput());
161
0
  }
162
0
  static std::string value;
163
0
  value = cmList::to_string(values);
164
0
  return cmValue(value);
165
0
}
166
167
cmGlobalGenerator* cmGeneratorTarget::GetGlobalGenerator() const
168
0
{
169
0
  return this->GetLocalGenerator()->GetGlobalGenerator();
170
0
}
171
172
cmLocalGenerator* cmGeneratorTarget::GetLocalGenerator() const
173
0
{
174
0
  return this->LocalGenerator;
175
0
}
176
177
cmStateEnums::TargetType cmGeneratorTarget::GetType() const
178
0
{
179
0
  return this->Target->GetType();
180
0
}
181
182
std::string const& cmGeneratorTarget::GetName() const
183
0
{
184
0
  return this->Target->GetName();
185
0
}
186
187
std::string cmGeneratorTarget::GetFamilyName() const
188
0
{
189
0
  if (!this->IsImported() && !this->IsSynthetic()) {
190
0
    return this->Target->GetTemplateName();
191
0
  }
192
0
  cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_512);
193
0
  constexpr size_t HASH_TRUNCATION = 12;
194
0
  auto dirhash =
195
0
    hasher.HashString(this->GetLocalGenerator()->GetCurrentBinaryDirectory());
196
0
  auto targetIdent = hasher.HashString(cmStrCat("@d_", dirhash));
197
0
  return cmStrCat(this->Target->GetTemplateName(), '@',
198
0
                  targetIdent.substr(0, HASH_TRUNCATION));
199
0
}
200
201
std::string cmGeneratorTarget::GetExportName() const
202
0
{
203
0
  cmValue exportName = this->GetProperty("EXPORT_NAME");
204
205
0
  if (cmNonempty(exportName)) {
206
0
    if (!cmGeneratorExpression::IsValidTargetName(*exportName)) {
207
0
      std::ostringstream e;
208
0
      e << "EXPORT_NAME property \"" << *exportName << "\" for \""
209
0
        << this->GetName() << "\": is not valid.";
210
0
      cmSystemTools::Error(e.str());
211
0
      return "";
212
0
    }
213
0
    return *exportName;
214
0
  }
215
0
  return this->GetName();
216
0
}
217
218
std::string cmGeneratorTarget::GetFilesystemExportName() const
219
0
{
220
0
  auto fs_safe = this->GetExportName();
221
  // First escape any `_` characters to avoid collisions.
222
0
  cmSystemTools::ReplaceString(fs_safe, "_", "__");
223
  // Escape other characters that are not generally filesystem-safe.
224
0
  cmSystemTools::ReplaceString(fs_safe, ":", "_c");
225
0
  return fs_safe;
226
0
}
227
228
cmValue cmGeneratorTarget::GetProperty(std::string const& prop) const
229
0
{
230
0
  if (cmValue result =
231
0
        cmTargetPropertyComputer::GetProperty(this, prop, *this->Makefile)) {
232
0
    return result;
233
0
  }
234
0
  if (cmSystemTools::GetFatalErrorOccurred()) {
235
0
    return nullptr;
236
0
  }
237
0
  return this->Target->GetProperty(prop);
238
0
}
239
240
std::string const& cmGeneratorTarget::GetSafeProperty(
241
  std::string const& prop) const
242
0
{
243
0
  return this->GetProperty(prop);
244
0
}
245
246
char const* cmGeneratorTarget::GetOutputTargetType(
247
  cmStateEnums::ArtifactType artifact) const
248
0
{
249
0
  if (this->IsFrameworkOnApple() || this->GetGlobalGenerator()->IsXcode()) {
250
    // import file (i.e. .tbd file) is always in same location as library
251
0
    artifact = cmStateEnums::RuntimeBinaryArtifact;
252
0
  }
253
254
0
  switch (this->GetType()) {
255
0
    case cmStateEnums::SHARED_LIBRARY:
256
0
      if (this->IsDLLPlatform()) {
257
0
        switch (artifact) {
258
0
          case cmStateEnums::RuntimeBinaryArtifact:
259
            // A DLL shared library is treated as a runtime target.
260
0
            return "RUNTIME";
261
0
          case cmStateEnums::ImportLibraryArtifact:
262
            // A DLL import library is treated as an archive target.
263
0
            return "ARCHIVE";
264
0
        }
265
0
      } else {
266
0
        switch (artifact) {
267
0
          case cmStateEnums::RuntimeBinaryArtifact:
268
            // For non-DLL platforms shared libraries are treated as
269
            // library targets.
270
0
            return "LIBRARY";
271
0
          case cmStateEnums::ImportLibraryArtifact:
272
            // Library import libraries are treated as archive targets.
273
0
            return "ARCHIVE";
274
0
        }
275
0
      }
276
0
      break;
277
0
    case cmStateEnums::STATIC_LIBRARY:
278
      // Static libraries are always treated as archive targets.
279
0
      return "ARCHIVE";
280
0
    case cmStateEnums::MODULE_LIBRARY:
281
0
      switch (artifact) {
282
0
        case cmStateEnums::RuntimeBinaryArtifact:
283
          // Module libraries are always treated as library targets.
284
0
          return "LIBRARY";
285
0
        case cmStateEnums::ImportLibraryArtifact:
286
          // Module import libraries are treated as archive targets.
287
0
          return "ARCHIVE";
288
0
      }
289
0
      break;
290
0
    case cmStateEnums::OBJECT_LIBRARY:
291
      // Object libraries are always treated as object targets.
292
0
      return "OBJECT";
293
0
    case cmStateEnums::EXECUTABLE:
294
0
      switch (artifact) {
295
0
        case cmStateEnums::RuntimeBinaryArtifact:
296
          // Executables are always treated as runtime targets.
297
0
          return "RUNTIME";
298
0
        case cmStateEnums::ImportLibraryArtifact:
299
          // Executable import libraries are treated as archive targets.
300
0
          return "ARCHIVE";
301
0
      }
302
0
      break;
303
0
    default:
304
0
      break;
305
0
  }
306
0
  return "";
307
0
}
308
309
std::string cmGeneratorTarget::GetOutputName(
310
  std::string const& config, cmStateEnums::ArtifactType artifact) const
311
0
{
312
  // Lookup/compute/cache the output name for this configuration.
313
0
  OutputNameKey key(config, artifact);
314
0
  auto i = this->OutputNameMap.find(key);
315
0
  if (i == this->OutputNameMap.end()) {
316
    // Add empty name in map to detect potential recursion.
317
0
    OutputNameMapType::value_type entry(key, "");
318
0
    i = this->OutputNameMap.insert(entry).first;
319
320
    // Compute output name.
321
0
    std::vector<std::string> props;
322
0
    std::string type = this->GetOutputTargetType(artifact);
323
0
    std::string configUpper = cmSystemTools::UpperCase(config);
324
0
    if (!type.empty() && !configUpper.empty()) {
325
      // <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME_<CONFIG>
326
0
      props.push_back(cmStrCat(type, "_OUTPUT_NAME_", configUpper));
327
0
    }
328
0
    if (!type.empty()) {
329
      // <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME
330
0
      props.push_back(type + "_OUTPUT_NAME");
331
0
    }
332
0
    if (!configUpper.empty()) {
333
      // OUTPUT_NAME_<CONFIG>
334
0
      props.push_back("OUTPUT_NAME_" + configUpper);
335
      // <CONFIG>_OUTPUT_NAME
336
0
      props.push_back(configUpper + "_OUTPUT_NAME");
337
0
    }
338
    // OUTPUT_NAME
339
0
    props.emplace_back("OUTPUT_NAME");
340
341
0
    std::string outName;
342
0
    for (std::string const& p : props) {
343
0
      if (cmValue outNameProp = this->GetProperty(p)) {
344
0
        outName = *outNameProp;
345
0
        break;
346
0
      }
347
0
    }
348
349
0
    if (outName.empty()) {
350
0
      outName = this->GetName();
351
0
    }
352
353
    // Now evaluate genex and update the previously-prepared map entry.
354
0
    i->second =
355
0
      cmGeneratorExpression::Evaluate(outName, this->LocalGenerator, config);
356
0
  } else if (i->second.empty()) {
357
    // An empty map entry indicates we have been called recursively
358
    // from the above block.
359
0
    this->LocalGenerator->GetCMakeInstance()->IssueMessage(
360
0
      MessageType::FATAL_ERROR,
361
0
      cmStrCat("Target '", this->GetName(),
362
0
               "' OUTPUT_NAME depends on itself."),
363
0
      this->GetBacktrace());
364
0
  }
365
0
  return i->second;
366
0
}
367
368
std::string cmGeneratorTarget::GetFilePrefix(
369
  std::string const& config, cmStateEnums::ArtifactType artifact) const
370
0
{
371
0
  if (this->IsImported()) {
372
0
    cmValue prefix = this->GetFilePrefixInternal(config, artifact);
373
0
    return prefix ? *prefix : std::string();
374
0
  }
375
0
  return this->GetFullNameInternalComponents(config, artifact).prefix;
376
0
}
377
std::string cmGeneratorTarget::GetFileSuffix(
378
  std::string const& config, cmStateEnums::ArtifactType artifact) const
379
0
{
380
0
  if (this->IsImported()) {
381
0
    cmValue suffix = this->GetFileSuffixInternal(config, artifact);
382
0
    return suffix ? *suffix : std::string();
383
0
  }
384
0
  return this->GetFullNameInternalComponents(config, artifact).suffix;
385
0
}
386
387
std::string cmGeneratorTarget::GetFilePostfix(std::string const& config) const
388
0
{
389
0
  cmValue postfix = nullptr;
390
0
  std::string frameworkPostfix;
391
0
  if (!config.empty()) {
392
0
    std::string configProp =
393
0
      cmStrCat(cmSystemTools::UpperCase(config), "_POSTFIX");
394
0
    postfix = this->GetProperty(configProp);
395
396
    // Mac application bundles and frameworks have no regular postfix like
397
    // libraries do.
398
0
    if (!this->IsImported() && postfix &&
399
0
        (this->IsAppBundleOnApple() || this->IsFrameworkOnApple())) {
400
0
      postfix = nullptr;
401
0
    }
402
403
    // Frameworks created by multi config generators can have a special
404
    // framework postfix.
405
0
    frameworkPostfix = this->GetFrameworkMultiConfigPostfix(config);
406
0
    if (!frameworkPostfix.empty()) {
407
0
      postfix = cmValue(frameworkPostfix);
408
0
    }
409
0
  }
410
0
  return postfix ? *postfix : std::string();
411
0
}
412
413
std::string cmGeneratorTarget::GetFrameworkMultiConfigPostfix(
414
  std::string const& config) const
415
0
{
416
0
  cmValue postfix = nullptr;
417
0
  if (!config.empty()) {
418
0
    std::string configProp = cmStrCat("FRAMEWORK_MULTI_CONFIG_POSTFIX_",
419
0
                                      cmSystemTools::UpperCase(config));
420
0
    postfix = this->GetProperty(configProp);
421
422
0
    if (!this->IsImported() && postfix &&
423
0
        (this->IsFrameworkOnApple() &&
424
0
         !this->GetGlobalGenerator()->IsMultiConfig())) {
425
0
      postfix = nullptr;
426
0
    }
427
0
  }
428
0
  return postfix ? *postfix : std::string();
429
0
}
430
431
cmValue cmGeneratorTarget::GetFilePrefixInternal(
432
  std::string const& config, cmStateEnums::ArtifactType artifact,
433
  std::string const& language) const
434
0
{
435
  // no prefix for non-main target types.
436
0
  if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
437
0
      this->GetType() != cmStateEnums::SHARED_LIBRARY &&
438
0
      this->GetType() != cmStateEnums::MODULE_LIBRARY &&
439
0
      this->GetType() != cmStateEnums::EXECUTABLE) {
440
0
    return nullptr;
441
0
  }
442
443
0
  bool const isImportedLibraryArtifact =
444
0
    (artifact == cmStateEnums::ImportLibraryArtifact);
445
446
  // Return an empty prefix for the import library if this platform
447
  // does not support import libraries.
448
0
  if (isImportedLibraryArtifact && !this->NeedImportLibraryName(config)) {
449
0
    return nullptr;
450
0
  }
451
452
  // The implib option is only allowed for shared libraries, module
453
  // libraries, and executables.
454
0
  if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
455
0
      this->GetType() != cmStateEnums::MODULE_LIBRARY &&
456
0
      this->GetType() != cmStateEnums::EXECUTABLE) {
457
0
    artifact = cmStateEnums::RuntimeBinaryArtifact;
458
0
  }
459
460
  // Compute prefix value.
461
0
  cmValue targetPrefix =
462
0
    (isImportedLibraryArtifact ? this->GetProperty("IMPORT_PREFIX")
463
0
                               : this->GetProperty("PREFIX"));
464
465
0
  if (!targetPrefix) {
466
0
    char const* prefixVar = this->Target->GetPrefixVariableInternal(artifact);
467
0
    if (!language.empty() && cmNonempty(prefixVar)) {
468
0
      std::string langPrefix = cmStrCat(prefixVar, '_', language);
469
0
      targetPrefix = this->Makefile->GetDefinition(langPrefix);
470
0
    }
471
472
    // if there is no prefix on the target nor specific language
473
    // use the cmake definition.
474
0
    if (!targetPrefix && prefixVar) {
475
0
      targetPrefix = this->Makefile->GetDefinition(prefixVar);
476
0
    }
477
0
  }
478
479
0
  return targetPrefix;
480
0
}
481
482
cmValue cmGeneratorTarget::GetFileSuffixInternal(
483
  std::string const& config, cmStateEnums::ArtifactType artifact,
484
  std::string const& language) const
485
0
{
486
  // no suffix for non-main target types.
487
0
  if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
488
0
      this->GetType() != cmStateEnums::SHARED_LIBRARY &&
489
0
      this->GetType() != cmStateEnums::MODULE_LIBRARY &&
490
0
      this->GetType() != cmStateEnums::EXECUTABLE) {
491
0
    return nullptr;
492
0
  }
493
494
0
  bool const isImportedLibraryArtifact =
495
0
    (artifact == cmStateEnums::ImportLibraryArtifact);
496
497
  // Return an empty suffix for the import library if this platform
498
  // does not support import libraries.
499
0
  if (isImportedLibraryArtifact && !this->NeedImportLibraryName(config)) {
500
0
    return nullptr;
501
0
  }
502
503
  // The implib option is only allowed for shared libraries, module
504
  // libraries, and executables.
505
0
  if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
506
0
      this->GetType() != cmStateEnums::MODULE_LIBRARY &&
507
0
      this->GetType() != cmStateEnums::EXECUTABLE) {
508
0
    artifact = cmStateEnums::RuntimeBinaryArtifact;
509
0
  }
510
511
  // Compute suffix value.
512
0
  cmValue targetSuffix =
513
0
    (isImportedLibraryArtifact ? this->GetProperty("IMPORT_SUFFIX")
514
0
                               : this->GetProperty("SUFFIX"));
515
516
0
  if (!targetSuffix) {
517
0
    char const* suffixVar = this->Target->GetSuffixVariableInternal(artifact);
518
0
    if (!language.empty() && cmNonempty(suffixVar)) {
519
0
      std::string langSuffix = cmStrCat(suffixVar, '_', language);
520
0
      targetSuffix = this->Makefile->GetDefinition(langSuffix);
521
0
    }
522
523
    // if there is no suffix on the target nor specific language
524
    // use the cmake definition.
525
0
    if (!targetSuffix && suffixVar) {
526
0
      targetSuffix = this->Makefile->GetDefinition(suffixVar);
527
0
    }
528
0
  }
529
530
0
  return targetSuffix;
531
0
}
532
533
void cmGeneratorTarget::ClearSourcesCache()
534
0
{
535
0
  this->AllConfigSources.clear();
536
0
  this->AllConfigCompileLanguages.clear();
537
0
  this->KindedSourcesMap.clear();
538
0
  this->SourcesAreContextDependent = Tribool::Indeterminate;
539
0
  this->Objects.clear();
540
0
  this->VisitedConfigsForObjects.clear();
541
0
  this->LinkImplClosureForLinkMap.clear();
542
0
  this->LinkImplClosureForUsageMap.clear();
543
0
  this->LinkImplMap.clear();
544
0
  this->LinkImplUsageRequirementsOnlyMap.clear();
545
0
  this->IncludeDirectoriesCache.clear();
546
0
  this->CompileOptionsCache.clear();
547
0
  this->CompileDefinitionsCache.clear();
548
0
  this->CustomTransitiveBuildPropertiesMap.clear();
549
0
  this->CustomTransitiveInterfacePropertiesMap.clear();
550
0
  this->PrecompileHeadersCache.clear();
551
0
  this->LinkOptionsCache.clear();
552
0
  this->LinkDirectoriesCache.clear();
553
0
  this->RuntimeBinaryFullNameCache.clear();
554
0
  this->ImportLibraryFullNameCache.clear();
555
0
}
556
557
void cmGeneratorTarget::ClearLinkInterfaceCache()
558
0
{
559
0
  this->LinkInterfaceMap.clear();
560
0
  this->LinkInterfaceUsageRequirementsOnlyMap.clear();
561
0
}
562
563
void cmGeneratorTarget::AddSourceCommon(std::string const& src, bool before)
564
0
{
565
0
  this->SourceEntries.insert(
566
0
    before ? this->SourceEntries.begin() : this->SourceEntries.end(),
567
0
    TargetPropertyEntry::Create(
568
0
      *this->LocalGenerator->GetCMakeInstance(),
569
0
      BT<std::string>(src, this->Makefile->GetBacktrace()), true));
570
0
  this->ClearSourcesCache();
571
0
}
572
573
cmSourceFile* cmGeneratorTarget::AddSource(std::string const& src, bool before)
574
0
{
575
0
  auto* sf = this->Target->AddSource(src, before);
576
0
  this->AddSourceCommon(src, before);
577
0
  return sf;
578
0
}
579
580
void cmGeneratorTarget::AddTracedSources(std::vector<std::string> const& srcs)
581
0
{
582
0
  this->Target->AddTracedSources(srcs);
583
0
  if (!srcs.empty()) {
584
0
    this->AddSourceCommon(cmJoin(srcs, ";"));
585
0
  }
586
0
}
587
588
void cmGeneratorTarget::AddIncludeDirectory(std::string const& src,
589
                                            bool before)
590
0
{
591
0
  this->Target->InsertInclude(
592
0
    BT<std::string>(src, this->Makefile->GetBacktrace()), before);
593
0
  this->IncludeDirectoriesEntries.insert(
594
0
    before ? this->IncludeDirectoriesEntries.begin()
595
0
           : this->IncludeDirectoriesEntries.end(),
596
0
    TargetPropertyEntry::Create(
597
0
      *this->Makefile->GetCMakeInstance(),
598
0
      BT<std::string>(src, this->Makefile->GetBacktrace()), true));
599
0
}
600
601
void cmGeneratorTarget::AddSystemIncludeDirectory(std::string const& inc,
602
                                                  std::string const& lang)
603
0
{
604
0
  std::string config_upper;
605
0
  auto const& configs =
606
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
607
608
0
  for (auto const& config : configs) {
609
0
    std::string inc_with_config = inc;
610
0
    if (!config.empty()) {
611
0
      cmSystemTools::ReplaceString(inc_with_config, "$<CONFIG>", config);
612
0
      config_upper = cmSystemTools::UpperCase(config);
613
0
    }
614
0
    auto const& key = cmStrCat(config_upper, '/', lang);
615
0
    this->Target->AddSystemIncludeDirectories({ inc_with_config });
616
0
    if (this->SystemIncludesCache.find(key) ==
617
0
        this->SystemIncludesCache.end()) {
618
0
      this->AddSystemIncludeCacheKey(key, config, lang);
619
0
    }
620
0
    this->SystemIncludesCache[key].emplace_back(inc_with_config);
621
622
    // SystemIncludesCache should be sorted so that binary search can be used
623
0
    std::sort(this->SystemIncludesCache[key].begin(),
624
0
              this->SystemIncludesCache[key].end());
625
0
  }
626
0
}
627
628
std::vector<cmSourceFile*> const* cmGeneratorTarget::GetSourceDepends(
629
  cmSourceFile const* sf) const
630
0
{
631
0
  auto i = this->SourceDepends.find(sf);
632
0
  if (i != this->SourceDepends.end()) {
633
0
    return &i->second.Depends;
634
0
  }
635
0
  return nullptr;
636
0
}
637
638
namespace {
639
void handleSystemIncludesDep(cmLocalGenerator const* lg,
640
                             cmGeneratorTarget const* depTgt,
641
                             std::string const& config,
642
                             cmGeneratorTarget const* headTarget,
643
                             cmGeneratorExpressionDAGChecker* dagChecker,
644
                             cmList& result, bool excludeImported,
645
                             std::string const& language)
646
0
{
647
0
  if (cmValue dirs =
648
0
        depTgt->GetProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES")) {
649
0
    result.append(cmGeneratorExpression::Evaluate(
650
0
      *dirs, lg, config, headTarget, dagChecker, depTgt, language));
651
0
  }
652
0
  if (!depTgt->GetPropertyAsBool("SYSTEM")) {
653
0
    return;
654
0
  }
655
0
  if (depTgt->IsImported()) {
656
0
    if (excludeImported) {
657
0
      return;
658
0
    }
659
0
    if (depTgt->GetPropertyAsBool("IMPORTED_NO_SYSTEM")) {
660
0
      return;
661
0
    }
662
0
  }
663
664
0
  if (cmValue dirs = depTgt->GetProperty("INTERFACE_INCLUDE_DIRECTORIES")) {
665
0
    result.append(cmGeneratorExpression::Evaluate(
666
0
      *dirs, lg, config, headTarget, dagChecker, depTgt, language));
667
0
  }
668
669
0
  if (depTgt->Target->IsFrameworkOnApple() ||
670
0
      depTgt->IsImportedFrameworkFolderOnApple(config)) {
671
0
    if (auto fwDescriptor = depTgt->GetGlobalGenerator()->SplitFrameworkPath(
672
0
          depTgt->GetLocation(config))) {
673
0
      result.push_back(fwDescriptor->Directory);
674
0
      result.push_back(fwDescriptor->GetFrameworkPath());
675
0
    }
676
0
  }
677
0
}
678
}
679
680
/* clang-format off */
681
#define IMPLEMENT_VISIT(KIND)                                                 \
682
0
  do {                                                                        \
683
0
    KindedSources const& kinded = this->GetKindedSources(config);             \
684
0
    for (SourceAndKind const& s : kinded.Sources) {                           \
685
0
      if (s.Kind == KIND) {                                                   \
686
0
        data.push_back(s.Source.Value);                                       \
687
0
      }                                                                       \
688
0
    }                                                                         \
689
0
  } while (false)
690
/* clang-format on */
691
692
void cmGeneratorTarget::GetObjectSources(
693
  std::vector<cmSourceFile const*>& data, std::string const& config) const
694
0
{
695
0
  IMPLEMENT_VISIT(SourceKindObjectSource);
696
697
0
  if (this->VisitedConfigsForObjects.count(config)) {
698
0
    return;
699
0
  }
700
701
0
  for (cmSourceFile const* it : data) {
702
0
    this->Objects[it];
703
0
  }
704
705
0
  this->LocalGenerator->ComputeObjectFilenames(this->Objects, config, this);
706
0
  this->VisitedConfigsForObjects.insert(config);
707
0
}
708
709
void cmGeneratorTarget::ComputeObjectMapping()
710
0
{
711
0
  auto const& configs =
712
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
713
0
  std::set<std::string> configSet(configs.begin(), configs.end());
714
0
  if (configSet == this->VisitedConfigsForObjects) {
715
0
    return;
716
0
  }
717
718
0
  for (std::string const& c : configs) {
719
0
    std::vector<cmSourceFile const*> sourceFiles;
720
0
    this->GetObjectSources(sourceFiles, c);
721
0
  }
722
0
}
723
724
cmValue cmGeneratorTarget::GetFeature(std::string const& feature,
725
                                      std::string const& config) const
726
0
{
727
0
  if (!config.empty()) {
728
0
    std::string featureConfig =
729
0
      cmStrCat(feature, '_', cmSystemTools::UpperCase(config));
730
0
    if (cmValue value = this->GetProperty(featureConfig)) {
731
0
      return value;
732
0
    }
733
0
  }
734
0
  if (cmValue value = this->GetProperty(feature)) {
735
0
    return value;
736
0
  }
737
0
  return this->LocalGenerator->GetFeature(feature, config);
738
0
}
739
740
std::string cmGeneratorTarget::GetLinkerTypeProperty(
741
  std::string const& lang, std::string const& config) const
742
0
{
743
0
  std::string propName{ "LINKER_TYPE" };
744
0
  auto linkerType = this->GetProperty(propName);
745
0
  if (!linkerType.IsEmpty()) {
746
0
    cm::GenEx::Context context(this->LocalGenerator, config, lang);
747
0
    cmGeneratorExpressionDAGChecker dagChecker{
748
0
      this, propName, nullptr, nullptr, context,
749
0
    };
750
0
    auto ltype = cmGeneratorExpression::Evaluate(
751
0
      *linkerType, context.LG, context.Config, this, &dagChecker, this,
752
0
      context.Language);
753
0
    if (this->IsDeviceLink()) {
754
0
      cmList list{ ltype };
755
0
      auto const DL_BEGIN = "<DEVICE_LINK>"_s;
756
0
      auto const DL_END = "</DEVICE_LINK>"_s;
757
0
      cm::erase_if(list, [&](std::string const& item) {
758
0
        return item == DL_BEGIN || item == DL_END;
759
0
      });
760
0
      return list.to_string();
761
0
    }
762
0
    return ltype;
763
0
  }
764
0
  return std::string{};
765
0
}
766
767
char const* cmGeneratorTarget::GetLinkPIEProperty(
768
  std::string const& config) const
769
0
{
770
0
  static std::string PICValue;
771
772
0
  PICValue = this->GetLinkInterfaceDependentStringAsBoolProperty(
773
0
    "POSITION_INDEPENDENT_CODE", config);
774
775
0
  if (PICValue == "(unset)") {
776
    // POSITION_INDEPENDENT_CODE is not set
777
0
    return nullptr;
778
0
  }
779
780
0
  auto status = this->GetPolicyStatusCMP0083();
781
0
  return (status != cmPolicies::WARN && status != cmPolicies::OLD)
782
0
    ? PICValue.c_str()
783
0
    : nullptr;
784
0
}
785
786
bool cmGeneratorTarget::IsIPOEnabled(std::string const& lang,
787
                                     std::string const& config) const
788
0
{
789
0
  cmValue feature = this->GetFeature("INTERPROCEDURAL_OPTIMIZATION", config);
790
791
0
  if (!feature.IsOn()) {
792
    // 'INTERPROCEDURAL_OPTIMIZATION' is off, no need to check policies
793
0
    return false;
794
0
  }
795
796
0
  if (lang != "C" && lang != "CXX" && lang != "CUDA" && lang != "Fortran") {
797
    // We do not define IPO behavior for other languages.
798
0
    return false;
799
0
  }
800
801
0
  if (lang == "CUDA") {
802
    // CUDA IPO requires both CUDA_ARCHITECTURES and CUDA_SEPARABLE_COMPILATION
803
0
    if (cmIsOff(this->GetSafeProperty("CUDA_ARCHITECTURES")) ||
804
0
        cmIsOff(this->GetSafeProperty("CUDA_SEPARABLE_COMPILATION"))) {
805
0
      return false;
806
0
    }
807
0
  }
808
809
0
  cmPolicies::PolicyStatus cmp0069 = this->GetPolicyStatusCMP0069();
810
811
0
  if (cmp0069 == cmPolicies::OLD || cmp0069 == cmPolicies::WARN) {
812
0
    if (this->Makefile->IsOn(
813
0
          cmStrCat("_CMAKE_", lang, "_IPO_LEGACY_BEHAVIOR"))) {
814
0
      return true;
815
0
    }
816
0
    if (this->PolicyReportedCMP0069) {
817
      // problem is already reported, no need to issue a message
818
0
      return false;
819
0
    }
820
0
    bool const in_try_compile =
821
0
      this->LocalGenerator->GetCMakeInstance()->GetIsInTryCompile();
822
0
    if (cmp0069 == cmPolicies::WARN && !in_try_compile) {
823
0
      std::ostringstream w;
824
0
      w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0069) << "\n";
825
0
      w << "INTERPROCEDURAL_OPTIMIZATION property will be ignored for target "
826
0
        << "'" << this->GetName() << "'.";
827
0
      this->LocalGenerator->GetCMakeInstance()->IssueMessage(
828
0
        MessageType::AUTHOR_WARNING, w.str(), this->GetBacktrace());
829
830
0
      this->PolicyReportedCMP0069 = true;
831
0
    }
832
0
    return false;
833
0
  }
834
835
  // Note: check consistency with messages from CheckIPOSupported
836
0
  char const* message = nullptr;
837
0
  if (!this->Makefile->IsOn("_CMAKE_" + lang + "_IPO_SUPPORTED_BY_CMAKE")) {
838
0
    message = "CMake doesn't support IPO for current compiler";
839
0
  } else if (!this->Makefile->IsOn("_CMAKE_" + lang +
840
0
                                   "_IPO_MAY_BE_SUPPORTED_BY_COMPILER")) {
841
0
    message = "Compiler doesn't support IPO";
842
0
  } else if (!this->GlobalGenerator->IsIPOSupported()) {
843
0
    message = "CMake doesn't support IPO for current generator";
844
0
  }
845
846
0
  if (!message) {
847
    // No error/warning messages
848
0
    return true;
849
0
  }
850
851
0
  if (this->PolicyReportedCMP0069) {
852
    // problem is already reported, no need to issue a message
853
0
    return false;
854
0
  }
855
856
0
  this->PolicyReportedCMP0069 = true;
857
858
0
  this->LocalGenerator->GetCMakeInstance()->IssueMessage(
859
0
    MessageType::FATAL_ERROR, message, this->GetBacktrace());
860
0
  return false;
861
0
}
862
863
std::string const& cmGeneratorTarget::GetObjectName(cmSourceFile const* file)
864
0
{
865
0
  this->ComputeObjectMapping();
866
0
  auto const useShortPaths = this->GetUseShortObjectNames()
867
0
    ? cmObjectLocations::UseShortPath::Yes
868
0
    : cmObjectLocations::UseShortPath::No;
869
0
  return this->Objects[file].GetPath(useShortPaths);
870
0
}
871
872
char const* cmGeneratorTarget::GetCustomObjectExtension() const
873
0
{
874
0
  struct compiler_mode
875
0
  {
876
0
    std::string variable;
877
0
    std::string extension;
878
0
  };
879
0
  static std::array<compiler_mode, 4> const modes{
880
0
    { { "CUDA_PTX_COMPILATION", ".ptx" },
881
0
      { "CUDA_CUBIN_COMPILATION", ".cubin" },
882
0
      { "CUDA_FATBIN_COMPILATION", ".fatbin" },
883
0
      { "CUDA_OPTIX_COMPILATION", ".optixir" } }
884
0
  };
885
886
0
  std::string const& compiler =
887
0
    this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID");
888
0
  if (!compiler.empty()) {
889
0
    for (auto const& m : modes) {
890
0
      bool const has_extension = this->GetPropertyAsBool(m.variable);
891
0
      if (has_extension) {
892
0
        return m.extension.c_str();
893
0
      }
894
0
    }
895
0
  }
896
0
  return nullptr;
897
0
}
898
899
void cmGeneratorTarget::AddExplicitObjectName(cmSourceFile const* sf)
900
0
{
901
0
  this->ExplicitObjectName.insert(sf);
902
0
}
903
904
bool cmGeneratorTarget::HasExplicitObjectName(cmSourceFile const* file) const
905
0
{
906
0
  const_cast<cmGeneratorTarget*>(this)->ComputeObjectMapping();
907
0
  auto it = this->ExplicitObjectName.find(file);
908
0
  return it != this->ExplicitObjectName.end();
909
0
}
910
911
BTs<std::string> const* cmGeneratorTarget::GetLanguageStandardProperty(
912
  std::string const& lang, std::string const& config) const
913
0
{
914
0
  std::string key = cmStrCat(cmSystemTools::UpperCase(config), '-', lang);
915
0
  auto langStandardIter = this->LanguageStandardMap.find(key);
916
0
  if (langStandardIter != this->LanguageStandardMap.end()) {
917
0
    return &langStandardIter->second;
918
0
  }
919
920
0
  return this->Target->GetLanguageStandardProperty(
921
0
    cmStrCat(lang, "_STANDARD"));
922
0
}
923
924
cmValue cmGeneratorTarget::GetLanguageStandard(std::string const& lang,
925
                                               std::string const& config) const
926
0
{
927
0
  BTs<std::string> const* languageStandard =
928
0
    this->GetLanguageStandardProperty(lang, config);
929
930
0
  if (languageStandard) {
931
0
    return cmValue(languageStandard->Value);
932
0
  }
933
934
0
  return nullptr;
935
0
}
936
937
cmValue cmGeneratorTarget::GetPropertyWithPairedLanguageSupport(
938
  std::string const& lang, char const* suffix) const
939
0
{
940
0
  cmValue propertyValue = this->Target->GetProperty(cmStrCat(lang, suffix));
941
0
  if (!propertyValue) {
942
    // Check if we should use the value set by another language.
943
0
    if (lang == "OBJC") {
944
0
      propertyValue = this->GetPropertyWithPairedLanguageSupport("C", suffix);
945
0
    } else if (lang == "OBJCXX" || lang == "CUDA" || lang == "HIP") {
946
0
      propertyValue =
947
0
        this->GetPropertyWithPairedLanguageSupport("CXX", suffix);
948
0
    }
949
0
  }
950
0
  return propertyValue;
951
0
}
952
953
cmValue cmGeneratorTarget::GetLanguageExtensions(std::string const& lang) const
954
0
{
955
0
  return this->GetPropertyWithPairedLanguageSupport(lang, "_EXTENSIONS");
956
0
}
957
958
bool cmGeneratorTarget::GetLanguageStandardRequired(
959
  std::string const& lang) const
960
0
{
961
0
  return this->GetPropertyWithPairedLanguageSupport(lang, "_STANDARD_REQUIRED")
962
0
    .IsOn();
963
0
}
964
965
void cmGeneratorTarget::GetModuleDefinitionSources(
966
  std::vector<cmSourceFile const*>& data, std::string const& config) const
967
0
{
968
0
  IMPLEMENT_VISIT(SourceKindModuleDefinition);
969
0
}
970
971
void cmGeneratorTarget::GetHeaderSources(
972
  std::vector<cmSourceFile const*>& data, std::string const& config) const
973
0
{
974
0
  IMPLEMENT_VISIT(SourceKindHeader);
975
0
}
976
977
void cmGeneratorTarget::GetCxxModuleSources(
978
  std::vector<cmSourceFile const*>& data, std::string const& config) const
979
0
{
980
0
  IMPLEMENT_VISIT(SourceKindCxxModuleSource);
981
0
}
982
983
void cmGeneratorTarget::GetExtraSources(std::vector<cmSourceFile const*>& data,
984
                                        std::string const& config) const
985
0
{
986
0
  IMPLEMENT_VISIT(SourceKindExtra);
987
0
}
988
989
void cmGeneratorTarget::GetCustomCommands(
990
  std::vector<cmSourceFile const*>& data, std::string const& config) const
991
0
{
992
0
  IMPLEMENT_VISIT(SourceKindCustomCommand);
993
0
}
994
995
void cmGeneratorTarget::GetExternalObjects(
996
  std::vector<cmSourceFile const*>& data, std::string const& config) const
997
0
{
998
0
  IMPLEMENT_VISIT(SourceKindExternalObject);
999
0
}
1000
1001
void cmGeneratorTarget::GetManifests(std::vector<cmSourceFile const*>& data,
1002
                                     std::string const& config) const
1003
0
{
1004
0
  IMPLEMENT_VISIT(SourceKindManifest);
1005
0
}
1006
1007
std::set<cmLinkItem> const& cmGeneratorTarget::GetUtilityItems() const
1008
0
{
1009
0
  if (!this->UtilityItemsDone) {
1010
0
    this->UtilityItemsDone = true;
1011
0
    std::set<BT<std::pair<std::string, bool>>> const& utilities =
1012
0
      this->GetUtilities();
1013
0
    for (BT<std::pair<std::string, bool>> const& i : utilities) {
1014
0
      if (cmGeneratorTarget* gt =
1015
0
            this->LocalGenerator->FindGeneratorTargetToUse(i.Value.first)) {
1016
0
        this->UtilityItems.insert(cmLinkItem(gt, i.Value.second, i.Backtrace));
1017
0
      } else {
1018
0
        this->UtilityItems.insert(
1019
0
          cmLinkItem(i.Value.first, i.Value.second, i.Backtrace));
1020
0
      }
1021
0
    }
1022
0
    if (cmGeneratorTarget const* reuseTarget = this->GetPchReuseTarget()) {
1023
0
      this->UtilityItems.insert(
1024
0
        cmLinkItem(reuseTarget, false, cmListFileBacktrace()));
1025
0
    }
1026
0
  }
1027
0
  return this->UtilityItems;
1028
0
}
1029
1030
std::string const& cmGeneratorTarget::GetLocation(
1031
  std::string const& config) const
1032
0
{
1033
0
  static std::string location;
1034
0
  if (this->IsImported()) {
1035
0
    location = this->Target->ImportedGetFullPath(
1036
0
      config, cmStateEnums::RuntimeBinaryArtifact);
1037
0
  } else {
1038
0
    location = this->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
1039
0
  }
1040
0
  return location;
1041
0
}
1042
1043
cm::optional<std::string> cmGeneratorTarget::MaybeGetLocation(
1044
  std::string const& config) const
1045
0
{
1046
0
  cm::optional<std::string> location;
1047
0
  if (cmGeneratorTarget::ImportInfo const* imp = this->GetImportInfo(config)) {
1048
0
    if (!imp->Location.empty()) {
1049
0
      location = imp->Location;
1050
0
    }
1051
0
  } else {
1052
0
    location = this->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
1053
0
  }
1054
0
  return location;
1055
0
}
1056
1057
std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPreBuildCommands()
1058
  const
1059
0
{
1060
0
  return this->Target->GetPreBuildCommands();
1061
0
}
1062
1063
std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPreLinkCommands()
1064
  const
1065
0
{
1066
0
  return this->Target->GetPreLinkCommands();
1067
0
}
1068
1069
std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPostBuildCommands()
1070
  const
1071
0
{
1072
0
  return this->Target->GetPostBuildCommands();
1073
0
}
1074
1075
void cmGeneratorTarget::AppendCustomCommandSideEffects(
1076
  std::set<cmGeneratorTarget const*>& sideEffects) const
1077
0
{
1078
0
  if (!this->GetPreBuildCommands().empty() ||
1079
0
      !this->GetPreLinkCommands().empty() ||
1080
0
      !this->GetPostBuildCommands().empty()) {
1081
0
    sideEffects.insert(this);
1082
0
  } else {
1083
0
    for (auto const& source : this->GetAllConfigSources()) {
1084
0
      if (source.Source->GetCustomCommand()) {
1085
0
        sideEffects.insert(this);
1086
0
        break;
1087
0
      }
1088
0
    }
1089
0
  }
1090
0
}
1091
1092
void cmGeneratorTarget::AppendLanguageSideEffects(
1093
  std::map<std::string, std::set<cmGeneratorTarget const*>>& sideEffects) const
1094
0
{
1095
0
  static std::set<cm::string_view> const LANGS_WITH_NO_SIDE_EFFECTS = {
1096
0
    "C"_s, "CXX"_s, "OBJC"_s, "OBJCXX"_s, "ASM"_s, "CUDA"_s, "HIP"_s
1097
0
  };
1098
1099
0
  for (auto const& lang : this->GetAllConfigCompileLanguages()) {
1100
0
    if (!LANGS_WITH_NO_SIDE_EFFECTS.count(lang)) {
1101
0
      sideEffects[lang].insert(this);
1102
0
    }
1103
0
  }
1104
0
}
1105
1106
bool cmGeneratorTarget::IsInBuildSystem() const
1107
0
{
1108
0
  if (this->IsImported()) {
1109
0
    return false;
1110
0
  }
1111
0
  switch (this->Target->GetType()) {
1112
0
    case cmStateEnums::EXECUTABLE:
1113
0
    case cmStateEnums::STATIC_LIBRARY:
1114
0
    case cmStateEnums::SHARED_LIBRARY:
1115
0
    case cmStateEnums::MODULE_LIBRARY:
1116
0
    case cmStateEnums::OBJECT_LIBRARY:
1117
0
    case cmStateEnums::UTILITY:
1118
0
    case cmStateEnums::GLOBAL_TARGET:
1119
0
      return true;
1120
0
    case cmStateEnums::INTERFACE_LIBRARY:
1121
      // An INTERFACE library is in the build system if it has SOURCES
1122
      // or C++ module filesets.
1123
0
      if (!this->SourceEntries.empty() ||
1124
0
          !this->Target->GetHeaderSetsEntries().empty() ||
1125
0
          !this->Target->GetCxxModuleSetsEntries().empty()) {
1126
0
        return true;
1127
0
      }
1128
0
      break;
1129
0
    case cmStateEnums::UNKNOWN_LIBRARY:
1130
0
      break;
1131
0
  }
1132
0
  return false;
1133
0
}
1134
1135
bool cmGeneratorTarget::IsNormal() const
1136
0
{
1137
0
  return this->Target->IsNormal();
1138
0
}
1139
1140
bool cmGeneratorTarget::IsRuntimeBinary() const
1141
0
{
1142
0
  return this->Target->IsRuntimeBinary();
1143
0
}
1144
1145
bool cmGeneratorTarget::IsSynthetic() const
1146
0
{
1147
0
  return this->Target->IsSynthetic();
1148
0
}
1149
1150
bool cmGeneratorTarget::IsImported() const
1151
0
{
1152
0
  return this->Target->IsImported();
1153
0
}
1154
1155
bool cmGeneratorTarget::IsImportedGloballyVisible() const
1156
0
{
1157
0
  return this->Target->IsImportedGloballyVisible();
1158
0
}
1159
1160
bool cmGeneratorTarget::IsSymbolic() const
1161
0
{
1162
0
  return this->Target->IsSymbolic();
1163
0
}
1164
1165
bool cmGeneratorTarget::IsForeign() const
1166
0
{
1167
0
  return this->Target->IsForeign();
1168
0
}
1169
1170
bool cmGeneratorTarget::CanCompileSources() const
1171
0
{
1172
0
  return this->Target->CanCompileSources();
1173
0
}
1174
1175
bool cmGeneratorTarget::HasKnownRuntimeArtifactLocation(
1176
  std::string const& config) const
1177
0
{
1178
0
  if (!this->IsRuntimeBinary()) {
1179
0
    return false;
1180
0
  }
1181
0
  if (!this->IsImported()) {
1182
0
    return true;
1183
0
  }
1184
0
  ImportInfo const* info = this->GetImportInfo(config);
1185
0
  return info && !info->Location.empty();
1186
0
}
1187
1188
std::string const& cmGeneratorTarget::GetLocationForBuild() const
1189
0
{
1190
0
  static std::string location;
1191
0
  if (this->IsImported()) {
1192
0
    location = this->Target->ImportedGetFullPath(
1193
0
      "", cmStateEnums::RuntimeBinaryArtifact);
1194
0
    return location;
1195
0
  }
1196
1197
  // Now handle the deprecated build-time configuration location.
1198
0
  std::string const noConfig;
1199
0
  location = this->GetDirectory(noConfig);
1200
0
  cmValue cfgid = this->Makefile->GetDefinition("CMAKE_CFG_INTDIR");
1201
0
  if (cfgid && (*cfgid != ".")) {
1202
0
    location += "/";
1203
0
    location += *cfgid;
1204
0
  }
1205
1206
0
  if (this->IsAppBundleOnApple()) {
1207
0
    std::string macdir = this->BuildBundleDirectory("", "", FullLevel);
1208
0
    if (!macdir.empty()) {
1209
0
      location += "/";
1210
0
      location += macdir;
1211
0
    }
1212
0
  }
1213
0
  location += "/";
1214
0
  location += this->GetFullName("", cmStateEnums::RuntimeBinaryArtifact);
1215
0
  return location;
1216
0
}
1217
1218
void cmGeneratorTarget::AddSystemIncludeCacheKey(
1219
  std::string const& key, std::string const& config,
1220
  std::string const& language) const
1221
0
{
1222
0
  cm::GenEx::Context context(this->LocalGenerator, config, language);
1223
0
  cmGeneratorExpressionDAGChecker dagChecker{
1224
0
    this, "SYSTEM_INCLUDE_DIRECTORIES", nullptr, nullptr, context,
1225
0
  };
1226
1227
0
  bool excludeImported = this->GetPropertyAsBool("NO_SYSTEM_FROM_IMPORTED");
1228
1229
0
  cmList result;
1230
0
  for (std::string const& it : this->Target->GetSystemIncludeDirectories()) {
1231
0
    result.append(
1232
0
      cmGeneratorExpression::Evaluate(it, context.LG, context.Config, this,
1233
0
                                      &dagChecker, nullptr, context.Language));
1234
0
  }
1235
1236
0
  std::vector<cmGeneratorTarget const*> const& deps =
1237
0
    this->GetLinkImplementationClosure(config, UseTo::Compile);
1238
0
  for (cmGeneratorTarget const* dep : deps) {
1239
0
    handleSystemIncludesDep(context.LG, dep, context.Config, this, &dagChecker,
1240
0
                            result, excludeImported, context.Language);
1241
0
  }
1242
1243
0
  cmLinkImplementation const* impl =
1244
0
    this->GetLinkImplementation(config, UseTo::Compile);
1245
0
  if (impl) {
1246
0
    auto runtimeEntries = impl->LanguageRuntimeLibraries.find(language);
1247
0
    if (runtimeEntries != impl->LanguageRuntimeLibraries.end()) {
1248
0
      for (auto const& lib : runtimeEntries->second) {
1249
0
        if (lib.Target) {
1250
0
          handleSystemIncludesDep(context.LG, lib.Target, context.Config, this,
1251
0
                                  &dagChecker, result, excludeImported,
1252
0
                                  context.Language);
1253
0
        }
1254
0
      }
1255
0
    }
1256
0
  }
1257
1258
0
  std::for_each(result.begin(), result.end(),
1259
0
                cmSystemTools::ConvertToUnixSlashes);
1260
0
  std::sort(result.begin(), result.end());
1261
0
  result.erase(std::unique(result.begin(), result.end()), result.end());
1262
0
  SystemIncludesCache.emplace(key, result);
1263
0
}
1264
1265
bool cmGeneratorTarget::IsSystemIncludeDirectory(
1266
  std::string const& dir, std::string const& config,
1267
  std::string const& language) const
1268
0
{
1269
0
  std::string config_upper;
1270
0
  if (!config.empty()) {
1271
0
    config_upper = cmSystemTools::UpperCase(config);
1272
0
  }
1273
1274
0
  std::string key = cmStrCat(config_upper, '/', language);
1275
0
  auto iter = this->SystemIncludesCache.find(key);
1276
1277
0
  if (iter == this->SystemIncludesCache.end()) {
1278
0
    this->AddSystemIncludeCacheKey(key, config, language);
1279
0
    iter = this->SystemIncludesCache.find(key);
1280
0
  }
1281
1282
0
  return std::binary_search(iter->second.begin(), iter->second.end(), dir);
1283
0
}
1284
1285
bool cmGeneratorTarget::GetPropertyAsBool(std::string const& prop) const
1286
0
{
1287
0
  return this->Target->GetPropertyAsBool(prop);
1288
0
}
1289
1290
std::string cmGeneratorTarget::GetCompilePDBName(
1291
  std::string const& config) const
1292
0
{
1293
0
  if (cmGeneratorTarget const* reuseTarget = this->GetPchReuseTarget()) {
1294
0
    if (reuseTarget != this) {
1295
0
      return reuseTarget->GetCompilePDBName(config);
1296
0
    }
1297
0
  }
1298
1299
  // Check for a per-configuration output directory target property.
1300
0
  std::string configUpper = cmSystemTools::UpperCase(config);
1301
0
  std::string configProp = cmStrCat("COMPILE_PDB_NAME_", configUpper);
1302
0
  cmValue config_name = this->GetProperty(configProp);
1303
0
  if (cmNonempty(config_name)) {
1304
0
    std::string pdbName = cmGeneratorExpression::Evaluate(
1305
0
      *config_name, this->LocalGenerator, config, this);
1306
0
    NameComponents const& components = GetFullNameInternalComponents(
1307
0
      config, cmStateEnums::RuntimeBinaryArtifact);
1308
0
    return components.prefix + pdbName + ".pdb";
1309
0
  }
1310
1311
0
  cmValue name = this->GetProperty("COMPILE_PDB_NAME");
1312
0
  if (cmNonempty(name)) {
1313
0
    std::string pdbName = cmGeneratorExpression::Evaluate(
1314
0
      *name, this->LocalGenerator, config, this);
1315
0
    NameComponents const& components = GetFullNameInternalComponents(
1316
0
      config, cmStateEnums::RuntimeBinaryArtifact);
1317
0
    return components.prefix + pdbName + ".pdb";
1318
0
  }
1319
1320
  // If the target is PCH-reused or PCH-reuses, we need a stable name for the
1321
  // PDB file so that reusing targets can construct a stable name for it.
1322
0
  cmGeneratorTarget const* reuseTarget = this->GetPchReuseTarget();
1323
0
  bool const hasReuse = reuseTarget && reuseTarget != this;
1324
0
  if (this->PchReused || hasReuse) {
1325
0
    NameComponents const& components = GetFullNameInternalComponents(
1326
0
      config, cmStateEnums::RuntimeBinaryArtifact);
1327
0
    return cmStrCat(components.prefix, this->GetName(), ".pdb");
1328
0
  }
1329
1330
0
  return std::string{};
1331
0
}
1332
1333
std::string cmGeneratorTarget::GetCompilePDBPath(
1334
  std::string const& config) const
1335
0
{
1336
0
  std::string dir = this->GetCompilePDBDirectory(config);
1337
0
  std::string name = this->GetCompilePDBName(config);
1338
0
  if (dir.empty() && !name.empty() && this->HaveWellDefinedOutputFiles()) {
1339
0
    dir = this->GetPDBDirectory(config);
1340
0
  }
1341
0
  if (!dir.empty()) {
1342
0
    dir += "/";
1343
0
  }
1344
0
  return dir + name;
1345
0
}
1346
1347
bool cmGeneratorTarget::HasSOName(std::string const& config) const
1348
0
{
1349
  // soname is supported only for shared libraries and modules,
1350
  // and then only when the platform supports an soname flag.
1351
0
  return ((this->GetType() == cmStateEnums::SHARED_LIBRARY) &&
1352
0
          !this->GetPropertyAsBool("NO_SONAME") &&
1353
0
          (this->Makefile->GetSONameFlag(this->GetLinkerLanguage(config)) ||
1354
0
           this->IsArchivedAIXSharedLibrary()));
1355
0
}
1356
1357
bool cmGeneratorTarget::NeedRelinkBeforeInstall(
1358
  std::string const& config) const
1359
0
{
1360
  // Only executables and shared libraries can have an rpath and may
1361
  // need relinking.
1362
0
  if (this->GetType() != cmStateEnums::EXECUTABLE &&
1363
0
      this->GetType() != cmStateEnums::SHARED_LIBRARY &&
1364
0
      this->GetType() != cmStateEnums::MODULE_LIBRARY) {
1365
0
    return false;
1366
0
  }
1367
1368
  // If there is no install location this target will not be installed
1369
  // and therefore does not need relinking.
1370
0
  if (!this->Target->GetHaveInstallRule()) {
1371
0
    return false;
1372
0
  }
1373
1374
  // If skipping all rpaths completely then no relinking is needed.
1375
0
  if (this->Makefile->IsOn("CMAKE_SKIP_RPATH")) {
1376
0
    return false;
1377
0
  }
1378
1379
  // If building with the install-tree rpath no relinking is needed.
1380
0
  if (this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH")) {
1381
0
    return false;
1382
0
  }
1383
1384
  // If chrpath is going to be used no relinking is needed.
1385
0
  if (this->IsChrpathUsed(config)) {
1386
0
    return false;
1387
0
  }
1388
1389
  // Check for rpath support on this platform.
1390
0
  std::string ll = this->GetLinkerLanguage(config);
1391
0
  if (!ll.empty()) {
1392
0
    std::string flagVar =
1393
0
      cmStrCat("CMAKE_SHARED_LIBRARY_RUNTIME_", ll, "_FLAG");
1394
0
    if (!this->Makefile->IsSet(flagVar)) {
1395
      // There is no rpath support on this platform so nothing needs
1396
      // relinking.
1397
0
      return false;
1398
0
    }
1399
0
  } else {
1400
    // No linker language is known.  This error will be reported by
1401
    // other code.
1402
0
    return false;
1403
0
  }
1404
1405
  // If either a build or install tree rpath is set then the rpath
1406
  // will likely change between the build tree and install tree and
1407
  // this target must be relinked.
1408
0
  bool have_rpath =
1409
0
    this->HaveBuildTreeRPATH(config) || this->HaveInstallTreeRPATH(config);
1410
0
  bool is_ninja = this->LocalGenerator->GetGlobalGenerator()->IsNinja();
1411
1412
0
  if (have_rpath && is_ninja) {
1413
0
    std::ostringstream w;
1414
    /* clang-format off */
1415
0
    w <<
1416
0
      "The install of the " << this->GetName() << " target requires changing "
1417
0
      "an RPATH from the build tree, but this is not supported with the Ninja "
1418
0
      "generator unless on an ELF-based or XCOFF-based platform.  "
1419
0
      "The CMAKE_BUILD_WITH_INSTALL_RPATH variable may be set to avoid this "
1420
0
      "relinking step."
1421
0
      ;
1422
    /* clang-format on */
1423
1424
0
    cmake* cm = this->LocalGenerator->GetCMakeInstance();
1425
0
    cm->IssueMessage(MessageType::FATAL_ERROR, w.str(), this->GetBacktrace());
1426
0
  }
1427
1428
0
  return have_rpath;
1429
0
}
1430
1431
bool cmGeneratorTarget::IsChrpathUsed(std::string const& config) const
1432
0
{
1433
  // Only certain target types have an rpath.
1434
0
  if (!(this->GetType() == cmStateEnums::SHARED_LIBRARY ||
1435
0
        this->GetType() == cmStateEnums::MODULE_LIBRARY ||
1436
0
        this->GetType() == cmStateEnums::EXECUTABLE)) {
1437
0
    return false;
1438
0
  }
1439
1440
  // If the target will not be installed we do not need to change its
1441
  // rpath.
1442
0
  if (!this->Target->GetHaveInstallRule()) {
1443
0
    return false;
1444
0
  }
1445
1446
  // Skip chrpath if skipping rpath altogether.
1447
0
  if (this->Makefile->IsOn("CMAKE_SKIP_RPATH")) {
1448
0
    return false;
1449
0
  }
1450
1451
  // Skip chrpath if it does not need to be changed at install time.
1452
0
  if (this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH")) {
1453
0
    return false;
1454
0
  }
1455
1456
  // Allow the user to disable builtin chrpath explicitly.
1457
0
  if (this->Makefile->IsOn("CMAKE_NO_BUILTIN_CHRPATH")) {
1458
0
    return false;
1459
0
  }
1460
1461
0
  if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
1462
0
    return true;
1463
0
  }
1464
1465
  // Enable if the rpath flag uses a separator and the target uses
1466
  // binaries we know how to edit.
1467
0
  std::string ll = this->GetLinkerLanguage(config);
1468
0
  if (!ll.empty()) {
1469
0
    std::string sepVar =
1470
0
      cmStrCat("CMAKE_SHARED_LIBRARY_RUNTIME_", ll, "_FLAG_SEP");
1471
0
    cmValue sep = this->Makefile->GetDefinition(sepVar);
1472
0
    if (cmNonempty(sep)) {
1473
      // TODO: Add binary format check to ABI detection and get rid of
1474
      // CMAKE_EXECUTABLE_FORMAT.
1475
0
      if (cmValue fmt =
1476
0
            this->Makefile->GetDefinition("CMAKE_EXECUTABLE_FORMAT")) {
1477
0
        if (*fmt == "ELF") {
1478
0
          return true;
1479
0
        }
1480
#if defined(CMake_USE_XCOFF_PARSER)
1481
        if (*fmt == "XCOFF") {
1482
          return true;
1483
        }
1484
#endif
1485
0
      }
1486
0
    }
1487
0
  }
1488
0
  return false;
1489
0
}
1490
1491
bool cmGeneratorTarget::IsImportedSharedLibWithoutSOName(
1492
  std::string const& config) const
1493
0
{
1494
0
  if (this->IsImported() && this->GetType() == cmStateEnums::SHARED_LIBRARY) {
1495
0
    if (cmGeneratorTarget::ImportInfo const* info =
1496
0
          this->GetImportInfo(config)) {
1497
0
      return info->NoSOName;
1498
0
    }
1499
0
  }
1500
0
  return false;
1501
0
}
1502
1503
bool cmGeneratorTarget::HasMacOSXRpathInstallNameDir(
1504
  std::string const& config) const
1505
0
{
1506
0
  TargetPtrToBoolMap& cache = this->MacOSXRpathInstallNameDirCache[config];
1507
0
  auto const lookup = cache.find(this->Target);
1508
1509
0
  if (lookup != cache.cend()) {
1510
0
    return lookup->second;
1511
0
  }
1512
1513
0
  bool const result = this->DetermineHasMacOSXRpathInstallNameDir(config);
1514
0
  cache[this->Target] = result;
1515
0
  return result;
1516
0
}
1517
1518
bool cmGeneratorTarget::DetermineHasMacOSXRpathInstallNameDir(
1519
  std::string const& config) const
1520
0
{
1521
0
  bool install_name_is_rpath = false;
1522
0
  bool macosx_rpath = false;
1523
1524
0
  if (!this->IsImported()) {
1525
0
    if (this->GetType() != cmStateEnums::SHARED_LIBRARY) {
1526
0
      return false;
1527
0
    }
1528
0
    cmValue install_name = this->GetProperty("INSTALL_NAME_DIR");
1529
0
    bool use_install_name = this->MacOSXUseInstallNameDir();
1530
0
    if (install_name && use_install_name && *install_name == "@rpath") {
1531
0
      install_name_is_rpath = true;
1532
0
    } else if (install_name && use_install_name) {
1533
0
      return false;
1534
0
    }
1535
0
    if (!install_name_is_rpath) {
1536
0
      macosx_rpath = this->MacOSXRpathInstallNameDirDefault();
1537
0
    }
1538
0
  } else {
1539
    // Lookup the imported soname.
1540
0
    if (cmGeneratorTarget::ImportInfo const* info =
1541
0
          this->GetImportInfo(config)) {
1542
0
      if (!info->NoSOName && !info->SOName.empty()) {
1543
0
        if (cmHasLiteralPrefix(info->SOName, "@rpath/")) {
1544
0
          install_name_is_rpath = true;
1545
0
        }
1546
0
      } else {
1547
0
        std::string install_name;
1548
0
        cmSystemTools::GuessLibraryInstallName(info->Location, install_name);
1549
0
        if (install_name.find("@rpath") != std::string::npos) {
1550
0
          install_name_is_rpath = true;
1551
0
        }
1552
0
      }
1553
0
    }
1554
0
  }
1555
1556
0
  if (!install_name_is_rpath && !macosx_rpath) {
1557
0
    return false;
1558
0
  }
1559
1560
0
  if (!this->Makefile->IsSet("CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG")) {
1561
0
    std::ostringstream w;
1562
0
    w << "Attempting to use ";
1563
0
    if (macosx_rpath) {
1564
0
      w << "MACOSX_RPATH";
1565
0
    } else {
1566
0
      w << "@rpath";
1567
0
    }
1568
0
    w << " without CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG being set.";
1569
0
    w << "  This could be because you are using a Mac OS X version";
1570
0
    w << " less than 10.5 or because CMake's platform configuration is";
1571
0
    w << " corrupt.";
1572
0
    cmake* cm = this->LocalGenerator->GetCMakeInstance();
1573
0
    cm->IssueMessage(MessageType::FATAL_ERROR, w.str(), this->GetBacktrace());
1574
0
  }
1575
1576
0
  return true;
1577
0
}
1578
1579
bool cmGeneratorTarget::MacOSXRpathInstallNameDirDefault() const
1580
0
{
1581
  // we can't do rpaths when unsupported
1582
0
  if (!this->Makefile->IsSet("CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG")) {
1583
0
    return false;
1584
0
  }
1585
1586
0
  cmValue macosx_rpath_str = this->GetProperty("MACOSX_RPATH");
1587
0
  if (macosx_rpath_str) {
1588
0
    return this->GetPropertyAsBool("MACOSX_RPATH");
1589
0
  }
1590
1591
0
  return true;
1592
0
}
1593
1594
bool cmGeneratorTarget::MacOSXUseInstallNameDir() const
1595
0
{
1596
0
  cmValue build_with_install_name =
1597
0
    this->GetProperty("BUILD_WITH_INSTALL_NAME_DIR");
1598
0
  if (build_with_install_name) {
1599
0
    return build_with_install_name.IsOn();
1600
0
  }
1601
1602
0
  cmPolicies::PolicyStatus cmp0068 = this->GetPolicyStatusCMP0068();
1603
0
  if (cmp0068 == cmPolicies::NEW) {
1604
0
    return false;
1605
0
  }
1606
1607
0
  bool use_install_name = this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH");
1608
1609
0
  if (use_install_name && cmp0068 == cmPolicies::WARN) {
1610
0
    this->LocalGenerator->GetGlobalGenerator()->AddCMP0068WarnTarget(
1611
0
      this->GetName());
1612
0
  }
1613
1614
0
  return use_install_name;
1615
0
}
1616
1617
bool cmGeneratorTarget::CanGenerateInstallNameDir(
1618
  InstallNameType name_type) const
1619
0
{
1620
0
  cmPolicies::PolicyStatus cmp0068 = this->GetPolicyStatusCMP0068();
1621
1622
0
  if (cmp0068 == cmPolicies::NEW) {
1623
0
    return true;
1624
0
  }
1625
1626
0
  bool skip = this->Makefile->IsOn("CMAKE_SKIP_RPATH");
1627
0
  if (name_type == INSTALL_NAME_FOR_INSTALL) {
1628
0
    skip |= this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
1629
0
  } else {
1630
0
    skip |= this->GetPropertyAsBool("SKIP_BUILD_RPATH");
1631
0
  }
1632
1633
0
  if (skip && cmp0068 == cmPolicies::WARN) {
1634
0
    this->LocalGenerator->GetGlobalGenerator()->AddCMP0068WarnTarget(
1635
0
      this->GetName());
1636
0
  }
1637
1638
0
  return !skip;
1639
0
}
1640
1641
std::string cmGeneratorTarget::GetSOName(
1642
  std::string const& config, cmStateEnums::ArtifactType artifact) const
1643
0
{
1644
0
  if (this->IsImported()) {
1645
    // Lookup the imported soname.
1646
0
    if (cmGeneratorTarget::ImportInfo const* info =
1647
0
          this->GetImportInfo(config)) {
1648
0
      if (info->NoSOName) {
1649
        // The imported library has no builtin soname so the name
1650
        // searched at runtime will be just the filename.
1651
0
        return cmSystemTools::GetFilenameName(info->Location);
1652
0
      }
1653
      // Use the soname given if any.
1654
0
      if (this->IsFrameworkOnApple()) {
1655
0
        auto fwDescriptor = this->GetGlobalGenerator()->SplitFrameworkPath(
1656
0
          info->SOName, cmGlobalGenerator::FrameworkFormat::Strict);
1657
0
        if (fwDescriptor) {
1658
0
          return fwDescriptor->GetVersionedName();
1659
0
        }
1660
0
      }
1661
0
      if (cmHasLiteralPrefix(info->SOName, "@rpath/")) {
1662
0
        return info->SOName.substr(cmStrLen("@rpath/"));
1663
0
      }
1664
0
      return info->SOName;
1665
0
    }
1666
0
    return "";
1667
0
  }
1668
  // Compute the soname that will be built.
1669
0
  return artifact == cmStateEnums::RuntimeBinaryArtifact
1670
0
    ? this->GetLibraryNames(config).SharedObject
1671
0
    : this->GetLibraryNames(config).ImportLibrary;
1672
0
}
1673
1674
namespace {
1675
bool shouldAddFullLevel(cmGeneratorTarget::BundleDirectoryLevel level)
1676
0
{
1677
0
  return level == cmGeneratorTarget::FullLevel;
1678
0
}
1679
1680
bool shouldAddContentLevel(cmGeneratorTarget::BundleDirectoryLevel level)
1681
0
{
1682
0
  return level == cmGeneratorTarget::ContentLevel || shouldAddFullLevel(level);
1683
0
}
1684
}
1685
1686
std::string cmGeneratorTarget::GetAppBundleDirectory(
1687
  std::string const& config, BundleDirectoryLevel level) const
1688
0
{
1689
0
  std::string fpath = cmStrCat(
1690
0
    this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact), '.');
1691
0
  cmValue ext = this->GetProperty("BUNDLE_EXTENSION");
1692
0
  fpath += (ext ? *ext : "app");
1693
0
  if (shouldAddContentLevel(level) &&
1694
0
      !this->Makefile->PlatformIsAppleEmbedded()) {
1695
0
    fpath += "/Contents";
1696
0
    if (shouldAddFullLevel(level)) {
1697
0
      fpath += "/MacOS";
1698
0
    }
1699
0
  }
1700
0
  return fpath;
1701
0
}
1702
1703
bool cmGeneratorTarget::IsBundleOnApple() const
1704
0
{
1705
0
  return this->IsFrameworkOnApple() || this->IsAppBundleOnApple() ||
1706
0
    this->IsCFBundleOnApple();
1707
0
}
1708
1709
bool cmGeneratorTarget::IsWin32Executable(std::string const& config) const
1710
0
{
1711
0
  return cmIsOn(cmGeneratorExpression::Evaluate(
1712
0
    this->GetSafeProperty("WIN32_EXECUTABLE"), this->LocalGenerator, config));
1713
0
}
1714
1715
std::string cmGeneratorTarget::GetCFBundleDirectory(
1716
  std::string const& config, BundleDirectoryLevel level) const
1717
0
{
1718
0
  std::string fpath = cmStrCat(
1719
0
    this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact), '.');
1720
0
  std::string ext;
1721
0
  if (cmValue p = this->GetProperty("BUNDLE_EXTENSION")) {
1722
0
    ext = *p;
1723
0
  } else {
1724
0
    if (this->IsXCTestOnApple()) {
1725
0
      ext = "xctest";
1726
0
    } else {
1727
0
      ext = "bundle";
1728
0
    }
1729
0
  }
1730
0
  fpath += ext;
1731
0
  if (shouldAddContentLevel(level) &&
1732
0
      !this->Makefile->PlatformIsAppleEmbedded()) {
1733
0
    fpath += "/Contents";
1734
0
    if (shouldAddFullLevel(level)) {
1735
0
      fpath += "/MacOS";
1736
0
    }
1737
0
  }
1738
0
  return fpath;
1739
0
}
1740
1741
std::string cmGeneratorTarget::GetFrameworkDirectory(
1742
  std::string const& config, BundleDirectoryLevel level) const
1743
0
{
1744
0
  std::string fpath = cmStrCat(
1745
0
    this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact), '.');
1746
0
  cmValue ext = this->GetProperty("BUNDLE_EXTENSION");
1747
0
  fpath += (ext ? *ext : "framework");
1748
0
  if (shouldAddFullLevel(level) &&
1749
0
      !this->Makefile->PlatformIsAppleEmbedded()) {
1750
0
    fpath += "/Versions/";
1751
0
    fpath += this->GetFrameworkVersion();
1752
0
  }
1753
0
  return fpath;
1754
0
}
1755
1756
std::string cmGeneratorTarget::GetFullName(
1757
  std::string const& config, cmStateEnums::ArtifactType artifact) const
1758
0
{
1759
0
  if (this->IsImported()) {
1760
0
    return this->GetFullNameImported(config, artifact);
1761
0
  }
1762
0
  return this->GetFullNameInternal(config, artifact);
1763
0
}
1764
1765
std::string cmGeneratorTarget::GetInstallNameDirForBuildTree(
1766
  std::string const& config) const
1767
0
{
1768
0
  if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
1769
1770
    // If building directly for installation then the build tree install_name
1771
    // is the same as the install tree.
1772
0
    if (this->MacOSXUseInstallNameDir()) {
1773
0
      std::string installPrefix =
1774
0
        this->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
1775
0
      return this->GetInstallNameDirForInstallTree(config, installPrefix);
1776
0
    }
1777
1778
    // Use the build tree directory for the target.
1779
0
    if (this->CanGenerateInstallNameDir(INSTALL_NAME_FOR_BUILD)) {
1780
0
      std::string dir;
1781
0
      if (this->MacOSXRpathInstallNameDirDefault()) {
1782
0
        dir = "@rpath";
1783
0
      } else {
1784
0
        dir = this->GetDirectory(config);
1785
0
      }
1786
0
      dir += "/";
1787
0
      return dir;
1788
0
    }
1789
0
  }
1790
0
  return "";
1791
0
}
1792
1793
std::string cmGeneratorTarget::GetInstallNameDirForInstallTree(
1794
  std::string const& config, std::string const& installPrefix) const
1795
0
{
1796
0
  if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
1797
0
    std::string dir;
1798
0
    cmValue install_name_dir = this->GetProperty("INSTALL_NAME_DIR");
1799
1800
0
    if (this->CanGenerateInstallNameDir(INSTALL_NAME_FOR_INSTALL)) {
1801
0
      if (cmNonempty(install_name_dir)) {
1802
0
        dir = *install_name_dir;
1803
0
        cmGeneratorExpression::ReplaceInstallPrefix(dir, installPrefix);
1804
0
        dir =
1805
0
          cmGeneratorExpression::Evaluate(dir, this->LocalGenerator, config);
1806
0
        if (!dir.empty()) {
1807
0
          dir = cmStrCat(dir, '/');
1808
0
        }
1809
0
      }
1810
0
    }
1811
0
    if (!install_name_dir) {
1812
0
      if (this->MacOSXRpathInstallNameDirDefault()) {
1813
0
        dir = "@rpath/";
1814
0
      }
1815
0
    }
1816
0
    return dir;
1817
0
  }
1818
0
  return "";
1819
0
}
1820
1821
cmListFileBacktrace cmGeneratorTarget::GetBacktrace() const
1822
0
{
1823
0
  return this->Target->GetBacktrace();
1824
0
}
1825
1826
std::set<BT<std::pair<std::string, bool>>> const&
1827
cmGeneratorTarget::GetUtilities() const
1828
0
{
1829
0
  return this->Target->GetUtilities();
1830
0
}
1831
1832
bool cmGeneratorTarget::HaveWellDefinedOutputFiles() const
1833
0
{
1834
0
  return this->GetType() == cmStateEnums::STATIC_LIBRARY ||
1835
0
    this->GetType() == cmStateEnums::SHARED_LIBRARY ||
1836
0
    this->GetType() == cmStateEnums::MODULE_LIBRARY ||
1837
0
    this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
1838
0
    this->GetType() == cmStateEnums::EXECUTABLE;
1839
0
}
1840
1841
std::string const* cmGeneratorTarget::GetExportMacro() const
1842
0
{
1843
  // Define the symbol for targets that export symbols.
1844
0
  if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
1845
0
      this->GetType() == cmStateEnums::MODULE_LIBRARY ||
1846
0
      this->IsExecutableWithExports()) {
1847
0
    if (cmValue custom_export_name = this->GetProperty("DEFINE_SYMBOL")) {
1848
0
      this->ExportMacro = *custom_export_name;
1849
0
    } else {
1850
0
      std::string in = cmStrCat(this->GetName(), "_EXPORTS");
1851
0
      this->ExportMacro = cmSystemTools::MakeCidentifier(in);
1852
0
    }
1853
0
    return &this->ExportMacro;
1854
0
  }
1855
0
  return nullptr;
1856
0
}
1857
1858
cmList const& cmGeneratorTarget::GetSharedLibraryCompileDefs(
1859
  std::string const& config) const
1860
0
{
1861
0
  {
1862
0
    auto it = this->SharedLibraryCompileDefs.find(config);
1863
0
    if (it != this->SharedLibraryCompileDefs.end()) {
1864
0
      return it->second;
1865
0
    }
1866
0
  }
1867
1868
0
  auto emplaceResult =
1869
0
    this->SharedLibraryCompileDefs.emplace(config, cmList{});
1870
0
  auto& defs = emplaceResult.first->second;
1871
0
  if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
1872
0
      this->GetType() != cmStateEnums::MODULE_LIBRARY) {
1873
0
    return defs;
1874
0
  }
1875
1876
0
  if (this->GetPolicyStatusCMP0203() != cmPolicies::NEW) {
1877
0
    return defs;
1878
0
  }
1879
1880
0
  auto linkerLang = this->GetLinkerLanguage(config);
1881
0
  auto definitionVar =
1882
0
    cmStrCat("CMAKE_", linkerLang, "_SHARED_LIBRARY_COMPILE_DEFINITIONS");
1883
0
  defs = this->Makefile->GetSafeDefinition(definitionVar);
1884
1885
0
  return defs;
1886
0
}
1887
1888
cmGeneratorTarget::NameComponents const&
1889
cmGeneratorTarget::GetFullNameComponents(
1890
  std::string const& config, cmStateEnums::ArtifactType artifact) const
1891
0
{
1892
0
  return this->GetFullNameInternalComponents(config, artifact);
1893
0
}
1894
1895
std::string cmGeneratorTarget::BuildBundleDirectory(
1896
  std::string const& base, std::string const& config,
1897
  BundleDirectoryLevel level) const
1898
0
{
1899
0
  std::string fpath = base;
1900
0
  if (this->IsAppBundleOnApple()) {
1901
0
    fpath += this->GetAppBundleDirectory(config, level);
1902
0
  }
1903
0
  if (this->IsFrameworkOnApple()) {
1904
0
    fpath += this->GetFrameworkDirectory(config, level);
1905
0
  }
1906
0
  if (this->IsCFBundleOnApple()) {
1907
0
    fpath += this->GetCFBundleDirectory(config, level);
1908
0
  }
1909
0
  return fpath;
1910
0
}
1911
1912
std::string cmGeneratorTarget::GetMacContentDirectory(
1913
  std::string const& config, cmStateEnums::ArtifactType artifact) const
1914
0
{
1915
  // Start with the output directory for the target.
1916
0
  std::string fpath = cmStrCat(this->GetDirectory(config, artifact), '/');
1917
0
  BundleDirectoryLevel level = ContentLevel;
1918
0
  if (this->IsFrameworkOnApple()) {
1919
    // additional files with a framework go into the version specific
1920
    // directory
1921
0
    level = FullLevel;
1922
0
  }
1923
0
  fpath = this->BuildBundleDirectory(fpath, config, level);
1924
0
  return fpath;
1925
0
}
1926
1927
std::string cmGeneratorTarget::GetEffectiveFolderName() const
1928
0
{
1929
0
  std::string effectiveFolder;
1930
1931
0
  if (!this->GlobalGenerator->UseFolderProperty()) {
1932
0
    return effectiveFolder;
1933
0
  }
1934
1935
0
  cmValue targetFolder = this->GetProperty("FOLDER");
1936
0
  if (targetFolder) {
1937
0
    effectiveFolder += *targetFolder;
1938
0
  }
1939
1940
0
  return effectiveFolder;
1941
0
}
1942
1943
cmGeneratorTarget::CompileInfo const* cmGeneratorTarget::GetCompileInfo(
1944
  std::string const& config) const
1945
0
{
1946
  // There is no compile information for imported targets.
1947
0
  if (this->IsImported()) {
1948
0
    return nullptr;
1949
0
  }
1950
1951
0
  if (this->GetType() > cmStateEnums::OBJECT_LIBRARY) {
1952
0
    std::string msg = cmStrCat("cmTarget::GetCompileInfo called for ",
1953
0
                               this->GetName(), " which has type ",
1954
0
                               cmState::GetTargetTypeName(this->GetType()));
1955
0
    this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
1956
0
    return nullptr;
1957
0
  }
1958
1959
  // Lookup/compute/cache the compile information for this configuration.
1960
0
  std::string config_upper;
1961
0
  if (!config.empty()) {
1962
0
    config_upper = cmSystemTools::UpperCase(config);
1963
0
  }
1964
0
  auto i = this->CompileInfoMap.find(config_upper);
1965
0
  if (i == this->CompileInfoMap.end()) {
1966
0
    CompileInfo info;
1967
0
    this->ComputePDBOutputDir("COMPILE_PDB", config, info.CompilePdbDir);
1968
0
    CompileInfoMapType::value_type entry(config_upper, info);
1969
0
    i = this->CompileInfoMap.insert(entry).first;
1970
0
  }
1971
0
  return &i->second;
1972
0
}
1973
1974
cmGeneratorTarget::ModuleDefinitionInfo const*
1975
cmGeneratorTarget::GetModuleDefinitionInfo(std::string const& config) const
1976
0
{
1977
  // A module definition file only makes sense on certain target types.
1978
0
  if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
1979
0
      this->GetType() != cmStateEnums::MODULE_LIBRARY &&
1980
0
      !this->IsExecutableWithExports()) {
1981
0
    return nullptr;
1982
0
  }
1983
1984
  // Lookup/compute/cache the compile information for this configuration.
1985
0
  std::string config_upper;
1986
0
  if (!config.empty()) {
1987
0
    config_upper = cmSystemTools::UpperCase(config);
1988
0
  }
1989
0
  auto i = this->ModuleDefinitionInfoMap.find(config_upper);
1990
0
  if (i == this->ModuleDefinitionInfoMap.end()) {
1991
0
    ModuleDefinitionInfo info;
1992
0
    this->ComputeModuleDefinitionInfo(config, info);
1993
0
    ModuleDefinitionInfoMapType::value_type entry(config_upper, info);
1994
0
    i = this->ModuleDefinitionInfoMap.insert(entry).first;
1995
0
  }
1996
0
  return &i->second;
1997
0
}
1998
1999
void cmGeneratorTarget::ComputeModuleDefinitionInfo(
2000
  std::string const& config, ModuleDefinitionInfo& info) const
2001
0
{
2002
0
  this->GetModuleDefinitionSources(info.Sources, config);
2003
0
  info.WindowsExportAllSymbols =
2004
0
    this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS") &&
2005
0
    this->GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS");
2006
0
#if !defined(CMAKE_BOOTSTRAP)
2007
0
  info.DefFileGenerated =
2008
0
    info.WindowsExportAllSymbols || info.Sources.size() > 1;
2009
#else
2010
  // Our __create_def helper is not available during CMake bootstrap.
2011
  info.DefFileGenerated = false;
2012
#endif
2013
0
  if (info.DefFileGenerated) {
2014
0
    info.DefFile =
2015
0
      this->GetObjectDirectory(config) /* has slash */ + "exports.def";
2016
0
  } else if (!info.Sources.empty()) {
2017
0
    info.DefFile = info.Sources.front()->GetFullPath();
2018
0
  }
2019
0
}
2020
2021
bool cmGeneratorTarget::IsAIX() const
2022
0
{
2023
0
  return this->Target->IsAIX();
2024
0
}
2025
2026
bool cmGeneratorTarget::IsApple() const
2027
0
{
2028
0
  return this->Target->IsApple();
2029
0
}
2030
2031
bool cmGeneratorTarget::IsDLLPlatform() const
2032
0
{
2033
0
  return this->Target->IsDLLPlatform();
2034
0
}
2035
2036
void cmGeneratorTarget::GetAutoUicOptions(std::vector<std::string>& result,
2037
                                          std::string const& config) const
2038
0
{
2039
0
  char const* prop =
2040
0
    this->GetLinkInterfaceDependentStringProperty("AUTOUIC_OPTIONS", config);
2041
0
  if (!prop) {
2042
0
    return;
2043
0
  }
2044
2045
0
  cm::GenEx::Context context(this->LocalGenerator, config);
2046
0
  cmGeneratorExpressionDAGChecker dagChecker{
2047
0
    this, "AUTOUIC_OPTIONS", nullptr, nullptr, context,
2048
0
  };
2049
0
  cmExpandList(cmGeneratorExpression::Evaluate(
2050
0
                 prop, context.LG, context.Config, this, &dagChecker),
2051
0
               result);
2052
0
}
2053
2054
void cmGeneratorTarget::TraceDependencies()
2055
0
{
2056
  // CMake-generated targets have no dependencies to trace.  Normally tracing
2057
  // would find nothing anyway, but when building CMake itself the "install"
2058
  // target command ends up referencing the "cmake" target but we do not
2059
  // really want the dependency because "install" depend on "all" anyway.
2060
0
  if (this->GetType() == cmStateEnums::GLOBAL_TARGET) {
2061
0
    return;
2062
0
  }
2063
2064
  // Use a helper object to trace the dependencies.
2065
0
  cmTargetTraceDependencies tracer(this);
2066
0
  tracer.Trace();
2067
0
}
2068
2069
std::string cmGeneratorTarget::GetCompilePDBDirectory(
2070
  std::string const& config) const
2071
0
{
2072
0
  if (CompileInfo const* info = this->GetCompileInfo(config)) {
2073
0
    return info->CompilePdbDir;
2074
0
  }
2075
0
  return "";
2076
0
}
2077
2078
std::vector<std::string> cmGeneratorTarget::GetAppleArchs(
2079
  std::string const& config, cm::optional<std::string> lang) const
2080
0
{
2081
0
  cmList archList;
2082
0
  if (!this->IsApple()) {
2083
0
    return std::move(archList.data());
2084
0
  }
2085
0
  cmValue archs = nullptr;
2086
0
  if (!config.empty()) {
2087
0
    std::string defVarName =
2088
0
      cmStrCat("OSX_ARCHITECTURES_", cmSystemTools::UpperCase(config));
2089
0
    archs = this->GetProperty(defVarName);
2090
0
  }
2091
0
  if (!archs) {
2092
0
    archs = this->GetProperty("OSX_ARCHITECTURES");
2093
0
  }
2094
0
  if (archs) {
2095
0
    archList.assign(*archs);
2096
0
  }
2097
0
  if (archList.empty() &&
2098
      // Fall back to a default architecture if no compiler target is set.
2099
0
      (!lang ||
2100
0
       this->Makefile
2101
0
         ->GetDefinition(cmStrCat("CMAKE_", *lang, "_COMPILER_TARGET"))
2102
0
         .IsEmpty())) {
2103
0
    archList.assign(
2104
0
      this->Makefile->GetDefinition("_CMAKE_APPLE_ARCHS_DEFAULT"));
2105
0
  }
2106
0
  return std::move(archList.data());
2107
0
}
2108
2109
namespace {
2110
2111
bool IsSupportedClassifiedFlagsLanguage(std::string const& lang)
2112
0
{
2113
0
  return lang == "CXX"_s;
2114
0
}
2115
2116
bool CanUseCompilerLauncher(std::string const& lang)
2117
0
{
2118
  // Also found in `cmCommonTargetGenerator::GetCompilerLauncher`.
2119
0
  return lang == "C"_s || lang == "CXX"_s || lang == "Fortran"_s ||
2120
0
    lang == "CUDA"_s || lang == "HIP"_s || lang == "ISPC"_s ||
2121
0
    lang == "OBJC"_s || lang == "OBJCXX"_s;
2122
0
}
2123
2124
// FIXME: return a vector of `cm::string_view` instead to avoid lots of tiny
2125
// allocations.
2126
std::vector<std::string> SplitFlags(std::string const& flags)
2127
0
{
2128
0
  std::vector<std::string> options;
2129
2130
#ifdef _WIN32
2131
  cmSystemTools::ParseWindowsCommandLine(flags.c_str(), options);
2132
#else
2133
0
  cmSystemTools::ParseUnixCommandLine(flags.c_str(), options);
2134
0
#endif
2135
2136
0
  return options;
2137
0
}
2138
2139
}
2140
2141
cmGeneratorTarget::ClassifiedFlags
2142
cmGeneratorTarget::GetClassifiedFlagsForSource(cmSourceFile const* sf,
2143
                                               std::string const& config)
2144
0
{
2145
0
  auto& sourceFlagsCache = this->Configs[config].SourceFlags;
2146
0
  auto cacheEntry = sourceFlagsCache.lower_bound(sf);
2147
0
  if (cacheEntry != sourceFlagsCache.end() && cacheEntry->first == sf) {
2148
0
    return cacheEntry->second;
2149
0
  }
2150
2151
0
  ClassifiedFlags flags;
2152
0
  std::string const& lang = sf->GetLanguage();
2153
2154
0
  if (!IsSupportedClassifiedFlagsLanguage(lang)) {
2155
0
    return flags;
2156
0
  }
2157
2158
0
  auto* const lg = this->GetLocalGenerator();
2159
0
  auto const* const mf = this->Makefile;
2160
2161
  // Compute the compiler launcher flags.
2162
0
  if (CanUseCompilerLauncher(lang)) {
2163
    // Compiler launchers are all execution flags and should not be relevant to
2164
    // the actual compilation.
2165
0
    FlagClassification cls = FlagClassification::ExecutionFlag;
2166
0
    FlagKind kind = FlagKind::NotAFlag;
2167
2168
0
    std::string const clauncher_prop = cmStrCat(lang, "_COMPILER_LAUNCHER");
2169
0
    cmValue clauncher = this->GetProperty(clauncher_prop);
2170
0
    std::string const evaluatedClauncher = cmGeneratorExpression::Evaluate(
2171
0
      *clauncher, lg, config, this, nullptr, this, lang);
2172
2173
0
    for (auto const& flag : SplitFlags(evaluatedClauncher)) {
2174
0
      flags.emplace_back(cls, kind, flag);
2175
0
    }
2176
0
  }
2177
2178
0
  ClassifiedFlags define_flags;
2179
0
  ClassifiedFlags include_flags;
2180
0
  ClassifiedFlags compile_flags;
2181
2182
0
  SourceVariables sfVars = this->GetSourceVariables(sf, config);
2183
2184
  // Compute language flags.
2185
0
  {
2186
0
    FlagClassification cls = FlagClassification::BaselineFlag;
2187
0
    FlagKind kind = FlagKind::Compile;
2188
2189
0
    std::string mfFlags;
2190
    // Explicitly add the explicit language flag before any other flag
2191
    // so user flags can override it.
2192
0
    this->AddExplicitLanguageFlags(mfFlags, *sf);
2193
2194
0
    for (auto const& flag : SplitFlags(mfFlags)) {
2195
0
      flags.emplace_back(cls, kind, flag);
2196
0
    }
2197
0
  }
2198
2199
0
  std::unordered_map<std::string, std::string> pchSources;
2200
0
  std::string filterArch;
2201
2202
0
  {
2203
0
    std::vector<std::string> pchArchs = this->GetPchArchs(config, lang);
2204
2205
0
    for (std::string const& arch : pchArchs) {
2206
0
      std::string const pchSource = this->GetPchSource(config, lang, arch);
2207
0
      if (pchSource == sf->GetFullPath()) {
2208
0
        filterArch = arch;
2209
0
      }
2210
0
      if (!pchSource.empty()) {
2211
0
        pchSources.insert(std::make_pair(pchSource, arch));
2212
0
      }
2213
0
    }
2214
0
  }
2215
2216
  // Compute target-wide flags.
2217
0
  {
2218
0
    FlagClassification cls = FlagClassification::BaselineFlag;
2219
2220
    // Compile flags
2221
0
    {
2222
0
      FlagKind kind = FlagKind::Compile;
2223
0
      std::string targetFlags;
2224
2225
0
      lg->GetTargetCompileFlags(this, config, lang, targetFlags, filterArch);
2226
2227
0
      for (auto&& flag : SplitFlags(targetFlags)) {
2228
0
        compile_flags.emplace_back(cls, kind, std::move(flag));
2229
0
      }
2230
0
    }
2231
2232
    // Define flags
2233
0
    {
2234
0
      FlagKind kind = FlagKind::Definition;
2235
0
      std::set<std::string> defines;
2236
2237
0
      lg->GetTargetDefines(this, config, lang, defines);
2238
2239
0
      std::string defineFlags;
2240
0
      lg->JoinDefines(defines, defineFlags, lang);
2241
2242
0
      for (auto&& flag : SplitFlags(defineFlags)) {
2243
0
        define_flags.emplace_back(cls, kind, std::move(flag));
2244
0
      }
2245
0
    }
2246
2247
    // Include flags
2248
0
    {
2249
0
      FlagKind kind = FlagKind::Include;
2250
0
      std::vector<std::string> includes;
2251
2252
0
      lg->GetIncludeDirectories(includes, this, lang, config);
2253
0
      auto includeFlags =
2254
0
        lg->GetIncludeFlags(includes, this, lang, config, false);
2255
2256
0
      for (auto&& flag : SplitFlags(includeFlags)) {
2257
0
        include_flags.emplace_back(cls, kind, std::move(flag));
2258
0
      }
2259
0
    }
2260
0
  }
2261
2262
0
  std::string const COMPILE_FLAGS("COMPILE_FLAGS");
2263
0
  std::string const COMPILE_OPTIONS("COMPILE_OPTIONS");
2264
2265
0
  cmGeneratorExpressionInterpreter genexInterpreter(lg, config, this, lang);
2266
2267
  // Source-specific flags.
2268
0
  {
2269
0
    FlagClassification cls = FlagClassification::PrivateFlag;
2270
0
    FlagKind kind = FlagKind::Compile;
2271
2272
0
    std::string sourceFlags;
2273
2274
0
    if (cmValue cflags = sf->GetProperty(COMPILE_FLAGS)) {
2275
0
      lg->AppendFlags(sourceFlags,
2276
0
                      genexInterpreter.Evaluate(*cflags, COMPILE_FLAGS));
2277
0
    }
2278
2279
0
    if (cmValue coptions = sf->GetProperty(COMPILE_OPTIONS)) {
2280
0
      lg->AppendCompileOptions(
2281
0
        sourceFlags, genexInterpreter.Evaluate(*coptions, COMPILE_OPTIONS));
2282
0
    }
2283
2284
0
    for (auto&& flag : SplitFlags(sourceFlags)) {
2285
0
      compile_flags.emplace_back(cls, kind, std::move(flag));
2286
0
    }
2287
2288
    // Dependency tracking flags.
2289
0
    {
2290
0
      if (!sfVars.DependencyFlags.empty()) {
2291
0
        cmRulePlaceholderExpander::RuleVariables vars;
2292
0
        auto rulePlaceholderExpander = lg->CreateRulePlaceholderExpander();
2293
2294
0
        vars.DependencyFile = sfVars.DependencyFile.c_str();
2295
0
        vars.DependencyTarget = sfVars.DependencyTarget.c_str();
2296
2297
0
        std::string depfileFlags = sfVars.DependencyFlags;
2298
0
        rulePlaceholderExpander->ExpandRuleVariables(lg, depfileFlags, vars);
2299
0
        for (auto&& flag : SplitFlags(depfileFlags)) {
2300
0
          compile_flags.emplace_back(FlagClassification::LocationFlag,
2301
0
                                     FlagKind::BuildSystem, std::move(flag));
2302
0
        }
2303
0
      }
2304
0
    }
2305
0
  }
2306
2307
  // Precompiled headers.
2308
0
  {
2309
0
    FlagClassification cls = FlagClassification::PrivateFlag;
2310
0
    FlagKind kind = FlagKind::Compile;
2311
2312
0
    std::string pchFlags;
2313
2314
    // Add precompile headers compile options.
2315
0
    if (!sf->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
2316
0
      if (!pchSources.empty()) {
2317
0
        std::string pchOptions;
2318
0
        auto pchIt = pchSources.find(sf->GetFullPath());
2319
0
        if (pchIt != pchSources.end()) {
2320
0
          pchOptions =
2321
0
            this->GetPchCreateCompileOptions(config, lang, pchIt->second);
2322
0
        } else {
2323
0
          pchOptions = this->GetPchUseCompileOptions(config, lang);
2324
0
        }
2325
2326
0
        this->LocalGenerator->AppendCompileOptions(
2327
0
          pchFlags, genexInterpreter.Evaluate(pchOptions, COMPILE_OPTIONS));
2328
0
      }
2329
0
    }
2330
2331
0
    for (auto&& flag : SplitFlags(pchFlags)) {
2332
0
      compile_flags.emplace_back(cls, kind, std::move(flag));
2333
0
    }
2334
0
  }
2335
2336
  // C++ module flags.
2337
0
  if (lang == "CXX"_s) {
2338
0
    FlagClassification cls = FlagClassification::LocationFlag;
2339
0
    FlagKind kind = FlagKind::BuildSystem;
2340
2341
0
    std::string bmiFlags;
2342
2343
0
    auto const* fs = this->GetFileSetForSource(config, sf);
2344
0
    if (fs && fs->GetType() == "CXX_MODULES"_s) {
2345
0
      if (lang != "CXX"_s) {
2346
0
        mf->IssueMessage(
2347
0
          MessageType::FATAL_ERROR,
2348
0
          cmStrCat(
2349
0
            "Target \"", this->Target->GetName(), "\" contains the source\n  ",
2350
0
            sf->GetFullPath(), "\nin a file set of type \"", fs->GetType(),
2351
0
            R"(" but the source is not classified as a "CXX" source.)"));
2352
0
      }
2353
2354
0
      if (!this->Target->IsNormal()) {
2355
0
        auto flag = mf->GetSafeDefinition("CMAKE_CXX_MODULE_BMI_ONLY_FLAG");
2356
0
        cmRulePlaceholderExpander::RuleVariables compileObjectVars;
2357
0
        compileObjectVars.Object = sfVars.ObjectFileDir.c_str();
2358
0
        auto rulePlaceholderExpander = lg->CreateRulePlaceholderExpander();
2359
0
        rulePlaceholderExpander->ExpandRuleVariables(lg, flag,
2360
0
                                                     compileObjectVars);
2361
0
        lg->AppendCompileOptions(bmiFlags, flag);
2362
0
      }
2363
0
    }
2364
2365
0
    for (auto&& flag : SplitFlags(bmiFlags)) {
2366
0
      compile_flags.emplace_back(cls, kind, std::move(flag));
2367
0
    }
2368
0
  }
2369
2370
0
  cmRulePlaceholderExpander::RuleVariables vars;
2371
0
  vars.CMTargetName = this->GetName().c_str();
2372
0
  vars.CMTargetType = cmState::GetTargetTypeName(this->GetType()).c_str();
2373
0
  vars.Language = lang.c_str();
2374
2375
0
  auto const sfPath = this->LocalGenerator->ConvertToOutputFormat(
2376
0
    sf->GetFullPath(), cmOutputConverter::SHELL);
2377
2378
  // Compute the base compiler command line. We'll find placeholders and
2379
  // replace them with arguments later in this function.
2380
0
  {
2381
0
    FlagClassification cls = FlagClassification::ExecutionFlag;
2382
0
    FlagKind kind = FlagKind::NotAFlag;
2383
2384
0
    std::string const cmdVar = cmStrCat("CMAKE_", lang, "_COMPILE_OBJECT");
2385
0
    std::string const& compileCmd = mf->GetRequiredDefinition(cmdVar);
2386
0
    cmList compileCmds(compileCmd); // FIXME: which command to use?
2387
0
    std::string& cmd = compileCmds[0];
2388
0
    auto rulePlaceholderExpander = lg->CreateRulePlaceholderExpander();
2389
2390
0
    static std::string const PlaceholderDefines = "__CMAKE_DEFINES";
2391
0
    static std::string const PlaceholderIncludes = "__CMAKE_INCLUDES";
2392
0
    static std::string const PlaceholderFlags = "__CMAKE_FLAGS";
2393
0
    static std::string const PlaceholderSource = "__CMAKE_SOURCE";
2394
2395
0
    vars.Defines = PlaceholderDefines.c_str();
2396
0
    vars.Includes = PlaceholderIncludes.c_str();
2397
0
    vars.TargetPDB = sfVars.TargetPDB.c_str();
2398
0
    vars.TargetCompilePDB = sfVars.TargetCompilePDB.c_str();
2399
0
    vars.Object = sfVars.ObjectFileDir.c_str();
2400
0
    vars.ObjectDir = sfVars.ObjectDir.c_str();
2401
0
    vars.ObjectFileDir = sfVars.ObjectFileDir.c_str();
2402
0
    vars.TargetSupportDir = sfVars.TargetSupportDir.c_str();
2403
0
    vars.Flags = PlaceholderFlags.c_str();
2404
0
    vars.DependencyFile = sfVars.DependencyFile.c_str();
2405
0
    vars.DependencyTarget = sfVars.DependencyTarget.c_str();
2406
0
    vars.Source = PlaceholderSource.c_str();
2407
2408
0
    rulePlaceholderExpander->ExpandRuleVariables(lg, cmd, vars);
2409
0
    for (auto&& flag : SplitFlags(cmd)) {
2410
0
      if (flag == PlaceholderDefines) {
2411
0
        flags.insert(flags.end(), define_flags.begin(), define_flags.end());
2412
0
      } else if (flag == PlaceholderIncludes) {
2413
0
        flags.insert(flags.end(), include_flags.begin(), include_flags.end());
2414
0
      } else if (flag == PlaceholderFlags) {
2415
0
        flags.insert(flags.end(), compile_flags.begin(), compile_flags.end());
2416
0
      } else if (flag == PlaceholderSource) {
2417
0
        flags.emplace_back(FlagClassification::LocationFlag,
2418
0
                           FlagKind::BuildSystem, sfPath);
2419
0
      } else {
2420
0
        flags.emplace_back(cls, kind, std::move(flag));
2421
        // All remaining flags here are build system flags.
2422
0
        kind = FlagKind::BuildSystem;
2423
0
      }
2424
0
    }
2425
0
  }
2426
2427
0
  cacheEntry = sourceFlagsCache.emplace_hint(cacheEntry, sf, std::move(flags));
2428
0
  return cacheEntry->second;
2429
0
}
2430
2431
cmGeneratorTarget::SourceVariables cmGeneratorTarget::GetSourceVariables(
2432
  cmSourceFile const* sf, std::string const& config)
2433
0
{
2434
0
  SourceVariables vars;
2435
0
  auto const language = sf->GetLanguage();
2436
0
  auto const targetType = this->GetType();
2437
0
  auto const* const lg = this->GetLocalGenerator();
2438
0
  auto const* const gg = this->GetGlobalGenerator();
2439
0
  auto const* const mf = this->Makefile;
2440
2441
  // PDB settings.
2442
0
  {
2443
0
    if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
2444
0
        mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID") ||
2445
0
        mf->GetDefinition("MSVC_CUDA_ARCHITECTURE_ID")) {
2446
0
      std::string pdbPath;
2447
0
      std::string compilePdbPath;
2448
0
      if (targetType <= cmStateEnums::OBJECT_LIBRARY) {
2449
0
        compilePdbPath = this->GetCompilePDBPath(config);
2450
0
        if (compilePdbPath.empty()) {
2451
          // Match VS default: `$(IntDir)vc$(PlatformToolsetVersion).pdb`.
2452
          // A trailing slash tells the toolchain to add its default file name.
2453
0
          compilePdbPath = this->GetSupportDirectory();
2454
0
          if (gg->IsMultiConfig()) {
2455
0
            compilePdbPath = cmStrCat(compilePdbPath, '/', config);
2456
0
          }
2457
0
          compilePdbPath += '/';
2458
0
          if (targetType == cmStateEnums::STATIC_LIBRARY) {
2459
            // Match VS default for static libs: `$(IntDir)$(ProjectName).pdb`.
2460
0
            compilePdbPath = cmStrCat(compilePdbPath, this->GetName(), ".pdb");
2461
0
          }
2462
0
        }
2463
0
      }
2464
2465
0
      if (targetType == cmStateEnums::EXECUTABLE ||
2466
0
          targetType == cmStateEnums::STATIC_LIBRARY ||
2467
0
          targetType == cmStateEnums::SHARED_LIBRARY ||
2468
0
          targetType == cmStateEnums::MODULE_LIBRARY) {
2469
0
        pdbPath = cmStrCat(this->GetPDBDirectory(config), '/',
2470
0
                           this->GetPDBName(config));
2471
0
      }
2472
2473
0
      vars.TargetPDB = lg->ConvertToOutputFormat(
2474
0
        gg->ConvertToOutputPath(std::move(pdbPath)), cmOutputConverter::SHELL);
2475
0
      vars.TargetCompilePDB = lg->ConvertToOutputFormat(
2476
0
        gg->ConvertToOutputPath(std::move(compilePdbPath)),
2477
0
        cmOutputConverter::SHELL);
2478
0
    }
2479
0
  }
2480
2481
  // Object settings.
2482
0
  {
2483
0
    std::string const targetSupportDir = lg->MaybeRelativeToTopBinDir(
2484
0
      gg->ConvertToOutputPath(this->GetCMFSupportDirectory()));
2485
0
    std::string const objectDir = gg->ConvertToOutputPath(
2486
0
      cmStrCat(this->GetSupportDirectory(), gg->GetConfigDirectory(config)));
2487
0
    std::string const objectFileName = this->GetObjectName(sf);
2488
0
    std::string const objectFilePath =
2489
0
      cmStrCat(objectDir, '/', objectFileName);
2490
2491
0
    vars.TargetSupportDir =
2492
0
      lg->ConvertToOutputFormat(targetSupportDir, cmOutputConverter::SHELL);
2493
0
    vars.ObjectDir =
2494
0
      lg->ConvertToOutputFormat(objectDir, cmOutputConverter::SHELL);
2495
0
    vars.ObjectFileDir =
2496
0
      lg->ConvertToOutputFormat(objectFilePath, cmOutputConverter::SHELL);
2497
2498
    // Dependency settings.
2499
0
    {
2500
0
      std::string const depfileFormatName =
2501
0
        cmStrCat("CMAKE_", language, "_DEPFILE_FORMAT");
2502
0
      std::string const depfileFormat =
2503
0
        mf->GetSafeDefinition(depfileFormatName);
2504
0
      if (depfileFormat != "msvc"_s) {
2505
0
        std::string const flagsName =
2506
0
          cmStrCat("CMAKE_DEPFILE_FLAGS_", language);
2507
0
        std::string const depfileFlags = mf->GetSafeDefinition(flagsName);
2508
0
        if (!depfileFlags.empty()) {
2509
0
          bool replaceExt = false;
2510
0
          if (!language.empty()) {
2511
0
            std::string const repVar =
2512
0
              cmStrCat("CMAKE_", language, "_DEPFILE_EXTENSION_REPLACE");
2513
0
            replaceExt = mf->IsOn(repVar);
2514
0
          }
2515
0
          std::string const depfilePath = cmStrCat(
2516
0
            objectDir, '/',
2517
0
            replaceExt
2518
0
              ? cmSystemTools::GetFilenameWithoutLastExtension(objectFileName)
2519
0
              : objectFileName,
2520
0
            ".d");
2521
2522
0
          vars.DependencyFlags = depfileFlags;
2523
0
          vars.DependencyTarget = vars.ObjectFileDir;
2524
0
          vars.DependencyFile =
2525
0
            lg->ConvertToOutputFormat(depfilePath, cmOutputConverter::SHELL);
2526
0
        }
2527
0
      }
2528
0
    }
2529
0
  }
2530
2531
0
  return vars;
2532
0
}
2533
2534
void cmGeneratorTarget::AddExplicitLanguageFlags(std::string& flags,
2535
                                                 cmSourceFile const& sf) const
2536
0
{
2537
0
  cmValue lang = sf.GetProperty("LANGUAGE");
2538
0
  if (!lang) {
2539
0
    return;
2540
0
  }
2541
2542
0
  switch (this->GetPolicyStatusCMP0119()) {
2543
0
    case cmPolicies::WARN:
2544
0
      CM_FALLTHROUGH;
2545
0
    case cmPolicies::OLD:
2546
      // The OLD behavior is to not add explicit language flags.
2547
0
      return;
2548
0
    case cmPolicies::NEW:
2549
      // The NEW behavior is to add explicit language flags.
2550
0
      break;
2551
0
  }
2552
2553
0
  this->LocalGenerator->AppendFeatureOptions(flags, *lang,
2554
0
                                             "EXPLICIT_LANGUAGE");
2555
0
}
2556
2557
void cmGeneratorTarget::AddCUDAArchitectureFlags(cmBuildStep compileOrLink,
2558
                                                 std::string const& config,
2559
                                                 std::string& flags) const
2560
0
{
2561
0
  std::string arch = this->GetSafeProperty("CUDA_ARCHITECTURES");
2562
2563
0
  if (arch.empty()) {
2564
0
    switch (this->GetPolicyStatusCMP0104()) {
2565
0
      case cmPolicies::WARN:
2566
0
        if (!this->LocalGenerator->GetCMakeInstance()->GetIsInTryCompile()) {
2567
0
          this->Makefile->IssueMessage(
2568
0
            MessageType::AUTHOR_WARNING,
2569
0
            cmPolicies::GetPolicyWarning(cmPolicies::CMP0104) +
2570
0
              "\nCUDA_ARCHITECTURES is empty for target \"" + this->GetName() +
2571
0
              "\".");
2572
0
        }
2573
0
        CM_FALLTHROUGH;
2574
0
      case cmPolicies::OLD:
2575
0
        break;
2576
0
      default:
2577
0
        this->Makefile->IssueMessage(
2578
0
          MessageType::FATAL_ERROR,
2579
0
          "CUDA_ARCHITECTURES is empty for target \"" + this->GetName() +
2580
0
            "\".");
2581
0
    }
2582
0
  }
2583
2584
  // If CUDA_ARCHITECTURES is false we don't add any architectures.
2585
0
  if (cmIsOff(arch)) {
2586
0
    return;
2587
0
  }
2588
2589
0
  this->AddCUDAArchitectureFlagsImpl(compileOrLink, config, "CUDA",
2590
0
                                     std::move(arch), flags);
2591
0
}
2592
2593
void cmGeneratorTarget::AddCUDAArchitectureFlagsImpl(cmBuildStep compileOrLink,
2594
                                                     std::string const& config,
2595
                                                     std::string const& lang,
2596
                                                     std::string arch,
2597
                                                     std::string& flags) const
2598
0
{
2599
0
  std::string const& compiler = this->Makefile->GetSafeDefinition(
2600
0
    cmStrCat("CMAKE_", lang, "_COMPILER_ID"));
2601
0
  bool const ipoEnabled = this->IsIPOEnabled(lang, config);
2602
2603
  // Check for special modes: `all`, `all-major`.
2604
0
  if (arch == "all" || arch == "all-major") {
2605
0
    if (compiler == "NVIDIA" &&
2606
0
        cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
2607
0
                                      this->Makefile->GetDefinition(cmStrCat(
2608
0
                                        "CMAKE_", lang, "_COMPILER_VERSION")),
2609
0
                                      "11.5")) {
2610
0
      flags = cmStrCat(flags, " -arch=", arch);
2611
0
      return;
2612
0
    }
2613
0
    if (arch == "all") {
2614
0
      arch = *this->Makefile->GetDefinition(
2615
0
        cmStrCat("CMAKE_", lang, "_ARCHITECTURES_ALL"));
2616
0
    } else if (arch == "all-major") {
2617
0
      arch = *this->Makefile->GetDefinition(
2618
0
        cmStrCat("CMAKE_", lang, "_ARCHITECTURES_ALL_MAJOR"));
2619
0
    }
2620
0
  } else if (arch == "native") {
2621
0
    cmValue native = this->Makefile->GetDefinition(
2622
0
      cmStrCat("CMAKE_", lang, "_ARCHITECTURES_NATIVE"));
2623
0
    if (native.IsEmpty()) {
2624
0
      this->Makefile->IssueMessage(
2625
0
        MessageType::FATAL_ERROR,
2626
0
        cmStrCat(lang,
2627
0
                 "_ARCHITECTURES is set to \"native\", but no NVIDIA GPU was "
2628
0
                 "detected."));
2629
0
    }
2630
0
    if (compiler == "NVIDIA" &&
2631
0
        cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
2632
0
                                      this->Makefile->GetDefinition(cmStrCat(
2633
0
                                        "CMAKE_", lang, "_COMPILER_VERSION")),
2634
0
                                      "11.6")) {
2635
0
      flags = cmStrCat(flags, " -arch=", arch);
2636
0
      return;
2637
0
    }
2638
0
    arch = *native;
2639
0
  }
2640
2641
0
  struct CudaArchitecture
2642
0
  {
2643
0
    std::string name;
2644
0
    bool real{ true };
2645
0
    bool virtual_{ true };
2646
0
  };
2647
0
  std::vector<CudaArchitecture> architectures;
2648
2649
0
  {
2650
0
    cmList options(arch);
2651
2652
0
    for (auto& option : options) {
2653
0
      CudaArchitecture architecture;
2654
2655
      // Architecture name is up to the first specifier.
2656
0
      std::size_t pos = option.find_first_of('-');
2657
0
      architecture.name = option.substr(0, pos);
2658
2659
0
      if (pos != std::string::npos) {
2660
0
        cm::string_view specifier{ option.c_str() + pos + 1,
2661
0
                                   option.length() - pos - 1 };
2662
2663
0
        if (specifier == "real") {
2664
0
          architecture.real = true;
2665
0
          architecture.virtual_ = false;
2666
0
        } else if (specifier == "virtual") {
2667
0
          architecture.real = false;
2668
0
          architecture.virtual_ = true;
2669
0
        } else {
2670
0
          this->Makefile->IssueMessage(
2671
0
            MessageType::FATAL_ERROR,
2672
0
            "Unknown CUDA architecture specifier \"" + std::string(specifier) +
2673
0
              "\".");
2674
0
        }
2675
0
      }
2676
2677
0
      architectures.emplace_back(architecture);
2678
0
    }
2679
0
  }
2680
2681
0
  if (compiler == "NVIDIA") {
2682
0
    if (ipoEnabled && compileOrLink == cmBuildStep::Link) {
2683
0
      if (cmValue cudaIPOFlags = this->Makefile->GetDefinition(
2684
0
            cmStrCat("CMAKE_", lang, "_LINK_OPTIONS_IPO"))) {
2685
0
        flags += *cudaIPOFlags;
2686
0
      }
2687
0
    }
2688
2689
0
    for (CudaArchitecture& architecture : architectures) {
2690
0
      flags = cmStrCat(std::move(flags), " \"--generate-code=arch=compute_",
2691
0
                       architecture.name, ",code=[");
2692
2693
0
      if (architecture.virtual_) {
2694
0
        flags = cmStrCat(std::move(flags), "compute_", architecture.name);
2695
2696
0
        if (ipoEnabled || architecture.real) {
2697
0
          flags += ',';
2698
0
        }
2699
0
      }
2700
2701
0
      if (ipoEnabled) {
2702
0
        if (compileOrLink == cmBuildStep::Compile) {
2703
0
          flags = cmStrCat(std::move(flags), "lto_", architecture.name);
2704
0
        } else if (compileOrLink == cmBuildStep::Link) {
2705
0
          flags = cmStrCat(std::move(flags), "sm_", architecture.name);
2706
0
        }
2707
0
      } else if (architecture.real) {
2708
0
        flags = cmStrCat(std::move(flags), "sm_", architecture.name);
2709
0
      }
2710
2711
0
      flags += "]\"";
2712
0
    }
2713
0
  } else if (compiler == "Clang" && compileOrLink == cmBuildStep::Compile) {
2714
0
    for (CudaArchitecture& architecture : architectures) {
2715
0
      flags =
2716
0
        cmStrCat(std::move(flags), " --cuda-gpu-arch=sm_", architecture.name);
2717
2718
0
      if (!architecture.real) {
2719
0
        this->Makefile->IssueMessage(
2720
0
          MessageType::WARNING,
2721
0
          "Clang doesn't support disabling CUDA real code generation.");
2722
0
      }
2723
2724
0
      if (!architecture.virtual_) {
2725
0
        flags = cmStrCat(std::move(flags), " --no-cuda-include-ptx=sm_",
2726
0
                         architecture.name);
2727
0
      }
2728
0
    }
2729
0
  }
2730
0
}
2731
2732
void cmGeneratorTarget::AddISPCTargetFlags(std::string& flags) const
2733
0
{
2734
0
  std::string const& arch = this->GetSafeProperty("ISPC_INSTRUCTION_SETS");
2735
2736
  // If ISPC_TARGET is false we don't add any architectures.
2737
0
  if (cmIsOff(arch)) {
2738
0
    return;
2739
0
  }
2740
2741
0
  std::string const& compiler =
2742
0
    this->Makefile->GetSafeDefinition("CMAKE_ISPC_COMPILER_ID");
2743
2744
0
  if (compiler == "Intel") {
2745
0
    cmList targets(arch);
2746
0
    if (!targets.empty()) {
2747
0
      flags += cmStrCat(" --target=", cmWrap("", targets, "", ","));
2748
0
    }
2749
0
  }
2750
0
}
2751
2752
void cmGeneratorTarget::AddHIPArchitectureFlags(cmBuildStep compileOrLink,
2753
                                                std::string const& config,
2754
                                                std::string& flags) const
2755
0
{
2756
0
  std::string arch = this->GetSafeProperty("HIP_ARCHITECTURES");
2757
2758
0
  if (arch.empty()) {
2759
0
    this->Makefile->IssueMessage(
2760
0
      MessageType::FATAL_ERROR,
2761
0
      cmStrCat("HIP_ARCHITECTURES is empty for target \"", this->GetName(),
2762
0
               "\"."));
2763
0
  }
2764
2765
  // If HIP_ARCHITECTURES is false we don't add any architectures.
2766
0
  if (cmIsOff(arch)) {
2767
0
    return;
2768
0
  }
2769
2770
0
  if (this->Makefile->GetSafeDefinition("CMAKE_HIP_PLATFORM") == "nvidia") {
2771
0
    this->AddCUDAArchitectureFlagsImpl(compileOrLink, config, "HIP",
2772
0
                                       std::move(arch), flags);
2773
0
    return;
2774
0
  }
2775
2776
  // For spirv platform, chipStar handles targeting via the hip package
2777
0
  if (this->Makefile->GetSafeDefinition("CMAKE_HIP_PLATFORM") == "spirv") {
2778
0
    return;
2779
0
  }
2780
2781
0
  cmList options(arch);
2782
2783
0
  for (std::string& option : options) {
2784
0
    flags += " --offload-arch=" + option;
2785
0
  }
2786
0
}
2787
2788
void cmGeneratorTarget::AddCUDAToolkitFlags(std::string& flags) const
2789
0
{
2790
0
  std::string const& compiler =
2791
0
    this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID");
2792
2793
0
  if (compiler == "Clang") {
2794
    // Pass CUDA toolkit explicitly to Clang.
2795
    // Clang's searching for the system CUDA toolkit isn't very good and it's
2796
    // expected the user will explicitly pass the toolkit path.
2797
    // This also avoids Clang having to search for the toolkit on every
2798
    // invocation.
2799
0
    std::string toolkitRoot =
2800
0
      this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_LIBRARY_ROOT");
2801
2802
0
    if (!toolkitRoot.empty()) {
2803
0
      flags += " --cuda-path=" +
2804
0
        this->LocalGenerator->ConvertToOutputFormat(toolkitRoot,
2805
0
                                                    cmOutputConverter::SHELL);
2806
0
    }
2807
0
  }
2808
0
}
2809
2810
//----------------------------------------------------------------------------
2811
std::string cmGeneratorTarget::GetFeatureSpecificLinkRuleVariable(
2812
  std::string const& var, std::string const& lang,
2813
  std::string const& config) const
2814
0
{
2815
0
  if (this->IsIPOEnabled(lang, config)) {
2816
0
    std::string varIPO = var + "_IPO";
2817
0
    if (this->Makefile->IsDefinitionSet(varIPO)) {
2818
0
      return varIPO;
2819
0
    }
2820
0
  }
2821
2822
0
  return var;
2823
0
}
2824
2825
//----------------------------------------------------------------------------
2826
std::string cmGeneratorTarget::GetCreateRuleVariable(
2827
  std::string const& lang, std::string const& config) const
2828
0
{
2829
0
  switch (this->GetType()) {
2830
0
    case cmStateEnums::STATIC_LIBRARY: {
2831
0
      std::string var = cmStrCat("CMAKE_", lang, "_CREATE_STATIC_LIBRARY");
2832
0
      return this->GetFeatureSpecificLinkRuleVariable(var, lang, config);
2833
0
    }
2834
0
    case cmStateEnums::SHARED_LIBRARY:
2835
0
      if (this->IsArchivedAIXSharedLibrary()) {
2836
0
        return cmStrCat("CMAKE_", lang, "_CREATE_SHARED_LIBRARY_ARCHIVE");
2837
0
      }
2838
0
      return cmStrCat("CMAKE_", lang, "_CREATE_SHARED_LIBRARY");
2839
0
    case cmStateEnums::MODULE_LIBRARY:
2840
0
      return cmStrCat("CMAKE_", lang, "_CREATE_SHARED_MODULE");
2841
0
    case cmStateEnums::EXECUTABLE:
2842
0
      if (this->IsExecutableWithExports()) {
2843
0
        std::string linkExeWithExports =
2844
0
          cmStrCat("CMAKE_", lang, "_LINK_EXECUTABLE_WITH_EXPORTS");
2845
0
        if (this->Makefile->IsDefinitionSet(linkExeWithExports)) {
2846
0
          return linkExeWithExports;
2847
0
        }
2848
0
      }
2849
0
      return cmStrCat("CMAKE_", lang, "_LINK_EXECUTABLE");
2850
0
    default:
2851
0
      break;
2852
0
  }
2853
0
  return {};
2854
0
}
2855
2856
//----------------------------------------------------------------------------
2857
std::string cmGeneratorTarget::GetClangTidyExportFixesDirectory(
2858
  std::string const& lang) const
2859
0
{
2860
0
  cmValue val =
2861
0
    this->GetProperty(cmStrCat(lang, "_CLANG_TIDY_EXPORT_FIXES_DIR"));
2862
0
  if (!cmNonempty(val)) {
2863
0
    return {};
2864
0
  }
2865
2866
0
  std::string path = *val;
2867
0
  if (!cmSystemTools::FileIsFullPath(path)) {
2868
0
    path =
2869
0
      cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(), '/', path);
2870
0
  }
2871
0
  return cmSystemTools::CollapseFullPath(path);
2872
0
}
2873
2874
struct CycleWatcher
2875
{
2876
  CycleWatcher(bool& flag)
2877
0
    : Flag(flag)
2878
0
  {
2879
0
    this->Flag = true;
2880
0
  }
2881
0
  ~CycleWatcher() { this->Flag = false; }
2882
  bool& Flag;
2883
};
2884
2885
cmGeneratorTarget const* cmGeneratorTarget::GetPchReuseTarget() const
2886
0
{
2887
0
  if (this->ComputingPchReuse) {
2888
    // TODO: Get the full cycle.
2889
0
    if (!this->PchReuseCycleDetected) {
2890
0
      this->Makefile->IssueMessage(
2891
0
        MessageType::FATAL_ERROR,
2892
0
        cmStrCat("Circular PCH reuse target involving '", this->GetName(),
2893
0
                 '\''));
2894
0
    }
2895
0
    this->PchReuseCycleDetected = true;
2896
0
    return nullptr;
2897
0
  }
2898
0
  CycleWatcher watch(this->ComputingPchReuse);
2899
0
  (void)watch;
2900
0
  cmValue pchReuseFrom = this->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
2901
0
  if (!pchReuseFrom) {
2902
0
    return nullptr;
2903
0
  }
2904
0
  cmGeneratorTarget const* generatorTarget =
2905
0
    this->GetGlobalGenerator()->FindGeneratorTarget(*pchReuseFrom);
2906
0
  if (!generatorTarget) {
2907
0
    this->Makefile->IssueMessage(
2908
0
      MessageType::FATAL_ERROR,
2909
0
      cmStrCat(
2910
0
        "Target \"", *pchReuseFrom, "\" for the \"", this->GetName(),
2911
0
        R"(" target's "PRECOMPILE_HEADERS_REUSE_FROM" property does not exist.)"));
2912
0
  }
2913
0
  if (this->GetProperty("PRECOMPILE_HEADERS").IsOn()) {
2914
0
    this->Makefile->IssueMessage(
2915
0
      MessageType::FATAL_ERROR,
2916
0
      cmStrCat("PRECOMPILE_HEADERS property is already set on target (\"",
2917
0
               this->GetName(), "\")\n"));
2918
0
  }
2919
2920
0
  if (generatorTarget) {
2921
0
    if (generatorTarget->GetPropertyAsBool("DISABLE_PRECOMPILE_HEADERS")) {
2922
0
      this->Makefile->IssueMessage(
2923
0
        MessageType::WARNING,
2924
0
        cmStrCat(
2925
0
          "Target \"", *pchReuseFrom, "\" for the \"", this->GetName(),
2926
0
          R"(" target's "PRECOMPILE_HEADERS_REUSE_FROM" property has set "DISABLE_PRECOMPILE_HEADERS"; ignoring.)"));
2927
0
      return nullptr;
2928
0
    }
2929
2930
0
    if (auto const* recurseReuseTarget =
2931
0
          generatorTarget->GetPchReuseTarget()) {
2932
0
      return recurseReuseTarget;
2933
0
    }
2934
0
  }
2935
0
  return generatorTarget;
2936
0
}
2937
2938
cmGeneratorTarget* cmGeneratorTarget::GetPchReuseTarget()
2939
0
{
2940
0
  if (this->ComputingPchReuse) {
2941
    // TODO: Get the full cycle.
2942
0
    if (!this->PchReuseCycleDetected) {
2943
0
      this->Makefile->IssueMessage(
2944
0
        MessageType::FATAL_ERROR,
2945
0
        cmStrCat("Circular PCH reuse target involving '", this->GetName(),
2946
0
                 '\''));
2947
0
    }
2948
0
    this->PchReuseCycleDetected = true;
2949
0
    return nullptr;
2950
0
  }
2951
0
  CycleWatcher watch(this->ComputingPchReuse);
2952
0
  (void)watch;
2953
0
  cmValue pchReuseFrom = this->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
2954
0
  if (!pchReuseFrom) {
2955
0
    return nullptr;
2956
0
  }
2957
0
  cmGeneratorTarget* generatorTarget =
2958
0
    this->GetGlobalGenerator()->FindGeneratorTarget(*pchReuseFrom);
2959
0
  if (!generatorTarget) {
2960
0
    this->Makefile->IssueMessage(
2961
0
      MessageType::FATAL_ERROR,
2962
0
      cmStrCat(
2963
0
        "Target \"", *pchReuseFrom, "\" for the \"", this->GetName(),
2964
0
        R"(" target's "PRECOMPILE_HEADERS_REUSE_FROM" property does not exist.)"));
2965
0
  }
2966
0
  if (this->GetProperty("PRECOMPILE_HEADERS").IsOn()) {
2967
0
    this->Makefile->IssueMessage(
2968
0
      MessageType::FATAL_ERROR,
2969
0
      cmStrCat("PRECOMPILE_HEADERS property is already set on target (\"",
2970
0
               this->GetName(), "\")\n"));
2971
0
  }
2972
2973
0
  if (generatorTarget) {
2974
0
    if (generatorTarget->GetPropertyAsBool("DISABLE_PRECOMPILE_HEADERS")) {
2975
0
      this->Makefile->IssueMessage(
2976
0
        MessageType::WARNING,
2977
0
        cmStrCat(
2978
0
          "Target \"", *pchReuseFrom, "\" for the \"", this->GetName(),
2979
0
          R"(" target's "PRECOMPILE_HEADERS_REUSE_FROM" property has set "DISABLE_PRECOMPILE_HEADERS"; ignoring.)"));
2980
0
      return nullptr;
2981
0
    }
2982
2983
0
    if (auto* recurseReuseTarget = generatorTarget->GetPchReuseTarget()) {
2984
0
      return recurseReuseTarget;
2985
0
    }
2986
0
  }
2987
0
  return generatorTarget;
2988
0
}
2989
2990
std::vector<std::string> cmGeneratorTarget::GetPchArchs(
2991
  std::string const& config, std::string const& lang) const
2992
0
{
2993
0
  std::vector<std::string> pchArchs;
2994
0
  if (!this->GetGlobalGenerator()->IsXcode()) {
2995
0
    pchArchs = this->GetAppleArchs(config, lang);
2996
0
  }
2997
0
  if (pchArchs.size() < 2) {
2998
    // We do not need per-arch PCH files when building for one architecture.
2999
0
    pchArchs = { {} };
3000
0
  }
3001
0
  return pchArchs;
3002
0
}
3003
3004
std::string cmGeneratorTarget::GetPchHeader(std::string const& config,
3005
                                            std::string const& language,
3006
                                            std::string const& arch) const
3007
0
{
3008
0
  if (language != "C" && language != "CXX" && language != "OBJC" &&
3009
0
      language != "OBJCXX") {
3010
0
    return std::string();
3011
0
  }
3012
3013
0
  if (this->GetPropertyAsBool("DISABLE_PRECOMPILE_HEADERS")) {
3014
0
    return std::string();
3015
0
  }
3016
0
  cmGeneratorTarget const* generatorTarget = this;
3017
0
  cmGeneratorTarget const* reuseTarget = this->GetPchReuseTarget();
3018
0
  bool const haveReuseTarget = reuseTarget && reuseTarget != this;
3019
0
  if (reuseTarget) {
3020
0
    generatorTarget = reuseTarget;
3021
0
  }
3022
3023
0
  auto const inserted =
3024
0
    this->PchHeaders.insert(std::make_pair(language + config + arch, ""));
3025
0
  if (inserted.second) {
3026
0
    std::vector<BT<std::string>> const headers =
3027
0
      this->GetPrecompileHeaders(config, language);
3028
0
    if (headers.empty() && !haveReuseTarget) {
3029
0
      return std::string();
3030
0
    }
3031
0
    std::string& filename = inserted.first->second;
3032
3033
0
    std::map<std::string, std::string> const languageToExtension = {
3034
0
      { "C", ".h" },
3035
0
      { "CXX", ".hxx" },
3036
0
      { "OBJC", ".objc.h" },
3037
0
      { "OBJCXX", ".objcxx.hxx" }
3038
0
    };
3039
3040
0
    filename = generatorTarget->GetCMFSupportDirectory();
3041
3042
0
    if (this->GetGlobalGenerator()->IsMultiConfig()) {
3043
0
      filename = cmStrCat(filename, '/', config);
3044
0
    }
3045
3046
    // This is acceptable as its the source file, won't have a rename/hash
3047
0
    filename =
3048
0
      cmStrCat(filename, "/cmake_pch", arch.empty() ? "" : cmStrCat('_', arch),
3049
0
               languageToExtension.at(language));
3050
3051
0
    std::string const filename_tmp = cmStrCat(filename, ".tmp");
3052
0
    if (!haveReuseTarget) {
3053
0
      cmValue pchPrologue =
3054
0
        this->Makefile->GetDefinition("CMAKE_PCH_PROLOGUE");
3055
0
      cmValue pchEpilogue =
3056
0
        this->Makefile->GetDefinition("CMAKE_PCH_EPILOGUE");
3057
3058
0
      std::string firstHeaderOnDisk;
3059
0
      {
3060
0
        cmGeneratedFileStream file(
3061
0
          filename_tmp, false,
3062
0
          this->GetGlobalGenerator()->GetMakefileEncoding());
3063
0
        file << "/* generated by CMake */\n\n";
3064
0
        if (pchPrologue) {
3065
0
          file << *pchPrologue << "\n";
3066
0
        }
3067
0
        if (this->GetGlobalGenerator()->IsXcode()) {
3068
0
          file << "#ifndef CMAKE_SKIP_PRECOMPILE_HEADERS\n";
3069
0
        }
3070
0
        if (language == "CXX" && !this->GetGlobalGenerator()->IsXcode()) {
3071
0
          file << "#ifdef __cplusplus\n";
3072
0
        }
3073
0
        for (auto const& header_bt : headers) {
3074
0
          if (header_bt.Value.empty()) {
3075
0
            continue;
3076
0
          }
3077
0
          if (header_bt.Value[0] == '<' || header_bt.Value[0] == '\"') {
3078
0
            file << "#include " << header_bt.Value << "\n";
3079
0
          } else {
3080
0
            file << "#include \"" << header_bt.Value << "\"\n";
3081
0
          }
3082
3083
0
          if (cmSystemTools::FileExists(header_bt.Value) &&
3084
0
              firstHeaderOnDisk.empty()) {
3085
0
            firstHeaderOnDisk = header_bt.Value;
3086
0
          }
3087
0
        }
3088
0
        if (language == "CXX" && !this->GetGlobalGenerator()->IsXcode()) {
3089
0
          file << "#endif // __cplusplus\n";
3090
0
        }
3091
0
        if (this->GetGlobalGenerator()->IsXcode()) {
3092
0
          file << "#endif // CMAKE_SKIP_PRECOMPILE_HEADERS\n";
3093
0
        }
3094
0
        if (pchEpilogue) {
3095
0
          file << *pchEpilogue << "\n";
3096
0
        }
3097
0
      }
3098
3099
0
      if (!firstHeaderOnDisk.empty()) {
3100
0
        cmFileTimes::Copy(firstHeaderOnDisk, filename_tmp);
3101
0
      }
3102
3103
0
      cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
3104
0
    }
3105
0
  }
3106
0
  return inserted.first->second;
3107
0
}
3108
3109
std::string cmGeneratorTarget::GetPchSource(std::string const& config,
3110
                                            std::string const& language,
3111
                                            std::string const& arch) const
3112
0
{
3113
0
  if (language != "C" && language != "CXX" && language != "OBJC" &&
3114
0
      language != "OBJCXX") {
3115
0
    return std::string();
3116
0
  }
3117
0
  auto const inserted =
3118
0
    this->PchSources.insert(std::make_pair(language + config + arch, ""));
3119
0
  if (inserted.second) {
3120
0
    std::string const pchHeader = this->GetPchHeader(config, language, arch);
3121
0
    if (pchHeader.empty()) {
3122
0
      return std::string();
3123
0
    }
3124
0
    std::string& filename = inserted.first->second;
3125
3126
0
    cmGeneratorTarget const* generatorTarget = this;
3127
0
    cmGeneratorTarget const* reuseTarget = this->GetPchReuseTarget();
3128
0
    bool const haveReuseTarget = reuseTarget && reuseTarget != this;
3129
0
    if (reuseTarget) {
3130
0
      generatorTarget = reuseTarget;
3131
0
    }
3132
3133
0
    filename =
3134
0
      cmStrCat(generatorTarget->GetCMFSupportDirectory(), "/cmake_pch");
3135
3136
    // For GCC the source extension will be transformed into .h[xx].gch
3137
0
    if (!this->Makefile->IsOn("CMAKE_LINK_PCH")) {
3138
0
      std::map<std::string, std::string> const languageToExtension = {
3139
0
        { "C", ".h.c" },
3140
0
        { "CXX", ".hxx.cxx" },
3141
0
        { "OBJC", ".objc.h.m" },
3142
0
        { "OBJCXX", ".objcxx.hxx.mm" }
3143
0
      };
3144
3145
0
      filename = cmStrCat(filename, arch.empty() ? "" : cmStrCat('_', arch),
3146
0
                          languageToExtension.at(language));
3147
0
    } else {
3148
0
      std::map<std::string, std::string> const languageToExtension = {
3149
0
        { "C", ".c" }, { "CXX", ".cxx" }, { "OBJC", ".m" }, { "OBJCXX", ".mm" }
3150
0
      };
3151
3152
0
      filename = cmStrCat(filename, arch.empty() ? "" : cmStrCat('_', arch),
3153
0
                          languageToExtension.at(language));
3154
0
    }
3155
3156
0
    std::string const filename_tmp = cmStrCat(filename, ".tmp");
3157
0
    if (!haveReuseTarget) {
3158
0
      {
3159
0
        cmGeneratedFileStream file(filename_tmp);
3160
0
        file << "/* generated by CMake */\n";
3161
0
      }
3162
0
      cmFileTimes::Copy(pchHeader, filename_tmp);
3163
0
      cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
3164
0
    }
3165
0
  }
3166
0
  return inserted.first->second;
3167
0
}
3168
3169
std::string cmGeneratorTarget::GetPchFileObject(std::string const& config,
3170
                                                std::string const& language,
3171
                                                std::string const& arch)
3172
0
{
3173
0
  if (language != "C" && language != "CXX" && language != "OBJC" &&
3174
0
      language != "OBJCXX") {
3175
0
    return std::string();
3176
0
  }
3177
0
  auto const inserted =
3178
0
    this->PchObjectFiles.insert(std::make_pair(language + config + arch, ""));
3179
0
  if (inserted.second) {
3180
0
    std::string const pchSource = this->GetPchSource(config, language, arch);
3181
0
    if (pchSource.empty()) {
3182
0
      return std::string();
3183
0
    }
3184
0
    std::string& filename = inserted.first->second;
3185
3186
0
    auto* pchSf = this->Makefile->GetOrCreateSource(
3187
0
      pchSource, false, cmSourceFileLocationKind::Known);
3188
0
    pchSf->SetSpecialSourceType(cmSourceFile::SpecialSourceType::PchSource);
3189
0
    pchSf->ResolveFullPath();
3190
0
    filename = cmStrCat(this->GetObjectDirectory(config), '/',
3191
0
                        this->GetObjectName(pchSf));
3192
0
  }
3193
0
  return inserted.first->second;
3194
0
}
3195
3196
std::string cmGeneratorTarget::GetPchFile(std::string const& config,
3197
                                          std::string const& language,
3198
                                          std::string const& arch)
3199
0
{
3200
0
  auto const inserted =
3201
0
    this->PchFiles.insert(std::make_pair(language + config + arch, ""));
3202
0
  if (inserted.second) {
3203
0
    std::string& pchFile = inserted.first->second;
3204
3205
0
    std::string const pchExtension =
3206
0
      this->Makefile->GetSafeDefinition("CMAKE_PCH_EXTENSION");
3207
3208
0
    if (this->Makefile->IsOn("CMAKE_LINK_PCH")) {
3209
0
      auto replaceExtension = [](std::string const& str,
3210
0
                                 std::string const& ext) -> std::string {
3211
0
        auto dot_pos = str.rfind('.');
3212
0
        std::string result;
3213
0
        if (dot_pos != std::string::npos) {
3214
0
          result = str.substr(0, dot_pos);
3215
0
        }
3216
0
        result += ext;
3217
0
        return result;
3218
0
      };
3219
3220
0
      cmGeneratorTarget* generatorTarget = this;
3221
0
      cmGeneratorTarget* reuseTarget = this->GetPchReuseTarget();
3222
0
      if (reuseTarget) {
3223
0
        generatorTarget = reuseTarget;
3224
0
      }
3225
3226
0
      std::string const pchFileObject =
3227
0
        generatorTarget->GetPchFileObject(config, language, arch);
3228
0
      if (!pchExtension.empty()) {
3229
0
        pchFile = replaceExtension(pchFileObject, pchExtension);
3230
0
      }
3231
0
    } else {
3232
0
      if (this->GetUseShortObjectNames() && !pchExtension.empty()) {
3233
0
        auto pchSource = this->GetPchSource(config, language, arch);
3234
0
        auto* pchSf = this->Makefile->GetOrCreateSource(
3235
0
          pchSource, false, cmSourceFileLocationKind::Known);
3236
0
        pchSf->SetSpecialSourceType(
3237
0
          cmSourceFile::SpecialSourceType::PchSource);
3238
0
        pchSf->ResolveFullPath();
3239
0
        std::string cfgSubdir;
3240
0
        if (this->GetGlobalGenerator()->IsMultiConfig()) {
3241
0
          cfgSubdir = cmStrCat(config, '/');
3242
0
        }
3243
0
        pchFile = cmStrCat(
3244
0
          this->GetSupportDirectory(), '/', cfgSubdir,
3245
0
          this->LocalGenerator->GetShortObjectFileName(*pchSf), pchExtension);
3246
0
      } else {
3247
0
        pchFile =
3248
0
          cmStrCat(this->GetPchHeader(config, language, arch), pchExtension);
3249
0
      }
3250
0
    }
3251
0
  }
3252
0
  return inserted.first->second;
3253
0
}
3254
3255
std::string cmGeneratorTarget::GetPchCreateCompileOptions(
3256
  std::string const& config, std::string const& language,
3257
  std::string const& arch)
3258
0
{
3259
0
  auto const inserted = this->PchCreateCompileOptions.insert(
3260
0
    std::make_pair(language + config + arch, ""));
3261
0
  if (inserted.second) {
3262
0
    std::string& createOptionList = inserted.first->second;
3263
3264
0
    if (this->GetPropertyAsBool("PCH_WARN_INVALID")) {
3265
0
      createOptionList = this->Makefile->GetSafeDefinition(
3266
0
        cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_INVALID_PCH"));
3267
0
    }
3268
3269
0
    if (this->GetPropertyAsBool("PCH_INSTANTIATE_TEMPLATES")) {
3270
0
      std::string varName = cmStrCat(
3271
0
        "CMAKE_", language, "_COMPILE_OPTIONS_INSTANTIATE_TEMPLATES_PCH");
3272
0
      std::string instantiateOption =
3273
0
        this->Makefile->GetSafeDefinition(varName);
3274
0
      if (!instantiateOption.empty()) {
3275
0
        createOptionList = cmStrCat(createOptionList, ';', instantiateOption);
3276
0
      }
3277
0
    }
3278
3279
0
    std::string const createOptVar =
3280
0
      cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_CREATE_PCH");
3281
3282
0
    createOptionList = cmStrCat(
3283
0
      createOptionList, ';', this->Makefile->GetSafeDefinition(createOptVar));
3284
3285
0
    std::string const pchHeader = this->GetPchHeader(config, language, arch);
3286
0
    std::string const pchFile = this->GetPchFile(config, language, arch);
3287
3288
0
    if (GlobalGenerator->IsFastbuild()) {
3289
      // Account for potential spaces in a shell-friendly way.
3290
0
      cmSystemTools::ReplaceString(createOptionList, "<PCH_HEADER>",
3291
0
                                   cmStrCat('"', pchHeader, '"'));
3292
0
      cmSystemTools::ReplaceString(createOptionList, "<PCH_FILE>",
3293
0
                                   cmStrCat('"', pchFile, '"'));
3294
0
    } else {
3295
0
      cmSystemTools::ReplaceString(createOptionList, "<PCH_HEADER>",
3296
0
                                   pchHeader);
3297
0
      cmSystemTools::ReplaceString(createOptionList, "<PCH_FILE>", pchFile);
3298
0
    }
3299
0
  }
3300
0
  return inserted.first->second;
3301
0
}
3302
3303
std::string cmGeneratorTarget::GetPchUseCompileOptions(
3304
  std::string const& config, std::string const& language,
3305
  std::string const& arch)
3306
0
{
3307
0
  auto const inserted = this->PchUseCompileOptions.insert(
3308
0
    std::make_pair(language + config + arch, ""));
3309
0
  if (inserted.second) {
3310
0
    std::string& useOptionList = inserted.first->second;
3311
3312
0
    if (this->GetPropertyAsBool("PCH_WARN_INVALID")) {
3313
0
      useOptionList = this->Makefile->GetSafeDefinition(
3314
0
        cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_INVALID_PCH"));
3315
0
    }
3316
3317
0
    std::string const useOptVar =
3318
0
      cmStrCat(language, "_COMPILE_OPTIONS_USE_PCH");
3319
3320
0
    std::string const& useOptionListProperty =
3321
0
      this->GetSafeProperty(useOptVar);
3322
3323
0
    useOptionList = cmStrCat(
3324
0
      useOptionList, ';',
3325
0
      useOptionListProperty.empty()
3326
0
        ? this->Makefile->GetSafeDefinition(cmStrCat("CMAKE_", useOptVar))
3327
0
        : useOptionListProperty);
3328
3329
0
    std::string const pchHeader = this->GetPchHeader(config, language, arch);
3330
0
    std::string const pchFile = this->GetPchFile(config, language, arch);
3331
3332
0
    if (GlobalGenerator->IsFastbuild()) {
3333
      // Account for potential spaces in a shell-friendly way.
3334
0
      cmSystemTools::ReplaceString(useOptionList, "<PCH_HEADER>",
3335
0
                                   cmStrCat('"', pchHeader, '"'));
3336
0
      cmSystemTools::ReplaceString(useOptionList, "<PCH_FILE>",
3337
0
                                   cmStrCat('"', pchFile, '"'));
3338
0
    } else {
3339
0
      cmSystemTools::ReplaceString(useOptionList, "<PCH_HEADER>", pchHeader);
3340
0
      cmSystemTools::ReplaceString(useOptionList, "<PCH_FILE>", pchFile);
3341
0
    }
3342
0
  }
3343
0
  return inserted.first->second;
3344
0
}
3345
3346
void cmGeneratorTarget::AddSourceFileToUnityBatch(
3347
  std::string const& sourceFilename)
3348
0
{
3349
0
  this->UnityBatchedSourceFiles.insert(sourceFilename);
3350
0
}
3351
3352
bool cmGeneratorTarget::IsSourceFilePartOfUnityBatch(
3353
  std::string const& sourceFilename) const
3354
0
{
3355
0
  if (!this->GetPropertyAsBool("UNITY_BUILD")) {
3356
0
    return false;
3357
0
  }
3358
3359
0
  return this->UnityBatchedSourceFiles.find(sourceFilename) !=
3360
0
    this->UnityBatchedSourceFiles.end();
3361
0
}
3362
3363
void cmGeneratorTarget::ComputeTargetManifest(std::string const& config) const
3364
0
{
3365
0
  if (this->IsImported()) {
3366
0
    return;
3367
0
  }
3368
0
  cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
3369
3370
  // Get the names.
3371
0
  cmGeneratorTarget::Names targetNames;
3372
0
  if (this->GetType() == cmStateEnums::EXECUTABLE) {
3373
0
    targetNames = this->GetExecutableNames(config);
3374
0
  } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
3375
0
             this->GetType() == cmStateEnums::SHARED_LIBRARY ||
3376
0
             this->GetType() == cmStateEnums::MODULE_LIBRARY) {
3377
0
    targetNames = this->GetLibraryNames(config);
3378
0
  } else {
3379
0
    return;
3380
0
  }
3381
3382
  // Get the directory.
3383
0
  std::string dir =
3384
0
    this->GetDirectory(config, cmStateEnums::RuntimeBinaryArtifact);
3385
3386
  // Add each name.
3387
0
  std::string f;
3388
0
  if (!targetNames.Output.empty()) {
3389
0
    f = cmStrCat(dir, '/', targetNames.Output);
3390
0
    gg->AddToManifest(f);
3391
0
  }
3392
0
  if (!targetNames.SharedObject.empty()) {
3393
0
    f = cmStrCat(dir, '/', targetNames.SharedObject);
3394
0
    gg->AddToManifest(f);
3395
0
  }
3396
0
  if (!targetNames.Real.empty()) {
3397
0
    f = cmStrCat(dir, '/', targetNames.Real);
3398
0
    gg->AddToManifest(f);
3399
0
  }
3400
0
  if (!targetNames.PDB.empty()) {
3401
0
    f = cmStrCat(dir, '/', targetNames.PDB);
3402
0
    gg->AddToManifest(f);
3403
0
  }
3404
3405
0
  dir = this->GetDirectory(config, cmStateEnums::ImportLibraryArtifact);
3406
0
  if (!targetNames.ImportOutput.empty()) {
3407
0
    f = cmStrCat(dir, '/', targetNames.ImportOutput);
3408
0
    gg->AddToManifest(f);
3409
0
  }
3410
0
  if (!targetNames.ImportLibrary.empty()) {
3411
0
    f = cmStrCat(dir, '/', targetNames.ImportLibrary);
3412
0
    gg->AddToManifest(f);
3413
0
  }
3414
0
  if (!targetNames.ImportReal.empty()) {
3415
0
    f = cmStrCat(dir, '/', targetNames.ImportReal);
3416
0
    gg->AddToManifest(f);
3417
0
  }
3418
0
}
3419
3420
cm::optional<cmStandardLevel> cmGeneratorTarget::GetExplicitStandardLevel(
3421
  std::string const& lang, std::string const& config) const
3422
0
{
3423
0
  cm::optional<cmStandardLevel> level;
3424
0
  std::string key = cmStrCat(cmSystemTools::UpperCase(config), '-', lang);
3425
0
  auto i = this->ExplicitStandardLevel.find(key);
3426
0
  if (i != this->ExplicitStandardLevel.end()) {
3427
0
    level = i->second;
3428
0
  }
3429
0
  return level;
3430
0
}
3431
3432
void cmGeneratorTarget::UpdateExplicitStandardLevel(std::string const& lang,
3433
                                                    std::string const& config,
3434
                                                    cmStandardLevel level)
3435
0
{
3436
0
  auto e = this->ExplicitStandardLevel.emplace(
3437
0
    cmStrCat(cmSystemTools::UpperCase(config), '-', lang), level);
3438
0
  if (!e.second && e.first->second < level) {
3439
0
    e.first->second = level;
3440
0
  }
3441
0
}
3442
3443
bool cmGeneratorTarget::ComputeCompileFeatures(std::string const& config)
3444
0
{
3445
0
  cmStandardLevelResolver standardResolver(this->Makefile);
3446
3447
0
  for (std::string const& lang :
3448
0
       this->Makefile->GetState()->GetEnabledLanguages()) {
3449
0
    if (cmValue languageStd = this->GetLanguageStandard(lang, config)) {
3450
0
      if (cm::optional<cmStandardLevel> langLevel =
3451
0
            standardResolver.LanguageStandardLevel(lang, *languageStd)) {
3452
0
        this->UpdateExplicitStandardLevel(lang, config, *langLevel);
3453
0
      }
3454
0
    }
3455
0
  }
3456
3457
  // Compute the language standard based on the compile features.
3458
0
  std::vector<BT<std::string>> features = this->GetCompileFeatures(config);
3459
0
  for (BT<std::string> const& f : features) {
3460
0
    std::string lang;
3461
0
    if (!standardResolver.CompileFeatureKnown(this->Target->GetName(), f.Value,
3462
0
                                              lang, nullptr)) {
3463
0
      return false;
3464
0
    }
3465
3466
0
    std::string key = cmStrCat(cmSystemTools::UpperCase(config), '-', lang);
3467
0
    cmValue currentLanguageStandard = this->GetLanguageStandard(lang, config);
3468
3469
0
    cm::optional<cmStandardLevel> featureLevel;
3470
0
    std::string newRequiredStandard;
3471
0
    if (!standardResolver.GetNewRequiredStandard(
3472
0
          this->Target->GetName(), f.Value, currentLanguageStandard,
3473
0
          featureLevel, newRequiredStandard)) {
3474
0
      return false;
3475
0
    }
3476
3477
0
    if (featureLevel) {
3478
0
      this->UpdateExplicitStandardLevel(lang, config, *featureLevel);
3479
0
    }
3480
3481
0
    if (!newRequiredStandard.empty()) {
3482
0
      BTs<std::string>& languageStandardProperty =
3483
0
        this->LanguageStandardMap[key];
3484
0
      if (languageStandardProperty.Value != newRequiredStandard) {
3485
0
        languageStandardProperty.Value = newRequiredStandard;
3486
0
        languageStandardProperty.Backtraces.clear();
3487
0
      }
3488
0
      languageStandardProperty.Backtraces.emplace_back(f.Backtrace);
3489
0
    }
3490
0
  }
3491
3492
0
  return true;
3493
0
}
3494
3495
bool cmGeneratorTarget::ComputeCompileFeatures(
3496
  std::string const& config, std::set<LanguagePair> const& languagePairs)
3497
0
{
3498
0
  for (auto const& language : languagePairs) {
3499
0
    BTs<std::string> const* generatorTargetLanguageStandard =
3500
0
      this->GetLanguageStandardProperty(language.first, config);
3501
0
    if (!generatorTargetLanguageStandard) {
3502
      // If the standard isn't explicitly set we copy it over from the
3503
      // specified paired language.
3504
0
      std::string key =
3505
0
        cmStrCat(cmSystemTools::UpperCase(config), '-', language.first);
3506
0
      BTs<std::string> const* standardToCopy =
3507
0
        this->GetLanguageStandardProperty(language.second, config);
3508
0
      if (standardToCopy) {
3509
0
        this->LanguageStandardMap[key] = *standardToCopy;
3510
0
        generatorTargetLanguageStandard = &this->LanguageStandardMap[key];
3511
0
      } else {
3512
0
        cmValue defaultStandard = this->Makefile->GetDefinition(
3513
0
          cmStrCat("CMAKE_", language.second, "_STANDARD_DEFAULT"));
3514
0
        if (defaultStandard) {
3515
0
          this->LanguageStandardMap[key] = BTs<std::string>(*defaultStandard);
3516
0
          generatorTargetLanguageStandard = &this->LanguageStandardMap[key];
3517
0
        }
3518
0
      }
3519
3520
      // Custom updates for the CUDA standard.
3521
0
      if (generatorTargetLanguageStandard && (language.first == "CUDA")) {
3522
0
        if (generatorTargetLanguageStandard->Value == "98") {
3523
0
          this->LanguageStandardMap[key].Value = "03";
3524
0
        }
3525
0
      }
3526
0
    }
3527
0
  }
3528
3529
0
  return true;
3530
0
}
3531
3532
std::string cmGeneratorTarget::GetImportedLibName(
3533
  std::string const& config) const
3534
0
{
3535
0
  if (cmGeneratorTarget::ImportInfo const* info =
3536
0
        this->GetImportInfo(config)) {
3537
0
    return info->LibName;
3538
0
  }
3539
0
  return std::string();
3540
0
}
3541
3542
std::string cmGeneratorTarget::GetFullPath(std::string const& config,
3543
                                           cmStateEnums::ArtifactType artifact,
3544
                                           bool realname) const
3545
0
{
3546
0
  if (this->IsImported()) {
3547
0
    return this->Target->ImportedGetFullPath(config, artifact);
3548
0
  }
3549
0
  return this->NormalGetFullPath(config, artifact, realname);
3550
0
}
3551
3552
std::string cmGeneratorTarget::NormalGetFullPath(
3553
  std::string const& config, cmStateEnums::ArtifactType artifact,
3554
  bool realname) const
3555
0
{
3556
0
  std::string fpath = cmStrCat(this->GetDirectory(config, artifact), '/');
3557
0
  if (this->IsAppBundleOnApple()) {
3558
0
    fpath =
3559
0
      cmStrCat(this->BuildBundleDirectory(fpath, config, FullLevel), '/');
3560
0
  }
3561
3562
  // Add the full name of the target.
3563
0
  switch (artifact) {
3564
0
    case cmStateEnums::RuntimeBinaryArtifact:
3565
0
      if (realname) {
3566
0
        fpath += this->NormalGetRealName(config);
3567
0
      } else {
3568
0
        fpath +=
3569
0
          this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact);
3570
0
      }
3571
0
      break;
3572
0
    case cmStateEnums::ImportLibraryArtifact:
3573
0
      if (realname) {
3574
0
        fpath +=
3575
0
          this->NormalGetRealName(config, cmStateEnums::ImportLibraryArtifact);
3576
0
      } else {
3577
0
        fpath +=
3578
0
          this->GetFullName(config, cmStateEnums::ImportLibraryArtifact);
3579
0
      }
3580
0
      break;
3581
0
  }
3582
0
  return fpath;
3583
0
}
3584
3585
std::string cmGeneratorTarget::NormalGetRealName(
3586
  std::string const& config, cmStateEnums::ArtifactType artifact) const
3587
0
{
3588
  // This should not be called for imported targets.
3589
  // TODO: Split cmTarget into a class hierarchy to get compile-time
3590
  // enforcement of the limited imported target API.
3591
0
  if (this->IsImported()) {
3592
0
    std::string msg = cmStrCat("NormalGetRealName called on imported target: ",
3593
0
                               this->GetName());
3594
0
    this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
3595
0
  }
3596
3597
0
  Names names = this->GetType() == cmStateEnums::EXECUTABLE
3598
0
    ? this->GetExecutableNames(config)
3599
0
    : this->GetLibraryNames(config);
3600
3601
  // Compute the real name that will be built.
3602
0
  return artifact == cmStateEnums::RuntimeBinaryArtifact ? names.Real
3603
0
                                                         : names.ImportReal;
3604
0
}
3605
3606
cmGeneratorTarget::Names cmGeneratorTarget::GetLibraryNames(
3607
  std::string const& config) const
3608
0
{
3609
0
  cmGeneratorTarget::Names targetNames;
3610
3611
  // This should not be called for imported targets.
3612
  // TODO: Split cmTarget into a class hierarchy to get compile-time
3613
  // enforcement of the limited imported target API.
3614
0
  if (this->IsImported()) {
3615
0
    std::string msg =
3616
0
      cmStrCat("GetLibraryNames called on imported target: ", this->GetName());
3617
0
    this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
3618
0
  }
3619
3620
  // Check for library version properties.
3621
0
  cmValue version = this->GetProperty("VERSION");
3622
0
  cmValue soversion = this->GetProperty("SOVERSION");
3623
0
  if (!this->HasSOName(config) ||
3624
0
      this->Makefile->IsOn("CMAKE_PLATFORM_NO_VERSIONED_SONAME") ||
3625
0
      this->IsFrameworkOnApple()) {
3626
    // Versioning is supported only for shared libraries and modules,
3627
    // and then only when the platform supports an soname flag.
3628
0
    version = nullptr;
3629
0
    soversion = nullptr;
3630
0
  }
3631
0
  if (version && !soversion) {
3632
    // The soversion must be set if the library version is set.  Use
3633
    // the library version as the soversion.
3634
0
    soversion = version;
3635
0
  }
3636
0
  if (!version && soversion) {
3637
    // Use the soversion as the library version.
3638
0
    version = soversion;
3639
0
  }
3640
3641
  // Get the components of the library name.
3642
0
  NameComponents const& components = this->GetFullNameInternalComponents(
3643
0
    config, cmStateEnums::RuntimeBinaryArtifact);
3644
3645
  // The library name.
3646
0
  targetNames.Base = components.base;
3647
0
  targetNames.Output =
3648
0
    cmStrCat(components.prefix, targetNames.Base, components.suffix);
3649
3650
0
  if (this->IsFrameworkOnApple()) {
3651
0
    targetNames.Real = components.prefix;
3652
0
    if (!this->Makefile->PlatformIsAppleEmbedded()) {
3653
0
      targetNames.Real +=
3654
0
        cmStrCat("Versions/", this->GetFrameworkVersion(), '/');
3655
0
    }
3656
0
    targetNames.Real += cmStrCat(targetNames.Base, components.suffix);
3657
0
    targetNames.SharedObject = targetNames.Real;
3658
0
  } else if (this->IsArchivedAIXSharedLibrary()) {
3659
0
    targetNames.SharedObject =
3660
0
      cmStrCat(components.prefix, targetNames.Base, ".so");
3661
0
    if (soversion) {
3662
0
      targetNames.SharedObject += ".";
3663
0
      targetNames.SharedObject += *soversion;
3664
0
    }
3665
0
    targetNames.Real = targetNames.Output;
3666
0
  } else {
3667
    // The library's soname.
3668
0
    targetNames.SharedObject = this->ComputeVersionedName(
3669
0
      components.prefix, targetNames.Base, components.suffix,
3670
0
      targetNames.Output, soversion);
3671
3672
    // The library's real name on disk.
3673
0
    targetNames.Real = this->ComputeVersionedName(
3674
0
      components.prefix, targetNames.Base, components.suffix,
3675
0
      targetNames.Output, version);
3676
0
  }
3677
3678
  // The import library names.
3679
0
  if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
3680
0
      this->GetType() == cmStateEnums::MODULE_LIBRARY) {
3681
0
    NameComponents const& importComponents =
3682
0
      this->GetFullNameInternalComponents(config,
3683
0
                                          cmStateEnums::ImportLibraryArtifact);
3684
0
    targetNames.ImportOutput = cmStrCat(
3685
0
      importComponents.prefix, importComponents.base, importComponents.suffix);
3686
3687
0
    if (this->IsFrameworkOnApple() && this->IsSharedLibraryWithExports()) {
3688
0
      targetNames.ImportReal = components.prefix;
3689
0
      if (!this->Makefile->PlatformIsAppleEmbedded()) {
3690
0
        targetNames.ImportReal +=
3691
0
          cmStrCat("Versions/", this->GetFrameworkVersion(), '/');
3692
0
      }
3693
0
      targetNames.ImportReal +=
3694
0
        cmStrCat(importComponents.base, importComponents.suffix);
3695
0
      targetNames.ImportLibrary = targetNames.ImportOutput;
3696
0
    } else {
3697
      // The import library's soname.
3698
0
      targetNames.ImportLibrary = this->ComputeVersionedName(
3699
0
        importComponents.prefix, importComponents.base,
3700
0
        importComponents.suffix, targetNames.ImportOutput, soversion);
3701
3702
      // The import library's real name on disk.
3703
0
      targetNames.ImportReal = this->ComputeVersionedName(
3704
0
        importComponents.prefix, importComponents.base,
3705
0
        importComponents.suffix, targetNames.ImportOutput, version);
3706
0
    }
3707
0
  }
3708
3709
  // The program database file name.
3710
0
  targetNames.PDB = this->GetPDBName(config);
3711
3712
0
  return targetNames;
3713
0
}
3714
3715
cmGeneratorTarget::Names cmGeneratorTarget::GetExecutableNames(
3716
  std::string const& config) const
3717
0
{
3718
0
  cmGeneratorTarget::Names targetNames;
3719
3720
  // This should not be called for imported targets.
3721
  // TODO: Split cmTarget into a class hierarchy to get compile-time
3722
  // enforcement of the limited imported target API.
3723
0
  if (this->IsImported()) {
3724
0
    std::string msg = cmStrCat(
3725
0
      "GetExecutableNames called on imported target: ", this->GetName());
3726
0
    this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
3727
0
  }
3728
3729
// This versioning is supported only for executables and then only
3730
// when the platform supports symbolic links.
3731
#if defined(_WIN32) && !defined(__CYGWIN__)
3732
  cmValue version;
3733
#else
3734
  // Check for executable version properties.
3735
0
  cmValue version = this->GetProperty("VERSION");
3736
0
  if (this->GetType() != cmStateEnums::EXECUTABLE ||
3737
0
      this->Makefile->IsOn("XCODE")) {
3738
0
    version = nullptr;
3739
0
  }
3740
0
#endif
3741
3742
  // Get the components of the executable name.
3743
0
  NameComponents const& components = this->GetFullNameInternalComponents(
3744
0
    config, cmStateEnums::RuntimeBinaryArtifact);
3745
3746
  // The executable name.
3747
0
  targetNames.Base = components.base;
3748
3749
0
  if (this->IsArchivedAIXSharedLibrary()) {
3750
0
    targetNames.Output = components.prefix + targetNames.Base;
3751
0
  } else {
3752
0
    targetNames.Output =
3753
0
      components.prefix + targetNames.Base + components.suffix;
3754
0
  }
3755
3756
// The executable's real name on disk.
3757
#if defined(__CYGWIN__)
3758
  targetNames.Real = components.prefix + targetNames.Base;
3759
#else
3760
0
  targetNames.Real = targetNames.Output;
3761
0
#endif
3762
0
  if (version) {
3763
0
    targetNames.Real += "-";
3764
0
    targetNames.Real += *version;
3765
0
  }
3766
#if defined(__CYGWIN__)
3767
  targetNames.Real += components.suffix;
3768
#endif
3769
3770
  // The import library name.
3771
0
  targetNames.ImportLibrary =
3772
0
    this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
3773
0
  targetNames.ImportReal = targetNames.ImportLibrary;
3774
0
  targetNames.ImportOutput = targetNames.ImportLibrary;
3775
3776
  // The program database file name.
3777
0
  targetNames.PDB = this->GetPDBName(config);
3778
3779
0
  return targetNames;
3780
0
}
3781
3782
std::string cmGeneratorTarget::GetFullNameInternal(
3783
  std::string const& config, cmStateEnums::ArtifactType artifact) const
3784
0
{
3785
0
  NameComponents const& components =
3786
0
    this->GetFullNameInternalComponents(config, artifact);
3787
0
  return components.prefix + components.base + components.suffix;
3788
0
}
3789
3790
std::string cmGeneratorTarget::ImportedGetLocation(
3791
  std::string const& config) const
3792
0
{
3793
0
  assert(this->IsImported());
3794
0
  return this->Target->ImportedGetFullPath(
3795
0
    config, cmStateEnums::RuntimeBinaryArtifact);
3796
0
}
3797
3798
std::string cmGeneratorTarget::GetFullNameImported(
3799
  std::string const& config, cmStateEnums::ArtifactType artifact) const
3800
0
{
3801
0
  return cmSystemTools::GetFilenameName(
3802
0
    this->Target->ImportedGetFullPath(config, artifact));
3803
0
}
3804
3805
cmGeneratorTarget::NameComponents const&
3806
cmGeneratorTarget::GetFullNameInternalComponents(
3807
  std::string const& config, cmStateEnums::ArtifactType artifact) const
3808
0
{
3809
0
  assert(artifact == cmStateEnums::RuntimeBinaryArtifact ||
3810
0
         artifact == cmStateEnums::ImportLibraryArtifact);
3811
0
  FullNameCache& cache = artifact == cmStateEnums::RuntimeBinaryArtifact
3812
0
    ? RuntimeBinaryFullNameCache
3813
0
    : ImportLibraryFullNameCache;
3814
0
  auto search = cache.find(config);
3815
0
  if (search != cache.end()) {
3816
0
    return search->second;
3817
0
  }
3818
  // Use just the target name for non-main target types.
3819
0
  if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
3820
0
      this->GetType() != cmStateEnums::SHARED_LIBRARY &&
3821
0
      this->GetType() != cmStateEnums::MODULE_LIBRARY &&
3822
0
      this->GetType() != cmStateEnums::EXECUTABLE) {
3823
0
    NameComponents components;
3824
0
    components.base = this->GetName();
3825
0
    return cache.emplace(config, std::move(components)).first->second;
3826
0
  }
3827
3828
0
  bool const isImportedLibraryArtifact =
3829
0
    (artifact == cmStateEnums::ImportLibraryArtifact);
3830
3831
  // Return an empty name for the import library if this platform
3832
  // does not support import libraries.
3833
0
  if (isImportedLibraryArtifact && !this->NeedImportLibraryName(config)) {
3834
0
    return cache.emplace(config, NameComponents()).first->second;
3835
0
  }
3836
3837
0
  NameComponents parts;
3838
0
  std::string& outPrefix = parts.prefix;
3839
0
  std::string& outBase = parts.base;
3840
0
  std::string& outSuffix = parts.suffix;
3841
3842
  // retrieve prefix and suffix
3843
0
  std::string ll = this->GetLinkerLanguage(config);
3844
0
  cmValue targetPrefix = this->GetFilePrefixInternal(config, artifact, ll);
3845
0
  cmValue targetSuffix = this->GetFileSuffixInternal(config, artifact, ll);
3846
3847
  // The implib option is only allowed for shared libraries, module
3848
  // libraries, and executables.
3849
0
  if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
3850
0
      this->GetType() != cmStateEnums::MODULE_LIBRARY &&
3851
0
      this->GetType() != cmStateEnums::EXECUTABLE) {
3852
0
    artifact = cmStateEnums::RuntimeBinaryArtifact;
3853
0
  }
3854
3855
  // Compute the full name for main target types.
3856
0
  std::string configPostfix = this->GetFilePostfix(config);
3857
3858
  // frameworks have directory prefix
3859
0
  std::string fw_prefix;
3860
0
  if (this->IsFrameworkOnApple()) {
3861
0
    fw_prefix =
3862
0
      cmStrCat(this->GetFrameworkDirectory(config, ContentLevel), '/');
3863
0
    targetPrefix = cmValue(fw_prefix);
3864
0
    if (!isImportedLibraryArtifact) {
3865
      // no suffix
3866
0
      targetSuffix = nullptr;
3867
0
    }
3868
0
  }
3869
3870
0
  if (this->IsCFBundleOnApple()) {
3871
0
    fw_prefix = cmStrCat(this->GetCFBundleDirectory(config, FullLevel), '/');
3872
0
    targetPrefix = cmValue(fw_prefix);
3873
0
    targetSuffix = nullptr;
3874
0
  }
3875
3876
  // Begin the final name with the prefix.
3877
0
  outPrefix = targetPrefix ? *targetPrefix : "";
3878
3879
  // Append the target name or property-specified name.
3880
0
  outBase += this->GetOutputName(config, artifact);
3881
3882
  // Append the per-configuration postfix.
3883
  // When using Xcode, the postfix should be part of the suffix rather than
3884
  // the base, because the suffix ends up being used in Xcode's
3885
  // EXECUTABLE_SUFFIX attribute.
3886
0
  if (this->IsFrameworkOnApple() && this->GetGlobalGenerator()->IsXcode()) {
3887
0
    configPostfix += *targetSuffix;
3888
0
    targetSuffix = cmValue(configPostfix);
3889
0
  } else {
3890
0
    outBase += configPostfix;
3891
0
  }
3892
3893
  // Name shared libraries with their version number on some platforms.
3894
0
  if (cmValue soversion = this->GetProperty("SOVERSION")) {
3895
0
    cmValue dllProp;
3896
0
    if (this->IsDLLPlatform()) {
3897
0
      dllProp = this->GetProperty("DLL_NAME_WITH_SOVERSION");
3898
0
    }
3899
0
    if (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
3900
0
        !isImportedLibraryArtifact &&
3901
0
        (dllProp.IsOn() ||
3902
0
         (!dllProp.IsSet() &&
3903
0
          this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION")))) {
3904
0
      outBase += "-";
3905
0
      outBase += *soversion;
3906
0
    }
3907
0
  }
3908
3909
  // Append the suffix.
3910
0
  outSuffix = targetSuffix ? *targetSuffix : "";
3911
3912
0
  return cache.emplace(config, std::move(parts)).first->second;
3913
0
}
3914
3915
std::string cmGeneratorTarget::GetLinkerLanguage(
3916
  std::string const& config) const
3917
0
{
3918
0
  return this->GetLinkClosure(config)->LinkerLanguage;
3919
0
}
3920
3921
std::string cmGeneratorTarget::GetLinkerTool(std::string const& config) const
3922
0
{
3923
0
  return this->GetLinkerTool(this->GetLinkerLanguage(config), config);
3924
0
}
3925
3926
std::string cmGeneratorTarget::GetLinkerTool(std::string const& lang,
3927
                                             std::string const& config) const
3928
0
{
3929
0
  auto linkMode = cmStrCat(
3930
0
    "CMAKE_", lang, this->IsDeviceLink() ? "_DEVICE_" : "_", "LINK_MODE");
3931
0
  auto mode = this->Makefile->GetDefinition(linkMode);
3932
0
  if (!mode || mode != "LINKER"_s) {
3933
0
    return this->Makefile->GetDefinition("CMAKE_LINKER");
3934
0
  }
3935
3936
0
  auto linkerType = this->GetLinkerTypeProperty(lang, config);
3937
0
  if (linkerType.empty()) {
3938
0
    linkerType = "DEFAULT";
3939
0
  }
3940
0
  auto usingLinker =
3941
0
    cmStrCat("CMAKE_", lang, "_USING_", this->IsDeviceLink() ? "DEVICE_" : "",
3942
0
             "LINKER_", linkerType);
3943
0
  auto linkerTool = this->Makefile->GetDefinition(usingLinker);
3944
3945
0
  if (!linkerTool) {
3946
0
    if (this->GetGlobalGenerator()->IsVisualStudio() &&
3947
0
        linkerType == "DEFAULT"_s) {
3948
0
      return std::string{};
3949
0
    }
3950
3951
    // fall-back to generic definition
3952
0
    linkerTool = this->Makefile->GetDefinition("CMAKE_LINKER");
3953
3954
0
    if (linkerType != "DEFAULT"_s) {
3955
0
      auto isCMakeLinkerType = [](std::string const& type) -> bool {
3956
0
        return std::all_of(type.cbegin(), type.cend(),
3957
0
                           [](char c) { return std::isupper(c); });
3958
0
      };
3959
0
      if (isCMakeLinkerType(linkerType)) {
3960
0
        this->LocalGenerator->IssueMessage(
3961
0
          MessageType::FATAL_ERROR,
3962
0
          cmStrCat("LINKER_TYPE '", linkerType,
3963
0
                   "' is unknown or not supported by this toolchain."));
3964
0
      } else {
3965
0
        this->LocalGenerator->IssueMessage(
3966
0
          MessageType::FATAL_ERROR,
3967
0
          cmStrCat("LINKER_TYPE '", linkerType,
3968
0
                   "' is unknown. Did you forget to define the '", usingLinker,
3969
0
                   "' variable?"));
3970
0
      }
3971
0
    }
3972
0
  }
3973
3974
0
  return linkerTool;
3975
0
}
3976
3977
bool cmGeneratorTarget::LinkerEnforcesNoAllowShLibUndefined(
3978
  std::string const& config) const
3979
0
{
3980
  // FIXME(#25486): Account for the LINKER_TYPE target property.
3981
  // Also factor out the hard-coded list below into a platform
3982
  // information table based on the linker id.
3983
0
  std::string ll = this->GetLinkerLanguage(config);
3984
0
  std::string linkerIdVar = cmStrCat("CMAKE_", ll, "_COMPILER_LINKER_ID");
3985
0
  cmValue linkerId = this->Makefile->GetDefinition(linkerIdVar);
3986
  // The GNU bfd-based linker may enforce '--no-allow-shlib-undefined'
3987
  // recursively by default.  The Solaris linker has similar behavior.
3988
0
  return linkerId && (*linkerId == "GNU" || *linkerId == "Solaris");
3989
0
}
3990
3991
std::string cmGeneratorTarget::GetPDBOutputName(
3992
  std::string const& config) const
3993
0
{
3994
  // Lookup/compute/cache the pdb output name for this configuration.
3995
0
  auto i = this->PdbOutputNameMap.find(config);
3996
0
  if (i == this->PdbOutputNameMap.end()) {
3997
    // Add empty name in map to detect potential recursion.
3998
0
    PdbOutputNameMapType::value_type entry(config, "");
3999
0
    i = this->PdbOutputNameMap.insert(entry).first;
4000
4001
    // Compute output name.
4002
0
    std::vector<std::string> props;
4003
0
    std::string configUpper = cmSystemTools::UpperCase(config);
4004
0
    if (!configUpper.empty()) {
4005
      // PDB_NAME_<CONFIG>
4006
0
      props.push_back("PDB_NAME_" + configUpper);
4007
0
    }
4008
4009
    // PDB_NAME
4010
0
    props.emplace_back("PDB_NAME");
4011
4012
0
    std::string outName;
4013
0
    for (std::string const& p : props) {
4014
0
      if (cmValue outNameProp = this->GetProperty(p)) {
4015
0
        outName = *outNameProp;
4016
0
        break;
4017
0
      }
4018
0
    }
4019
4020
    // Now evaluate genex and update the previously-prepared map entry.
4021
0
    if (outName.empty()) {
4022
0
      i->second = cmStrCat(
4023
0
        this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact),
4024
0
        this->GetPolicyStatusCMP0202() != cmPolicies::NEW
4025
0
          ? this->GetFilePostfix(config)
4026
0
          : "");
4027
0
    } else {
4028
0
      i->second =
4029
0
        cmGeneratorExpression::Evaluate(outName, this->LocalGenerator, config);
4030
0
    }
4031
0
  } else if (i->second.empty()) {
4032
    // An empty map entry indicates we have been called recursively
4033
    // from the above block.
4034
0
    this->LocalGenerator->GetCMakeInstance()->IssueMessage(
4035
0
      MessageType::FATAL_ERROR,
4036
0
      cmStrCat("Target '", this->GetName(), "' PDB_NAME depends on itself."),
4037
0
      this->GetBacktrace());
4038
0
  }
4039
0
  return i->second;
4040
0
}
4041
4042
std::string cmGeneratorTarget::GetPDBName(std::string const& config) const
4043
0
{
4044
0
  NameComponents const& parts = this->GetFullNameInternalComponents(
4045
0
    config, cmStateEnums::RuntimeBinaryArtifact);
4046
4047
0
  std::string base = this->GetPDBOutputName(config);
4048
4049
0
  return cmStrCat(parts.prefix, base,
4050
0
                  this->GetPolicyStatusCMP0202() == cmPolicies::NEW
4051
0
                    ? this->GetFilePostfix(config)
4052
0
                    : "",
4053
0
                  ".pdb");
4054
0
}
4055
4056
std::string cmGeneratorTarget::GetObjectDirectory(
4057
  std::string const& config) const
4058
0
{
4059
0
  std::string obj_dir =
4060
0
    this->GlobalGenerator->ExpandCFGIntDir(this->ObjectDirectory, config);
4061
#if defined(__APPLE__)
4062
  // Replace Xcode's placeholder for the object file directory since
4063
  // installation and export scripts need to know the real directory.
4064
  // Xcode has build-time settings (e.g. for sanitizers) that affect this,
4065
  // but we use the default here.  Users that want to enable sanitizers
4066
  // will do so at the cost of object library installation and export.
4067
  cmSystemTools::ReplaceString(obj_dir, "$(OBJECT_FILE_DIR_normal:base)",
4068
                               "Objects-normal");
4069
#endif
4070
0
  return obj_dir;
4071
0
}
4072
4073
void cmGeneratorTarget::GetTargetObjectNames(
4074
  std::string const& config, std::vector<std::string>& objects) const
4075
0
{
4076
0
  this->GetTargetObjectLocations(
4077
0
    config,
4078
0
    [&objects](cmObjectLocation const& buildLoc, cmObjectLocation const&) {
4079
0
      objects.push_back(buildLoc.GetPath());
4080
0
    });
4081
0
}
4082
4083
void cmGeneratorTarget::GetTargetObjectLocations(
4084
  std::string const& config,
4085
  std::function<void(cmObjectLocation const&, cmObjectLocation const&)> cb)
4086
  const
4087
0
{
4088
0
  std::vector<cmSourceFile const*> objectSources;
4089
0
  this->GetObjectSources(objectSources, config);
4090
0
  std::map<cmSourceFile const*, cmObjectLocations> mapping;
4091
4092
0
  for (cmSourceFile const* sf : objectSources) {
4093
0
    mapping[sf];
4094
0
  }
4095
4096
0
  this->LocalGenerator->ComputeObjectFilenames(mapping, config, this);
4097
4098
0
  auto const buildUseShortPaths = this->GetUseShortObjectNames()
4099
0
    ? cmObjectLocations::UseShortPath::Yes
4100
0
    : cmObjectLocations::UseShortPath::No;
4101
0
  auto const installUseShortPaths = this->GetUseShortObjectNamesForInstall();
4102
4103
0
  for (cmSourceFile const* src : objectSources) {
4104
    // Find the object file name corresponding to this source file.
4105
0
    auto map_it = mapping.find(src);
4106
0
    auto const& buildLoc = map_it->second.GetLocation(buildUseShortPaths);
4107
0
    auto const& installLoc =
4108
0
      map_it->second.GetInstallLocation(installUseShortPaths, config);
4109
    // It must exist because we populated the mapping just above.
4110
0
    assert(!buildLoc.GetPath().empty());
4111
0
    assert(!installLoc.GetPath().empty());
4112
0
    cb(buildLoc, installLoc);
4113
0
  }
4114
4115
  // We need to compute the relative path from the root of
4116
  // of the object directory to handle subdirectory paths
4117
0
  std::string rootObjectDir = this->GetObjectDirectory(config);
4118
0
  rootObjectDir = cmSystemTools::CollapseFullPath(rootObjectDir);
4119
0
  auto ispcObjects = this->GetGeneratedISPCObjects(config);
4120
0
  for (std::string const& output : ispcObjects) {
4121
0
    auto relativePathFromObjectDir = output.substr(rootObjectDir.size());
4122
0
    cmObjectLocation ispcLoc(relativePathFromObjectDir);
4123
    // FIXME: apply short path to this object if needed.
4124
0
    cb(ispcLoc, ispcLoc);
4125
0
  }
4126
0
}
4127
4128
bool cmGeneratorTarget::StrictTargetComparison::operator()(
4129
  cmGeneratorTarget const* t1, cmGeneratorTarget const* t2) const
4130
0
{
4131
0
  int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
4132
0
  if (nameResult == 0) {
4133
0
    return strcmp(
4134
0
             t1->GetLocalGenerator()->GetCurrentBinaryDirectory().c_str(),
4135
0
             t2->GetLocalGenerator()->GetCurrentBinaryDirectory().c_str()) < 0;
4136
0
  }
4137
0
  return nameResult < 0;
4138
0
}
4139
4140
struct cmGeneratorTarget::SourceFileFlags
4141
cmGeneratorTarget::GetTargetSourceFileFlags(cmSourceFile const* sf) const
4142
0
{
4143
0
  struct SourceFileFlags flags;
4144
0
  this->ConstructSourceFileFlags();
4145
0
  auto si = this->SourceFlagsMap.find(sf);
4146
0
  if (si != this->SourceFlagsMap.end()) {
4147
0
    flags = si->second;
4148
0
  } else {
4149
    // Handle the MACOSX_PACKAGE_LOCATION property on source files that
4150
    // were not listed in one of the other lists.
4151
0
    if (cmValue location = sf->GetProperty("MACOSX_PACKAGE_LOCATION")) {
4152
0
      flags.MacFolder = location->c_str();
4153
0
      bool const stripResources =
4154
0
        this->GlobalGenerator->ShouldStripResourcePath(this->Makefile);
4155
0
      if (*location == "Resources") {
4156
0
        flags.Type = cmGeneratorTarget::SourceFileTypeResource;
4157
0
        if (stripResources) {
4158
0
          flags.MacFolder = "";
4159
0
        }
4160
0
      } else if (cmHasLiteralPrefix(*location, "Resources/")) {
4161
0
        flags.Type = cmGeneratorTarget::SourceFileTypeDeepResource;
4162
0
        if (stripResources) {
4163
0
          flags.MacFolder += cmStrLen("Resources/");
4164
0
        }
4165
0
      } else {
4166
0
        flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
4167
0
      }
4168
0
    }
4169
0
  }
4170
0
  return flags;
4171
0
}
4172
4173
void cmGeneratorTarget::ConstructSourceFileFlags() const
4174
0
{
4175
0
  if (this->SourceFileFlagsConstructed) {
4176
0
    return;
4177
0
  }
4178
0
  this->SourceFileFlagsConstructed = true;
4179
4180
  // Process public headers to mark the source files.
4181
0
  if (cmValue files = this->GetProperty("PUBLIC_HEADER")) {
4182
0
    cmList relFiles{ *files };
4183
0
    for (auto const& relFile : relFiles) {
4184
0
      if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
4185
0
        SourceFileFlags& flags = this->SourceFlagsMap[sf];
4186
0
        flags.MacFolder = "Headers";
4187
0
        flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
4188
0
      }
4189
0
    }
4190
0
  }
4191
4192
  // Process private headers after public headers so that they take
4193
  // precedence if a file is listed in both.
4194
0
  if (cmValue files = this->GetProperty("PRIVATE_HEADER")) {
4195
0
    cmList relFiles{ *files };
4196
0
    for (auto const& relFile : relFiles) {
4197
0
      if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
4198
0
        SourceFileFlags& flags = this->SourceFlagsMap[sf];
4199
0
        flags.MacFolder = "PrivateHeaders";
4200
0
        flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
4201
0
      }
4202
0
    }
4203
0
  }
4204
4205
  // Mark sources listed as resources.
4206
0
  if (cmValue files = this->GetProperty("RESOURCE")) {
4207
0
    cmList relFiles{ *files };
4208
0
    for (auto const& relFile : relFiles) {
4209
0
      if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
4210
0
        SourceFileFlags& flags = this->SourceFlagsMap[sf];
4211
0
        flags.MacFolder = "";
4212
0
        if (!this->GlobalGenerator->ShouldStripResourcePath(this->Makefile)) {
4213
0
          flags.MacFolder = "Resources";
4214
0
        }
4215
0
        flags.Type = cmGeneratorTarget::SourceFileTypeResource;
4216
0
      }
4217
0
    }
4218
0
  }
4219
0
}
4220
4221
bool cmGeneratorTarget::SetDeviceLink(bool deviceLink)
4222
0
{
4223
0
  bool previous = this->DeviceLink;
4224
0
  this->DeviceLink = deviceLink;
4225
0
  return previous;
4226
0
}
4227
4228
void cmGeneratorTarget::GetTargetVersion(int& major, int& minor) const
4229
0
{
4230
0
  int patch;
4231
0
  this->GetTargetVersion("VERSION", major, minor, patch);
4232
0
}
4233
4234
void cmGeneratorTarget::GetTargetVersionFallback(
4235
  std::string const& property, std::string const& fallback_property,
4236
  int& major, int& minor, int& patch) const
4237
0
{
4238
0
  if (this->GetProperty(property)) {
4239
0
    this->GetTargetVersion(property, major, minor, patch);
4240
0
  } else {
4241
0
    this->GetTargetVersion(fallback_property, major, minor, patch);
4242
0
  }
4243
0
}
4244
4245
void cmGeneratorTarget::GetTargetVersion(std::string const& property,
4246
                                         int& major, int& minor,
4247
                                         int& patch) const
4248
0
{
4249
  // Set the default values.
4250
0
  major = 0;
4251
0
  minor = 0;
4252
0
  patch = 0;
4253
4254
0
  assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
4255
4256
0
  if (cmValue version = this->GetProperty(property)) {
4257
    // Try to parse the version number and store the results that were
4258
    // successfully parsed.
4259
0
    int parsed_major;
4260
0
    int parsed_minor;
4261
0
    int parsed_patch;
4262
0
    switch (sscanf(version->c_str(), "%d.%d.%d", &parsed_major, &parsed_minor,
4263
0
                   &parsed_patch)) {
4264
0
      case 3:
4265
0
        patch = parsed_patch;
4266
0
        CM_FALLTHROUGH;
4267
0
      case 2:
4268
0
        minor = parsed_minor;
4269
0
        CM_FALLTHROUGH;
4270
0
      case 1:
4271
0
        major = parsed_major;
4272
0
        CM_FALLTHROUGH;
4273
0
      default:
4274
0
        break;
4275
0
    }
4276
0
  }
4277
0
}
4278
4279
std::string cmGeneratorTarget::GetRuntimeLinkLibrary(
4280
  std::string const& lang, std::string const& config) const
4281
0
{
4282
  // This is activated by the presence of a default selection whether or
4283
  // not it is overridden by a property.
4284
0
  cmValue runtimeLibraryDefault = this->Makefile->GetDefinition(
4285
0
    cmStrCat("CMAKE_", lang, "_RUNTIME_LIBRARY_DEFAULT"));
4286
0
  if (!cmNonempty(runtimeLibraryDefault)) {
4287
0
    return std::string();
4288
0
  }
4289
0
  cmValue runtimeLibraryValue =
4290
0
    this->Target->GetProperty(cmStrCat(lang, "_RUNTIME_LIBRARY"));
4291
0
  if (!runtimeLibraryValue) {
4292
0
    runtimeLibraryValue = runtimeLibraryDefault;
4293
0
  }
4294
0
  return cmSystemTools::UpperCase(cmGeneratorExpression::Evaluate(
4295
0
    *runtimeLibraryValue, this->LocalGenerator, config, this));
4296
0
}
4297
4298
std::string cmGeneratorTarget::GetFortranModuleDirectory(
4299
  std::string const& working_dir) const
4300
0
{
4301
0
  if (!this->FortranModuleDirectoryCreated) {
4302
0
    this->FortranModuleDirectory =
4303
0
      this->CreateFortranModuleDirectory(working_dir);
4304
0
    this->FortranModuleDirectoryCreated = true;
4305
0
  }
4306
4307
0
  return this->FortranModuleDirectory;
4308
0
}
4309
4310
bool cmGeneratorTarget::IsFortranBuildingIntrinsicModules() const
4311
0
{
4312
  // ATTENTION Before 4.0 the property name was misspelled.
4313
  // Check the correct name first and than the old name.
4314
0
  if (cmValue prop = this->GetProperty("Fortran_BUILDING_INTRINSIC_MODULES")) {
4315
0
    return prop.IsOn();
4316
0
  }
4317
0
  if (cmValue prop =
4318
0
        this->GetProperty("Fortran_BUILDING_INSTRINSIC_MODULES")) {
4319
0
    return prop.IsOn();
4320
0
  }
4321
0
  return false;
4322
0
}
4323
4324
std::string cmGeneratorTarget::CreateFortranModuleDirectory(
4325
  std::string const& working_dir) const
4326
0
{
4327
0
  std::string mod_dir;
4328
0
  std::string target_mod_dir;
4329
0
  if (cmValue prop = this->GetProperty("Fortran_MODULE_DIRECTORY")) {
4330
0
    target_mod_dir = *prop;
4331
0
  } else {
4332
0
    std::string const& default_mod_dir =
4333
0
      this->LocalGenerator->GetCurrentBinaryDirectory();
4334
0
    if (default_mod_dir != working_dir) {
4335
0
      target_mod_dir = default_mod_dir;
4336
0
    }
4337
0
  }
4338
0
  cmValue moddir_flag =
4339
0
    this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
4340
0
  if (!target_mod_dir.empty() && moddir_flag) {
4341
    // Compute the full path to the module directory.
4342
0
    if (cmSystemTools::FileIsFullPath(target_mod_dir)) {
4343
      // Already a full path.
4344
0
      mod_dir = target_mod_dir;
4345
0
    } else {
4346
      // Interpret relative to the current output directory.
4347
0
      mod_dir = cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(),
4348
0
                         '/', target_mod_dir);
4349
0
    }
4350
4351
    // Make sure the module output directory exists.
4352
0
    cmSystemTools::MakeDirectory(mod_dir);
4353
0
  }
4354
0
  return mod_dir;
4355
0
}
4356
4357
void cmGeneratorTarget::AddISPCGeneratedHeader(std::string const& header,
4358
                                               std::string const& config)
4359
0
{
4360
0
  std::string config_upper;
4361
0
  if (!config.empty()) {
4362
0
    config_upper = cmSystemTools::UpperCase(config);
4363
0
  }
4364
0
  auto iter = this->ISPCGeneratedHeaders.find(config_upper);
4365
0
  if (iter == this->ISPCGeneratedHeaders.end()) {
4366
0
    std::vector<std::string> headers;
4367
0
    headers.emplace_back(header);
4368
0
    this->ISPCGeneratedHeaders.insert({ config_upper, headers });
4369
0
  } else {
4370
0
    iter->second.emplace_back(header);
4371
0
  }
4372
0
}
4373
4374
std::vector<std::string> cmGeneratorTarget::GetGeneratedISPCHeaders(
4375
  std::string const& config) const
4376
0
{
4377
0
  std::string config_upper;
4378
0
  if (!config.empty()) {
4379
0
    config_upper = cmSystemTools::UpperCase(config);
4380
0
  }
4381
0
  auto iter = this->ISPCGeneratedHeaders.find(config_upper);
4382
0
  if (iter == this->ISPCGeneratedHeaders.end()) {
4383
0
    return std::vector<std::string>{};
4384
0
  }
4385
0
  return iter->second;
4386
0
}
4387
4388
void cmGeneratorTarget::AddISPCGeneratedObject(std::vector<std::string>&& objs,
4389
                                               std::string const& config)
4390
0
{
4391
0
  std::string config_upper;
4392
0
  if (!config.empty()) {
4393
0
    config_upper = cmSystemTools::UpperCase(config);
4394
0
  }
4395
0
  auto iter = this->ISPCGeneratedObjects.find(config_upper);
4396
0
  if (iter == this->ISPCGeneratedObjects.end()) {
4397
0
    this->ISPCGeneratedObjects.insert({ config_upper, objs });
4398
0
  } else {
4399
0
    iter->second.insert(iter->second.end(), objs.begin(), objs.end());
4400
0
  }
4401
0
}
4402
4403
std::vector<std::string> cmGeneratorTarget::GetGeneratedISPCObjects(
4404
  std::string const& config) const
4405
0
{
4406
0
  std::string config_upper;
4407
0
  if (!config.empty()) {
4408
0
    config_upper = cmSystemTools::UpperCase(config);
4409
0
  }
4410
0
  auto iter = this->ISPCGeneratedObjects.find(config_upper);
4411
0
  if (iter == this->ISPCGeneratedObjects.end()) {
4412
0
    return std::vector<std::string>{};
4413
0
  }
4414
0
  return iter->second;
4415
0
}
4416
4417
std::string cmGeneratorTarget::GetFrameworkVersion() const
4418
0
{
4419
0
  assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
4420
4421
0
  if (cmValue fversion = this->GetProperty("FRAMEWORK_VERSION")) {
4422
0
    return *fversion;
4423
0
  }
4424
0
  if (cmValue tversion = this->GetProperty("VERSION")) {
4425
0
    return *tversion;
4426
0
  }
4427
0
  return "A";
4428
0
}
4429
4430
std::string cmGeneratorTarget::ComputeVersionedName(std::string const& prefix,
4431
                                                    std::string const& base,
4432
                                                    std::string const& suffix,
4433
                                                    std::string const& name,
4434
                                                    cmValue version) const
4435
0
{
4436
0
  std::string vName = this->IsApple() ? (prefix + base) : name;
4437
0
  if (version) {
4438
0
    vName += ".";
4439
0
    vName += *version;
4440
0
  }
4441
0
  vName += this->IsApple() ? suffix : std::string();
4442
0
  return vName;
4443
0
}
4444
4445
std::vector<std::string> cmGeneratorTarget::GetPropertyKeys() const
4446
0
{
4447
0
  return this->Target->GetProperties().GetKeys();
4448
0
}
4449
4450
void cmGeneratorTarget::ReportPropertyOrigin(
4451
  std::string const& p, std::string const& result, std::string const& report,
4452
  std::string const& compatibilityType) const
4453
0
{
4454
0
  cmList debugProperties{ this->Target->GetMakefile()->GetDefinition(
4455
0
    "CMAKE_DEBUG_TARGET_PROPERTIES") };
4456
0
  bool debugOrigin = !this->DebugCompatiblePropertiesDone[p] &&
4457
0
    cm::contains(debugProperties, p);
4458
4459
0
  this->DebugCompatiblePropertiesDone[p] = true;
4460
0
  if (!debugOrigin) {
4461
0
    return;
4462
0
  }
4463
4464
0
  std::string areport =
4465
0
    cmStrCat(compatibilityType, " of property \"", p, "\" for target \"",
4466
0
             this->GetName(), "\" (result: \"", result, "\"):\n", report);
4467
4468
0
  this->LocalGenerator->GetCMakeInstance()->IssueMessage(MessageType::LOG,
4469
0
                                                         areport);
4470
0
}
4471
4472
std::string cmGeneratorTarget::GetDirectory(
4473
  std::string const& config, cmStateEnums::ArtifactType artifact) const
4474
0
{
4475
0
  if (this->IsImported()) {
4476
0
    auto fullPath = this->Target->ImportedGetFullPath(config, artifact);
4477
0
    if (this->IsFrameworkOnApple()) {
4478
0
      auto fwDescriptor = this->GetGlobalGenerator()->SplitFrameworkPath(
4479
0
        fullPath, cmGlobalGenerator::FrameworkFormat::Strict);
4480
0
      if (fwDescriptor) {
4481
0
        return fwDescriptor->Directory;
4482
0
      }
4483
0
    }
4484
    // Return the directory from which the target is imported.
4485
0
    return cmSystemTools::GetFilenamePath(fullPath);
4486
0
  }
4487
0
  if (OutputInfo const* info = this->GetOutputInfo(config)) {
4488
    // Return the directory in which the target will be built.
4489
0
    switch (artifact) {
4490
0
      case cmStateEnums::RuntimeBinaryArtifact:
4491
0
        return info->OutDir;
4492
0
      case cmStateEnums::ImportLibraryArtifact:
4493
0
        return info->ImpDir;
4494
0
    }
4495
0
  }
4496
0
  return "";
4497
0
}
4498
4499
bool cmGeneratorTarget::UsesDefaultOutputDir(
4500
  std::string const& config, cmStateEnums::ArtifactType artifact) const
4501
0
{
4502
0
  std::string dir;
4503
0
  return this->ComputeOutputDir(config, artifact, dir);
4504
0
}
4505
4506
cmGeneratorTarget::OutputInfo const* cmGeneratorTarget::GetOutputInfo(
4507
  std::string const& config) const
4508
0
{
4509
  // There is no output information for imported targets.
4510
0
  if (this->IsImported()) {
4511
0
    return nullptr;
4512
0
  }
4513
4514
  // Synthetic targets don't have output.
4515
0
  if (this->IsSynthetic()) {
4516
0
    return nullptr;
4517
0
  }
4518
4519
  // Only libraries and executables have well-defined output files.
4520
0
  if (!this->HaveWellDefinedOutputFiles()) {
4521
0
    std::string msg = cmStrCat("cmGeneratorTarget::GetOutputInfo called for ",
4522
0
                               this->GetName(), " which has type ",
4523
0
                               cmState::GetTargetTypeName(this->GetType()));
4524
0
    this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
4525
0
    return nullptr;
4526
0
  }
4527
4528
  // Lookup/compute/cache the output information for this configuration.
4529
0
  std::string config_upper;
4530
0
  if (!config.empty()) {
4531
0
    config_upper = cmSystemTools::UpperCase(config);
4532
0
  }
4533
0
  auto i = this->OutputInfoMap.find(config_upper);
4534
0
  if (i == this->OutputInfoMap.end()) {
4535
    // Add empty info in map to detect potential recursion.
4536
0
    OutputInfo info;
4537
0
    OutputInfoMapType::value_type entry(config_upper, info);
4538
0
    i = this->OutputInfoMap.insert(entry).first;
4539
4540
    // Compute output directories.
4541
0
    this->ComputeOutputDir(config, cmStateEnums::RuntimeBinaryArtifact,
4542
0
                           info.OutDir);
4543
0
    this->ComputeOutputDir(config, cmStateEnums::ImportLibraryArtifact,
4544
0
                           info.ImpDir);
4545
0
    if (!this->ComputePDBOutputDir("PDB", config, info.PdbDir)) {
4546
0
      info.PdbDir = info.OutDir;
4547
0
    }
4548
4549
    // Now update the previously-prepared map entry.
4550
0
    i->second = info;
4551
0
  } else if (i->second.empty()) {
4552
    // An empty map entry indicates we have been called recursively
4553
    // from the above block.
4554
0
    this->LocalGenerator->GetCMakeInstance()->IssueMessage(
4555
0
      MessageType::FATAL_ERROR,
4556
0
      cmStrCat("Target '", this->GetName(),
4557
0
               "' OUTPUT_DIRECTORY depends on itself."),
4558
0
      this->GetBacktrace());
4559
0
    return nullptr;
4560
0
  }
4561
0
  return &i->second;
4562
0
}
4563
4564
bool cmGeneratorTarget::ComputeOutputDir(std::string const& config,
4565
                                         cmStateEnums::ArtifactType artifact,
4566
                                         std::string& out) const
4567
0
{
4568
0
  bool usesDefaultOutputDir = false;
4569
0
  std::string conf = config;
4570
4571
  // Look for a target property defining the target output directory
4572
  // based on the target type.
4573
0
  std::string targetTypeName = this->GetOutputTargetType(artifact);
4574
0
  std::string propertyName;
4575
0
  if (!targetTypeName.empty()) {
4576
0
    propertyName = cmStrCat(targetTypeName, "_OUTPUT_DIRECTORY");
4577
0
  }
4578
4579
  // Check for a per-configuration output directory target property.
4580
0
  std::string configUpper = cmSystemTools::UpperCase(conf);
4581
0
  std::string configProp;
4582
0
  if (!targetTypeName.empty()) {
4583
0
    configProp = cmStrCat(targetTypeName, "_OUTPUT_DIRECTORY_", configUpper);
4584
0
  }
4585
4586
  // Select an output directory.
4587
0
  if (cmValue config_outdir = this->GetProperty(configProp)) {
4588
    // Use the user-specified per-configuration output directory.
4589
0
    out = cmGeneratorExpression::Evaluate(*config_outdir, this->LocalGenerator,
4590
0
                                          config, this);
4591
4592
    // Skip per-configuration subdirectory.
4593
0
    conf.clear();
4594
0
  } else if (cmValue outdir = this->GetProperty(propertyName)) {
4595
    // Use the user-specified output directory.
4596
0
    out = cmGeneratorExpression::Evaluate(*outdir, this->LocalGenerator,
4597
0
                                          config, this);
4598
    // Skip per-configuration subdirectory if the value contained a
4599
    // generator expression.
4600
0
    if (out != *outdir) {
4601
0
      conf.clear();
4602
0
    }
4603
0
  } else if (this->GetType() == cmStateEnums::EXECUTABLE) {
4604
    // Lookup the output path for executables.
4605
0
    out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
4606
0
  } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
4607
0
             this->GetType() == cmStateEnums::SHARED_LIBRARY ||
4608
0
             this->GetType() == cmStateEnums::MODULE_LIBRARY) {
4609
    // Lookup the output path for libraries.
4610
0
    out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
4611
0
  }
4612
0
  if (out.empty()) {
4613
    // Default to the current output directory.
4614
0
    usesDefaultOutputDir = true;
4615
0
    out = ".";
4616
0
  }
4617
4618
  // Convert the output path to a full path in case it is
4619
  // specified as a relative path.  Treat a relative path as
4620
  // relative to the current output directory for this makefile.
4621
0
  out = (cmSystemTools::CollapseFullPath(
4622
0
    out, this->LocalGenerator->GetCurrentBinaryDirectory()));
4623
4624
  // The generator may add the configuration's subdirectory.
4625
0
  if (!conf.empty()) {
4626
0
    bool useEPN =
4627
0
      this->GlobalGenerator->UseEffectivePlatformName(this->Makefile);
4628
0
    std::string suffix =
4629
0
      usesDefaultOutputDir && useEPN ? "${EFFECTIVE_PLATFORM_NAME}" : "";
4630
0
    this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
4631
0
      "/", conf, suffix, out);
4632
0
  }
4633
4634
0
  return usesDefaultOutputDir;
4635
0
}
4636
4637
bool cmGeneratorTarget::ComputePDBOutputDir(std::string const& kind,
4638
                                            std::string const& config,
4639
                                            std::string& out) const
4640
0
{
4641
  // Look for a target property defining the target output directory
4642
  // based on the target type.
4643
0
  std::string propertyName;
4644
0
  if (!kind.empty()) {
4645
0
    propertyName = cmStrCat(kind, "_OUTPUT_DIRECTORY");
4646
0
  }
4647
0
  std::string conf = config;
4648
4649
  // Check for a per-configuration output directory target property.
4650
0
  std::string configUpper = cmSystemTools::UpperCase(conf);
4651
0
  std::string configProp;
4652
0
  if (!kind.empty()) {
4653
0
    configProp = cmStrCat(kind, "_OUTPUT_DIRECTORY_", configUpper);
4654
0
  }
4655
4656
  // Select an output directory.
4657
0
  if (cmValue config_outdir = this->GetProperty(configProp)) {
4658
    // Use the user-specified per-configuration output directory.
4659
0
    out = cmGeneratorExpression::Evaluate(*config_outdir, this->LocalGenerator,
4660
0
                                          config);
4661
4662
    // Skip per-configuration subdirectory.
4663
0
    conf.clear();
4664
0
  } else if (cmValue outdir = this->GetProperty(propertyName)) {
4665
    // Use the user-specified output directory.
4666
0
    out =
4667
0
      cmGeneratorExpression::Evaluate(*outdir, this->LocalGenerator, config);
4668
4669
    // Skip per-configuration subdirectory if the value contained a
4670
    // generator expression.
4671
0
    if (out != *outdir) {
4672
0
      conf.clear();
4673
0
    }
4674
0
  }
4675
0
  if (out.empty()) {
4676
0
    cmGeneratorTarget const* reuseTarget = this->GetPchReuseTarget();
4677
0
    bool const hasReuse = reuseTarget && reuseTarget != this;
4678
    // Compiler-generated PDB output always needed for REUSE_FROM.
4679
0
    if (kind == "COMPILE_PDB"_s && (this->PchReused || hasReuse)) {
4680
0
      out = this->GetSupportDirectory();
4681
0
    } else {
4682
0
      return false;
4683
0
    }
4684
0
  }
4685
4686
  // Convert the output path to a full path in case it is
4687
  // specified as a relative path.  Treat a relative path as
4688
  // relative to the current output directory for this makefile.
4689
0
  out = (cmSystemTools::CollapseFullPath(
4690
0
    out, this->LocalGenerator->GetCurrentBinaryDirectory()));
4691
4692
  // The generator may add the configuration's subdirectory.
4693
0
  if (!conf.empty()) {
4694
0
    this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
4695
0
      "/", conf, "", out);
4696
0
  }
4697
0
  return true;
4698
0
}
4699
4700
bool cmGeneratorTarget::HaveInstallTreeRPATH(std::string const& config) const
4701
0
{
4702
0
  std::string install_rpath;
4703
0
  this->GetInstallRPATH(config, install_rpath);
4704
0
  return !install_rpath.empty() &&
4705
0
    !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
4706
0
}
4707
4708
bool cmGeneratorTarget::GetBuildRPATH(std::string const& config,
4709
                                      std::string& rpath) const
4710
0
{
4711
0
  return this->GetRPATH(config, "BUILD_RPATH", rpath);
4712
0
}
4713
4714
bool cmGeneratorTarget::GetInstallRPATH(std::string const& config,
4715
                                        std::string& rpath) const
4716
0
{
4717
0
  return this->GetRPATH(config, "INSTALL_RPATH", rpath);
4718
0
}
4719
4720
bool cmGeneratorTarget::GetRPATH(std::string const& config,
4721
                                 std::string const& prop,
4722
                                 std::string& rpath) const
4723
0
{
4724
0
  cmValue value = this->GetProperty(prop);
4725
0
  if (!value) {
4726
0
    return false;
4727
0
  }
4728
4729
0
  rpath =
4730
0
    cmGeneratorExpression::Evaluate(*value, this->LocalGenerator, config);
4731
4732
0
  return true;
4733
0
}
4734
4735
cmGeneratorTarget::ImportInfo const* cmGeneratorTarget::GetImportInfo(
4736
  std::string const& config) const
4737
0
{
4738
  // There is no imported information for non-imported targets.
4739
0
  if (!this->IsImported()) {
4740
0
    return nullptr;
4741
0
  }
4742
4743
  // Lookup/compute/cache the import information for this
4744
  // configuration.
4745
0
  std::string config_upper;
4746
0
  if (!config.empty()) {
4747
0
    config_upper = cmSystemTools::UpperCase(config);
4748
0
  } else {
4749
0
    config_upper = "NOCONFIG";
4750
0
  }
4751
4752
0
  auto i = this->ImportInfoMap.find(config_upper);
4753
0
  if (i == this->ImportInfoMap.end()) {
4754
0
    ImportInfo info;
4755
0
    this->ComputeImportInfo(config_upper, info);
4756
0
    ImportInfoMapType::value_type entry(config_upper, info);
4757
0
    i = this->ImportInfoMap.insert(entry).first;
4758
0
  }
4759
4760
0
  if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
4761
0
    return &i->second;
4762
0
  }
4763
  // If the location is empty then the target is not available for
4764
  // this configuration.
4765
0
  if (i->second.Location.empty() && i->second.ImportLibrary.empty()) {
4766
0
    return nullptr;
4767
0
  }
4768
4769
  // Return the import information.
4770
0
  return &i->second;
4771
0
}
4772
4773
void cmGeneratorTarget::ComputeImportInfo(std::string const& desired_config,
4774
                                          ImportInfo& info) const
4775
0
{
4776
  // This method finds information about an imported target from its
4777
  // properties.  The "IMPORTED_" namespace is reserved for properties
4778
  // defined by the project exporting the target.
4779
4780
  // Initialize members.
4781
0
  info.NoSOName = false;
4782
4783
0
  cmValue loc = nullptr;
4784
0
  cmValue imp = nullptr;
4785
0
  std::string suffix;
4786
0
  if (!this->Target->GetMappedConfig(desired_config, loc, imp, suffix)) {
4787
0
    return;
4788
0
  }
4789
4790
  // Get the link interface.
4791
0
  {
4792
    // Use the INTERFACE_LINK_LIBRARIES special representation directly
4793
    // to get backtraces.
4794
0
    cmBTStringRange entries = this->Target->GetLinkInterfaceEntries();
4795
0
    if (!entries.empty()) {
4796
0
      info.LibrariesProp = "INTERFACE_LINK_LIBRARIES";
4797
0
      for (BT<std::string> const& entry : entries) {
4798
0
        info.Libraries.emplace_back(entry);
4799
0
      }
4800
0
    } else if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
4801
0
      std::string linkProp =
4802
0
        cmStrCat("IMPORTED_LINK_INTERFACE_LIBRARIES", suffix);
4803
0
      cmValue propertyLibs = this->GetProperty(linkProp);
4804
0
      if (!propertyLibs) {
4805
0
        linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
4806
0
        propertyLibs = this->GetProperty(linkProp);
4807
0
      }
4808
0
      if (propertyLibs) {
4809
0
        info.LibrariesProp = linkProp;
4810
0
        info.Libraries.emplace_back(*propertyLibs);
4811
0
      }
4812
0
    }
4813
0
  }
4814
0
  for (BT<std::string> const& entry :
4815
0
       this->Target->GetLinkInterfaceDirectEntries()) {
4816
0
    info.LibrariesHeadInclude.emplace_back(entry);
4817
0
  }
4818
0
  for (BT<std::string> const& entry :
4819
0
       this->Target->GetLinkInterfaceDirectExcludeEntries()) {
4820
0
    info.LibrariesHeadExclude.emplace_back(entry);
4821
0
  }
4822
0
  if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
4823
0
    if (loc) {
4824
0
      info.LibName = *loc;
4825
0
    }
4826
0
    return;
4827
0
  }
4828
4829
  // A provided configuration has been chosen.  Load the
4830
  // configuration's properties.
4831
4832
  // Get the location.
4833
0
  if (loc) {
4834
0
    info.Location = *loc;
4835
0
  } else {
4836
0
    std::string impProp = cmStrCat("IMPORTED_LOCATION", suffix);
4837
0
    if (cmValue config_location = this->GetProperty(impProp)) {
4838
0
      info.Location = *config_location;
4839
0
    } else if (cmValue location = this->GetProperty("IMPORTED_LOCATION")) {
4840
0
      info.Location = *location;
4841
0
    }
4842
0
  }
4843
4844
  // Get the soname.
4845
0
  if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
4846
0
    std::string soProp = cmStrCat("IMPORTED_SONAME", suffix);
4847
0
    if (cmValue config_soname = this->GetProperty(soProp)) {
4848
0
      info.SOName = *config_soname;
4849
0
    } else if (cmValue soname = this->GetProperty("IMPORTED_SONAME")) {
4850
0
      info.SOName = *soname;
4851
0
    }
4852
0
  }
4853
4854
  // Get the "no-soname" mark.
4855
0
  if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
4856
0
    std::string soProp = cmStrCat("IMPORTED_NO_SONAME", suffix);
4857
0
    if (cmValue config_no_soname = this->GetProperty(soProp)) {
4858
0
      info.NoSOName = config_no_soname.IsOn();
4859
0
    } else if (cmValue no_soname = this->GetProperty("IMPORTED_NO_SONAME")) {
4860
0
      info.NoSOName = no_soname.IsOn();
4861
0
    }
4862
0
  }
4863
4864
  // Get the import library.
4865
0
  if (imp) {
4866
0
    info.ImportLibrary = *imp;
4867
0
  } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
4868
0
             this->IsExecutableWithExports()) {
4869
0
    std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
4870
0
    if (cmValue config_implib = this->GetProperty(impProp)) {
4871
0
      info.ImportLibrary = *config_implib;
4872
0
    } else if (cmValue implib = this->GetProperty("IMPORTED_IMPLIB")) {
4873
0
      info.ImportLibrary = *implib;
4874
0
    }
4875
0
  }
4876
4877
  // Get the link dependencies.
4878
0
  {
4879
0
    std::string linkProp =
4880
0
      cmStrCat("IMPORTED_LINK_DEPENDENT_LIBRARIES", suffix);
4881
0
    if (cmValue config_libs = this->GetProperty(linkProp)) {
4882
0
      info.SharedDeps = *config_libs;
4883
0
    } else if (cmValue libs =
4884
0
                 this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES")) {
4885
0
      info.SharedDeps = *libs;
4886
0
    }
4887
0
  }
4888
4889
  // Get the link languages.
4890
0
  if (this->LinkLanguagePropagatesToDependents()) {
4891
0
    std::string linkProp =
4892
0
      cmStrCat("IMPORTED_LINK_INTERFACE_LANGUAGES", suffix);
4893
0
    if (cmValue config_libs = this->GetProperty(linkProp)) {
4894
0
      info.Languages = *config_libs;
4895
0
    } else if (cmValue libs =
4896
0
                 this->GetProperty("IMPORTED_LINK_INTERFACE_LANGUAGES")) {
4897
0
      info.Languages = *libs;
4898
0
    }
4899
0
  }
4900
4901
  // Get information if target is managed assembly.
4902
0
  {
4903
0
    std::string linkProp = "IMPORTED_COMMON_LANGUAGE_RUNTIME";
4904
0
    if (cmValue pc = this->GetProperty(linkProp + suffix)) {
4905
0
      info.Managed = this->CheckManagedType(*pc);
4906
0
    } else if (cmValue p = this->GetProperty(linkProp)) {
4907
0
      info.Managed = this->CheckManagedType(*p);
4908
0
    }
4909
0
  }
4910
4911
  // Get the cyclic repetition count.
4912
0
  if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
4913
0
    std::string linkProp =
4914
0
      cmStrCat("IMPORTED_LINK_INTERFACE_MULTIPLICITY", suffix);
4915
0
    if (cmValue config_reps = this->GetProperty(linkProp)) {
4916
0
      sscanf(config_reps->c_str(), "%u", &info.Multiplicity);
4917
0
    } else if (cmValue reps =
4918
0
                 this->GetProperty("IMPORTED_LINK_INTERFACE_MULTIPLICITY")) {
4919
0
      sscanf(reps->c_str(), "%u", &info.Multiplicity);
4920
0
    }
4921
0
  }
4922
0
}
4923
4924
bool cmGeneratorTarget::GetConfigCommonSourceFilesForXcode(
4925
  std::vector<cmSourceFile*>& files) const
4926
0
{
4927
0
  std::vector<std::string> const& configs =
4928
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
4929
4930
0
  auto it = configs.begin();
4931
0
  std::string const& firstConfig = *it;
4932
0
  this->GetSourceFilesWithoutObjectLibraries(files, firstConfig);
4933
4934
0
  for (; it != configs.end(); ++it) {
4935
0
    std::vector<cmSourceFile*> configFiles;
4936
0
    this->GetSourceFilesWithoutObjectLibraries(configFiles, *it);
4937
0
    if (configFiles != files) {
4938
0
      std::string firstConfigFiles;
4939
0
      char const* sep = "";
4940
0
      for (cmSourceFile* f : files) {
4941
0
        firstConfigFiles += sep;
4942
0
        firstConfigFiles += f->ResolveFullPath();
4943
0
        sep = "\n  ";
4944
0
      }
4945
4946
0
      std::string thisConfigFiles;
4947
0
      sep = "";
4948
0
      for (cmSourceFile* f : configFiles) {
4949
0
        thisConfigFiles += sep;
4950
0
        thisConfigFiles += f->ResolveFullPath();
4951
0
        sep = "\n  ";
4952
0
      }
4953
0
      std::ostringstream e;
4954
      /* clang-format off */
4955
0
      e << "Target \"" << this->GetName()
4956
0
        << "\" has source files which vary by "
4957
0
        "configuration. This is not supported by the \""
4958
0
        << this->GlobalGenerator->GetName()
4959
0
        << "\" generator.\n"
4960
0
          "Config \"" << firstConfig << "\":\n"
4961
0
          "  " << firstConfigFiles << "\n"
4962
0
          "Config \"" << *it << "\":\n"
4963
0
          "  " << thisConfigFiles << "\n";
4964
      /* clang-format on */
4965
0
      this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
4966
0
      return false;
4967
0
    }
4968
0
  }
4969
0
  return true;
4970
0
}
4971
4972
void cmGeneratorTarget::GetObjectLibrariesInSources(
4973
  std::vector<BT<cmGeneratorTarget*>>& objlibs) const
4974
0
{
4975
  // FIXME: This searches SOURCES for TARGET_OBJECTS for backwards
4976
  // compatibility with the OLD behavior of CMP0026 since this
4977
  // could be called at configure time.  CMP0026 has been removed,
4978
  // so this should now be called only at generate time.
4979
  // Therefore we should be able to improve the implementation
4980
  // with generate-time information.
4981
0
  cmBTStringRange rng = this->Target->GetSourceEntries();
4982
0
  for (auto const& entry : rng) {
4983
0
    cmList files{ entry.Value };
4984
0
    for (auto const& li : files) {
4985
0
      if (cmHasLiteralPrefix(li, "$<TARGET_OBJECTS:") && li.back() == '>') {
4986
0
        std::string objLibName = li.substr(17, li.size() - 18);
4987
4988
0
        if (cmGeneratorExpression::Find(objLibName) != std::string::npos) {
4989
0
          continue;
4990
0
        }
4991
0
        cmGeneratorTarget* objLib =
4992
0
          this->LocalGenerator->FindGeneratorTargetToUse(objLibName);
4993
0
        if (objLib) {
4994
0
          objlibs.emplace_back(objLib, entry.Backtrace);
4995
0
        }
4996
0
      }
4997
0
    }
4998
0
  }
4999
0
}
5000
5001
std::string cmGeneratorTarget::CheckCMP0004(std::string const& item) const
5002
0
{
5003
  // Strip whitespace off the library names because we used to do this
5004
  // in case variables were expanded at generate time.  We no longer
5005
  // do the expansion but users link to libraries like " ${VAR} ".
5006
0
  std::string lib = item;
5007
0
  std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
5008
0
  if (pos != std::string::npos) {
5009
0
    lib = lib.substr(pos);
5010
0
  }
5011
0
  pos = lib.find_last_not_of(" \t\r\n");
5012
0
  if (pos != std::string::npos) {
5013
0
    lib = lib.substr(0, pos + 1);
5014
0
  }
5015
0
  if (lib != item) {
5016
0
    cmake* cm = this->LocalGenerator->GetCMakeInstance();
5017
0
    std::ostringstream e;
5018
0
    e << "Target \"" << this->GetName() << "\" links to item \"" << item
5019
0
      << "\" which has leading or trailing whitespace.  "
5020
0
      << "This is now an error according to policy CMP0004.";
5021
0
    cm->IssueMessage(MessageType::FATAL_ERROR, e.str(), this->GetBacktrace());
5022
0
  }
5023
0
  return lib;
5024
0
}
5025
5026
bool cmGeneratorTarget::IsDeprecated() const
5027
0
{
5028
0
  cmValue deprecation = this->GetProperty("DEPRECATION");
5029
0
  return cmNonempty(deprecation);
5030
0
}
5031
5032
std::string cmGeneratorTarget::GetDeprecation() const
5033
0
{
5034
  // find DEPRECATION property
5035
0
  if (cmValue deprecation = this->GetProperty("DEPRECATION")) {
5036
0
    return *deprecation;
5037
0
  }
5038
0
  return std::string();
5039
0
}
5040
5041
void cmGeneratorTarget::GetLanguages(std::set<std::string>& languages,
5042
                                     std::string const& config) const
5043
0
{
5044
  // Targets that do not compile anything have no languages.
5045
0
  if (!this->CanCompileSources()) {
5046
0
    return;
5047
0
  }
5048
5049
0
  std::vector<cmSourceFile*> sourceFiles;
5050
0
  this->GetSourceFiles(sourceFiles, config);
5051
0
  for (cmSourceFile* src : sourceFiles) {
5052
0
    std::string const& lang = src->GetOrDetermineLanguage();
5053
0
    if (!lang.empty()) {
5054
0
      languages.insert(lang);
5055
0
    }
5056
0
  }
5057
5058
0
  std::set<cmGeneratorTarget const*> objectLibraries =
5059
0
    this->GetSourceObjectLibraries(config);
5060
0
  for (cmGeneratorTarget const* objLib : objectLibraries) {
5061
0
    objLib->GetLanguages(languages, config);
5062
0
  }
5063
0
}
5064
5065
std::set<cmGeneratorTarget const*> cmGeneratorTarget::GetSourceObjectLibraries(
5066
  std::string const& config) const
5067
0
{
5068
0
  std::set<cmGeneratorTarget const*> objectLibraries;
5069
0
  std::vector<cmSourceFile const*> externalObjects;
5070
0
  this->GetExternalObjects(externalObjects, config);
5071
0
  for (cmSourceFile const* extObj : externalObjects) {
5072
0
    std::string objLib = extObj->GetObjectLibrary();
5073
0
    if (cmGeneratorTarget* tgt =
5074
0
          this->LocalGenerator->FindGeneratorTargetToUse(objLib)) {
5075
0
      objectLibraries.insert(tgt);
5076
0
    }
5077
0
  }
5078
5079
0
  return objectLibraries;
5080
0
}
5081
5082
bool cmGeneratorTarget::IsLanguageUsed(std::string const& language,
5083
                                       std::string const& config) const
5084
0
{
5085
0
  std::set<std::string> languages;
5086
0
  this->GetLanguages(languages, config);
5087
0
  return languages.count(language);
5088
0
}
5089
5090
bool cmGeneratorTarget::IsCSharpOnly() const
5091
0
{
5092
  // Only certain target types may compile CSharp.
5093
0
  if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
5094
0
      this->GetType() != cmStateEnums::STATIC_LIBRARY &&
5095
0
      this->GetType() != cmStateEnums::EXECUTABLE) {
5096
0
    return false;
5097
0
  }
5098
0
  std::set<std::string> languages = this->GetAllConfigCompileLanguages();
5099
  // Consider an explicit linker language property, but *not* the
5100
  // computed linker language that may depend on linked targets.
5101
0
  cmValue linkLang = this->GetProperty("LINKER_LANGUAGE");
5102
0
  if (cmNonempty(linkLang)) {
5103
0
    languages.insert(*linkLang);
5104
0
  }
5105
0
  return languages.size() == 1 && languages.count("CSharp") > 0;
5106
0
}
5107
5108
bool cmGeneratorTarget::IsDotNetSdkTarget() const
5109
0
{
5110
0
  return !this->GetProperty("DOTNET_SDK").IsEmpty();
5111
0
}
5112
5113
void cmGeneratorTarget::ComputeLinkImplementationLanguages(
5114
  std::string const& config, cmOptionalLinkImplementation& impl) const
5115
0
{
5116
  // This target needs runtime libraries for its source languages.
5117
0
  std::set<std::string> languages;
5118
  // Get languages used in our source files.
5119
0
  this->GetLanguages(languages, config);
5120
  // Copy the set of languages to the link implementation.
5121
0
  impl.Languages.insert(impl.Languages.begin(), languages.begin(),
5122
0
                        languages.end());
5123
0
}
5124
5125
bool cmGeneratorTarget::HaveBuildTreeRPATH(std::string const& config) const
5126
0
{
5127
0
  if (this->GetPropertyAsBool("SKIP_BUILD_RPATH")) {
5128
0
    return false;
5129
0
  }
5130
0
  std::string build_rpath;
5131
0
  if (this->GetBuildRPATH(config, build_rpath)) {
5132
0
    return true;
5133
0
  }
5134
0
  if (cmLinkImplementationLibraries const* impl =
5135
0
        this->GetLinkImplementationLibraries(config, UseTo::Link)) {
5136
0
    return !impl->Libraries.empty();
5137
0
  }
5138
0
  return false;
5139
0
}
5140
5141
bool cmGeneratorTarget::IsNullImpliedByLinkLibraries(
5142
  std::string const& p) const
5143
0
{
5144
0
  return cm::contains(this->LinkImplicitNullProperties, p);
5145
0
}
5146
5147
namespace {
5148
bool CreateCxxStdlibTarget(cmMakefile* makefile, cmLocalGenerator* lg,
5149
                           std::string const& targetName,
5150
                           std::string const& cxxTargetName,
5151
                           std::string const& stdLevel,
5152
                           std::vector<std::string> const& configs)
5153
0
{
5154
0
#ifndef CMAKE_BOOTSTRAP
5155
5156
0
  static cm::optional<cmCxxModuleMetadata> metadata;
5157
5158
  // Load metadata only when we need to create a target
5159
0
  if (!metadata) {
5160
0
    auto errorMessage =
5161
0
      makefile->GetDefinition("CMAKE_CXX_COMPILER_IMPORT_STD_ERROR_MESSAGE");
5162
0
    if (!errorMessage.IsEmpty()) {
5163
0
      makefile->IssueMessage(
5164
0
        MessageType::FATAL_ERROR,
5165
0
        cmStrCat(R"(The "CXX_MODULE_STD" property on target ")", targetName,
5166
0
                 "\" requires toolchain support, but it was not provided.  "
5167
0
                 "Reason:\n  ",
5168
0
                 *errorMessage));
5169
0
      return false;
5170
0
    }
5171
5172
0
    auto metadataPath =
5173
0
      makefile->GetDefinition("CMAKE_CXX_STDLIB_MODULES_JSON");
5174
0
    if (metadataPath.IsEmpty()) {
5175
0
      makefile->IssueMessage(
5176
0
        MessageType::FATAL_ERROR,
5177
0
        cmStrCat(
5178
0
          R"("The "CXX_MODULE_STD" property on target ")", targetName,
5179
0
          "\" requires CMAKE_CXX_STDLIB_MODULES_JSON be set, but it was not "
5180
0
          "provided by the toolchain."));
5181
0
      return false;
5182
0
    }
5183
5184
0
    auto parseResult = cmCxxModuleMetadata::LoadFromFile(*metadataPath);
5185
0
    if (!parseResult) {
5186
0
      makefile->IssueMessage(
5187
0
        MessageType::FATAL_ERROR,
5188
0
        cmStrCat("Failed to load C++ standard library modules metadata "
5189
0
                 "from \"",
5190
0
                 *metadataPath, "\": ", parseResult.Error));
5191
0
      return false;
5192
0
    }
5193
5194
0
    metadata = std::move(*parseResult.Meta);
5195
0
  }
5196
5197
0
  auto* stdlibTgt = makefile->AddImportedTarget(
5198
0
    cxxTargetName, cmStateEnums::INTERFACE_LIBRARY, true);
5199
0
  cmCxxModuleMetadata::PopulateTarget(*stdlibTgt, *metadata, configs);
5200
0
  stdlibTgt->AppendProperty("IMPORTED_CXX_MODULES_COMPILE_FEATURES",
5201
0
                            cmStrCat("cxx_std_", stdLevel));
5202
5203
0
  lg->AddGeneratorTarget(cm::make_unique<cmGeneratorTarget>(stdlibTgt, lg));
5204
5205
0
#endif // CMAKE_BOOTSTRAP
5206
5207
0
  return true;
5208
0
}
5209
} // namespace
5210
5211
bool cmGeneratorTarget::ApplyCXXStdTargets()
5212
0
{
5213
0
  cmStandardLevelResolver standardResolver(this->Makefile);
5214
0
  cmStandardLevel const cxxStd23 =
5215
0
    *standardResolver.LanguageStandardLevel("CXX", "23");
5216
0
  std::vector<std::string> const& configs =
5217
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
5218
0
  auto std_prop = this->GetProperty("CXX_MODULE_STD");
5219
0
  if (!std_prop) {
5220
    // TODO(cxxmodules): Add a target policy to flip the default here. Set
5221
    // `std_prop` based on it.
5222
0
    return true;
5223
0
  }
5224
5225
0
  std::string std_prop_value;
5226
0
  if (std_prop) {
5227
    // Evaluate generator expressions.
5228
0
    cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance());
5229
0
    auto cge = ge.Parse(*std_prop);
5230
0
    if (!cge) {
5231
0
      this->Makefile->IssueMessage(
5232
0
        MessageType::FATAL_ERROR,
5233
0
        cmStrCat(R"(The "CXX_MODULE_STD" property on the target ")",
5234
0
                 this->GetName(), "\" is not a valid generator expression."));
5235
0
      return false;
5236
0
    }
5237
    // But do not allow context-sensitive queries. Whether a target uses
5238
    // `import std` should not depend on configuration or properties of the
5239
    // consumer (head target). The link language also shouldn't matter, so ban
5240
    // it as well.
5241
0
    if (cge->GetHadHeadSensitiveCondition()) {
5242
      // Not reachable; all target-sensitive genexes actually fail to parse.
5243
0
      this->Makefile->IssueMessage(
5244
0
        MessageType::FATAL_ERROR,
5245
0
        cmStrCat(R"(The "CXX_MODULE_STD" property on the target ")",
5246
0
                 this->GetName(),
5247
0
                 "\" contains a condition that queries the "
5248
0
                 "consuming target which is not supported."));
5249
0
      return false;
5250
0
    }
5251
0
    if (cge->GetHadLinkLanguageSensitiveCondition()) {
5252
      // Not reachable; all link language genexes actually fail to parse.
5253
0
      this->Makefile->IssueMessage(
5254
0
        MessageType::FATAL_ERROR,
5255
0
        cmStrCat(R"(The "CXX_MODULE_STD" property on the target ")",
5256
0
                 this->GetName(),
5257
0
                 "\" contains a condition that queries the "
5258
0
                 "link language which is not supported."));
5259
0
      return false;
5260
0
    }
5261
0
    std_prop_value = cge->Evaluate(this->LocalGenerator, "");
5262
0
    if (cge->GetHadContextSensitiveCondition()) {
5263
0
      this->Makefile->IssueMessage(
5264
0
        MessageType::FATAL_ERROR,
5265
0
        cmStrCat(R"(The "CXX_MODULE_STD" property on the target ")",
5266
0
                 this->GetName(),
5267
0
                 "\" contains a context-sensitive condition "
5268
0
                 "that is not supported."));
5269
0
      return false;
5270
0
    }
5271
0
  }
5272
0
  auto use_std = cmIsOn(std_prop_value);
5273
5274
  // If we have a value and it is not true, there's nothing to do.
5275
0
  if (std_prop && !use_std) {
5276
0
    return true;
5277
0
  }
5278
5279
0
  for (auto const& config : configs) {
5280
0
    if (this->HaveCxxModuleSupport(config) != Cxx20SupportLevel::Supported) {
5281
0
      continue;
5282
0
    }
5283
5284
0
    cm::optional<cmStandardLevel> explicitLevel =
5285
0
      this->GetExplicitStandardLevel("CXX", config);
5286
0
    if (!explicitLevel || *explicitLevel < cxxStd23) {
5287
0
      continue;
5288
0
    }
5289
5290
0
    auto const stdLevel =
5291
0
      standardResolver.GetLevelString("CXX", *explicitLevel);
5292
0
    auto const cxxTargetName = cmStrCat("__CMAKE::CXX", stdLevel);
5293
5294
    // Create the __CMAKE::CXX## IMPORTED interface target if it doesn't
5295
    // already exist
5296
0
    if (!this->Makefile->FindTargetToUse(cxxTargetName) &&
5297
0
        !CreateCxxStdlibTarget(this->Makefile, this->LocalGenerator,
5298
0
                               this->GetName(), cxxTargetName, stdLevel,
5299
0
                               configs)) {
5300
0
      return false;
5301
0
    }
5302
5303
0
    this->Target->AppendProperty(
5304
0
      "LINK_LIBRARIES",
5305
0
      cmStrCat("$<BUILD_LOCAL_INTERFACE:$<$<CONFIG:", config,
5306
0
               ">:", cxxTargetName, ">>"));
5307
0
  }
5308
5309
0
  return true;
5310
0
}
5311
5312
bool cmGeneratorTarget::DiscoverSyntheticTargets(cmSyntheticTargetCache& cache,
5313
                                                 std::string const& config)
5314
0
{
5315
0
  std::vector<std::string> allConfigs =
5316
0
    this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
5317
0
  cmOptionalLinkImplementation impl;
5318
0
  this->ComputeLinkImplementationLibraries(config, impl, UseTo::Link);
5319
5320
0
  cmCxxModuleUsageEffects usage(this);
5321
5322
0
  auto& SyntheticDeps = this->Configs[config].SyntheticDeps;
5323
5324
0
  for (auto const& entry : impl.Libraries) {
5325
0
    auto const* gt = entry.Target;
5326
0
    if (!gt || !gt->IsImported()) {
5327
0
      continue;
5328
0
    }
5329
5330
0
    if (gt->HaveCxx20ModuleSources()) {
5331
0
      cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_512);
5332
0
      constexpr size_t HASH_TRUNCATION = 12;
5333
0
      auto dirhash = hasher.HashString(
5334
0
        gt->GetLocalGenerator()->GetCurrentBinaryDirectory());
5335
0
      std::string safeName = gt->GetName();
5336
0
      cmSystemTools::ReplaceString(safeName, ":", "_");
5337
0
      auto targetIdent =
5338
0
        hasher.HashString(cmStrCat("@d_", dirhash, "@u_", usage.GetHash()));
5339
0
      std::string targetName =
5340
0
        cmStrCat(safeName, "@synth_", targetIdent.substr(0, HASH_TRUNCATION));
5341
5342
      // Check the cache to see if this instance of the imported target has
5343
      // already been created.
5344
0
      auto cached = cache.CxxModuleTargets.find(targetName);
5345
0
      cmGeneratorTarget const* synthDep = nullptr;
5346
0
      if (cached == cache.CxxModuleTargets.end()) {
5347
0
        auto const* model = gt->Target;
5348
0
        auto* mf = gt->Makefile;
5349
0
        auto* lg = gt->GetLocalGenerator();
5350
0
        auto* tgt = mf->AddSynthesizedTarget(cmStateEnums::INTERFACE_LIBRARY,
5351
0
                                             targetName);
5352
5353
        // Copy relevant information from the existing IMPORTED target.
5354
5355
        // Copy policies to the target.
5356
0
        tgt->CopyPolicyStatuses(model);
5357
5358
        // Copy file sets.
5359
0
        {
5360
0
          auto fsNames = model->GetAllFileSetNames();
5361
0
          for (auto const& fsName : fsNames) {
5362
0
            auto const* fs = model->GetFileSet(fsName);
5363
0
            if (!fs) {
5364
0
              mf->IssueMessage(MessageType::INTERNAL_ERROR,
5365
0
                               cmStrCat("Failed to find file set named '",
5366
0
                                        fsName, "' on target '",
5367
0
                                        tgt->GetName(), '\''));
5368
0
              continue;
5369
0
            }
5370
0
            auto* newFs = tgt
5371
0
                            ->GetOrCreateFileSet(fs->GetName(), fs->GetType(),
5372
0
                                                 fs->GetVisibility())
5373
0
                            .first;
5374
0
            newFs->CopyEntries(fs);
5375
0
          }
5376
0
        }
5377
5378
        // Copy imported C++ module properties.
5379
0
        tgt->CopyImportedCxxModulesEntries(model);
5380
5381
        // Copy other properties which may affect the C++ module BMI
5382
        // generation.
5383
0
        tgt->CopyImportedCxxModulesProperties(model);
5384
5385
0
        tgt->AddLinkLibrary(*mf,
5386
0
                            cmStrCat("$<COMPILE_ONLY:", model->GetName(), '>'),
5387
0
                            GENERAL_LibraryType);
5388
5389
        // Apply usage requirements to the target.
5390
0
        usage.ApplyToTarget(tgt);
5391
5392
        // Create the generator target and attach it to the local generator.
5393
0
        auto gtp = cm::make_unique<cmGeneratorTarget>(tgt, lg);
5394
5395
0
        synthDep = gtp.get();
5396
0
        cache.CxxModuleTargets[targetName] = synthDep;
5397
5398
        // See `localGen->ComputeTargetCompileFeatures()` call in
5399
        // `cmGlobalGenerator::Compute` for where non-synthetic targets resolve
5400
        // this.
5401
0
        for (auto const& innerConfig : allConfigs) {
5402
0
          gtp->ComputeCompileFeatures(innerConfig);
5403
0
        }
5404
        // See `cmGlobalGenerator::ApplyCXXStdTargets` in
5405
        // `cmGlobalGenerator::Compute` for non-synthetic target resolutions.
5406
0
        if (!gtp->ApplyCXXStdTargets()) {
5407
0
          return false;
5408
0
        }
5409
5410
0
        gtp->DiscoverSyntheticTargets(cache, config);
5411
5412
0
        lg->AddGeneratorTarget(std::move(gtp));
5413
0
      } else {
5414
0
        synthDep = cached->second;
5415
0
      }
5416
5417
0
      SyntheticDeps[gt].push_back(synthDep);
5418
0
    }
5419
0
  }
5420
5421
0
  return true;
5422
0
}
5423
5424
bool cmGeneratorTarget::HasPackageReferences() const
5425
0
{
5426
0
  return this->IsInBuildSystem() &&
5427
0
    !this->GetProperty("VS_PACKAGE_REFERENCES")->empty();
5428
0
}
5429
5430
std::vector<std::string> cmGeneratorTarget::GetPackageReferences() const
5431
0
{
5432
0
  cmList packageReferences;
5433
5434
0
  if (this->IsInBuildSystem()) {
5435
0
    if (cmValue vsPackageReferences =
5436
0
          this->GetProperty("VS_PACKAGE_REFERENCES")) {
5437
0
      packageReferences.assign(*vsPackageReferences);
5438
0
    }
5439
0
  }
5440
5441
0
  return std::move(packageReferences.data());
5442
0
}
5443
5444
std::string cmGeneratorTarget::GetPDBDirectory(std::string const& config) const
5445
0
{
5446
0
  if (OutputInfo const* info = this->GetOutputInfo(config)) {
5447
    // Return the directory in which the target will be built.
5448
0
    return info->PdbDir;
5449
0
  }
5450
0
  return "";
5451
0
}
5452
5453
bool cmGeneratorTarget::HasImplibGNUtoMS(std::string const& config) const
5454
0
{
5455
0
  return this->HasImportLibrary(config) && this->GetPropertyAsBool("GNUtoMS");
5456
0
}
5457
5458
bool cmGeneratorTarget::GetImplibGNUtoMS(std::string const& config,
5459
                                         std::string const& gnuName,
5460
                                         std::string& out,
5461
                                         char const* newExt) const
5462
0
{
5463
0
  if (this->HasImplibGNUtoMS(config) && gnuName.size() > 6 &&
5464
0
      gnuName.substr(gnuName.size() - 6) == ".dll.a") {
5465
0
    out = cmStrCat(cm::string_view(gnuName).substr(0, gnuName.size() - 6),
5466
0
                   newExt ? newExt : ".lib");
5467
0
    return true;
5468
0
  }
5469
0
  return false;
5470
0
}
5471
5472
bool cmGeneratorTarget::HasContextDependentSources() const
5473
0
{
5474
0
  return this->SourcesAreContextDependent == Tribool::True;
5475
0
}
5476
5477
bool cmGeneratorTarget::IsExecutableWithExports() const
5478
0
{
5479
0
  return this->Target->IsExecutableWithExports();
5480
0
}
5481
5482
bool cmGeneratorTarget::IsSharedLibraryWithExports() const
5483
0
{
5484
0
  return this->Target->IsSharedLibraryWithExports();
5485
0
}
5486
5487
bool cmGeneratorTarget::HasImportLibrary(std::string const& config) const
5488
0
{
5489
0
  bool generate_Stubs = true;
5490
0
  if (this->GetGlobalGenerator()->IsXcode()) {
5491
    // take care of CMAKE_XCODE_ATTRIBUTE_GENERATE_TEXT_BASED_STUBS variable
5492
    // as well as XCODE_ATTRIBUTE_GENERATE_TEXT_BASED_STUBS property
5493
0
    if (cmValue propGenStubs =
5494
0
          this->GetProperty("XCODE_ATTRIBUTE_GENERATE_TEXT_BASED_STUBS")) {
5495
0
      generate_Stubs = propGenStubs == "YES";
5496
0
    } else if (cmValue varGenStubs = this->Makefile->GetDefinition(
5497
0
                 "CMAKE_XCODE_ATTRIBUTE_GENERATE_TEXT_BASED_STUBS")) {
5498
0
      generate_Stubs = varGenStubs == "YES";
5499
0
    }
5500
0
  }
5501
5502
0
  return (this->IsDLLPlatform() &&
5503
0
          (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
5504
0
           this->IsExecutableWithExports()) &&
5505
          // Assemblies which have only managed code do not have
5506
          // import libraries.
5507
0
          this->GetManagedType(config) != ManagedType::Managed) ||
5508
0
    (this->IsAIX() && this->IsExecutableWithExports()) ||
5509
0
    (this->Makefile->PlatformSupportsAppleTextStubs() &&
5510
0
     this->IsSharedLibraryWithExports() && generate_Stubs);
5511
0
}
5512
5513
bool cmGeneratorTarget::NeedImportLibraryName(std::string const& config) const
5514
0
{
5515
0
  return this->HasImportLibrary(config) ||
5516
    // On DLL platforms we always generate the import library name
5517
    // just in case the sources have export markup.
5518
0
    (this->IsDLLPlatform() &&
5519
0
     (this->GetType() == cmStateEnums::EXECUTABLE ||
5520
0
      this->GetType() == cmStateEnums::MODULE_LIBRARY));
5521
0
}
5522
5523
bool cmGeneratorTarget::GetUseShortObjectNames(
5524
  cmStateEnums::IntermediateDirKind kind) const
5525
0
{
5526
0
  return this->LocalGenerator->UseShortObjectNames(kind);
5527
0
}
5528
5529
cmObjectLocations::UseShortPath
5530
cmGeneratorTarget::GetUseShortObjectNamesForInstall() const
5531
0
{
5532
0
  auto prop = this->Target->GetProperty("INSTALL_OBJECT_NAME_STRATEGY");
5533
0
  if (prop == "SHORT"_s) {
5534
0
    return cmObjectLocations::UseShortPath::Yes;
5535
0
  }
5536
0
  if (prop == "FULL"_s) {
5537
0
    return cmObjectLocations::UseShortPath::No;
5538
0
  }
5539
0
  if (prop.IsSet()) {
5540
0
    this->Makefile->IssueMessage(
5541
0
      MessageType::FATAL_ERROR,
5542
0
      cmStrCat("Property INSTALL_OBJECT_NAME_STRATEGY of target \"",
5543
0
               this->GetName(), "\" set to the unsupported strategy ", prop));
5544
0
  }
5545
0
  return cmObjectLocations::UseShortPath::No;
5546
0
}
5547
5548
std::string cmGeneratorTarget::GetSupportDirectory(
5549
  cmStateEnums::IntermediateDirKind kind) const
5550
0
{
5551
0
  cmLocalGenerator* lg = this->GetLocalGenerator();
5552
0
  auto targetDir = cmStrCat(lg->GetObjectOutputRoot(kind), '/',
5553
0
                            lg->GetTargetDirectory(this, kind));
5554
5555
0
#ifndef CMAKE_BOOTSTRAP
5556
0
  auto& tdr =
5557
0
    this->GetGlobalGenerator()->RegisterTargetDirectory(this, targetDir);
5558
0
  if (tdr.CollidesWith && !tdr.Warned) {
5559
0
    this->Makefile->IssueMessage(
5560
0
      MessageType::WARNING,
5561
0
      cmStrCat("The '", tdr.CollidesWith->GetName(), "' and '",
5562
0
               this->GetName(),
5563
0
               "' targets share an intermediate directory\n    ", targetDir,
5564
0
               "\nwhich may cause problems with the build graph. This project "
5565
0
               "is not compatible with the `SHORT` target intermediate "
5566
0
               "directory strategy. Possible remedies include: moving the "
5567
0
               "target into different directories or renaming a target."));
5568
0
    tdr.Warned = true;
5569
0
  }
5570
0
#endif
5571
5572
0
  return targetDir;
5573
0
}
5574
5575
std::string cmGeneratorTarget::GetCMFSupportDirectory(
5576
  cmStateEnums::IntermediateDirKind kind) const
5577
0
{
5578
0
  cmLocalGenerator* lg = this->GetLocalGenerator();
5579
0
  if (!lg->AlwaysUsesCMFPaths()) {
5580
0
    return cmStrCat(lg->GetCurrentBinaryDirectory(), "/CMakeFiles/",
5581
0
                    lg->GetTargetDirectory(this, kind));
5582
0
  }
5583
0
  return cmStrCat(lg->GetObjectOutputRoot(kind), '/',
5584
0
                  lg->GetTargetDirectory(this, kind));
5585
0
}
5586
5587
bool cmGeneratorTarget::IsLinkable() const
5588
0
{
5589
0
  return (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
5590
0
          this->GetType() == cmStateEnums::SHARED_LIBRARY ||
5591
0
          this->GetType() == cmStateEnums::MODULE_LIBRARY ||
5592
0
          this->GetType() == cmStateEnums::UNKNOWN_LIBRARY ||
5593
0
          this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
5594
0
          this->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
5595
0
          this->IsExecutableWithExports());
5596
0
}
5597
5598
bool cmGeneratorTarget::HasLinkDependencyFile(std::string const& config) const
5599
0
{
5600
0
  if (this->GetType() != cmStateEnums::EXECUTABLE &&
5601
0
      this->GetType() != cmStateEnums::SHARED_LIBRARY &&
5602
0
      this->GetType() != cmStateEnums::MODULE_LIBRARY) {
5603
0
    return false;
5604
0
  }
5605
5606
0
  if (this->Target->GetProperty("LINK_DEPENDS_NO_SHARED").IsOn()) {
5607
    // Do not use the linker dependency file because it includes shared
5608
    // libraries as well
5609
0
    return false;
5610
0
  }
5611
5612
0
  std::string const depsUseLinker{ "CMAKE_LINK_DEPENDS_USE_LINKER" };
5613
0
  auto linkLanguage = this->GetLinkerLanguage(config);
5614
0
  std::string const langDepsUseLinker{ cmStrCat("CMAKE_", linkLanguage,
5615
0
                                                "_LINK_DEPENDS_USE_LINKER") };
5616
5617
0
  return (!this->Makefile->IsDefinitionSet(depsUseLinker) ||
5618
0
          this->Makefile->IsOn(depsUseLinker)) &&
5619
0
    this->Makefile->IsOn(langDepsUseLinker);
5620
0
}
5621
5622
bool cmGeneratorTarget::IsFrameworkOnApple() const
5623
0
{
5624
0
  return this->Target->IsFrameworkOnApple();
5625
0
}
5626
5627
bool cmGeneratorTarget::IsArchivedAIXSharedLibrary() const
5628
0
{
5629
0
  return this->Target->IsArchivedAIXSharedLibrary();
5630
0
}
5631
5632
bool cmGeneratorTarget::IsImportedFrameworkFolderOnApple(
5633
  std::string const& config) const
5634
0
{
5635
0
  if (this->IsApple() && this->IsImported() &&
5636
0
      (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
5637
0
       this->GetType() == cmStateEnums::SHARED_LIBRARY ||
5638
0
       this->GetType() == cmStateEnums::UNKNOWN_LIBRARY)) {
5639
0
    std::string cfg = config;
5640
0
    if (cfg.empty() && this->GetGlobalGenerator()->IsXcode()) {
5641
      // FIXME(#25515): Remove the need for this workaround.
5642
      // The Xcode generator queries include directories without any
5643
      // specific configuration.  Pick one in case this target does
5644
      // not set either IMPORTED_LOCATION or IMPORTED_CONFIGURATIONS.
5645
0
      cfg =
5646
0
        this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig)[0];
5647
0
    }
5648
0
    return cmSystemTools::IsPathToFramework(this->GetLocation(cfg));
5649
0
  }
5650
5651
0
  return false;
5652
0
}
5653
5654
bool cmGeneratorTarget::IsAppBundleOnApple() const
5655
0
{
5656
0
  return this->Target->IsAppBundleOnApple();
5657
0
}
5658
5659
bool cmGeneratorTarget::IsXCTestOnApple() const
5660
0
{
5661
0
  return (this->IsCFBundleOnApple() && this->GetPropertyAsBool("XCTEST"));
5662
0
}
5663
5664
bool cmGeneratorTarget::IsCFBundleOnApple() const
5665
0
{
5666
0
  return (this->GetType() == cmStateEnums::MODULE_LIBRARY && this->IsApple() &&
5667
0
          this->GetPropertyAsBool("BUNDLE"));
5668
0
}
5669
5670
cmGeneratorTarget::ManagedType cmGeneratorTarget::CheckManagedType(
5671
  std::string const& propval) const
5672
0
{
5673
  // The type of the managed assembly (mixed unmanaged C++ and C++/CLI,
5674
  // or only C++/CLI) does only depend on whether the property is an empty
5675
  // string or contains any value at all. In Visual Studio generators
5676
  // this propval is prepended with /clr[:] which results in:
5677
  //
5678
  // 1. propval does not exist: no /clr flag, unmanaged target, has import
5679
  //                            lib
5680
  // 2. empty propval:          add /clr as flag, mixed unmanaged/managed
5681
  //                            target, has import lib
5682
  // 3. netcore propval:        add /clr:netcore as flag, mixed
5683
  //                            unmanaged/managed target, has import lib.
5684
  // 4. any value (safe,pure):  add /clr:[propval] as flag, target with
5685
  //                            managed code only, no import lib
5686
0
  if (propval.empty() || propval == "netcore") {
5687
0
    return ManagedType::Mixed;
5688
0
  }
5689
0
  return ManagedType::Managed;
5690
0
}
5691
5692
cmGeneratorTarget::ManagedType cmGeneratorTarget::GetManagedType(
5693
  std::string const& config) const
5694
0
{
5695
  // Only libraries and executables can be managed targets.
5696
0
  if (this->GetType() > cmStateEnums::SHARED_LIBRARY) {
5697
0
    return ManagedType::Undefined;
5698
0
  }
5699
5700
0
  if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
5701
0
    return ManagedType::Native;
5702
0
  }
5703
5704
  // Check imported target.
5705
0
  if (this->IsImported()) {
5706
0
    if (cmGeneratorTarget::ImportInfo const* info =
5707
0
          this->GetImportInfo(config)) {
5708
0
      return info->Managed;
5709
0
    }
5710
0
    return ManagedType::Undefined;
5711
0
  }
5712
5713
  // Check for explicitly set clr target property.
5714
0
  if (cmValue clr = this->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
5715
0
    return this->CheckManagedType(*clr);
5716
0
  }
5717
5718
  // C# targets are always managed. This language specific check
5719
  // is added to avoid that the COMMON_LANGUAGE_RUNTIME target property
5720
  // has to be set manually for C# targets.
5721
0
  return this->IsCSharpOnly() ? ManagedType::Managed : ManagedType::Native;
5722
0
}
5723
5724
bool cmGeneratorTarget::AddHeaderSetVerification()
5725
0
{
5726
0
  for (bool const isInterface : { false, true }) {
5727
0
    if (!this->GetPropertyAsBool(isInterface ? "VERIFY_INTERFACE_HEADER_SETS"
5728
0
                                             : "VERIFY_PRIVATE_HEADER_SETS")) {
5729
0
      continue;
5730
0
    }
5731
5732
0
    if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
5733
0
        this->GetType() != cmStateEnums::SHARED_LIBRARY &&
5734
0
        (this->GetType() != cmStateEnums::MODULE_LIBRARY || isInterface) &&
5735
0
        this->GetType() != cmStateEnums::UNKNOWN_LIBRARY &&
5736
0
        this->GetType() != cmStateEnums::OBJECT_LIBRARY &&
5737
0
        this->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
5738
0
        this->GetType() != cmStateEnums::EXECUTABLE) {
5739
0
      continue;
5740
0
    }
5741
5742
0
    char const* headerSetsProperty = isInterface
5743
0
      ? "INTERFACE_HEADER_SETS_TO_VERIFY"
5744
0
      : "PRIVATE_HEADER_SETS_TO_VERIFY";
5745
5746
0
    auto verifyValue = this->GetProperty(headerSetsProperty);
5747
0
    bool const all = verifyValue.IsEmpty();
5748
0
    std::set<std::string> verifySet;
5749
0
    if (!all) {
5750
0
      cmList verifyList{ verifyValue };
5751
0
      verifySet.insert(verifyList.begin(), verifyList.end());
5752
0
    }
5753
5754
0
    cmTarget* verifyTarget = nullptr;
5755
0
    std::string const verifyTargetName =
5756
0
      cmStrCat(this->GetName(),
5757
0
               isInterface ? "_verify_interface_header_sets"
5758
0
                           : "_verify_private_header_sets");
5759
5760
0
    char const* allVerifyTargetName = isInterface
5761
0
      ? "all_verify_interface_header_sets"
5762
0
      : "all_verify_private_header_sets";
5763
0
    cmTarget* allVerifyTarget =
5764
0
      this->GlobalGenerator->GetMakefiles().front()->FindTargetToUse(
5765
0
        allVerifyTargetName, { cmStateEnums::TargetDomain::NATIVE });
5766
5767
0
    auto fileSetEntries = isInterface
5768
0
      ? this->Target->GetInterfaceHeaderSetsEntries()
5769
0
      : this->Target->GetHeaderSetsEntries();
5770
5771
0
    std::set<cmFileSet*> fileSets;
5772
0
    for (auto const& entry : fileSetEntries) {
5773
0
      for (auto const& name : cmList{ entry.Value }) {
5774
0
        if (all || verifySet.count(name)) {
5775
0
          fileSets.insert(this->Target->GetFileSet(name));
5776
0
          verifySet.erase(name);
5777
0
        }
5778
0
      }
5779
0
    }
5780
5781
0
    if (isInterface) {
5782
0
      cmPolicies::PolicyStatus const cmp0209 = this->GetPolicyStatusCMP0209();
5783
0
      if (cmp0209 != cmPolicies::NEW &&
5784
0
          this->GetType() == cmStateEnums::EXECUTABLE &&
5785
0
          !this->GetPropertyAsBool("ENABLE_EXPORTS")) {
5786
0
        if (cmp0209 == cmPolicies::WARN && !fileSets.empty()) {
5787
0
          this->Makefile->IssueMessage(
5788
0
            MessageType::AUTHOR_WARNING,
5789
0
            cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0209),
5790
0
                     "\n"
5791
0
                     "Executable target \"",
5792
0
                     this->GetName(),
5793
0
                     "\" has interface header file sets, but it does not "
5794
0
                     "enable exports. Those headers would be verified under "
5795
0
                     "CMP0209 NEW behavior.\n"));
5796
0
        }
5797
0
        continue;
5798
0
      }
5799
0
    }
5800
5801
0
    if (!verifySet.empty()) {
5802
0
      this->Makefile->IssueMessage(
5803
0
        MessageType::FATAL_ERROR,
5804
0
        cmStrCat("Property ", headerSetsProperty, " of target \"",
5805
0
                 this->GetName(),
5806
0
                 "\" contained the following header sets that are nonexistent "
5807
0
                 "or not ",
5808
0
                 isInterface ? "INTERFACE" : "PRIVATE", ":\n  ",
5809
0
                 cmJoin(verifySet, "\n  ")));
5810
0
      return false;
5811
0
    }
5812
5813
0
    cm::optional<std::set<std::string>> languages;
5814
0
    for (auto* fileSet : fileSets) {
5815
0
      auto dirCges = fileSet->CompileDirectoryEntries();
5816
0
      auto fileCges = fileSet->CompileFileEntries();
5817
5818
0
      static auto const contextSensitive =
5819
0
        [](std::unique_ptr<cmCompiledGeneratorExpression> const& cge) {
5820
0
          return cge->GetHadContextSensitiveCondition();
5821
0
        };
5822
0
      bool dirCgesContextSensitive = false;
5823
0
      bool fileCgesContextSensitive = false;
5824
5825
0
      std::vector<std::string> dirs;
5826
0
      std::map<std::string, std::vector<std::string>> filesPerDir;
5827
0
      bool first = true;
5828
0
      for (auto const& config : this->Makefile->GetGeneratorConfigs(
5829
0
             cmMakefile::GeneratorConfigQuery::IncludeEmptyConfig)) {
5830
0
        cm::GenEx::Context context(this->LocalGenerator, config);
5831
0
        if (first || dirCgesContextSensitive) {
5832
0
          dirs = fileSet->EvaluateDirectoryEntries(dirCges, context, this);
5833
0
          dirCgesContextSensitive =
5834
0
            std::any_of(dirCges.begin(), dirCges.end(), contextSensitive);
5835
0
        }
5836
0
        if (first || fileCgesContextSensitive) {
5837
0
          filesPerDir.clear();
5838
0
          for (auto const& fileCge : fileCges) {
5839
0
            fileSet->EvaluateFileEntry(dirs, filesPerDir, fileCge, context,
5840
0
                                       this);
5841
0
            if (fileCge->GetHadContextSensitiveCondition()) {
5842
0
              fileCgesContextSensitive = true;
5843
0
            }
5844
0
          }
5845
0
        }
5846
5847
0
        for (auto const& files : filesPerDir) {
5848
0
          for (auto const& file : files.second) {
5849
0
            std::string filename = this->GenerateHeaderSetVerificationFile(
5850
0
              *this->Makefile->GetOrCreateSource(file), files.first,
5851
0
              verifyTargetName, languages);
5852
0
            if (filename.empty()) {
5853
0
              continue;
5854
0
            }
5855
5856
0
            if (!verifyTarget) {
5857
0
              {
5858
0
                cmMakefile::PolicyPushPop polScope(this->Makefile);
5859
0
                this->Makefile->SetPolicy(cmPolicies::CMP0119,
5860
0
                                          cmPolicies::NEW);
5861
0
                verifyTarget = this->Makefile->AddLibrary(
5862
0
                  verifyTargetName, cmStateEnums::OBJECT_LIBRARY, {}, true);
5863
0
              }
5864
5865
0
              if (isInterface) {
5866
                // Link to the original target so that we pick up its
5867
                // interface compile options just like a consumer would.
5868
                // This also ensures any generated headers in the original
5869
                // target will be created.
5870
0
                verifyTarget->AddLinkLibrary(
5871
0
                  *this->Makefile, this->GetName(),
5872
0
                  cmTargetLinkLibraryType::GENERAL_LibraryType);
5873
0
              } else {
5874
                // For private file sets, we need to simulate compiling the
5875
                // same way as the original target. That includes linking to
5876
                // the same things so we pick up the same transitive
5877
                // properties. For the <LANG>_... properties, we don't care if
5878
                // we set them for languages this target won't eventually use.
5879
                // The verify header sets feature currently only supports the
5880
                // C and C++ languages, so we just always set those here for
5881
                // simplicity rather than working out all languages the target
5882
                // has to compile for.
5883
0
                static std::vector<std::string> propertiesToCopy = {
5884
0
                  "COMPILE_DEFINITIONS", "COMPILE_FEATURES",
5885
0
                  "COMPILE_FLAGS",       "COMPILE_OPTIONS",
5886
0
                  "DEFINE_SYMBOL",       "INCLUDE_DIRECTORIES",
5887
0
                  "LINK_LIBRARIES",      "C_STANDARD",
5888
0
                  "C_STANDARD_REQUIRED", "C_EXTENSIONS",
5889
0
                  "CXX_STANDARD",        "CXX_STANDARD_REQUIRED",
5890
0
                  "CXX_EXTENSIONS"
5891
0
                };
5892
0
                for (std::string const& prop : propertiesToCopy) {
5893
0
                  cmValue propValue = this->Target->GetProperty(prop);
5894
0
                  if (propValue.IsSet()) {
5895
0
                    verifyTarget->SetProperty(prop, propValue);
5896
0
                  }
5897
0
                }
5898
                // The original target might have generated headers. Since
5899
                // we only link to the original target for compilation,
5900
                // there's nothing to force such generation to happen yet.
5901
                // Our verify target must depend on the original target to
5902
                // ensure such generated files will be created.
5903
0
                verifyTarget->AddUtility(this->GetName(), false,
5904
0
                                         this->Makefile);
5905
0
                verifyTarget->AddCodegenDependency(this->GetName());
5906
0
              }
5907
5908
0
              verifyTarget->SetProperty("AUTOMOC", "OFF");
5909
0
              verifyTarget->SetProperty("AUTORCC", "OFF");
5910
0
              verifyTarget->SetProperty("AUTOUIC", "OFF");
5911
0
              verifyTarget->SetProperty("DISABLE_PRECOMPILE_HEADERS", "ON");
5912
0
              verifyTarget->SetProperty("UNITY_BUILD", "OFF");
5913
0
              verifyTarget->SetProperty("CXX_SCAN_FOR_MODULES", "OFF");
5914
5915
0
              if (isInterface) {
5916
0
                verifyTarget->FinalizeTargetConfiguration(
5917
0
                  this->Makefile->GetCompileDefinitionsEntries());
5918
0
              } else {
5919
                // Private verification only needs to add the directory scope
5920
                // definitions here
5921
0
                for (auto const& def :
5922
0
                     this->Makefile->GetCompileDefinitionsEntries()) {
5923
0
                  verifyTarget->InsertCompileDefinition(def);
5924
0
                }
5925
0
              }
5926
5927
0
              if (!allVerifyTarget) {
5928
0
                allVerifyTarget =
5929
0
                  this->GlobalGenerator->GetMakefiles()
5930
0
                    .front()
5931
0
                    ->AddNewUtilityTarget(allVerifyTargetName, true);
5932
0
              }
5933
5934
0
              allVerifyTarget->AddUtility(verifyTargetName, false);
5935
0
            }
5936
5937
0
            if (fileCgesContextSensitive) {
5938
0
              filename = cmStrCat("$<$<CONFIG:", config, ">:", filename, '>');
5939
0
            }
5940
0
            verifyTarget->AddSource(filename);
5941
0
          }
5942
0
        }
5943
5944
0
        if (!dirCgesContextSensitive && !fileCgesContextSensitive) {
5945
0
          break;
5946
0
        }
5947
0
        first = false;
5948
0
      }
5949
0
    }
5950
5951
0
    if (verifyTarget) {
5952
0
      this->LocalGenerator->AddGeneratorTarget(
5953
0
        cm::make_unique<cmGeneratorTarget>(verifyTarget,
5954
0
                                           this->LocalGenerator));
5955
0
    }
5956
0
  }
5957
5958
0
  return true;
5959
0
}
5960
5961
std::string cmGeneratorTarget::GenerateHeaderSetVerificationFile(
5962
  cmSourceFile& source, std::string const& dir,
5963
  std::string const& verifyTargetName,
5964
  cm::optional<std::set<std::string>>& languages) const
5965
0
{
5966
0
  std::string extension;
5967
0
  std::string language = source.GetOrDetermineLanguage();
5968
5969
0
  if (source.GetPropertyAsBool("SKIP_LINTING")) {
5970
0
    return std::string{};
5971
0
  }
5972
5973
0
  if (language.empty()) {
5974
0
    if (!languages) {
5975
0
      languages.emplace();
5976
0
      for (auto const& tgtSource : this->GetAllConfigSources()) {
5977
0
        auto const& tgtSourceLanguage =
5978
0
          tgtSource.Source->GetOrDetermineLanguage();
5979
0
        if (tgtSourceLanguage == "CXX") {
5980
0
          languages->insert("CXX");
5981
0
          break; // C++ overrides everything else, so we don't need to keep
5982
                 // checking.
5983
0
        }
5984
0
        if (tgtSourceLanguage == "C") {
5985
0
          languages->insert("C");
5986
0
        }
5987
0
      }
5988
5989
0
      if (languages->empty()) {
5990
0
        std::vector<std::string> languagesVector;
5991
0
        this->GlobalGenerator->GetEnabledLanguages(languagesVector);
5992
0
        languages->insert(languagesVector.begin(), languagesVector.end());
5993
0
      }
5994
0
    }
5995
5996
0
    if (languages->count("CXX")) {
5997
0
      language = "CXX";
5998
0
    } else if (languages->count("C")) {
5999
0
      language = "C";
6000
0
    }
6001
0
  }
6002
6003
0
  if (language == "C") {
6004
0
    extension = ".c";
6005
0
  } else if (language == "CXX") {
6006
0
    extension = ".cxx";
6007
0
  } else {
6008
0
    return "";
6009
0
  }
6010
6011
0
  std::string headerFilename = dir;
6012
0
  if (!headerFilename.empty()) {
6013
0
    headerFilename += '/';
6014
0
  }
6015
0
  headerFilename += source.GetLocation().GetName();
6016
6017
0
  auto filename =
6018
0
    cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(), '/',
6019
0
             verifyTargetName, '/', headerFilename, extension);
6020
0
  auto* verificationSource = this->Makefile->GetOrCreateSource(filename);
6021
0
  source.SetSpecialSourceType(
6022
0
    cmSourceFile::SpecialSourceType::HeaderSetVerificationSource);
6023
0
  verificationSource->SetProperty("LANGUAGE", language);
6024
6025
0
  cmSystemTools::MakeDirectory(cmSystemTools::GetFilenamePath(filename));
6026
6027
0
  cmGeneratedFileStream fout(filename);
6028
0
  fout.SetCopyIfDifferent(true);
6029
  // The IWYU "associated" pragma tells include-what-you-use to
6030
  // consider the headerFile as part of the entire language
6031
  // unit within include-what-you-use and as a result allows
6032
  // one to get IWYU advice for headers.
6033
  // Also suppress clang-tidy include checks in generated code.
6034
0
  fout
6035
0
    << "/* NOLINTNEXTLINE(misc-header-include-cycle,misc-include-cleaner) */\n"
6036
0
    << "#include <" << headerFilename << "> /* IWYU pragma: associated */\n";
6037
0
  fout.close();
6038
6039
0
  return filename;
6040
0
}
6041
6042
std::string cmGeneratorTarget::GetImportedXcFrameworkPath(
6043
  std::string const& config) const
6044
0
{
6045
0
  if (!(this->IsApple() && this->IsImported() &&
6046
0
        (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
6047
0
         this->GetType() == cmStateEnums::STATIC_LIBRARY ||
6048
0
         this->GetType() == cmStateEnums::UNKNOWN_LIBRARY))) {
6049
0
    return {};
6050
0
  }
6051
6052
0
  std::string desiredConfig = config;
6053
0
  if (config.empty()) {
6054
0
    desiredConfig = "NOCONFIG";
6055
0
  }
6056
6057
0
  std::string result;
6058
6059
0
  cmValue loc = nullptr;
6060
0
  cmValue imp = nullptr;
6061
0
  std::string suffix;
6062
6063
0
  if (this->Target->GetMappedConfig(desiredConfig, loc, imp, suffix)) {
6064
0
    if (loc) {
6065
0
      result = *loc;
6066
0
    } else {
6067
0
      std::string impProp = cmStrCat("IMPORTED_LOCATION", suffix);
6068
0
      if (cmValue configLocation = this->GetProperty(impProp)) {
6069
0
        result = *configLocation;
6070
0
      } else if (cmValue location = this->GetProperty("IMPORTED_LOCATION")) {
6071
0
        result = *location;
6072
0
      }
6073
0
    }
6074
6075
0
    if (cmSystemTools::IsPathToXcFramework(result)) {
6076
0
      return result;
6077
0
    }
6078
0
  }
6079
6080
0
  return {};
6081
0
}
6082
6083
bool cmGeneratorTarget::HaveFortranSources(std::string const& config) const
6084
0
{
6085
0
  auto sources = this->GetSourceFiles(config);
6086
0
  bool const have_direct = std::any_of(
6087
0
    sources.begin(), sources.end(), [](BT<cmSourceFile*> const& sf) -> bool {
6088
0
      return sf.Value->GetLanguage() == "Fortran"_s;
6089
0
    });
6090
0
  bool have_via_target_objects = false;
6091
0
  if (!have_direct) {
6092
0
    auto const sourceObjectLibraries = this->GetSourceObjectLibraries(config);
6093
0
    have_via_target_objects =
6094
0
      std::any_of(sourceObjectLibraries.begin(), sourceObjectLibraries.end(),
6095
0
                  [&config](cmGeneratorTarget const* tgt) -> bool {
6096
0
                    return tgt->HaveFortranSources(config);
6097
0
                  });
6098
0
  }
6099
0
  return have_direct || have_via_target_objects;
6100
0
}
6101
6102
bool cmGeneratorTarget::HaveFortranSources() const
6103
0
{
6104
0
  auto sources = this->GetAllConfigSources();
6105
0
  bool const have_direct = std::any_of(
6106
0
    sources.begin(), sources.end(), [](AllConfigSource const& sf) -> bool {
6107
0
      return sf.Source->GetLanguage() == "Fortran"_s;
6108
0
    });
6109
0
  bool have_via_target_objects = false;
6110
0
  if (!have_direct) {
6111
0
    std::vector<std::string> configs =
6112
0
      this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
6113
0
    for (auto const& config : configs) {
6114
0
      auto const sourceObjectLibraries =
6115
0
        this->GetSourceObjectLibraries(config);
6116
0
      have_via_target_objects =
6117
0
        std::any_of(sourceObjectLibraries.begin(), sourceObjectLibraries.end(),
6118
0
                    [&config](cmGeneratorTarget const* tgt) -> bool {
6119
0
                      return tgt->HaveFortranSources(config);
6120
0
                    });
6121
0
      if (have_via_target_objects) {
6122
0
        break;
6123
0
      }
6124
0
    }
6125
0
  }
6126
0
  return have_direct || have_via_target_objects;
6127
0
}
6128
6129
bool cmGeneratorTarget::HaveCxx20ModuleSources(std::string* errorMessage) const
6130
0
{
6131
0
  auto const& fs_names = this->Target->GetAllFileSetNames();
6132
0
  return std::any_of(
6133
0
    fs_names.begin(), fs_names.end(),
6134
0
    [this, errorMessage](std::string const& name) -> bool {
6135
0
      auto const* file_set = this->Target->GetFileSet(name);
6136
0
      if (!file_set) {
6137
0
        auto message = cmStrCat("Target \"", this->Target->GetName(),
6138
0
                                "\" is tracked to have file set \"", name,
6139
0
                                "\", but it was not found.");
6140
0
        if (errorMessage) {
6141
0
          *errorMessage = std::move(message);
6142
0
        } else {
6143
0
          this->Makefile->IssueMessage(MessageType::INTERNAL_ERROR, message);
6144
0
        }
6145
0
        return false;
6146
0
      }
6147
6148
0
      auto const& fs_type = file_set->GetType();
6149
0
      return fs_type == "CXX_MODULES"_s;
6150
0
    });
6151
0
}
6152
6153
cmGeneratorTarget::Cxx20SupportLevel cmGeneratorTarget::HaveCxxModuleSupport(
6154
  std::string const& config) const
6155
0
{
6156
0
  auto const* state = this->Makefile->GetState();
6157
0
  if (!state->GetLanguageEnabled("CXX")) {
6158
0
    return Cxx20SupportLevel::MissingCxx;
6159
0
  }
6160
6161
0
  cmValue standardDefault =
6162
0
    this->Makefile->GetDefinition("CMAKE_CXX_STANDARD_DEFAULT");
6163
0
  if (!standardDefault || standardDefault->empty()) {
6164
    // We do not know any meaningful C++ standard levels for this compiler.
6165
0
    return Cxx20SupportLevel::NoCxx20;
6166
0
  }
6167
6168
0
  cmStandardLevelResolver standardResolver(this->Makefile);
6169
0
  cmStandardLevel const cxxStd20 =
6170
0
    *standardResolver.LanguageStandardLevel("CXX", "20");
6171
0
  cm::optional<cmStandardLevel> explicitLevel =
6172
0
    this->GetExplicitStandardLevel("CXX", config);
6173
0
  if (!explicitLevel || *explicitLevel < cxxStd20) {
6174
0
    return Cxx20SupportLevel::NoCxx20;
6175
0
  }
6176
6177
0
  cmValue scandepRule =
6178
0
    this->Makefile->GetDefinition("CMAKE_CXX_SCANDEP_SOURCE");
6179
0
  if (!scandepRule) {
6180
0
    return Cxx20SupportLevel::MissingRule;
6181
0
  }
6182
0
  return Cxx20SupportLevel::Supported;
6183
0
}
6184
6185
void cmGeneratorTarget::CheckCxxModuleStatus(std::string const& config) const
6186
0
{
6187
0
  bool haveScannableSources = false;
6188
6189
  // Check for `CXX_MODULE*` file sets and a lack of support.
6190
0
  if (this->HaveCxx20ModuleSources()) {
6191
0
    haveScannableSources = true;
6192
0
  }
6193
6194
0
  if (!haveScannableSources) {
6195
    // Check to see if there are regular sources that have requested scanning.
6196
0
    auto sources = this->GetSourceFiles(config);
6197
0
    for (auto const& source : sources) {
6198
0
      auto const* sf = source.Value;
6199
0
      auto const& lang = sf->GetLanguage();
6200
0
      if (lang != "CXX"_s) {
6201
0
        continue;
6202
0
      }
6203
      // Ignore sources which do not need dyndep.
6204
0
      if (this->NeedDyndepForSource(lang, config, sf)) {
6205
0
        haveScannableSources = true;
6206
0
      }
6207
0
    }
6208
0
  }
6209
6210
  // If there isn't anything scannable, ignore it.
6211
0
  if (!haveScannableSources) {
6212
0
    return;
6213
0
  }
6214
6215
  // If the generator doesn't support modules at all, error that we have
6216
  // sources that require the support.
6217
0
  if (!this->GetGlobalGenerator()->CheckCxxModuleSupport(
6218
0
        cmGlobalGenerator::CxxModuleSupportQuery::Expected)) {
6219
0
    this->Makefile->IssueMessage(
6220
0
      MessageType::FATAL_ERROR,
6221
0
      cmStrCat("The target named \"", this->GetName(),
6222
0
               "\" has C++ sources that may use modules, but modules are not "
6223
0
               "supported by this generator:\n  ",
6224
0
               this->GetGlobalGenerator()->GetName(),
6225
0
               "\n"
6226
0
               "Modules are supported only by Ninja, Ninja Multi-Config, "
6227
0
               "and Visual Studio generators for VS 17.4 and newer.  "
6228
0
               "See the cmake-cxxmodules(7) manual for details.  "
6229
0
               "Use the CMAKE_CXX_SCAN_FOR_MODULES variable to enable or "
6230
0
               "disable scanning."));
6231
0
    return;
6232
0
  }
6233
6234
0
  switch (this->HaveCxxModuleSupport(config)) {
6235
0
    case cmGeneratorTarget::Cxx20SupportLevel::MissingCxx:
6236
0
      this->Makefile->IssueMessage(
6237
0
        MessageType::FATAL_ERROR,
6238
0
        cmStrCat("The target named \"", this->GetName(),
6239
0
                 "\" has C++ sources that use modules, but the \"CXX\" "
6240
0
                 "language has not been enabled."));
6241
0
      break;
6242
0
    case cmGeneratorTarget::Cxx20SupportLevel::NoCxx20: {
6243
0
      cmStandardLevelResolver standardResolver(this->Makefile);
6244
0
      auto effStandard =
6245
0
        standardResolver.GetEffectiveStandard(this, "CXX", config);
6246
0
      if (effStandard.empty()) {
6247
0
        effStandard = "; no C++ standard found";
6248
0
      } else {
6249
0
        effStandard = cmStrCat("; found \"cxx_std_", effStandard, '"');
6250
0
      }
6251
0
      this->Makefile->IssueMessage(
6252
0
        MessageType::FATAL_ERROR,
6253
0
        cmStrCat(
6254
0
          "The target named \"", this->GetName(),
6255
0
          "\" has C++ sources that use modules, but does not include "
6256
0
          "\"cxx_std_20\" (or newer) among its `target_compile_features`",
6257
0
          effStandard, '.'));
6258
0
    } break;
6259
0
    case cmGeneratorTarget::Cxx20SupportLevel::MissingRule: {
6260
0
      this->Makefile->IssueMessage(
6261
0
        MessageType::FATAL_ERROR,
6262
0
        cmStrCat("The target named \"", this->GetName(),
6263
0
                 "\" has C++ sources that may use modules, but the compiler "
6264
0
                 "does not provide a way to discover the import graph "
6265
0
                 "dependencies.  See the cmake-cxxmodules(7) manual for "
6266
0
                 "details.  Use the CMAKE_CXX_SCAN_FOR_MODULES variable to "
6267
0
                 "enable or disable scanning."));
6268
0
    } break;
6269
0
    case cmGeneratorTarget::Cxx20SupportLevel::Supported:
6270
      // All is well.
6271
0
      break;
6272
0
  }
6273
0
}
6274
6275
bool cmGeneratorTarget::NeedCxxModuleSupport(std::string const& lang,
6276
                                             std::string const& config) const
6277
0
{
6278
0
  if (lang != "CXX"_s) {
6279
0
    return false;
6280
0
  }
6281
0
  return this->HaveCxxModuleSupport(config) == Cxx20SupportLevel::Supported &&
6282
0
    this->GetGlobalGenerator()->CheckCxxModuleSupport(
6283
0
      cmGlobalGenerator::CxxModuleSupportQuery::Inspect);
6284
0
}
6285
6286
bool cmGeneratorTarget::NeedDyndep(std::string const& lang,
6287
                                   std::string const& config) const
6288
0
{
6289
0
  return lang == "Fortran"_s || this->NeedCxxModuleSupport(lang, config);
6290
0
}
6291
6292
cmFileSet const* cmGeneratorTarget::GetFileSetForSource(
6293
  std::string const& config, cmSourceFile const* sf) const
6294
0
{
6295
0
  this->BuildFileSetInfoCache(config);
6296
6297
0
  auto const& path = sf->GetFullPath();
6298
0
  auto const& per_config = this->Configs[config];
6299
6300
0
  auto const fsit = per_config.FileSetCache.find(path);
6301
0
  if (fsit == per_config.FileSetCache.end()) {
6302
0
    return nullptr;
6303
0
  }
6304
0
  return fsit->second;
6305
0
}
6306
6307
bool cmGeneratorTarget::NeedDyndepForSource(std::string const& lang,
6308
                                            std::string const& config,
6309
                                            cmSourceFile const* sf) const
6310
0
{
6311
  // Fortran always needs to be scanned.
6312
0
  if (lang == "Fortran"_s) {
6313
0
    return true;
6314
0
  }
6315
  // Only C++ code needs scanned otherwise.
6316
0
  if (lang != "CXX"_s) {
6317
0
    return false;
6318
0
  }
6319
6320
  // Any file in `CXX_MODULES` file sets need scanned (it being `CXX` is
6321
  // enforced elsewhere).
6322
0
  auto const* fs = this->GetFileSetForSource(config, sf);
6323
0
  if (fs && fs->GetType() == "CXX_MODULES"_s) {
6324
0
    return true;
6325
0
  }
6326
6327
0
  auto targetDyndep = this->NeedCxxDyndep(config);
6328
0
  if (targetDyndep == CxxModuleSupport::Unavailable) {
6329
0
    return false;
6330
0
  }
6331
0
  auto const sfProp = sf->GetProperty("CXX_SCAN_FOR_MODULES");
6332
0
  if (sfProp.IsSet()) {
6333
0
    return sfProp.IsOn();
6334
0
  }
6335
0
  return targetDyndep == CxxModuleSupport::Enabled;
6336
0
}
6337
6338
cmGeneratorTarget::CxxModuleSupport cmGeneratorTarget::NeedCxxDyndep(
6339
  std::string const& config) const
6340
0
{
6341
0
  bool haveRule = false;
6342
0
  switch (this->HaveCxxModuleSupport(config)) {
6343
0
    case Cxx20SupportLevel::MissingCxx:
6344
0
    case Cxx20SupportLevel::NoCxx20:
6345
0
      return CxxModuleSupport::Unavailable;
6346
0
    case Cxx20SupportLevel::MissingRule:
6347
0
      break;
6348
0
    case Cxx20SupportLevel::Supported:
6349
0
      haveRule = true;
6350
0
      break;
6351
0
  }
6352
0
  bool haveGeneratorSupport =
6353
0
    this->GetGlobalGenerator()->CheckCxxModuleSupport(
6354
0
      cmGlobalGenerator::CxxModuleSupportQuery::Inspect);
6355
0
  auto const tgtProp = this->GetProperty("CXX_SCAN_FOR_MODULES");
6356
0
  if (tgtProp.IsSet()) {
6357
0
    return tgtProp.IsOn() ? CxxModuleSupport::Enabled
6358
0
                          : CxxModuleSupport::Disabled;
6359
0
  }
6360
6361
0
  CxxModuleSupport policyAnswer = CxxModuleSupport::Unavailable;
6362
0
  switch (this->GetPolicyStatusCMP0155()) {
6363
0
    case cmPolicies::WARN:
6364
0
    case cmPolicies::OLD:
6365
      // The OLD behavior is to not scan the source.
6366
0
      policyAnswer = CxxModuleSupport::Disabled;
6367
0
      break;
6368
0
    case cmPolicies::NEW:
6369
      // The NEW behavior is to scan the source if the compiler supports
6370
      // scanning and the generator supports it.
6371
0
      if (haveRule && haveGeneratorSupport) {
6372
0
        policyAnswer = CxxModuleSupport::Enabled;
6373
0
      } else {
6374
0
        policyAnswer = CxxModuleSupport::Disabled;
6375
0
      }
6376
0
      break;
6377
0
  }
6378
0
  return policyAnswer;
6379
0
}
6380
6381
std::string cmGeneratorTarget::BuildDatabasePath(
6382
  std::string const& lang, std::string const& config) const
6383
0
{
6384
  // Check to see if the target wants it.
6385
0
  if (!this->GetPropertyAsBool("EXPORT_BUILD_DATABASE")) {
6386
0
    return {};
6387
0
  }
6388
0
  if (!cmExperimental::HasSupportEnabled(
6389
0
        *this->Makefile, cmExperimental::Feature::ExportBuildDatabase)) {
6390
0
    return {};
6391
0
  }
6392
  // Check to see if the generator supports it.
6393
0
  if (!this->GetGlobalGenerator()->SupportsBuildDatabase()) {
6394
0
    return {};
6395
0
  }
6396
6397
0
  if (this->GetGlobalGenerator()->IsMultiConfig()) {
6398
0
    return cmStrCat(this->GetSupportDirectory(), '/', config, '/', lang,
6399
0
                    "_build_database.json");
6400
0
  }
6401
6402
0
  return cmStrCat(this->GetSupportDirectory(), '/', lang,
6403
0
                  "_build_database.json");
6404
0
}
6405
6406
void cmGeneratorTarget::BuildFileSetInfoCache(std::string const& config) const
6407
0
{
6408
0
  auto& per_config = this->Configs[config];
6409
6410
0
  if (per_config.BuiltFileSetCache) {
6411
0
    return;
6412
0
  }
6413
6414
0
  auto const* tgt = this->Target;
6415
6416
0
  for (auto const& name : tgt->GetAllFileSetNames()) {
6417
0
    auto const* file_set = tgt->GetFileSet(name);
6418
0
    if (!file_set) {
6419
0
      tgt->GetMakefile()->IssueMessage(
6420
0
        MessageType::INTERNAL_ERROR,
6421
0
        cmStrCat("Target \"", tgt->GetName(),
6422
0
                 "\" is tracked to have file set \"", name,
6423
0
                 "\", but it was not found."));
6424
0
      continue;
6425
0
    }
6426
6427
0
    cm::GenEx::Context context(this->LocalGenerator, config);
6428
6429
0
    auto fileEntries = file_set->CompileFileEntries();
6430
0
    auto directoryEntries = file_set->CompileDirectoryEntries();
6431
0
    auto directories =
6432
0
      file_set->EvaluateDirectoryEntries(directoryEntries, context, this);
6433
6434
0
    std::map<std::string, std::vector<std::string>> files;
6435
0
    for (auto const& entry : fileEntries) {
6436
0
      file_set->EvaluateFileEntry(directories, files, entry, context, this);
6437
0
    }
6438
6439
0
    for (auto const& it : files) {
6440
0
      for (auto const& filename : it.second) {
6441
0
        auto collapsedFile = cmSystemTools::CollapseFullPath(filename);
6442
0
        per_config.FileSetCache[collapsedFile] = file_set;
6443
0
      }
6444
0
    }
6445
0
  }
6446
6447
0
  per_config.BuiltFileSetCache = true;
6448
0
}
6449
6450
std::string cmGeneratorTarget::GetSwiftModuleName() const
6451
0
{
6452
0
  return this->GetPropertyOrDefault("Swift_MODULE_NAME", this->GetName());
6453
0
}
6454
6455
std::string cmGeneratorTarget::GetSwiftModuleFileName() const
6456
0
{
6457
0
  std::string moduleFilename = this->GetPropertyOrDefault(
6458
0
    "Swift_MODULE", this->GetSwiftModuleName() + ".swiftmodule");
6459
0
  if (this->GetPolicyStatusCMP0195() == cmPolicies::NEW) {
6460
0
    if (cmValue moduleTriple =
6461
0
          this->Makefile->GetDefinition("CMAKE_Swift_MODULE_TRIPLE")) {
6462
0
      moduleFilename = cmStrCat(std::move(moduleFilename), '/', *moduleTriple,
6463
0
                                ".swiftmodule");
6464
0
    }
6465
0
  }
6466
0
  return moduleFilename;
6467
0
}
6468
6469
std::string cmGeneratorTarget::GetSwiftModuleDirectory(
6470
  std::string const& config) const
6471
0
{
6472
  // This is like the *_OUTPUT_DIRECTORY properties except that we don't have a
6473
  // separate per-configuration target property.
6474
  //
6475
  // The property expands generator expressions. Multi-config generators append
6476
  // a per-configuration subdirectory to the specified directory unless a
6477
  // generator expression is used.
6478
0
  bool appendConfigDir = true;
6479
0
  std::string moduleDirectory;
6480
6481
0
  if (cmValue value = this->GetProperty("Swift_MODULE_DIRECTORY")) {
6482
0
    moduleDirectory = cmGeneratorExpression::Evaluate(
6483
0
      *value, this->LocalGenerator, config, this);
6484
0
    appendConfigDir = *value == moduleDirectory;
6485
0
  }
6486
0
  if (moduleDirectory.empty()) {
6487
0
    moduleDirectory = this->LocalGenerator->GetCurrentBinaryDirectory();
6488
0
  }
6489
0
  if (appendConfigDir) {
6490
0
    this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
6491
0
      "/", config, "", moduleDirectory);
6492
0
  }
6493
0
  return moduleDirectory;
6494
0
}
6495
6496
std::string cmGeneratorTarget::GetSwiftModulePath(
6497
  std::string const& config) const
6498
0
{
6499
0
  return cmStrCat(this->GetSwiftModuleDirectory(config), '/',
6500
0
                  this->GetSwiftModuleFileName());
6501
0
}
6502
6503
std::string cmGeneratorTarget::GetPropertyOrDefault(
6504
  std::string const& property, std::string defaultValue) const
6505
0
{
6506
0
  if (cmValue name = this->GetProperty(property)) {
6507
0
    return *name;
6508
0
  }
6509
0
  return defaultValue;
6510
0
}