blob: b10036ee761f371c01d7be93fc06f6acf5aab436 [file] [log] [blame]
LaMont Jonesaa005ae2023-12-19 19:01:57 +00001// 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
15package android
16
17import (
18 "github.com/google/blueprint"
19)
20
21var (
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
31type AconfigDeclarationsProviderData struct {
32 Package string
33 Container string
34 IntermediateCacheOutputPath WritablePath
35 IntermediateDumpOutputPath WritablePath
36}
37
38var 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.
42type AconfigTransitiveDeclarationsInfo struct {
43 AconfigFiles map[string]Paths
44}
45
46var AconfigTransitiveDeclarationsInfoProvider = blueprint.NewProvider[AconfigTransitiveDeclarationsInfo]()
47
48func CollectDependencyAconfigFiles(ctx ModuleContext, mergedAconfigFiles *map[string]Paths) {
49 if *mergedAconfigFiles == nil {
50 *mergedAconfigFiles = make(map[string]Paths)
51 }
LaMont Jones1e0a69a2023-12-16 18:03:18 +000052 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
53 // Walk our direct dependencies, ignoring blueprint Modules and disabled Android Modules.
54 aModule, _ := module.(Module)
55 if aModule == nil || !aModule.Enabled() {
56 return
57 }
58
LaMont Jonesaa005ae2023-12-19 19:01:57 +000059 if dep, _ := OtherModuleProvider(ctx, module, AconfigDeclarationsProviderKey); dep.IntermediateCacheOutputPath != nil {
60 (*mergedAconfigFiles)[dep.Container] = append((*mergedAconfigFiles)[dep.Container], dep.IntermediateCacheOutputPath)
61 return
62 }
63 if dep, _ := OtherModuleProvider(ctx, module, AconfigTransitiveDeclarationsInfoProvider); len(dep.AconfigFiles) > 0 {
64 for container, v := range dep.AconfigFiles {
65 (*mergedAconfigFiles)[container] = append((*mergedAconfigFiles)[container], v...)
66 }
67 }
68 })
69
70 for container, aconfigFiles := range *mergedAconfigFiles {
71 (*mergedAconfigFiles)[container] = mergeAconfigFiles(ctx, aconfigFiles)
72 }
73
74 SetProvider(ctx, AconfigTransitiveDeclarationsInfoProvider, AconfigTransitiveDeclarationsInfo{
75 AconfigFiles: *mergedAconfigFiles,
76 })
77}
78
79func mergeAconfigFiles(ctx ModuleContext, inputs Paths) Paths {
80 inputs = LastUniquePaths(inputs)
81 if len(inputs) == 1 {
82 return Paths{inputs[0]}
83 }
84
85 output := PathForModuleOut(ctx, "aconfig_merged.pb")
86
87 ctx.Build(pctx, BuildParams{
88 Rule: mergeAconfigFilesRule,
89 Description: "merge aconfig files",
90 Inputs: inputs,
91 Output: output,
92 Args: map[string]string{
93 "flags": JoinWithPrefix(inputs.Strings(), "--cache "),
94 },
95 })
96
97 return Paths{output}
98}
LaMont Jonesacae2d72024-01-09 22:53:52 +000099
100func SetAconfigFileMkEntries(m *ModuleBase, entries *AndroidMkEntries, aconfigFiles map[string]Paths) {
101 // TODO(b/311155208): The default container here should be system.
102 container := ""
103
104 if m.SocSpecific() {
105 container = "vendor"
106 } else if m.ProductSpecific() {
107 container = "product"
108 } else if m.SystemExtSpecific() {
109 container = "system_ext"
110 }
111
112 entries.SetPaths("LOCAL_ACONFIG_FILES", aconfigFiles[container])
113}