blob: e530a5b0ac5de9f4cf767cb2983d6ff1997e4225 [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 {
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 }
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 }
102 return generatedPartitions
103}
104
105func createFsGenState(ctx android.LoadHookContext, generatedPrebuiltEtcModuleNames []string) *FsGenState {
106 return ctx.Config().Once(fsGenStateOnceKey, func() interface{} {
107 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
108 candidates := android.FirstUniqueStrings(android.Concat(partitionVars.ProductPackages, partitionVars.ProductPackagesDebug))
109 candidates = android.Concat(candidates, generatedPrebuiltEtcModuleNames)
110
111 return &FsGenState{
112 depCandidates: candidates,
113 fsDeps: map[string]*multilibDeps{
114 // These additional deps are added according to the cuttlefish system image bp.
115 "system": {
116 "com.android.apex.cts.shim.v1_prebuilt": defaultDepCandidateProps(ctx.Config()),
117 "dex_bootjars": defaultDepCandidateProps(ctx.Config()),
118 "framework_compatibility_matrix.device.xml": defaultDepCandidateProps(ctx.Config()),
119 "init.environ.rc-soong": defaultDepCandidateProps(ctx.Config()),
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000120 "libcompiler_rt": defaultDepCandidateProps(ctx.Config()),
121 "libdmabufheap": defaultDepCandidateProps(ctx.Config()),
122 "libgsi": defaultDepCandidateProps(ctx.Config()),
123 "llndk.libraries.txt": defaultDepCandidateProps(ctx.Config()),
124 "logpersist.start": defaultDepCandidateProps(ctx.Config()),
125 "update_engine_sideload": defaultDepCandidateProps(ctx.Config()),
126 },
127 "vendor": {
128 "fs_config_files_vendor": defaultDepCandidateProps(ctx.Config()),
129 "fs_config_dirs_vendor": defaultDepCandidateProps(ctx.Config()),
130 generatedModuleName(ctx.Config(), "vendor-build.prop"): defaultDepCandidateProps(ctx.Config()),
131 },
132 "odm": {
133 // fs_config_* files are automatically installed for all products with odm partitions.
134 // https://cs.android.com/android/_/android/platform/build/+/e4849e87ab660b59a6501b3928693db065ee873b:tools/fs_config/Android.mk;l=34;drc=8d6481b92c4b4e9b9f31a61545b6862090fcc14b;bpv=1;bpt=0
135 "fs_config_files_odm": defaultDepCandidateProps(ctx.Config()),
136 "fs_config_dirs_odm": defaultDepCandidateProps(ctx.Config()),
137 },
138 "product": {},
139 "system_ext": {
140 // VNDK apexes are automatically included.
141 // This hardcoded list will need to be updated if `PRODUCT_EXTRA_VNDK_VERSIONS` is updated.
142 // https://cs.android.com/android/_/android/platform/build/+/adba533072b00c53ac0f198c550a3cbd7a00e4cd:core/main.mk;l=984;bpv=1;bpt=0;drc=174db7b179592cf07cbfd2adb0119486fda911e7
143 "com.android.vndk.v30": defaultDepCandidateProps(ctx.Config()),
144 "com.android.vndk.v31": defaultDepCandidateProps(ctx.Config()),
145 "com.android.vndk.v32": defaultDepCandidateProps(ctx.Config()),
146 "com.android.vndk.v33": defaultDepCandidateProps(ctx.Config()),
147 "com.android.vndk.v34": defaultDepCandidateProps(ctx.Config()),
148 },
149 "system_dlkm": {},
mrziwang23ba8762024-11-07 16:21:53 -0800150 "userdata": {},
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000151 },
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000152 fsDepsMutex: sync.Mutex{},
153 moduleToInstallationProps: map[string]installationProperties{},
154 }
155 }).(*FsGenState)
156}
157
158func checkDepModuleInMultipleNamespaces(mctx android.BottomUpMutatorContext, foundDeps multilibDeps, module string, partitionName string) {
159 otherNamespace := mctx.Namespace().Path
160 if val, found := foundDeps[module]; found && otherNamespace != "." && !android.InList(val.Namespace, []string{".", otherNamespace}) {
161 mctx.ModuleErrorf("found in multiple namespaces(%s and %s) when including in %s partition", val.Namespace, otherNamespace, partitionName)
162 }
163}
164
165func appendDepIfAppropriate(mctx android.BottomUpMutatorContext, deps *multilibDeps, installPartition string) {
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000166 moduleName := mctx.ModuleName()
167 checkDepModuleInMultipleNamespaces(mctx, *deps, moduleName, installPartition)
168 if _, ok := (*deps)[moduleName]; ok {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000169 // Prefer the namespace-specific module over the platform module
170 if mctx.Namespace().Path != "." {
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000171 (*deps)[moduleName].Namespace = mctx.Namespace().Path
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000172 }
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000173 (*deps)[moduleName].Arch = append((*deps)[moduleName].Arch, mctx.Module().Target().Arch.ArchType)
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000174 } else {
175 multilib, _ := mctx.Module().DecodeMultilib(mctx)
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000176 (*deps)[moduleName] = &depCandidateProps{
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000177 Namespace: mctx.Namespace().Path,
178 Multilib: multilib,
179 Arch: []android.ArchType{mctx.Module().Target().Arch.ArchType},
180 }
181 }
182}
183
184func collectDepsMutator(mctx android.BottomUpMutatorContext) {
185 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
186
187 m := mctx.Module()
Jihoon Kang81aeb9e2024-11-05 00:22:35 +0000188 if m.Target().Os.Class == android.Device && slices.Contains(fsGenState.depCandidates, mctx.ModuleName()) {
Jihoon Kangadd2bb22024-11-05 22:29:34 +0000189 installPartition := m.PartitionTag(mctx.DeviceConfig())
190 fsGenState.fsDepsMutex.Lock()
191 // Only add the module as dependency when:
192 // - its enabled
193 // - its namespace is included in PRODUCT_SOONG_NAMESPACES
194 if m.Enabled(mctx) && m.ExportedToMake() {
195 appendDepIfAppropriate(mctx, fsGenState.fsDeps[installPartition], installPartition)
196 }
197 fsGenState.fsDepsMutex.Unlock()
198 }
199 // store the map of module to (required,overrides) even if the module is not in PRODUCT_PACKAGES.
200 // the module might be installed transitively.
201 if m.Target().Os.Class == android.Device && m.Enabled(mctx) && m.ExportedToMake() {
202 fsGenState.fsDepsMutex.Lock()
203 fsGenState.moduleToInstallationProps[m.Name()] = installationProperties{
204 Required: m.RequiredModuleNames(mctx),
205 Overrides: m.Overrides(),
206 }
207 fsGenState.fsDepsMutex.Unlock()
208 }
209}
210
211type depsStruct struct {
212 Deps []string
213}
214
215type multilibDepsStruct struct {
216 Common depsStruct
217 Lib32 depsStruct
218 Lib64 depsStruct
219 Both depsStruct
220 Prefer32 depsStruct
221}
222
223type packagingPropsStruct struct {
224 High_priority_deps []string
225 Deps []string
226 Multilib multilibDepsStruct
227}
228
229func fullyQualifiedModuleName(moduleName, namespace string) string {
230 if namespace == "." {
231 return moduleName
232 }
233 return fmt.Sprintf("//%s:%s", namespace, moduleName)
234}
235
236func getBitness(archTypes []android.ArchType) (ret []string) {
237 for _, archType := range archTypes {
238 if archType.Multilib == "" {
239 ret = append(ret, android.COMMON_VARIANT)
240 } else {
241 ret = append(ret, archType.Bitness())
242 }
243 }
244 return ret
245}
246
247func setDepsMutator(mctx android.BottomUpMutatorContext) {
248 removeOverriddenDeps(mctx)
249 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
250 fsDeps := fsGenState.fsDeps
251 soongGeneratedPartitionMap := getAllSoongGeneratedPartitionNames(mctx.Config(), fsGenState.soongGeneratedPartitions)
252 m := mctx.Module()
253 if partition, ok := soongGeneratedPartitionMap[m.Name()]; ok {
254 depsStruct := generateDepStruct(*fsDeps[partition])
255 if err := proptools.AppendMatchingProperties(m.GetProperties(), depsStruct, nil); err != nil {
256 mctx.ModuleErrorf(err.Error())
257 }
258 }
259}
260
261// removeOverriddenDeps collects PRODUCT_PACKAGES and (transitive) required deps.
262// it then removes any modules which appear in `overrides` of the above list.
263func removeOverriddenDeps(mctx android.BottomUpMutatorContext) {
264 mctx.Config().Once(fsGenRemoveOverridesOnceKey, func() interface{} {
265 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
266 fsDeps := fsGenState.fsDeps
267 overridden := map[string]bool{}
268 allDeps := []string{}
269
270 // Step 1: Initialization: Append PRODUCT_PACKAGES to the queue
271 for _, fsDep := range fsDeps {
272 for depName, _ := range *fsDep {
273 allDeps = append(allDeps, depName)
274 }
275 }
276
277 // Step 2: Process the queue, and add required modules to the queue.
278 i := 0
279 for {
280 if i == len(allDeps) {
281 break
282 }
283 depName := allDeps[i]
284 for _, overrides := range fsGenState.moduleToInstallationProps[depName].Overrides {
285 overridden[overrides] = true
286 }
287 // add required dep to the queue.
288 allDeps = append(allDeps, fsGenState.moduleToInstallationProps[depName].Required...)
289 i += 1
290 }
291
292 // Step 3: Delete all the overridden modules.
293 for overridden, _ := range overridden {
294 for partition, _ := range fsDeps {
295 delete(*fsDeps[partition], overridden)
296 }
297 }
298 return nil
299 })
300}
301
302var HighPriorityDeps = []string{}
303
304func generateDepStruct(deps map[string]*depCandidateProps) *packagingPropsStruct {
305 depsStruct := packagingPropsStruct{}
306 for depName, depProps := range deps {
307 bitness := getBitness(depProps.Arch)
308 fullyQualifiedDepName := fullyQualifiedModuleName(depName, depProps.Namespace)
309 if android.InList(depName, HighPriorityDeps) {
310 depsStruct.High_priority_deps = append(depsStruct.High_priority_deps, fullyQualifiedDepName)
311 } else if android.InList("32", bitness) && android.InList("64", bitness) {
312 // If both 32 and 64 bit variants are enabled for this module
313 switch depProps.Multilib {
314 case string(android.MultilibBoth):
315 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
316 case string(android.MultilibCommon), string(android.MultilibFirst):
317 depsStruct.Deps = append(depsStruct.Deps, fullyQualifiedDepName)
318 case "32":
319 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
320 case "64", "darwin_universal":
321 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
322 case "prefer32", "first_prefer32":
323 depsStruct.Multilib.Prefer32.Deps = append(depsStruct.Multilib.Prefer32.Deps, fullyQualifiedDepName)
324 default:
325 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
326 }
327 } else if android.InList("64", bitness) {
328 // If only 64 bit variant is enabled
329 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
330 } else if android.InList("32", bitness) {
331 // If only 32 bit variant is enabled
332 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
333 } else {
334 // If only common variant is enabled
335 depsStruct.Multilib.Common.Deps = append(depsStruct.Multilib.Common.Deps, fullyQualifiedDepName)
336 }
337 }
338 depsStruct.Deps = android.SortedUniqueStrings(depsStruct.Deps)
339 depsStruct.Multilib.Lib32.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Lib32.Deps)
340 depsStruct.Multilib.Lib64.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Lib64.Deps)
341 depsStruct.Multilib.Prefer32.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Prefer32.Deps)
342 depsStruct.Multilib.Both.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Both.Deps)
343 depsStruct.Multilib.Common.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Common.Deps)
344
345 return &depsStruct
346}