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" |
| 21 | "github.com/google/blueprint" |
| 22 | ) |
| 23 | |
| 24 | var ( |
| 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 | |
| 44 | type ProvenanceMetadata interface { |
| 45 | ProvenanceMetaDataFile() android.OutputPath |
| 46 | } |
| 47 | |
| 48 | func init() { |
| 49 | RegisterProvenanceSingleton(android.InitRegistrationContext) |
| 50 | } |
| 51 | |
| 52 | func RegisterProvenanceSingleton(ctx android.RegistrationContext) { |
| 53 | ctx.RegisterSingletonType("provenance_metadata_singleton", provenanceInfoSingletonFactory) |
| 54 | } |
| 55 | |
| 56 | var PrepareForTestWithProvenanceSingleton = android.FixtureRegisterWithContext(RegisterProvenanceSingleton) |
| 57 | |
| 58 | func provenanceInfoSingletonFactory() android.Singleton { |
| 59 | return &provenanceInfoSingleton{} |
| 60 | } |
| 61 | |
| 62 | type provenanceInfoSingleton struct { |
Wei Li | 7cf80a6 | 2022-04-20 23:59:47 +0000 | [diff] [blame] | 63 | mergedMetaDataFile android.OutputPath |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Wei Li | 7cf80a6 | 2022-04-20 23:59:47 +0000 | [diff] [blame] | 66 | func (p *provenanceInfoSingleton) GenerateBuildActions(context android.SingletonContext) { |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 67 | allMetaDataFiles := make([]android.Path, 0) |
| 68 | context.VisitAllModulesIf(moduleFilter, func(module android.Module) { |
| 69 | if p, ok := module.(ProvenanceMetadata); ok { |
| 70 | allMetaDataFiles = append(allMetaDataFiles, p.ProvenanceMetaDataFile()) |
| 71 | } |
| 72 | }) |
Wei Li | 7cf80a6 | 2022-04-20 23:59:47 +0000 | [diff] [blame] | 73 | p.mergedMetaDataFile = android.PathForOutput(context, "provenance_metadata.textproto") |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 74 | context.Build(pctx, android.BuildParams{ |
| 75 | Rule: mergeProvenanceMetaData, |
| 76 | Description: "merge provenance metadata", |
| 77 | Inputs: allMetaDataFiles, |
Wei Li | 7cf80a6 | 2022-04-20 23:59:47 +0000 | [diff] [blame] | 78 | Output: p.mergedMetaDataFile, |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 79 | }) |
| 80 | |
| 81 | context.Build(pctx, android.BuildParams{ |
| 82 | Rule: blueprint.Phony, |
| 83 | Description: "phony rule of merge provenance metadata", |
Wei Li | 7cf80a6 | 2022-04-20 23:59:47 +0000 | [diff] [blame] | 84 | Inputs: []android.Path{p.mergedMetaDataFile}, |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 85 | Output: android.PathForPhony(context, "provenance_metadata"), |
| 86 | }) |
Wei Li | cc4a259 | 2022-04-19 17:29:16 -0700 | [diff] [blame] | 87 | |
| 88 | context.Phony("droidcore", android.PathForPhony(context, "provenance_metadata")) |
Wei Li | 340ee8e | 2022-03-18 17:33:24 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | func moduleFilter(module android.Module) bool { |
| 92 | if !module.Enabled() || module.IsSkipInstall() { |
| 93 | return false |
| 94 | } |
| 95 | if p, ok := module.(ProvenanceMetadata); ok { |
| 96 | return p.ProvenanceMetaDataFile().String() != "" |
| 97 | } |
| 98 | return false |
| 99 | } |
| 100 | |
| 101 | func GenerateArtifactProvenanceMetaData(ctx android.ModuleContext, artifactPath android.Path, installedFile android.InstallPath) android.OutputPath { |
| 102 | onDevicePathOfInstalledFile := android.InstallPathToOnDevicePath(ctx, installedFile) |
| 103 | artifactMetaDataFile := android.PathForIntermediates(ctx, "provenance_metadata", ctx.ModuleDir(), ctx.ModuleName(), "provenance_metadata.textproto") |
| 104 | ctx.Build(pctx, android.BuildParams{ |
| 105 | Rule: genProvenanceMetaData, |
| 106 | Description: "generate artifact provenance metadata", |
| 107 | Inputs: []android.Path{artifactPath}, |
| 108 | Output: artifactMetaDataFile, |
| 109 | Args: map[string]string{ |
| 110 | "module_name": ctx.ModuleName(), |
| 111 | "install_path": onDevicePathOfInstalledFile, |
| 112 | }}) |
| 113 | |
| 114 | return artifactMetaDataFile |
| 115 | } |
Wei Li | 7cf80a6 | 2022-04-20 23:59:47 +0000 | [diff] [blame] | 116 | |
| 117 | func (p *provenanceInfoSingleton) MakeVars(ctx android.MakeVarsContext) { |
| 118 | ctx.DistForGoal("droidcore", p.mergedMetaDataFile) |
| 119 | } |
| 120 | |
| 121 | var _ android.SingletonMakeVarsProvider = (*provenanceInfoSingleton)(nil) |