Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmExportSbomGenerator.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 "cmExportSbomGenerator.h"
4
5
#include <array>
6
#include <map>
7
#include <set>
8
#include <string>
9
#include <utility>
10
#include <vector>
11
12
#include <cm/optional>
13
#include <cmext/algorithm>
14
15
#include "cmArgumentParserTypes.h"
16
#include "cmFindPackageStack.h"
17
#include "cmGeneratorExpression.h"
18
#include "cmGeneratorTarget.h"
19
#include "cmList.h"
20
#include "cmMakefile.h"
21
#include "cmMessageType.h"
22
#include "cmSbomArguments.h"
23
#include "cmSbomObject.h"
24
#include "cmSpdx.h"
25
#include "cmSpdxSerializer.h"
26
#include "cmStateTypes.h"
27
#include "cmStringAlgorithms.h"
28
#include "cmSystemTools.h"
29
#include "cmTarget.h"
30
#include "cmValue.h"
31
32
cmSpdxPackage::PurposeId GetPurpose(cmStateEnums::TargetType type)
33
0
{
34
0
  switch (type) {
35
0
    case cmStateEnums::TargetType::EXECUTABLE:
36
0
      return cmSpdxPackage::PurposeId::APPLICATION;
37
0
    case cmStateEnums::TargetType::STATIC_LIBRARY:
38
0
    case cmStateEnums::TargetType::SHARED_LIBRARY:
39
0
    case cmStateEnums::TargetType::MODULE_LIBRARY:
40
0
    case cmStateEnums::TargetType::OBJECT_LIBRARY:
41
0
    case cmStateEnums::TargetType::INTERFACE_LIBRARY:
42
0
      return cmSpdxPackage::PurposeId::LIBRARY;
43
0
    case cmStateEnums::TargetType::UTILITY:
44
0
      return cmSpdxPackage::PurposeId::SOURCE;
45
0
    case cmStateEnums::TargetType::GLOBAL_TARGET:
46
0
    case cmStateEnums::TargetType::UNKNOWN_LIBRARY:
47
0
    default:
48
0
      return cmSpdxPackage::PurposeId::ARCHIVE;
49
0
  }
50
0
}
51
52
cmExportSbomGenerator::cmExportSbomGenerator(cmSbomArguments args)
53
0
  : PackageName(std::move(args.PackageName))
54
0
  , PackageVersion(std::move(args.Version))
55
0
  , PackageDescription(std::move(args.Description))
56
0
  , PackageWebsite(std::move(args.Website))
57
0
  , PackageLicense(std::move(args.License))
58
0
  , PackageFormat(args.GetFormat())
59
0
{
60
0
}
61
62
bool cmExportSbomGenerator::GenerateImportFile(std::ostream& os)
63
0
{
64
0
  return this->GenerateMainFile(os);
65
0
}
66
67
void cmExportSbomGenerator::WriteSbom(cmSbomDocument& doc,
68
                                      std::ostream& os) const
69
0
{
70
0
  switch (this->PackageFormat) {
71
0
    case cmSbomArguments::SbomFormat::SPDX_3_0_JSON:
72
0
      cmSpdxSerializer{}.WriteSbom(os, cmSbomObject(doc));
73
0
      break;
74
0
    case cmSbomArguments::SbomFormat::NONE:
75
0
      break;
76
0
  }
77
0
}
78
79
bool cmExportSbomGenerator::AddPackageInformation(
80
  cmSpdxPackage& artifact, std::string const& name,
81
  cmPackageInformation const& package) const
82
0
{
83
0
  if (name.empty()) {
84
0
    return false;
85
0
  }
86
87
0
  cmSpdxOrganization org;
88
0
  org.Name = name;
89
0
  artifact.OriginatedBy.emplace_back(std::move(org));
90
91
0
  if (package.Description) {
92
0
    artifact.Description = *package.Description;
93
0
  }
94
95
0
  if (package.Version) {
96
0
    artifact.PackageVersion = *package.Version;
97
0
  }
98
99
0
  if (package.PackageUrl) {
100
0
    artifact.PackageUrl = *package.PackageUrl;
101
0
  }
102
103
0
  if (package.License) {
104
0
    artifact.CopyrightText = *package.License;
105
0
  }
106
107
0
  artifact.BuiltTime = cmSystemTools::GetCurrentDateTime("%FT%TZ");
108
0
  cmSpdxExternalRef externalRef;
109
0
  externalRef.Locator = cmStrCat("cmake:find_package(", name, ")");
110
0
  externalRef.ExternalRefType = "buildSystem";
111
0
  return true;
112
0
}
113
114
cmSpdxDocument cmExportSbomGenerator::GenerateSbom() const
115
0
{
116
0
  cmSpdxTool tool;
117
0
  tool.SpdxId = "CMake#Agent";
118
0
  tool.Name = "CMake";
119
120
0
  cmSpdxCreationInfo ci;
121
0
  ci.Created = cmSystemTools::GetCurrentDateTime("%FT%TZ");
122
0
  ci.CreatedUsing = { tool };
123
0
  ci.Comment = "This SBOM was generated from the CMakeLists.txt File";
124
125
0
  cmSpdxDocument proj;
126
0
  proj.Name = PackageName;
127
0
  proj.SpdxId = cmStrCat(PackageName, "#SPDXDocument");
128
0
  proj.ProfileConformance = { "core", "software" };
129
0
  proj.CreationInfo = ci;
130
131
0
  if (!this->PackageDescription.empty()) {
132
0
    proj.Description = this->PackageDescription;
133
0
  }
134
135
0
  if (!this->PackageLicense.empty()) {
136
0
    proj.DataLicense = this->PackageLicense;
137
0
  }
138
139
0
  return proj;
140
0
}
141
142
cmSpdxPackage cmExportSbomGenerator::GenerateImportTarget(
143
  cmGeneratorTarget const* target) const
144
0
{
145
0
  cmSpdxPackage package;
146
0
  package.SpdxId = cmStrCat(target->GetName(), "#Package");
147
0
  package.Name = target->GetName();
148
0
  package.PrimaryPurpose = GetPurpose(target->GetType());
149
150
0
  cmSpdxExternalRef buildSystem;
151
0
  buildSystem.Locator = "CMake#Agent";
152
0
  buildSystem.ExternalRefType = "buildSystem";
153
0
  buildSystem.Comment = "Build System used for this target";
154
0
  package.ExternalRef = { buildSystem };
155
156
0
  if (!this->PackageVersion.empty()) {
157
0
    package.PackageVersion = this->PackageVersion;
158
0
  }
159
160
0
  if (!this->PackageWebsite.empty()) {
161
0
    package.Homepage = this->PackageWebsite;
162
0
  }
163
164
0
  if (!this->PackageUrl.empty()) {
165
0
    package.DownloadLocation = this->PackageUrl;
166
0
  }
167
168
0
  return package;
169
0
}
170
171
void cmExportSbomGenerator::GenerateLinkProperties(
172
  cmSbomDocument& doc, cmSpdxDocument* project, std::string const& libraries,
173
  TargetProperties const& current,
174
  std::vector<TargetProperties> const& allTargets) const
175
0
{
176
0
  auto itProp = current.Properties.find(libraries);
177
0
  if (itProp == current.Properties.end()) {
178
0
    return;
179
0
  }
180
181
0
  std::map<std::string, std::vector<std::string>> allowList = { { "LINK_ONLY",
182
0
                                                                  {} } };
183
0
  std::string interfaceLinkLibraries;
184
0
  if (!cmGeneratorExpression::ForbidGeneratorExpressions(
185
0
        current.Target, itProp->first, itProp->second, interfaceLinkLibraries,
186
0
        allowList)) {
187
0
    return;
188
0
  }
189
190
0
  auto makeRel = [&](char const* desc) {
191
0
    cmSpdxRelationship r;
192
0
    r.RelationshipType = cmSpdxRelationship::RelationshipTypeId::DEPENDS_ON;
193
0
    r.Description = desc;
194
0
    r.From = current.Package;
195
0
    return r;
196
0
  };
197
198
0
  auto linkLibraries = makeRel("Linked Libraries");
199
0
  auto linkRequires = makeRel("Required Runtime Libraries");
200
0
  auto buildRequires = makeRel("Required Build-Time Libraries");
201
202
0
  auto addArtifact =
203
0
    [&](std::string const& name) -> std::pair<bool, cmSpdxPackage const*> {
204
0
    auto it = this->LinkTargets.find(name);
205
0
    if (it != this->LinkTargets.end()) {
206
0
      LinkInfo const& linkInfo = it->second;
207
0
      if (linkInfo.Package.empty()) {
208
0
        for (auto const& t : allTargets) {
209
0
          if (t.Target->GetName() == linkInfo.Component) {
210
0
            return { true, t.Package };
211
0
          }
212
0
        }
213
0
      }
214
0
      std::string pkgName =
215
0
        cmStrCat(linkInfo.Package, ":", linkInfo.Component);
216
0
      cmSpdxPackage pkg;
217
0
      pkg.Name = pkgName;
218
0
      pkg.SpdxId = cmStrCat(pkgName, "#Package");
219
0
      if (!linkInfo.Package.empty()) {
220
0
        auto const& pkgIt = this->Requirements.find(linkInfo.Package);
221
0
        if (pkgIt != this->Requirements.end() &&
222
0
            pkgIt->second.Components.count(linkInfo.Component) > 0) {
223
0
          this->AddPackageInformation(pkg, pkgIt->first, pkgIt->second);
224
0
        }
225
0
      }
226
227
0
      return { true, insert_back(project->Elements, std::move(pkg)) };
228
0
    }
229
230
0
    cmSpdxPackage pkg;
231
0
    pkg.SpdxId = cmStrCat(name, "#Package");
232
0
    pkg.Name = name;
233
0
    return { false, insert_back(project->Elements, std::move(pkg)) };
234
0
  };
235
236
0
  auto handleDependencies = [&](std::vector<std::string> const& names,
237
0
                                cmSpdxRelationship& internalDeps,
238
0
                                cmSpdxRelationship& externalDeps) {
239
0
    for (auto const& n : names) {
240
0
      auto res = addArtifact(n);
241
0
      if (!res.second) {
242
0
        continue;
243
0
      }
244
245
0
      if (res.first) {
246
0
        internalDeps.To.push_back(res.second);
247
0
      } else {
248
0
        externalDeps.To.push_back(res.second);
249
0
      }
250
0
    }
251
0
  };
252
253
0
  handleDependencies(allowList["LINK_ONLY"], linkLibraries, linkRequires);
254
0
  handleDependencies(cmList{ interfaceLinkLibraries }, linkLibraries,
255
0
                     buildRequires);
256
257
0
  if (!linkLibraries.To.empty()) {
258
0
    insert_back(doc.Graph, std::move(linkLibraries));
259
0
  }
260
0
  if (!linkRequires.To.empty()) {
261
0
    insert_back(doc.Graph, std::move(linkRequires));
262
0
  }
263
0
  if (!buildRequires.To.empty()) {
264
0
    insert_back(doc.Graph, std::move(buildRequires));
265
0
  }
266
0
}
267
268
bool cmExportSbomGenerator::GenerateProperties(
269
  cmSbomDocument& doc, cmSpdxDocument* proj, TargetProperties const& current,
270
  std::vector<TargetProperties> const& allTargets) const
271
0
{
272
0
  this->GenerateLinkProperties(doc, proj, "LINK_LIBRARIES", current,
273
0
                               allTargets);
274
0
  this->GenerateLinkProperties(doc, proj, "INTERFACE_LINK_LIBRARIES", current,
275
0
                               allTargets);
276
0
  return true;
277
0
}
278
279
bool cmExportSbomGenerator::PopulateLinkLibrariesProperty(
280
  cmGeneratorTarget const* target,
281
  cmGeneratorExpression::PreprocessContext preprocessRule,
282
  ImportPropertyMap& properties)
283
0
{
284
0
  static std::array<std::string, 3> const linkIfaceProps = {
285
0
    { "LINK_LIBRARIES", "LINK_LIBRARIES_DIRECT",
286
0
      "LINK_LIBRARIES_DIRECT_EXCLUDE" }
287
0
  };
288
0
  bool hadLINK_LIBRARIES = false;
289
0
  for (std::string const& linkIfaceProp : linkIfaceProps) {
290
0
    if (cmValue input = target->GetProperty(linkIfaceProp)) {
291
0
      std::string prepro =
292
0
        cmGeneratorExpression::Preprocess(*input, preprocessRule);
293
0
      if (!prepro.empty()) {
294
0
        this->ResolveTargetsInGeneratorExpressions(prepro, target,
295
0
                                                   ReplaceFreeTargets);
296
0
        properties[linkIfaceProp] = prepro;
297
0
        hadLINK_LIBRARIES = true;
298
0
      }
299
0
    }
300
0
  }
301
0
  return hadLINK_LIBRARIES;
302
0
}
303
304
bool cmExportSbomGenerator::NoteLinkedTarget(
305
  cmGeneratorTarget const* target, std::string const& linkedName,
306
  cmGeneratorTarget const* linkedTarget)
307
0
{
308
0
  if (cm::contains(this->ExportedTargets, linkedTarget)) {
309
0
    this->LinkTargets.emplace(linkedName,
310
0
                              LinkInfo{ "", linkedTarget->GetExportName() });
311
0
    return true;
312
0
  }
313
314
0
  if (linkedTarget->IsImported()) {
315
0
    using Package = cm::optional<std::pair<std::string, cmPackageInformation>>;
316
0
    auto pkgInfo = [](cmTarget* t) -> Package {
317
0
      cmFindPackageStack pkgStack = t->GetFindPackageStack();
318
0
      if (!pkgStack.Empty()) {
319
0
        return std::make_pair(pkgStack.Top().Name, pkgStack.Top().PackageInfo);
320
0
      }
321
0
      std::string const pkgName =
322
0
        t->GetSafeProperty("EXPORT_FIND_PACKAGE_NAME");
323
0
      if (pkgName.empty()) {
324
0
        return cm::nullopt;
325
0
      }
326
0
      cmPackageInformation package;
327
0
      return std::make_pair(pkgName, package);
328
0
    }(linkedTarget->Target);
329
330
0
    if (!pkgInfo) {
331
0
      target->Makefile->IssueMessage(
332
0
        MessageType::AUTHOR_WARNING,
333
0
        cmStrCat("Target \"", target->GetName(),
334
0
                 "\" references imported target \"", linkedName,
335
0
                 "\" which does not come from any known package."));
336
0
      return false;
337
0
    }
338
339
0
    std::string const& pkgName = pkgInfo->first;
340
0
    auto const& prefix = cmStrCat(pkgName, "::");
341
0
    std::string component;
342
0
    if (!cmHasPrefix(linkedName, prefix)) {
343
0
      component = linkedName;
344
0
    } else {
345
0
      component = linkedName.substr(prefix.length());
346
0
    }
347
0
    this->LinkTargets.emplace(linkedName, LinkInfo{ pkgName, component });
348
0
    cmPackageInformation& req =
349
0
      this->Requirements.insert(std::move(*pkgInfo)).first->second;
350
0
    req.Components.emplace(std::move(component));
351
352
0
    return true;
353
0
  }
354
355
  // Target belongs to another export from this build.
356
0
  auto const& exportInfo = this->FindExportInfo(linkedTarget);
357
0
  if (exportInfo.Namespaces.size() == 1 && exportInfo.Sets.size() == 1) {
358
0
    auto const& linkNamespace = *exportInfo.Namespaces.begin();
359
0
    if (!cmHasSuffix(linkNamespace, "::")) {
360
0
      target->Makefile->IssueMessage(
361
0
        MessageType::AUTHOR_WARNING,
362
0
        cmStrCat("Target \"", target->GetName(), "\" references target \"",
363
0
                 linkedName,
364
0
                 "\", which does not use the standard namespace separator. "
365
0
                 "This is not allowed."));
366
0
    }
367
368
0
    std::string pkgName{ linkNamespace.data(), linkNamespace.size() - 2 };
369
0
    std::string component = linkedTarget->GetExportName();
370
0
    if (pkgName == this->GetPackageName()) {
371
0
      this->LinkTargets.emplace(linkedName, LinkInfo{ "", component });
372
0
    } else {
373
0
      this->LinkTargets.emplace(linkedName, LinkInfo{ pkgName, component });
374
0
      this->Requirements[pkgName].Components.emplace(std::move(component));
375
0
    }
376
0
    return true;
377
0
  }
378
379
  // Target belongs to multiple namespaces or multiple export sets.
380
  // cmExportFileGenerator::HandleMissingTarget should have complained about
381
  // this already.
382
0
  return false;
383
0
}