blob: f0eea8e44fe11fe832010658ad0ced3733a9fb9b [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 (
Jihoon Kangadd2bb22024-11-05 22:29:34 +000018 "fmt"
19 "slices"
Jihoon Kang3a8759c2024-11-08 19:35:09 +000020 "strings"
Jihoon Kangadd2bb22024-11-05 22:29:34 +000021 "sync"
22
mrziwange5b1bb32024-11-05 15:51:40 -080023 "android/soong/android"
24
Jihoon Kangadd2bb22024-11-05 22:29:34 +000025 "github.com/google/blueprint/proptools"
26)
27
28func RegisterCollectFileSystemDepsMutators(ctx android.RegisterMutatorsContext) {
29 ctx.BottomUp("fs_collect_deps", collectDepsMutator).MutatesGlobalState()
30 ctx.BottomUp("fs_set_deps", setDepsMutator)
31}
32
33var fsGenStateOnceKey = android.NewOnceKey("FsGenState")
34var fsGenRemoveOverridesOnceKey = android.NewOnceKey("FsGenRemoveOverrides")
35
36// Map of partition module name to its partition that may be generated by Soong.
37// Note that it is not guaranteed that all modules returned by this function are successfully
38// created.
39func getAllSoongGeneratedPartitionNames(config android.Config, partitions []string) map[string]string {
40 ret := map[string]string{}
41 for _, partition := range partitions {
42 ret[generatedModuleNameForPartition(config, partition)] = partition
43 }
44 return ret
45}
46
47type depCandidateProps struct {
48 Namespace string
49 Multilib string
50 Arch []android.ArchType
51}
52
53// Map of module name to depCandidateProps
54type multilibDeps map[string]*depCandidateProps
55
56// Information necessary to generate the filesystem modules, including details about their
57// dependencies
58type FsGenState struct {
59 // List of modules in `PRODUCT_PACKAGES` and `PRODUCT_PACKAGES_DEBUG`
60 depCandidates []string
61 // Map of names of partition to the information of modules to be added as deps
62 fsDeps map[string]*multilibDeps
Cole Faust76e8aa12025-01-27 18:21:31 -080063 // Information about the main soong-generated partitions
64 soongGeneratedPartitions allGeneratedPartitionData
Jihoon Kangadd2bb22024-11-05 22:29:34 +000065 // Mutex to protect the fsDeps
66 fsDepsMutex sync.Mutex
67 // Map of _all_ soong module names to their corresponding installation properties
68 moduleToInstallationProps map[string]installationProperties
Jihoon Kang0d7b0112024-11-13 20:44:05 +000069 // List of prebuilt_* modules that are autogenerated.
70 generatedPrebuiltEtcModuleNames []string
Cole Faust953476f2024-11-14 14:11:29 -080071 // Mapping from a path to an avb key to the name of a filegroup module that contains it
72 avbKeyFilegroups map[string]string
Jihoon Kangadd2bb22024-11-05 22:29:34 +000073}
74
75type installationProperties struct {
76 Required []string
77 Overrides []string
78}
79
80func defaultDepCandidateProps(config android.Config) *depCandidateProps {
81 return &depCandidateProps{
82 Namespace: ".",
83 Arch: []android.ArchType{config.BuildArch},
84 }
85}
86
Jihoon Kang04f12c92024-11-12 23:03:08 +000087func createFsGenState(ctx android.LoadHookContext, generatedPrebuiltEtcModuleNames []string, avbpubkeyGenerated bool) *FsGenState {
Jihoon Kangadd2bb22024-11-05 22:29:34 +000088 return ctx.Config().Once(fsGenStateOnceKey, func() interface{} {
89 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
90 candidates := android.FirstUniqueStrings(android.Concat(partitionVars.ProductPackages, partitionVars.ProductPackagesDebug))
91 candidates = android.Concat(candidates, generatedPrebuiltEtcModuleNames)
92
Jihoon Kang04f12c92024-11-12 23:03:08 +000093 fsGenState := FsGenState{
Jihoon Kangadd2bb22024-11-05 22:29:34 +000094 depCandidates: candidates,
95 fsDeps: map[string]*multilibDeps{
96 // These additional deps are added according to the cuttlefish system image bp.
97 "system": {
Cole Faust1d4e76c2024-11-26 14:15:29 -080098 // keep-sorted start
Jihoon Kangadd2bb22024-11-05 22:29:34 +000099 "com.android.apex.cts.shim.v1_prebuilt": defaultDepCandidateProps(ctx.Config()),
100 "dex_bootjars": defaultDepCandidateProps(ctx.Config()),
101 "framework_compatibility_matrix.device.xml": defaultDepCandidateProps(ctx.Config()),
Cole Faust1d4e76c2024-11-26 14:15:29 -0800102 "init.environ.rc-soong": defaultDepCandidateProps(ctx.Config()),
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000103 "libcompiler_rt": defaultDepCandidateProps(ctx.Config()),
104 "libdmabufheap": defaultDepCandidateProps(ctx.Config()),
105 "libgsi": defaultDepCandidateProps(ctx.Config()),
106 "llndk.libraries.txt": defaultDepCandidateProps(ctx.Config()),
107 "logpersist.start": defaultDepCandidateProps(ctx.Config()),
108 "update_engine_sideload": defaultDepCandidateProps(ctx.Config()),
Cole Faust1d4e76c2024-11-26 14:15:29 -0800109 // keep-sorted end
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000110 },
111 "vendor": {
112 "fs_config_files_vendor": defaultDepCandidateProps(ctx.Config()),
113 "fs_config_dirs_vendor": defaultDepCandidateProps(ctx.Config()),
114 generatedModuleName(ctx.Config(), "vendor-build.prop"): defaultDepCandidateProps(ctx.Config()),
115 },
116 "odm": {
117 // fs_config_* files are automatically installed for all products with odm partitions.
118 // https://cs.android.com/android/_/android/platform/build/+/e4849e87ab660b59a6501b3928693db065ee873b:tools/fs_config/Android.mk;l=34;drc=8d6481b92c4b4e9b9f31a61545b6862090fcc14b;bpv=1;bpt=0
119 "fs_config_files_odm": defaultDepCandidateProps(ctx.Config()),
120 "fs_config_dirs_odm": defaultDepCandidateProps(ctx.Config()),
121 },
122 "product": {},
123 "system_ext": {
124 // VNDK apexes are automatically included.
125 // This hardcoded list will need to be updated if `PRODUCT_EXTRA_VNDK_VERSIONS` is updated.
126 // https://cs.android.com/android/_/android/platform/build/+/adba533072b00c53ac0f198c550a3cbd7a00e4cd:core/main.mk;l=984;bpv=1;bpt=0;drc=174db7b179592cf07cbfd2adb0119486fda911e7
127 "com.android.vndk.v30": defaultDepCandidateProps(ctx.Config()),
128 "com.android.vndk.v31": defaultDepCandidateProps(ctx.Config()),
129 "com.android.vndk.v32": defaultDepCandidateProps(ctx.Config()),
130 "com.android.vndk.v33": defaultDepCandidateProps(ctx.Config()),
131 "com.android.vndk.v34": defaultDepCandidateProps(ctx.Config()),
132 },
Spandan Das912d26b2024-11-06 19:35:17 +0000133 "userdata": {},
134 "system_dlkm": {
135 // these are phony required deps of the phony fs_config_dirs_nonsystem
136 "fs_config_dirs_system_dlkm": defaultDepCandidateProps(ctx.Config()),
137 "fs_config_files_system_dlkm": defaultDepCandidateProps(ctx.Config()),
138 // build props are automatically added to `ALL_DEFAULT_INSTALLED_MODULES`
139 "system_dlkm-build.prop": defaultDepCandidateProps(ctx.Config()),
140 },
Spandan Das5b493cd2024-11-07 20:55:56 +0000141 "vendor_dlkm": {
142 "fs_config_dirs_vendor_dlkm": defaultDepCandidateProps(ctx.Config()),
143 "fs_config_files_vendor_dlkm": defaultDepCandidateProps(ctx.Config()),
144 "vendor_dlkm-build.prop": defaultDepCandidateProps(ctx.Config()),
145 },
146 "odm_dlkm": {
147 "fs_config_dirs_odm_dlkm": defaultDepCandidateProps(ctx.Config()),
148 "fs_config_files_odm_dlkm": defaultDepCandidateProps(ctx.Config()),
149 "odm_dlkm-build.prop": defaultDepCandidateProps(ctx.Config()),
150 },
Cole Faust24938e22024-11-18 14:01:58 -0800151 "ramdisk": {},
152 "vendor_ramdisk": {},
Jihoon Kangc44172e2024-12-23 21:30:08 +0000153 "recovery": {
154 "sepolicy.recovery": defaultDepCandidateProps(ctx.Config()),
155 "plat_file_contexts.recovery": defaultDepCandidateProps(ctx.Config()),
156 "plat_service_contexts.recovery": defaultDepCandidateProps(ctx.Config()),
157 "plat_property_contexts.recovery": defaultDepCandidateProps(ctx.Config()),
158 "system_ext_file_contexts.recovery": defaultDepCandidateProps(ctx.Config()),
159 "system_ext_service_contexts.recovery": defaultDepCandidateProps(ctx.Config()),
160 "system_ext_property_contexts.recovery": defaultDepCandidateProps(ctx.Config()),
161 "vendor_file_contexts.recovery": defaultDepCandidateProps(ctx.Config()),
162 "vendor_service_contexts.recovery": defaultDepCandidateProps(ctx.Config()),
163 "vendor_property_contexts.recovery": defaultDepCandidateProps(ctx.Config()),
164 "odm_file_contexts.recovery": defaultDepCandidateProps(ctx.Config()),
165 "odm_property_contexts.recovery": defaultDepCandidateProps(ctx.Config()),
166 "product_file_contexts.recovery": defaultDepCandidateProps(ctx.Config()),
167 "product_service_contexts.recovery": defaultDepCandidateProps(ctx.Config()),
168 "product_property_contexts.recovery": defaultDepCandidateProps(ctx.Config()),
169 },
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000170 },
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000171 fsDepsMutex: sync.Mutex{},
172 moduleToInstallationProps: map[string]installationProperties{},
173 generatedPrebuiltEtcModuleNames: generatedPrebuiltEtcModuleNames,
Cole Faust953476f2024-11-14 14:11:29 -0800174 avbKeyFilegroups: map[string]string{},
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000175 }
Jihoon Kang04f12c92024-11-12 23:03:08 +0000176
177 if avbpubkeyGenerated {
178 (*fsGenState.fsDeps["product"])["system_other_avbpubkey"] = defaultDepCandidateProps(ctx.Config())
179 }
180
Jihoon Kang2f7efc02025-03-12 22:35:21 +0000181 if len(ctx.Config().DeviceManifestFiles()) > 0 {
182 (*fsGenState.fsDeps["vendor"])["vendor_manifest.xml"] = defaultDepCandidateProps(ctx.Config())
183 }
184
Jihoon Kang45893372024-12-04 00:15:36 +0000185 // Add common resources `prebuilt_res` module as dep of recovery partition
186 (*fsGenState.fsDeps["recovery"])[fmt.Sprintf("recovery-resources-common-%s", getDpi(ctx))] = defaultDepCandidateProps(ctx.Config())
Jihoon Kangd5244352024-12-10 00:06:51 +0000187 (*fsGenState.fsDeps["recovery"])[getRecoveryFontModuleName(ctx)] = defaultDepCandidateProps(ctx.Config())
Jihoon Kangefd04b92024-12-10 23:35:09 +0000188 (*fsGenState.fsDeps["recovery"])[createRecoveryBuildProp(ctx)] = defaultDepCandidateProps(ctx.Config())
Jihoon Kang45893372024-12-04 00:15:36 +0000189
Jihoon Kang04f12c92024-11-12 23:03:08 +0000190 return &fsGenState
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000191 }).(*FsGenState)
192}
193
194func checkDepModuleInMultipleNamespaces(mctx android.BottomUpMutatorContext, foundDeps multilibDeps, module string, partitionName string) {
195 otherNamespace := mctx.Namespace().Path
196 if val, found := foundDeps[module]; found && otherNamespace != "." && !android.InList(val.Namespace, []string{".", otherNamespace}) {
197 mctx.ModuleErrorf("found in multiple namespaces(%s and %s) when including in %s partition", val.Namespace, otherNamespace, partitionName)
198 }
199}
200
201func appendDepIfAppropriate(mctx android.BottomUpMutatorContext, deps *multilibDeps, installPartition string) {
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000202 moduleName := mctx.ModuleName()
203 checkDepModuleInMultipleNamespaces(mctx, *deps, moduleName, installPartition)
204 if _, ok := (*deps)[moduleName]; ok {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000205 // Prefer the namespace-specific module over the platform module
206 if mctx.Namespace().Path != "." {
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000207 (*deps)[moduleName].Namespace = mctx.Namespace().Path
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000208 }
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000209 (*deps)[moduleName].Arch = append((*deps)[moduleName].Arch, mctx.Module().Target().Arch.ArchType)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000210 } else {
211 multilib, _ := mctx.Module().DecodeMultilib(mctx)
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000212 (*deps)[moduleName] = &depCandidateProps{
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000213 Namespace: mctx.Namespace().Path,
214 Multilib: multilib,
215 Arch: []android.ArchType{mctx.Module().Target().Arch.ArchType},
216 }
217 }
218}
219
220func collectDepsMutator(mctx android.BottomUpMutatorContext) {
Cole Faust76a6e952024-11-07 16:56:45 -0800221 m := mctx.Module()
222 if m.Target().Os.Class != android.Device {
223 return
224 }
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000225 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
226
Cole Faust76a6e952024-11-07 16:56:45 -0800227 fsGenState.fsDepsMutex.Lock()
228 defer fsGenState.fsDepsMutex.Unlock()
229
230 if slices.Contains(fsGenState.depCandidates, mctx.ModuleName()) {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000231 installPartition := m.PartitionTag(mctx.DeviceConfig())
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000232 // Only add the module as dependency when:
233 // - its enabled
234 // - its namespace is included in PRODUCT_SOONG_NAMESPACES
235 if m.Enabled(mctx) && m.ExportedToMake() {
236 appendDepIfAppropriate(mctx, fsGenState.fsDeps[installPartition], installPartition)
237 }
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000238 }
239 // store the map of module to (required,overrides) even if the module is not in PRODUCT_PACKAGES.
240 // the module might be installed transitively.
Cole Faust76a6e952024-11-07 16:56:45 -0800241 if m.Enabled(mctx) && m.ExportedToMake() {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000242 fsGenState.moduleToInstallationProps[m.Name()] = installationProperties{
243 Required: m.RequiredModuleNames(mctx),
244 Overrides: m.Overrides(),
245 }
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000246 }
247}
248
249type depsStruct struct {
250 Deps []string
251}
252
253type multilibDepsStruct struct {
254 Common depsStruct
255 Lib32 depsStruct
256 Lib64 depsStruct
257 Both depsStruct
258 Prefer32 depsStruct
259}
260
261type packagingPropsStruct struct {
262 High_priority_deps []string
263 Deps []string
264 Multilib multilibDepsStruct
265}
266
267func fullyQualifiedModuleName(moduleName, namespace string) string {
268 if namespace == "." {
269 return moduleName
270 }
271 return fmt.Sprintf("//%s:%s", namespace, moduleName)
272}
273
274func getBitness(archTypes []android.ArchType) (ret []string) {
275 for _, archType := range archTypes {
276 if archType.Multilib == "" {
277 ret = append(ret, android.COMMON_VARIANT)
278 } else {
279 ret = append(ret, archType.Bitness())
280 }
281 }
282 return ret
283}
284
285func setDepsMutator(mctx android.BottomUpMutatorContext) {
286 removeOverriddenDeps(mctx)
287 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
288 fsDeps := fsGenState.fsDeps
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000289 m := mctx.Module()
Cole Faust76e8aa12025-01-27 18:21:31 -0800290 if partition := fsGenState.soongGeneratedPartitions.typeForName(m.Name()); partition != "" {
291 if fsGenState.soongGeneratedPartitions.isHandwritten(m.Name()) {
292 // Handwritten image, don't modify it
293 return
294 }
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000295 depsStruct := generateDepStruct(*fsDeps[partition], fsGenState.generatedPrebuiltEtcModuleNames)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000296 if err := proptools.AppendMatchingProperties(m.GetProperties(), depsStruct, nil); err != nil {
297 mctx.ModuleErrorf(err.Error())
298 }
299 }
300}
301
302// removeOverriddenDeps collects PRODUCT_PACKAGES and (transitive) required deps.
303// it then removes any modules which appear in `overrides` of the above list.
304func removeOverriddenDeps(mctx android.BottomUpMutatorContext) {
305 mctx.Config().Once(fsGenRemoveOverridesOnceKey, func() interface{} {
306 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
307 fsDeps := fsGenState.fsDeps
308 overridden := map[string]bool{}
309 allDeps := []string{}
310
311 // Step 1: Initialization: Append PRODUCT_PACKAGES to the queue
312 for _, fsDep := range fsDeps {
313 for depName, _ := range *fsDep {
314 allDeps = append(allDeps, depName)
315 }
316 }
317
318 // Step 2: Process the queue, and add required modules to the queue.
319 i := 0
320 for {
321 if i == len(allDeps) {
322 break
323 }
324 depName := allDeps[i]
325 for _, overrides := range fsGenState.moduleToInstallationProps[depName].Overrides {
326 overridden[overrides] = true
327 }
328 // add required dep to the queue.
329 allDeps = append(allDeps, fsGenState.moduleToInstallationProps[depName].Required...)
330 i += 1
331 }
332
333 // Step 3: Delete all the overridden modules.
334 for overridden, _ := range overridden {
335 for partition, _ := range fsDeps {
336 delete(*fsDeps[partition], overridden)
337 }
338 }
339 return nil
340 })
341}
342
343var HighPriorityDeps = []string{}
344
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000345func isHighPriorityDep(depName string) bool {
346 for _, highPriorityDeps := range HighPriorityDeps {
347 if strings.HasPrefix(depName, highPriorityDeps) {
348 return true
349 }
350 }
351 return false
352}
353
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000354func generateDepStruct(deps map[string]*depCandidateProps, highPriorityDeps []string) *packagingPropsStruct {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000355 depsStruct := packagingPropsStruct{}
356 for depName, depProps := range deps {
357 bitness := getBitness(depProps.Arch)
358 fullyQualifiedDepName := fullyQualifiedModuleName(depName, depProps.Namespace)
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000359 if android.InList(depName, highPriorityDeps) {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000360 depsStruct.High_priority_deps = append(depsStruct.High_priority_deps, fullyQualifiedDepName)
361 } else if android.InList("32", bitness) && android.InList("64", bitness) {
362 // If both 32 and 64 bit variants are enabled for this module
363 switch depProps.Multilib {
364 case string(android.MultilibBoth):
365 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
366 case string(android.MultilibCommon), string(android.MultilibFirst):
367 depsStruct.Deps = append(depsStruct.Deps, fullyQualifiedDepName)
368 case "32":
369 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
370 case "64", "darwin_universal":
371 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
372 case "prefer32", "first_prefer32":
373 depsStruct.Multilib.Prefer32.Deps = append(depsStruct.Multilib.Prefer32.Deps, fullyQualifiedDepName)
374 default:
375 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
376 }
377 } else if android.InList("64", bitness) {
378 // If only 64 bit variant is enabled
379 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
380 } else if android.InList("32", bitness) {
381 // If only 32 bit variant is enabled
382 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
383 } else {
384 // If only common variant is enabled
385 depsStruct.Multilib.Common.Deps = append(depsStruct.Multilib.Common.Deps, fullyQualifiedDepName)
386 }
387 }
388 depsStruct.Deps = android.SortedUniqueStrings(depsStruct.Deps)
389 depsStruct.Multilib.Lib32.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Lib32.Deps)
390 depsStruct.Multilib.Lib64.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Lib64.Deps)
391 depsStruct.Multilib.Prefer32.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Prefer32.Deps)
392 depsStruct.Multilib.Both.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Both.Deps)
393 depsStruct.Multilib.Common.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Common.Deps)
Cole Faust866c6032024-11-13 16:17:48 -0800394 depsStruct.High_priority_deps = android.SortedUniqueStrings(depsStruct.High_priority_deps)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000395
396 return &depsStruct
397}