blob: b6ba6c4705fe21489874a2d7f025cee5897a0a84 [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"
20 "sync"
21
mrziwange5b1bb32024-11-05 15:51:40 -080022 "android/soong/android"
23
Jihoon Kangadd2bb22024-11-05 22:29:34 +000024 "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 {
Cole Faust76a6e952024-11-07 16:56:45 -080083 generatedPartitions := []string{"system", "ramdisk"}
Jihoon Kangadd2bb22024-11-05 22:29:34 +000084 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 }
mrziwang23ba8762024-11-07 16:21:53 -080096 if ctx.DeviceConfig().BuildingUserdataImage() && ctx.DeviceConfig().UserdataPath() == "data" {
97 generatedPartitions = append(generatedPartitions, "userdata")
98 }
Jihoon Kangadd2bb22024-11-05 22:29:34 +000099 if ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.BuildingSystemDlkmImage {
100 generatedPartitions = append(generatedPartitions, "system_dlkm")
101 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000102 if ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.BuildingVendorDlkmImage {
103 generatedPartitions = append(generatedPartitions, "vendor_dlkm")
104 }
105 if ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.BuildingOdmDlkmImage {
106 generatedPartitions = append(generatedPartitions, "odm_dlkm")
107 }
108
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000109 return generatedPartitions
110}
111
112func createFsGenState(ctx android.LoadHookContext, generatedPrebuiltEtcModuleNames []string) *FsGenState {
113 return ctx.Config().Once(fsGenStateOnceKey, func() interface{} {
114 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
115 candidates := android.FirstUniqueStrings(android.Concat(partitionVars.ProductPackages, partitionVars.ProductPackagesDebug))
116 candidates = android.Concat(candidates, generatedPrebuiltEtcModuleNames)
117
118 return &FsGenState{
119 depCandidates: candidates,
120 fsDeps: map[string]*multilibDeps{
121 // These additional deps are added according to the cuttlefish system image bp.
122 "system": {
123 "com.android.apex.cts.shim.v1_prebuilt": defaultDepCandidateProps(ctx.Config()),
124 "dex_bootjars": defaultDepCandidateProps(ctx.Config()),
125 "framework_compatibility_matrix.device.xml": defaultDepCandidateProps(ctx.Config()),
126 "init.environ.rc-soong": defaultDepCandidateProps(ctx.Config()),
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000127 "libcompiler_rt": defaultDepCandidateProps(ctx.Config()),
128 "libdmabufheap": defaultDepCandidateProps(ctx.Config()),
129 "libgsi": defaultDepCandidateProps(ctx.Config()),
130 "llndk.libraries.txt": defaultDepCandidateProps(ctx.Config()),
131 "logpersist.start": defaultDepCandidateProps(ctx.Config()),
132 "update_engine_sideload": defaultDepCandidateProps(ctx.Config()),
133 },
134 "vendor": {
135 "fs_config_files_vendor": defaultDepCandidateProps(ctx.Config()),
136 "fs_config_dirs_vendor": defaultDepCandidateProps(ctx.Config()),
137 generatedModuleName(ctx.Config(), "vendor-build.prop"): defaultDepCandidateProps(ctx.Config()),
138 },
139 "odm": {
140 // fs_config_* files are automatically installed for all products with odm partitions.
141 // https://cs.android.com/android/_/android/platform/build/+/e4849e87ab660b59a6501b3928693db065ee873b:tools/fs_config/Android.mk;l=34;drc=8d6481b92c4b4e9b9f31a61545b6862090fcc14b;bpv=1;bpt=0
142 "fs_config_files_odm": defaultDepCandidateProps(ctx.Config()),
143 "fs_config_dirs_odm": defaultDepCandidateProps(ctx.Config()),
144 },
145 "product": {},
146 "system_ext": {
147 // VNDK apexes are automatically included.
148 // This hardcoded list will need to be updated if `PRODUCT_EXTRA_VNDK_VERSIONS` is updated.
149 // https://cs.android.com/android/_/android/platform/build/+/adba533072b00c53ac0f198c550a3cbd7a00e4cd:core/main.mk;l=984;bpv=1;bpt=0;drc=174db7b179592cf07cbfd2adb0119486fda911e7
150 "com.android.vndk.v30": defaultDepCandidateProps(ctx.Config()),
151 "com.android.vndk.v31": defaultDepCandidateProps(ctx.Config()),
152 "com.android.vndk.v32": defaultDepCandidateProps(ctx.Config()),
153 "com.android.vndk.v33": defaultDepCandidateProps(ctx.Config()),
154 "com.android.vndk.v34": defaultDepCandidateProps(ctx.Config()),
155 },
Spandan Das912d26b2024-11-06 19:35:17 +0000156 "userdata": {},
157 "system_dlkm": {
158 // these are phony required deps of the phony fs_config_dirs_nonsystem
159 "fs_config_dirs_system_dlkm": defaultDepCandidateProps(ctx.Config()),
160 "fs_config_files_system_dlkm": defaultDepCandidateProps(ctx.Config()),
161 // build props are automatically added to `ALL_DEFAULT_INSTALLED_MODULES`
162 "system_dlkm-build.prop": defaultDepCandidateProps(ctx.Config()),
163 },
Spandan Das5b493cd2024-11-07 20:55:56 +0000164 "vendor_dlkm": {
165 "fs_config_dirs_vendor_dlkm": defaultDepCandidateProps(ctx.Config()),
166 "fs_config_files_vendor_dlkm": defaultDepCandidateProps(ctx.Config()),
167 "vendor_dlkm-build.prop": defaultDepCandidateProps(ctx.Config()),
168 },
169 "odm_dlkm": {
170 "fs_config_dirs_odm_dlkm": defaultDepCandidateProps(ctx.Config()),
171 "fs_config_files_odm_dlkm": defaultDepCandidateProps(ctx.Config()),
172 "odm_dlkm-build.prop": defaultDepCandidateProps(ctx.Config()),
173 },
Cole Faust76a6e952024-11-07 16:56:45 -0800174 "ramdisk": {},
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000175 },
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000176 fsDepsMutex: sync.Mutex{},
177 moduleToInstallationProps: map[string]installationProperties{},
178 }
179 }).(*FsGenState)
180}
181
182func checkDepModuleInMultipleNamespaces(mctx android.BottomUpMutatorContext, foundDeps multilibDeps, module string, partitionName string) {
183 otherNamespace := mctx.Namespace().Path
184 if val, found := foundDeps[module]; found && otherNamespace != "." && !android.InList(val.Namespace, []string{".", otherNamespace}) {
185 mctx.ModuleErrorf("found in multiple namespaces(%s and %s) when including in %s partition", val.Namespace, otherNamespace, partitionName)
186 }
187}
188
189func appendDepIfAppropriate(mctx android.BottomUpMutatorContext, deps *multilibDeps, installPartition string) {
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000190 moduleName := mctx.ModuleName()
191 checkDepModuleInMultipleNamespaces(mctx, *deps, moduleName, installPartition)
192 if _, ok := (*deps)[moduleName]; ok {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000193 // Prefer the namespace-specific module over the platform module
194 if mctx.Namespace().Path != "." {
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000195 (*deps)[moduleName].Namespace = mctx.Namespace().Path
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000196 }
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000197 (*deps)[moduleName].Arch = append((*deps)[moduleName].Arch, mctx.Module().Target().Arch.ArchType)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000198 } else {
199 multilib, _ := mctx.Module().DecodeMultilib(mctx)
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000200 (*deps)[moduleName] = &depCandidateProps{
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000201 Namespace: mctx.Namespace().Path,
202 Multilib: multilib,
203 Arch: []android.ArchType{mctx.Module().Target().Arch.ArchType},
204 }
205 }
206}
207
208func collectDepsMutator(mctx android.BottomUpMutatorContext) {
Cole Faust76a6e952024-11-07 16:56:45 -0800209 m := mctx.Module()
210 if m.Target().Os.Class != android.Device {
211 return
212 }
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000213 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
214
Cole Faust76a6e952024-11-07 16:56:45 -0800215 fsGenState.fsDepsMutex.Lock()
216 defer fsGenState.fsDepsMutex.Unlock()
217
218 if slices.Contains(fsGenState.depCandidates, mctx.ModuleName()) {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000219 installPartition := m.PartitionTag(mctx.DeviceConfig())
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000220 // Only add the module as dependency when:
221 // - its enabled
222 // - its namespace is included in PRODUCT_SOONG_NAMESPACES
223 if m.Enabled(mctx) && m.ExportedToMake() {
224 appendDepIfAppropriate(mctx, fsGenState.fsDeps[installPartition], installPartition)
225 }
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000226 }
227 // store the map of module to (required,overrides) even if the module is not in PRODUCT_PACKAGES.
228 // the module might be installed transitively.
Cole Faust76a6e952024-11-07 16:56:45 -0800229 if m.Enabled(mctx) && m.ExportedToMake() {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000230 fsGenState.moduleToInstallationProps[m.Name()] = installationProperties{
231 Required: m.RequiredModuleNames(mctx),
232 Overrides: m.Overrides(),
233 }
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000234 }
235}
236
237type depsStruct struct {
238 Deps []string
239}
240
241type multilibDepsStruct struct {
242 Common depsStruct
243 Lib32 depsStruct
244 Lib64 depsStruct
245 Both depsStruct
246 Prefer32 depsStruct
247}
248
249type packagingPropsStruct struct {
250 High_priority_deps []string
251 Deps []string
252 Multilib multilibDepsStruct
253}
254
255func fullyQualifiedModuleName(moduleName, namespace string) string {
256 if namespace == "." {
257 return moduleName
258 }
259 return fmt.Sprintf("//%s:%s", namespace, moduleName)
260}
261
262func getBitness(archTypes []android.ArchType) (ret []string) {
263 for _, archType := range archTypes {
264 if archType.Multilib == "" {
265 ret = append(ret, android.COMMON_VARIANT)
266 } else {
267 ret = append(ret, archType.Bitness())
268 }
269 }
270 return ret
271}
272
273func setDepsMutator(mctx android.BottomUpMutatorContext) {
274 removeOverriddenDeps(mctx)
275 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
276 fsDeps := fsGenState.fsDeps
277 soongGeneratedPartitionMap := getAllSoongGeneratedPartitionNames(mctx.Config(), fsGenState.soongGeneratedPartitions)
278 m := mctx.Module()
279 if partition, ok := soongGeneratedPartitionMap[m.Name()]; ok {
280 depsStruct := generateDepStruct(*fsDeps[partition])
281 if err := proptools.AppendMatchingProperties(m.GetProperties(), depsStruct, nil); err != nil {
282 mctx.ModuleErrorf(err.Error())
283 }
284 }
285}
286
287// removeOverriddenDeps collects PRODUCT_PACKAGES and (transitive) required deps.
288// it then removes any modules which appear in `overrides` of the above list.
289func removeOverriddenDeps(mctx android.BottomUpMutatorContext) {
290 mctx.Config().Once(fsGenRemoveOverridesOnceKey, func() interface{} {
291 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
292 fsDeps := fsGenState.fsDeps
293 overridden := map[string]bool{}
294 allDeps := []string{}
295
296 // Step 1: Initialization: Append PRODUCT_PACKAGES to the queue
297 for _, fsDep := range fsDeps {
298 for depName, _ := range *fsDep {
299 allDeps = append(allDeps, depName)
300 }
301 }
302
303 // Step 2: Process the queue, and add required modules to the queue.
304 i := 0
305 for {
306 if i == len(allDeps) {
307 break
308 }
309 depName := allDeps[i]
310 for _, overrides := range fsGenState.moduleToInstallationProps[depName].Overrides {
311 overridden[overrides] = true
312 }
313 // add required dep to the queue.
314 allDeps = append(allDeps, fsGenState.moduleToInstallationProps[depName].Required...)
315 i += 1
316 }
317
318 // Step 3: Delete all the overridden modules.
319 for overridden, _ := range overridden {
320 for partition, _ := range fsDeps {
321 delete(*fsDeps[partition], overridden)
322 }
323 }
324 return nil
325 })
326}
327
328var HighPriorityDeps = []string{}
329
330func generateDepStruct(deps map[string]*depCandidateProps) *packagingPropsStruct {
331 depsStruct := packagingPropsStruct{}
332 for depName, depProps := range deps {
333 bitness := getBitness(depProps.Arch)
334 fullyQualifiedDepName := fullyQualifiedModuleName(depName, depProps.Namespace)
335 if android.InList(depName, HighPriorityDeps) {
336 depsStruct.High_priority_deps = append(depsStruct.High_priority_deps, fullyQualifiedDepName)
337 } else if android.InList("32", bitness) && android.InList("64", bitness) {
338 // If both 32 and 64 bit variants are enabled for this module
339 switch depProps.Multilib {
340 case string(android.MultilibBoth):
341 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
342 case string(android.MultilibCommon), string(android.MultilibFirst):
343 depsStruct.Deps = append(depsStruct.Deps, fullyQualifiedDepName)
344 case "32":
345 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
346 case "64", "darwin_universal":
347 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
348 case "prefer32", "first_prefer32":
349 depsStruct.Multilib.Prefer32.Deps = append(depsStruct.Multilib.Prefer32.Deps, fullyQualifiedDepName)
350 default:
351 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
352 }
353 } else if android.InList("64", bitness) {
354 // If only 64 bit variant is enabled
355 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
356 } else if android.InList("32", bitness) {
357 // If only 32 bit variant is enabled
358 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
359 } else {
360 // If only common variant is enabled
361 depsStruct.Multilib.Common.Deps = append(depsStruct.Multilib.Common.Deps, fullyQualifiedDepName)
362 }
363 }
364 depsStruct.Deps = android.SortedUniqueStrings(depsStruct.Deps)
365 depsStruct.Multilib.Lib32.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Lib32.Deps)
366 depsStruct.Multilib.Lib64.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Lib64.Deps)
367 depsStruct.Multilib.Prefer32.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Prefer32.Deps)
368 depsStruct.Multilib.Both.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Both.Deps)
369 depsStruct.Multilib.Common.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Common.Deps)
370
371 return &depsStruct
372}