Coverage Report

Created: 2026-02-09 06:05

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