Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmExportInstallPackageInfoGenerator.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 "cmExportInstallPackageInfoGenerator.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/optional>
14
#include <cm/string_view>
15
#include <cmext/algorithm>
16
#include <cmext/string_view>
17
18
#include <cm3p/json/value.h>
19
20
#include "cmAlgorithms.h"
21
#include "cmExportSet.h"
22
#include "cmFileSet.h"
23
#include "cmGeneratorExpression.h"
24
#include "cmGeneratorTarget.h"
25
#include "cmInstallExportGenerator.h"
26
#include "cmInstallFileSetGenerator.h"
27
#include "cmList.h"
28
#include "cmLocalGenerator.h"
29
#include "cmMakefile.h"
30
#include "cmMessageType.h"
31
#include "cmOutputConverter.h"
32
#include "cmPackageInfoArguments.h"
33
#include "cmStateTypes.h"
34
#include "cmStringAlgorithms.h"
35
#include "cmSystemTools.h"
36
#include "cmTarget.h"
37
#include "cmTargetExport.h"
38
39
cmExportInstallPackageInfoGenerator::cmExportInstallPackageInfoGenerator(
40
  cmInstallExportGenerator* iegen, cmPackageInfoArguments arguments)
41
0
  : cmExportPackageInfoGenerator(std::move(arguments))
42
0
  , cmExportInstallFileGenerator(iegen)
43
0
{
44
0
}
Unexecuted instantiation: cmExportInstallPackageInfoGenerator::cmExportInstallPackageInfoGenerator(cmInstallExportGenerator*, cmPackageInfoArguments)
Unexecuted instantiation: cmExportInstallPackageInfoGenerator::cmExportInstallPackageInfoGenerator(cmInstallExportGenerator*, cmPackageInfoArguments)
45
46
std::string cmExportInstallPackageInfoGenerator::GetConfigImportFileGlob()
47
  const
48
0
{
49
0
  std::string glob = cmStrCat(this->FileBase, "@*", this->FileExt);
50
0
  return glob;
51
0
}
52
53
std::string const& cmExportInstallPackageInfoGenerator::GetExportName() const
54
0
{
55
0
  return this->GetPackageName();
56
0
}
57
58
bool cmExportInstallPackageInfoGenerator::GenerateMainFile(std::ostream& os)
59
0
{
60
0
  std::vector<cmTargetExport const*> allTargets;
61
0
  {
62
0
    auto visitor = [&](cmTargetExport const* te) { allTargets.push_back(te); };
63
64
0
    if (!this->CollectExports(visitor)) {
65
0
      return false;
66
0
    }
67
0
  }
68
69
0
  if (!this->CheckPackage()) {
70
0
    return false;
71
0
  }
72
73
0
  Json::Value root = this->GeneratePackageInfo();
74
0
  Json::Value& components = root["components"];
75
76
  // Compute the relative import prefix for the file
77
0
  std::string const& packagePath = this->GenerateImportPrefix();
78
0
  if (packagePath.empty()) {
79
0
    return false;
80
0
  }
81
0
  root["cps_path"] = packagePath;
82
83
  // Create all the imported targets.
84
0
  for (cmTargetExport const* te : allTargets) {
85
0
    cmGeneratorTarget* gt = te->Target;
86
0
    cmStateEnums::TargetType targetType = this->GetExportTargetType(te);
87
88
0
    Json::Value* const component =
89
0
      this->GenerateImportTarget(components, gt, targetType);
90
0
    if (!component) {
91
0
      return false;
92
0
    }
93
94
0
    ImportPropertyMap properties;
95
0
    if (!this->PopulateInterfaceProperties(te, properties)) {
96
0
      return false;
97
0
    }
98
0
    this->PopulateInterfaceLinkLibrariesProperty(
99
0
      gt, cmGeneratorExpression::InstallInterface, properties);
100
101
0
    if (targetType != cmStateEnums::INTERFACE_LIBRARY) {
102
0
      this->RequiresConfigFiles = true;
103
0
    }
104
105
    // De-duplicate include directories prior to generation.
106
0
    auto it = properties.find("INTERFACE_INCLUDE_DIRECTORIES");
107
0
    if (it != properties.end()) {
108
0
      auto list = cmList{ it->second };
109
0
      list = cmList{ list.begin(), cmRemoveDuplicates(list) };
110
0
      properties["INTERFACE_INCLUDE_DIRECTORIES"] = list.to_string();
111
0
    }
112
113
    // Set configuration-agnostic properties for component.
114
0
    this->GenerateInterfaceProperties(*component, gt, properties);
115
0
    if (!this->GenerateFileSetProperties(*component, gt, te, packagePath)) {
116
0
      return false;
117
0
    }
118
0
  }
119
120
0
  this->GeneratePackageRequires(root);
121
122
  // Write the primary packing information file.
123
0
  this->WritePackageInfo(root, os);
124
125
0
  bool result = true;
126
127
  // Generate an import file for each configuration.
128
0
  if (this->RequiresConfigFiles) {
129
0
    for (std::string const& c : this->Configurations) {
130
0
      if (!this->GenerateImportFileConfig(c)) {
131
0
        result = false;
132
0
      }
133
0
    }
134
0
  }
135
136
0
  return result;
137
0
}
138
139
void cmExportInstallPackageInfoGenerator::GenerateImportTargetsConfig(
140
  std::ostream& os, std::string const& config, std::string const& suffix)
141
0
{
142
0
  Json::Value root;
143
0
  root["name"] = this->GetPackageName();
144
0
  root["configuration"] = (config.empty() ? "noconfig" : config);
145
146
0
  std::string const& packagePath = this->GenerateImportPrefix();
147
148
0
  Json::Value& components = root["components"];
149
150
0
  for (auto const& te : this->GetExportSet()->GetTargetExports()) {
151
    // Collect import properties for this target.
152
0
    ImportPropertyMap properties;
153
0
    std::set<std::string> importedLocations;
154
155
0
    if (this->GetExportTargetType(te.get()) !=
156
0
        cmStateEnums::INTERFACE_LIBRARY) {
157
0
      this->PopulateImportProperties(config, suffix, te.get(), properties,
158
0
                                     importedLocations);
159
0
    }
160
161
0
    Json::Value component =
162
0
      this->GenerateInterfaceConfigProperties(suffix, properties);
163
0
    this->GenerateFileSetProperties(component, te->Target, te.get(),
164
0
                                    packagePath, config);
165
166
0
    if (!component.empty()) {
167
0
      components[te->Target->GetExportName()] = std::move(component);
168
0
    }
169
0
  }
170
171
0
  this->WritePackageInfo(root, os);
172
0
}
173
174
std::string cmExportInstallPackageInfoGenerator::GenerateImportPrefix() const
175
0
{
176
0
  std::string expDest = this->IEGen->GetDestination();
177
0
  if (cmSystemTools::FileIsFullPath(expDest)) {
178
0
    std::string const& installPrefix =
179
0
      this->IEGen->GetLocalGenerator()->GetMakefile()->GetSafeDefinition(
180
0
        "CMAKE_INSTALL_PREFIX");
181
0
    if (cmHasPrefix(expDest, installPrefix)) {
182
0
      auto n = installPrefix.length();
183
0
      while (n < expDest.length() && expDest[n] == '/') {
184
0
        ++n;
185
0
      }
186
0
      expDest = expDest.substr(n);
187
0
    } else {
188
0
      this->ReportError(
189
0
        cmStrCat("install(PACKAGE_INFO \"", this->GetExportName(),
190
0
                 "\" ...) specifies DESTINATION \"", expDest,
191
0
                 "\" which is not a subdirectory of the install prefix."));
192
0
      return {};
193
0
    }
194
0
  }
195
196
0
  if (expDest.empty()) {
197
0
    return this->GetInstallPrefix();
198
0
  }
199
0
  return cmStrCat(this->GetImportPrefixWithSlash(), expDest);
200
0
}
201
202
std::string cmExportInstallPackageInfoGenerator::InstallNameDir(
203
  cmGeneratorTarget const* target, std::string const& config)
204
0
{
205
0
  std::string install_name_dir;
206
207
0
  cmMakefile* mf = target->Target->GetMakefile();
208
0
  if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
209
0
    install_name_dir =
210
0
      target->GetInstallNameDirForInstallTree(config, "@prefix@");
211
0
  }
212
213
0
  return install_name_dir;
214
0
}
215
216
std::string cmExportInstallPackageInfoGenerator::GetCxxModulesDirectory() const
217
0
{
218
0
  return IEGen->GetCxxModuleDirectory();
219
0
}
220
221
cm::optional<std::string>
222
cmExportInstallPackageInfoGenerator::GetFileSetDirectory(
223
  cmGeneratorTarget* gte, cmTargetExport const* te, cmFileSet* fileSet,
224
  cm::optional<std::string> const& config)
225
0
{
226
0
  cmGeneratorExpression ge(*gte->Makefile->GetCMakeInstance());
227
0
  auto cge =
228
0
    ge.Parse(te->FileSetGenerators.at(fileSet->GetName())->GetDestination());
229
230
0
  std::string const unescapedDest =
231
0
    cge->Evaluate(gte->LocalGenerator, config.value_or(""), gte);
232
0
  bool const isConfigDependent = cge->GetHadContextSensitiveCondition();
233
234
0
  if (config && !isConfigDependent) {
235
0
    return {};
236
0
  }
237
0
  if (!config && isConfigDependent) {
238
0
    this->RequiresConfigFiles = true;
239
0
    return {};
240
0
  }
241
242
0
  std::string const& type = fileSet->GetType();
243
0
  if (config && (type == "CXX_MODULES"_s)) {
244
    // C++ modules do not support interface file sets which are dependent
245
    // upon the configuration.
246
0
    cmMakefile* mf = gte->LocalGenerator->GetMakefile();
247
0
    std::ostringstream e;
248
0
    e << "The \"" << gte->GetName() << "\" target's interface file set \""
249
0
      << fileSet->GetName() << "\" of type \"" << type
250
0
      << "\" contains context-sensitive base file entries which is not "
251
0
         "supported.";
252
0
    mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
253
0
    return {};
254
0
  }
255
256
0
  cm::optional<std::string> dest = cmOutputConverter::EscapeForCMake(
257
0
    unescapedDest, cmOutputConverter::WrapQuotes::NoWrap);
258
259
0
  if (!cmSystemTools::FileIsFullPath(unescapedDest)) {
260
0
    dest = cmStrCat("@prefix@/"_s, *dest);
261
0
  }
262
263
0
  return dest;
264
0
}
265
266
bool cmExportInstallPackageInfoGenerator::GenerateFileSetProperties(
267
  Json::Value& component, cmGeneratorTarget* gte, cmTargetExport const* te,
268
  std::string const& packagePath, cm::optional<std::string> config)
269
0
{
270
0
  bool hasModules = false;
271
0
  std::set<std::string> seenIncludeDirectories;
272
0
  for (auto const& name : gte->Target->GetAllInterfaceFileSets()) {
273
0
    cmFileSet* fileSet = gte->Target->GetFileSet(name);
274
275
0
    if (!fileSet) {
276
0
      gte->Makefile->IssueMessage(
277
0
        MessageType::FATAL_ERROR,
278
0
        cmStrCat("File set \"", name,
279
0
                 "\" is listed in interface file sets of ", gte->GetName(),
280
0
                 " but has not been created"));
281
0
      return false;
282
0
    }
283
284
0
    cm::optional<std::string> const& fileSetDirectory =
285
0
      this->GetFileSetDirectory(gte, te, fileSet, config);
286
287
0
    if (fileSet->GetType() == "HEADERS"_s) {
288
0
      if (fileSetDirectory &&
289
0
          !cm::contains(seenIncludeDirectories, *fileSetDirectory)) {
290
0
        component["includes"].append(*fileSetDirectory);
291
0
        seenIncludeDirectories.insert(*fileSetDirectory);
292
0
      }
293
0
    } else if (fileSet->GetType() == "CXX_MODULES"_s) {
294
0
      hasModules = true;
295
0
      this->RequiresConfigFiles = true;
296
0
    }
297
0
  }
298
299
0
  if (hasModules && config) {
300
0
    std::string const manifestPath =
301
0
      this->GenerateCxxModules(component, gte, packagePath, *config);
302
0
    if (!manifestPath.empty()) {
303
0
      this->ConfigCxxModuleFiles[*config] =
304
0
        cmStrCat(this->FileDir, '/', manifestPath);
305
0
    }
306
0
  }
307
0
  return true;
308
0
}