blob: 7b3026404e55c7908f05c99d0d700b507a3f6494 [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
63 // List of name of partitions to be generated by the filesystem_creator module
64 soongGeneratedPartitions []string
65 // 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
87func generatedPartitions(ctx android.LoadHookContext) []string {
Cole Faust76a6e952024-11-07 16:56:45 -080088 generatedPartitions := []string{"system", "ramdisk"}
Jihoon Kangadd2bb22024-11-05 22:29:34 +000089 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
90 generatedPartitions = append(generatedPartitions, "system_ext")
91 }
92 if ctx.DeviceConfig().BuildingVendorImage() && ctx.DeviceConfig().VendorPath() == "vendor" {
93 generatedPartitions = append(generatedPartitions, "vendor")
94 }
95 if ctx.DeviceConfig().BuildingProductImage() && ctx.DeviceConfig().ProductPath() == "product" {
96 generatedPartitions = append(generatedPartitions, "product")
97 }
98 if ctx.DeviceConfig().BuildingOdmImage() && ctx.DeviceConfig().OdmPath() == "odm" {
99 generatedPartitions = append(generatedPartitions, "odm")
100 }
mrziwang23ba8762024-11-07 16:21:53 -0800101 if ctx.DeviceConfig().BuildingUserdataImage() && ctx.DeviceConfig().UserdataPath() == "data" {
102 generatedPartitions = append(generatedPartitions, "userdata")
103 }
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000104 if ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.BuildingSystemDlkmImage {
105 generatedPartitions = append(generatedPartitions, "system_dlkm")
106 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000107 if ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.BuildingVendorDlkmImage {
108 generatedPartitions = append(generatedPartitions, "vendor_dlkm")
109 }
110 if ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.BuildingOdmDlkmImage {
111 generatedPartitions = append(generatedPartitions, "odm_dlkm")
112 }
113
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000114 return generatedPartitions
115}
116
Jihoon Kang04f12c92024-11-12 23:03:08 +0000117func createFsGenState(ctx android.LoadHookContext, generatedPrebuiltEtcModuleNames []string, avbpubkeyGenerated bool) *FsGenState {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000118 return ctx.Config().Once(fsGenStateOnceKey, func() interface{} {
119 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
120 candidates := android.FirstUniqueStrings(android.Concat(partitionVars.ProductPackages, partitionVars.ProductPackagesDebug))
121 candidates = android.Concat(candidates, generatedPrebuiltEtcModuleNames)
122
Jihoon Kang04f12c92024-11-12 23:03:08 +0000123 fsGenState := FsGenState{
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000124 depCandidates: candidates,
125 fsDeps: map[string]*multilibDeps{
126 // These additional deps are added according to the cuttlefish system image bp.
127 "system": {
128 "com.android.apex.cts.shim.v1_prebuilt": defaultDepCandidateProps(ctx.Config()),
129 "dex_bootjars": defaultDepCandidateProps(ctx.Config()),
130 "framework_compatibility_matrix.device.xml": defaultDepCandidateProps(ctx.Config()),
131 "init.environ.rc-soong": defaultDepCandidateProps(ctx.Config()),
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000132 "libcompiler_rt": defaultDepCandidateProps(ctx.Config()),
133 "libdmabufheap": defaultDepCandidateProps(ctx.Config()),
134 "libgsi": defaultDepCandidateProps(ctx.Config()),
135 "llndk.libraries.txt": defaultDepCandidateProps(ctx.Config()),
136 "logpersist.start": defaultDepCandidateProps(ctx.Config()),
137 "update_engine_sideload": defaultDepCandidateProps(ctx.Config()),
138 },
139 "vendor": {
140 "fs_config_files_vendor": defaultDepCandidateProps(ctx.Config()),
141 "fs_config_dirs_vendor": defaultDepCandidateProps(ctx.Config()),
142 generatedModuleName(ctx.Config(), "vendor-build.prop"): defaultDepCandidateProps(ctx.Config()),
143 },
144 "odm": {
145 // fs_config_* files are automatically installed for all products with odm partitions.
146 // https://cs.android.com/android/_/android/platform/build/+/e4849e87ab660b59a6501b3928693db065ee873b:tools/fs_config/Android.mk;l=34;drc=8d6481b92c4b4e9b9f31a61545b6862090fcc14b;bpv=1;bpt=0
147 "fs_config_files_odm": defaultDepCandidateProps(ctx.Config()),
148 "fs_config_dirs_odm": defaultDepCandidateProps(ctx.Config()),
149 },
150 "product": {},
151 "system_ext": {
152 // VNDK apexes are automatically included.
153 // This hardcoded list will need to be updated if `PRODUCT_EXTRA_VNDK_VERSIONS` is updated.
154 // https://cs.android.com/android/_/android/platform/build/+/adba533072b00c53ac0f198c550a3cbd7a00e4cd:core/main.mk;l=984;bpv=1;bpt=0;drc=174db7b179592cf07cbfd2adb0119486fda911e7
155 "com.android.vndk.v30": defaultDepCandidateProps(ctx.Config()),
156 "com.android.vndk.v31": defaultDepCandidateProps(ctx.Config()),
157 "com.android.vndk.v32": defaultDepCandidateProps(ctx.Config()),
158 "com.android.vndk.v33": defaultDepCandidateProps(ctx.Config()),
159 "com.android.vndk.v34": defaultDepCandidateProps(ctx.Config()),
160 },
Spandan Das912d26b2024-11-06 19:35:17 +0000161 "userdata": {},
162 "system_dlkm": {
163 // these are phony required deps of the phony fs_config_dirs_nonsystem
164 "fs_config_dirs_system_dlkm": defaultDepCandidateProps(ctx.Config()),
165 "fs_config_files_system_dlkm": defaultDepCandidateProps(ctx.Config()),
166 // build props are automatically added to `ALL_DEFAULT_INSTALLED_MODULES`
167 "system_dlkm-build.prop": defaultDepCandidateProps(ctx.Config()),
168 },
Spandan Das5b493cd2024-11-07 20:55:56 +0000169 "vendor_dlkm": {
170 "fs_config_dirs_vendor_dlkm": defaultDepCandidateProps(ctx.Config()),
171 "fs_config_files_vendor_dlkm": defaultDepCandidateProps(ctx.Config()),
172 "vendor_dlkm-build.prop": defaultDepCandidateProps(ctx.Config()),
173 },
174 "odm_dlkm": {
175 "fs_config_dirs_odm_dlkm": defaultDepCandidateProps(ctx.Config()),
176 "fs_config_files_odm_dlkm": defaultDepCandidateProps(ctx.Config()),
177 "odm_dlkm-build.prop": defaultDepCandidateProps(ctx.Config()),
178 },
Cole Faust76a6e952024-11-07 16:56:45 -0800179 "ramdisk": {},
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000180 },
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000181 fsDepsMutex: sync.Mutex{},
182 moduleToInstallationProps: map[string]installationProperties{},
183 generatedPrebuiltEtcModuleNames: generatedPrebuiltEtcModuleNames,
Cole Faust953476f2024-11-14 14:11:29 -0800184 avbKeyFilegroups: map[string]string{},
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000185 }
Jihoon Kang04f12c92024-11-12 23:03:08 +0000186
187 if avbpubkeyGenerated {
188 (*fsGenState.fsDeps["product"])["system_other_avbpubkey"] = defaultDepCandidateProps(ctx.Config())
189 }
190
191 return &fsGenState
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000192 }).(*FsGenState)
193}
194
195func checkDepModuleInMultipleNamespaces(mctx android.BottomUpMutatorContext, foundDeps multilibDeps, module string, partitionName string) {
196 otherNamespace := mctx.Namespace().Path
197 if val, found := foundDeps[module]; found && otherNamespace != "." && !android.InList(val.Namespace, []string{".", otherNamespace}) {
198 mctx.ModuleErrorf("found in multiple namespaces(%s and %s) when including in %s partition", val.Namespace, otherNamespace, partitionName)
199 }
200}
201
202func appendDepIfAppropriate(mctx android.BottomUpMutatorContext, deps *multilibDeps, installPartition string) {
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000203 moduleName := mctx.ModuleName()
204 checkDepModuleInMultipleNamespaces(mctx, *deps, moduleName, installPartition)
205 if _, ok := (*deps)[moduleName]; ok {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000206 // Prefer the namespace-specific module over the platform module
207 if mctx.Namespace().Path != "." {
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000208 (*deps)[moduleName].Namespace = mctx.Namespace().Path
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000209 }
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000210 (*deps)[moduleName].Arch = append((*deps)[moduleName].Arch, mctx.Module().Target().Arch.ArchType)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000211 } else {
212 multilib, _ := mctx.Module().DecodeMultilib(mctx)
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000213 (*deps)[moduleName] = &depCandidateProps{
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000214 Namespace: mctx.Namespace().Path,
215 Multilib: multilib,
216 Arch: []android.ArchType{mctx.Module().Target().Arch.ArchType},
217 }
218 }
219}
220
221func collectDepsMutator(mctx android.BottomUpMutatorContext) {
Cole Faust76a6e952024-11-07 16:56:45 -0800222 m := mctx.Module()
223 if m.Target().Os.Class != android.Device {
224 return
225 }
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000226 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
227
Cole Faust76a6e952024-11-07 16:56:45 -0800228 fsGenState.fsDepsMutex.Lock()
229 defer fsGenState.fsDepsMutex.Unlock()
230
231 if slices.Contains(fsGenState.depCandidates, mctx.ModuleName()) {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000232 installPartition := m.PartitionTag(mctx.DeviceConfig())
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000233 // Only add the module as dependency when:
234 // - its enabled
235 // - its namespace is included in PRODUCT_SOONG_NAMESPACES
236 if m.Enabled(mctx) && m.ExportedToMake() {
237 appendDepIfAppropriate(mctx, fsGenState.fsDeps[installPartition], installPartition)
238 }
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000239 }
240 // store the map of module to (required,overrides) even if the module is not in PRODUCT_PACKAGES.
241 // the module might be installed transitively.
Cole Faust76a6e952024-11-07 16:56:45 -0800242 if m.Enabled(mctx) && m.ExportedToMake() {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000243 fsGenState.moduleToInstallationProps[m.Name()] = installationProperties{
244 Required: m.RequiredModuleNames(mctx),
245 Overrides: m.Overrides(),
246 }
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000247 }
248}
249
250type depsStruct struct {
251 Deps []string
252}
253
254type multilibDepsStruct struct {
255 Common depsStruct
256 Lib32 depsStruct
257 Lib64 depsStruct
258 Both depsStruct
259 Prefer32 depsStruct
260}
261
262type packagingPropsStruct struct {
263 High_priority_deps []string
264 Deps []string
265 Multilib multilibDepsStruct
266}
267
268func fullyQualifiedModuleName(moduleName, namespace string) string {
269 if namespace == "." {
270 return moduleName
271 }
272 return fmt.Sprintf("//%s:%s", namespace, moduleName)
273}
274
275func getBitness(archTypes []android.ArchType) (ret []string) {
276 for _, archType := range archTypes {
277 if archType.Multilib == "" {
278 ret = append(ret, android.COMMON_VARIANT)
279 } else {
280 ret = append(ret, archType.Bitness())
281 }
282 }
283 return ret
284}
285
286func setDepsMutator(mctx android.BottomUpMutatorContext) {
287 removeOverriddenDeps(mctx)
288 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
289 fsDeps := fsGenState.fsDeps
290 soongGeneratedPartitionMap := getAllSoongGeneratedPartitionNames(mctx.Config(), fsGenState.soongGeneratedPartitions)
291 m := mctx.Module()
292 if partition, ok := soongGeneratedPartitionMap[m.Name()]; ok {
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000293 depsStruct := generateDepStruct(*fsDeps[partition], fsGenState.generatedPrebuiltEtcModuleNames)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000294 if err := proptools.AppendMatchingProperties(m.GetProperties(), depsStruct, nil); err != nil {
295 mctx.ModuleErrorf(err.Error())
296 }
297 }
298}
299
300// removeOverriddenDeps collects PRODUCT_PACKAGES and (transitive) required deps.
301// it then removes any modules which appear in `overrides` of the above list.
302func removeOverriddenDeps(mctx android.BottomUpMutatorContext) {
303 mctx.Config().Once(fsGenRemoveOverridesOnceKey, func() interface{} {
304 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
305 fsDeps := fsGenState.fsDeps
306 overridden := map[string]bool{}
307 allDeps := []string{}
308
309 // Step 1: Initialization: Append PRODUCT_PACKAGES to the queue
310 for _, fsDep := range fsDeps {
311 for depName, _ := range *fsDep {
312 allDeps = append(allDeps, depName)
313 }
314 }
315
316 // Step 2: Process the queue, and add required modules to the queue.
317 i := 0
318 for {
319 if i == len(allDeps) {
320 break
321 }
322 depName := allDeps[i]
323 for _, overrides := range fsGenState.moduleToInstallationProps[depName].Overrides {
324 overridden[overrides] = true
325 }
326 // add required dep to the queue.
327 allDeps = append(allDeps, fsGenState.moduleToInstallationProps[depName].Required...)
328 i += 1
329 }
330
331 // Step 3: Delete all the overridden modules.
332 for overridden, _ := range overridden {
333 for partition, _ := range fsDeps {
334 delete(*fsDeps[partition], overridden)
335 }
336 }
337 return nil
338 })
339}
340
341var HighPriorityDeps = []string{}
342
Jihoon Kang3a8759c2024-11-08 19:35:09 +0000343func isHighPriorityDep(depName string) bool {
344 for _, highPriorityDeps := range HighPriorityDeps {
345 if strings.HasPrefix(depName, highPriorityDeps) {
346 return true
347 }
348 }
349 return false
350}
351
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000352func generateDepStruct(deps map[string]*depCandidateProps, highPriorityDeps []string) *packagingPropsStruct {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000353 depsStruct := packagingPropsStruct{}
354 for depName, depProps := range deps {
355 bitness := getBitness(depProps.Arch)
356 fullyQualifiedDepName := fullyQualifiedModuleName(depName, depProps.Namespace)
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000357 if android.InList(depName, highPriorityDeps) {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000358 depsStruct.High_priority_deps = append(depsStruct.High_priority_deps, fullyQualifiedDepName)
359 } else if android.InList("32", bitness) && android.InList("64", bitness) {
360 // If both 32 and 64 bit variants are enabled for this module
361 switch depProps.Multilib {
362 case string(android.MultilibBoth):
363 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
364 case string(android.MultilibCommon), string(android.MultilibFirst):
365 depsStruct.Deps = append(depsStruct.Deps, fullyQualifiedDepName)
366 case "32":
367 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
368 case "64", "darwin_universal":
369 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
370 case "prefer32", "first_prefer32":
371 depsStruct.Multilib.Prefer32.Deps = append(depsStruct.Multilib.Prefer32.Deps, fullyQualifiedDepName)
372 default:
373 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
374 }
375 } else if android.InList("64", bitness) {
376 // If only 64 bit variant is enabled
377 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
378 } else if android.InList("32", bitness) {
379 // If only 32 bit variant is enabled
380 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
381 } else {
382 // If only common variant is enabled
383 depsStruct.Multilib.Common.Deps = append(depsStruct.Multilib.Common.Deps, fullyQualifiedDepName)
384 }
385 }
386 depsStruct.Deps = android.SortedUniqueStrings(depsStruct.Deps)
387 depsStruct.Multilib.Lib32.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Lib32.Deps)
388 depsStruct.Multilib.Lib64.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Lib64.Deps)
389 depsStruct.Multilib.Prefer32.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Prefer32.Deps)
390 depsStruct.Multilib.Both.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Both.Deps)
391 depsStruct.Multilib.Common.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Common.Deps)
Cole Faust866c6032024-11-13 16:17:48 -0800392 depsStruct.High_priority_deps = android.SortedUniqueStrings(depsStruct.High_priority_deps)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000393
394 return &depsStruct
395}