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