Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package provenance |
| 18 | |
| 19 | import ( |
| 20 | "android/soong/android" |
Cole Faust | 021bf3d | 2024-05-01 16:59:00 -0700 | [diff] [blame^] | 21 | |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint" |
| 23 | ) |
| 24 | |
| 25 | var ( |
| 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 Li | 7eb7503 | 2022-06-23 15:17:12 -0700 | [diff] [blame] | 39 | Command: `rm -rf $out && ` + |
Wei Li | b68b367 | 2022-05-03 16:13:00 -0700 | [diff] [blame] | 40 | `echo "# proto-file: build/soong/provenance/proto/provenance_metadata.proto" > $out && ` + |
| 41 | `echo "# proto-message: ProvenanceMetaDataList" >> $out && ` + |
Wei Li | 04e4dfa | 2023-08-24 23:19:30 -0700 | [diff] [blame] | 42 | `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 Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 45 | }) |
| 46 | ) |
| 47 | |
| 48 | type ProvenanceMetadata interface { |
| 49 | ProvenanceMetaDataFile() android.OutputPath |
| 50 | } |
| 51 | |
| 52 | func init() { |
| 53 | RegisterProvenanceSingleton(android.InitRegistrationContext) |
| 54 | } |
| 55 | |
| 56 | func RegisterProvenanceSingleton(ctx android.RegistrationContext) { |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 57 | ctx.RegisterParallelSingletonType("provenance_metadata_singleton", provenanceInfoSingletonFactory) |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | var PrepareForTestWithProvenanceSingleton = android.FixtureRegisterWithContext(RegisterProvenanceSingleton) |
| 61 | |
| 62 | func provenanceInfoSingletonFactory() android.Singleton { |
| 63 | return &provenanceInfoSingleton{} |
| 64 | } |
| 65 | |
| 66 | type provenanceInfoSingleton struct { |
Wei Li | 7cf80a6 | 2022-04-20 23:59:47 +0000 | [diff] [blame] | 67 | mergedMetaDataFile android.OutputPath |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Wei Li | 7cf80a6 | 2022-04-20 23:59:47 +0000 | [diff] [blame] | 70 | func (p *provenanceInfoSingleton) GenerateBuildActions(context android.SingletonContext) { |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 71 | allMetaDataFiles := make([]android.Path, 0) |
Cole Faust | 021bf3d | 2024-05-01 16:59:00 -0700 | [diff] [blame^] | 72 | 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 Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 81 | context.VisitAllModulesIf(moduleFilter, func(module android.Module) { |
| 82 | if p, ok := module.(ProvenanceMetadata); ok { |
| 83 | allMetaDataFiles = append(allMetaDataFiles, p.ProvenanceMetaDataFile()) |
| 84 | } |
| 85 | }) |
Wei Li | 7cf80a6 | 2022-04-20 23:59:47 +0000 | [diff] [blame] | 86 | p.mergedMetaDataFile = android.PathForOutput(context, "provenance_metadata.textproto") |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 87 | context.Build(pctx, android.BuildParams{ |
| 88 | Rule: mergeProvenanceMetaData, |
| 89 | Description: "merge provenance metadata", |
| 90 | Inputs: allMetaDataFiles, |
Wei Li | 7cf80a6 | 2022-04-20 23:59:47 +0000 | [diff] [blame] | 91 | Output: p.mergedMetaDataFile, |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 92 | }) |
| 93 | |
| 94 | context.Build(pctx, android.BuildParams{ |
| 95 | Rule: blueprint.Phony, |
| 96 | Description: "phony rule of merge provenance metadata", |
Wei Li | 7cf80a6 | 2022-04-20 23:59:47 +0000 | [diff] [blame] | 97 | Inputs: []android.Path{p.mergedMetaDataFile}, |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 98 | Output: android.PathForPhony(context, "provenance_metadata"), |
| 99 | }) |
Wei Li | cc4a259 | 2022-04-19 17:29:16 -0700 | [diff] [blame] | 100 | |
| 101 | context.Phony("droidcore", android.PathForPhony(context, "provenance_metadata")) |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 104 | func 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 Li | 7cf80a6 | 2022-04-20 23:59:47 +0000 | [diff] [blame] | 119 | |
| 120 | func (p *provenanceInfoSingleton) MakeVars(ctx android.MakeVarsContext) { |
| 121 | ctx.DistForGoal("droidcore", p.mergedMetaDataFile) |
| 122 | } |
| 123 | |
| 124 | var _ android.SingletonMakeVarsProvider = (*provenanceInfoSingleton)(nil) |