blob: 242ab512a4a4bac0b96b02c5cd987affcd1269ac [file] [log] [blame]
Jihoon Kang98047cf2024-10-02 17:13:54 +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 (
Cole Faust92ccbe22024-10-03 14:38:37 -070018 "crypto/sha256"
Jihoon Kang98047cf2024-10-02 17:13:54 +000019 "fmt"
Spandan Das312cc412024-10-29 18:20:11 +000020 "path/filepath"
mrziwang8f86c882024-10-03 12:34:33 -070021 "slices"
Jihoon Kang98047cf2024-10-02 17:13:54 +000022 "strconv"
mrziwang8f86c882024-10-03 12:34:33 -070023 "strings"
24 "sync"
25
26 "android/soong/android"
Jihoon Kang675d4682024-10-24 23:45:11 +000027 "android/soong/etc"
mrziwang8f86c882024-10-03 12:34:33 -070028 "android/soong/filesystem"
Spandan Das5e336422024-11-01 22:31:20 +000029 "android/soong/kernel"
Jihoon Kang98047cf2024-10-02 17:13:54 +000030
Cole Faust92ccbe22024-10-03 14:38:37 -070031 "github.com/google/blueprint"
mrziwang8f86c882024-10-03 12:34:33 -070032 "github.com/google/blueprint/parser"
Jihoon Kang98047cf2024-10-02 17:13:54 +000033 "github.com/google/blueprint/proptools"
34)
35
Cole Faust92ccbe22024-10-03 14:38:37 -070036var pctx = android.NewPackageContext("android/soong/fsgen")
37
Jihoon Kang98047cf2024-10-02 17:13:54 +000038func init() {
39 registerBuildComponents(android.InitRegistrationContext)
40}
41
42func registerBuildComponents(ctx android.RegistrationContext) {
43 ctx.RegisterModuleType("soong_filesystem_creator", filesystemCreatorFactory)
mrziwang8f86c882024-10-03 12:34:33 -070044 ctx.PreDepsMutators(RegisterCollectFileSystemDepsMutators)
45}
46
47func RegisterCollectFileSystemDepsMutators(ctx android.RegisterMutatorsContext) {
48 ctx.BottomUp("fs_collect_deps", collectDepsMutator).MutatesGlobalState()
Jihoon Kang0d545b82024-10-11 00:21:57 +000049 ctx.BottomUp("fs_set_deps", setDepsMutator)
mrziwang8f86c882024-10-03 12:34:33 -070050}
51
Jihoon Kang0d545b82024-10-11 00:21:57 +000052var fsGenStateOnceKey = android.NewOnceKey("FsGenState")
Spandan Dase1860e42024-10-24 22:29:50 +000053var fsGenRemoveOverridesOnceKey = android.NewOnceKey("FsGenRemoveOverrides")
Jihoon Kang0d545b82024-10-11 00:21:57 +000054
55// Map of partition module name to its partition that may be generated by Soong.
56// Note that it is not guaranteed that all modules returned by this function are successfully
57// created.
58func getAllSoongGeneratedPartitionNames(config android.Config, partitions []string) map[string]string {
59 ret := map[string]string{}
60 for _, partition := range partitions {
61 ret[generatedModuleNameForPartition(config, partition)] = partition
62 }
63 return ret
64}
65
66type depCandidateProps struct {
67 Namespace string
68 Multilib string
69 Arch []android.ArchType
70}
71
72// Map of module name to depCandidateProps
Jihoon Kang83f135b2024-11-01 22:56:07 +000073type multilibDeps map[string]*depCandidateProps
Jihoon Kang0d545b82024-10-11 00:21:57 +000074
75// Information necessary to generate the filesystem modules, including details about their
76// dependencies
77type FsGenState struct {
78 // List of modules in `PRODUCT_PACKAGES` and `PRODUCT_PACKAGES_DEBUG`
79 depCandidates []string
80 // Map of names of partition to the information of modules to be added as deps
Jihoon Kang83f135b2024-11-01 22:56:07 +000081 fsDeps map[string]*multilibDeps
Jihoon Kang0d545b82024-10-11 00:21:57 +000082 // List of name of partitions to be generated by the filesystem_creator module
83 soongGeneratedPartitions []string
84 // Mutex to protect the fsDeps
85 fsDepsMutex sync.Mutex
Spandan Dase1860e42024-10-24 22:29:50 +000086 // Map of _all_ soong module names to their corresponding installation properties
87 moduleToInstallationProps map[string]installationProperties
88}
89
90type installationProperties struct {
91 Required []string
92 Overrides []string
Jihoon Kang0d545b82024-10-11 00:21:57 +000093}
94
Jihoon Kang0d545b82024-10-11 00:21:57 +000095func defaultDepCandidateProps(config android.Config) *depCandidateProps {
96 return &depCandidateProps{
97 Namespace: ".",
98 Arch: []android.ArchType{config.BuildArch},
99 }
100}
101
Jihoon Kang675d4682024-10-24 23:45:11 +0000102type srcBaseFileInstallBaseFileTuple struct {
103 srcBaseFile string
104 installBaseFile string
105}
106
107// prebuilt src files grouped by the install partitions.
108// Each groups are a mapping of the relative install path to the name of the files
109type prebuiltSrcGroupByInstallPartition struct {
110 system map[string][]srcBaseFileInstallBaseFileTuple
111 system_ext map[string][]srcBaseFileInstallBaseFileTuple
112 product map[string][]srcBaseFileInstallBaseFileTuple
113 vendor map[string][]srcBaseFileInstallBaseFileTuple
114}
115
116func newPrebuiltSrcGroupByInstallPartition() *prebuiltSrcGroupByInstallPartition {
117 return &prebuiltSrcGroupByInstallPartition{
118 system: map[string][]srcBaseFileInstallBaseFileTuple{},
119 system_ext: map[string][]srcBaseFileInstallBaseFileTuple{},
120 product: map[string][]srcBaseFileInstallBaseFileTuple{},
121 vendor: map[string][]srcBaseFileInstallBaseFileTuple{},
122 }
123}
124
125func isSubdirectory(parent, child string) bool {
126 rel, err := filepath.Rel(parent, child)
127 if err != nil {
128 return false
129 }
130 return !strings.HasPrefix(rel, "..")
131}
132
133func appendIfCorrectInstallPartition(partitionToInstallPathList []partitionToInstallPath, destPath, srcPath string, srcGroup *prebuiltSrcGroupByInstallPartition) {
134 for _, part := range partitionToInstallPathList {
135 partition := part.name
136 installPath := part.installPath
137
138 if isSubdirectory(installPath, destPath) {
139 relativeInstallPath, _ := filepath.Rel(installPath, destPath)
140 relativeInstallDir := filepath.Dir(relativeInstallPath)
141 var srcMap map[string][]srcBaseFileInstallBaseFileTuple
142 switch partition {
143 case "system":
144 srcMap = srcGroup.system
145 case "system_ext":
146 srcMap = srcGroup.system_ext
147 case "product":
148 srcMap = srcGroup.product
149 case "vendor":
150 srcMap = srcGroup.vendor
151 }
152 if srcMap != nil {
153 srcMap[relativeInstallDir] = append(srcMap[relativeInstallDir], srcBaseFileInstallBaseFileTuple{
154 srcBaseFile: filepath.Base(srcPath),
155 installBaseFile: filepath.Base(destPath),
156 })
157 }
158 return
159 }
160 }
161}
162
163func uniqueExistingProductCopyFileMap(ctx android.LoadHookContext) map[string]string {
164 seen := make(map[string]bool)
165 filtered := make(map[string]string)
166
167 for src, dest := range ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.ProductCopyFiles {
168 if _, ok := seen[dest]; !ok {
169 if optionalPath := android.ExistentPathForSource(ctx, src); optionalPath.Valid() {
170 seen[dest] = true
171 filtered[src] = dest
172 }
173 }
174 }
175
176 return filtered
177}
178
179type partitionToInstallPath struct {
180 name string
181 installPath string
182}
183
184func processProductCopyFiles(ctx android.LoadHookContext) map[string]*prebuiltSrcGroupByInstallPartition {
185 // Filter out duplicate dest entries and non existing src entries
186 productCopyFileMap := uniqueExistingProductCopyFileMap(ctx)
187
188 // System is intentionally added at the last to consider the scenarios where
189 // non-system partitions are installed as part of the system partition
190 partitionToInstallPathList := []partitionToInstallPath{
191 {name: "vendor", installPath: ctx.DeviceConfig().VendorPath()},
192 {name: "product", installPath: ctx.DeviceConfig().ProductPath()},
193 {name: "system_ext", installPath: ctx.DeviceConfig().SystemExtPath()},
194 {name: "system", installPath: "system"},
195 }
196
197 groupedSources := map[string]*prebuiltSrcGroupByInstallPartition{}
198 for _, src := range android.SortedKeys(productCopyFileMap) {
199 dest := productCopyFileMap[src]
200 srcFileDir := filepath.Dir(src)
201 if _, ok := groupedSources[srcFileDir]; !ok {
202 groupedSources[srcFileDir] = newPrebuiltSrcGroupByInstallPartition()
203 }
204 appendIfCorrectInstallPartition(partitionToInstallPathList, dest, filepath.Base(src), groupedSources[srcFileDir])
205 }
206
207 return groupedSources
208}
209
210type prebuiltModuleProperties struct {
211 Name *string
212
213 Soc_specific *bool
214 Product_specific *bool
215 System_ext_specific *bool
216
217 Srcs []string
218 Dsts []string
219
220 No_full_install *bool
221
222 NamespaceExportedToMake bool
223
224 Visibility []string
225}
226
227// Split relative_install_path to a separate struct, because it is not supported for every
228// modules listed in [etcInstallPathToFactoryMap]
229type prebuiltSubdirProperties struct {
230 // If the base file name of the src and dst all match, dsts property does not need to be
231 // set, and only relative_install_path can be set.
232 Relative_install_path *string
233}
234
235var (
236 etcInstallPathToFactoryList = map[string]android.ModuleFactory{
237 "": etc.PrebuiltRootFactory,
238 "avb": etc.PrebuiltAvbFactory,
239 "bin": etc.PrebuiltBinaryFactory,
240 "bt_firmware": etc.PrebuiltBtFirmwareFactory,
241 "cacerts": etc.PrebuiltEtcCaCertsFactory,
242 "dsp": etc.PrebuiltDSPFactory,
243 "etc": etc.PrebuiltEtcFactory,
244 "etc/dsp": etc.PrebuiltDSPFactory,
245 "etc/firmware": etc.PrebuiltFirmwareFactory,
246 "firmware": etc.PrebuiltFirmwareFactory,
247 "fonts": etc.PrebuiltFontFactory,
248 "framework": etc.PrebuiltFrameworkFactory,
249 "lib": etc.PrebuiltRenderScriptBitcodeFactory,
250 "lib64": etc.PrebuiltRenderScriptBitcodeFactory,
251 "lib/rfsa": etc.PrebuiltRFSAFactory,
252 "media": etc.PrebuiltMediaFactory,
253 "odm": etc.PrebuiltOdmFactory,
254 "overlay": etc.PrebuiltOverlayFactory,
255 "priv-app": etc.PrebuiltPrivAppFactory,
256 "res": etc.PrebuiltResFactory,
257 "rfs": etc.PrebuiltRfsFactory,
258 "tts": etc.PrebuiltVoicepackFactory,
259 "usr/share": etc.PrebuiltUserShareFactory,
260 "usr/hyphen-data": etc.PrebuiltUserHyphenDataFactory,
261 "usr/keylayout": etc.PrebuiltUserKeyLayoutFactory,
262 "usr/keychars": etc.PrebuiltUserKeyCharsFactory,
263 "usr/srec": etc.PrebuiltUserSrecFactory,
264 "usr/idc": etc.PrebuiltUserIdcFactory,
265 "vendor_dlkm": etc.PrebuiltVendorDlkmFactory,
266 "wallpaper": etc.PrebuiltWallpaperFactory,
267 "wlc_upt": etc.PrebuiltWlcUptFactory,
268 }
269)
270
271func createPrebuiltEtcModule(ctx android.LoadHookContext, partition, srcDir, destDir string, destFiles []srcBaseFileInstallBaseFileTuple) string {
272 moduleProps := &prebuiltModuleProperties{}
273 propsList := []interface{}{moduleProps}
274
275 // generated module name follows the pattern:
276 // <install partition>-<src file path>-<relative install path from partition root>-<install file extension>
277 // Note that all path separators are replaced with "_" in the name
278 moduleName := partition
279 if !android.InList(srcDir, []string{"", "."}) {
280 moduleName += fmt.Sprintf("-%s", strings.ReplaceAll(srcDir, string(filepath.Separator), "_"))
281 }
282 if !android.InList(destDir, []string{"", "."}) {
283 moduleName += fmt.Sprintf("-%s", strings.ReplaceAll(destDir, string(filepath.Separator), "_"))
284 }
285 if len(destFiles) > 0 {
286 if ext := filepath.Ext(destFiles[0].srcBaseFile); ext != "" {
287 moduleName += fmt.Sprintf("-%s", strings.TrimPrefix(ext, "."))
288 }
289 }
290 moduleProps.Name = proptools.StringPtr(moduleName)
291
292 allCopyFileNamesUnchanged := true
293 var srcBaseFiles, installBaseFiles []string
294 for _, tuple := range destFiles {
295 if tuple.srcBaseFile != tuple.installBaseFile {
296 allCopyFileNamesUnchanged = false
297 }
298 srcBaseFiles = append(srcBaseFiles, tuple.srcBaseFile)
299 installBaseFiles = append(installBaseFiles, tuple.installBaseFile)
300 }
301
302 // Find out the most appropriate module type to generate
303 var etcInstallPathKey string
304 for _, etcInstallPath := range android.SortedKeys(etcInstallPathToFactoryList) {
305 // Do not break when found but iterate until the end to find a module with more
306 // specific install path
307 if strings.HasPrefix(destDir, etcInstallPath) {
308 etcInstallPathKey = etcInstallPath
309 }
310 }
311 destDir, _ = filepath.Rel(etcInstallPathKey, destDir)
312
313 // Set partition specific properties
314 switch partition {
315 case "system_ext":
316 moduleProps.System_ext_specific = proptools.BoolPtr(true)
317 case "product":
318 moduleProps.Product_specific = proptools.BoolPtr(true)
319 case "vendor":
320 moduleProps.Soc_specific = proptools.BoolPtr(true)
321 }
322
323 // Set appropriate srcs, dsts, and releative_install_path based on
324 // the source and install file names
325 if allCopyFileNamesUnchanged {
326 moduleProps.Srcs = srcBaseFiles
327
328 // Specify relative_install_path if it is not installed in the root directory of the
329 // partition
330 if !android.InList(destDir, []string{"", "."}) {
331 propsList = append(propsList, &prebuiltSubdirProperties{
332 Relative_install_path: proptools.StringPtr(destDir),
333 })
334 }
335 } else {
336 moduleProps.Srcs = srcBaseFiles
337 dsts := []string{}
338 for _, installBaseFile := range installBaseFiles {
339 dsts = append(dsts, filepath.Join(destDir, installBaseFile))
340 }
341 moduleProps.Dsts = dsts
342 }
343
344 moduleProps.No_full_install = proptools.BoolPtr(true)
345 moduleProps.NamespaceExportedToMake = true
346 moduleProps.Visibility = []string{"//visibility:public"}
347
348 ctx.CreateModuleInDirectory(etcInstallPathToFactoryList[etcInstallPathKey], srcDir, propsList...)
349
350 return moduleName
351}
352
353func createPrebuiltEtcModulesForPartition(ctx android.LoadHookContext, partition, srcDir string, destDirFilesMap map[string][]srcBaseFileInstallBaseFileTuple) (ret []string) {
354 for _, destDir := range android.SortedKeys(destDirFilesMap) {
355 ret = append(ret, createPrebuiltEtcModule(ctx, partition, srcDir, destDir, destDirFilesMap[destDir]))
356 }
357 return ret
358}
359
360// Creates prebuilt_* modules based on the install paths and returns the list of generated
361// module names
362func createPrebuiltEtcModules(ctx android.LoadHookContext) (ret []string) {
363 groupedSources := processProductCopyFiles(ctx)
364 for _, srcDir := range android.SortedKeys(groupedSources) {
365 groupedSource := groupedSources[srcDir]
366 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "system", srcDir, groupedSource.system)...)
367 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "system_ext", srcDir, groupedSource.system_ext)...)
368 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "product", srcDir, groupedSource.product)...)
369 ret = append(ret, createPrebuiltEtcModulesForPartition(ctx, "vendor", srcDir, groupedSource.vendor)...)
370 }
371
372 return ret
373}
374
375func generatedPartitions(ctx android.LoadHookContext) []string {
376 generatedPartitions := []string{"system"}
377 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
378 generatedPartitions = append(generatedPartitions, "system_ext")
379 }
380 if ctx.DeviceConfig().BuildingVendorImage() && ctx.DeviceConfig().VendorPath() == "vendor" {
381 generatedPartitions = append(generatedPartitions, "vendor")
382 }
383 if ctx.DeviceConfig().BuildingProductImage() && ctx.DeviceConfig().ProductPath() == "product" {
384 generatedPartitions = append(generatedPartitions, "product")
385 }
386 if ctx.DeviceConfig().BuildingOdmImage() && ctx.DeviceConfig().OdmPath() == "odm" {
387 generatedPartitions = append(generatedPartitions, "odm")
388 }
389 if ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.BuildingSystemDlkmImage {
390 generatedPartitions = append(generatedPartitions, "system_dlkm")
391 }
392 return generatedPartitions
393}
394
395func createFsGenState(ctx android.LoadHookContext, generatedPrebuiltEtcModuleNames []string) *FsGenState {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000396 return ctx.Config().Once(fsGenStateOnceKey, func() interface{} {
397 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
398 candidates := android.FirstUniqueStrings(android.Concat(partitionVars.ProductPackages, partitionVars.ProductPackagesDebug))
Jihoon Kang675d4682024-10-24 23:45:11 +0000399 candidates = android.Concat(candidates, generatedPrebuiltEtcModuleNames)
Jihoon Kang0d545b82024-10-11 00:21:57 +0000400
401 return &FsGenState{
402 depCandidates: candidates,
Jihoon Kang83f135b2024-11-01 22:56:07 +0000403 fsDeps: map[string]*multilibDeps{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000404 // These additional deps are added according to the cuttlefish system image bp.
Jihoon Kang83f135b2024-11-01 22:56:07 +0000405 "system": {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000406 "com.android.apex.cts.shim.v1_prebuilt": defaultDepCandidateProps(ctx.Config()),
407 "dex_bootjars": defaultDepCandidateProps(ctx.Config()),
408 "framework_compatibility_matrix.device.xml": defaultDepCandidateProps(ctx.Config()),
Jihoon Kang675d4682024-10-24 23:45:11 +0000409 "init.environ.rc-soong": defaultDepCandidateProps(ctx.Config()),
410 "libclang_rt.asan": defaultDepCandidateProps(ctx.Config()),
411 "libcompiler_rt": defaultDepCandidateProps(ctx.Config()),
412 "libdmabufheap": defaultDepCandidateProps(ctx.Config()),
413 "libgsi": defaultDepCandidateProps(ctx.Config()),
414 "llndk.libraries.txt": defaultDepCandidateProps(ctx.Config()),
415 "logpersist.start": defaultDepCandidateProps(ctx.Config()),
416 "update_engine_sideload": defaultDepCandidateProps(ctx.Config()),
Jihoon Kang0d545b82024-10-11 00:21:57 +0000417 },
Jihoon Kang83f135b2024-11-01 22:56:07 +0000418 "vendor": {
Spandan Das168098c2024-10-28 19:44:34 +0000419 "fs_config_files_vendor": defaultDepCandidateProps(ctx.Config()),
420 "fs_config_dirs_vendor": defaultDepCandidateProps(ctx.Config()),
421 generatedModuleName(ctx.Config(), "vendor-build.prop"): defaultDepCandidateProps(ctx.Config()),
Spandan Das49cc3e82024-10-23 20:54:01 +0000422 },
Jihoon Kang83f135b2024-11-01 22:56:07 +0000423 "odm": {
Spandan Dasc5717162024-11-01 18:33:57 +0000424 // fs_config_* files are automatically installed for all products with odm partitions.
425 // https://cs.android.com/android/_/android/platform/build/+/e4849e87ab660b59a6501b3928693db065ee873b:tools/fs_config/Android.mk;l=34;drc=8d6481b92c4b4e9b9f31a61545b6862090fcc14b;bpv=1;bpt=0
426 "fs_config_files_odm": defaultDepCandidateProps(ctx.Config()),
427 "fs_config_dirs_odm": defaultDepCandidateProps(ctx.Config()),
428 },
Jihoon Kang83f135b2024-11-01 22:56:07 +0000429 "product": {},
430 "system_ext": {
Spandan Dasd9875bc2024-10-17 21:36:17 +0000431 // VNDK apexes are automatically included.
432 // This hardcoded list will need to be updated if `PRODUCT_EXTRA_VNDK_VERSIONS` is updated.
433 // https://cs.android.com/android/_/android/platform/build/+/adba533072b00c53ac0f198c550a3cbd7a00e4cd:core/main.mk;l=984;bpv=1;bpt=0;drc=174db7b179592cf07cbfd2adb0119486fda911e7
434 "com.android.vndk.v30": defaultDepCandidateProps(ctx.Config()),
435 "com.android.vndk.v31": defaultDepCandidateProps(ctx.Config()),
436 "com.android.vndk.v32": defaultDepCandidateProps(ctx.Config()),
437 "com.android.vndk.v33": defaultDepCandidateProps(ctx.Config()),
438 "com.android.vndk.v34": defaultDepCandidateProps(ctx.Config()),
439 },
Spandan Das5e336422024-11-01 22:31:20 +0000440 "system_dlkm": {},
Jihoon Kang0d545b82024-10-11 00:21:57 +0000441 },
Jihoon Kang675d4682024-10-24 23:45:11 +0000442 soongGeneratedPartitions: generatedPartitions(ctx),
Spandan Dase1860e42024-10-24 22:29:50 +0000443 fsDepsMutex: sync.Mutex{},
444 moduleToInstallationProps: map[string]installationProperties{},
Jihoon Kang0d545b82024-10-11 00:21:57 +0000445 }
446 }).(*FsGenState)
447}
448
Jihoon Kang83f135b2024-11-01 22:56:07 +0000449func checkDepModuleInMultipleNamespaces(mctx android.BottomUpMutatorContext, foundDeps multilibDeps, module string, partitionName string) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000450 otherNamespace := mctx.Namespace().Path
451 if val, found := foundDeps[module]; found && otherNamespace != "." && !android.InList(val.Namespace, []string{".", otherNamespace}) {
452 mctx.ModuleErrorf("found in multiple namespaces(%s and %s) when including in %s partition", val.Namespace, otherNamespace, partitionName)
453 }
454}
455
Jihoon Kang83f135b2024-11-01 22:56:07 +0000456func appendDepIfAppropriate(mctx android.BottomUpMutatorContext, deps *multilibDeps, installPartition string) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000457 checkDepModuleInMultipleNamespaces(mctx, *deps, mctx.Module().Name(), installPartition)
458 if _, ok := (*deps)[mctx.Module().Name()]; ok {
459 // Prefer the namespace-specific module over the platform module
460 if mctx.Namespace().Path != "." {
461 (*deps)[mctx.Module().Name()].Namespace = mctx.Namespace().Path
462 }
463 (*deps)[mctx.Module().Name()].Arch = append((*deps)[mctx.Module().Name()].Arch, mctx.Module().Target().Arch.ArchType)
464 } else {
465 multilib, _ := mctx.Module().DecodeMultilib(mctx)
466 (*deps)[mctx.Module().Name()] = &depCandidateProps{
467 Namespace: mctx.Namespace().Path,
468 Multilib: multilib,
469 Arch: []android.ArchType{mctx.Module().Target().Arch.ArchType},
470 }
471 }
472}
mrziwang8f86c882024-10-03 12:34:33 -0700473
474func collectDepsMutator(mctx android.BottomUpMutatorContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000475 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
mrziwang8f86c882024-10-03 12:34:33 -0700476
477 m := mctx.Module()
Spandan Dasfcc07c02024-10-21 23:33:43 +0000478 if m.Target().Os.Class == android.Device && slices.Contains(fsGenState.depCandidates, m.Name()) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000479 installPartition := m.PartitionTag(mctx.DeviceConfig())
480 fsGenState.fsDepsMutex.Lock()
481 // Only add the module as dependency when:
482 // - its enabled
483 // - its namespace is included in PRODUCT_SOONG_NAMESPACES
484 if m.Enabled(mctx) && m.ExportedToMake() {
485 appendDepIfAppropriate(mctx, fsGenState.fsDeps[installPartition], installPartition)
486 }
487 fsGenState.fsDepsMutex.Unlock()
488 }
Spandan Dase1860e42024-10-24 22:29:50 +0000489 // store the map of module to (required,overrides) even if the module is not in PRODUCT_PACKAGES.
490 // the module might be installed transitively.
491 if m.Target().Os.Class == android.Device && m.Enabled(mctx) && m.ExportedToMake() {
492 fsGenState.fsDepsMutex.Lock()
493 fsGenState.moduleToInstallationProps[m.Name()] = installationProperties{
494 Required: m.RequiredModuleNames(mctx),
495 Overrides: m.Overrides(),
496 }
497 fsGenState.fsDepsMutex.Unlock()
498 }
Jihoon Kang0d545b82024-10-11 00:21:57 +0000499}
500
501type depsStruct struct {
502 Deps []string
503}
504
505type multilibDepsStruct struct {
506 Common depsStruct
507 Lib32 depsStruct
508 Lib64 depsStruct
509 Both depsStruct
510 Prefer32 depsStruct
511}
512
513type packagingPropsStruct struct {
Jihoon Kang26cfe3c2024-10-30 18:53:51 +0000514 High_priority_deps []string
515 Deps []string
516 Multilib multilibDepsStruct
Jihoon Kang0d545b82024-10-11 00:21:57 +0000517}
518
519func fullyQualifiedModuleName(moduleName, namespace string) string {
520 if namespace == "." {
521 return moduleName
522 }
523 return fmt.Sprintf("//%s:%s", namespace, moduleName)
524}
525
Jihoon Kang0d545b82024-10-11 00:21:57 +0000526func getBitness(archTypes []android.ArchType) (ret []string) {
527 for _, archType := range archTypes {
528 if archType.Multilib == "" {
529 ret = append(ret, android.COMMON_VARIANT)
530 } else {
531 ret = append(ret, archType.Bitness())
532 }
533 }
534 return ret
535}
536
537func setDepsMutator(mctx android.BottomUpMutatorContext) {
Spandan Dase1860e42024-10-24 22:29:50 +0000538 removeOverriddenDeps(mctx)
Jihoon Kang0d545b82024-10-11 00:21:57 +0000539 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
540 fsDeps := fsGenState.fsDeps
541 soongGeneratedPartitionMap := getAllSoongGeneratedPartitionNames(mctx.Config(), fsGenState.soongGeneratedPartitions)
542 m := mctx.Module()
543 if partition, ok := soongGeneratedPartitionMap[m.Name()]; ok {
mrziwang2a506cf2024-10-17 15:38:37 -0700544 depsStruct := generateDepStruct(*fsDeps[partition])
545 if err := proptools.AppendMatchingProperties(m.GetProperties(), depsStruct, nil); err != nil {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000546 mctx.ModuleErrorf(err.Error())
mrziwang8f86c882024-10-03 12:34:33 -0700547 }
548 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000549}
550
Spandan Dase1860e42024-10-24 22:29:50 +0000551// removeOverriddenDeps collects PRODUCT_PACKAGES and (transitive) required deps.
552// it then removes any modules which appear in `overrides` of the above list.
553func removeOverriddenDeps(mctx android.BottomUpMutatorContext) {
554 mctx.Config().Once(fsGenRemoveOverridesOnceKey, func() interface{} {
555 fsGenState := mctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
556 fsDeps := fsGenState.fsDeps
557 overridden := map[string]bool{}
558 allDeps := []string{}
559
560 // Step 1: Initialization: Append PRODUCT_PACKAGES to the queue
561 for _, fsDep := range fsDeps {
562 for depName, _ := range *fsDep {
563 allDeps = append(allDeps, depName)
564 }
565 }
566
567 // Step 2: Process the queue, and add required modules to the queue.
568 i := 0
569 for {
570 if i == len(allDeps) {
571 break
572 }
573 depName := allDeps[i]
574 for _, overrides := range fsGenState.moduleToInstallationProps[depName].Overrides {
575 overridden[overrides] = true
576 }
577 // add required dep to the queue.
578 allDeps = append(allDeps, fsGenState.moduleToInstallationProps[depName].Required...)
579 i += 1
580 }
581
582 // Step 3: Delete all the overridden modules.
583 for overridden, _ := range overridden {
584 for partition, _ := range fsDeps {
585 delete(*fsDeps[partition], overridden)
586 }
587 }
588 return nil
589 })
590}
591
Jihoon Kang26cfe3c2024-10-30 18:53:51 +0000592var HighPriorityDeps = []string{}
593
mrziwang2a506cf2024-10-17 15:38:37 -0700594func generateDepStruct(deps map[string]*depCandidateProps) *packagingPropsStruct {
595 depsStruct := packagingPropsStruct{}
596 for depName, depProps := range deps {
597 bitness := getBitness(depProps.Arch)
598 fullyQualifiedDepName := fullyQualifiedModuleName(depName, depProps.Namespace)
Jihoon Kang26cfe3c2024-10-30 18:53:51 +0000599 if android.InList(depName, HighPriorityDeps) {
600 depsStruct.High_priority_deps = append(depsStruct.High_priority_deps, fullyQualifiedDepName)
601 } else if android.InList("32", bitness) && android.InList("64", bitness) {
mrziwang2a506cf2024-10-17 15:38:37 -0700602 // If both 32 and 64 bit variants are enabled for this module
603 switch depProps.Multilib {
604 case string(android.MultilibBoth):
605 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
606 case string(android.MultilibCommon), string(android.MultilibFirst):
607 depsStruct.Deps = append(depsStruct.Deps, fullyQualifiedDepName)
608 case "32":
609 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
610 case "64", "darwin_universal":
611 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
612 case "prefer32", "first_prefer32":
613 depsStruct.Multilib.Prefer32.Deps = append(depsStruct.Multilib.Prefer32.Deps, fullyQualifiedDepName)
614 default:
615 depsStruct.Multilib.Both.Deps = append(depsStruct.Multilib.Both.Deps, fullyQualifiedDepName)
616 }
617 } else if android.InList("64", bitness) {
618 // If only 64 bit variant is enabled
619 depsStruct.Multilib.Lib64.Deps = append(depsStruct.Multilib.Lib64.Deps, fullyQualifiedDepName)
620 } else if android.InList("32", bitness) {
621 // If only 32 bit variant is enabled
622 depsStruct.Multilib.Lib32.Deps = append(depsStruct.Multilib.Lib32.Deps, fullyQualifiedDepName)
623 } else {
624 // If only common variant is enabled
625 depsStruct.Multilib.Common.Deps = append(depsStruct.Multilib.Common.Deps, fullyQualifiedDepName)
626 }
627 }
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000628 depsStruct.Deps = android.SortedUniqueStrings(depsStruct.Deps)
629 depsStruct.Multilib.Lib32.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Lib32.Deps)
630 depsStruct.Multilib.Lib64.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Lib64.Deps)
631 depsStruct.Multilib.Prefer32.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Prefer32.Deps)
632 depsStruct.Multilib.Both.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Both.Deps)
633 depsStruct.Multilib.Common.Deps = android.SortedUniqueStrings(depsStruct.Multilib.Common.Deps)
634
mrziwang2a506cf2024-10-17 15:38:37 -0700635 return &depsStruct
636}
637
Cole Faust92ccbe22024-10-03 14:38:37 -0700638type filesystemCreatorProps struct {
639 Generated_partition_types []string `blueprint:"mutated"`
640 Unsupported_partition_types []string `blueprint:"mutated"`
641}
642
Jihoon Kang98047cf2024-10-02 17:13:54 +0000643type filesystemCreator struct {
644 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -0700645
646 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +0000647}
648
649func filesystemCreatorFactory() android.Module {
650 module := &filesystemCreator{}
651
Cole Faust69788792024-10-10 11:00:36 -0700652 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -0700653 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000654 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jihoon Kang675d4682024-10-24 23:45:11 +0000655 generatedPrebuiltEtcModuleNames := createPrebuiltEtcModules(ctx)
656 createFsGenState(ctx, generatedPrebuiltEtcModuleNames)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000657 module.createInternalModules(ctx)
658 })
659
660 return module
661}
662
663func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000664 soongGeneratedPartitions := &ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions
665 for _, partitionType := range *soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -0700666 if f.createPartition(ctx, partitionType) {
667 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
668 } else {
669 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
Jihoon Kang0d545b82024-10-11 00:21:57 +0000670 _, *soongGeneratedPartitions = android.RemoveFromList(partitionType, *soongGeneratedPartitions)
Cole Faust92ccbe22024-10-03 14:38:37 -0700671 }
672 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000673 f.createDeviceModule(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000674}
675
Jihoon Kang0d545b82024-10-11 00:21:57 +0000676func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700677 prefix := "soong"
678 if cfg.HasDeviceProduct() {
679 prefix = cfg.DeviceProduct()
680 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000681 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
682}
683
Jihoon Kang0d545b82024-10-11 00:21:57 +0000684func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
685 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000686}
687
688func (f *filesystemCreator) createDeviceModule(ctx android.LoadHookContext) {
689 baseProps := &struct {
690 Name *string
691 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000692 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000693 }
694
Priyanka Advani (xWF)dafaa7f2024-10-21 22:55:13 +0000695 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000696 partitionProps := &filesystem.PartitionNameProperties{}
697 if android.InList("system", f.properties.Generated_partition_types) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000698 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000699 }
Spandan Das7a46f6c2024-10-14 18:41:18 +0000700 if android.InList("system_ext", f.properties.Generated_partition_types) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000701 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000702 }
Spandan Dase3b65312024-10-22 00:27:27 +0000703 if android.InList("vendor", f.properties.Generated_partition_types) {
704 partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
705 }
Jihoon Kang6dd13b62024-10-22 23:21:02 +0000706 if android.InList("product", f.properties.Generated_partition_types) {
707 partitionProps.Product_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product"))
708 }
Spandan Dasc5717162024-11-01 18:33:57 +0000709 if android.InList("odm", f.properties.Generated_partition_types) {
710 partitionProps.Odm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm"))
711 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000712
713 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700714}
715
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000716func partitionSpecificFsProps(fsProps *filesystem.FilesystemProperties, partitionType string) {
717 switch partitionType {
718 case "system":
719 fsProps.Build_logtags = proptools.BoolPtr(true)
720 // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
721 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000722 // Identical to that of the generic_system_image
723 fsProps.Fsverity.Inputs = []string{
724 "etc/boot-image.prof",
725 "etc/dirty-image-objects",
726 "etc/preloaded-classes",
727 "etc/classpaths/*.pb",
728 "framework/*",
729 "framework/*/*", // framework/{arch}
730 "framework/oat/*/*", // framework/oat/{arch}
731 }
732 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
733 case "system_ext":
734 fsProps.Fsverity.Inputs = []string{
735 "framework/*",
736 "framework/*/*", // framework/{arch}
737 "framework/oat/*/*", // framework/oat/{arch}
738 }
739 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000740 case "product":
741 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
742 case "vendor":
743 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das69464c32024-10-25 20:08:06 +0000744 fsProps.Symlinks = []filesystem.SymlinkDefinition{
745 filesystem.SymlinkDefinition{
746 Target: proptools.StringPtr("/odm"),
747 Name: proptools.StringPtr("vendor/odm"),
748 },
749 filesystem.SymlinkDefinition{
750 Target: proptools.StringPtr("/vendor_dlkm/lib/modules"),
751 Name: proptools.StringPtr("vendor/lib/modules"),
752 },
753 }
754 fsProps.Base_dir = proptools.StringPtr("vendor")
Spandan Dasc5717162024-11-01 18:33:57 +0000755 case "odm":
756 fsProps.Symlinks = []filesystem.SymlinkDefinition{
757 filesystem.SymlinkDefinition{
758 Target: proptools.StringPtr("/odm_dlkm/lib/modules"),
759 Name: proptools.StringPtr("odm/lib/modules"),
760 },
761 }
762 fsProps.Base_dir = proptools.StringPtr("odm")
763
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000764 }
765}
Spandan Dascbe641a2024-10-14 21:07:34 +0000766
Cole Faust92ccbe22024-10-03 14:38:37 -0700767// Creates a soong module to build the given partition. Returns false if we can't support building
768// it.
769func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwang4b0ca972024-10-17 14:56:19 -0700770 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
771
772 fsProps, supported := generateFsProps(ctx, partitionType)
773 if !supported {
774 return false
mrziwanga077b942024-10-16 16:00:06 -0700775 }
mrziwanga077b942024-10-16 16:00:06 -0700776
Spandan Das8fe68dc2024-10-29 18:20:11 +0000777 if partitionType == "vendor" || partitionType == "product" {
Spandan Das173256b2024-10-31 19:59:30 +0000778 fsProps.Linkerconfig.Gen_linker_config = proptools.BoolPtr(true)
779 fsProps.Linkerconfig.Linker_config_srcs = f.createLinkerConfigSourceFilegroups(ctx, partitionType)
Spandan Das312cc412024-10-29 18:20:11 +0000780 }
781
Spandan Das5e336422024-11-01 22:31:20 +0000782 if partitionType == "system_dlkm" {
783 kernelModules := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules
784 f.createPrebuiltKernelModules(ctx, partitionType, kernelModules)
785 }
786
mrziwang4b0ca972024-10-17 14:56:19 -0700787 var module android.Module
788 if partitionType == "system" {
789 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
Spandan Das5e336422024-11-01 22:31:20 +0000790 } else if partitionType == "system_dlkm" {
791 // Do not set partition_type. build/soong/android/paths#modulePartition currently does not support dlkm
792 // partitions. Since `android_filesystem` uses a partition based filter, setting the partition here
793 // would result in missing in entries.
794 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
mrziwang4b0ca972024-10-17 14:56:19 -0700795 } else {
796 // Explicitly set the partition.
797 fsProps.Partition_type = proptools.StringPtr(partitionType)
798 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
799 }
800 module.HideFromMake()
Spandan Das168098c2024-10-28 19:44:34 +0000801 if partitionType == "vendor" {
Spandan Das4cd93b52024-11-05 23:27:03 +0000802 f.createVendorBuildProp(ctx)
Spandan Das168098c2024-10-28 19:44:34 +0000803 }
mrziwang4b0ca972024-10-17 14:56:19 -0700804 return true
805}
806
Spandan Das5e336422024-11-01 22:31:20 +0000807// createPrebuiltKernelModules creates `prebuilt_kernel_modules`. These modules will be added to deps of the
808// autogenerated *_dlkm filsystem modules.
809// The input `kernelModules` is a space separated list of .ko files in the workspace. This will be partitioned per directory
810// and a `prebuilt_kernel_modules` will be created per partition.
811// These autogenerated modules will be subsequently added to the deps of the top level *_dlkm android_filesystem
812func (f *filesystemCreator) createPrebuiltKernelModules(ctx android.LoadHookContext, partitionType string, kernelModules []string) {
813 // Partition the files per directory
814 dirToFiles := map[string][]string{}
815 for _, kernelModule := range kernelModules {
816 dir := filepath.Dir(kernelModule)
817 base := filepath.Base(kernelModule)
818 dirToFiles[dir] = append(dirToFiles[dir], base)
819 }
820 // Create a prebuilt_kernel_modules module per partition
821 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
822 for index, dir := range android.SortedKeys(dirToFiles) {
823 name := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-kernel-modules-%s", partitionType, strconv.Itoa(index)))
824 props := &struct {
825 Name *string
826 Srcs []string
827 }{
828 Name: proptools.StringPtr(name),
829 Srcs: dirToFiles[dir],
830 }
831 kernelModule := ctx.CreateModuleInDirectory(
832 kernel.PrebuiltKernelModulesFactory,
833 dir,
834 props,
835 )
836 kernelModule.HideFromMake()
837 // Add to deps
838 (*fsGenState.fsDeps[partitionType])[name] = defaultDepCandidateProps(ctx.Config())
839 }
840}
841
Spandan Das4cd93b52024-11-05 23:27:03 +0000842// Create a build_prop and android_info module. This will be used to create /vendor/build.prop
843func (f *filesystemCreator) createVendorBuildProp(ctx android.LoadHookContext) {
844 // Create a android_info for vendor
845 // The board info files might be in a directory outside the root soong namespace, so create
846 // the module in "."
847 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
848 androidInfoProps := &struct {
849 Name *string
850 Board_info_files []string
851 Bootloader_board_name *string
852 }{
853 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "android-info.prop")),
854 Board_info_files: partitionVars.BoardInfoFiles,
855 }
856 if len(androidInfoProps.Board_info_files) == 0 {
857 androidInfoProps.Bootloader_board_name = proptools.StringPtr(partitionVars.BootLoaderBoardName)
858 }
859 androidInfoProp := ctx.CreateModuleInDirectory(
860 android.AndroidInfoFactory,
861 ".",
862 androidInfoProps,
863 )
864 androidInfoProp.HideFromMake()
865 // Create a build prop for vendor
866 vendorBuildProps := &struct {
867 Name *string
868 Vendor *bool
869 Stem *string
870 Product_config *string
871 Android_info *string
872 }{
873 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "vendor-build.prop")),
874 Vendor: proptools.BoolPtr(true),
875 Stem: proptools.StringPtr("build.prop"),
876 Product_config: proptools.StringPtr(":product_config"),
877 Android_info: proptools.StringPtr(":" + androidInfoProp.Name()),
878 }
879 vendorBuildProp := ctx.CreateModule(
880 android.BuildPropFactory,
881 vendorBuildProps,
882 )
883 vendorBuildProp.HideFromMake()
884}
885
Spandan Das8fe68dc2024-10-29 18:20:11 +0000886// createLinkerConfigSourceFilegroups creates filegroup modules to generate linker.config.pb for the following partitions
887// 1. vendor: Using PRODUCT_VENDOR_LINKER_CONFIG_FRAGMENTS (space separated file list)
888// 1. product: Using PRODUCT_PRODUCT_LINKER_CONFIG_FRAGMENTS (space separated file list)
889// It creates a filegroup for each file in the fragment list
Spandan Das312cc412024-10-29 18:20:11 +0000890// The filegroup modules are then added to `linker_config_srcs` of the autogenerated vendor `android_filesystem`.
Spandan Das8fe68dc2024-10-29 18:20:11 +0000891func (f *filesystemCreator) createLinkerConfigSourceFilegroups(ctx android.LoadHookContext, partitionType string) []string {
Spandan Das312cc412024-10-29 18:20:11 +0000892 ret := []string{}
893 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Spandan Das8fe68dc2024-10-29 18:20:11 +0000894 var linkerConfigSrcs []string
895 if partitionType == "vendor" {
896 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.VendorLinkerConfigSrcs)
897 } else if partitionType == "product" {
898 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.ProductLinkerConfigSrcs)
899 } else {
900 ctx.ModuleErrorf("linker.config.pb is only supported for vendor and product partitions. For system partition, use `android_system_image`")
901 }
902
903 if len(linkerConfigSrcs) > 0 {
Spandan Das312cc412024-10-29 18:20:11 +0000904 // Create a filegroup, and add `:<filegroup_name>` to ret.
905 for index, linkerConfigSrc := range linkerConfigSrcs {
906 dir := filepath.Dir(linkerConfigSrc)
907 base := filepath.Base(linkerConfigSrc)
Spandan Das8fe68dc2024-10-29 18:20:11 +0000908 fgName := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-linker-config-src%s", partitionType, strconv.Itoa(index)))
Spandan Das312cc412024-10-29 18:20:11 +0000909 srcs := []string{base}
910 fgProps := &struct {
911 Name *string
912 Srcs proptools.Configurable[[]string]
913 }{
914 Name: proptools.StringPtr(fgName),
915 Srcs: proptools.NewSimpleConfigurable(srcs),
916 }
917 ctx.CreateModuleInDirectory(
918 android.FileGroupFactory,
919 dir,
920 fgProps,
921 )
922 ret = append(ret, ":"+fgName)
923 }
924 }
925 return ret
926}
927
mrziwang4b0ca972024-10-17 14:56:19 -0700928type filesystemBaseProperty struct {
929 Name *string
930 Compile_multilib *string
931}
932
933func generateBaseProps(namePtr *string) *filesystemBaseProperty {
934 return &filesystemBaseProperty{
935 Name: namePtr,
936 Compile_multilib: proptools.StringPtr("both"),
937 }
938}
939
940func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700941 fsProps := &filesystem.FilesystemProperties{}
942
mrziwang4b0ca972024-10-17 14:56:19 -0700943 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
944 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
945
946 // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE
947 fsType := specificPartitionVars.BoardFileSystemType
948 if fsType == "" {
949 fsType = "ext4" //default
950 }
951 fsProps.Type = proptools.StringPtr(fsType)
952 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
953 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
954 return nil, false
955 }
956
Cole Faust92ccbe22024-10-03 14:38:37 -0700957 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
958 // and sometimes don't build.
959 fsProps.Unchecked_module = proptools.BoolPtr(true)
960
Jihoon Kang98047cf2024-10-02 17:13:54 +0000961 // BOARD_AVB_ENABLE
962 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
963 // BOARD_AVB_KEY_PATH
Cole Faust92ccbe22024-10-03 14:38:37 -0700964 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000965 // BOARD_AVB_ALGORITHM
Cole Faust92ccbe22024-10-03 14:38:37 -0700966 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000967 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust92ccbe22024-10-03 14:38:37 -0700968 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000969 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
970 }
971
Cole Faust92ccbe22024-10-03 14:38:37 -0700972 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000973
Cole Faust92ccbe22024-10-03 14:38:37 -0700974 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000975
Jihoon Kang0d545b82024-10-11 00:21:57 +0000976 fsProps.Is_auto_generated = proptools.BoolPtr(true)
977
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000978 partitionSpecificFsProps(fsProps, partitionType)
979
Jihoon Kang98047cf2024-10-02 17:13:54 +0000980 // system_image properties that are not set:
981 // - filesystemProperties.Avb_hash_algorithm
982 // - filesystemProperties.File_contexts
983 // - filesystemProperties.Dirs
984 // - filesystemProperties.Symlinks
985 // - filesystemProperties.Fake_timestamp
986 // - filesystemProperties.Uuid
987 // - filesystemProperties.Mount_point
988 // - filesystemProperties.Include_make_built_files
989 // - filesystemProperties.Build_logtags
Jihoon Kang98047cf2024-10-02 17:13:54 +0000990 // - systemImageProperties.Linker_config_src
mrziwang4b0ca972024-10-17 14:56:19 -0700991
992 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700993}
994
995func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000996 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700997 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
998 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
999 if !ok {
1000 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
1001 }
1002 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
1003 // For now, don't allowlist anything. The test will fail, but that's fine in the current
1004 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +00001005 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -07001006 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +00001007 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -07001008
1009 builder := android.NewRuleBuilder(pctx, ctx)
1010 builder.Command().BuiltTool("file_list_diff").
1011 Input(makeFileList).
1012 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +00001013 Text(partitionModuleName).
1014 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -07001015 builder.Command().Text("touch").Output(diffTestResultFile)
1016 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
1017 return diffTestResultFile
1018}
1019
1020func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
1021 hasher := sha256.New()
1022 hasher.Write([]byte(message))
1023 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
1024 file := android.PathForModuleOut(ctx, filename)
1025 builder := android.NewRuleBuilder(pctx, ctx)
1026 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
1027 builder.Command().Text("exit 1 #").Output(file)
1028 builder.Build("failing command "+filename, "failing command "+filename)
1029 return file
1030}
1031
1032type systemImageDepTagType struct {
1033 blueprint.BaseDependencyTag
1034}
1035
1036var generatedFilesystemDepTag systemImageDepTagType
1037
1038func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
1039 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +00001040 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -07001041 }
Jihoon Kang98047cf2024-10-02 17:13:54 +00001042}
1043
1044func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -07001045 if ctx.ModuleDir() != "build/soong/fsgen" {
1046 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
1047 }
1048 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +00001049
Jihoon Kang4e5d8de2024-10-19 01:59:58 +00001050 var content strings.Builder
1051 generatedBp := android.PathForModuleOut(ctx, "soong_generated_product_config.bp")
1052 for _, partition := range ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions {
1053 content.WriteString(generateBpContent(ctx, partition))
1054 content.WriteString("\n")
1055 }
1056 android.WriteFileRule(ctx, generatedBp, content.String())
1057
mrziwang8f86c882024-10-03 12:34:33 -07001058 ctx.Phony("product_config_to_bp", generatedBp)
1059
Cole Faust92ccbe22024-10-03 14:38:37 -07001060 var diffTestFiles []android.Path
1061 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +00001062 diffTestFile := f.createDiffTest(ctx, partitionType)
1063 diffTestFiles = append(diffTestFiles, diffTestFile)
1064 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -07001065 }
1066 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +00001067 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
1068 diffTestFiles = append(diffTestFiles, diffTestFile)
1069 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -07001070 }
1071 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +00001072}
mrziwang8f86c882024-10-03 12:34:33 -07001073
mrziwang8f86c882024-10-03 12:34:33 -07001074func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
mrziwang4b0ca972024-10-17 14:56:19 -07001075 fsProps, fsTypeSupported := generateFsProps(ctx, partitionType)
1076 if !fsTypeSupported {
1077 return ""
mrziwang8f86c882024-10-03 12:34:33 -07001078 }
Spandan Dasc5717162024-11-01 18:33:57 +00001079 if partitionType == "vendor" || partitionType == "odm" {
Spandan Das69464c32024-10-25 20:08:06 +00001080 return "" // TODO: Handle struct props
1081 }
mrziwang8f86c882024-10-03 12:34:33 -07001082
mrziwang4b0ca972024-10-17 14:56:19 -07001083 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
mrziwang2a506cf2024-10-17 15:38:37 -07001084 deps := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).fsDeps[partitionType]
1085 depProps := generateDepStruct(*deps)
mrziwang8f86c882024-10-03 12:34:33 -07001086
mrziwang4b0ca972024-10-17 14:56:19 -07001087 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -07001088 if err != nil {
1089 ctx.ModuleErrorf(err.Error())
1090 }
1091
Jihoon Kang4e5d8de2024-10-19 01:59:58 +00001092 moduleType := "android_filesystem"
1093 if partitionType == "system" {
1094 moduleType = "android_system_image"
1095 }
1096
mrziwang8f86c882024-10-03 12:34:33 -07001097 file := &parser.File{
1098 Defs: []parser.Definition{
1099 &parser.Module{
Jihoon Kang4e5d8de2024-10-19 01:59:58 +00001100 Type: moduleType,
mrziwang8f86c882024-10-03 12:34:33 -07001101 Map: *result,
1102 },
1103 },
1104 }
1105 bytes, err := parser.Print(file)
1106 if err != nil {
1107 ctx.ModuleErrorf(err.Error())
1108 }
1109 return strings.TrimSpace(string(bytes))
1110}