Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmInstallSbomGenerator.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 "cmInstallSbomGenerator.h"
4
5
#include <ostream>
6
#include <utility>
7
#include <vector>
8
9
#include <cm/memory>
10
11
#include "cmCryptoHash.h"
12
#include "cmDiagnosticContext.h"
13
#include "cmGeneratedFileStream.h"
14
#include "cmInstallSbomBuilder.h"
15
#include "cmInstallType.h"
16
#include "cmLocalGenerator.h"
17
#include "cmMakefile.h"
18
#include "cmSbomArguments.h"
19
#include "cmScriptGenerator.h"
20
#include "cmStringAlgorithms.h"
21
#include "cmSystemTools.h"
22
23
cmInstallSbomGenerator::cmInstallSbomGenerator(
24
  std::vector<cmExportSet*> exportSets, std::string destination,
25
  std::string filePermissions, std::vector<std::string> const& configurations,
26
  std::string component, MessageLevel message, bool excludeFromAll,
27
  cmSbomArguments args, cmDiagnosticContext context)
28
0
  : cmInstallGenerator(std::move(destination), configurations,
29
0
                       std::move(component), message, excludeFromAll, false,
30
0
                       std::move(context))
31
0
  , FilePermissions(std::move(filePermissions))
32
0
  , SbomFileName(args.GetPackageName())
33
0
  , SbomFilePath(cmStrCat(this->Destination, '/', this->SbomFileName))
34
0
  , SbomFormat(args.GetFormat())
35
0
  , Builder(cm::make_unique<cmInstallSbomBuilder>(std::move(args),
36
0
                                                  std::move(exportSets)))
37
0
{
38
0
}
39
40
0
cmInstallSbomGenerator::~cmInstallSbomGenerator() = default;
41
42
bool cmInstallSbomGenerator::Compute(cmLocalGenerator* lg)
43
0
{
44
0
  this->LocalGenerator = lg;
45
0
  this->Builder->Compute(lg);
46
0
  return true;
47
0
}
48
49
bool cmInstallSbomGenerator::CoversTarget(
50
  cmGeneratorTarget const* target) const
51
0
{
52
0
  return this->Builder->CoversTarget(target);
53
0
}
54
55
std::string const& cmInstallSbomGenerator::GetPackageName() const
56
0
{
57
0
  return this->Builder->GetPackageName();
58
0
}
59
60
bool cmInstallSbomGenerator::CoversExportSet(cmExportSet const* set) const
61
0
{
62
0
  return this->Builder->CoversExportSet(set);
63
0
}
64
65
void cmInstallSbomGenerator::GenerateScript(std::ostream& os)
66
0
{
67
  // Choose a temporary directory in the build tree to hold the generated SBOM.
68
0
  cmCryptoHash hasher(cmCryptoHash::AlgoMD5);
69
0
  std::string const tempDir =
70
0
    cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(),
71
0
             "/CMakeFiles/sbom/", hasher.HashString(this->Destination));
72
0
  cmSystemTools::MakeDirectory(tempDir);
73
74
0
  for (std::string const& c :
75
0
       this->LocalGenerator->GetMakefile()->GetGeneratorConfigs(
76
0
         cmMakefile::IncludeEmptyConfig)) {
77
0
    std::string configName =
78
0
      cmStrCat(tempDir, '/', this->SbomFileName, "-", c, ".spdx.json");
79
0
    cmGeneratedFileStream sbomStream(configName);
80
0
    this->TempSbomFiles.emplace(c, configName);
81
0
    if (!this->Builder->Generate(sbomStream, c)) {
82
0
      break;
83
0
    }
84
0
  }
85
86
  // Emit the cmake_install.cmake script to copy the file at install time.
87
0
  this->cmInstallGenerator::GenerateScript(os);
88
0
}
89
90
void cmInstallSbomGenerator::GenerateScriptActions(std::ostream& os,
91
                                                   Indent indent)
92
0
{
93
0
  for (auto const& i : this->TempSbomFiles) {
94
0
    std::string configTest = this->CreateConfigTest(i.first);
95
0
    os << indent << "if(" << configTest << ")\n";
96
0
    this->AddInstallRule(os, this->Destination, cmInstallType_FILES,
97
0
                         { i.second }, false, this->FilePermissions.c_str(),
98
0
                         nullptr, nullptr, nullptr, indent.Next());
99
0
    os << indent << "endif()\n";
100
0
  }
101
0
}