Coverage Report

Created: 2026-06-15 07:03

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
0
  return true;
69
0
}
70
71
cm::string_view cmSbomArguments::CommandName() const
72
0
{
73
0
  return "SBOM"_s;
74
0
}
75
76
std::string cmSbomArguments::GetNamespace() const
77
0
{
78
0
  return cmStrCat(this->PackageName, "::"_s);
79
0
}
80
81
std::string cmSbomArguments::GetPackageName() const
82
0
{
83
0
  return this->PackageName;
84
0
}
85
86
std::string cmSbomArguments::GetDefaultDestination(
87
  std::string const& root) const
88
0
{
89
0
  if (root.empty()) {
90
0
    return cmStrCat("sbom/"_s, this->GetPackageName());
91
0
  }
92
0
  return cmStrCat(root, '/', "sbom/"_s, this->GetPackageName());
93
0
}
94
95
cmSbomArguments::SbomFormat cmSbomArguments::GetFormat() const
96
0
{
97
0
  if (this->Format.empty()) {
98
0
    return SbomFormat::SPDX_3_0_JSON;
99
0
  }
100
0
  return ParseSbomFormat(this->Format);
101
0
}