blob: e8b0a4fb7b6d91f50f6a2c3f16365d9cf766635c [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
Jihoon Kang70c1c682024-11-20 23:58:38 +0000127 dtbImg := createDtbImgFilegroup(ctx)
128
Cole Faust24938e22024-11-18 14:01:58 -0800129 if buildingBootImage(partitionVars) {
Jihoon Kang70c1c682024-11-20 23:58:38 +0000130 if createBootImage(ctx, dtbImg) {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800131 f.properties.Boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "boot")
132 } else {
133 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "boot")
134 }
135 }
Cole Faust24938e22024-11-18 14:01:58 -0800136 if buildingVendorBootImage(partitionVars) {
Jihoon Kang70c1c682024-11-20 23:58:38 +0000137 if createVendorBootImage(ctx, dtbImg) {
Cole Faust24938e22024-11-18 14:01:58 -0800138 f.properties.Vendor_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "vendor_boot")
139 } else {
140 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "vendor_boot")
141 }
142 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000143 if buildingInitBootImage(partitionVars) {
144 if createInitBootImage(ctx) {
145 f.properties.Init_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "init_boot")
146 } else {
147 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "init_boot")
148 }
149 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800150
Cole Faust3552eb62024-11-06 18:07:26 -0800151 for _, x := range createVbmetaPartitions(ctx, finalSoongGeneratedPartitions) {
152 f.properties.Vbmeta_module_names = append(f.properties.Vbmeta_module_names, x.moduleName)
153 f.properties.Vbmeta_partition_names = append(f.properties.Vbmeta_partition_names, x.partitionName)
154 }
155
156 ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions = finalSoongGeneratedPartitions
157 f.createDeviceModule(ctx, finalSoongGeneratedPartitions, f.properties.Vbmeta_module_names)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000158}
159
Jihoon Kang0d545b82024-10-11 00:21:57 +0000160func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700161 prefix := "soong"
162 if cfg.HasDeviceProduct() {
163 prefix = cfg.DeviceProduct()
164 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000165 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
166}
167
Jihoon Kang0d545b82024-10-11 00:21:57 +0000168func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
169 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000170}
171
Cole Faust3552eb62024-11-06 18:07:26 -0800172func (f *filesystemCreator) createDeviceModule(
173 ctx android.LoadHookContext,
174 generatedPartitionTypes []string,
175 vbmetaPartitions []string,
176) {
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000177 baseProps := &struct {
178 Name *string
179 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000180 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000181 }
182
Priyanka Advani (xWF)dafaa7f2024-10-21 22:55:13 +0000183 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000184 partitionProps := &filesystem.PartitionNameProperties{}
Cole Faust3552eb62024-11-06 18:07:26 -0800185 if android.InList("system", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000186 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000187 }
Cole Faust3552eb62024-11-06 18:07:26 -0800188 if android.InList("system_ext", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000189 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000190 }
Cole Faust3552eb62024-11-06 18:07:26 -0800191 if android.InList("vendor", generatedPartitionTypes) {
Spandan Dase3b65312024-10-22 00:27:27 +0000192 partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
193 }
Cole Faust3552eb62024-11-06 18:07:26 -0800194 if android.InList("product", generatedPartitionTypes) {
Jihoon Kang6dd13b62024-10-22 23:21:02 +0000195 partitionProps.Product_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product"))
196 }
Cole Faust3552eb62024-11-06 18:07:26 -0800197 if android.InList("odm", generatedPartitionTypes) {
Spandan Dasc5717162024-11-01 18:33:57 +0000198 partitionProps.Odm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm"))
199 }
mrziwang23ba8762024-11-07 16:21:53 -0800200 if android.InList("userdata", f.properties.Generated_partition_types) {
201 partitionProps.Userdata_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "userdata"))
202 }
Cole Faust3552eb62024-11-06 18:07:26 -0800203 partitionProps.Vbmeta_partitions = vbmetaPartitions
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000204
205 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700206}
207
Jihoon Kangd098d442024-11-19 00:03:22 +0000208func partitionSpecificFsProps(fsProps *filesystem.FilesystemProperties, partitionVars android.PartitionVariables, partitionType string) {
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000209 switch partitionType {
210 case "system":
211 fsProps.Build_logtags = proptools.BoolPtr(true)
212 // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
213 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Justin Yuned3dbce2024-11-15 11:57:24 +0900214 // Identical to that of the aosp_shared_system_image
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000215 fsProps.Fsverity.Inputs = []string{
216 "etc/boot-image.prof",
217 "etc/dirty-image-objects",
218 "etc/preloaded-classes",
219 "etc/classpaths/*.pb",
220 "framework/*",
221 "framework/*/*", // framework/{arch}
222 "framework/oat/*/*", // framework/oat/{arch}
223 }
224 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
mrziwang9afc2982024-11-05 14:29:48 -0800225 // TODO(b/377734331): only generate the symlinks if the relevant partitions exist
226 fsProps.Symlinks = []filesystem.SymlinkDefinition{
227 filesystem.SymlinkDefinition{
228 Target: proptools.StringPtr("/product"),
229 Name: proptools.StringPtr("system/product"),
230 },
231 filesystem.SymlinkDefinition{
232 Target: proptools.StringPtr("/system_ext"),
233 Name: proptools.StringPtr("system/system_ext"),
234 },
235 filesystem.SymlinkDefinition{
236 Target: proptools.StringPtr("/vendor"),
237 Name: proptools.StringPtr("system/vendor"),
238 },
239 filesystem.SymlinkDefinition{
240 Target: proptools.StringPtr("/system_dlkm/lib/modules"),
241 Name: proptools.StringPtr("system/lib/modules"),
242 },
243 }
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000244 case "system_ext":
245 fsProps.Fsverity.Inputs = []string{
246 "framework/*",
247 "framework/*/*", // framework/{arch}
248 "framework/oat/*/*", // framework/oat/{arch}
249 }
250 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000251 case "product":
252 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
253 case "vendor":
254 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das69464c32024-10-25 20:08:06 +0000255 fsProps.Symlinks = []filesystem.SymlinkDefinition{
256 filesystem.SymlinkDefinition{
257 Target: proptools.StringPtr("/odm"),
258 Name: proptools.StringPtr("vendor/odm"),
259 },
260 filesystem.SymlinkDefinition{
261 Target: proptools.StringPtr("/vendor_dlkm/lib/modules"),
262 Name: proptools.StringPtr("vendor/lib/modules"),
263 },
264 }
Spandan Dasc5717162024-11-01 18:33:57 +0000265 case "odm":
266 fsProps.Symlinks = []filesystem.SymlinkDefinition{
267 filesystem.SymlinkDefinition{
268 Target: proptools.StringPtr("/odm_dlkm/lib/modules"),
269 Name: proptools.StringPtr("odm/lib/modules"),
270 },
271 }
mrziwang23ba8762024-11-07 16:21:53 -0800272 case "userdata":
273 fsProps.Base_dir = proptools.StringPtr("data")
Jihoon Kangd098d442024-11-19 00:03:22 +0000274 case "ramdisk":
275 // Following the logic in https://cs.android.com/android/platform/superproject/main/+/c3c5063df32748a8806ce5da5dd0db158eab9ad9:build/make/core/Makefile;l=1307
276 fsProps.Dirs = android.NewSimpleConfigurable([]string{
277 "debug_ramdisk",
278 "dev",
279 "metadata",
280 "mnt",
281 "proc",
282 "second_stage_resources",
283 "sys",
284 })
285 if partitionVars.BoardUsesGenericKernelImage {
286 fsProps.Dirs.AppendSimpleValue([]string{
287 "first_stage_ramdisk/debug_ramdisk",
288 "first_stage_ramdisk/dev",
289 "first_stage_ramdisk/metadata",
290 "first_stage_ramdisk/mnt",
291 "first_stage_ramdisk/proc",
292 "first_stage_ramdisk/second_stage_resources",
293 "first_stage_ramdisk/sys",
294 })
295 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000296 }
297}
Spandan Dascbe641a2024-10-14 21:07:34 +0000298
Spandan Das5b493cd2024-11-07 20:55:56 +0000299var (
300 dlkmPartitions = []string{
301 "system_dlkm",
302 "vendor_dlkm",
303 "odm_dlkm",
304 }
305)
306
Cole Faust92ccbe22024-10-03 14:38:37 -0700307// Creates a soong module to build the given partition. Returns false if we can't support building
308// it.
309func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwang4b0ca972024-10-17 14:56:19 -0700310 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
311
312 fsProps, supported := generateFsProps(ctx, partitionType)
313 if !supported {
314 return false
mrziwanga077b942024-10-16 16:00:06 -0700315 }
mrziwanga077b942024-10-16 16:00:06 -0700316
Cole Faust7db05752024-11-21 13:30:41 -0800317 if partitionType == "vendor" || partitionType == "product" || partitionType == "system" {
Spandan Das2047a4c2024-11-11 21:24:58 +0000318 fsProps.Linker_config.Gen_linker_config = proptools.BoolPtr(true)
Cole Faust7db05752024-11-21 13:30:41 -0800319 if partitionType != "system" {
320 fsProps.Linker_config.Linker_config_srcs = f.createLinkerConfigSourceFilegroups(ctx, partitionType)
321 }
Spandan Das312cc412024-10-29 18:20:11 +0000322 }
323
Spandan Das5b493cd2024-11-07 20:55:56 +0000324 if android.InList(partitionType, dlkmPartitions) {
325 f.createPrebuiltKernelModules(ctx, partitionType)
Spandan Das5e336422024-11-01 22:31:20 +0000326 }
327
mrziwang4b0ca972024-10-17 14:56:19 -0700328 var module android.Module
329 if partitionType == "system" {
330 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
331 } else {
332 // Explicitly set the partition.
333 fsProps.Partition_type = proptools.StringPtr(partitionType)
334 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
335 }
336 module.HideFromMake()
Spandan Das168098c2024-10-28 19:44:34 +0000337 if partitionType == "vendor" {
Spandan Das4cd93b52024-11-05 23:27:03 +0000338 f.createVendorBuildProp(ctx)
Spandan Das168098c2024-10-28 19:44:34 +0000339 }
mrziwang4b0ca972024-10-17 14:56:19 -0700340 return true
341}
342
Cole Faust953476f2024-11-14 14:11:29 -0800343// Creates filegroups for the files specified in BOARD_(partition_)AVB_KEY_PATH
344func (f *filesystemCreator) createAvbKeyFilegroups(ctx android.LoadHookContext) {
345 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
346 var files []string
347
348 if len(partitionVars.BoardAvbKeyPath) > 0 {
349 files = append(files, partitionVars.BoardAvbKeyPath)
350 }
351 for _, partition := range android.SortedKeys(partitionVars.PartitionQualifiedVariables) {
352 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partition]
353 if len(specificPartitionVars.BoardAvbKeyPath) > 0 {
354 files = append(files, specificPartitionVars.BoardAvbKeyPath)
355 }
356 }
357
358 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
359 for _, file := range files {
360 if _, ok := fsGenState.avbKeyFilegroups[file]; ok {
361 continue
362 }
363 if file == "external/avb/test/data/testkey_rsa4096.pem" {
364 // There already exists a checked-in filegroup for this commonly-used key, just use that
365 fsGenState.avbKeyFilegroups[file] = "avb_testkey_rsa4096"
366 continue
367 }
368 dir := filepath.Dir(file)
369 base := filepath.Base(file)
370 name := fmt.Sprintf("avb_key_%x", strings.ReplaceAll(file, "/", "_"))
371 ctx.CreateModuleInDirectory(
372 android.FileGroupFactory,
373 dir,
374 &struct {
375 Name *string
376 Srcs []string
377 Visibility []string
378 }{
379 Name: proptools.StringPtr(name),
380 Srcs: []string{base},
381 Visibility: []string{"//visibility:public"},
382 },
383 )
384 fsGenState.avbKeyFilegroups[file] = name
385 }
386}
387
Spandan Das5e336422024-11-01 22:31:20 +0000388// createPrebuiltKernelModules creates `prebuilt_kernel_modules`. These modules will be added to deps of the
Spandan Das7b25a512024-11-06 20:41:26 +0000389// autogenerated *_dlkm filsystem modules. Each _dlkm partition should have a single prebuilt_kernel_modules dependency.
390// This ensures that the depmod artifacts (modules.* installed in /lib/modules/) are generated with a complete view.
Spandan Das5b493cd2024-11-07 20:55:56 +0000391func (f *filesystemCreator) createPrebuiltKernelModules(ctx android.LoadHookContext, partitionType string) {
Spandan Das5e336422024-11-01 22:31:20 +0000392 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
Spandan Das7b25a512024-11-06 20:41:26 +0000393 name := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-kernel-modules", partitionType))
394 props := &struct {
Spandan Das912d26b2024-11-06 19:35:17 +0000395 Name *string
396 Srcs []string
Spandan Das5b493cd2024-11-07 20:55:56 +0000397 System_deps []string
Spandan Das912d26b2024-11-06 19:35:17 +0000398 System_dlkm_specific *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000399 Vendor_dlkm_specific *bool
400 Odm_dlkm_specific *bool
401 Load_by_default *bool
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000402 Blocklist_file *string
Spandan Das7b25a512024-11-06 20:41:26 +0000403 }{
404 Name: proptools.StringPtr(name),
Spandan Das5e336422024-11-01 22:31:20 +0000405 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000406 switch partitionType {
407 case "system_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000408 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules).Strings()
Spandan Das912d26b2024-11-06 19:35:17 +0000409 props.System_dlkm_specific = proptools.BoolPtr(true)
Spandan Das5b493cd2024-11-07 20:55:56 +0000410 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelLoadModules) == 0 {
411 // Create empty modules.load file for system
412 // https://source.corp.google.com/h/googleplex-android/platform/build/+/ef55daac9954896161b26db4f3ef1781b5a5694c:core/Makefile;l=695-700;drc=549fe2a5162548bd8b47867d35f907eb22332023;bpv=1;bpt=0
413 props.Load_by_default = proptools.BoolPtr(false)
414 }
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000415 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelBlocklistFile; blocklistFile != "" {
416 props.Blocklist_file = proptools.StringPtr(blocklistFile)
417 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000418 case "vendor_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000419 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000420 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules) > 0 {
421 props.System_deps = []string{":" + generatedModuleName(ctx.Config(), "system_dlkm-kernel-modules") + "{.modules}"}
422 }
423 props.Vendor_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000424 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelBlocklistFile; blocklistFile != "" {
425 props.Blocklist_file = proptools.StringPtr(blocklistFile)
426 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000427 case "odm_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000428 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000429 props.Odm_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000430 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelBlocklistFile; blocklistFile != "" {
431 props.Blocklist_file = proptools.StringPtr(blocklistFile)
432 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000433 default:
434 ctx.ModuleErrorf("DLKM is not supported for %s\n", partitionType)
Spandan Das912d26b2024-11-06 19:35:17 +0000435 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000436
437 if len(props.Srcs) == 0 {
438 return // do not generate `prebuilt_kernel_modules` if there are no sources
439 }
440
Spandan Das7b25a512024-11-06 20:41:26 +0000441 kernelModule := ctx.CreateModuleInDirectory(
442 kernel.PrebuiltKernelModulesFactory,
443 ".", // create in root directory for now
444 props,
445 )
446 kernelModule.HideFromMake()
447 // Add to deps
448 (*fsGenState.fsDeps[partitionType])[name] = defaultDepCandidateProps(ctx.Config())
Spandan Das5e336422024-11-01 22:31:20 +0000449}
450
Spandan Das4cd93b52024-11-05 23:27:03 +0000451// Create a build_prop and android_info module. This will be used to create /vendor/build.prop
452func (f *filesystemCreator) createVendorBuildProp(ctx android.LoadHookContext) {
453 // Create a android_info for vendor
454 // The board info files might be in a directory outside the root soong namespace, so create
455 // the module in "."
456 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
457 androidInfoProps := &struct {
458 Name *string
459 Board_info_files []string
460 Bootloader_board_name *string
461 }{
462 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "android-info.prop")),
463 Board_info_files: partitionVars.BoardInfoFiles,
464 }
465 if len(androidInfoProps.Board_info_files) == 0 {
466 androidInfoProps.Bootloader_board_name = proptools.StringPtr(partitionVars.BootLoaderBoardName)
467 }
468 androidInfoProp := ctx.CreateModuleInDirectory(
469 android.AndroidInfoFactory,
470 ".",
471 androidInfoProps,
472 )
473 androidInfoProp.HideFromMake()
474 // Create a build prop for vendor
475 vendorBuildProps := &struct {
476 Name *string
477 Vendor *bool
478 Stem *string
479 Product_config *string
480 Android_info *string
481 }{
482 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "vendor-build.prop")),
483 Vendor: proptools.BoolPtr(true),
484 Stem: proptools.StringPtr("build.prop"),
485 Product_config: proptools.StringPtr(":product_config"),
486 Android_info: proptools.StringPtr(":" + androidInfoProp.Name()),
487 }
488 vendorBuildProp := ctx.CreateModule(
489 android.BuildPropFactory,
490 vendorBuildProps,
491 )
492 vendorBuildProp.HideFromMake()
493}
494
Spandan Das8fe68dc2024-10-29 18:20:11 +0000495// createLinkerConfigSourceFilegroups creates filegroup modules to generate linker.config.pb for the following partitions
496// 1. vendor: Using PRODUCT_VENDOR_LINKER_CONFIG_FRAGMENTS (space separated file list)
497// 1. product: Using PRODUCT_PRODUCT_LINKER_CONFIG_FRAGMENTS (space separated file list)
498// It creates a filegroup for each file in the fragment list
Spandan Das312cc412024-10-29 18:20:11 +0000499// The filegroup modules are then added to `linker_config_srcs` of the autogenerated vendor `android_filesystem`.
Spandan Das8fe68dc2024-10-29 18:20:11 +0000500func (f *filesystemCreator) createLinkerConfigSourceFilegroups(ctx android.LoadHookContext, partitionType string) []string {
Spandan Das312cc412024-10-29 18:20:11 +0000501 ret := []string{}
502 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Spandan Das8fe68dc2024-10-29 18:20:11 +0000503 var linkerConfigSrcs []string
504 if partitionType == "vendor" {
505 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.VendorLinkerConfigSrcs)
506 } else if partitionType == "product" {
507 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.ProductLinkerConfigSrcs)
508 } else {
509 ctx.ModuleErrorf("linker.config.pb is only supported for vendor and product partitions. For system partition, use `android_system_image`")
510 }
511
512 if len(linkerConfigSrcs) > 0 {
Spandan Das312cc412024-10-29 18:20:11 +0000513 // Create a filegroup, and add `:<filegroup_name>` to ret.
514 for index, linkerConfigSrc := range linkerConfigSrcs {
515 dir := filepath.Dir(linkerConfigSrc)
516 base := filepath.Base(linkerConfigSrc)
Spandan Das8fe68dc2024-10-29 18:20:11 +0000517 fgName := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-linker-config-src%s", partitionType, strconv.Itoa(index)))
Spandan Das312cc412024-10-29 18:20:11 +0000518 srcs := []string{base}
519 fgProps := &struct {
520 Name *string
521 Srcs proptools.Configurable[[]string]
522 }{
523 Name: proptools.StringPtr(fgName),
524 Srcs: proptools.NewSimpleConfigurable(srcs),
525 }
526 ctx.CreateModuleInDirectory(
527 android.FileGroupFactory,
528 dir,
529 fgProps,
530 )
531 ret = append(ret, ":"+fgName)
532 }
533 }
534 return ret
535}
536
mrziwang4b0ca972024-10-17 14:56:19 -0700537type filesystemBaseProperty struct {
538 Name *string
539 Compile_multilib *string
Cole Faust3552eb62024-11-06 18:07:26 -0800540 Visibility []string
mrziwang4b0ca972024-10-17 14:56:19 -0700541}
542
543func generateBaseProps(namePtr *string) *filesystemBaseProperty {
544 return &filesystemBaseProperty{
545 Name: namePtr,
546 Compile_multilib: proptools.StringPtr("both"),
Cole Faust3552eb62024-11-06 18:07:26 -0800547 // The vbmeta modules are currently in the root directory and depend on the partitions
548 Visibility: []string{"//.", "//build/soong:__subpackages__"},
mrziwang4b0ca972024-10-17 14:56:19 -0700549 }
550}
551
552func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700553 fsProps := &filesystem.FilesystemProperties{}
554
mrziwang4b0ca972024-10-17 14:56:19 -0700555 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust0c4b4152024-11-20 16:42:53 -0800556 var avbInfo avbInfo
Cole Faust76a6e952024-11-07 16:56:45 -0800557 var fsType string
558 if strings.Contains(partitionType, "ramdisk") {
559 fsType = "compressed_cpio"
560 } else {
Cole Faust953476f2024-11-14 14:11:29 -0800561 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Cole Faust76a6e952024-11-07 16:56:45 -0800562 fsType = specificPartitionVars.BoardFileSystemType
Cole Faust0c4b4152024-11-20 16:42:53 -0800563 avbInfo = getAvbInfo(ctx.Config(), partitionType)
Cole Faust953476f2024-11-14 14:11:29 -0800564 if fsType == "" {
565 fsType = "ext4" //default
566 }
Cole Faust76a6e952024-11-07 16:56:45 -0800567 }
Cole Faust76a6e952024-11-07 16:56:45 -0800568
mrziwang4b0ca972024-10-17 14:56:19 -0700569 fsProps.Type = proptools.StringPtr(fsType)
570 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
571 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
572 return nil, false
573 }
574
Cole Faust92ccbe22024-10-03 14:38:37 -0700575 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
576 // and sometimes don't build.
577 fsProps.Unchecked_module = proptools.BoolPtr(true)
578
Jihoon Kang98047cf2024-10-02 17:13:54 +0000579 // BOARD_AVB_ENABLE
Cole Faust0c4b4152024-11-20 16:42:53 -0800580 fsProps.Use_avb = avbInfo.avbEnable
Jihoon Kang98047cf2024-10-02 17:13:54 +0000581 // BOARD_AVB_KEY_PATH
Cole Faust0c4b4152024-11-20 16:42:53 -0800582 fsProps.Avb_private_key = avbInfo.avbkeyFilegroup
Jihoon Kang98047cf2024-10-02 17:13:54 +0000583 // BOARD_AVB_ALGORITHM
Cole Faust0c4b4152024-11-20 16:42:53 -0800584 fsProps.Avb_algorithm = avbInfo.avbAlgorithm
Jihoon Kang98047cf2024-10-02 17:13:54 +0000585 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust0c4b4152024-11-20 16:42:53 -0800586 fsProps.Rollback_index = avbInfo.avbRollbackIndex
Jihoon Kang98047cf2024-10-02 17:13:54 +0000587
Cole Faust92ccbe22024-10-03 14:38:37 -0700588 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000589
Cole Faust68382192024-11-19 10:36:03 -0800590 if !strings.Contains(partitionType, "ramdisk") {
591 fsProps.Base_dir = proptools.StringPtr(partitionType)
592 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000593
Jihoon Kang0d545b82024-10-11 00:21:57 +0000594 fsProps.Is_auto_generated = proptools.BoolPtr(true)
595
Jihoon Kangd098d442024-11-19 00:03:22 +0000596 partitionSpecificFsProps(fsProps, partitionVars, partitionType)
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000597
Jihoon Kang98047cf2024-10-02 17:13:54 +0000598 // system_image properties that are not set:
599 // - filesystemProperties.Avb_hash_algorithm
600 // - filesystemProperties.File_contexts
601 // - filesystemProperties.Dirs
602 // - filesystemProperties.Symlinks
603 // - filesystemProperties.Fake_timestamp
604 // - filesystemProperties.Uuid
605 // - filesystemProperties.Mount_point
606 // - filesystemProperties.Include_make_built_files
607 // - filesystemProperties.Build_logtags
Jihoon Kang98047cf2024-10-02 17:13:54 +0000608 // - systemImageProperties.Linker_config_src
mrziwang4b0ca972024-10-17 14:56:19 -0700609
610 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700611}
612
Cole Faust0c4b4152024-11-20 16:42:53 -0800613type avbInfo struct {
614 avbEnable *bool
615 avbKeyPath *string
616 avbkeyFilegroup *string
617 avbAlgorithm *string
618 avbRollbackIndex *int64
619 avbMode *string
620}
621
622func getAvbInfo(config android.Config, partitionType string) avbInfo {
623 partitionVars := config.ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
624 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
625 var result avbInfo
626 boardAvbEnable := partitionVars.BoardAvbEnable
627 if boardAvbEnable {
628 result.avbEnable = proptools.BoolPtr(true)
629 if specificPartitionVars.BoardAvbKeyPath != "" {
630 result.avbKeyPath = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
631 } else if partitionVars.BoardAvbKeyPath != "" {
632 result.avbKeyPath = proptools.StringPtr(partitionVars.BoardAvbKeyPath)
633 }
634 if specificPartitionVars.BoardAvbAlgorithm != "" {
635 result.avbAlgorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
636 } else if partitionVars.BoardAvbAlgorithm != "" {
637 result.avbAlgorithm = proptools.StringPtr(partitionVars.BoardAvbAlgorithm)
638 }
639 if specificPartitionVars.BoardAvbRollbackIndex != "" {
640 parsed, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64)
641 if err != nil {
642 panic(fmt.Sprintf("Rollback index must be an int, got %s", specificPartitionVars.BoardAvbRollbackIndex))
643 }
644 result.avbRollbackIndex = &parsed
645 } else if partitionVars.BoardAvbRollbackIndex != "" {
646 parsed, err := strconv.ParseInt(partitionVars.BoardAvbRollbackIndex, 10, 64)
647 if err != nil {
648 panic(fmt.Sprintf("Rollback index must be an int, got %s", partitionVars.BoardAvbRollbackIndex))
649 }
650 result.avbRollbackIndex = &parsed
651 }
652 result.avbMode = proptools.StringPtr("make_legacy")
653 }
654 if result.avbKeyPath != nil {
655 fsGenState := config.Get(fsGenStateOnceKey).(*FsGenState)
656 filegroup := fsGenState.avbKeyFilegroups[*result.avbKeyPath]
657 result.avbkeyFilegroup = proptools.StringPtr(":" + filegroup)
658 }
659 return result
660}
661
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800662func (f *filesystemCreator) createFileListDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000663 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700664 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
665 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
666 if !ok {
667 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
668 }
669 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 +0000670 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700671
672 builder := android.NewRuleBuilder(pctx, ctx)
673 builder.Command().BuiltTool("file_list_diff").
674 Input(makeFileList).
675 Input(filesystemInfo.FileListFile).
Cole Faust56301572024-11-07 15:22:42 -0800676 Text(partitionModuleName)
Cole Faust92ccbe22024-10-03 14:38:37 -0700677 builder.Command().Text("touch").Output(diffTestResultFile)
678 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
679 return diffTestResultFile
680}
681
682func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
683 hasher := sha256.New()
684 hasher.Write([]byte(message))
685 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
686 file := android.PathForModuleOut(ctx, filename)
687 builder := android.NewRuleBuilder(pctx, ctx)
688 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
689 builder.Command().Text("exit 1 #").Output(file)
690 builder.Build("failing command "+filename, "failing command "+filename)
691 return file
692}
693
Cole Faust3552eb62024-11-06 18:07:26 -0800694func createVbmetaDiff(ctx android.ModuleContext, vbmetaModuleName string, vbmetaPartitionName string) android.Path {
695 vbmetaModule := ctx.GetDirectDepWithTag(vbmetaModuleName, generatedVbmetaPartitionDepTag)
696 outputFilesProvider, ok := android.OtherModuleProvider(ctx, vbmetaModule, android.OutputFilesProvider)
697 if !ok {
698 ctx.ModuleErrorf("Expected module %s to provide OutputFiles", vbmetaModule)
699 }
700 if len(outputFilesProvider.DefaultOutputFiles) != 1 {
701 ctx.ModuleErrorf("Expected 1 output file from module %s", vbmetaModule)
702 }
703 soongVbMetaFile := outputFilesProvider.DefaultOutputFiles[0]
704 makeVbmetaFile := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/%s.img", ctx.Config().DeviceName(), vbmetaPartitionName))
705
706 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", vbmetaModuleName))
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800707 createDiffTest(ctx, diffTestResultFile, soongVbMetaFile, makeVbmetaFile)
708 return diffTestResultFile
709}
710
711func createDiffTest(ctx android.ModuleContext, diffTestResultFile android.WritablePath, file1 android.Path, file2 android.Path) {
Cole Faust3552eb62024-11-06 18:07:26 -0800712 builder := android.NewRuleBuilder(pctx, ctx)
713 builder.Command().Text("diff").
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800714 Input(file1).
715 Input(file2)
Cole Faust3552eb62024-11-06 18:07:26 -0800716 builder.Command().Text("touch").Output(diffTestResultFile)
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800717 builder.Build("diff test "+diffTestResultFile.String(), "diff test")
Cole Faust3552eb62024-11-06 18:07:26 -0800718}
719
Cole Faust92ccbe22024-10-03 14:38:37 -0700720type systemImageDepTagType struct {
721 blueprint.BaseDependencyTag
722}
723
724var generatedFilesystemDepTag systemImageDepTagType
Cole Faust3552eb62024-11-06 18:07:26 -0800725var generatedVbmetaPartitionDepTag systemImageDepTagType
Cole Faust92ccbe22024-10-03 14:38:37 -0700726
727func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
728 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000729 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700730 }
Cole Faust3552eb62024-11-06 18:07:26 -0800731 for _, vbmetaModule := range f.properties.Vbmeta_module_names {
732 ctx.AddDependency(ctx.Module(), generatedVbmetaPartitionDepTag, vbmetaModule)
733 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000734}
735
736func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700737 if ctx.ModuleDir() != "build/soong/fsgen" {
738 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
739 }
740 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000741
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000742 var content strings.Builder
743 generatedBp := android.PathForModuleOut(ctx, "soong_generated_product_config.bp")
744 for _, partition := range ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions {
745 content.WriteString(generateBpContent(ctx, partition))
746 content.WriteString("\n")
747 }
748 android.WriteFileRule(ctx, generatedBp, content.String())
749
mrziwang8f86c882024-10-03 12:34:33 -0700750 ctx.Phony("product_config_to_bp", generatedBp)
751
Cole Faust92ccbe22024-10-03 14:38:37 -0700752 var diffTestFiles []android.Path
753 for _, partitionType := range f.properties.Generated_partition_types {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800754 diffTestFile := f.createFileListDiffTest(ctx, partitionType)
Jihoon Kang72f812f2024-10-17 18:46:24 +0000755 diffTestFiles = append(diffTestFiles, diffTestFile)
756 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700757 }
758 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000759 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
760 diffTestFiles = append(diffTestFiles, diffTestFile)
761 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700762 }
Cole Faust3552eb62024-11-06 18:07:26 -0800763 for i, vbmetaModule := range f.properties.Vbmeta_module_names {
764 diffTestFile := createVbmetaDiff(ctx, vbmetaModule, f.properties.Vbmeta_partition_names[i])
765 diffTestFiles = append(diffTestFiles, diffTestFile)
766 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", f.properties.Vbmeta_partition_names[i]), diffTestFile)
767 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800768 if f.properties.Boot_image != "" {
769 diffTestFile := android.PathForModuleOut(ctx, "boot_diff_test.txt")
770 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Boot_image)
771 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/boot.img", ctx.Config().DeviceName()))
772 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
773 diffTestFiles = append(diffTestFiles, diffTestFile)
774 ctx.Phony("soong_generated_boot_filesystem_test", diffTestFile)
775 }
Cole Faust24938e22024-11-18 14:01:58 -0800776 if f.properties.Vendor_boot_image != "" {
777 diffTestFile := android.PathForModuleOut(ctx, "vendor_boot_diff_test.txt")
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000778 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Vendor_boot_image)
Cole Faust24938e22024-11-18 14:01:58 -0800779 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/vendor_boot.img", ctx.Config().DeviceName()))
780 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
781 diffTestFiles = append(diffTestFiles, diffTestFile)
782 ctx.Phony("soong_generated_vendor_boot_filesystem_test", diffTestFile)
783 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000784 if f.properties.Init_boot_image != "" {
785 diffTestFile := android.PathForModuleOut(ctx, "init_boot_diff_test.txt")
786 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Init_boot_image)
787 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/init_boot.img", ctx.Config().DeviceName()))
788 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
789 diffTestFiles = append(diffTestFiles, diffTestFile)
790 ctx.Phony("soong_generated_init_boot_filesystem_test", diffTestFile)
791 }
Cole Faust92ccbe22024-10-03 14:38:37 -0700792 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000793}
mrziwang8f86c882024-10-03 12:34:33 -0700794
mrziwang8f86c882024-10-03 12:34:33 -0700795func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
mrziwang4b0ca972024-10-17 14:56:19 -0700796 fsProps, fsTypeSupported := generateFsProps(ctx, partitionType)
797 if !fsTypeSupported {
798 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700799 }
800
mrziwang4b0ca972024-10-17 14:56:19 -0700801 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000802 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
803 deps := fsGenState.fsDeps[partitionType]
804 highPriorityDeps := fsGenState.generatedPrebuiltEtcModuleNames
805 depProps := generateDepStruct(*deps, highPriorityDeps)
mrziwang8f86c882024-10-03 12:34:33 -0700806
mrziwang4b0ca972024-10-17 14:56:19 -0700807 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -0700808 if err != nil {
Cole Faustae3e1d32024-11-05 13:22:50 -0800809 ctx.ModuleErrorf("%s", err.Error())
810 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700811 }
812
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000813 moduleType := "android_filesystem"
814 if partitionType == "system" {
815 moduleType = "android_system_image"
816 }
817
mrziwang8f86c882024-10-03 12:34:33 -0700818 file := &parser.File{
819 Defs: []parser.Definition{
820 &parser.Module{
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000821 Type: moduleType,
mrziwang8f86c882024-10-03 12:34:33 -0700822 Map: *result,
823 },
824 },
825 }
826 bytes, err := parser.Print(file)
827 if err != nil {
828 ctx.ModuleErrorf(err.Error())
829 }
830 return strings.TrimSpace(string(bytes))
831}