blob: d4301f9d38509cb9265e81a772ba28b258d3329a [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"
Jihoon Kang98047cf2024-10-02 17:13:54 +000021 "strconv"
mrziwang8f86c882024-10-03 12:34:33 -070022 "strings"
mrziwang8f86c882024-10-03 12:34:33 -070023
24 "android/soong/android"
25 "android/soong/filesystem"
Spandan Das5e336422024-11-01 22:31:20 +000026 "android/soong/kernel"
Jihoon Kang98047cf2024-10-02 17:13:54 +000027
Cole Faust92ccbe22024-10-03 14:38:37 -070028 "github.com/google/blueprint"
mrziwang8f86c882024-10-03 12:34:33 -070029 "github.com/google/blueprint/parser"
Jihoon Kang98047cf2024-10-02 17:13:54 +000030 "github.com/google/blueprint/proptools"
31)
32
Cole Faust92ccbe22024-10-03 14:38:37 -070033var pctx = android.NewPackageContext("android/soong/fsgen")
34
Jihoon Kang98047cf2024-10-02 17:13:54 +000035func init() {
36 registerBuildComponents(android.InitRegistrationContext)
37}
38
39func registerBuildComponents(ctx android.RegistrationContext) {
40 ctx.RegisterModuleType("soong_filesystem_creator", filesystemCreatorFactory)
mrziwang8f86c882024-10-03 12:34:33 -070041 ctx.PreDepsMutators(RegisterCollectFileSystemDepsMutators)
42}
43
Cole Faust92ccbe22024-10-03 14:38:37 -070044type filesystemCreatorProps struct {
45 Generated_partition_types []string `blueprint:"mutated"`
46 Unsupported_partition_types []string `blueprint:"mutated"`
Cole Faust3552eb62024-11-06 18:07:26 -080047
48 Vbmeta_module_names []string `blueprint:"mutated"`
49 Vbmeta_partition_names []string `blueprint:"mutated"`
Cole Faustf2a6e8b2024-11-14 10:54:48 -080050
Cole Faust24938e22024-11-18 14:01:58 -080051 Boot_image string `blueprint:"mutated" android:"path_device_first"`
52 Vendor_boot_image string `blueprint:"mutated" android:"path_device_first"`
Jihoon Kang95eb1da2024-11-19 20:55:20 +000053 Init_boot_image string `blueprint:"mutated" android:"path_device_first"`
Cole Faust92ccbe22024-10-03 14:38:37 -070054}
55
Jihoon Kang98047cf2024-10-02 17:13:54 +000056type filesystemCreator struct {
57 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -070058
59 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +000060}
61
62func filesystemCreatorFactory() android.Module {
63 module := &filesystemCreator{}
64
Cole Faust69788792024-10-10 11:00:36 -070065 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -070066 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +000067 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jihoon Kang675d4682024-10-24 23:45:11 +000068 generatedPrebuiltEtcModuleNames := createPrebuiltEtcModules(ctx)
Jihoon Kang04f12c92024-11-12 23:03:08 +000069 avbpubkeyGenerated := createAvbpubkeyModule(ctx)
70 createFsGenState(ctx, generatedPrebuiltEtcModuleNames, avbpubkeyGenerated)
Cole Faust953476f2024-11-14 14:11:29 -080071 module.createAvbKeyFilegroups(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +000072 module.createInternalModules(ctx)
73 })
74
75 return module
76}
77
Cole Faustf2a6e8b2024-11-14 10:54:48 -080078func generatedPartitions(ctx android.LoadHookContext) []string {
Cole Faust24938e22024-11-18 14:01:58 -080079 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faustf2a6e8b2024-11-14 10:54:48 -080080 generatedPartitions := []string{"system"}
81 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
82 generatedPartitions = append(generatedPartitions, "system_ext")
83 }
84 if ctx.DeviceConfig().BuildingVendorImage() && ctx.DeviceConfig().VendorPath() == "vendor" {
85 generatedPartitions = append(generatedPartitions, "vendor")
86 }
87 if ctx.DeviceConfig().BuildingProductImage() && ctx.DeviceConfig().ProductPath() == "product" {
88 generatedPartitions = append(generatedPartitions, "product")
89 }
90 if ctx.DeviceConfig().BuildingOdmImage() && ctx.DeviceConfig().OdmPath() == "odm" {
91 generatedPartitions = append(generatedPartitions, "odm")
92 }
93 if ctx.DeviceConfig().BuildingUserdataImage() && ctx.DeviceConfig().UserdataPath() == "data" {
94 generatedPartitions = append(generatedPartitions, "userdata")
95 }
Cole Faust24938e22024-11-18 14:01:58 -080096 if partitionVars.BuildingSystemDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -080097 generatedPartitions = append(generatedPartitions, "system_dlkm")
98 }
Cole Faust24938e22024-11-18 14:01:58 -080099 if partitionVars.BuildingVendorDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800100 generatedPartitions = append(generatedPartitions, "vendor_dlkm")
101 }
Cole Faust24938e22024-11-18 14:01:58 -0800102 if partitionVars.BuildingOdmDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800103 generatedPartitions = append(generatedPartitions, "odm_dlkm")
104 }
Cole Faust24938e22024-11-18 14:01:58 -0800105 if partitionVars.BuildingRamdiskImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800106 generatedPartitions = append(generatedPartitions, "ramdisk")
107 }
Cole Faust24938e22024-11-18 14:01:58 -0800108 if buildingVendorBootImage(partitionVars) {
109 generatedPartitions = append(generatedPartitions, "vendor_ramdisk")
110 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800111 return generatedPartitions
112}
113
Jihoon Kang98047cf2024-10-02 17:13:54 +0000114func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Cole Faust3552eb62024-11-06 18:07:26 -0800115 soongGeneratedPartitions := generatedPartitions(ctx)
116 finalSoongGeneratedPartitions := make([]string, 0, len(soongGeneratedPartitions))
117 for _, partitionType := range soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -0700118 if f.createPartition(ctx, partitionType) {
119 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
Cole Faust3552eb62024-11-06 18:07:26 -0800120 finalSoongGeneratedPartitions = append(finalSoongGeneratedPartitions, partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700121 } else {
122 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
123 }
124 }
Cole Faust3552eb62024-11-06 18:07:26 -0800125
Cole Faust24938e22024-11-18 14:01:58 -0800126 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
127 if buildingBootImage(partitionVars) {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800128 if createBootImage(ctx) {
129 f.properties.Boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "boot")
130 } else {
131 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "boot")
132 }
133 }
Cole Faust24938e22024-11-18 14:01:58 -0800134 if buildingVendorBootImage(partitionVars) {
135 if createVendorBootImage(ctx) {
136 f.properties.Vendor_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "vendor_boot")
137 } else {
138 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "vendor_boot")
139 }
140 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000141 if buildingInitBootImage(partitionVars) {
142 if createInitBootImage(ctx) {
143 f.properties.Init_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "init_boot")
144 } else {
145 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "init_boot")
146 }
147 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800148
Cole Faust3552eb62024-11-06 18:07:26 -0800149 for _, x := range createVbmetaPartitions(ctx, finalSoongGeneratedPartitions) {
150 f.properties.Vbmeta_module_names = append(f.properties.Vbmeta_module_names, x.moduleName)
151 f.properties.Vbmeta_partition_names = append(f.properties.Vbmeta_partition_names, x.partitionName)
152 }
153
154 ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions = finalSoongGeneratedPartitions
155 f.createDeviceModule(ctx, finalSoongGeneratedPartitions, f.properties.Vbmeta_module_names)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000156}
157
Jihoon Kang0d545b82024-10-11 00:21:57 +0000158func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700159 prefix := "soong"
160 if cfg.HasDeviceProduct() {
161 prefix = cfg.DeviceProduct()
162 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000163 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
164}
165
Jihoon Kang0d545b82024-10-11 00:21:57 +0000166func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
167 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000168}
169
Cole Faust3552eb62024-11-06 18:07:26 -0800170func (f *filesystemCreator) createDeviceModule(
171 ctx android.LoadHookContext,
172 generatedPartitionTypes []string,
173 vbmetaPartitions []string,
174) {
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000175 baseProps := &struct {
176 Name *string
177 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000178 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000179 }
180
Priyanka Advani (xWF)dafaa7f2024-10-21 22:55:13 +0000181 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000182 partitionProps := &filesystem.PartitionNameProperties{}
Cole Faust3552eb62024-11-06 18:07:26 -0800183 if android.InList("system", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000184 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000185 }
Cole Faust3552eb62024-11-06 18:07:26 -0800186 if android.InList("system_ext", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000187 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000188 }
Cole Faust3552eb62024-11-06 18:07:26 -0800189 if android.InList("vendor", generatedPartitionTypes) {
Spandan Dase3b65312024-10-22 00:27:27 +0000190 partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
191 }
Cole Faust3552eb62024-11-06 18:07:26 -0800192 if android.InList("product", generatedPartitionTypes) {
Jihoon Kang6dd13b62024-10-22 23:21:02 +0000193 partitionProps.Product_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product"))
194 }
Cole Faust3552eb62024-11-06 18:07:26 -0800195 if android.InList("odm", generatedPartitionTypes) {
Spandan Dasc5717162024-11-01 18:33:57 +0000196 partitionProps.Odm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm"))
197 }
mrziwang23ba8762024-11-07 16:21:53 -0800198 if android.InList("userdata", f.properties.Generated_partition_types) {
199 partitionProps.Userdata_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "userdata"))
200 }
Cole Faust3552eb62024-11-06 18:07:26 -0800201 partitionProps.Vbmeta_partitions = vbmetaPartitions
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000202
203 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700204}
205
Jihoon Kangd098d442024-11-19 00:03:22 +0000206func partitionSpecificFsProps(fsProps *filesystem.FilesystemProperties, partitionVars android.PartitionVariables, partitionType string) {
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000207 switch partitionType {
208 case "system":
209 fsProps.Build_logtags = proptools.BoolPtr(true)
210 // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
211 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Justin Yuned3dbce2024-11-15 11:57:24 +0900212 // Identical to that of the aosp_shared_system_image
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000213 fsProps.Fsverity.Inputs = []string{
214 "etc/boot-image.prof",
215 "etc/dirty-image-objects",
216 "etc/preloaded-classes",
217 "etc/classpaths/*.pb",
218 "framework/*",
219 "framework/*/*", // framework/{arch}
220 "framework/oat/*/*", // framework/oat/{arch}
221 }
222 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
mrziwang9afc2982024-11-05 14:29:48 -0800223 // TODO(b/377734331): only generate the symlinks if the relevant partitions exist
224 fsProps.Symlinks = []filesystem.SymlinkDefinition{
225 filesystem.SymlinkDefinition{
226 Target: proptools.StringPtr("/product"),
227 Name: proptools.StringPtr("system/product"),
228 },
229 filesystem.SymlinkDefinition{
230 Target: proptools.StringPtr("/system_ext"),
231 Name: proptools.StringPtr("system/system_ext"),
232 },
233 filesystem.SymlinkDefinition{
234 Target: proptools.StringPtr("/vendor"),
235 Name: proptools.StringPtr("system/vendor"),
236 },
237 filesystem.SymlinkDefinition{
238 Target: proptools.StringPtr("/system_dlkm/lib/modules"),
239 Name: proptools.StringPtr("system/lib/modules"),
240 },
241 }
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000242 case "system_ext":
243 fsProps.Fsverity.Inputs = []string{
244 "framework/*",
245 "framework/*/*", // framework/{arch}
246 "framework/oat/*/*", // framework/oat/{arch}
247 }
248 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000249 case "product":
250 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
251 case "vendor":
252 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das69464c32024-10-25 20:08:06 +0000253 fsProps.Symlinks = []filesystem.SymlinkDefinition{
254 filesystem.SymlinkDefinition{
255 Target: proptools.StringPtr("/odm"),
256 Name: proptools.StringPtr("vendor/odm"),
257 },
258 filesystem.SymlinkDefinition{
259 Target: proptools.StringPtr("/vendor_dlkm/lib/modules"),
260 Name: proptools.StringPtr("vendor/lib/modules"),
261 },
262 }
Spandan Dasc5717162024-11-01 18:33:57 +0000263 case "odm":
264 fsProps.Symlinks = []filesystem.SymlinkDefinition{
265 filesystem.SymlinkDefinition{
266 Target: proptools.StringPtr("/odm_dlkm/lib/modules"),
267 Name: proptools.StringPtr("odm/lib/modules"),
268 },
269 }
mrziwang23ba8762024-11-07 16:21:53 -0800270 case "userdata":
271 fsProps.Base_dir = proptools.StringPtr("data")
Jihoon Kangd098d442024-11-19 00:03:22 +0000272 case "ramdisk":
273 // Following the logic in https://cs.android.com/android/platform/superproject/main/+/c3c5063df32748a8806ce5da5dd0db158eab9ad9:build/make/core/Makefile;l=1307
274 fsProps.Dirs = android.NewSimpleConfigurable([]string{
275 "debug_ramdisk",
276 "dev",
277 "metadata",
278 "mnt",
279 "proc",
280 "second_stage_resources",
281 "sys",
282 })
283 if partitionVars.BoardUsesGenericKernelImage {
284 fsProps.Dirs.AppendSimpleValue([]string{
285 "first_stage_ramdisk/debug_ramdisk",
286 "first_stage_ramdisk/dev",
287 "first_stage_ramdisk/metadata",
288 "first_stage_ramdisk/mnt",
289 "first_stage_ramdisk/proc",
290 "first_stage_ramdisk/second_stage_resources",
291 "first_stage_ramdisk/sys",
292 })
293 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000294 }
295}
Spandan Dascbe641a2024-10-14 21:07:34 +0000296
Spandan Das5b493cd2024-11-07 20:55:56 +0000297var (
298 dlkmPartitions = []string{
299 "system_dlkm",
300 "vendor_dlkm",
301 "odm_dlkm",
302 }
303)
304
Cole Faust92ccbe22024-10-03 14:38:37 -0700305// Creates a soong module to build the given partition. Returns false if we can't support building
306// it.
307func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwang4b0ca972024-10-17 14:56:19 -0700308 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
309
310 fsProps, supported := generateFsProps(ctx, partitionType)
311 if !supported {
312 return false
mrziwanga077b942024-10-16 16:00:06 -0700313 }
mrziwanga077b942024-10-16 16:00:06 -0700314
Spandan Das8fe68dc2024-10-29 18:20:11 +0000315 if partitionType == "vendor" || partitionType == "product" {
Spandan Das2047a4c2024-11-11 21:24:58 +0000316 fsProps.Linker_config.Gen_linker_config = proptools.BoolPtr(true)
317 fsProps.Linker_config.Linker_config_srcs = f.createLinkerConfigSourceFilegroups(ctx, partitionType)
Spandan Das312cc412024-10-29 18:20:11 +0000318 }
319
Spandan Das5b493cd2024-11-07 20:55:56 +0000320 if android.InList(partitionType, dlkmPartitions) {
321 f.createPrebuiltKernelModules(ctx, partitionType)
Spandan Das5e336422024-11-01 22:31:20 +0000322 }
323
mrziwang4b0ca972024-10-17 14:56:19 -0700324 var module android.Module
325 if partitionType == "system" {
326 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
327 } else {
328 // Explicitly set the partition.
329 fsProps.Partition_type = proptools.StringPtr(partitionType)
330 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
331 }
332 module.HideFromMake()
Spandan Das168098c2024-10-28 19:44:34 +0000333 if partitionType == "vendor" {
Spandan Das4cd93b52024-11-05 23:27:03 +0000334 f.createVendorBuildProp(ctx)
Spandan Das168098c2024-10-28 19:44:34 +0000335 }
mrziwang4b0ca972024-10-17 14:56:19 -0700336 return true
337}
338
Cole Faust953476f2024-11-14 14:11:29 -0800339// Creates filegroups for the files specified in BOARD_(partition_)AVB_KEY_PATH
340func (f *filesystemCreator) createAvbKeyFilegroups(ctx android.LoadHookContext) {
341 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
342 var files []string
343
344 if len(partitionVars.BoardAvbKeyPath) > 0 {
345 files = append(files, partitionVars.BoardAvbKeyPath)
346 }
347 for _, partition := range android.SortedKeys(partitionVars.PartitionQualifiedVariables) {
348 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partition]
349 if len(specificPartitionVars.BoardAvbKeyPath) > 0 {
350 files = append(files, specificPartitionVars.BoardAvbKeyPath)
351 }
352 }
353
354 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
355 for _, file := range files {
356 if _, ok := fsGenState.avbKeyFilegroups[file]; ok {
357 continue
358 }
359 if file == "external/avb/test/data/testkey_rsa4096.pem" {
360 // There already exists a checked-in filegroup for this commonly-used key, just use that
361 fsGenState.avbKeyFilegroups[file] = "avb_testkey_rsa4096"
362 continue
363 }
364 dir := filepath.Dir(file)
365 base := filepath.Base(file)
366 name := fmt.Sprintf("avb_key_%x", strings.ReplaceAll(file, "/", "_"))
367 ctx.CreateModuleInDirectory(
368 android.FileGroupFactory,
369 dir,
370 &struct {
371 Name *string
372 Srcs []string
373 Visibility []string
374 }{
375 Name: proptools.StringPtr(name),
376 Srcs: []string{base},
377 Visibility: []string{"//visibility:public"},
378 },
379 )
380 fsGenState.avbKeyFilegroups[file] = name
381 }
382}
383
Spandan Das5e336422024-11-01 22:31:20 +0000384// createPrebuiltKernelModules creates `prebuilt_kernel_modules`. These modules will be added to deps of the
Spandan Das7b25a512024-11-06 20:41:26 +0000385// autogenerated *_dlkm filsystem modules. Each _dlkm partition should have a single prebuilt_kernel_modules dependency.
386// This ensures that the depmod artifacts (modules.* installed in /lib/modules/) are generated with a complete view.
Spandan Das5b493cd2024-11-07 20:55:56 +0000387func (f *filesystemCreator) createPrebuiltKernelModules(ctx android.LoadHookContext, partitionType string) {
Spandan Das5e336422024-11-01 22:31:20 +0000388 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
Spandan Das7b25a512024-11-06 20:41:26 +0000389 name := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-kernel-modules", partitionType))
390 props := &struct {
Spandan Das912d26b2024-11-06 19:35:17 +0000391 Name *string
392 Srcs []string
Spandan Das5b493cd2024-11-07 20:55:56 +0000393 System_deps []string
Spandan Das912d26b2024-11-06 19:35:17 +0000394 System_dlkm_specific *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000395 Vendor_dlkm_specific *bool
396 Odm_dlkm_specific *bool
397 Load_by_default *bool
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000398 Blocklist_file *string
Spandan Das7b25a512024-11-06 20:41:26 +0000399 }{
400 Name: proptools.StringPtr(name),
Spandan Das5e336422024-11-01 22:31:20 +0000401 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000402 switch partitionType {
403 case "system_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000404 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules).Strings()
Spandan Das912d26b2024-11-06 19:35:17 +0000405 props.System_dlkm_specific = proptools.BoolPtr(true)
Spandan Das5b493cd2024-11-07 20:55:56 +0000406 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelLoadModules) == 0 {
407 // Create empty modules.load file for system
408 // https://source.corp.google.com/h/googleplex-android/platform/build/+/ef55daac9954896161b26db4f3ef1781b5a5694c:core/Makefile;l=695-700;drc=549fe2a5162548bd8b47867d35f907eb22332023;bpv=1;bpt=0
409 props.Load_by_default = proptools.BoolPtr(false)
410 }
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000411 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelBlocklistFile; blocklistFile != "" {
412 props.Blocklist_file = proptools.StringPtr(blocklistFile)
413 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000414 case "vendor_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000415 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000416 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules) > 0 {
417 props.System_deps = []string{":" + generatedModuleName(ctx.Config(), "system_dlkm-kernel-modules") + "{.modules}"}
418 }
419 props.Vendor_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000420 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelBlocklistFile; blocklistFile != "" {
421 props.Blocklist_file = proptools.StringPtr(blocklistFile)
422 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000423 case "odm_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000424 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000425 props.Odm_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000426 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelBlocklistFile; blocklistFile != "" {
427 props.Blocklist_file = proptools.StringPtr(blocklistFile)
428 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000429 default:
430 ctx.ModuleErrorf("DLKM is not supported for %s\n", partitionType)
Spandan Das912d26b2024-11-06 19:35:17 +0000431 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000432
433 if len(props.Srcs) == 0 {
434 return // do not generate `prebuilt_kernel_modules` if there are no sources
435 }
436
Spandan Das7b25a512024-11-06 20:41:26 +0000437 kernelModule := ctx.CreateModuleInDirectory(
438 kernel.PrebuiltKernelModulesFactory,
439 ".", // create in root directory for now
440 props,
441 )
442 kernelModule.HideFromMake()
443 // Add to deps
444 (*fsGenState.fsDeps[partitionType])[name] = defaultDepCandidateProps(ctx.Config())
Spandan Das5e336422024-11-01 22:31:20 +0000445}
446
Spandan Das4cd93b52024-11-05 23:27:03 +0000447// Create a build_prop and android_info module. This will be used to create /vendor/build.prop
448func (f *filesystemCreator) createVendorBuildProp(ctx android.LoadHookContext) {
449 // Create a android_info for vendor
450 // The board info files might be in a directory outside the root soong namespace, so create
451 // the module in "."
452 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
453 androidInfoProps := &struct {
454 Name *string
455 Board_info_files []string
456 Bootloader_board_name *string
457 }{
458 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "android-info.prop")),
459 Board_info_files: partitionVars.BoardInfoFiles,
460 }
461 if len(androidInfoProps.Board_info_files) == 0 {
462 androidInfoProps.Bootloader_board_name = proptools.StringPtr(partitionVars.BootLoaderBoardName)
463 }
464 androidInfoProp := ctx.CreateModuleInDirectory(
465 android.AndroidInfoFactory,
466 ".",
467 androidInfoProps,
468 )
469 androidInfoProp.HideFromMake()
470 // Create a build prop for vendor
471 vendorBuildProps := &struct {
472 Name *string
473 Vendor *bool
474 Stem *string
475 Product_config *string
476 Android_info *string
477 }{
478 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "vendor-build.prop")),
479 Vendor: proptools.BoolPtr(true),
480 Stem: proptools.StringPtr("build.prop"),
481 Product_config: proptools.StringPtr(":product_config"),
482 Android_info: proptools.StringPtr(":" + androidInfoProp.Name()),
483 }
484 vendorBuildProp := ctx.CreateModule(
485 android.BuildPropFactory,
486 vendorBuildProps,
487 )
488 vendorBuildProp.HideFromMake()
489}
490
Spandan Das8fe68dc2024-10-29 18:20:11 +0000491// createLinkerConfigSourceFilegroups creates filegroup modules to generate linker.config.pb for the following partitions
492// 1. vendor: Using PRODUCT_VENDOR_LINKER_CONFIG_FRAGMENTS (space separated file list)
493// 1. product: Using PRODUCT_PRODUCT_LINKER_CONFIG_FRAGMENTS (space separated file list)
494// It creates a filegroup for each file in the fragment list
Spandan Das312cc412024-10-29 18:20:11 +0000495// The filegroup modules are then added to `linker_config_srcs` of the autogenerated vendor `android_filesystem`.
Spandan Das8fe68dc2024-10-29 18:20:11 +0000496func (f *filesystemCreator) createLinkerConfigSourceFilegroups(ctx android.LoadHookContext, partitionType string) []string {
Spandan Das312cc412024-10-29 18:20:11 +0000497 ret := []string{}
498 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Spandan Das8fe68dc2024-10-29 18:20:11 +0000499 var linkerConfigSrcs []string
500 if partitionType == "vendor" {
501 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.VendorLinkerConfigSrcs)
502 } else if partitionType == "product" {
503 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.ProductLinkerConfigSrcs)
504 } else {
505 ctx.ModuleErrorf("linker.config.pb is only supported for vendor and product partitions. For system partition, use `android_system_image`")
506 }
507
508 if len(linkerConfigSrcs) > 0 {
Spandan Das312cc412024-10-29 18:20:11 +0000509 // Create a filegroup, and add `:<filegroup_name>` to ret.
510 for index, linkerConfigSrc := range linkerConfigSrcs {
511 dir := filepath.Dir(linkerConfigSrc)
512 base := filepath.Base(linkerConfigSrc)
Spandan Das8fe68dc2024-10-29 18:20:11 +0000513 fgName := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-linker-config-src%s", partitionType, strconv.Itoa(index)))
Spandan Das312cc412024-10-29 18:20:11 +0000514 srcs := []string{base}
515 fgProps := &struct {
516 Name *string
517 Srcs proptools.Configurable[[]string]
518 }{
519 Name: proptools.StringPtr(fgName),
520 Srcs: proptools.NewSimpleConfigurable(srcs),
521 }
522 ctx.CreateModuleInDirectory(
523 android.FileGroupFactory,
524 dir,
525 fgProps,
526 )
527 ret = append(ret, ":"+fgName)
528 }
529 }
530 return ret
531}
532
mrziwang4b0ca972024-10-17 14:56:19 -0700533type filesystemBaseProperty struct {
534 Name *string
535 Compile_multilib *string
Cole Faust3552eb62024-11-06 18:07:26 -0800536 Visibility []string
mrziwang4b0ca972024-10-17 14:56:19 -0700537}
538
539func generateBaseProps(namePtr *string) *filesystemBaseProperty {
540 return &filesystemBaseProperty{
541 Name: namePtr,
542 Compile_multilib: proptools.StringPtr("both"),
Cole Faust3552eb62024-11-06 18:07:26 -0800543 // The vbmeta modules are currently in the root directory and depend on the partitions
544 Visibility: []string{"//.", "//build/soong:__subpackages__"},
mrziwang4b0ca972024-10-17 14:56:19 -0700545 }
546}
547
548func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700549 fsProps := &filesystem.FilesystemProperties{}
550
mrziwang4b0ca972024-10-17 14:56:19 -0700551 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust0c4b4152024-11-20 16:42:53 -0800552 var avbInfo avbInfo
Cole Faust76a6e952024-11-07 16:56:45 -0800553 var fsType string
554 if strings.Contains(partitionType, "ramdisk") {
555 fsType = "compressed_cpio"
556 } else {
Cole Faust953476f2024-11-14 14:11:29 -0800557 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Cole Faust76a6e952024-11-07 16:56:45 -0800558 fsType = specificPartitionVars.BoardFileSystemType
Cole Faust0c4b4152024-11-20 16:42:53 -0800559 avbInfo = getAvbInfo(ctx.Config(), partitionType)
Cole Faust953476f2024-11-14 14:11:29 -0800560 if fsType == "" {
561 fsType = "ext4" //default
562 }
Cole Faust76a6e952024-11-07 16:56:45 -0800563 }
Cole Faust76a6e952024-11-07 16:56:45 -0800564
mrziwang4b0ca972024-10-17 14:56:19 -0700565 fsProps.Type = proptools.StringPtr(fsType)
566 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
567 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
568 return nil, false
569 }
570
Cole Faust92ccbe22024-10-03 14:38:37 -0700571 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
572 // and sometimes don't build.
573 fsProps.Unchecked_module = proptools.BoolPtr(true)
574
Jihoon Kang98047cf2024-10-02 17:13:54 +0000575 // BOARD_AVB_ENABLE
Cole Faust0c4b4152024-11-20 16:42:53 -0800576 fsProps.Use_avb = avbInfo.avbEnable
Jihoon Kang98047cf2024-10-02 17:13:54 +0000577 // BOARD_AVB_KEY_PATH
Cole Faust0c4b4152024-11-20 16:42:53 -0800578 fsProps.Avb_private_key = avbInfo.avbkeyFilegroup
Jihoon Kang98047cf2024-10-02 17:13:54 +0000579 // BOARD_AVB_ALGORITHM
Cole Faust0c4b4152024-11-20 16:42:53 -0800580 fsProps.Avb_algorithm = avbInfo.avbAlgorithm
Jihoon Kang98047cf2024-10-02 17:13:54 +0000581 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust0c4b4152024-11-20 16:42:53 -0800582 fsProps.Rollback_index = avbInfo.avbRollbackIndex
Jihoon Kang98047cf2024-10-02 17:13:54 +0000583
Cole Faust92ccbe22024-10-03 14:38:37 -0700584 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000585
Cole Faust68382192024-11-19 10:36:03 -0800586 if !strings.Contains(partitionType, "ramdisk") {
587 fsProps.Base_dir = proptools.StringPtr(partitionType)
588 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000589
Jihoon Kang0d545b82024-10-11 00:21:57 +0000590 fsProps.Is_auto_generated = proptools.BoolPtr(true)
591
Jihoon Kangd098d442024-11-19 00:03:22 +0000592 partitionSpecificFsProps(fsProps, partitionVars, partitionType)
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000593
Jihoon Kang98047cf2024-10-02 17:13:54 +0000594 // system_image properties that are not set:
595 // - filesystemProperties.Avb_hash_algorithm
596 // - filesystemProperties.File_contexts
597 // - filesystemProperties.Dirs
598 // - filesystemProperties.Symlinks
599 // - filesystemProperties.Fake_timestamp
600 // - filesystemProperties.Uuid
601 // - filesystemProperties.Mount_point
602 // - filesystemProperties.Include_make_built_files
603 // - filesystemProperties.Build_logtags
Jihoon Kang98047cf2024-10-02 17:13:54 +0000604 // - systemImageProperties.Linker_config_src
mrziwang4b0ca972024-10-17 14:56:19 -0700605
606 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700607}
608
Cole Faust0c4b4152024-11-20 16:42:53 -0800609type avbInfo struct {
610 avbEnable *bool
611 avbKeyPath *string
612 avbkeyFilegroup *string
613 avbAlgorithm *string
614 avbRollbackIndex *int64
615 avbMode *string
616}
617
618func getAvbInfo(config android.Config, partitionType string) avbInfo {
619 partitionVars := config.ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
620 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
621 var result avbInfo
622 boardAvbEnable := partitionVars.BoardAvbEnable
623 if boardAvbEnable {
624 result.avbEnable = proptools.BoolPtr(true)
625 if specificPartitionVars.BoardAvbKeyPath != "" {
626 result.avbKeyPath = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
627 } else if partitionVars.BoardAvbKeyPath != "" {
628 result.avbKeyPath = proptools.StringPtr(partitionVars.BoardAvbKeyPath)
629 }
630 if specificPartitionVars.BoardAvbAlgorithm != "" {
631 result.avbAlgorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
632 } else if partitionVars.BoardAvbAlgorithm != "" {
633 result.avbAlgorithm = proptools.StringPtr(partitionVars.BoardAvbAlgorithm)
634 }
635 if specificPartitionVars.BoardAvbRollbackIndex != "" {
636 parsed, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64)
637 if err != nil {
638 panic(fmt.Sprintf("Rollback index must be an int, got %s", specificPartitionVars.BoardAvbRollbackIndex))
639 }
640 result.avbRollbackIndex = &parsed
641 } else if partitionVars.BoardAvbRollbackIndex != "" {
642 parsed, err := strconv.ParseInt(partitionVars.BoardAvbRollbackIndex, 10, 64)
643 if err != nil {
644 panic(fmt.Sprintf("Rollback index must be an int, got %s", partitionVars.BoardAvbRollbackIndex))
645 }
646 result.avbRollbackIndex = &parsed
647 }
648 result.avbMode = proptools.StringPtr("make_legacy")
649 }
650 if result.avbKeyPath != nil {
651 fsGenState := config.Get(fsGenStateOnceKey).(*FsGenState)
652 filegroup := fsGenState.avbKeyFilegroups[*result.avbKeyPath]
653 result.avbkeyFilegroup = proptools.StringPtr(":" + filegroup)
654 }
655 return result
656}
657
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800658func (f *filesystemCreator) createFileListDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000659 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700660 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
661 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
662 if !ok {
663 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
664 }
665 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
Jihoon Kang9e866c82024-10-07 22:39:18 +0000666 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700667
668 builder := android.NewRuleBuilder(pctx, ctx)
669 builder.Command().BuiltTool("file_list_diff").
670 Input(makeFileList).
671 Input(filesystemInfo.FileListFile).
Cole Faust56301572024-11-07 15:22:42 -0800672 Text(partitionModuleName)
Cole Faust92ccbe22024-10-03 14:38:37 -0700673 builder.Command().Text("touch").Output(diffTestResultFile)
674 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
675 return diffTestResultFile
676}
677
678func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
679 hasher := sha256.New()
680 hasher.Write([]byte(message))
681 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
682 file := android.PathForModuleOut(ctx, filename)
683 builder := android.NewRuleBuilder(pctx, ctx)
684 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
685 builder.Command().Text("exit 1 #").Output(file)
686 builder.Build("failing command "+filename, "failing command "+filename)
687 return file
688}
689
Cole Faust3552eb62024-11-06 18:07:26 -0800690func createVbmetaDiff(ctx android.ModuleContext, vbmetaModuleName string, vbmetaPartitionName string) android.Path {
691 vbmetaModule := ctx.GetDirectDepWithTag(vbmetaModuleName, generatedVbmetaPartitionDepTag)
692 outputFilesProvider, ok := android.OtherModuleProvider(ctx, vbmetaModule, android.OutputFilesProvider)
693 if !ok {
694 ctx.ModuleErrorf("Expected module %s to provide OutputFiles", vbmetaModule)
695 }
696 if len(outputFilesProvider.DefaultOutputFiles) != 1 {
697 ctx.ModuleErrorf("Expected 1 output file from module %s", vbmetaModule)
698 }
699 soongVbMetaFile := outputFilesProvider.DefaultOutputFiles[0]
700 makeVbmetaFile := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/%s.img", ctx.Config().DeviceName(), vbmetaPartitionName))
701
702 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", vbmetaModuleName))
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800703 createDiffTest(ctx, diffTestResultFile, soongVbMetaFile, makeVbmetaFile)
704 return diffTestResultFile
705}
706
707func createDiffTest(ctx android.ModuleContext, diffTestResultFile android.WritablePath, file1 android.Path, file2 android.Path) {
Cole Faust3552eb62024-11-06 18:07:26 -0800708 builder := android.NewRuleBuilder(pctx, ctx)
709 builder.Command().Text("diff").
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800710 Input(file1).
711 Input(file2)
Cole Faust3552eb62024-11-06 18:07:26 -0800712 builder.Command().Text("touch").Output(diffTestResultFile)
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800713 builder.Build("diff test "+diffTestResultFile.String(), "diff test")
Cole Faust3552eb62024-11-06 18:07:26 -0800714}
715
Cole Faust92ccbe22024-10-03 14:38:37 -0700716type systemImageDepTagType struct {
717 blueprint.BaseDependencyTag
718}
719
720var generatedFilesystemDepTag systemImageDepTagType
Cole Faust3552eb62024-11-06 18:07:26 -0800721var generatedVbmetaPartitionDepTag systemImageDepTagType
Cole Faust92ccbe22024-10-03 14:38:37 -0700722
723func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
724 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000725 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700726 }
Cole Faust3552eb62024-11-06 18:07:26 -0800727 for _, vbmetaModule := range f.properties.Vbmeta_module_names {
728 ctx.AddDependency(ctx.Module(), generatedVbmetaPartitionDepTag, vbmetaModule)
729 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000730}
731
732func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700733 if ctx.ModuleDir() != "build/soong/fsgen" {
734 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
735 }
736 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000737
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000738 var content strings.Builder
739 generatedBp := android.PathForModuleOut(ctx, "soong_generated_product_config.bp")
740 for _, partition := range ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions {
741 content.WriteString(generateBpContent(ctx, partition))
742 content.WriteString("\n")
743 }
744 android.WriteFileRule(ctx, generatedBp, content.String())
745
mrziwang8f86c882024-10-03 12:34:33 -0700746 ctx.Phony("product_config_to_bp", generatedBp)
747
Cole Faust92ccbe22024-10-03 14:38:37 -0700748 var diffTestFiles []android.Path
749 for _, partitionType := range f.properties.Generated_partition_types {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800750 diffTestFile := f.createFileListDiffTest(ctx, partitionType)
Jihoon Kang72f812f2024-10-17 18:46:24 +0000751 diffTestFiles = append(diffTestFiles, diffTestFile)
752 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700753 }
754 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000755 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
756 diffTestFiles = append(diffTestFiles, diffTestFile)
757 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700758 }
Cole Faust3552eb62024-11-06 18:07:26 -0800759 for i, vbmetaModule := range f.properties.Vbmeta_module_names {
760 diffTestFile := createVbmetaDiff(ctx, vbmetaModule, f.properties.Vbmeta_partition_names[i])
761 diffTestFiles = append(diffTestFiles, diffTestFile)
762 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", f.properties.Vbmeta_partition_names[i]), diffTestFile)
763 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800764 if f.properties.Boot_image != "" {
765 diffTestFile := android.PathForModuleOut(ctx, "boot_diff_test.txt")
766 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Boot_image)
767 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/boot.img", ctx.Config().DeviceName()))
768 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
769 diffTestFiles = append(diffTestFiles, diffTestFile)
770 ctx.Phony("soong_generated_boot_filesystem_test", diffTestFile)
771 }
Cole Faust24938e22024-11-18 14:01:58 -0800772 if f.properties.Vendor_boot_image != "" {
773 diffTestFile := android.PathForModuleOut(ctx, "vendor_boot_diff_test.txt")
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000774 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Vendor_boot_image)
Cole Faust24938e22024-11-18 14:01:58 -0800775 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/vendor_boot.img", ctx.Config().DeviceName()))
776 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
777 diffTestFiles = append(diffTestFiles, diffTestFile)
778 ctx.Phony("soong_generated_vendor_boot_filesystem_test", diffTestFile)
779 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000780 if f.properties.Init_boot_image != "" {
781 diffTestFile := android.PathForModuleOut(ctx, "init_boot_diff_test.txt")
782 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Init_boot_image)
783 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/init_boot.img", ctx.Config().DeviceName()))
784 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
785 diffTestFiles = append(diffTestFiles, diffTestFile)
786 ctx.Phony("soong_generated_init_boot_filesystem_test", diffTestFile)
787 }
Cole Faust92ccbe22024-10-03 14:38:37 -0700788 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000789}
mrziwang8f86c882024-10-03 12:34:33 -0700790
mrziwang8f86c882024-10-03 12:34:33 -0700791func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
mrziwang4b0ca972024-10-17 14:56:19 -0700792 fsProps, fsTypeSupported := generateFsProps(ctx, partitionType)
793 if !fsTypeSupported {
794 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700795 }
796
mrziwang4b0ca972024-10-17 14:56:19 -0700797 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000798 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
799 deps := fsGenState.fsDeps[partitionType]
800 highPriorityDeps := fsGenState.generatedPrebuiltEtcModuleNames
801 depProps := generateDepStruct(*deps, highPriorityDeps)
mrziwang8f86c882024-10-03 12:34:33 -0700802
mrziwang4b0ca972024-10-17 14:56:19 -0700803 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -0700804 if err != nil {
Cole Faustae3e1d32024-11-05 13:22:50 -0800805 ctx.ModuleErrorf("%s", err.Error())
806 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700807 }
808
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000809 moduleType := "android_filesystem"
810 if partitionType == "system" {
811 moduleType = "android_system_image"
812 }
813
mrziwang8f86c882024-10-03 12:34:33 -0700814 file := &parser.File{
815 Defs: []parser.Definition{
816 &parser.Module{
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000817 Type: moduleType,
mrziwang8f86c882024-10-03 12:34:33 -0700818 Map: *result,
819 },
820 },
821 }
822 bytes, err := parser.Print(file)
823 if err != nil {
824 ctx.ModuleErrorf(err.Error())
825 }
826 return strings.TrimSpace(string(bytes))
827}