blob: 8a415247e6dc2a8151769f5f493d922ca170b843 [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 Faust92ccbe22024-10-03 14:38:37 -070050}
51
Jihoon Kang98047cf2024-10-02 17:13:54 +000052type filesystemCreator struct {
53 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -070054
55 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +000056}
57
58func filesystemCreatorFactory() android.Module {
59 module := &filesystemCreator{}
60
Cole Faust69788792024-10-10 11:00:36 -070061 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -070062 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +000063 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jihoon Kang675d4682024-10-24 23:45:11 +000064 generatedPrebuiltEtcModuleNames := createPrebuiltEtcModules(ctx)
Jihoon Kang04f12c92024-11-12 23:03:08 +000065 avbpubkeyGenerated := createAvbpubkeyModule(ctx)
66 createFsGenState(ctx, generatedPrebuiltEtcModuleNames, avbpubkeyGenerated)
Cole Faust953476f2024-11-14 14:11:29 -080067 module.createAvbKeyFilegroups(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +000068 module.createInternalModules(ctx)
69 })
70
71 return module
72}
73
74func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Cole Faust3552eb62024-11-06 18:07:26 -080075 soongGeneratedPartitions := generatedPartitions(ctx)
76 finalSoongGeneratedPartitions := make([]string, 0, len(soongGeneratedPartitions))
77 for _, partitionType := range soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -070078 if f.createPartition(ctx, partitionType) {
79 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
Cole Faust3552eb62024-11-06 18:07:26 -080080 finalSoongGeneratedPartitions = append(finalSoongGeneratedPartitions, partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -070081 } else {
82 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
83 }
84 }
Cole Faust3552eb62024-11-06 18:07:26 -080085
86 for _, x := range createVbmetaPartitions(ctx, finalSoongGeneratedPartitions) {
87 f.properties.Vbmeta_module_names = append(f.properties.Vbmeta_module_names, x.moduleName)
88 f.properties.Vbmeta_partition_names = append(f.properties.Vbmeta_partition_names, x.partitionName)
89 }
90
91 ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions = finalSoongGeneratedPartitions
92 f.createDeviceModule(ctx, finalSoongGeneratedPartitions, f.properties.Vbmeta_module_names)
Jihoon Kang98047cf2024-10-02 17:13:54 +000093}
94
Jihoon Kang0d545b82024-10-11 00:21:57 +000095func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -070096 prefix := "soong"
97 if cfg.HasDeviceProduct() {
98 prefix = cfg.DeviceProduct()
99 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000100 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
101}
102
Jihoon Kang0d545b82024-10-11 00:21:57 +0000103func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
104 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000105}
106
Cole Faust3552eb62024-11-06 18:07:26 -0800107func (f *filesystemCreator) createDeviceModule(
108 ctx android.LoadHookContext,
109 generatedPartitionTypes []string,
110 vbmetaPartitions []string,
111) {
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000112 baseProps := &struct {
113 Name *string
114 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000115 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000116 }
117
Priyanka Advani (xWF)dafaa7f2024-10-21 22:55:13 +0000118 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000119 partitionProps := &filesystem.PartitionNameProperties{}
Cole Faust3552eb62024-11-06 18:07:26 -0800120 if android.InList("system", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000121 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000122 }
Cole Faust3552eb62024-11-06 18:07:26 -0800123 if android.InList("system_ext", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000124 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000125 }
Cole Faust3552eb62024-11-06 18:07:26 -0800126 if android.InList("vendor", generatedPartitionTypes) {
Spandan Dase3b65312024-10-22 00:27:27 +0000127 partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
128 }
Cole Faust3552eb62024-11-06 18:07:26 -0800129 if android.InList("product", generatedPartitionTypes) {
Jihoon Kang6dd13b62024-10-22 23:21:02 +0000130 partitionProps.Product_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product"))
131 }
Cole Faust3552eb62024-11-06 18:07:26 -0800132 if android.InList("odm", generatedPartitionTypes) {
Spandan Dasc5717162024-11-01 18:33:57 +0000133 partitionProps.Odm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm"))
134 }
mrziwang23ba8762024-11-07 16:21:53 -0800135 if android.InList("userdata", f.properties.Generated_partition_types) {
136 partitionProps.Userdata_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "userdata"))
137 }
Cole Faust3552eb62024-11-06 18:07:26 -0800138 partitionProps.Vbmeta_partitions = vbmetaPartitions
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000139
140 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700141}
142
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000143func partitionSpecificFsProps(fsProps *filesystem.FilesystemProperties, partitionType string) {
144 switch partitionType {
145 case "system":
146 fsProps.Build_logtags = proptools.BoolPtr(true)
147 // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
148 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Justin Yuned3dbce2024-11-15 11:57:24 +0900149 // Identical to that of the aosp_shared_system_image
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000150 fsProps.Fsverity.Inputs = []string{
151 "etc/boot-image.prof",
152 "etc/dirty-image-objects",
153 "etc/preloaded-classes",
154 "etc/classpaths/*.pb",
155 "framework/*",
156 "framework/*/*", // framework/{arch}
157 "framework/oat/*/*", // framework/oat/{arch}
158 }
159 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
mrziwang9afc2982024-11-05 14:29:48 -0800160 // TODO(b/377734331): only generate the symlinks if the relevant partitions exist
161 fsProps.Symlinks = []filesystem.SymlinkDefinition{
162 filesystem.SymlinkDefinition{
163 Target: proptools.StringPtr("/product"),
164 Name: proptools.StringPtr("system/product"),
165 },
166 filesystem.SymlinkDefinition{
167 Target: proptools.StringPtr("/system_ext"),
168 Name: proptools.StringPtr("system/system_ext"),
169 },
170 filesystem.SymlinkDefinition{
171 Target: proptools.StringPtr("/vendor"),
172 Name: proptools.StringPtr("system/vendor"),
173 },
174 filesystem.SymlinkDefinition{
175 Target: proptools.StringPtr("/system_dlkm/lib/modules"),
176 Name: proptools.StringPtr("system/lib/modules"),
177 },
178 }
179 fsProps.Base_dir = proptools.StringPtr("system")
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000180 case "system_ext":
181 fsProps.Fsverity.Inputs = []string{
182 "framework/*",
183 "framework/*/*", // framework/{arch}
184 "framework/oat/*/*", // framework/oat/{arch}
185 }
186 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000187 case "product":
188 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
189 case "vendor":
190 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das69464c32024-10-25 20:08:06 +0000191 fsProps.Symlinks = []filesystem.SymlinkDefinition{
192 filesystem.SymlinkDefinition{
193 Target: proptools.StringPtr("/odm"),
194 Name: proptools.StringPtr("vendor/odm"),
195 },
196 filesystem.SymlinkDefinition{
197 Target: proptools.StringPtr("/vendor_dlkm/lib/modules"),
198 Name: proptools.StringPtr("vendor/lib/modules"),
199 },
200 }
201 fsProps.Base_dir = proptools.StringPtr("vendor")
Spandan Dasc5717162024-11-01 18:33:57 +0000202 case "odm":
203 fsProps.Symlinks = []filesystem.SymlinkDefinition{
204 filesystem.SymlinkDefinition{
205 Target: proptools.StringPtr("/odm_dlkm/lib/modules"),
206 Name: proptools.StringPtr("odm/lib/modules"),
207 },
208 }
209 fsProps.Base_dir = proptools.StringPtr("odm")
mrziwang23ba8762024-11-07 16:21:53 -0800210 case "userdata":
211 fsProps.Base_dir = proptools.StringPtr("data")
Spandan Dasc5717162024-11-01 18:33:57 +0000212
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000213 }
214}
Spandan Dascbe641a2024-10-14 21:07:34 +0000215
Spandan Das5b493cd2024-11-07 20:55:56 +0000216var (
217 dlkmPartitions = []string{
218 "system_dlkm",
219 "vendor_dlkm",
220 "odm_dlkm",
221 }
222)
223
Cole Faust92ccbe22024-10-03 14:38:37 -0700224// Creates a soong module to build the given partition. Returns false if we can't support building
225// it.
226func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwang4b0ca972024-10-17 14:56:19 -0700227 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
228
229 fsProps, supported := generateFsProps(ctx, partitionType)
230 if !supported {
231 return false
mrziwanga077b942024-10-16 16:00:06 -0700232 }
mrziwanga077b942024-10-16 16:00:06 -0700233
Spandan Das8fe68dc2024-10-29 18:20:11 +0000234 if partitionType == "vendor" || partitionType == "product" {
Spandan Das2047a4c2024-11-11 21:24:58 +0000235 fsProps.Linker_config.Gen_linker_config = proptools.BoolPtr(true)
236 fsProps.Linker_config.Linker_config_srcs = f.createLinkerConfigSourceFilegroups(ctx, partitionType)
Spandan Das312cc412024-10-29 18:20:11 +0000237 }
238
Spandan Das5b493cd2024-11-07 20:55:56 +0000239 if android.InList(partitionType, dlkmPartitions) {
240 f.createPrebuiltKernelModules(ctx, partitionType)
Spandan Das5e336422024-11-01 22:31:20 +0000241 }
242
mrziwang4b0ca972024-10-17 14:56:19 -0700243 var module android.Module
244 if partitionType == "system" {
245 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
246 } else {
247 // Explicitly set the partition.
248 fsProps.Partition_type = proptools.StringPtr(partitionType)
249 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
250 }
251 module.HideFromMake()
Spandan Das168098c2024-10-28 19:44:34 +0000252 if partitionType == "vendor" {
Spandan Das4cd93b52024-11-05 23:27:03 +0000253 f.createVendorBuildProp(ctx)
Spandan Das168098c2024-10-28 19:44:34 +0000254 }
mrziwang4b0ca972024-10-17 14:56:19 -0700255 return true
256}
257
Cole Faust953476f2024-11-14 14:11:29 -0800258// Creates filegroups for the files specified in BOARD_(partition_)AVB_KEY_PATH
259func (f *filesystemCreator) createAvbKeyFilegroups(ctx android.LoadHookContext) {
260 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
261 var files []string
262
263 if len(partitionVars.BoardAvbKeyPath) > 0 {
264 files = append(files, partitionVars.BoardAvbKeyPath)
265 }
266 for _, partition := range android.SortedKeys(partitionVars.PartitionQualifiedVariables) {
267 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partition]
268 if len(specificPartitionVars.BoardAvbKeyPath) > 0 {
269 files = append(files, specificPartitionVars.BoardAvbKeyPath)
270 }
271 }
272
273 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
274 for _, file := range files {
275 if _, ok := fsGenState.avbKeyFilegroups[file]; ok {
276 continue
277 }
278 if file == "external/avb/test/data/testkey_rsa4096.pem" {
279 // There already exists a checked-in filegroup for this commonly-used key, just use that
280 fsGenState.avbKeyFilegroups[file] = "avb_testkey_rsa4096"
281 continue
282 }
283 dir := filepath.Dir(file)
284 base := filepath.Base(file)
285 name := fmt.Sprintf("avb_key_%x", strings.ReplaceAll(file, "/", "_"))
286 ctx.CreateModuleInDirectory(
287 android.FileGroupFactory,
288 dir,
289 &struct {
290 Name *string
291 Srcs []string
292 Visibility []string
293 }{
294 Name: proptools.StringPtr(name),
295 Srcs: []string{base},
296 Visibility: []string{"//visibility:public"},
297 },
298 )
299 fsGenState.avbKeyFilegroups[file] = name
300 }
301}
302
Spandan Das5e336422024-11-01 22:31:20 +0000303// createPrebuiltKernelModules creates `prebuilt_kernel_modules`. These modules will be added to deps of the
Spandan Das7b25a512024-11-06 20:41:26 +0000304// autogenerated *_dlkm filsystem modules. Each _dlkm partition should have a single prebuilt_kernel_modules dependency.
305// This ensures that the depmod artifacts (modules.* installed in /lib/modules/) are generated with a complete view.
Spandan Das5b493cd2024-11-07 20:55:56 +0000306func (f *filesystemCreator) createPrebuiltKernelModules(ctx android.LoadHookContext, partitionType string) {
Spandan Das5e336422024-11-01 22:31:20 +0000307 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
Spandan Das7b25a512024-11-06 20:41:26 +0000308 name := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-kernel-modules", partitionType))
309 props := &struct {
Spandan Das912d26b2024-11-06 19:35:17 +0000310 Name *string
311 Srcs []string
Spandan Das5b493cd2024-11-07 20:55:56 +0000312 System_deps []string
Spandan Das912d26b2024-11-06 19:35:17 +0000313 System_dlkm_specific *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000314 Vendor_dlkm_specific *bool
315 Odm_dlkm_specific *bool
316 Load_by_default *bool
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000317 Blocklist_file *string
Spandan Das7b25a512024-11-06 20:41:26 +0000318 }{
319 Name: proptools.StringPtr(name),
Spandan Das5e336422024-11-01 22:31:20 +0000320 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000321 switch partitionType {
322 case "system_dlkm":
323 props.Srcs = ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules
Spandan Das912d26b2024-11-06 19:35:17 +0000324 props.System_dlkm_specific = proptools.BoolPtr(true)
Spandan Das5b493cd2024-11-07 20:55:56 +0000325 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelLoadModules) == 0 {
326 // Create empty modules.load file for system
327 // https://source.corp.google.com/h/googleplex-android/platform/build/+/ef55daac9954896161b26db4f3ef1781b5a5694c:core/Makefile;l=695-700;drc=549fe2a5162548bd8b47867d35f907eb22332023;bpv=1;bpt=0
328 props.Load_by_default = proptools.BoolPtr(false)
329 }
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000330 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelBlocklistFile; blocklistFile != "" {
331 props.Blocklist_file = proptools.StringPtr(blocklistFile)
332 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000333 case "vendor_dlkm":
334 props.Srcs = ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelModules
335 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules) > 0 {
336 props.System_deps = []string{":" + generatedModuleName(ctx.Config(), "system_dlkm-kernel-modules") + "{.modules}"}
337 }
338 props.Vendor_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000339 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelBlocklistFile; blocklistFile != "" {
340 props.Blocklist_file = proptools.StringPtr(blocklistFile)
341 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000342 case "odm_dlkm":
343 props.Srcs = ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelModules
344 props.Odm_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000345 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelBlocklistFile; blocklistFile != "" {
346 props.Blocklist_file = proptools.StringPtr(blocklistFile)
347 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000348 default:
349 ctx.ModuleErrorf("DLKM is not supported for %s\n", partitionType)
Spandan Das912d26b2024-11-06 19:35:17 +0000350 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000351
352 if len(props.Srcs) == 0 {
353 return // do not generate `prebuilt_kernel_modules` if there are no sources
354 }
355
Spandan Das7b25a512024-11-06 20:41:26 +0000356 kernelModule := ctx.CreateModuleInDirectory(
357 kernel.PrebuiltKernelModulesFactory,
358 ".", // create in root directory for now
359 props,
360 )
361 kernelModule.HideFromMake()
362 // Add to deps
363 (*fsGenState.fsDeps[partitionType])[name] = defaultDepCandidateProps(ctx.Config())
Spandan Das5e336422024-11-01 22:31:20 +0000364}
365
Spandan Das4cd93b52024-11-05 23:27:03 +0000366// Create a build_prop and android_info module. This will be used to create /vendor/build.prop
367func (f *filesystemCreator) createVendorBuildProp(ctx android.LoadHookContext) {
368 // Create a android_info for vendor
369 // The board info files might be in a directory outside the root soong namespace, so create
370 // the module in "."
371 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
372 androidInfoProps := &struct {
373 Name *string
374 Board_info_files []string
375 Bootloader_board_name *string
376 }{
377 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "android-info.prop")),
378 Board_info_files: partitionVars.BoardInfoFiles,
379 }
380 if len(androidInfoProps.Board_info_files) == 0 {
381 androidInfoProps.Bootloader_board_name = proptools.StringPtr(partitionVars.BootLoaderBoardName)
382 }
383 androidInfoProp := ctx.CreateModuleInDirectory(
384 android.AndroidInfoFactory,
385 ".",
386 androidInfoProps,
387 )
388 androidInfoProp.HideFromMake()
389 // Create a build prop for vendor
390 vendorBuildProps := &struct {
391 Name *string
392 Vendor *bool
393 Stem *string
394 Product_config *string
395 Android_info *string
396 }{
397 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "vendor-build.prop")),
398 Vendor: proptools.BoolPtr(true),
399 Stem: proptools.StringPtr("build.prop"),
400 Product_config: proptools.StringPtr(":product_config"),
401 Android_info: proptools.StringPtr(":" + androidInfoProp.Name()),
402 }
403 vendorBuildProp := ctx.CreateModule(
404 android.BuildPropFactory,
405 vendorBuildProps,
406 )
407 vendorBuildProp.HideFromMake()
408}
409
Spandan Das8fe68dc2024-10-29 18:20:11 +0000410// createLinkerConfigSourceFilegroups creates filegroup modules to generate linker.config.pb for the following partitions
411// 1. vendor: Using PRODUCT_VENDOR_LINKER_CONFIG_FRAGMENTS (space separated file list)
412// 1. product: Using PRODUCT_PRODUCT_LINKER_CONFIG_FRAGMENTS (space separated file list)
413// It creates a filegroup for each file in the fragment list
Spandan Das312cc412024-10-29 18:20:11 +0000414// The filegroup modules are then added to `linker_config_srcs` of the autogenerated vendor `android_filesystem`.
Spandan Das8fe68dc2024-10-29 18:20:11 +0000415func (f *filesystemCreator) createLinkerConfigSourceFilegroups(ctx android.LoadHookContext, partitionType string) []string {
Spandan Das312cc412024-10-29 18:20:11 +0000416 ret := []string{}
417 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Spandan Das8fe68dc2024-10-29 18:20:11 +0000418 var linkerConfigSrcs []string
419 if partitionType == "vendor" {
420 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.VendorLinkerConfigSrcs)
421 } else if partitionType == "product" {
422 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.ProductLinkerConfigSrcs)
423 } else {
424 ctx.ModuleErrorf("linker.config.pb is only supported for vendor and product partitions. For system partition, use `android_system_image`")
425 }
426
427 if len(linkerConfigSrcs) > 0 {
Spandan Das312cc412024-10-29 18:20:11 +0000428 // Create a filegroup, and add `:<filegroup_name>` to ret.
429 for index, linkerConfigSrc := range linkerConfigSrcs {
430 dir := filepath.Dir(linkerConfigSrc)
431 base := filepath.Base(linkerConfigSrc)
Spandan Das8fe68dc2024-10-29 18:20:11 +0000432 fgName := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-linker-config-src%s", partitionType, strconv.Itoa(index)))
Spandan Das312cc412024-10-29 18:20:11 +0000433 srcs := []string{base}
434 fgProps := &struct {
435 Name *string
436 Srcs proptools.Configurable[[]string]
437 }{
438 Name: proptools.StringPtr(fgName),
439 Srcs: proptools.NewSimpleConfigurable(srcs),
440 }
441 ctx.CreateModuleInDirectory(
442 android.FileGroupFactory,
443 dir,
444 fgProps,
445 )
446 ret = append(ret, ":"+fgName)
447 }
448 }
449 return ret
450}
451
mrziwang4b0ca972024-10-17 14:56:19 -0700452type filesystemBaseProperty struct {
453 Name *string
454 Compile_multilib *string
Cole Faust3552eb62024-11-06 18:07:26 -0800455 Visibility []string
mrziwang4b0ca972024-10-17 14:56:19 -0700456}
457
458func generateBaseProps(namePtr *string) *filesystemBaseProperty {
459 return &filesystemBaseProperty{
460 Name: namePtr,
461 Compile_multilib: proptools.StringPtr("both"),
Cole Faust3552eb62024-11-06 18:07:26 -0800462 // The vbmeta modules are currently in the root directory and depend on the partitions
463 Visibility: []string{"//.", "//build/soong:__subpackages__"},
mrziwang4b0ca972024-10-17 14:56:19 -0700464 }
465}
466
467func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust953476f2024-11-14 14:11:29 -0800468 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
Cole Faust92ccbe22024-10-03 14:38:37 -0700469 fsProps := &filesystem.FilesystemProperties{}
470
mrziwang4b0ca972024-10-17 14:56:19 -0700471 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust76a6e952024-11-07 16:56:45 -0800472 var boardAvbEnable bool
Cole Faust953476f2024-11-14 14:11:29 -0800473 var boardAvbKeyPath string
474 var boardAvbAlgorithm string
475 var boardAvbRollbackIndex string
Cole Faust76a6e952024-11-07 16:56:45 -0800476 var fsType string
477 if strings.Contains(partitionType, "ramdisk") {
478 fsType = "compressed_cpio"
479 } else {
Cole Faust953476f2024-11-14 14:11:29 -0800480 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Cole Faust76a6e952024-11-07 16:56:45 -0800481 fsType = specificPartitionVars.BoardFileSystemType
Cole Faust953476f2024-11-14 14:11:29 -0800482 boardAvbEnable = partitionVars.BoardAvbEnable
483 boardAvbKeyPath = specificPartitionVars.BoardAvbKeyPath
484 boardAvbAlgorithm = specificPartitionVars.BoardAvbAlgorithm
485 boardAvbRollbackIndex = specificPartitionVars.BoardAvbRollbackIndex
486 if boardAvbEnable {
487 if boardAvbKeyPath == "" {
488 boardAvbKeyPath = partitionVars.BoardAvbKeyPath
489 }
490 if boardAvbAlgorithm == "" {
491 boardAvbAlgorithm = partitionVars.BoardAvbAlgorithm
492 }
493 if boardAvbRollbackIndex == "" {
494 boardAvbRollbackIndex = partitionVars.BoardAvbRollbackIndex
495 }
496 }
497 if fsType == "" {
498 fsType = "ext4" //default
499 }
Cole Faust76a6e952024-11-07 16:56:45 -0800500 }
Cole Faust953476f2024-11-14 14:11:29 -0800501 if boardAvbKeyPath != "" {
502 boardAvbKeyPath = ":" + fsGenState.avbKeyFilegroups[boardAvbKeyPath]
mrziwang4b0ca972024-10-17 14:56:19 -0700503 }
Cole Faust76a6e952024-11-07 16:56:45 -0800504
mrziwang4b0ca972024-10-17 14:56:19 -0700505 fsProps.Type = proptools.StringPtr(fsType)
506 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
507 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
508 return nil, false
509 }
510
Cole Faust92ccbe22024-10-03 14:38:37 -0700511 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
512 // and sometimes don't build.
513 fsProps.Unchecked_module = proptools.BoolPtr(true)
514
Jihoon Kang98047cf2024-10-02 17:13:54 +0000515 // BOARD_AVB_ENABLE
Cole Faust76a6e952024-11-07 16:56:45 -0800516 fsProps.Use_avb = proptools.BoolPtr(boardAvbEnable)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000517 // BOARD_AVB_KEY_PATH
Cole Faust953476f2024-11-14 14:11:29 -0800518 fsProps.Avb_private_key = proptools.StringPtr(boardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000519 // BOARD_AVB_ALGORITHM
Cole Faust953476f2024-11-14 14:11:29 -0800520 fsProps.Avb_algorithm = proptools.StringPtr(boardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000521 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust953476f2024-11-14 14:11:29 -0800522 if rollbackIndex, err := strconv.ParseInt(boardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000523 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
524 }
525
Cole Faust92ccbe22024-10-03 14:38:37 -0700526 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000527
Cole Faust92ccbe22024-10-03 14:38:37 -0700528 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000529
Jihoon Kang0d545b82024-10-11 00:21:57 +0000530 fsProps.Is_auto_generated = proptools.BoolPtr(true)
531
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000532 partitionSpecificFsProps(fsProps, partitionType)
533
Jihoon Kang98047cf2024-10-02 17:13:54 +0000534 // system_image properties that are not set:
535 // - filesystemProperties.Avb_hash_algorithm
536 // - filesystemProperties.File_contexts
537 // - filesystemProperties.Dirs
538 // - filesystemProperties.Symlinks
539 // - filesystemProperties.Fake_timestamp
540 // - filesystemProperties.Uuid
541 // - filesystemProperties.Mount_point
542 // - filesystemProperties.Include_make_built_files
543 // - filesystemProperties.Build_logtags
Jihoon Kang98047cf2024-10-02 17:13:54 +0000544 // - systemImageProperties.Linker_config_src
mrziwang4b0ca972024-10-17 14:56:19 -0700545
546 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700547}
548
549func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000550 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700551 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
552 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
553 if !ok {
554 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
555 }
556 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 +0000557 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700558
559 builder := android.NewRuleBuilder(pctx, ctx)
560 builder.Command().BuiltTool("file_list_diff").
561 Input(makeFileList).
562 Input(filesystemInfo.FileListFile).
Cole Faust56301572024-11-07 15:22:42 -0800563 Text(partitionModuleName)
Cole Faust92ccbe22024-10-03 14:38:37 -0700564 builder.Command().Text("touch").Output(diffTestResultFile)
565 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
566 return diffTestResultFile
567}
568
569func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
570 hasher := sha256.New()
571 hasher.Write([]byte(message))
572 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
573 file := android.PathForModuleOut(ctx, filename)
574 builder := android.NewRuleBuilder(pctx, ctx)
575 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
576 builder.Command().Text("exit 1 #").Output(file)
577 builder.Build("failing command "+filename, "failing command "+filename)
578 return file
579}
580
Cole Faust3552eb62024-11-06 18:07:26 -0800581func createVbmetaDiff(ctx android.ModuleContext, vbmetaModuleName string, vbmetaPartitionName string) android.Path {
582 vbmetaModule := ctx.GetDirectDepWithTag(vbmetaModuleName, generatedVbmetaPartitionDepTag)
583 outputFilesProvider, ok := android.OtherModuleProvider(ctx, vbmetaModule, android.OutputFilesProvider)
584 if !ok {
585 ctx.ModuleErrorf("Expected module %s to provide OutputFiles", vbmetaModule)
586 }
587 if len(outputFilesProvider.DefaultOutputFiles) != 1 {
588 ctx.ModuleErrorf("Expected 1 output file from module %s", vbmetaModule)
589 }
590 soongVbMetaFile := outputFilesProvider.DefaultOutputFiles[0]
591 makeVbmetaFile := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/%s.img", ctx.Config().DeviceName(), vbmetaPartitionName))
592
593 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", vbmetaModuleName))
594 builder := android.NewRuleBuilder(pctx, ctx)
595 builder.Command().Text("diff").
596 Input(soongVbMetaFile).
597 Input(makeVbmetaFile)
598 builder.Command().Text("touch").Output(diffTestResultFile)
599 builder.Build(vbmetaModuleName+" diff test", vbmetaModuleName+" diff test")
600 return diffTestResultFile
601}
602
Cole Faust92ccbe22024-10-03 14:38:37 -0700603type systemImageDepTagType struct {
604 blueprint.BaseDependencyTag
605}
606
607var generatedFilesystemDepTag systemImageDepTagType
Cole Faust3552eb62024-11-06 18:07:26 -0800608var generatedVbmetaPartitionDepTag systemImageDepTagType
Cole Faust92ccbe22024-10-03 14:38:37 -0700609
610func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
611 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000612 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700613 }
Cole Faust3552eb62024-11-06 18:07:26 -0800614 for _, vbmetaModule := range f.properties.Vbmeta_module_names {
615 ctx.AddDependency(ctx.Module(), generatedVbmetaPartitionDepTag, vbmetaModule)
616 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000617}
618
619func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700620 if ctx.ModuleDir() != "build/soong/fsgen" {
621 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
622 }
623 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000624
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000625 var content strings.Builder
626 generatedBp := android.PathForModuleOut(ctx, "soong_generated_product_config.bp")
627 for _, partition := range ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions {
628 content.WriteString(generateBpContent(ctx, partition))
629 content.WriteString("\n")
630 }
631 android.WriteFileRule(ctx, generatedBp, content.String())
632
mrziwang8f86c882024-10-03 12:34:33 -0700633 ctx.Phony("product_config_to_bp", generatedBp)
634
Cole Faust92ccbe22024-10-03 14:38:37 -0700635 var diffTestFiles []android.Path
636 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000637 diffTestFile := f.createDiffTest(ctx, partitionType)
638 diffTestFiles = append(diffTestFiles, diffTestFile)
639 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700640 }
641 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000642 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
643 diffTestFiles = append(diffTestFiles, diffTestFile)
644 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700645 }
Cole Faust3552eb62024-11-06 18:07:26 -0800646 for i, vbmetaModule := range f.properties.Vbmeta_module_names {
647 diffTestFile := createVbmetaDiff(ctx, vbmetaModule, f.properties.Vbmeta_partition_names[i])
648 diffTestFiles = append(diffTestFiles, diffTestFile)
649 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", f.properties.Vbmeta_partition_names[i]), diffTestFile)
650 }
Cole Faust92ccbe22024-10-03 14:38:37 -0700651 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000652}
mrziwang8f86c882024-10-03 12:34:33 -0700653
mrziwang8f86c882024-10-03 12:34:33 -0700654func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
mrziwang4b0ca972024-10-17 14:56:19 -0700655 fsProps, fsTypeSupported := generateFsProps(ctx, partitionType)
656 if !fsTypeSupported {
657 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700658 }
659
mrziwang4b0ca972024-10-17 14:56:19 -0700660 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000661 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
662 deps := fsGenState.fsDeps[partitionType]
663 highPriorityDeps := fsGenState.generatedPrebuiltEtcModuleNames
664 depProps := generateDepStruct(*deps, highPriorityDeps)
mrziwang8f86c882024-10-03 12:34:33 -0700665
mrziwang4b0ca972024-10-17 14:56:19 -0700666 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -0700667 if err != nil {
Cole Faustae3e1d32024-11-05 13:22:50 -0800668 ctx.ModuleErrorf("%s", err.Error())
669 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700670 }
671
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000672 moduleType := "android_filesystem"
673 if partitionType == "system" {
674 moduleType = "android_system_image"
675 }
676
mrziwang8f86c882024-10-03 12:34:33 -0700677 file := &parser.File{
678 Defs: []parser.Definition{
679 &parser.Module{
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000680 Type: moduleType,
mrziwang8f86c882024-10-03 12:34:33 -0700681 Map: *result,
682 },
683 },
684 }
685 bytes, err := parser.Print(file)
686 if err != nil {
687 ctx.ModuleErrorf(err.Error())
688 }
689 return strings.TrimSpace(string(bytes))
690}