Coverage Report

Created: 2026-07-14 07:09

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