/src/CMake/Source/cmSbomBuilder.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 "cmSbomBuilder.h" |
4 | | |
5 | | #include <algorithm> |
6 | | #include <array> |
7 | | #include <functional> |
8 | | #include <map> |
9 | | #include <memory> |
10 | | #include <set> |
11 | | #include <sstream> |
12 | | #include <string> |
13 | | #include <utility> |
14 | | #include <vector> |
15 | | |
16 | | #include <cm/optional> |
17 | | #include <cmext/algorithm> |
18 | | #include <cmext/string_view> |
19 | | |
20 | | #include "cmArgumentParserTypes.h" |
21 | | #include "cmDiagnostics.h" |
22 | | #include "cmExportFileGenerator.h" |
23 | | #include "cmExportSet.h" |
24 | | #include "cmFindPackageStack.h" |
25 | | #include "cmGeneratorExpression.h" |
26 | | #include "cmGeneratorTarget.h" |
27 | | #include "cmList.h" |
28 | | #include "cmLocalGenerator.h" |
29 | | #include "cmMakefile.h" |
30 | | #include "cmMessageType.h" |
31 | | #include "cmSbomArguments.h" |
32 | | #include "cmSbomObject.h" |
33 | | #include "cmSpdx.h" |
34 | | #include "cmSpdxSerializer.h" |
35 | | #include "cmStateTypes.h" |
36 | | #include "cmStringAlgorithms.h" |
37 | | #include "cmSystemTools.h" |
38 | | #include "cmTarget.h" |
39 | | #include "cmTargetExport.h" |
40 | | #include "cmValue.h" |
41 | | |
42 | | cmSpdxPackage::PurposeId GetPurpose(cmStateEnums::TargetType type) |
43 | 0 | { |
44 | 0 | switch (type) { |
45 | 0 | case cmStateEnums::TargetType::EXECUTABLE: |
46 | 0 | return cmSpdxPackage::PurposeId::APPLICATION; |
47 | 0 | case cmStateEnums::TargetType::STATIC_LIBRARY: |
48 | 0 | case cmStateEnums::TargetType::SHARED_LIBRARY: |
49 | 0 | case cmStateEnums::TargetType::MODULE_LIBRARY: |
50 | 0 | case cmStateEnums::TargetType::OBJECT_LIBRARY: |
51 | 0 | case cmStateEnums::TargetType::INTERFACE_LIBRARY: |
52 | 0 | return cmSpdxPackage::PurposeId::LIBRARY; |
53 | 0 | case cmStateEnums::TargetType::UTILITY: |
54 | 0 | return cmSpdxPackage::PurposeId::SOURCE; |
55 | 0 | case cmStateEnums::TargetType::GLOBAL_TARGET: |
56 | 0 | case cmStateEnums::TargetType::UNKNOWN_LIBRARY: |
57 | 0 | default: |
58 | 0 | return cmSpdxPackage::PurposeId::ARCHIVE; |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | | cmSbomBuilder::cmSbomBuilder(cmSbomArguments args, |
63 | | std::vector<cmExportSet*> exportSets, |
64 | | cmLocalGenerator* lg) |
65 | 0 | : LocalGenerator(lg) |
66 | 0 | , ExportSets(std::move(exportSets)) |
67 | 0 | , PackageName(std::move(args.PackageName)) |
68 | 0 | , Namespace(cmStrCat(this->PackageName, "::"_s)) |
69 | 0 | , PackageVersion(std::move(args.Version)) |
70 | 0 | , PackageDescription(std::move(args.Description)) |
71 | 0 | , PackageWebsite(std::move(args.Website)) |
72 | 0 | , PackageUrl(std::move(args.PackageUrl)) |
73 | 0 | , PackageLicense(std::move(args.License)) |
74 | 0 | , PackageFormat(args.GetFormat()) |
75 | 0 | { |
76 | 0 | } |
77 | | |
78 | | std::set<cmGeneratorTarget const*> cmSbomBuilder::CollectTargets() const |
79 | 0 | { |
80 | 0 | std::set<cmGeneratorTarget const*> targets; |
81 | 0 | for (cmExportSet* exportSet : this->ExportSets) { |
82 | 0 | for (auto const& te : exportSet->GetTargetExports()) { |
83 | 0 | if (cmGeneratorTarget const* gt = |
84 | 0 | this->LocalGenerator->FindGeneratorTargetToUse(te->TargetName)) { |
85 | 0 | targets.emplace(gt); |
86 | 0 | } |
87 | 0 | } |
88 | 0 | } |
89 | 0 | return targets; |
90 | 0 | } |
91 | | |
92 | | void cmSbomBuilder::Compute(cmLocalGenerator* lg) |
93 | 0 | { |
94 | 0 | this->LocalGenerator = lg; |
95 | 0 | if (!lg) { |
96 | 0 | return; |
97 | 0 | } |
98 | 0 | for (cmExportSet* es : this->ExportSets) { |
99 | 0 | es->Compute(lg); |
100 | 0 | } |
101 | | // Populate the cache now (rather than at Generate time) so peer SBOMs can |
102 | | // query CoversTarget() during their own NoteLinkedTarget walks. |
103 | 0 | this->SbomTargets = this->CollectTargets(); |
104 | 0 | } |
105 | | |
106 | | bool cmSbomBuilder::CoversTarget(cmGeneratorTarget const* target) const |
107 | 0 | { |
108 | 0 | return cm::contains(this->SbomTargets, target); |
109 | 0 | } |
110 | | |
111 | | bool cmSbomBuilder::CoversExportSet(cmExportSet const* set) const |
112 | 0 | { |
113 | 0 | return std::find(this->ExportSets.cbegin(), this->ExportSets.cend(), set) != |
114 | 0 | this->ExportSets.cend(); |
115 | 0 | } |
116 | | |
117 | | bool cmSbomBuilder::GenerateForTargets( |
118 | | std::ostream& os, std::string const& config, |
119 | | cmGeneratorExpression::PreprocessContext preprocessContext) |
120 | 0 | { |
121 | 0 | cmSbomDocument doc; |
122 | 0 | doc.Graph.reserve(512); |
123 | |
|
124 | 0 | cmSpdxCreationInfo const* ci = |
125 | 0 | insert_back(doc.Graph, this->GenerateCreationInfo()); |
126 | 0 | cmSpdxDocument* project = insert_back(doc.Graph, this->GenerateSbom(ci)); |
127 | 0 | std::vector<TargetProperties> targetProps; |
128 | 0 | targetProps.reserve(this->SbomTargets.size()); |
129 | |
|
130 | 0 | for (cmGeneratorTarget const* target : this->SbomTargets) { |
131 | 0 | ImportPropertyMap properties; |
132 | 0 | this->PopulateLinkLibrariesProperty(target, preprocessContext, properties); |
133 | 0 | this->PopulateInterfaceLinkLibrariesProperty(target, preprocessContext, |
134 | 0 | properties); |
135 | 0 | targetProps.push_back(TargetProperties{ |
136 | 0 | insert_back(project->RootElements, |
137 | 0 | this->GenerateImportTarget(ci, target)), |
138 | 0 | target, |
139 | 0 | std::move(properties), |
140 | 0 | }); |
141 | 0 | } |
142 | |
|
143 | 0 | bool status = true; |
144 | 0 | for (TargetProperties const& target : targetProps) { |
145 | 0 | status &= |
146 | 0 | this->GenerateProperties(doc, project, ci, target, targetProps, config); |
147 | 0 | } |
148 | 0 | if (status) { |
149 | 0 | this->WriteSbom(doc, os); |
150 | 0 | } |
151 | 0 | return status; |
152 | 0 | } |
153 | | |
154 | | void cmSbomBuilder::WriteSbom(cmSbomDocument& doc, std::ostream& os) const |
155 | 0 | { |
156 | 0 | switch (this->PackageFormat) { |
157 | 0 | case cmSbomArguments::SbomFormat::SPDX_3_0_JSON: |
158 | 0 | cmSpdxSerializer{}.WriteSbom(os, cmSbomObject(doc)); |
159 | 0 | break; |
160 | 0 | case cmSbomArguments::SbomFormat::NONE: |
161 | 0 | break; |
162 | 0 | } |
163 | 0 | } |
164 | | |
165 | | bool cmSbomBuilder::AddPackageInformation( |
166 | | cmSpdxPackage& artifact, std::string const& name, |
167 | | cmPackageInformation const& package) const |
168 | 0 | { |
169 | 0 | if (name.empty()) { |
170 | 0 | return false; |
171 | 0 | } |
172 | | |
173 | 0 | cmSpdxOrganization org; |
174 | 0 | org.SpdxId = cmStrCat("urn:", name, "#Organization"); |
175 | 0 | org.Name = name; |
176 | 0 | org.CreationInfo = artifact.CreationInfo; |
177 | 0 | artifact.OriginatedBy.emplace_back(std::move(org)); |
178 | |
|
179 | 0 | if (package.Description) { |
180 | 0 | artifact.Description = *package.Description; |
181 | 0 | } |
182 | |
|
183 | 0 | if (package.Version) { |
184 | 0 | artifact.PackageVersion = *package.Version; |
185 | 0 | } |
186 | |
|
187 | 0 | if (package.PackageUrl) { |
188 | 0 | artifact.PackageUrl = *package.PackageUrl; |
189 | 0 | } |
190 | |
|
191 | 0 | if (package.License) { |
192 | 0 | artifact.CopyrightText = *package.License; |
193 | 0 | } |
194 | |
|
195 | 0 | artifact.BuiltTime = cmSystemTools::GetCurrentDateTime("%FT%TZ"); |
196 | 0 | cmSpdxExternalRef externalRef; |
197 | 0 | externalRef.Locator = cmStrCat("cmake:find_package(", name, ")"); |
198 | 0 | externalRef.ExternalRefType = "buildSystem"; |
199 | 0 | return true; |
200 | 0 | } |
201 | | |
202 | | cmSpdxCreationInfo cmSbomBuilder::GenerateCreationInfo() const |
203 | 0 | { |
204 | 0 | cmSpdxCreationInfo ci; |
205 | 0 | ci.SpdxId = "_:Build#CreationInfo"; |
206 | 0 | ci.Created = cmSystemTools::GetCurrentDateTime("%FT%TZ"); |
207 | 0 | ci.CreatedBy = { "https://gitlab.kitware.com/cmake/cmake" }; |
208 | 0 | ci.Comment = "This SBOM was generated from the CMakeLists.txt File"; |
209 | 0 | ci.SpecVersion = "3.0.1"; |
210 | 0 | return ci; |
211 | 0 | } |
212 | | |
213 | | cmSpdxDocument cmSbomBuilder::GenerateSbom(cmSpdxCreationInfo const* ci) const |
214 | 0 | { |
215 | 0 | cmSpdxDocument proj; |
216 | 0 | proj.Name = PackageName; |
217 | 0 | proj.SpdxId = cmStrCat("urn:", PackageName, "#SPDXDocument"); |
218 | 0 | proj.ProfileConformance = { "core", "software" }; |
219 | 0 | proj.CreationInfo = ci; |
220 | |
|
221 | 0 | if (!this->PackageDescription.empty()) { |
222 | 0 | proj.Description = this->PackageDescription; |
223 | 0 | } |
224 | |
|
225 | 0 | if (!this->PackageLicense.empty()) { |
226 | 0 | proj.DataLicense = this->PackageLicense; |
227 | 0 | } |
228 | |
|
229 | 0 | return proj; |
230 | 0 | } |
231 | | |
232 | | cmSpdxPackage cmSbomBuilder::GenerateImportTarget( |
233 | | cmSpdxCreationInfo const* ci, cmGeneratorTarget const* target) const |
234 | 0 | { |
235 | 0 | cmSpdxPackage package; |
236 | 0 | package.SpdxId = cmStrCat("urn:", target->GetName(), "#Package"); |
237 | 0 | package.Name = target->GetName(); |
238 | 0 | package.PrimaryPurpose = GetPurpose(target->GetType()); |
239 | 0 | package.CreationInfo = ci; |
240 | |
|
241 | 0 | if (!this->PackageVersion.empty()) { |
242 | 0 | package.PackageVersion = this->PackageVersion; |
243 | 0 | } |
244 | |
|
245 | 0 | if (!this->PackageWebsite.empty()) { |
246 | 0 | package.Homepage = this->PackageWebsite; |
247 | 0 | } |
248 | |
|
249 | 0 | if (!this->PackageUrl.empty()) { |
250 | 0 | package.DownloadLocation = this->PackageUrl; |
251 | 0 | } |
252 | |
|
253 | 0 | return package; |
254 | 0 | } |
255 | | |
256 | | bool cmSbomBuilder::GenerateLinkProperties( |
257 | | cmSbomDocument& doc, cmSpdxDocument* project, cmSpdxCreationInfo const* ci, |
258 | | std::string const& libraries, TargetProperties const& current, |
259 | | std::vector<TargetProperties> const& allTargets, |
260 | | std::string const& config) const |
261 | 0 | { |
262 | 0 | auto itProp = current.Properties.find(libraries); |
263 | 0 | if (itProp == current.Properties.end()) { |
264 | 0 | return true; |
265 | 0 | } |
266 | | |
267 | 0 | cmGeneratorExpression ge(*current.Target->Makefile->GetCMakeInstance()); |
268 | 0 | std::unique_ptr<cmCompiledGeneratorExpression> cge = |
269 | 0 | ge.Parse(itProp->second); |
270 | 0 | std::string evaluatedLibraries = |
271 | 0 | cge->Evaluate(current.Target->GetLocalGenerator(), config, current.Target); |
272 | |
|
273 | 0 | if (cge->GetHadHeadSensitiveCondition()) { |
274 | 0 | current.Target->Makefile->IssueMessage( |
275 | 0 | MessageType::FATAL_ERROR, |
276 | 0 | cmStrCat("Property \"", libraries, "\" of target \"", |
277 | 0 | current.Target->GetName(), |
278 | 0 | "\" contains a generator expression that is not allowed for " |
279 | 0 | "SBOM generation.")); |
280 | 0 | return false; |
281 | 0 | } |
282 | | |
283 | 0 | auto makeRel = [&](char const* id, char const* desc) { |
284 | 0 | cmSpdxRelationship r; |
285 | 0 | r.SpdxId = cmStrCat("urn:", id, "#Relationship"); |
286 | 0 | r.RelationshipType = cmSpdxRelationship::RelationshipTypeId::DEPENDS_ON; |
287 | 0 | r.Description = desc; |
288 | 0 | r.From = current.Package; |
289 | 0 | r.CreationInfo = ci; |
290 | 0 | return r; |
291 | 0 | }; |
292 | |
|
293 | 0 | auto linkLibraries = makeRel("Static", "Linked Libraries"); |
294 | 0 | auto linkRequires = makeRel("Dynamic", "Required Runtime Libraries"); |
295 | 0 | auto buildRequires = makeRel("Shared", "Required Build-Time Libraries"); |
296 | |
|
297 | 0 | auto addArtifact = |
298 | 0 | [&](std::string const& name) -> std::pair<bool, cmSpdxPackage const*> { |
299 | 0 | auto it = this->LinkTargets.find(name); |
300 | 0 | if (it != this->LinkTargets.end()) { |
301 | 0 | LinkInfo const& linkInfo = it->second; |
302 | 0 | if (linkInfo.Package.empty()) { |
303 | 0 | for (auto const& t : allTargets) { |
304 | 0 | if (t.Target->GetName() == linkInfo.Component) { |
305 | 0 | return { true, t.Package }; |
306 | 0 | } |
307 | 0 | } |
308 | 0 | } |
309 | 0 | std::string pkgName = |
310 | 0 | cmStrCat(linkInfo.Package, ":", linkInfo.Component); |
311 | 0 | cmSpdxPackage pkg; |
312 | 0 | pkg.Name = pkgName; |
313 | 0 | pkg.SpdxId = cmStrCat("urn:", pkgName, "#Package"); |
314 | 0 | pkg.CreationInfo = ci; |
315 | 0 | if (!linkInfo.Package.empty()) { |
316 | 0 | auto const& pkgIt = this->Requirements.find(linkInfo.Package); |
317 | 0 | if (pkgIt != this->Requirements.end() && |
318 | 0 | pkgIt->second.Components.count(linkInfo.Component) > 0) { |
319 | 0 | this->AddPackageInformation(pkg, pkgIt->first, pkgIt->second); |
320 | 0 | } |
321 | 0 | } |
322 | 0 | return { true, insert_back(project->Elements, std::move(pkg)) }; |
323 | 0 | } |
324 | | |
325 | 0 | cmSpdxPackage pkg; |
326 | 0 | pkg.SpdxId = cmStrCat("urn:", name, "#Package"); |
327 | 0 | pkg.Name = name; |
328 | 0 | pkg.CreationInfo = ci; |
329 | 0 | return { false, insert_back(project->Elements, std::move(pkg)) }; |
330 | 0 | }; |
331 | |
|
332 | 0 | cmList names{ evaluatedLibraries }; |
333 | 0 | names.sort(); |
334 | 0 | names.remove_duplicates(); |
335 | 0 | for (std::string const& n : names) { |
336 | 0 | auto res = addArtifact(n); |
337 | 0 | if (!res.second) { |
338 | 0 | continue; |
339 | 0 | } |
340 | | |
341 | 0 | if (res.first) { |
342 | 0 | linkLibraries.To.push_back(res.second); |
343 | 0 | } else { |
344 | 0 | buildRequires.To.push_back(res.second); |
345 | 0 | } |
346 | 0 | } |
347 | |
|
348 | 0 | if (!linkLibraries.To.empty()) { |
349 | 0 | insert_back(doc.Graph, std::move(linkLibraries)); |
350 | 0 | } |
351 | 0 | if (!linkRequires.To.empty()) { |
352 | 0 | insert_back(doc.Graph, std::move(linkRequires)); |
353 | 0 | } |
354 | 0 | if (!buildRequires.To.empty()) { |
355 | 0 | insert_back(doc.Graph, std::move(buildRequires)); |
356 | 0 | } |
357 | 0 | return true; |
358 | 0 | } |
359 | | |
360 | | bool cmSbomBuilder::GenerateProperties( |
361 | | cmSbomDocument& doc, cmSpdxDocument* proj, cmSpdxCreationInfo const* ci, |
362 | | TargetProperties const& current, |
363 | | std::vector<TargetProperties> const& allTargets, |
364 | | std::string const& config) const |
365 | 0 | { |
366 | 0 | bool status = true; |
367 | 0 | status &= this->GenerateLinkProperties(doc, proj, ci, "LINK_LIBRARIES", |
368 | 0 | current, allTargets, config); |
369 | 0 | status &= this->GenerateLinkProperties( |
370 | 0 | doc, proj, ci, "INTERFACE_LINK_LIBRARIES", current, allTargets, config); |
371 | |
|
372 | 0 | return status; |
373 | 0 | } |
374 | | |
375 | | bool cmSbomBuilder::PopulateLinkLibrariesProperty( |
376 | | cmGeneratorTarget const* target, |
377 | | cmGeneratorExpression::PreprocessContext preprocessRule, |
378 | | ImportPropertyMap& properties) |
379 | 0 | { |
380 | 0 | static std::array<std::string, 3> const linkIfaceProps = { { |
381 | 0 | "LINK_LIBRARIES", |
382 | 0 | "LINK_LIBRARIES_DIRECT", |
383 | 0 | "LINK_LIBRARIES_DIRECT_EXCLUDE", |
384 | 0 | } }; |
385 | 0 | bool hadLINK_LIBRARIES = false; |
386 | 0 | for (std::string const& linkIfaceProp : linkIfaceProps) { |
387 | 0 | if (cmValue input = target->GetProperty(linkIfaceProp)) { |
388 | 0 | std::string prepro = |
389 | 0 | cmGeneratorExpression::Preprocess(*input, preprocessRule); |
390 | 0 | if (!prepro.empty()) { |
391 | 0 | this->ResolveTargetsInGeneratorExpressions(prepro, target); |
392 | 0 | properties[linkIfaceProp] = prepro; |
393 | 0 | hadLINK_LIBRARIES = true; |
394 | 0 | } |
395 | 0 | } |
396 | 0 | } |
397 | 0 | return hadLINK_LIBRARIES; |
398 | 0 | } |
399 | | |
400 | | bool cmSbomBuilder::AddTargetNamespace(std::string& input, |
401 | | cmGeneratorTarget const* target, |
402 | | cmLocalGenerator const* lg) |
403 | 0 | { |
404 | 0 | cmGeneratorTarget::TargetOrString resolved = |
405 | 0 | target->ResolveTargetReference(input, lg); |
406 | |
|
407 | 0 | cmGeneratorTarget* tgt = resolved.Target; |
408 | 0 | if (!tgt) { |
409 | 0 | input = resolved.String; |
410 | 0 | return false; |
411 | 0 | } |
412 | | |
413 | 0 | if (tgt->IsImported()) { |
414 | 0 | input = tgt->GetName(); |
415 | 0 | return this->NoteLinkedTarget(target, input, tgt); |
416 | 0 | } |
417 | | |
418 | 0 | if (this->SbomTargets.find(tgt) != this->SbomTargets.end()) { |
419 | 0 | input = this->Namespace + tgt->GetExportName(); |
420 | 0 | } else { |
421 | 0 | input = tgt->GetName(); |
422 | 0 | } |
423 | |
|
424 | 0 | return this->NoteLinkedTarget(target, input, tgt); |
425 | 0 | } |
426 | | |
427 | | bool cmSbomBuilder::NoteLinkedTarget(cmGeneratorTarget const* target, |
428 | | std::string const& linkedName, |
429 | | cmGeneratorTarget const* linkedTarget) |
430 | 0 | { |
431 | 0 | if (cm::contains(this->SbomTargets, linkedTarget)) { |
432 | 0 | this->LinkTargets.emplace(linkedName, |
433 | 0 | LinkInfo{ "", linkedTarget->GetExportName() }); |
434 | 0 | return true; |
435 | 0 | } |
436 | | |
437 | 0 | if (linkedTarget->IsImported()) { |
438 | 0 | using Package = cm::optional<std::pair<std::string, cmPackageInformation>>; |
439 | 0 | auto pkgInfo = [](cmTarget* t) -> Package { |
440 | 0 | cmFindPackageStack pkgStack = t->GetFindPackageStack(); |
441 | 0 | if (!pkgStack.Empty()) { |
442 | 0 | return std::make_pair(pkgStack.Top().Name, |
443 | 0 | *pkgStack.Top().PackageInfo); |
444 | 0 | } |
445 | 0 | std::string const pkgName = |
446 | 0 | t->GetSafeProperty("EXPORT_FIND_PACKAGE_NAME"); |
447 | 0 | if (pkgName.empty()) { |
448 | 0 | return cm::nullopt; |
449 | 0 | } |
450 | 0 | cmPackageInformation package; |
451 | 0 | return std::make_pair(pkgName, package); |
452 | 0 | }(linkedTarget->Target); |
453 | |
|
454 | 0 | if (!pkgInfo) { |
455 | 0 | target->Makefile->IssueDiagnostic( |
456 | 0 | cmDiagnostics::CMD_AUTHOR, |
457 | 0 | cmStrCat("Target \"", target->GetName(), |
458 | 0 | "\" references imported target \"", linkedName, |
459 | 0 | "\" which does not come from any known package.")); |
460 | 0 | return false; |
461 | 0 | } |
462 | | |
463 | 0 | std::string const& pkgName = pkgInfo->first; |
464 | 0 | auto const& prefix = cmStrCat(pkgName, "::"); |
465 | 0 | std::string component; |
466 | 0 | if (!cmHasPrefix(linkedName, prefix)) { |
467 | 0 | component = linkedName; |
468 | 0 | } else { |
469 | 0 | component = linkedName.substr(prefix.length()); |
470 | 0 | } |
471 | 0 | this->LinkTargets.emplace(linkedName, LinkInfo{ pkgName, component }); |
472 | 0 | cmPackageInformation& req = |
473 | 0 | this->Requirements.insert(std::move(*pkgInfo)).first->second; |
474 | 0 | req.Components.emplace(std::move(component)); |
475 | 0 | return true; |
476 | 0 | } |
477 | | |
478 | | // Target belongs to another export from this build or install. |
479 | | // The leaf class chooses which export map to consult. |
480 | 0 | auto const& exportInfo = this->FindExportInfoFor(linkedTarget); |
481 | 0 | if (exportInfo.Namespaces.size() == 1 && exportInfo.Sets.size() == 1) { |
482 | 0 | auto const& linkNamespace = *exportInfo.Namespaces.begin(); |
483 | 0 | if (!cmHasSuffix(linkNamespace, "::")) { |
484 | 0 | target->Makefile->IssueDiagnostic( |
485 | 0 | cmDiagnostics::CMD_AUTHOR, |
486 | 0 | cmStrCat("Target \"", target->GetName(), "\" references target \"", |
487 | 0 | linkedName, |
488 | 0 | "\", whose export does not use the standard namespace " |
489 | 0 | "separator. The dependency will be recorded by its bare " |
490 | 0 | "target name without provenance.")); |
491 | 0 | return false; |
492 | 0 | } |
493 | | |
494 | 0 | std::string pkgName{ linkNamespace.data(), linkNamespace.size() - 2 }; |
495 | 0 | std::string component = linkedTarget->GetExportName(); |
496 | 0 | if (pkgName == this->GetPackageName()) { |
497 | 0 | this->LinkTargets.emplace(linkedName, LinkInfo{ "", component }); |
498 | 0 | } else { |
499 | 0 | this->LinkTargets.emplace(linkedName, LinkInfo{ pkgName, component }); |
500 | 0 | this->Requirements[pkgName].Components.emplace(std::move(component)); |
501 | 0 | } |
502 | 0 | return true; |
503 | 0 | } |
504 | | |
505 | 0 | if (exportInfo.Sets.empty()) { |
506 | | // install(export) provenance is unavailable. Fall back to SBOM-level |
507 | | // attribution: any peer SBOM (of the same build/install mode) that |
508 | | // covers this target lends its package name. install(export) wins |
509 | | // when both are available; only reached here when install(export) is |
510 | | // absent. |
511 | 0 | auto const& sbomInfo = this->FindSbomInfoFor(linkedTarget); |
512 | 0 | if (sbomInfo.Packages.empty()) { |
513 | 0 | target->Makefile->IssueMessage( |
514 | 0 | MessageType::FATAL_ERROR, |
515 | 0 | cmStrCat("Target \"", target->GetName(), "\" references target \"", |
516 | 0 | linkedName, |
517 | 0 | "\" which has no install(EXPORT)/export(EXPORT) namespace " |
518 | 0 | "and is not covered by any SBOM. An SBOM cannot attribute " |
519 | 0 | "this dependency. Give \"", |
520 | 0 | linkedTarget->GetName(), |
521 | 0 | "\" an install(EXPORT)/export(EXPORT) with a NAMESPACE, or " |
522 | 0 | "include it in an install(SBOM)/export(SBOM).")); |
523 | 0 | return false; |
524 | 0 | } |
525 | 0 | std::string const& pkgName = sbomInfo.Packages.front(); |
526 | 0 | if (sbomInfo.Packages.size() > 1) { |
527 | 0 | target->Makefile->IssueDiagnostic( |
528 | 0 | cmDiagnostics::CMD_AUTHOR, |
529 | 0 | cmStrCat( |
530 | 0 | "Target \"", target->GetName(), "\" references target \"", |
531 | 0 | linkedName, |
532 | 0 | "\" which has no install(EXPORT)/export(EXPORT) namespace and is " |
533 | 0 | "covered by multiple SBOMs: ", |
534 | 0 | cmJoin(sbomInfo.Packages, ", "), ". Attributing to \"", pkgName, |
535 | 0 | "\" (first alphabetically).")); |
536 | 0 | } |
537 | 0 | std::string component = linkedTarget->GetExportName(); |
538 | 0 | this->LinkTargets.emplace(linkedName, LinkInfo{ pkgName, component }); |
539 | 0 | this->Requirements[pkgName].Components.emplace(std::move(component)); |
540 | 0 | return true; |
541 | 0 | } |
542 | | |
543 | 0 | std::ostringstream e; |
544 | 0 | e << "Target \"" << target->GetName() << "\" references target \"" |
545 | 0 | << linkedName << "\" "; |
546 | 0 | if (exportInfo.Sets.size() == 1) { |
547 | 0 | e << "that is in an export set which is exported multiple times " |
548 | 0 | "with different namespaces: "; |
549 | 0 | } else { |
550 | 0 | e << "that is in multiple export sets: "; |
551 | 0 | } |
552 | 0 | e << cmJoin(exportInfo.Files, ", ") << ".\n" |
553 | 0 | << "An SBOM cannot attribute a dependency exported in more than one " |
554 | 0 | "export set or with more than one namespace. Consider " |
555 | 0 | "consolidating the exports of the \"" |
556 | 0 | << linkedTarget->GetName() << "\" target to a single export."; |
557 | 0 | target->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str()); |
558 | 0 | return false; |
559 | 0 | } |
560 | | |
561 | | void cmSbomBuilder::ResolveTargetsInGeneratorExpression( |
562 | | std::string& input, cmGeneratorTarget const* target, |
563 | | cmLocalGenerator const* lg) |
564 | 0 | { |
565 | 0 | auto err = cmResolveTargetsInGeneratorExpression( |
566 | 0 | input, [this, target, lg](std::string& name) { |
567 | 0 | return this->AddTargetNamespace(name, target, lg); |
568 | 0 | }); |
569 | 0 | if (err) { |
570 | 0 | target->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR, *err); |
571 | 0 | } |
572 | 0 | } |
573 | | |
574 | | void cmSbomBuilder::ResolveTargetsInGeneratorExpressions( |
575 | | std::string& input, cmGeneratorTarget const* target) |
576 | 0 | { |
577 | 0 | cmLocalGenerator const* lg = target->GetLocalGenerator(); |
578 | 0 | std::vector<std::string> parts; |
579 | 0 | cmGeneratorExpression::Split(input, parts); |
580 | |
|
581 | 0 | std::string sep; |
582 | 0 | input.clear(); |
583 | 0 | for (std::string& li : parts) { |
584 | 0 | if (target->IsLinkLookupScope(li, lg)) { |
585 | 0 | continue; |
586 | 0 | } |
587 | 0 | if (cmGeneratorExpression::Find(li) == std::string::npos) { |
588 | 0 | this->AddTargetNamespace(li, target, lg); |
589 | 0 | } else { |
590 | 0 | this->ResolveTargetsInGeneratorExpression(li, target, lg); |
591 | 0 | } |
592 | 0 | input += sep + li; |
593 | 0 | sep = ";"; |
594 | 0 | } |
595 | 0 | } |
596 | | |
597 | | bool cmSbomBuilder::PopulateInterfaceLinkLibrariesProperty( |
598 | | cmGeneratorTarget const* target, |
599 | | cmGeneratorExpression::PreprocessContext preprocessRule, |
600 | | ImportPropertyMap& properties) |
601 | 0 | { |
602 | 0 | if (!target->IsLinkable()) { |
603 | 0 | return false; |
604 | 0 | } |
605 | 0 | static std::array<std::string, 3> const linkIfaceProps = { |
606 | 0 | { "INTERFACE_LINK_LIBRARIES", "INTERFACE_LINK_LIBRARIES_DIRECT", |
607 | 0 | "INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE" } |
608 | 0 | }; |
609 | 0 | bool hadINTERFACE_LINK_LIBRARIES = false; |
610 | 0 | for (std::string const& linkIfaceProp : linkIfaceProps) { |
611 | 0 | if (cmValue input = target->GetProperty(linkIfaceProp)) { |
612 | 0 | std::string prepro = |
613 | 0 | cmGeneratorExpression::Preprocess(*input, preprocessRule); |
614 | 0 | if (!prepro.empty()) { |
615 | 0 | this->ResolveTargetsInGeneratorExpressions(prepro, target); |
616 | 0 | properties[linkIfaceProp] = prepro; |
617 | 0 | hadINTERFACE_LINK_LIBRARIES = true; |
618 | 0 | } |
619 | 0 | } |
620 | 0 | } |
621 | 0 | return hadINTERFACE_LINK_LIBRARIES; |
622 | 0 | } |