Coverage Report

Created: 2026-10-28 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmExportBuildCMakeConfigGenerator.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 "cmExportBuildCMakeConfigGenerator.h"
4
5
#include <cstddef>
6
#include <functional>
7
#include <map>
8
#include <ostream>
9
#include <set>
10
#include <utility>
11
#include <vector>
12
13
#include "cmCryptoHash.h"
14
#include "cmDiagnosticContext.h"
15
#include "cmExportSet.h"
16
#include "cmGenExContext.h"
17
#include "cmGeneratedFileStream.h"
18
#include "cmGeneratorExpression.h"
19
#include "cmGeneratorFileSet.h"
20
#include "cmGeneratorTarget.h"
21
#include "cmLocalGenerator.h"
22
#include "cmMakefile.h"
23
#include "cmOutputConverter.h"
24
#include "cmStringAlgorithms.h"
25
#include "cmSystemTools.h"
26
#include "cmTarget.h"
27
#include "cmTargetTypes.h"
28
29
cmExportBuildCMakeConfigGenerator::cmExportBuildCMakeConfigGenerator(
30
  cmDiagnosticContext context)
31
0
  : cmExportBuildFileGenerator(std::move(context))
32
0
{
33
0
  this->LG = nullptr;
34
0
  this->ExportSet = nullptr;
35
0
}
Unexecuted instantiation: cmExportBuildCMakeConfigGenerator::cmExportBuildCMakeConfigGenerator(cmDiagnosticContext)
Unexecuted instantiation: cmExportBuildCMakeConfigGenerator::cmExportBuildCMakeConfigGenerator(cmDiagnosticContext)
36
37
bool cmExportBuildCMakeConfigGenerator::GenerateMainFile(std::ostream& os)
38
0
{
39
0
  {
40
0
    std::string expectedTargets;
41
0
    std::string sep;
42
0
    bool generatedInterfaceRequired = false;
43
0
    auto visitor = [&](cmGeneratorTarget const* te) {
44
0
      expectedTargets = cmStrCat(std::move(expectedTargets), sep,
45
0
                                 this->Namespace, te->GetExportName());
46
0
      sep = " ";
47
48
0
      generatedInterfaceRequired |=
49
0
        this->GetExportTargetType(te) == cm::TargetType::INTERFACE_LIBRARY;
50
0
    };
51
52
0
    if (!this->CollectExports(visitor)) {
53
0
      return false;
54
0
    }
55
56
0
    if (generatedInterfaceRequired) {
57
0
      this->SetRequiredCMakeVersion(3, 0, 0);
58
0
    }
59
0
    this->GenerateExpectedTargetsCode(os, expectedTargets);
60
0
  }
61
62
  // Create all the imported targets.
63
0
  for (auto const& exp : this->Exports) {
64
0
    cmGeneratorTarget* gte = exp.Target;
65
0
    this->GenerateImportTargetCode(os, gte, this->GetExportTargetType(gte));
66
67
0
    gte->Target->AppendBuildInterfaceIncludes();
68
69
0
    ImportPropertyMap properties;
70
0
    if (!this->PopulateInterfaceProperties(gte, properties)) {
71
0
      return false;
72
0
    }
73
74
0
    ImportFileSetPropertyMap fsProperties;
75
0
    if (!this->PopulateFileSetInterfaceProperties(gte, fsProperties)) {
76
0
      return false;
77
0
    }
78
79
0
    this->PopulateInterfaceLinkLibrariesProperty(
80
0
      gte, cmGeneratorExpression::BuildInterface, properties);
81
82
0
    this->GenerateInterfaceProperties(gte, os, properties);
83
84
0
    this->GenerateTargetFileSets(gte, os, fsProperties);
85
0
  }
86
87
0
  std::string cxx_modules_name;
88
0
  if (this->ExportSet) {
89
0
    cxx_modules_name = this->ExportSet->GetName();
90
0
  } else {
91
0
    cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_512);
92
0
    constexpr std::size_t HASH_TRUNCATION = 12;
93
0
    for (auto const& target : this->Targets) {
94
0
      hasher.Append(target.Name);
95
0
    }
96
0
    cxx_modules_name = hasher.FinalizeHex().substr(0, HASH_TRUNCATION);
97
0
  }
98
99
0
  this->GenerateCxxModuleInformation(cxx_modules_name, os);
100
101
  // Generate import file content for each configuration.
102
0
  for (std::string const& c : this->Configurations) {
103
0
    this->GenerateImportConfig(os, c);
104
0
  }
105
106
  // Generate import file content for each configuration.
107
0
  for (std::string const& c : this->Configurations) {
108
0
    this->GenerateImportCxxModuleConfigTargetInclusion(cxx_modules_name, c);
109
0
  }
110
111
0
  this->GenerateMissingTargetsCheckCode(os);
112
113
0
  return true;
114
0
}
115
116
void cmExportBuildCMakeConfigGenerator::GenerateImportTargetsConfig(
117
  std::ostream& os, std::string const& config, std::string const& suffix)
118
0
{
119
0
  for (auto const& exp : this->Exports) {
120
0
    cmGeneratorTarget* target = exp.Target;
121
122
    // Collect import properties for this target.
123
0
    ImportPropertyMap properties;
124
125
0
    if (this->GetExportTargetType(target) !=
126
0
        cm::TargetType::INTERFACE_LIBRARY) {
127
0
      this->SetImportLocationProperty(config, suffix, target, properties);
128
0
    }
129
0
    if (!properties.empty()) {
130
      // Get the rest of the target details.
131
0
      if (this->GetExportTargetType(target) !=
132
0
          cm::TargetType::INTERFACE_LIBRARY) {
133
0
        this->SetImportDetailProperties(config, suffix, target, properties);
134
0
        this->SetImportLinkInterface(config, suffix,
135
0
                                     cmGeneratorExpression::BuildInterface,
136
0
                                     target, properties);
137
0
      }
138
139
      // TODO: PUBLIC_HEADER_LOCATION
140
      // This should wait until the build feature propagation stuff
141
      // is done.  Then this can be a propagated include directory.
142
      // this->GenerateImportProperty(config, te->HeaderGenerator,
143
      //                              properties);
144
145
      // Generate code in the export file.
146
0
      std::string importedXcFrameworkLocation = exp.XcFrameworkLocation;
147
0
      if (!importedXcFrameworkLocation.empty()) {
148
0
        importedXcFrameworkLocation = cmGeneratorExpression::Preprocess(
149
0
          importedXcFrameworkLocation,
150
0
          cmGeneratorExpression::PreprocessContext::BuildInterface);
151
0
        importedXcFrameworkLocation = cmGeneratorExpression::Evaluate(
152
0
          importedXcFrameworkLocation, exp.Target->GetLocalGenerator(), config,
153
0
          exp.Target, nullptr, exp.Target);
154
0
        if (!importedXcFrameworkLocation.empty() &&
155
0
            !cmSystemTools::FileIsFullPath(importedXcFrameworkLocation)) {
156
0
          importedXcFrameworkLocation =
157
0
            cmStrCat(this->LG->GetCurrentBinaryDirectory(), '/',
158
0
                     std::move(importedXcFrameworkLocation));
159
0
        }
160
0
      }
161
0
      this->GenerateImportPropertyCode(os, config, suffix, target, properties,
162
0
                                       importedXcFrameworkLocation);
163
0
    }
164
0
  }
165
0
}
166
167
std::string cmExportBuildCMakeConfigGenerator::GetFileSetDirectories(
168
  cmGeneratorTarget* gte, cmGeneratorFileSet const* fileSet,
169
  cmTargetExport const* /*te*/)
170
0
{
171
0
  std::vector<std::string> resultVector;
172
173
0
  auto configs =
174
0
    gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
175
176
0
  for (auto const& config : configs) {
177
0
    cm::GenEx::Context context(gte->LocalGenerator, config);
178
0
    auto directories = fileSet->GetDirectories(context, gte);
179
0
    bool const contextSensitive = directories.second;
180
181
0
    for (auto const& directory : directories.first) {
182
0
      auto dest = cmOutputConverter::EscapeForCMake(
183
0
        directory, cmOutputConverter::WrapQuotes::NoWrap);
184
185
0
      if (contextSensitive && configs.size() != 1) {
186
0
        resultVector.push_back(
187
0
          cmStrCat("\"$<$<CONFIG:", config, ">:", dest, ">\""));
188
0
      } else {
189
0
        resultVector.emplace_back(cmStrCat('"', dest, '"'));
190
0
      }
191
0
    }
192
0
  }
193
194
0
  return cmJoin(resultVector, " ");
195
0
}
196
197
std::string cmExportBuildCMakeConfigGenerator::GetFileSetFiles(
198
  cmGeneratorTarget* gte, cmGeneratorFileSet const* fileSet,
199
  cmTargetExport const* /*te*/)
200
0
{
201
0
  std::vector<std::string> resultVector;
202
203
0
  auto configs =
204
0
    gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
205
206
0
  for (auto const& config : configs) {
207
0
    cm::GenEx::Context context(gte->LocalGenerator, config);
208
209
0
    auto files = fileSet->GetFiles(context, gte);
210
0
    bool const contextSensitive = files.second;
211
212
0
    for (auto const& it : files.first) {
213
0
      for (auto const& filename : it.second) {
214
0
        auto escapedFile = cmOutputConverter::EscapeForCMake(
215
0
          filename, cmOutputConverter::WrapQuotes::NoWrap);
216
0
        if (contextSensitive && configs.size() != 1) {
217
0
          resultVector.push_back(
218
0
            cmStrCat("\"$<$<CONFIG:", config, ">:", escapedFile, ">\""));
219
0
        } else {
220
0
          resultVector.emplace_back(cmStrCat('"', escapedFile, '"'));
221
0
        }
222
0
      }
223
0
    }
224
225
0
    if (!(contextSensitive && configs.size() != 1)) {
226
0
      break;
227
0
    }
228
0
  }
229
230
0
  return cmJoin(resultVector, " ");
231
0
}
232
233
void cmExportBuildCMakeConfigGenerator::GenerateCxxModuleConfigInformation(
234
  std::string const& name, std::ostream& os) const
235
0
{
236
0
  char const* opt = "";
237
0
  if (this->Configurations.size() > 1) {
238
    // With more than one configuration, each individual file is optional.
239
0
    opt = " OPTIONAL";
240
0
  }
241
242
  // Generate import file content for each configuration.
243
0
  for (std::string c : this->Configurations) {
244
0
    if (c.empty()) {
245
0
      c = "noconfig";
246
0
    }
247
0
    os << "include(\"${CMAKE_CURRENT_LIST_DIR}/cxx-modules-" << name << '-'
248
0
       << c << ".cmake\"" << opt << ")\n";
249
0
  }
250
0
}
251
252
bool cmExportBuildCMakeConfigGenerator::
253
  GenerateImportCxxModuleConfigTargetInclusion(std::string const& name,
254
                                               std::string config) const
255
0
{
256
0
  auto cxx_modules_dirname = this->GetCxxModulesDirectory();
257
0
  if (cxx_modules_dirname.empty()) {
258
0
    return true;
259
0
  }
260
261
0
  if (config.empty()) {
262
0
    config = "noconfig";
263
0
  }
264
265
0
  std::string fileName =
266
0
    cmStrCat(this->FileDir, '/', cxx_modules_dirname, "/cxx-modules-", name,
267
0
             '-', config, ".cmake");
268
269
0
  cmGeneratedFileStream os(fileName, true);
270
0
  if (!os) {
271
0
    std::string se = cmSystemTools::GetLastSystemError();
272
0
    cmSystemTools::Error(
273
0
      cmStrCat("cannot write to file \"", fileName, "\": ", se));
274
0
    return false;
275
0
  }
276
0
  os.SetCopyIfDifferent(true);
277
278
0
  for (auto const* tgt : this->ExportedTargets) {
279
    // Only targets with C++ module sources will have a
280
    // collator-generated install script.
281
0
    if (!tgt->HaveCxx20ModuleSources()) {
282
0
      continue;
283
0
    }
284
285
0
    os << "include(\"${CMAKE_CURRENT_LIST_DIR}/target-"
286
0
       << tgt->GetFilesystemExportName() << '-' << config << ".cmake\")\n";
287
0
  }
288
289
0
  return true;
290
0
}