Coverage Report

Created: 2026-03-12 06:35

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