LaMont Jones | aa005ae | 2023-12-19 19:01:57 +0000 | [diff] [blame^] | 1 | // Copyright 2023 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "github.com/google/blueprint" |
| 19 | ) |
| 20 | |
| 21 | var ( |
| 22 | mergeAconfigFilesRule = pctx.AndroidStaticRule("mergeAconfigFilesRule", |
| 23 | blueprint.RuleParams{ |
| 24 | Command: `${aconfig} dump --dedup --format protobuf --out $out $flags`, |
| 25 | CommandDeps: []string{"${aconfig}"}, |
| 26 | }, "flags") |
| 27 | _ = pctx.HostBinToolVariable("aconfig", "aconfig") |
| 28 | ) |
| 29 | |
| 30 | // Provider published by aconfig_value_set |
| 31 | type AconfigDeclarationsProviderData struct { |
| 32 | Package string |
| 33 | Container string |
| 34 | IntermediateCacheOutputPath WritablePath |
| 35 | IntermediateDumpOutputPath WritablePath |
| 36 | } |
| 37 | |
| 38 | var AconfigDeclarationsProviderKey = blueprint.NewProvider[AconfigDeclarationsProviderData]() |
| 39 | |
| 40 | // This is used to collect the aconfig declarations info on the transitive closure, |
| 41 | // the data is keyed on the container. |
| 42 | type AconfigTransitiveDeclarationsInfo struct { |
| 43 | AconfigFiles map[string]Paths |
| 44 | } |
| 45 | |
| 46 | var AconfigTransitiveDeclarationsInfoProvider = blueprint.NewProvider[AconfigTransitiveDeclarationsInfo]() |
| 47 | |
| 48 | func CollectDependencyAconfigFiles(ctx ModuleContext, mergedAconfigFiles *map[string]Paths) { |
| 49 | if *mergedAconfigFiles == nil { |
| 50 | *mergedAconfigFiles = make(map[string]Paths) |
| 51 | } |
| 52 | ctx.VisitDirectDeps(func(module Module) { |
| 53 | if dep, _ := OtherModuleProvider(ctx, module, AconfigDeclarationsProviderKey); dep.IntermediateCacheOutputPath != nil { |
| 54 | (*mergedAconfigFiles)[dep.Container] = append((*mergedAconfigFiles)[dep.Container], dep.IntermediateCacheOutputPath) |
| 55 | return |
| 56 | } |
| 57 | if dep, _ := OtherModuleProvider(ctx, module, AconfigTransitiveDeclarationsInfoProvider); len(dep.AconfigFiles) > 0 { |
| 58 | for container, v := range dep.AconfigFiles { |
| 59 | (*mergedAconfigFiles)[container] = append((*mergedAconfigFiles)[container], v...) |
| 60 | } |
| 61 | } |
| 62 | }) |
| 63 | |
| 64 | for container, aconfigFiles := range *mergedAconfigFiles { |
| 65 | (*mergedAconfigFiles)[container] = mergeAconfigFiles(ctx, aconfigFiles) |
| 66 | } |
| 67 | |
| 68 | SetProvider(ctx, AconfigTransitiveDeclarationsInfoProvider, AconfigTransitiveDeclarationsInfo{ |
| 69 | AconfigFiles: *mergedAconfigFiles, |
| 70 | }) |
| 71 | } |
| 72 | |
| 73 | func mergeAconfigFiles(ctx ModuleContext, inputs Paths) Paths { |
| 74 | inputs = LastUniquePaths(inputs) |
| 75 | if len(inputs) == 1 { |
| 76 | return Paths{inputs[0]} |
| 77 | } |
| 78 | |
| 79 | output := PathForModuleOut(ctx, "aconfig_merged.pb") |
| 80 | |
| 81 | ctx.Build(pctx, BuildParams{ |
| 82 | Rule: mergeAconfigFilesRule, |
| 83 | Description: "merge aconfig files", |
| 84 | Inputs: inputs, |
| 85 | Output: output, |
| 86 | Args: map[string]string{ |
| 87 | "flags": JoinWithPrefix(inputs.Strings(), "--cache "), |
| 88 | }, |
| 89 | }) |
| 90 | |
| 91 | return Paths{output} |
| 92 | } |