Coverage Report

Created: 2026-03-12 06:35

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