/src/CMake/Source/cmSbomArguments.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 "cmSbomArguments.h" |
4 | | |
5 | | #include <algorithm> |
6 | | |
7 | | #include <cm/string_view> |
8 | | |
9 | | #include "cmsys/String.h" |
10 | | |
11 | | #include "cmExecutionStatus.h" |
12 | | #include "cmGeneratorExpression.h" |
13 | | #include "cmStringAlgorithms.h" |
14 | | |
15 | | struct SbomFormatPattern |
16 | | { |
17 | | cm::string_view Name; |
18 | | cm::string_view Alias; |
19 | | cmSbomArguments::SbomFormat Id; |
20 | | }; |
21 | | |
22 | | static SbomFormatPattern SbomPatterns[] = { |
23 | | { "spdx-3.0+json", "spdx", cmSbomArguments::SbomFormat::SPDX_3_0_JSON }, |
24 | | }; |
25 | | |
26 | | cmSbomArguments::SbomFormat ParseSbomFormat(std::string const& input) |
27 | 0 | { |
28 | 0 | if (input.empty()) { |
29 | 0 | return cmSbomArguments::SbomFormat::SPDX_3_0_JSON; |
30 | 0 | } |
31 | | |
32 | 0 | cm::string_view s(input); |
33 | 0 | for (auto const& p : SbomPatterns) { |
34 | 0 | if (s == p.Name || (!p.Alias.empty() && s == p.Alias)) { |
35 | 0 | return p.Id; |
36 | 0 | } |
37 | 0 | } |
38 | 0 | return cmSbomArguments::SbomFormat::NONE; |
39 | 0 | } |
40 | | |
41 | | std::string GetSbomFileExtension(cmSbomArguments::SbomFormat id) |
42 | 0 | { |
43 | 0 | switch (id) { |
44 | 0 | case cmSbomArguments::SbomFormat::SPDX_3_0_JSON: |
45 | 0 | return ".spdx.json"; |
46 | 0 | default: |
47 | 0 | return ""; |
48 | 0 | } |
49 | 0 | } |
50 | | |
51 | | template void cmSbomArguments::Bind<void>(cmArgumentParser<void>&, |
52 | | cmSbomArguments*); |
53 | | |
54 | | bool cmSbomArguments::Check(cmExecutionStatus& status) const |
55 | 0 | { |
56 | 0 | if (!this->PackageName.empty()) { |
57 | 0 | if (!cmGeneratorExpression::IsValidTargetName(this->PackageName) || |
58 | 0 | this->PackageName.find(':') != std::string::npos) { |
59 | 0 | status.SetError(cmStrCat(R"(SBOM given invalid package name ")"_s, |
60 | 0 | this->PackageName, R"(".)"_s)); |
61 | 0 | return false; |
62 | 0 | } |
63 | 0 | } |
64 | | |
65 | 0 | if (!this->Format.empty()) { |
66 | 0 | if (this->GetFormat() == SbomFormat::NONE) { |
67 | 0 | status.SetError( |
68 | 0 | cmStrCat(R"(SBOM given invalid format ")"_s, this->Format, R"(".)"_s)); |
69 | 0 | return false; |
70 | 0 | } |
71 | 0 | } |
72 | 0 | return true; |
73 | 0 | } |
74 | | |
75 | | cm::string_view cmSbomArguments::CommandName() const |
76 | 0 | { |
77 | 0 | return "SBOM"_s; |
78 | 0 | } |
79 | | |
80 | | std::string cmSbomArguments::GetNamespace() const |
81 | 0 | { |
82 | 0 | return cmStrCat(this->PackageName, "::"_s); |
83 | 0 | } |
84 | | |
85 | | std::string cmSbomArguments::GetPackageDirName() const |
86 | 0 | { |
87 | 0 | return this->PackageName; |
88 | 0 | } |
89 | | |
90 | | cmSbomArguments::SbomFormat cmSbomArguments::GetFormat() const |
91 | 0 | { |
92 | 0 | if (this->Format.empty()) { |
93 | 0 | return SbomFormat::SPDX_3_0_JSON; |
94 | 0 | } |
95 | 0 | return ParseSbomFormat(this->Format); |
96 | 0 | } |
97 | | |
98 | | std::string cmSbomArguments::GetPackageFileName() const |
99 | 0 | { |
100 | 0 | std::string const pkgNameOnDisk = this->GetPackageDirName(); |
101 | 0 | std::string format = GetSbomFileExtension(this->GetFormat()); |
102 | 0 | std::transform(format.begin(), format.end(), format.begin(), |
103 | 0 | cmsysString_tolower); |
104 | 0 | return cmStrCat(pkgNameOnDisk, format); |
105 | 0 | } |