blob: be9beb1ba277171e54e0256575d4c49ee00d907b [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 (
LaMont Jonesb5099382024-01-10 23:42:36 +000018 "fmt"
19 "io"
20 "reflect"
21
LaMont Jonesaa005ae2023-12-19 19:01:57 +000022 "github.com/google/blueprint"
23)
24
25var (
26 mergeAconfigFilesRule = pctx.AndroidStaticRule("mergeAconfigFilesRule",
27 blueprint.RuleParams{
28 Command: `${aconfig} dump --dedup --format protobuf --out $out $flags`,
29 CommandDeps: []string{"${aconfig}"},
30 }, "flags")
31 _ = pctx.HostBinToolVariable("aconfig", "aconfig")
32)
33
34// Provider published by aconfig_value_set
35type AconfigDeclarationsProviderData struct {
36 Package string
37 Container string
38 IntermediateCacheOutputPath WritablePath
39 IntermediateDumpOutputPath WritablePath
40}
41
42var AconfigDeclarationsProviderKey = blueprint.NewProvider[AconfigDeclarationsProviderData]()
43
44// This is used to collect the aconfig declarations info on the transitive closure,
45// the data is keyed on the container.
46type AconfigTransitiveDeclarationsInfo struct {
47 AconfigFiles map[string]Paths
48}
49
50var AconfigTransitiveDeclarationsInfoProvider = blueprint.NewProvider[AconfigTransitiveDeclarationsInfo]()
51
LaMont Jonesb5099382024-01-10 23:42:36 +000052// CollectDependencyAconfigFiles is used by some module types to provide finer dependency graphing than
53// we can do in ModuleBase.
LaMont Jonesaa005ae2023-12-19 19:01:57 +000054func CollectDependencyAconfigFiles(ctx ModuleContext, mergedAconfigFiles *map[string]Paths) {
55 if *mergedAconfigFiles == nil {
56 *mergedAconfigFiles = make(map[string]Paths)
57 }
LaMont Jones34314b72024-01-18 18:27:35 +000058 ctx.VisitDirectDepsIgnoreBlueprint(func(module Module) {
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 }
LaMont Jonesb5099382024-01-10 23:42:36 +000063 if dep, ok := OtherModuleProvider(ctx, module, aconfigPropagatingProviderKey); ok {
64 for container, v := range dep.AconfigFiles {
65 (*mergedAconfigFiles)[container] = append((*mergedAconfigFiles)[container], v...)
66 }
67 }
68 // We process these last, so that they determine the final value, eliminating any duplicates that we picked up
69 // from UpdateAndroidBuildActions.
70 if dep, ok := OtherModuleProvider(ctx, module, AconfigTransitiveDeclarationsInfoProvider); ok {
LaMont Jonesaa005ae2023-12-19 19:01:57 +000071 for container, v := range dep.AconfigFiles {
72 (*mergedAconfigFiles)[container] = append((*mergedAconfigFiles)[container], v...)
73 }
74 }
75 })
76
77 for container, aconfigFiles := range *mergedAconfigFiles {
LaMont Jonesb5099382024-01-10 23:42:36 +000078 (*mergedAconfigFiles)[container] = mergeAconfigFiles(ctx, container, aconfigFiles, false)
LaMont Jonesaa005ae2023-12-19 19:01:57 +000079 }
80
81 SetProvider(ctx, AconfigTransitiveDeclarationsInfoProvider, AconfigTransitiveDeclarationsInfo{
82 AconfigFiles: *mergedAconfigFiles,
83 })
84}
85
LaMont Jonesb5099382024-01-10 23:42:36 +000086func SetAconfigFileMkEntries(m *ModuleBase, entries *AndroidMkEntries, aconfigFiles map[string]Paths) {
87 setAconfigFileMkEntries(m, entries, aconfigFiles)
88}
89
90type aconfigPropagatingDeclarationsInfo struct {
91 AconfigFiles map[string]Paths
92}
93
94var aconfigPropagatingProviderKey = blueprint.NewProvider[aconfigPropagatingDeclarationsInfo]()
95
96func aconfigUpdateAndroidBuildActions(ctx ModuleContext) {
97 mergedAconfigFiles := make(map[string]Paths)
98 ctx.VisitDirectDepsIgnoreBlueprint(func(module Module) {
99 // If any of our dependencies have aconfig declarations (directly or propagated), then merge those and provide them.
100 if dep, ok := OtherModuleProvider(ctx, module, AconfigDeclarationsProviderKey); ok {
101 mergedAconfigFiles[dep.Container] = append(mergedAconfigFiles[dep.Container], dep.IntermediateCacheOutputPath)
102 }
103 if dep, ok := OtherModuleProvider(ctx, module, aconfigPropagatingProviderKey); ok {
104 for container, v := range dep.AconfigFiles {
105 mergedAconfigFiles[container] = append(mergedAconfigFiles[container], v...)
106 }
107 }
108 if dep, ok := OtherModuleProvider(ctx, module, AconfigTransitiveDeclarationsInfoProvider); ok {
109 for container, v := range dep.AconfigFiles {
110 mergedAconfigFiles[container] = append(mergedAconfigFiles[container], v...)
111 }
112 }
113 })
114 // We only need to set the provider if we have aconfig files.
115 if len(mergedAconfigFiles) > 0 {
116 for container, aconfigFiles := range mergedAconfigFiles {
117 mergedAconfigFiles[container] = mergeAconfigFiles(ctx, container, aconfigFiles, true)
118 }
119
120 SetProvider(ctx, aconfigPropagatingProviderKey, aconfigPropagatingDeclarationsInfo{
121 AconfigFiles: mergedAconfigFiles,
122 })
123 }
124}
125
126func aconfigUpdateAndroidMkData(ctx fillInEntriesContext, mod Module, data *AndroidMkData) {
127 info, ok := SingletonModuleProvider(ctx, mod, aconfigPropagatingProviderKey)
128 // If there is no aconfigPropagatingProvider, or there are no AconfigFiles, then we are done.
129 if !ok || len(info.AconfigFiles) == 0 {
130 return
131 }
132 data.Extra = append(data.Extra, func(w io.Writer, outputFile Path) {
133 AndroidMkEmitAssignList(w, "LOCAL_ACONFIG_FILES", getAconfigFilePaths(mod.base(), info.AconfigFiles).Strings())
134 })
135 // If there is a Custom writer, it needs to support this provider.
136 if data.Custom != nil {
137 switch reflect.TypeOf(mod).String() {
138 case "*aidl.aidlApi": // writes non-custom before adding .phony
139 case "*android_sdk.sdkRepoHost": // doesn't go through base_rules
140 case "*apex.apexBundle": // aconfig_file properties written
141 case "*bpf.bpf": // properties written (both for module and objs)
142 case "*genrule.Module": // writes non-custom before adding .phony
143 case "*java.SystemModules": // doesn't go through base_rules
144 case "*phony.phony": // properties written
145 case "*phony.PhonyRule": // writes phony deps and acts like `.PHONY`
146 case "*sysprop.syspropLibrary": // properties written
147 default:
148 panic(fmt.Errorf("custom make rules do not handle aconfig files for %q (%q) module %q", ctx.ModuleType(mod), reflect.TypeOf(mod), mod))
149 }
150 }
151}
152
153func aconfigUpdateAndroidMkEntries(ctx fillInEntriesContext, mod Module, entries *[]AndroidMkEntries) {
154 // If there are no entries, then we can ignore this module, even if it has aconfig files.
155 if len(*entries) == 0 {
156 return
157 }
158 info, ok := SingletonModuleProvider(ctx, mod, aconfigPropagatingProviderKey)
159 if !ok || len(info.AconfigFiles) == 0 {
160 return
161 }
162 // All of the files in the module potentially depend on the aconfig flag values.
163 for idx, _ := range *entries {
164 (*entries)[idx].ExtraEntries = append((*entries)[idx].ExtraEntries,
165 func(ctx AndroidMkExtraEntriesContext, entries *AndroidMkEntries) {
166 setAconfigFileMkEntries(mod.base(), entries, info.AconfigFiles)
167 },
168 )
169
170 }
171}
172
173func mergeAconfigFiles(ctx ModuleContext, container string, inputs Paths, generateRule bool) Paths {
LaMont Jonesaa005ae2023-12-19 19:01:57 +0000174 inputs = LastUniquePaths(inputs)
175 if len(inputs) == 1 {
176 return Paths{inputs[0]}
177 }
178
Yu Liuedeadbf2024-01-10 23:07:35 +0000179 output := PathForModuleOut(ctx, container, "aconfig_merged.pb")
LaMont Jonesaa005ae2023-12-19 19:01:57 +0000180
LaMont Jonesb5099382024-01-10 23:42:36 +0000181 if generateRule {
182 ctx.Build(pctx, BuildParams{
183 Rule: mergeAconfigFilesRule,
184 Description: "merge aconfig files",
185 Inputs: inputs,
186 Output: output,
187 Args: map[string]string{
188 "flags": JoinWithPrefix(inputs.Strings(), "--cache "),
189 },
190 })
191 }
LaMont Jonesaa005ae2023-12-19 19:01:57 +0000192
193 return Paths{output}
194}
LaMont Jonesacae2d72024-01-09 22:53:52 +0000195
LaMont Jonesb5099382024-01-10 23:42:36 +0000196func setAconfigFileMkEntries(m *ModuleBase, entries *AndroidMkEntries, aconfigFiles map[string]Paths) {
197 entries.AddPaths("LOCAL_ACONFIG_FILES", getAconfigFilePaths(m, aconfigFiles))
198}
199
200func getAconfigFilePaths(m *ModuleBase, aconfigFiles map[string]Paths) (paths Paths) {
LaMont Jonesacae2d72024-01-09 22:53:52 +0000201 // TODO(b/311155208): The default container here should be system.
LaMont Jonesb5099382024-01-10 23:42:36 +0000202 container := "system"
LaMont Jonesacae2d72024-01-09 22:53:52 +0000203
204 if m.SocSpecific() {
205 container = "vendor"
206 } else if m.ProductSpecific() {
207 container = "product"
208 } else if m.SystemExtSpecific() {
209 container = "system_ext"
210 }
211
LaMont Jonesb5099382024-01-10 23:42:36 +0000212 paths = append(paths, aconfigFiles[container]...)
213 if container == "system" {
214 // TODO(b/311155208): Once the default container is system, we can drop this.
215 paths = append(paths, aconfigFiles[""]...)
216 }
217 if container != "system" {
218 if len(aconfigFiles[container]) == 0 && len(aconfigFiles[""]) > 0 {
219 // TODO(b/308625757): Either we guessed the container wrong, or the flag is misdeclared.
220 // For now, just include the system (aka "") container if we get here.
221 //fmt.Printf("container_mismatch: module=%v container=%v files=%v\n", m, container, aconfigFiles)
222 }
223 paths = append(paths, aconfigFiles[""]...)
224 }
225 return
LaMont Jonesacae2d72024-01-09 22:53:52 +0000226}