blob: 679632c109366b4e37d75d1a3178b74df6f9bfd4 [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"
Cole Faust021bf3d2024-05-01 16:59:00 -070021
Wei Li340ee8e2022-03-18 17:33:24 -070022 "github.com/google/blueprint"
23)
24
25var (
26 pctx = android.NewPackageContext("android/soong/provenance")
27 rule = pctx.HostBinToolVariable("gen_provenance_metadata", "gen_provenance_metadata")
28
29 genProvenanceMetaData = pctx.AndroidStaticRule("genProvenanceMetaData",
30 blueprint.RuleParams{
31 Command: `rm -rf "$out" && ` +
32 `${gen_provenance_metadata} --module_name=${module_name} ` +
33 `--artifact_path=$in --install_path=${install_path} --metadata_path=$out`,
34 CommandDeps: []string{"${gen_provenance_metadata}"},
35 }, "module_name", "install_path")
36
37 mergeProvenanceMetaData = pctx.AndroidStaticRule("mergeProvenanceMetaData",
38 blueprint.RuleParams{
Wei Li7eb75032022-06-23 15:17:12 -070039 Command: `rm -rf $out && ` +
Wei Lib68b3672022-05-03 16:13:00 -070040 `echo "# proto-file: build/soong/provenance/proto/provenance_metadata.proto" > $out && ` +
41 `echo "# proto-message: ProvenanceMetaDataList" >> $out && ` +
Wei Li04e4dfa2023-08-24 23:19:30 -070042 `cat $out.rsp | tr ' ' '\n' | while read -r file || [ -n "$$file" ]; do echo '' >> $out; echo 'metadata {' | cat - $$file | grep -Ev "^#.*|^$$" >> $out; echo '}' >> $out; done`,
43 Rspfile: `$out.rsp`,
44 RspfileContent: `$in`,
Wei Li340ee8e2022-03-18 17:33:24 -070045 })
46)
47
48type ProvenanceMetadata interface {
49 ProvenanceMetaDataFile() android.OutputPath
50}
51
52func init() {
53 RegisterProvenanceSingleton(android.InitRegistrationContext)
54}
55
56func RegisterProvenanceSingleton(ctx android.RegistrationContext) {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000057 ctx.RegisterParallelSingletonType("provenance_metadata_singleton", provenanceInfoSingletonFactory)
Wei Li340ee8e2022-03-18 17:33:24 -070058}
59
60var PrepareForTestWithProvenanceSingleton = android.FixtureRegisterWithContext(RegisterProvenanceSingleton)
61
62func provenanceInfoSingletonFactory() android.Singleton {
63 return &provenanceInfoSingleton{}
64}
65
66type provenanceInfoSingleton struct {
Wei Li7cf80a62022-04-20 23:59:47 +000067 mergedMetaDataFile android.OutputPath
Wei Li340ee8e2022-03-18 17:33:24 -070068}
69
Wei Li7cf80a62022-04-20 23:59:47 +000070func (p *provenanceInfoSingleton) GenerateBuildActions(context android.SingletonContext) {
Wei Li340ee8e2022-03-18 17:33:24 -070071 allMetaDataFiles := make([]android.Path, 0)
Cole Faust021bf3d2024-05-01 16:59:00 -070072 moduleFilter := func(module android.Module) bool {
73 if !module.Enabled(context) || module.IsSkipInstall() {
74 return false
75 }
76 if p, ok := module.(ProvenanceMetadata); ok {
77 return p.ProvenanceMetaDataFile().String() != ""
78 }
79 return false
80 }
Wei Li340ee8e2022-03-18 17:33:24 -070081 context.VisitAllModulesIf(moduleFilter, func(module android.Module) {
82 if p, ok := module.(ProvenanceMetadata); ok {
83 allMetaDataFiles = append(allMetaDataFiles, p.ProvenanceMetaDataFile())
84 }
85 })
Wei Li7cf80a62022-04-20 23:59:47 +000086 p.mergedMetaDataFile = android.PathForOutput(context, "provenance_metadata.textproto")
Wei Li340ee8e2022-03-18 17:33:24 -070087 context.Build(pctx, android.BuildParams{
88 Rule: mergeProvenanceMetaData,
89 Description: "merge provenance metadata",
90 Inputs: allMetaDataFiles,
Wei Li7cf80a62022-04-20 23:59:47 +000091 Output: p.mergedMetaDataFile,
Wei Li340ee8e2022-03-18 17:33:24 -070092 })
93
94 context.Build(pctx, android.BuildParams{
95 Rule: blueprint.Phony,
96 Description: "phony rule of merge provenance metadata",
Wei Li7cf80a62022-04-20 23:59:47 +000097 Inputs: []android.Path{p.mergedMetaDataFile},
Wei Li340ee8e2022-03-18 17:33:24 -070098 Output: android.PathForPhony(context, "provenance_metadata"),
99 })
Wei Licc4a2592022-04-19 17:29:16 -0700100
101 context.Phony("droidcore", android.PathForPhony(context, "provenance_metadata"))
Wei Li340ee8e2022-03-18 17:33:24 -0700102}
103
Wei Li340ee8e2022-03-18 17:33:24 -0700104func GenerateArtifactProvenanceMetaData(ctx android.ModuleContext, artifactPath android.Path, installedFile android.InstallPath) android.OutputPath {
105 onDevicePathOfInstalledFile := android.InstallPathToOnDevicePath(ctx, installedFile)
106 artifactMetaDataFile := android.PathForIntermediates(ctx, "provenance_metadata", ctx.ModuleDir(), ctx.ModuleName(), "provenance_metadata.textproto")
107 ctx.Build(pctx, android.BuildParams{
108 Rule: genProvenanceMetaData,
109 Description: "generate artifact provenance metadata",
110 Inputs: []android.Path{artifactPath},
111 Output: artifactMetaDataFile,
112 Args: map[string]string{
113 "module_name": ctx.ModuleName(),
114 "install_path": onDevicePathOfInstalledFile,
115 }})
116
117 return artifactMetaDataFile
118}
Wei Li7cf80a62022-04-20 23:59:47 +0000119
120func (p *provenanceInfoSingleton) MakeVars(ctx android.MakeVarsContext) {
121 ctx.DistForGoal("droidcore", p.mergedMetaDataFile)
122}
123
124var _ android.SingletonMakeVarsProvider = (*provenanceInfoSingleton)(nil)