summaryrefslogtreecommitdiff
path: root/provenance/provenance_singleton.go
blob: ae96e1f7089f3c68292a1d8e664de104a74b01a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package provenance

import (
	"android/soong/android"
	"github.com/google/blueprint"
)

var (
	pctx = android.NewPackageContext("android/soong/provenance")
	rule = pctx.HostBinToolVariable("gen_provenance_metadata", "gen_provenance_metadata")

	genProvenanceMetaData = pctx.AndroidStaticRule("genProvenanceMetaData",
		blueprint.RuleParams{
			Command: `rm -rf "$out" && ` +
				`${gen_provenance_metadata} --module_name=${module_name} ` +
				`--artifact_path=$in --install_path=${install_path} --metadata_path=$out`,
			CommandDeps: []string{"${gen_provenance_metadata}"},
		}, "module_name", "install_path")

	mergeProvenanceMetaData = pctx.AndroidStaticRule("mergeProvenanceMetaData",
		blueprint.RuleParams{
			Command: `rm -rf $out $out.temp && ` +
				`echo -e "# proto-file: build/soong/provenance/proto/provenance_metadata.proto\n# proto-message: ProvenanceMetaDataList" > $out && ` +
				`touch $out.temp && cat $out.temp $in | grep -v "^#.*" >> $out && rm -rf $out.temp`,
		})
)

type ProvenanceMetadata interface {
	ProvenanceMetaDataFile() android.OutputPath
}

func init() {
	RegisterProvenanceSingleton(android.InitRegistrationContext)
}

func RegisterProvenanceSingleton(ctx android.RegistrationContext) {
	ctx.RegisterSingletonType("provenance_metadata_singleton", provenanceInfoSingletonFactory)
}

var PrepareForTestWithProvenanceSingleton = android.FixtureRegisterWithContext(RegisterProvenanceSingleton)

func provenanceInfoSingletonFactory() android.Singleton {
	return &provenanceInfoSingleton{}
}

type provenanceInfoSingleton struct {
}

func (b *provenanceInfoSingleton) GenerateBuildActions(context android.SingletonContext) {
	allMetaDataFiles := make([]android.Path, 0)
	context.VisitAllModulesIf(moduleFilter, func(module android.Module) {
		if p, ok := module.(ProvenanceMetadata); ok {
			allMetaDataFiles = append(allMetaDataFiles, p.ProvenanceMetaDataFile())
		}
	})
	mergedMetaDataFile := android.PathForOutput(context, "provenance_metadata.textproto")
	context.Build(pctx, android.BuildParams{
		Rule:        mergeProvenanceMetaData,
		Description: "merge provenance metadata",
		Inputs:      allMetaDataFiles,
		Output:      mergedMetaDataFile,
	})

	context.Build(pctx, android.BuildParams{
		Rule:        blueprint.Phony,
		Description: "phony rule of merge provenance metadata",
		Inputs:      []android.Path{mergedMetaDataFile},
		Output:      android.PathForPhony(context, "provenance_metadata"),
	})
}

func moduleFilter(module android.Module) bool {
	if !module.Enabled() || module.IsSkipInstall() {
		return false
	}
	if p, ok := module.(ProvenanceMetadata); ok {
		return p.ProvenanceMetaDataFile().String() != ""
	}
	return false
}

func GenerateArtifactProvenanceMetaData(ctx android.ModuleContext, artifactPath android.Path, installedFile android.InstallPath) android.OutputPath {
	onDevicePathOfInstalledFile := android.InstallPathToOnDevicePath(ctx, installedFile)
	artifactMetaDataFile := android.PathForIntermediates(ctx, "provenance_metadata", ctx.ModuleDir(), ctx.ModuleName(), "provenance_metadata.textproto")
	ctx.Build(pctx, android.BuildParams{
		Rule:        genProvenanceMetaData,
		Description: "generate artifact provenance metadata",
		Inputs:      []android.Path{artifactPath},
		Output:      artifactMetaDataFile,
		Args: map[string]string{
			"module_name":  ctx.ModuleName(),
			"install_path": onDevicePathOfInstalledFile,
		}})

	return artifactMetaDataFile
}