Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmExportFileGenerator.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 "cmExportFileGenerator.h"
4
5
#include <array>
6
#include <cstddef>
7
#include <functional>
8
#include <sstream>
9
#include <utility>
10
11
#include <cm/memory>
12
#include <cm/optional>
13
#include <cm/string_view>
14
#include <cmext/string_view>
15
16
#include "cmsys/FStream.hxx"
17
18
#include "cmComputeLinkInformation.h"
19
#include "cmFindPackageStack.h"
20
#include "cmGeneratedFileStream.h"
21
#include "cmGeneratorFileSet.h"
22
#include "cmGeneratorTarget.h"
23
#include "cmLinkItem.h"
24
#include "cmList.h"
25
#include "cmLocalGenerator.h"
26
#include "cmMakefile.h"
27
#include "cmMessageType.h"
28
#include "cmPropertyMap.h"
29
#include "cmStateTypes.h"
30
#include "cmStringAlgorithms.h"
31
#include "cmSystemTools.h"
32
#include "cmTarget.h"
33
#include "cmValue.h"
34
35
0
cmExportFileGenerator::cmExportFileGenerator() = default;
36
37
void cmExportFileGenerator::AddConfiguration(std::string const& config)
38
0
{
39
0
  this->Configurations.push_back(config);
40
0
}
41
42
void cmExportFileGenerator::SetExportFile(char const* mainFile)
43
0
{
44
0
  this->MainImportFile = mainFile;
45
0
  this->FileDir = cmSystemTools::GetFilenamePath(this->MainImportFile);
46
0
  this->FileBase =
47
0
    cmSystemTools::GetFilenameWithoutLastExtension(this->MainImportFile);
48
0
  this->FileExt =
49
0
    cmSystemTools::GetFilenameLastExtension(this->MainImportFile);
50
0
}
51
52
std::string const& cmExportFileGenerator::GetMainExportFileName() const
53
0
{
54
0
  return this->MainImportFile;
55
0
}
56
57
bool cmExportFileGenerator::GenerateImportFile()
58
0
{
59
  // Open the output file to generate it.
60
0
  std::unique_ptr<cmsys::ofstream> foutPtr;
61
0
  if (this->AppendMode) {
62
    // Open for append.
63
0
    auto openmodeApp = std::ios::app;
64
0
    foutPtr = cm::make_unique<cmsys::ofstream>(this->MainImportFile.c_str(),
65
0
                                               openmodeApp);
66
0
  } else {
67
    // Generate atomically and with copy-if-different.
68
0
    auto ap =
69
0
      cm::make_unique<cmGeneratedFileStream>(this->MainImportFile, true);
70
0
    ap->SetCopyIfDifferent(true);
71
0
    foutPtr = std::move(ap);
72
0
  }
73
0
  if (!foutPtr || !*foutPtr) {
74
0
    std::string se = cmSystemTools::GetLastSystemError();
75
0
    std::ostringstream e;
76
0
    e << "cannot write to file \"" << this->MainImportFile << "\": " << se;
77
0
    cmSystemTools::Error(e.str());
78
0
    return false;
79
0
  }
80
81
0
  return this->GenerateImportFile(*foutPtr);
82
0
}
83
84
std::string cmExportFileGenerator::PropertyConfigSuffix(
85
  std::string const& config)
86
0
{
87
  // Construct the property configuration suffix.
88
0
  if (config.empty()) {
89
0
    return "_NOCONFIG";
90
0
  }
91
0
  return cmStrCat('_', cmSystemTools::UpperCase(config));
92
0
}
93
94
void cmExportFileGenerator::GenerateImportConfig(std::ostream& os,
95
                                                 std::string const& config)
96
0
{
97
  // Generate the per-config target information.
98
0
  this->GenerateImportTargetsConfig(os, config, PropertyConfigSuffix(config));
99
0
}
100
101
bool cmExportFileGenerator::PopulateInterfaceProperties(
102
  cmGeneratorTarget const* target, std::string const& includesDestinationDirs,
103
  cmGeneratorExpression::PreprocessContext preprocessRule,
104
  ImportPropertyMap& properties)
105
0
{
106
0
  this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS", target,
107
0
                                  preprocessRule, properties);
108
0
  this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS", target,
109
0
                                  preprocessRule, properties);
110
0
  this->PopulateInterfaceProperty("INTERFACE_PRECOMPILE_HEADERS", target,
111
0
                                  preprocessRule, properties);
112
0
  this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS", target,
113
0
                                  preprocessRule, properties);
114
0
  this->PopulateInterfaceProperty("INTERFACE_AUTOMOC_MACRO_NAMES", target,
115
0
                                  preprocessRule, properties);
116
0
  this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", target,
117
0
                                  preprocessRule, properties);
118
0
  this->PopulateInterfaceProperty("INTERFACE_LINK_OPTIONS", target,
119
0
                                  preprocessRule, properties);
120
0
  this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE",
121
0
                                  target, properties);
122
123
0
  this->PopulateInterfaceProperty("SPDX_LICENSE", target, preprocessRule,
124
0
                                  properties);
125
126
0
  std::string errorMessage;
127
0
  if (!this->PopulateCxxModuleExportProperties(
128
0
        target, properties, preprocessRule, includesDestinationDirs,
129
0
        errorMessage)) {
130
0
    this->ReportError(errorMessage);
131
0
    return false;
132
0
  }
133
134
0
  if (!this->PopulateExportProperties(target, properties, errorMessage)) {
135
0
    this->ReportError(errorMessage);
136
0
    return false;
137
0
  }
138
0
  this->PopulateCompatibleInterfaceProperties(target, properties);
139
0
  this->PopulateCustomTransitiveInterfaceProperties(target, preprocessRule,
140
0
                                                    properties);
141
142
0
  return true;
143
0
}
144
145
bool cmExportFileGenerator::PopulateFileSetInterfaceProperties(
146
  cmGeneratorTarget const* target, cmGeneratorFileSet const* fileSet,
147
  cmGeneratorExpression::PreprocessContext preprocessRule,
148
  ImportPropertyMap& properties)
149
0
{
150
0
  this->PopulateFileSetInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS",
151
0
                                         target, fileSet, preprocessRule,
152
0
                                         properties);
153
0
  this->PopulateFileSetInterfaceProperty("INTERFACE_COMPILE_OPTIONS", target,
154
0
                                         fileSet, preprocessRule, properties);
155
156
0
  return true;
157
0
}
158
159
void cmExportFileGenerator::PopulateInterfaceProperty(
160
  std::string const& propName, cmGeneratorTarget const* target,
161
  ImportPropertyMap& properties) const
162
0
{
163
0
  cmValue input = target->GetProperty(propName);
164
0
  if (input) {
165
0
    properties[propName] = *input;
166
0
  }
167
0
}
168
169
void cmExportFileGenerator::PopulateInterfaceProperty(
170
  std::string const& propName, std::string const& outputName,
171
  cmGeneratorTarget const* target,
172
  cmGeneratorExpression::PreprocessContext preprocessRule,
173
  ImportPropertyMap& properties)
174
0
{
175
0
  cmValue input = target->GetProperty(propName);
176
0
  if (input) {
177
0
    if (input->empty()) {
178
      // Set to empty
179
0
      properties[outputName].clear();
180
0
      return;
181
0
    }
182
183
0
    std::string prepro =
184
0
      cmGeneratorExpression::Preprocess(*input, preprocessRule);
185
0
    if (!prepro.empty()) {
186
0
      this->ResolveTargetsInGeneratorExpressions(prepro, target);
187
0
      properties[outputName] = prepro;
188
0
    }
189
0
  }
190
0
}
191
192
void cmExportFileGenerator::PopulateInterfaceProperty(
193
  std::string const& propName, cmGeneratorTarget const* target,
194
  cmGeneratorExpression::PreprocessContext preprocessRule,
195
  ImportPropertyMap& properties)
196
0
{
197
0
  this->PopulateInterfaceProperty(propName, propName, target, preprocessRule,
198
0
                                  properties);
199
0
}
200
201
void cmExportFileGenerator::PopulateFileSetInterfaceProperty(
202
  std::string const& propName, cmGeneratorTarget const* target,
203
  cmGeneratorFileSet const* fileSet,
204
  cmGeneratorExpression::PreprocessContext preprocessRule,
205
  ImportPropertyMap& properties)
206
0
{
207
0
  cmValue input = fileSet->GetProperty(propName);
208
0
  if (input) {
209
0
    if (input->empty()) {
210
      // Set to empty
211
0
      properties[propName].clear();
212
0
      return;
213
0
    }
214
215
0
    std::string prepro =
216
0
      cmGeneratorExpression::Preprocess(*input, preprocessRule);
217
0
    if (!prepro.empty()) {
218
0
      this->ResolveTargetsInGeneratorExpressions(prepro, target);
219
0
      properties[propName] = prepro;
220
0
    }
221
0
  }
222
0
}
223
224
bool cmExportFileGenerator::PopulateInterfaceLinkLibrariesProperty(
225
  cmGeneratorTarget const* target,
226
  cmGeneratorExpression::PreprocessContext preprocessRule,
227
  ImportPropertyMap& properties)
228
0
{
229
0
  if (!target->IsLinkable()) {
230
0
    return false;
231
0
  }
232
0
  static std::array<std::string, 3> const linkIfaceProps = {
233
0
    { "INTERFACE_LINK_LIBRARIES", "INTERFACE_LINK_LIBRARIES_DIRECT",
234
0
      "INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE" }
235
0
  };
236
0
  bool hadINTERFACE_LINK_LIBRARIES = false;
237
0
  for (std::string const& linkIfaceProp : linkIfaceProps) {
238
0
    if (cmValue input = target->GetProperty(linkIfaceProp)) {
239
0
      std::string prepro =
240
0
        cmGeneratorExpression::Preprocess(*input, preprocessRule);
241
0
      if (!prepro.empty()) {
242
0
        this->ResolveTargetsInGeneratorExpressions(prepro, target,
243
0
                                                   ReplaceFreeTargets);
244
0
        properties[linkIfaceProp] = prepro;
245
0
        hadINTERFACE_LINK_LIBRARIES = true;
246
0
      }
247
0
    }
248
0
  }
249
0
  return hadINTERFACE_LINK_LIBRARIES;
250
0
}
251
252
void cmExportFileGenerator::AddImportPrefix(std::string& exportDirs) const
253
0
{
254
0
  std::vector<std::string> entries;
255
0
  cmGeneratorExpression::Split(exportDirs, entries);
256
0
  exportDirs.clear();
257
0
  char const* sep = "";
258
0
  cm::string_view const prefixWithSlash = this->GetImportPrefixWithSlash();
259
0
  for (std::string const& e : entries) {
260
0
    exportDirs += sep;
261
0
    sep = ";";
262
0
    if (!cmSystemTools::FileIsFullPath(e) &&
263
0
        !cmHasPrefix(e, prefixWithSlash)) {
264
0
      exportDirs += prefixWithSlash;
265
0
    }
266
0
    exportDirs += e;
267
0
  }
268
0
}
269
270
namespace {
271
void getPropertyContents(cmGeneratorTarget const* tgt, std::string const& prop,
272
                         std::set<std::string>& ifaceProperties)
273
0
{
274
0
  cmValue p = tgt->GetProperty(prop);
275
0
  if (!p) {
276
0
    return;
277
0
  }
278
0
  cmList content{ *p };
279
0
  ifaceProperties.insert(content.begin(), content.end());
280
0
}
281
282
void getCompatibleInterfaceProperties(cmGeneratorTarget const* target,
283
                                      std::set<std::string>& ifaceProperties,
284
                                      std::string const& config)
285
0
{
286
0
  if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
287
    // object libraries have no link information, so nothing to compute
288
0
    return;
289
0
  }
290
291
0
  cmComputeLinkInformation* info = target->GetLinkInformation(config);
292
293
0
  if (!info) {
294
0
    cmLocalGenerator* lg = target->GetLocalGenerator();
295
0
    std::ostringstream e;
296
0
    e << "Exporting the target \"" << target->GetName()
297
0
      << "\" is not "
298
0
         "allowed since its linker language cannot be determined";
299
0
    lg->IssueMessage(MessageType::FATAL_ERROR, e.str());
300
0
    return;
301
0
  }
302
303
0
  cmComputeLinkInformation::ItemVector const& deps = info->GetItems();
304
305
0
  for (auto const& dep : deps) {
306
0
    if (!dep.Target || dep.Target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
307
0
      continue;
308
0
    }
309
0
    getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_BOOL",
310
0
                        ifaceProperties);
311
0
    getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_STRING",
312
0
                        ifaceProperties);
313
0
    getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_NUMBER_MIN",
314
0
                        ifaceProperties);
315
0
    getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_NUMBER_MAX",
316
0
                        ifaceProperties);
317
0
  }
318
0
}
319
}
320
321
void cmExportFileGenerator::PopulateCompatibleInterfaceProperties(
322
  cmGeneratorTarget const* gtarget, ImportPropertyMap& properties) const
323
0
{
324
0
  this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_BOOL", gtarget,
325
0
                                  properties);
326
0
  this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_STRING", gtarget,
327
0
                                  properties);
328
0
  this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_NUMBER_MIN", gtarget,
329
0
                                  properties);
330
0
  this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_NUMBER_MAX", gtarget,
331
0
                                  properties);
332
333
0
  std::set<std::string> ifaceProperties;
334
335
0
  getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_BOOL", ifaceProperties);
336
0
  getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_STRING", ifaceProperties);
337
0
  getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_NUMBER_MIN",
338
0
                      ifaceProperties);
339
0
  getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_NUMBER_MAX",
340
0
                      ifaceProperties);
341
342
0
  if (gtarget->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
343
0
    std::vector<std::string> configNames =
344
0
      gtarget->Target->GetMakefile()->GetGeneratorConfigs(
345
0
        cmMakefile::IncludeEmptyConfig);
346
347
0
    for (std::string const& cn : configNames) {
348
0
      getCompatibleInterfaceProperties(gtarget, ifaceProperties, cn);
349
0
    }
350
0
  }
351
352
0
  for (std::string const& ip : ifaceProperties) {
353
0
    this->PopulateInterfaceProperty("INTERFACE_" + ip, gtarget, properties);
354
0
  }
355
0
}
356
357
void cmExportFileGenerator::PopulateCustomTransitiveInterfaceProperties(
358
  cmGeneratorTarget const* target,
359
  cmGeneratorExpression::PreprocessContext preprocessRule,
360
  ImportPropertyMap& properties)
361
0
{
362
0
  this->PopulateInterfaceProperty("TRANSITIVE_COMPILE_PROPERTIES", target,
363
0
                                  properties);
364
0
  this->PopulateInterfaceProperty("TRANSITIVE_LINK_PROPERTIES", target,
365
0
                                  properties);
366
0
  cmGeneratorTarget::CheckLinkLibrariesSuppressionRAII suppress;
367
0
  std::set<std::string> ifaceProperties;
368
0
  for (std::string const& config : this->Configurations) {
369
0
    for (auto const& i : target->GetCustomTransitiveProperties(
370
0
           config, cmGeneratorTarget::PropertyFor::Interface)) {
371
0
      ifaceProperties.emplace(i.second.InterfaceName);
372
0
    }
373
0
  }
374
0
  for (std::string const& ip : ifaceProperties) {
375
0
    this->PopulateInterfaceProperty(ip, target, preprocessRule, properties);
376
0
  }
377
0
}
378
379
bool cmExportFileGenerator::NoteLinkedTarget(
380
  cmGeneratorTarget const* /*target*/, std::string const& /*linkedName*/,
381
  cmGeneratorTarget const* /*linkedTarget*/)
382
0
{
383
  // Default implementation does nothing; only needed by some generators.
384
0
  return true;
385
0
}
386
387
bool cmExportFileGenerator::AddTargetNamespace(std::string& input,
388
                                               cmGeneratorTarget const* target,
389
                                               cmLocalGenerator const* lg)
390
0
{
391
0
  cmGeneratorTarget::TargetOrString resolved =
392
0
    target->ResolveTargetReference(input, lg);
393
394
0
  cmGeneratorTarget* tgt = resolved.Target;
395
0
  if (!tgt) {
396
0
    input = resolved.String;
397
0
    return false;
398
0
  }
399
400
0
  cmFindPackageStack const& pkgStack = tgt->Target->GetFindPackageStack();
401
0
  if (!pkgStack.Empty() ||
402
0
      tgt->Target->GetProperty("EXPORT_FIND_PACKAGE_NAME")) {
403
0
    this->ExternalTargets.emplace(tgt);
404
0
  }
405
406
0
  if (tgt->IsImported()) {
407
0
    input = tgt->GetName();
408
0
    return this->NoteLinkedTarget(target, input, tgt);
409
0
  }
410
411
0
  if (this->ExportedTargets.find(tgt) != this->ExportedTargets.end()) {
412
0
    input = this->Namespace + tgt->GetExportName();
413
0
  } else {
414
0
    std::string namespacedTarget;
415
0
    this->HandleMissingTarget(namespacedTarget, target, tgt);
416
0
    if (!namespacedTarget.empty()) {
417
0
      input = namespacedTarget;
418
0
    } else {
419
0
      input = tgt->GetName();
420
0
    }
421
0
  }
422
423
0
  return this->NoteLinkedTarget(target, input, tgt);
424
0
}
425
426
void cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
427
  std::string& input, cmGeneratorTarget const* target,
428
  FreeTargetsReplace replace)
429
0
{
430
0
  cmLocalGenerator const* lg = target->GetLocalGenerator();
431
0
  if (replace == NoReplaceFreeTargets) {
432
0
    this->ResolveTargetsInGeneratorExpression(input, target, lg);
433
0
    return;
434
0
  }
435
0
  std::vector<std::string> parts;
436
0
  cmGeneratorExpression::Split(input, parts);
437
438
0
  std::string sep;
439
0
  input.clear();
440
0
  for (std::string& li : parts) {
441
0
    if (target->IsLinkLookupScope(li, lg)) {
442
0
      continue;
443
0
    }
444
0
    if (cmGeneratorExpression::Find(li) == std::string::npos) {
445
0
      this->AddTargetNamespace(li, target, lg);
446
0
    } else {
447
0
      this->ResolveTargetsInGeneratorExpression(li, target, lg);
448
0
    }
449
0
    input += sep + li;
450
0
    sep = ";";
451
0
  }
452
0
}
453
454
cm::optional<std::string> cmResolveTargetsInGeneratorExpression(
455
  std::string& input,
456
  std::function<bool(std::string& name)> const& addTargetNamespace)
457
0
{
458
0
  std::string::size_type pos = 0;
459
0
  std::string::size_type lastPos = pos;
460
461
0
  while ((pos = input.find("$<TARGET_PROPERTY:", lastPos)) !=
462
0
         std::string::npos) {
463
0
    std::string::size_type nameStartPos = pos + cmStrLen("$<TARGET_PROPERTY:");
464
0
    std::string::size_type closePos = input.find('>', nameStartPos);
465
0
    std::string::size_type commaPos = input.find(',', nameStartPos);
466
0
    std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
467
0
    if (commaPos == std::string::npos    // Implied 'this' target
468
0
        || closePos == std::string::npos // Incomplete expression.
469
0
        || closePos < commaPos           // Implied 'this' target
470
0
        || nextOpenPos < commaPos)       // Non-literal
471
0
    {
472
0
      lastPos = nameStartPos;
473
0
      continue;
474
0
    }
475
476
0
    std::string targetName =
477
0
      input.substr(nameStartPos, commaPos - nameStartPos);
478
479
0
    if (addTargetNamespace(targetName)) {
480
0
      input.replace(nameStartPos, commaPos - nameStartPos, targetName);
481
0
    }
482
0
    lastPos = nameStartPos + targetName.size() + 1;
483
0
  }
484
485
0
  cm::optional<std::string> errorString;
486
0
  pos = 0;
487
0
  lastPos = pos;
488
0
  while ((pos = input.find("$<TARGET_NAME:", lastPos)) != std::string::npos) {
489
0
    std::string::size_type nameStartPos = pos + cmStrLen("$<TARGET_NAME:");
490
0
    std::string::size_type endPos = input.find('>', nameStartPos);
491
0
    if (endPos == std::string::npos) {
492
0
      errorString = "$<TARGET_NAME:...> expression incomplete";
493
0
      break;
494
0
    }
495
0
    std::string targetName = input.substr(nameStartPos, endPos - nameStartPos);
496
0
    if (targetName.find("$<") != std::string::npos) {
497
0
      errorString = "$<TARGET_NAME:...> requires its parameter to be a "
498
0
                    "literal.";
499
0
      break;
500
0
    }
501
0
    if (!addTargetNamespace(targetName)) {
502
0
      errorString = "$<TARGET_NAME:...> requires its parameter to be a "
503
0
                    "reachable target.";
504
0
      break;
505
0
    }
506
0
    input.replace(pos, endPos - pos + 1, targetName);
507
0
    lastPos = pos + targetName.size();
508
0
  }
509
510
0
  pos = 0;
511
0
  lastPos = pos;
512
0
  while (!errorString &&
513
0
         (pos = input.find("$<LINK_ONLY:", lastPos)) != std::string::npos) {
514
0
    std::string::size_type nameStartPos = pos + cmStrLen("$<LINK_ONLY:");
515
0
    std::string::size_type endPos = input.find('>', nameStartPos);
516
0
    if (endPos == std::string::npos) {
517
0
      errorString = "$<LINK_ONLY:...> expression incomplete";
518
0
      break;
519
0
    }
520
0
    std::string libName = input.substr(nameStartPos, endPos - nameStartPos);
521
0
    if (cmGeneratorExpression::IsValidTargetName(libName) &&
522
0
        addTargetNamespace(libName)) {
523
0
      input.replace(nameStartPos, endPos - nameStartPos, libName);
524
0
    }
525
0
    lastPos = nameStartPos + libName.size() + 1;
526
0
  }
527
528
0
  while (!errorString &&
529
0
         (pos = input.find("$<COMPILE_ONLY:", lastPos)) != std::string::npos) {
530
0
    std::string::size_type nameStartPos = pos + cmStrLen("$<COMPILE_ONLY:");
531
0
    std::string::size_type endPos = input.find('>', nameStartPos);
532
0
    if (endPos == std::string::npos) {
533
0
      errorString = "$<COMPILE_ONLY:...> expression incomplete";
534
0
      break;
535
0
    }
536
0
    std::string libName = input.substr(nameStartPos, endPos - nameStartPos);
537
0
    if (cmGeneratorExpression::IsValidTargetName(libName) &&
538
0
        addTargetNamespace(libName)) {
539
0
      input.replace(nameStartPos, endPos - nameStartPos, libName);
540
0
    }
541
0
    lastPos = nameStartPos + libName.size() + 1;
542
0
  }
543
544
0
  return errorString;
545
0
}
546
547
void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
548
  std::string& input, cmGeneratorTarget const* target,
549
  cmLocalGenerator const* lg)
550
0
{
551
0
  auto err = cmResolveTargetsInGeneratorExpression(
552
0
    input, [this, target, lg](std::string& name) {
553
0
      return this->AddTargetNamespace(name, target, lg);
554
0
    });
555
0
  this->ReplaceInstallPrefix(input);
556
0
  if (err) {
557
0
    target->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR, *err);
558
0
  }
559
0
}
560
561
void cmExportFileGenerator::ReplaceInstallPrefix(std::string& /*unused*/) const
562
0
{
563
  // Do nothing
564
0
}
565
566
void cmExportFileGenerator::SetImportDetailProperties(
567
  std::string const& config, std::string const& suffix,
568
  cmGeneratorTarget const* target, ImportPropertyMap& properties)
569
0
{
570
  // Get the makefile in which to lookup target information.
571
0
  cmMakefile* mf = target->Makefile;
572
573
  // Add the soname for unix shared libraries.
574
0
  if (target->GetType() == cmStateEnums::SHARED_LIBRARY ||
575
0
      target->GetType() == cmStateEnums::MODULE_LIBRARY) {
576
0
    if (!target->IsDLLPlatform()) {
577
0
      std::string prop;
578
0
      std::string value;
579
0
      if (target->HasSOName(config)) {
580
0
        if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
581
0
          value = this->InstallNameDir(target, config);
582
0
        }
583
0
        prop = "IMPORTED_SONAME";
584
0
        value += target->GetSOName(config);
585
0
      } else {
586
0
        prop = "IMPORTED_NO_SONAME";
587
0
        value = "TRUE";
588
0
      }
589
0
      prop += suffix;
590
0
      properties[prop] = value;
591
0
    }
592
0
  }
593
594
  // Add the transitive link dependencies for this configuration.
595
0
  if (cmLinkInterface const* iface =
596
0
        target->GetLinkInterface(config, target)) {
597
0
    this->SetImportLinkProperty(
598
0
      suffix, target, "IMPORTED_LINK_INTERFACE_LANGUAGES", iface->Languages,
599
0
      properties, ImportLinkPropertyTargetNames::No);
600
601
    // Export IMPORTED_LINK_DEPENDENT_LIBRARIES to help consuming linkers
602
    // find private dependencies of shared libraries.
603
0
    std::size_t oldMissingTargetsSize = this->MissingTargets.size();
604
0
    auto oldExternalTargets = this->ExternalTargets;
605
0
    this->SetImportLinkProperty(
606
0
      suffix, target, "IMPORTED_LINK_DEPENDENT_LIBRARIES", iface->SharedDeps,
607
0
      properties, ImportLinkPropertyTargetNames::Yes);
608
    // Avoid enforcing shared library private dependencies as public package
609
    // dependencies by ignoring missing targets added for them.
610
0
    this->MissingTargets.resize(oldMissingTargetsSize);
611
0
    this->ExternalTargets = std::move(oldExternalTargets);
612
613
0
    if (iface->Multiplicity > 0) {
614
0
      std::string prop =
615
0
        cmStrCat("IMPORTED_LINK_INTERFACE_MULTIPLICITY", suffix);
616
0
      properties[prop] = std::to_string(iface->Multiplicity);
617
0
    }
618
0
  }
619
620
  // Add information if this target is a managed target
621
0
  if (target->GetManagedType(config) !=
622
0
      cmGeneratorTarget::ManagedType::Native) {
623
0
    std::string prop = cmStrCat("IMPORTED_COMMON_LANGUAGE_RUNTIME", suffix);
624
0
    std::string propval;
625
0
    if (cmValue p = target->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
626
0
      propval = *p;
627
0
    } else if (target->IsCSharpOnly()) {
628
      // C# projects do not have the /clr flag, so we set the property
629
      // here to mark the target as (only) managed (i.e. no .lib file
630
      // to link to). Otherwise the  COMMON_LANGUAGE_RUNTIME target
631
      // property would have to be set manually for C# targets to make
632
      // exporting/importing work.
633
0
      propval = "CSharp";
634
0
    }
635
0
    properties[prop] = propval;
636
0
  }
637
0
}
638
639
namespace {
640
std::string const& asString(std::string const& l)
641
0
{
642
0
  return l;
643
0
}
644
645
std::string const& asString(cmLinkItem const& l)
646
0
{
647
0
  return l.AsStr();
648
0
}
649
}
650
651
template <typename T>
652
void cmExportFileGenerator::SetImportLinkProperty(
653
  std::string const& suffix, cmGeneratorTarget const* target,
654
  std::string const& propName, std::vector<T> const& entries,
655
  ImportPropertyMap& properties, ImportLinkPropertyTargetNames targetNames)
656
0
{
657
  // Skip the property if there are no entries.
658
0
  if (entries.empty()) {
659
0
    return;
660
0
  }
661
662
0
  cmLocalGenerator const* lg = target->GetLocalGenerator();
663
664
  // Construct the property value.
665
0
  std::string link_entries;
666
0
  char const* sep = "";
667
0
  for (T const& l : entries) {
668
    // Separate this from the previous entry.
669
0
    link_entries += sep;
670
0
    sep = ";";
671
672
0
    if (targetNames == ImportLinkPropertyTargetNames::Yes) {
673
0
      std::string temp = asString(l);
674
0
      this->AddTargetNamespace(temp, target, lg);
675
0
      link_entries += temp;
676
0
    } else {
677
0
      link_entries += asString(l);
678
0
    }
679
0
  }
680
681
  // Store the property.
682
0
  std::string prop = cmStrCat(propName, suffix);
683
0
  properties[prop] = link_entries;
684
0
}
Unexecuted instantiation: void cmExportFileGenerator::SetImportLinkProperty<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cmGeneratorTarget const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >&, cmExportFileGenerator::ImportLinkPropertyTargetNames)
Unexecuted instantiation: void cmExportFileGenerator::SetImportLinkProperty<cmLinkItem>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cmGeneratorTarget const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<cmLinkItem, std::__1::allocator<cmLinkItem> > const&, std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >&, cmExportFileGenerator::ImportLinkPropertyTargetNames)
685
686
template void cmExportFileGenerator::SetImportLinkProperty<std::string>(
687
  std::string const&, cmGeneratorTarget const*, std::string const&,
688
  std::vector<std::string> const&, ImportPropertyMap& properties,
689
  ImportLinkPropertyTargetNames);
690
691
template void cmExportFileGenerator::SetImportLinkProperty<cmLinkItem>(
692
  std::string const&, cmGeneratorTarget const*, std::string const&,
693
  std::vector<cmLinkItem> const&, ImportPropertyMap& properties,
694
  ImportLinkPropertyTargetNames);
695
696
namespace {
697
enum class ExportWhen
698
{
699
  Defined,
700
  Always,
701
};
702
703
enum class PropertyType
704
{
705
  Strings,
706
  Paths,
707
  IncludePaths,
708
};
709
710
bool PropertyTypeIsForPaths(PropertyType pt)
711
0
{
712
0
  switch (pt) {
713
0
    case PropertyType::Strings:
714
0
      return false;
715
0
    case PropertyType::Paths:
716
0
    case PropertyType::IncludePaths:
717
0
      return true;
718
0
  }
719
0
  return false;
720
0
}
721
}
722
723
bool cmExportFileGenerator::PopulateCxxModuleExportProperties(
724
  cmGeneratorTarget const* gte, ImportPropertyMap& properties,
725
  cmGeneratorExpression::PreprocessContext ctx,
726
  std::string const& includesDestinationDirs, std::string&)
727
0
{
728
0
  if (!gte->HaveCxx20ModuleSources()) {
729
0
    return true;
730
0
  }
731
732
0
  struct ModuleTargetPropertyTable
733
0
  {
734
0
    cm::static_string_view Name;
735
0
    ExportWhen Cond;
736
0
  };
737
738
0
  ModuleTargetPropertyTable const exportedDirectModuleProperties[] = {
739
0
    { "CXX_EXTENSIONS"_s, ExportWhen::Defined },
740
    // Always define this property as it is an intrinsic property of the target
741
    // and should not be inherited from the in-scope `CMAKE_CXX_MODULE_STD`
742
    // variable.
743
    //
744
    // TODO(cxxmodules): A future policy may make this "ON" based on the target
745
    // policies if unset. Add a new `ExportWhen` condition to handle it when
746
    // this happens.
747
0
    { "CXX_MODULE_STD"_s, ExportWhen::Always },
748
0
  };
749
0
  for (auto const& prop : exportedDirectModuleProperties) {
750
0
    auto const propNameStr = std::string(prop.Name);
751
0
    cmValue propValue = gte->Target->GetComputedProperty(
752
0
      propNameStr, *gte->Target->GetMakefile());
753
0
    if (!propValue) {
754
0
      propValue = gte->Target->GetProperty(propNameStr);
755
0
    }
756
0
    if (propValue) {
757
0
      properties[propNameStr] =
758
0
        cmGeneratorExpression::Preprocess(*propValue, ctx);
759
0
    } else if (prop.Cond == ExportWhen::Always) {
760
0
      properties[propNameStr] = "";
761
0
    }
762
0
  }
763
764
0
  struct ModulePropertyTable
765
0
  {
766
0
    cm::static_string_view Name;
767
0
    PropertyType Type;
768
0
  };
769
770
0
  ModulePropertyTable const exportedModuleProperties[] = {
771
0
    { "INCLUDE_DIRECTORIES"_s, PropertyType::IncludePaths },
772
0
    { "COMPILE_DEFINITIONS"_s, PropertyType::Strings },
773
0
    { "COMPILE_OPTIONS"_s, PropertyType::Strings },
774
0
    { "COMPILE_FEATURES"_s, PropertyType::Strings },
775
0
  };
776
0
  for (auto const& propEntry : exportedModuleProperties) {
777
0
    auto const propNameStr = std::string(propEntry.Name);
778
0
    cmValue prop = gte->Target->GetComputedProperty(
779
0
      propNameStr, *gte->Target->GetMakefile());
780
0
    if (!prop) {
781
0
      prop = gte->Target->GetProperty(propNameStr);
782
0
    }
783
0
    if (prop) {
784
0
      auto const exportedPropName =
785
0
        cmStrCat("IMPORTED_CXX_MODULES_", propEntry.Name);
786
0
      properties[exportedPropName] =
787
0
        cmGeneratorExpression::Preprocess(*prop, ctx);
788
0
      if (ctx == cmGeneratorExpression::InstallInterface &&
789
0
          PropertyTypeIsForPaths(propEntry.Type)) {
790
0
        this->ReplaceInstallPrefix(properties[exportedPropName]);
791
0
        this->AddImportPrefix(properties[exportedPropName]);
792
0
        if (propEntry.Type == PropertyType::IncludePaths &&
793
0
            !includesDestinationDirs.empty()) {
794
0
          if (!properties[exportedPropName].empty()) {
795
0
            properties[exportedPropName] += ';';
796
0
          }
797
0
          properties[exportedPropName] += includesDestinationDirs;
798
0
        }
799
0
      }
800
0
    }
801
0
  }
802
803
0
  cm::static_string_view const exportedLinkModuleProperties[] = {
804
0
    "LINK_LIBRARIES"_s,
805
0
  };
806
0
  for (auto const& propName : exportedLinkModuleProperties) {
807
0
    auto const propNameStr = std::string(propName);
808
0
    cmValue prop = gte->Target->GetComputedProperty(
809
0
      propNameStr, *gte->Target->GetMakefile());
810
0
    if (!prop) {
811
0
      prop = gte->Target->GetProperty(propNameStr);
812
0
    }
813
0
    if (prop) {
814
0
      auto const exportedPropName =
815
0
        cmStrCat("IMPORTED_CXX_MODULES_", propName);
816
0
      auto value = cmGeneratorExpression::Preprocess(*prop, ctx);
817
0
      this->ResolveTargetsInGeneratorExpressions(value, gte,
818
0
                                                 ReplaceFreeTargets);
819
0
      properties[exportedPropName] = value;
820
0
    }
821
0
  }
822
823
0
  return true;
824
0
}
825
826
bool cmExportFileGenerator::PopulateExportProperties(
827
  cmGeneratorTarget const* gte, ImportPropertyMap& properties,
828
  std::string& errorMessage) const
829
0
{
830
0
  auto const& targetProperties = gte->Target->GetProperties();
831
0
  if (cmValue exportProperties =
832
0
        targetProperties.GetPropertyValue("EXPORT_PROPERTIES")) {
833
0
    for (auto& prop : cmList{ *exportProperties }) {
834
      /* Black list reserved properties */
835
0
      if (cmHasLiteralPrefix(prop, "IMPORTED_") ||
836
0
          cmHasLiteralPrefix(prop, "INTERFACE_")) {
837
0
        std::ostringstream e;
838
0
        e << "Target \"" << gte->Target->GetName() << "\" contains property \""
839
0
          << prop << "\" in EXPORT_PROPERTIES but IMPORTED_* and INTERFACE_* "
840
0
          << "properties are reserved.";
841
0
        errorMessage = e.str();
842
0
        return false;
843
0
      }
844
0
      cmValue propertyValue = targetProperties.GetPropertyValue(prop);
845
0
      if (!propertyValue) {
846
        // Asked to export a property that isn't defined on the target. Do not
847
        // consider this an error, there's just nothing to export.
848
0
        continue;
849
0
      }
850
0
      std::string evaluatedValue = cmGeneratorExpression::Preprocess(
851
0
        *propertyValue, cmGeneratorExpression::StripAllGeneratorExpressions);
852
0
      if (evaluatedValue != *propertyValue) {
853
0
        std::ostringstream e;
854
0
        e << "Target \"" << gte->Target->GetName() << "\" contains property \""
855
0
          << prop << "\" in EXPORT_PROPERTIES but this property contains a "
856
0
          << "generator expression. This is not allowed.";
857
0
        errorMessage = e.str();
858
0
        return false;
859
0
      }
860
0
      properties[prop] = *propertyValue;
861
0
    }
862
0
  }
863
0
  return true;
864
0
}