blob: ae96e1f7089f3c68292a1d8e664de104a74b01a0 [file] [log] [blame]
Wei Li340ee8e2022-03-18 17:33:24 -07001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package provenance
18
19import (
20 "android/soong/android"
21 "github.com/google/blueprint"
22)
23
24var (
25 pctx = android.NewPackageContext("android/soong/provenance")
26 rule = pctx.HostBinToolVariable("gen_provenance_metadata", "gen_provenance_metadata")
27
28 genProvenanceMetaData = pctx.AndroidStaticRule("genProvenanceMetaData",
29 blueprint.RuleParams{
30 Command: `rm -rf "$out" && ` +
31 `${gen_provenance_metadata} --module_name=${module_name} ` +
32 `--artifact_path=$in --install_path=${install_path} --metadata_path=$out`,
33 CommandDeps: []string{"${gen_provenance_metadata}"},
34 }, "module_name", "install_path")
35
36 mergeProvenanceMetaData = pctx.AndroidStaticRule("mergeProvenanceMetaData",
37 blueprint.RuleParams{
38 Command: `rm -rf $out $out.temp && ` +
39 `echo -e "# proto-file: build/soong/provenance/proto/provenance_metadata.proto\n# proto-message: ProvenanceMetaDataList" > $out && ` +
40 `touch $out.temp && cat $out.temp $in | grep -v "^#.*" >> $out && rm -rf $out.temp`,
41 })
42)
43
44type ProvenanceMetadata interface {
45 ProvenanceMetaDataFile() android.OutputPath
46}
47
48func init() {
49 RegisterProvenanceSingleton(android.InitRegistrationContext)
50}
51
52func RegisterProvenanceSingleton(ctx android.RegistrationContext) {
53 ctx.RegisterSingletonType("provenance_metadata_singleton", provenanceInfoSingletonFactory)
54}
55
56var PrepareForTestWithProvenanceSingleton = android.FixtureRegisterWithContext(RegisterProvenanceSingleton)
57
58func provenanceInfoSingletonFactory() android.Singleton {
59 return &provenanceInfoSingleton{}
60}
61
62type provenanceInfoSingleton struct {
63}
64
65func (b *provenanceInfoSingleton) GenerateBuildActions(context android.SingletonContext) {
66 allMetaDataFiles := make([]android.Path, 0)
67 context.VisitAllModulesIf(moduleFilter, func(module android.Module) {
68 if p, ok := module.(ProvenanceMetadata); ok {
69 allMetaDataFiles = append(allMetaDataFiles, p.ProvenanceMetaDataFile())
70 }
71 })
72 mergedMetaDataFile := android.PathForOutput(context, "provenance_metadata.textproto")
73 context.Build(pctx, android.BuildParams{
74 Rule: mergeProvenanceMetaData,
75 Description: "merge provenance metadata",
76 Inputs: allMetaDataFiles,
77 Output: mergedMetaDataFile,
78 })
79
80 context.Build(pctx, android.BuildParams{
81 Rule: blueprint.Phony,
82 Description: "phony rule of merge provenance metadata",
83 Inputs: []android.Path{mergedMetaDataFile},
84 Output: android.PathForPhony(context, "provenance_metadata"),
85 })
86}
87
88func moduleFilter(module android.Module) bool {
89 if !module.Enabled() || module.IsSkipInstall() {
90 return false
91 }
92 if p, ok := module.(ProvenanceMetadata); ok {
93 return p.ProvenanceMetaDataFile().String() != ""
94 }
95 return false
96}
97
98func GenerateArtifactProvenanceMetaData(ctx android.ModuleContext, artifactPath android.Path, installedFile android.InstallPath) android.OutputPath {
99 onDevicePathOfInstalledFile := android.InstallPathToOnDevicePath(ctx, installedFile)
100 artifactMetaDataFile := android.PathForIntermediates(ctx, "provenance_metadata", ctx.ModuleDir(), ctx.ModuleName(), "provenance_metadata.textproto")
101 ctx.Build(pctx, android.BuildParams{
102 Rule: genProvenanceMetaData,
103 Description: "generate artifact provenance metadata",
104 Inputs: []android.Path{artifactPath},
105 Output: artifactMetaDataFile,
106 Args: map[string]string{
107 "module_name": ctx.ModuleName(),
108 "install_path": onDevicePathOfInstalledFile,
109 }})
110
111 return artifactMetaDataFile
112}