Coverage Report

Created: 2026-04-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmExportInstallCMakeConfigGenerator.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 "cmExportInstallCMakeConfigGenerator.h"
4
5
#include <functional>
6
#include <map>
7
#include <memory>
8
#include <set>
9
#include <sstream>
10
#include <utility>
11
#include <vector>
12
13
#include <cm/string_view>
14
15
#include "cmExportFileGenerator.h"
16
#include "cmExportSet.h"
17
#include "cmFileSetMetadata.h"
18
#include "cmGenExContext.h"
19
#include "cmGeneratedFileStream.h"
20
#include "cmGeneratorExpression.h"
21
#include "cmGeneratorFileSet.h"
22
#include "cmGeneratorTarget.h"
23
#include "cmInstallExportGenerator.h"
24
#include "cmInstallFileSetGenerator.h"
25
#include "cmLocalGenerator.h"
26
#include "cmMakefile.h"
27
#include "cmMessageType.h"
28
#include "cmOutputConverter.h"
29
#include "cmStateTypes.h"
30
#include "cmStringAlgorithms.h"
31
#include "cmSystemTools.h"
32
#include "cmTargetExport.h"
33
#include "cmValue.h"
34
35
cmExportInstallCMakeConfigGenerator::cmExportInstallCMakeConfigGenerator(
36
  cmInstallExportGenerator* iegen)
37
0
  : cmExportInstallFileGenerator(iegen)
38
0
{
39
0
}
Unexecuted instantiation: cmExportInstallCMakeConfigGenerator::cmExportInstallCMakeConfigGenerator(cmInstallExportGenerator*)
Unexecuted instantiation: cmExportInstallCMakeConfigGenerator::cmExportInstallCMakeConfigGenerator(cmInstallExportGenerator*)
40
41
std::string cmExportInstallCMakeConfigGenerator::GetConfigImportFileGlob()
42
  const
43
0
{
44
0
  std::string glob = cmStrCat(this->FileBase, "-*", this->FileExt);
45
0
  return glob;
46
0
}
47
48
bool cmExportInstallCMakeConfigGenerator::GenerateMainFile(std::ostream& os)
49
0
{
50
0
  std::vector<cmTargetExport const*> allTargets;
51
0
  {
52
0
    std::string expectedTargets;
53
0
    std::string sep;
54
0
    auto visitor = [&](cmTargetExport const* te) {
55
0
      allTargets.push_back(te);
56
0
      expectedTargets += sep + this->Namespace + te->Target->GetExportName();
57
0
      sep = " ";
58
0
    };
59
60
0
    if (!this->CollectExports(visitor)) {
61
0
      return false;
62
0
    }
63
64
0
    this->GenerateExpectedTargetsCode(os, expectedTargets);
65
0
  }
66
67
  // Compute the relative import prefix for the file
68
0
  this->GenerateImportPrefix(os);
69
70
0
  bool requiresConfigFiles = false;
71
  // Create all the imported targets.
72
0
  for (cmTargetExport const* te : allTargets) {
73
0
    cmGeneratorTarget* gt = te->Target;
74
0
    cmStateEnums::TargetType targetType = this->GetExportTargetType(te);
75
76
0
    requiresConfigFiles =
77
0
      requiresConfigFiles || targetType != cmStateEnums::INTERFACE_LIBRARY;
78
79
0
    this->GenerateImportTargetCode(os, gt, targetType);
80
81
0
    ImportPropertyMap properties;
82
0
    if (!this->PopulateInterfaceProperties(te, properties)) {
83
0
      return false;
84
0
    }
85
86
0
    ImportFileSetPropertyMap fsProperties;
87
0
    if (!this->PopulateFileSetInterfaceProperties(te, fsProperties)) {
88
0
      return false;
89
0
    }
90
91
0
    if (this->PopulateInterfaceLinkLibrariesProperty(
92
0
          gt, cmGeneratorExpression::InstallInterface, properties) &&
93
0
        !this->ExportOld) {
94
0
      this->SetRequiredCMakeVersion(2, 8, 12);
95
0
    }
96
0
    if (targetType == cmStateEnums::INTERFACE_LIBRARY) {
97
0
      this->SetRequiredCMakeVersion(3, 0, 0);
98
0
    }
99
0
    if (gt->GetProperty("INTERFACE_SOURCES")) {
100
      // We can only generate INTERFACE_SOURCES in CMake 3.3, but CMake 3.1
101
      // can consume them.
102
0
      this->SetRequiredCMakeVersion(3, 1, 0);
103
0
    }
104
105
0
    this->GenerateInterfaceProperties(gt, os, properties);
106
107
0
    this->GenerateTargetFileSets(gt, os, fsProperties, te);
108
0
  }
109
110
0
  this->LoadConfigFiles(os);
111
112
0
  bool result = true;
113
114
0
  std::string cxx_modules_name = this->GetExportSet()->GetName();
115
0
  this->GenerateCxxModuleInformation(cxx_modules_name, os);
116
0
  if (requiresConfigFiles) {
117
0
    for (std::string const& c : this->Configurations) {
118
0
      if (!this->GenerateImportCxxModuleConfigTargetInclusion(cxx_modules_name,
119
0
                                                              c)) {
120
0
        result = false;
121
0
      }
122
0
    }
123
0
  }
124
125
0
  this->CleanupTemporaryVariables(os);
126
0
  this->GenerateImportedFileCheckLoop(os);
127
128
  // Generate an import file for each configuration.
129
  // Don't do this if we only export INTERFACE_LIBRARY targets.
130
0
  if (requiresConfigFiles) {
131
0
    for (std::string const& c : this->Configurations) {
132
0
      if (!this->GenerateImportFileConfig(c)) {
133
0
        result = false;
134
0
      }
135
0
    }
136
0
  }
137
138
0
  this->GenerateMissingTargetsCheckCode(os);
139
140
0
  return result;
141
0
}
142
143
void cmExportInstallCMakeConfigGenerator::GenerateImportPrefix(
144
  std::ostream& os)
145
0
{
146
  // Set an _IMPORT_PREFIX variable for import location properties
147
  // to reference if they are relative to the install prefix.
148
0
  std::string installPrefix =
149
0
    this->IEGen->GetLocalGenerator()->GetMakefile()->GetSafeDefinition(
150
0
      "CMAKE_INSTALL_PREFIX");
151
0
  std::string const& expDest = this->IEGen->GetDestination();
152
0
  if (cmSystemTools::FileIsFullPath(expDest)) {
153
    // The export file is being installed to an absolute path so the
154
    // package is not relocatable.  Use the configured install prefix.
155
    /* clang-format off */
156
0
    os <<
157
0
      "# The installation prefix configured by this project.\n"
158
0
      "set(_IMPORT_PREFIX \"" << installPrefix << "\")\n"
159
0
      "\n";
160
    /* clang-format on */
161
0
  } else {
162
    // Add code to compute the installation prefix relative to the
163
    // import file location.
164
0
    std::string absDest = cmStrCat(installPrefix, '/', expDest);
165
0
    std::string absDestS = absDest + '/';
166
0
    os << "# Compute the installation prefix relative to this file.\n"
167
0
          "get_filename_component(_IMPORT_PREFIX"
168
0
          " \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n";
169
0
    if (cmHasLiteralPrefix(absDestS, "/lib/") ||
170
0
        cmHasLiteralPrefix(absDestS, "/lib64/") ||
171
0
        cmHasLiteralPrefix(absDestS, "/libx32/") ||
172
0
        cmHasLiteralPrefix(absDestS, "/usr/lib/") ||
173
0
        cmHasLiteralPrefix(absDestS, "/usr/lib64/") ||
174
0
        cmHasLiteralPrefix(absDestS, "/usr/libx32/")) {
175
      // Handle "/usr move" symlinks created by some Linux distros.
176
      /* clang-format off */
177
0
      os <<
178
0
        "# Use original install prefix when loaded through a\n"
179
0
        "# cross-prefix symbolic link such as /lib -> /usr/lib.\n"
180
0
        "get_filename_component(_realCurr \"${_IMPORT_PREFIX}\" REALPATH)\n"
181
0
        "get_filename_component(_realOrig \"" << absDest << "\" REALPATH)\n"
182
0
        "if(_realCurr STREQUAL _realOrig)\n"
183
0
        "  set(_IMPORT_PREFIX \"" << absDest << "\")\n"
184
0
        "endif()\n"
185
0
        "unset(_realOrig)\n"
186
0
        "unset(_realCurr)\n";
187
      /* clang-format on */
188
0
    }
189
0
    std::string dest = expDest;
190
0
    while (!dest.empty()) {
191
0
      os << "get_filename_component(_IMPORT_PREFIX \"${_IMPORT_PREFIX}\" "
192
0
            "PATH)\n";
193
0
      dest = cmSystemTools::GetFilenamePath(dest);
194
0
    }
195
0
    os << "if(_IMPORT_PREFIX STREQUAL \"/\")\n"
196
0
          "  set(_IMPORT_PREFIX \"\")\n"
197
0
          "endif()\n"
198
0
          "\n";
199
0
  }
200
0
}
201
202
void cmExportInstallCMakeConfigGenerator::CleanupTemporaryVariables(
203
  std::ostream& os)
204
0
{
205
  /* clang-format off */
206
0
  os << "# Cleanup temporary variables.\n"
207
0
        "set(_IMPORT_PREFIX)\n"
208
0
        "\n";
209
  /* clang-format on */
210
0
}
211
212
void cmExportInstallCMakeConfigGenerator::LoadConfigFiles(std::ostream& os)
213
0
{
214
  // Now load per-configuration properties for them.
215
  /* clang-format off */
216
0
  os << "# Load information for each installed configuration.\n"
217
0
        "file(GLOB _cmake_config_files \"${CMAKE_CURRENT_LIST_DIR}/"
218
0
        << this->GetConfigImportFileGlob() << "\")\n"
219
0
        "foreach(_cmake_config_file IN LISTS _cmake_config_files)\n"
220
0
        "  include(\"${_cmake_config_file}\")\n"
221
0
        "endforeach()\n"
222
0
        "unset(_cmake_config_file)\n"
223
0
        "unset(_cmake_config_files)\n"
224
0
        "\n";
225
  /* clang-format on */
226
0
}
227
228
void cmExportInstallCMakeConfigGenerator::GenerateImportConfig(
229
  std::ostream& os, std::string const& config)
230
0
{
231
  // Start with the import file header.
232
0
  this->GenerateImportHeaderCode(os, config);
233
234
  // Generate the per-config target information.
235
0
  this->cmExportFileGenerator::GenerateImportConfig(os, config);
236
237
  // End with the import file footer.
238
0
  this->GenerateImportFooterCode(os);
239
0
}
240
241
void cmExportInstallCMakeConfigGenerator::GenerateImportTargetsConfig(
242
  std::ostream& os, std::string const& config, std::string const& suffix)
243
0
{
244
  // Add each target in the set to the export.
245
0
  for (std::unique_ptr<cmTargetExport> const& te :
246
0
       this->GetExportSet()->GetTargetExports()) {
247
    // Collect import properties for this target.
248
0
    if (this->GetExportTargetType(te.get()) ==
249
0
        cmStateEnums::INTERFACE_LIBRARY) {
250
0
      continue;
251
0
    }
252
253
0
    ImportPropertyMap properties;
254
0
    std::set<std::string> importedLocations;
255
256
0
    this->PopulateImportProperties(config, suffix, te.get(), properties,
257
0
                                   importedLocations);
258
259
    // If any file location was set for the target add it to the
260
    // import file.
261
0
    if (!properties.empty()) {
262
0
      cmGeneratorTarget const* const gtgt = te->Target;
263
0
      std::string const importedXcFrameworkLocation =
264
0
        this->GetImportXcFrameworkLocation(config, te.get());
265
266
0
      this->SetImportLinkInterface(config, suffix,
267
0
                                   cmGeneratorExpression::InstallInterface,
268
0
                                   gtgt, properties);
269
270
0
      this->GenerateImportPropertyCode(os, config, suffix, gtgt, properties,
271
0
                                       importedXcFrameworkLocation);
272
0
      this->GenerateImportedFileChecksCode(
273
0
        os, gtgt, properties, importedLocations, importedXcFrameworkLocation);
274
0
    }
275
0
  }
276
0
}
277
278
std::string cmExportInstallCMakeConfigGenerator::GetFileSetDirectories(
279
  cmGeneratorTarget* gte, cmGeneratorFileSet const* fileSet,
280
  cmTargetExport const* te)
281
0
{
282
0
  std::vector<std::string> resultVector;
283
284
0
  auto configs =
285
0
    gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
286
287
0
  cmGeneratorExpression ge(*gte->Makefile->GetCMakeInstance());
288
0
  auto cge =
289
0
    ge.Parse(te->FileSetGenerators.at(fileSet->GetName())->GetDestination());
290
291
0
  for (auto const& config : configs) {
292
0
    auto unescapedDest = cge->Evaluate(gte->LocalGenerator, config, gte);
293
0
    auto dest = cmOutputConverter::EscapeForCMake(
294
0
      unescapedDest, cmOutputConverter::WrapQuotes::NoWrap);
295
0
    if (!cmSystemTools::FileIsFullPath(unescapedDest)) {
296
0
      dest = cmStrCat("${_IMPORT_PREFIX}/", dest);
297
0
    }
298
299
0
    auto const& type = fileSet->GetType();
300
    // C++ modules do not support interface file sets which are dependent upon
301
    // the configuration.
302
0
    if (cge->GetHadContextSensitiveCondition() &&
303
0
        type == cm::FileSetMetadata::CXX_MODULES) {
304
0
      auto* mf = this->IEGen->GetLocalGenerator()->GetMakefile();
305
0
      std::ostringstream e;
306
0
      e << "The \"" << gte->GetName() << "\" target's interface file set \""
307
0
        << fileSet->GetName() << "\" of type \"" << type
308
0
        << "\" contains context-sensitive base file entries which is not "
309
0
           "supported.";
310
0
      mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
311
0
      return std::string{};
312
0
    }
313
314
0
    if (cge->GetHadContextSensitiveCondition() && configs.size() != 1) {
315
0
      resultVector.push_back(
316
0
        cmStrCat("\"$<$<CONFIG:", config, ">:", dest, ">\""));
317
0
    } else {
318
0
      resultVector.emplace_back(cmStrCat('"', dest, '"'));
319
0
      break;
320
0
    }
321
0
  }
322
323
0
  return cmJoin(resultVector, " ");
324
0
}
325
326
std::string cmExportInstallCMakeConfigGenerator::GetFileSetFiles(
327
  cmGeneratorTarget* gte, cmGeneratorFileSet const* fileSet,
328
  cmTargetExport const* te)
329
0
{
330
0
  std::vector<std::string> resultVector;
331
332
0
  auto configs =
333
0
    gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
334
335
0
  cmGeneratorExpression destGe(*gte->Makefile->GetCMakeInstance());
336
0
  auto destCge = destGe.Parse(
337
0
    te->FileSetGenerators.at(fileSet->GetName())->GetDestination());
338
339
0
  for (auto const& config : configs) {
340
0
    cm::GenEx::Context context(gte->LocalGenerator, config);
341
0
    auto files = fileSet->GetFiles(context, gte);
342
343
0
    auto unescapedDest = destCge->Evaluate(gte->LocalGenerator, config, gte);
344
0
    auto dest =
345
0
      cmStrCat(cmOutputConverter::EscapeForCMake(
346
0
                 unescapedDest, cmOutputConverter::WrapQuotes::NoWrap),
347
0
               '/');
348
0
    if (!cmSystemTools::FileIsFullPath(unescapedDest)) {
349
0
      dest = cmStrCat("${_IMPORT_PREFIX}/", dest);
350
0
    }
351
352
0
    bool const contextSensitive =
353
0
      destCge->GetHadContextSensitiveCondition() || files.second;
354
0
    auto const& type = fileSet->GetType();
355
    // C++ modules do not support interface file sets which are dependent upon
356
    // the configuration.
357
0
    if (contextSensitive && type == cm::FileSetMetadata::CXX_MODULES) {
358
0
      auto* mf = this->IEGen->GetLocalGenerator()->GetMakefile();
359
0
      mf->IssueMessage(MessageType::FATAL_ERROR,
360
0
                       cmStrCat("The \"", gte->GetName(),
361
0
                                "\" target's interface file set \"",
362
0
                                fileSet->GetName(), "\" of type \"", type,
363
0
                                "\" contains context-sensitive base file "
364
0
                                "entries which is not supported."));
365
0
      return std::string{};
366
0
    }
367
368
0
    for (auto const& it : files.first) {
369
0
      auto prefix = it.first.empty() ? "" : cmStrCat(it.first, '/');
370
0
      for (auto const& filename : it.second) {
371
0
        auto relFile =
372
0
          cmStrCat(prefix, cmSystemTools::GetFilenameNameView(filename));
373
0
        auto escapedFile =
374
0
          cmStrCat(dest,
375
0
                   cmOutputConverter::EscapeForCMake(
376
0
                     relFile, cmOutputConverter::WrapQuotes::NoWrap));
377
0
        if (contextSensitive && configs.size() != 1) {
378
0
          resultVector.push_back(
379
0
            cmStrCat("\"$<$<CONFIG:", config, ">:", escapedFile, ">\""));
380
0
        } else {
381
0
          resultVector.emplace_back(cmStrCat('"', escapedFile, '"'));
382
0
        }
383
0
      }
384
0
    }
385
386
0
    if (!(contextSensitive && configs.size() != 1)) {
387
0
      break;
388
0
    }
389
0
  }
390
391
0
  return cmJoin(resultVector, " ");
392
0
}
393
394
std::string cmExportInstallCMakeConfigGenerator::GetCxxModulesDirectory() const
395
0
{
396
0
  return IEGen->GetCxxModuleDirectory();
397
0
}
398
399
void cmExportInstallCMakeConfigGenerator::GenerateCxxModuleConfigInformation(
400
  std::string const& name, std::ostream& os) const
401
0
{
402
  // Now load per-configuration properties for them.
403
  /* clang-format off */
404
0
  os << "# Load information for each installed configuration.\n"
405
0
        "file(GLOB _cmake_cxx_module_includes \"${CMAKE_CURRENT_LIST_DIR}/cxx-modules-" << name << "-*.cmake\")\n"
406
0
        "foreach(_cmake_cxx_module_include IN LISTS _cmake_cxx_module_includes)\n"
407
0
        "  include(\"${_cmake_cxx_module_include}\")\n"
408
0
        "endforeach()\n"
409
0
        "unset(_cmake_cxx_module_include)\n"
410
0
        "unset(_cmake_cxx_module_includes)\n";
411
  /* clang-format on */
412
0
}
413
414
bool cmExportInstallCMakeConfigGenerator::
415
  GenerateImportCxxModuleConfigTargetInclusion(std::string const& name,
416
                                               std::string const& config)
417
0
{
418
0
  auto cxx_modules_dirname = this->GetCxxModulesDirectory();
419
0
  if (cxx_modules_dirname.empty()) {
420
0
    return true;
421
0
  }
422
423
0
  std::string filename_config = config;
424
0
  if (filename_config.empty()) {
425
0
    filename_config = "noconfig";
426
0
  }
427
428
0
  std::string const dest =
429
0
    cmStrCat(this->FileDir, '/', cxx_modules_dirname, '/');
430
0
  std::string fileName =
431
0
    cmStrCat(dest, "cxx-modules-", name, '-', filename_config, ".cmake");
432
433
0
  cmGeneratedFileStream os(fileName, true);
434
0
  if (!os) {
435
0
    std::string se = cmSystemTools::GetLastSystemError();
436
0
    std::ostringstream e;
437
0
    e << "cannot write to file \"" << fileName << "\": " << se;
438
0
    cmSystemTools::Error(e.str());
439
0
    return false;
440
0
  }
441
0
  os.SetCopyIfDifferent(true);
442
443
  // Record this per-config import file.
444
0
  this->ConfigCxxModuleFiles[config] = fileName;
445
446
0
  auto& prop_files = this->ConfigCxxModuleTargetFiles[config];
447
0
  for (auto const* tgt : this->ExportedTargets) {
448
    // Only targets with C++ module sources will have a
449
    // collator-generated install script.
450
0
    if (!tgt->HaveCxx20ModuleSources()) {
451
0
      continue;
452
0
    }
453
454
0
    auto prop_filename = cmStrCat("target-", tgt->GetFilesystemExportName(),
455
0
                                  '-', filename_config, ".cmake");
456
0
    prop_files.emplace_back(cmStrCat(dest, prop_filename));
457
0
    os << "include(\"${CMAKE_CURRENT_LIST_DIR}/" << prop_filename << "\")\n";
458
0
  }
459
460
0
  return true;
461
0
}