blob: 2a5499ed8a243ab238fa9d7e32a10f073f336ca1 [file] [log] [blame]
Wei Lib85a1782024-02-05 14:50:54 -08001// Copyright 2024 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
17import (
18 "io"
19 "path/filepath"
20 "strings"
21
22 "github.com/google/blueprint"
23)
24
25var (
26 // Command line tool to generate SBOM in Soong
27 genSbom = pctx.HostBinToolVariable("genSbom", "gen_sbom")
28
29 // Command to generate SBOM in Soong.
30 genSbomRule = pctx.AndroidStaticRule("genSbomRule", blueprint.RuleParams{
31 Command: "rm -rf $out && ${genSbom} --output_file ${out} --metadata ${in} --product_out ${productOut} --soong_out ${soongOut} --build_version \"$$(cat ${buildFingerprintFile})\" --product_mfr \"${productManufacturer}\" --json",
32 CommandDeps: []string{"${genSbom}"},
33 }, "productOut", "soongOut", "buildFingerprintFile", "productManufacturer")
34)
35
36func init() {
37 RegisterSbomSingleton(InitRegistrationContext)
38}
39
40func RegisterSbomSingleton(ctx RegistrationContext) {
41 ctx.RegisterParallelSingletonType("sbom_singleton", sbomSingletonFactory)
42}
43
44// sbomSingleton is used to generate build actions of generating SBOM of products.
Wei Lib1af36a2024-08-02 18:00:38 -070045type sbomSingleton struct {
46 sbomFile OutputPath
47}
Wei Lib85a1782024-02-05 14:50:54 -080048
49func sbomSingletonFactory() Singleton {
50 return &sbomSingleton{}
51}
52
53// Generates SBOM of products
54func (this *sbomSingleton) GenerateBuildActions(ctx SingletonContext) {
55 if !ctx.Config().HasDeviceProduct() {
56 return
57 }
58 // Get all METADATA files and add them as implicit input
59 metadataFileListFile := PathForArbitraryOutput(ctx, ".module_paths", "METADATA.list")
60 f, err := ctx.Config().fs.Open(metadataFileListFile.String())
61 if err != nil {
62 panic(err)
63 }
64 b, err := io.ReadAll(f)
65 if err != nil {
66 panic(err)
67 }
68 allMetadataFiles := strings.Split(string(b), "\n")
69 implicits := []Path{metadataFileListFile}
70 for _, path := range allMetadataFiles {
71 implicits = append(implicits, PathForSource(ctx, path))
72 }
73 prodVars := ctx.Config().productVariables
74 buildFingerprintFile := PathForArbitraryOutput(ctx, "target", "product", String(prodVars.DeviceName), "build_fingerprint.txt")
75 implicits = append(implicits, buildFingerprintFile)
76
77 // Add installed_files.stamp as implicit input, which depends on all installed files of the product.
78 installedFilesStamp := PathForOutput(ctx, "compliance-metadata", ctx.Config().DeviceProduct(), "installed_files.stamp")
79 implicits = append(implicits, installedFilesStamp)
80
81 metadataDb := PathForOutput(ctx, "compliance-metadata", ctx.Config().DeviceProduct(), "compliance-metadata.db")
Wei Lib1af36a2024-08-02 18:00:38 -070082 this.sbomFile = PathForOutput(ctx, "sbom", ctx.Config().DeviceProduct(), "sbom.spdx.json")
Wei Lib85a1782024-02-05 14:50:54 -080083 ctx.Build(pctx, BuildParams{
84 Rule: genSbomRule,
85 Input: metadataDb,
86 Implicits: implicits,
Wei Lib1af36a2024-08-02 18:00:38 -070087 Output: this.sbomFile,
Wei Lib85a1782024-02-05 14:50:54 -080088 Args: map[string]string{
89 "productOut": filepath.Join(ctx.Config().OutDir(), "target", "product", String(prodVars.DeviceName)),
90 "soongOut": ctx.Config().soongOutDir,
91 "buildFingerprintFile": buildFingerprintFile.String(),
92 "productManufacturer": ctx.Config().ProductVariables().ProductManufacturer,
93 },
94 })
95
Wei Lib1af36a2024-08-02 18:00:38 -070096 if !ctx.Config().UnbundledBuildApps() {
97 // When building SBOM of products, phony rule "sbom" is for generating product SBOM in Soong.
98 ctx.Build(pctx, BuildParams{
99 Rule: blueprint.Phony,
100 Inputs: []Path{this.sbomFile},
101 Output: PathForPhony(ctx, "sbom"),
102 })
103 }
104}
105
106func (this *sbomSingleton) MakeVars(ctx MakeVarsContext) {
107 // When building SBOM of products
108 if !ctx.Config().UnbundledBuildApps() {
109 ctx.DistForGoalWithFilename("droid", this.sbomFile, "sbom/sbom.spdx.json")
110 }
Wei Lib85a1782024-02-05 14:50:54 -0800111}