blob: e6a7eac1ad63f0f82edc1030fff8f114c2d6a88a [file] [log] [blame]
Jihoon Kangadd2bb22024-11-05 22:29:34 +00001// Copyright (C) 2024 The Android Open Source Project
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 fsgen
16
17import (
18 "android/soong/android"
19 "fmt"
20 "slices"
Jihoon Kang3a8759c2024-11-08 19:35:09 +000021 "strings"
Jihoon Kangadd2bb22024-11-05 22:29:34 +000022 "sync"
23
24 "github.com/google/blueprint/proptools"
25)
26
27func RegisterCollectFileSystemDepsMutators(ctx android.RegisterMutatorsContext) {
28 ctx.BottomUp("fs_collect_deps", collectDepsMutator).MutatesGlobalState()
29 ctx.BottomUp("fs_set_deps", setDepsMutator)
30}
31
32var fsGenStateOnceKey = android.NewOnceKey("FsGenState")
33var fsGenRemoveOverridesOnceKey = android.NewOnceKey("FsGenRemoveOverrides")
34
35// Map of partition module name to its partition that may be generated by Soong.
36// Note that it is not guaranteed that all modules returned by this function are successfully
37// created.
38func getAllSoongGeneratedPartitionNames(config android.Config, partitions []string) map[string]string {
39 ret := map[string]string{}
40 for _, partition := range partitions {
41 ret[generatedModuleNameForPartition(config, partition)] = partition
42 }
43 return ret
44}
45
46type depCandidateProps struct {
47 Namespace string
48 Multilib string
49 Arch []android.ArchType
50}
51
52// Map of module name to depCandidateProps
53type multilibDeps map[string]*depCandidateProps
54
55// Information necessary to generate the filesystem modules, including details about their
56// dependencies
57type FsGenState struct {
58 // List of modules in `PRODUCT_PACKAGES` and `PRODUCT_PACKAGES_DEBUG`
59 depCandidates []string
60 // Map of names of partition to the information of modules to be added as deps
61 fsDeps map[string]*multilibDeps
62 // List of name of partitions to be generated by the filesystem_creator module
63 soongGeneratedPartitions []string
64 // Mutex to protect the fsDeps
65 fsDepsMutex sync.Mutex
66 // Map of _all_ soong module names to their corresponding installation properties
67 moduleToInstallationProps map[string]installationProperties
68}
69
70type installationProperties struct {
71 Required []string
72 Overrides []string
73}
74
75func defaultDepCandidateProps(config android.Config) *depCandidateProps {
76 return &depCandidateProps{
77 Namespace: ".",
78 Arch: []android.ArchType{config.BuildArch},
79 }
80}
81
82func generatedPartitions(ctx android.LoadHookContext) []string {
83 generatedPartitions := []string{"system"}
84 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
85 generatedPartitions = append(generatedPartitions, "system_ext")
86 }
87 if ctx.DeviceConfig().BuildingVendorImage() && ctx.DeviceConfig().VendorPath() == "vendor" {
88 generatedPartitions = append(generatedPartitions, "vendor")
89 }
90 if ctx.DeviceConfig().BuildingProductImage() && ctx.DeviceConfig().ProductPath() == "product" {
91 generatedPartitions = append(generatedPartitions, "product")
92 }
93 if ctx.DeviceConfig().BuildingOdmImage() && ctx.DeviceConfig().OdmPath() == "odm" {
94 generatedPartitions = append(generatedPartitions, "odm")
95 }
96 if ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.BuildingSystemDlkmImage {
97 generatedPartitions = append(generatedPartitions, "system_dlkm")
98 }
99 return generatedPartitions
100}
101
102func createFsGenState(ctx android.LoadHookContext, generatedPrebuiltEtcModuleNames []string) *FsGenState {
103 return ctx.Config().Once(fsGenStateOnceKey, func() interface{} {
104 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
105 candidates := android.FirstUniqueStrings(android.Concat(partitionVars.ProductPackages, partitionVars.ProductPackagesDebug))
106 candidates = android.Concat(candidates, generatedPrebuiltEtcModuleNames)
107
108 return &FsGenState{
109 depCandidates: candidates,
110 fsDeps: map[string]*multilibDeps{
111 // These additional deps are added according to the cuttlefish system image bp.
112 "system": {
113 "com.android.apex.cts.shim.v1_prebuilt": defaultDepCandidateProps(ctx.Config()),
114 "dex_bootjars": defaultDepCandidateProps(ctx.Config()),
115 "framework_compatibility_matrix.device.xml": defaultDepCandidateProps(ctx.Config()),
116 "init.environ.rc-soong": defaultDepCandidateProps(ctx.Config()),
117 "libclang_rt.asan": defaultDepCandidateProps(ctx.Config()),
118 "libcompiler_rt": defaultDepCandidateProps(ctx.Config()),
119 "libdmabufheap": defaultDepCandidateProps(ctx.Config()),
120 "libgsi": defaultDepCandidateProps(ctx.Config()),
121 "llndk.libraries.txt": defaultDepCandidateProps(ctx.Config()),
122 "logpersist.start": defaultDepCandidateProps(ctx.Config()),
123 "update_engine_sideload": defaultDepCandidateProps(ctx.Config()),
124 },
125 "vendor": {
126 "fs_config_files_vendor": defaultDepCandidateProps(ctx.Config()),
127 "fs_config_dirs_vendor": defaultDepCandidateProps(ctx.Config()),
128 generatedModuleName(ctx.Config(), "vendor-build.prop"): defaultDepCandidateProps(ctx.Config()),
129 },
130 "odm": {
131 // fs_config_* files are automatically installed for all products with odm partitions.
132 // https://cs.android.com/android/_/android/platform/build/+/e4849e87ab660b59a6501b3928693db065ee873b:tools/fs_config/Android.mk;l=34;drc=8d6481b92c4b4e9b9f31a61545b6862090fcc14b;bpv=1;bpt=0
133 "fs_config_files_odm": defaultDepCandidateProps(ctx.Config()),
134 "fs_config_dirs_odm": defaultDepCandidateProps(ctx.Config()),
135 },
136 "product": {},
137 "system_ext": {
138 // VNDK apexes are automatically included.
139 // This hardcoded list will need to be updated if `PRODUCT_EXTRA_VNDK_VERSIONS` is updated.
140 // https://cs.android.com/android/_/android/platform/build/+/adba533072b00c53ac0f198c550a3cbd7a00e4cd:core/main.mk;l=984;bpv=1;bpt=0;drc=174db7b179592cf07cbfd2adb0119486fda911e7
141 "com.android.vndk.v30": defaultDepCandidateProps(ctx.Config()),
142 "com.android.vndk.v31": defaultDepCandidateProps(ctx.Config()),
143 "com.android.vndk.v32": defaultDepCandidateProps(ctx.Config()),
144 "com.android.vndk.v33": defaultDepCandidateProps(ctx.Config()),
145 "com.android.vndk.v34": defaultDepCandidateProps(ctx.Config()),
146 },
147 "system_dlkm": {},
148 },
149 soongGeneratedPartitions: generatedPartitions(ctx),
150 fsDepsMutex: sync.Mutex{},
151 moduleToInstallationProps: map[string]installationProperties{},
152 }
153 }).(*FsGenState)
154}
155
156func checkDepModuleInMultipleNamespaces(mctx android.BottomUpMutatorContext, foundDeps multilibDeps, module string, partitionName string) {
157 otherNamespace := mctx.Namespace().Path
158 if val, found := foundDeps[module]; found && otherNamespace != "." && !android.InList(val.Namespace, []string{".", otherNamespace}) {
159 mctx.ModuleErrorf("found in multiple namespaces(%s and %s) when including in %s partition", val.Namespace, otherNamespace, partitionName)
160 }
161}
162
163func appendDepIfAppropriate(mctx android.BottomUpMutatorContext, deps *multilibDeps, installPartition string) {
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000164 moduleName := mctx.ModuleName()
165 checkDepModuleInMultipleNamespaces(mctx, *deps, moduleName, installPartition)
166 if _, ok := (*deps)[moduleName]; ok {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000167 // Prefer the namespace-specific module over the platform module
168 if mctx.Namespace().Path != "." {
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000169 (*deps)[moduleName].Namespace = mctx.Namespace().Path
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000170 }
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000171 (*deps)[moduleName].Arch = append((*deps)[moduleName].Arch, mctx.Module().Target().Arch.ArchType)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000172 } else {
173 multilib, _ := mctx.Module().DecodeMultilib(mctx)
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000174 (*deps)[moduleName] = &depCandidateProps{
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000175 Namespace: mctx.Namespace().Path,
176 Multilib: multilib,
177 Arch: []android.ArchType{mctx.Module().Target().Arch.ArchType},
178 }
179 }
180}
181
182func collectDepsMutator(mctx android.BottomUpMutatorContext) {
183 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
184
185 m := mctx.Module()
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000186 if m.Target().Os.Class == android.Device && slices.Contains(fsGenState.depCandidates, mctx.ModuleName()) {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000187 installPartition := m.PartitionTag(mctx.DeviceConfig())
188 fsGenState.fsDepsMutex.Lock()
189 // Only add the module as dependency when:
190 // - its enabled
191 // - its namespace is included in PRODUCT_SOONG_NAMESPACES
192 if m.Enabled(mctx) && m.ExportedToMake() {
193 appendDepIfAppropriate(mctx, fsGenState.fsDeps[installPartition], installPartition)
194 }
195 fsGenState.fsDepsMutex.Unlock()
196 }
197 // store the map of module to (required,overrides) even if the module is not in PRODUCT_PACKAGES.
198 // the module might be installed transitively.
199 if m.Target().Os.Class == android.Device && m.Enabled(mctx) && m.ExportedToMake() {
200 fsGenState.fsDepsMutex.Lock()
201 fsGenState.moduleToInstallationProps[m.Name()] = installationProperties{
202 Required: m.RequiredModuleNames(mctx),
203 Overrides: m.Overrides(),
204 }
205 fsGenState.fsDepsMutex.Unlock()
206 }
207}
208
209type depsStruct struct {
210 Deps []string
211}
212
213type multilibDepsStruct struct {
214 Common depsStruct
215 Lib32 depsStruct
216 Lib64 depsStruct
217 Both depsStruct
218 Prefer32 depsStruct
219}
220
221type packagingPropsStruct struct {
222 High_priority_deps []string
223 Deps []string
224 Multilib multilibDepsStruct
225}
226
227func fullyQualifiedModuleName(moduleName, namespace string) string {
228 if namespace == "." {
229 return moduleName
230 }
231 return fmt.Sprintf("//%s:%s", namespace, moduleName)
232}
233
234func getBitness(archTypes []android.ArchType) (ret []string) {
235 for _, archType := range archTypes {
236 if archType.Multilib == "" {
237 ret = append(ret, android.COMMON_VARIANT)
238 } else {
239 ret = append(ret, archType.Bitness())
240 }
241 }
242 return ret
243}
244
245func setDepsMutator(mctx android.BottomUpMutatorContext) {
246 removeOverriddenDeps(mctx)
247 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
248 fsDeps := fsGenState.fsDeps
249 soongGeneratedPartitionMap := getAllSoongGeneratedPartitionNames(mctx.Config(), fsGenState.soongGeneratedPartitions)
250 m := mctx.Module()
251 if partition, ok := soongGeneratedPartitionMap[m.Name()]; ok {
252 depsStruct := generateDepStruct(*fsDeps[partition])
253 if err := proptools.AppendMatchingProperties(m.GetProperties(), depsStruct, nil); err != nil {
254 mctx.ModuleErrorf(err.Error())
255 }
256 }
257}
258
259// removeOverriddenDeps collects PRODUCT_PACKAGES and (transitive) required deps.
260// it then removes any modules which appear in `overrides` of the above list.
261func removeOverriddenDeps(mctx android.BottomUpMutatorContext) {
262 mctx.Config().Once(fsGenRemoveOverridesOnceKey, func() interface{} {
263 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
264 fsDeps := fsGenState.fsDeps
265 overridden := map[string]bool{}
266 allDeps := []string{}
267
268 // Step 1: Initialization: Append PRODUCT_PACKAGES to the queue
269 for _, fsDep := range fsDeps {
270 for depName, _ := range *fsDep {
271 allDeps = append(allDeps, depName)
272 }
273 }
274
275 // Step 2: Process the queue, and add required modules to the queue.
276 i := 0
277 for {
278 if i == len(allDeps) {
279 break
280 }
281 depName := allDeps[i]
282 for _, overrides := range fsGenState.moduleToInstallationProps[depName].Overrides {
283 overridden[overrides] = true
284 }
285 // add required dep to the queue.
286 allDeps = append(allDeps, fsGenState.moduleToInstallationProps[depName].Required...)
287 i += 1
288 }
289
290 // Step 3: Delete all the overridden modules.
291 for overridden, _ := range overridden {
292 for partition, _ := range fsDeps {
293 delete(*fsDeps[partition], overridden)
294 }
295 }
296 return nil
297 })
298}
299
300var HighPriorityDeps = []string{}
301
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000302func isHighPriorityDep(depName string) bool {
303 for _, highPriorityDeps := range HighPriorityDeps {
304 if strings.HasPrefix(depName, highPriorityDeps) {
305 return true
306 }
307 }
308 return false
309}
310
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000311func generateDepStruct(deps map[string]*depCandidateProps) *packagingPropsStruct {
312 depsStruct := packagingPropsStruct{}
313 for depName, depProps := range deps {
314 bitness := getBitness(depProps.Arch)
315 fullyQualifiedDepName := fullyQualifiedModuleName(depName, depProps.Namespace)
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000316 if isHighPriorityDep(depName) {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000317 depsStruct.High_priority_deps = append(depsStruct.High_priority_deps, fullyQualifiedDepName)
318 } else if android.InList("32", bitness) && android.InList("64", bitness) {
319 // If both 32 and 64 bit variants are enabled for this module
320 switch depProps.Multilib {
321 case string(android.MultilibBoth):
322 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
323 case string(android.MultilibCommon), string(android.MultilibFirst):
324 depsStruct.Deps = append(depsStruct.Deps, fullyQualifiedDepName)
325 case "32":
326 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
327 case "64", "darwin_universal":
328 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
329 case "prefer32", "first_prefer32":
330 depsStruct.Multilib.Prefer32.Deps = append(depsStruct.Multilib.Prefer32.Deps, fullyQualifiedDepName)
331 default:
332 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
333 }
334 } else if android.InList("64", bitness) {
335 // If only 64 bit variant is enabled
336 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
337 } else if android.InList("32", bitness) {
338 // If only 32 bit variant is enabled
339 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
340 } else {
341 // If only common variant is enabled
342 depsStruct.Multilib.Common.Deps = append(depsStruct.Multilib.Common.Deps, fullyQualifiedDepName)
343 }
344 }
345 depsStruct.Deps = android.SortedUniqueStrings(depsStruct.Deps)
346 depsStruct.Multilib.Lib32.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Lib32.Deps)
347 depsStruct.Multilib.Lib64.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Lib64.Deps)
348 depsStruct.Multilib.Prefer32.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Prefer32.Deps)
349 depsStruct.Multilib.Both.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Both.Deps)
350 depsStruct.Multilib.Common.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Common.Deps)
351
352 return &depsStruct
353}