Coverage Report

Created: 2026-06-15 07:03

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