blob: 29fec48ac610d797772a7be19af18577a5bd3e3a [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"`
47}
48
Jihoon Kang98047cf2024-10-02 17:13:54 +000049type filesystemCreator struct {
50 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -070051
52 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +000053}
54
55func filesystemCreatorFactory() android.Module {
56 module := &filesystemCreator{}
57
Cole Faust69788792024-10-10 11:00:36 -070058 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -070059 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +000060 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jihoon Kang675d4682024-10-24 23:45:11 +000061 generatedPrebuiltEtcModuleNames := createPrebuiltEtcModules(ctx)
62 createFsGenState(ctx, generatedPrebuiltEtcModuleNames)
Jihoon Kang98047cf2024-10-02 17:13:54 +000063 module.createInternalModules(ctx)
64 })
65
66 return module
67}
68
69func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +000070 soongGeneratedPartitions := &ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions
71 for _, partitionType := range *soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -070072 if f.createPartition(ctx, partitionType) {
73 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
74 } else {
75 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
Jihoon Kang0d545b82024-10-11 00:21:57 +000076 _, *soongGeneratedPartitions = android.RemoveFromList(partitionType, *soongGeneratedPartitions)
Cole Faust92ccbe22024-10-03 14:38:37 -070077 }
78 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +000079 f.createDeviceModule(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +000080}
81
Jihoon Kang0d545b82024-10-11 00:21:57 +000082func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -070083 prefix := "soong"
84 if cfg.HasDeviceProduct() {
85 prefix = cfg.DeviceProduct()
86 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +000087 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
88}
89
Jihoon Kang0d545b82024-10-11 00:21:57 +000090func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
91 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +000092}
93
94func (f *filesystemCreator) createDeviceModule(ctx android.LoadHookContext) {
95 baseProps := &struct {
96 Name *string
97 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +000098 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +000099 }
100
Priyanka Advani (xWF)dafaa7f2024-10-21 22:55:13 +0000101 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000102 partitionProps := &filesystem.PartitionNameProperties{}
103 if android.InList("system", f.properties.Generated_partition_types) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000104 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000105 }
Spandan Das7a46f6c2024-10-14 18:41:18 +0000106 if android.InList("system_ext", f.properties.Generated_partition_types) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000107 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000108 }
Spandan Dase3b65312024-10-22 00:27:27 +0000109 if android.InList("vendor", f.properties.Generated_partition_types) {
110 partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
111 }
Jihoon Kang6dd13b62024-10-22 23:21:02 +0000112 if android.InList("product", f.properties.Generated_partition_types) {
113 partitionProps.Product_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product"))
114 }
Spandan Dasc5717162024-11-01 18:33:57 +0000115 if android.InList("odm", f.properties.Generated_partition_types) {
116 partitionProps.Odm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm"))
117 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000118
119 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700120}
121
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000122func partitionSpecificFsProps(fsProps *filesystem.FilesystemProperties, partitionType string) {
123 switch partitionType {
124 case "system":
125 fsProps.Build_logtags = proptools.BoolPtr(true)
126 // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
127 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000128 // Identical to that of the generic_system_image
129 fsProps.Fsverity.Inputs = []string{
130 "etc/boot-image.prof",
131 "etc/dirty-image-objects",
132 "etc/preloaded-classes",
133 "etc/classpaths/*.pb",
134 "framework/*",
135 "framework/*/*", // framework/{arch}
136 "framework/oat/*/*", // framework/oat/{arch}
137 }
138 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
139 case "system_ext":
140 fsProps.Fsverity.Inputs = []string{
141 "framework/*",
142 "framework/*/*", // framework/{arch}
143 "framework/oat/*/*", // framework/oat/{arch}
144 }
145 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000146 case "product":
147 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
148 case "vendor":
149 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das69464c32024-10-25 20:08:06 +0000150 fsProps.Symlinks = []filesystem.SymlinkDefinition{
151 filesystem.SymlinkDefinition{
152 Target: proptools.StringPtr("/odm"),
153 Name: proptools.StringPtr("vendor/odm"),
154 },
155 filesystem.SymlinkDefinition{
156 Target: proptools.StringPtr("/vendor_dlkm/lib/modules"),
157 Name: proptools.StringPtr("vendor/lib/modules"),
158 },
159 }
160 fsProps.Base_dir = proptools.StringPtr("vendor")
Spandan Dasc5717162024-11-01 18:33:57 +0000161 case "odm":
162 fsProps.Symlinks = []filesystem.SymlinkDefinition{
163 filesystem.SymlinkDefinition{
164 Target: proptools.StringPtr("/odm_dlkm/lib/modules"),
165 Name: proptools.StringPtr("odm/lib/modules"),
166 },
167 }
168 fsProps.Base_dir = proptools.StringPtr("odm")
169
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000170 }
171}
Spandan Dascbe641a2024-10-14 21:07:34 +0000172
Cole Faust92ccbe22024-10-03 14:38:37 -0700173// Creates a soong module to build the given partition. Returns false if we can't support building
174// it.
175func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwang4b0ca972024-10-17 14:56:19 -0700176 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
177
178 fsProps, supported := generateFsProps(ctx, partitionType)
179 if !supported {
180 return false
mrziwanga077b942024-10-16 16:00:06 -0700181 }
mrziwanga077b942024-10-16 16:00:06 -0700182
Spandan Das8fe68dc2024-10-29 18:20:11 +0000183 if partitionType == "vendor" || partitionType == "product" {
Spandan Das173256b2024-10-31 19:59:30 +0000184 fsProps.Linkerconfig.Gen_linker_config = proptools.BoolPtr(true)
185 fsProps.Linkerconfig.Linker_config_srcs = f.createLinkerConfigSourceFilegroups(ctx, partitionType)
Spandan Das312cc412024-10-29 18:20:11 +0000186 }
187
Spandan Das5e336422024-11-01 22:31:20 +0000188 if partitionType == "system_dlkm" {
189 kernelModules := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules
190 f.createPrebuiltKernelModules(ctx, partitionType, kernelModules)
191 }
192
mrziwang4b0ca972024-10-17 14:56:19 -0700193 var module android.Module
194 if partitionType == "system" {
195 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
Spandan Das5e336422024-11-01 22:31:20 +0000196 } else if partitionType == "system_dlkm" {
197 // Do not set partition_type. build/soong/android/paths#modulePartition currently does not support dlkm
198 // partitions. Since `android_filesystem` uses a partition based filter, setting the partition here
199 // would result in missing in entries.
200 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
mrziwang4b0ca972024-10-17 14:56:19 -0700201 } else {
202 // Explicitly set the partition.
203 fsProps.Partition_type = proptools.StringPtr(partitionType)
204 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
205 }
206 module.HideFromMake()
Spandan Das168098c2024-10-28 19:44:34 +0000207 if partitionType == "vendor" {
208 // Create a build prop for vendor
209 vendorBuildProps := &struct {
210 Name *string
211 Vendor *bool
212 Stem *string
213 Product_config *string
214 }{
215 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "vendor-build.prop")),
216 Vendor: proptools.BoolPtr(true),
217 Stem: proptools.StringPtr("build.prop"),
218 Product_config: proptools.StringPtr(":product_config"),
219 }
220 vendorBuildProp := ctx.CreateModule(
221 android.BuildPropFactory,
222 vendorBuildProps,
223 )
224 vendorBuildProp.HideFromMake()
225 }
mrziwang4b0ca972024-10-17 14:56:19 -0700226 return true
227}
228
Spandan Das5e336422024-11-01 22:31:20 +0000229// createPrebuiltKernelModules creates `prebuilt_kernel_modules`. These modules will be added to deps of the
230// autogenerated *_dlkm filsystem modules.
231// The input `kernelModules` is a space separated list of .ko files in the workspace. This will be partitioned per directory
232// and a `prebuilt_kernel_modules` will be created per partition.
233// These autogenerated modules will be subsequently added to the deps of the top level *_dlkm android_filesystem
234func (f *filesystemCreator) createPrebuiltKernelModules(ctx android.LoadHookContext, partitionType string, kernelModules []string) {
235 // Partition the files per directory
236 dirToFiles := map[string][]string{}
237 for _, kernelModule := range kernelModules {
238 dir := filepath.Dir(kernelModule)
239 base := filepath.Base(kernelModule)
240 dirToFiles[dir] = append(dirToFiles[dir], base)
241 }
242 // Create a prebuilt_kernel_modules module per partition
243 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
244 for index, dir := range android.SortedKeys(dirToFiles) {
245 name := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-kernel-modules-%s", partitionType, strconv.Itoa(index)))
246 props := &struct {
247 Name *string
248 Srcs []string
249 }{
250 Name: proptools.StringPtr(name),
251 Srcs: dirToFiles[dir],
252 }
253 kernelModule := ctx.CreateModuleInDirectory(
254 kernel.PrebuiltKernelModulesFactory,
255 dir,
256 props,
257 )
258 kernelModule.HideFromMake()
259 // Add to deps
260 (*fsGenState.fsDeps[partitionType])[name] = defaultDepCandidateProps(ctx.Config())
261 }
262}
263
Spandan Das8fe68dc2024-10-29 18:20:11 +0000264// createLinkerConfigSourceFilegroups creates filegroup modules to generate linker.config.pb for the following partitions
265// 1. vendor: Using PRODUCT_VENDOR_LINKER_CONFIG_FRAGMENTS (space separated file list)
266// 1. product: Using PRODUCT_PRODUCT_LINKER_CONFIG_FRAGMENTS (space separated file list)
267// It creates a filegroup for each file in the fragment list
Spandan Das312cc412024-10-29 18:20:11 +0000268// The filegroup modules are then added to `linker_config_srcs` of the autogenerated vendor `android_filesystem`.
Spandan Das8fe68dc2024-10-29 18:20:11 +0000269func (f *filesystemCreator) createLinkerConfigSourceFilegroups(ctx android.LoadHookContext, partitionType string) []string {
Spandan Das312cc412024-10-29 18:20:11 +0000270 ret := []string{}
271 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Spandan Das8fe68dc2024-10-29 18:20:11 +0000272 var linkerConfigSrcs []string
273 if partitionType == "vendor" {
274 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.VendorLinkerConfigSrcs)
275 } else if partitionType == "product" {
276 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.ProductLinkerConfigSrcs)
277 } else {
278 ctx.ModuleErrorf("linker.config.pb is only supported for vendor and product partitions. For system partition, use `android_system_image`")
279 }
280
281 if len(linkerConfigSrcs) > 0 {
Spandan Das312cc412024-10-29 18:20:11 +0000282 // Create a filegroup, and add `:<filegroup_name>` to ret.
283 for index, linkerConfigSrc := range linkerConfigSrcs {
284 dir := filepath.Dir(linkerConfigSrc)
285 base := filepath.Base(linkerConfigSrc)
Spandan Das8fe68dc2024-10-29 18:20:11 +0000286 fgName := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-linker-config-src%s", partitionType, strconv.Itoa(index)))
Spandan Das312cc412024-10-29 18:20:11 +0000287 srcs := []string{base}
288 fgProps := &struct {
289 Name *string
290 Srcs proptools.Configurable[[]string]
291 }{
292 Name: proptools.StringPtr(fgName),
293 Srcs: proptools.NewSimpleConfigurable(srcs),
294 }
295 ctx.CreateModuleInDirectory(
296 android.FileGroupFactory,
297 dir,
298 fgProps,
299 )
300 ret = append(ret, ":"+fgName)
301 }
302 }
303 return ret
304}
305
mrziwang4b0ca972024-10-17 14:56:19 -0700306type filesystemBaseProperty struct {
307 Name *string
308 Compile_multilib *string
309}
310
311func generateBaseProps(namePtr *string) *filesystemBaseProperty {
312 return &filesystemBaseProperty{
313 Name: namePtr,
314 Compile_multilib: proptools.StringPtr("both"),
315 }
316}
317
318func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700319 fsProps := &filesystem.FilesystemProperties{}
320
mrziwang4b0ca972024-10-17 14:56:19 -0700321 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
322 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
323
324 // BOARD_SYSTEMIMAGE_FILE_SYSTEM_TYPE
325 fsType := specificPartitionVars.BoardFileSystemType
326 if fsType == "" {
327 fsType = "ext4" //default
328 }
329 fsProps.Type = proptools.StringPtr(fsType)
330 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
331 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
332 return nil, false
333 }
334
Cole Faust92ccbe22024-10-03 14:38:37 -0700335 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
336 // and sometimes don't build.
337 fsProps.Unchecked_module = proptools.BoolPtr(true)
338
Jihoon Kang98047cf2024-10-02 17:13:54 +0000339 // BOARD_AVB_ENABLE
340 fsProps.Use_avb = proptools.BoolPtr(partitionVars.BoardAvbEnable)
341 // BOARD_AVB_KEY_PATH
Cole Faust92ccbe22024-10-03 14:38:37 -0700342 fsProps.Avb_private_key = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000343 // BOARD_AVB_ALGORITHM
Cole Faust92ccbe22024-10-03 14:38:37 -0700344 fsProps.Avb_algorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000345 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust92ccbe22024-10-03 14:38:37 -0700346 if rollbackIndex, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64); err == nil {
Jihoon Kang98047cf2024-10-02 17:13:54 +0000347 fsProps.Rollback_index = proptools.Int64Ptr(rollbackIndex)
348 }
349
Cole Faust92ccbe22024-10-03 14:38:37 -0700350 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000351
Cole Faust92ccbe22024-10-03 14:38:37 -0700352 fsProps.Base_dir = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000353
Jihoon Kang0d545b82024-10-11 00:21:57 +0000354 fsProps.Is_auto_generated = proptools.BoolPtr(true)
355
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000356 partitionSpecificFsProps(fsProps, partitionType)
357
Jihoon Kang98047cf2024-10-02 17:13:54 +0000358 // system_image properties that are not set:
359 // - filesystemProperties.Avb_hash_algorithm
360 // - filesystemProperties.File_contexts
361 // - filesystemProperties.Dirs
362 // - filesystemProperties.Symlinks
363 // - filesystemProperties.Fake_timestamp
364 // - filesystemProperties.Uuid
365 // - filesystemProperties.Mount_point
366 // - filesystemProperties.Include_make_built_files
367 // - filesystemProperties.Build_logtags
Jihoon Kang98047cf2024-10-02 17:13:54 +0000368 // - systemImageProperties.Linker_config_src
mrziwang4b0ca972024-10-17 14:56:19 -0700369
370 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700371}
372
373func (f *filesystemCreator) createDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000374 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700375 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
376 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
377 if !ok {
378 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
379 }
380 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
381 // For now, don't allowlist anything. The test will fail, but that's fine in the current
382 // early stages where we're just figuring out what we need
Jihoon Kang9e866c82024-10-07 22:39:18 +0000383 emptyAllowlistFile := android.PathForModuleOut(ctx, fmt.Sprintf("allowlist_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700384 android.WriteFileRule(ctx, emptyAllowlistFile, "")
Jihoon Kang9e866c82024-10-07 22:39:18 +0000385 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700386
387 builder := android.NewRuleBuilder(pctx, ctx)
388 builder.Command().BuiltTool("file_list_diff").
389 Input(makeFileList).
390 Input(filesystemInfo.FileListFile).
Jihoon Kang9e866c82024-10-07 22:39:18 +0000391 Text(partitionModuleName).
392 FlagWithInput("--allowlists ", emptyAllowlistFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700393 builder.Command().Text("touch").Output(diffTestResultFile)
394 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
395 return diffTestResultFile
396}
397
398func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
399 hasher := sha256.New()
400 hasher.Write([]byte(message))
401 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
402 file := android.PathForModuleOut(ctx, filename)
403 builder := android.NewRuleBuilder(pctx, ctx)
404 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
405 builder.Command().Text("exit 1 #").Output(file)
406 builder.Build("failing command "+filename, "failing command "+filename)
407 return file
408}
409
410type systemImageDepTagType struct {
411 blueprint.BaseDependencyTag
412}
413
414var generatedFilesystemDepTag systemImageDepTagType
415
416func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
417 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000418 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700419 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000420}
421
422func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700423 if ctx.ModuleDir() != "build/soong/fsgen" {
424 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
425 }
426 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000427
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000428 var content strings.Builder
429 generatedBp := android.PathForModuleOut(ctx, "soong_generated_product_config.bp")
430 for _, partition := range ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions {
431 content.WriteString(generateBpContent(ctx, partition))
432 content.WriteString("\n")
433 }
434 android.WriteFileRule(ctx, generatedBp, content.String())
435
mrziwang8f86c882024-10-03 12:34:33 -0700436 ctx.Phony("product_config_to_bp", generatedBp)
437
Cole Faust92ccbe22024-10-03 14:38:37 -0700438 var diffTestFiles []android.Path
439 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000440 diffTestFile := f.createDiffTest(ctx, partitionType)
441 diffTestFiles = append(diffTestFiles, diffTestFile)
442 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700443 }
444 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000445 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
446 diffTestFiles = append(diffTestFiles, diffTestFile)
447 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700448 }
449 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000450}
mrziwang8f86c882024-10-03 12:34:33 -0700451
mrziwang8f86c882024-10-03 12:34:33 -0700452func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
mrziwang4b0ca972024-10-17 14:56:19 -0700453 fsProps, fsTypeSupported := generateFsProps(ctx, partitionType)
454 if !fsTypeSupported {
455 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700456 }
Spandan Dasc5717162024-11-01 18:33:57 +0000457 if partitionType == "vendor" || partitionType == "odm" {
Spandan Das69464c32024-10-25 20:08:06 +0000458 return "" // TODO: Handle struct props
459 }
mrziwang8f86c882024-10-03 12:34:33 -0700460
mrziwang4b0ca972024-10-17 14:56:19 -0700461 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
mrziwang2a506cf2024-10-17 15:38:37 -0700462 deps := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).fsDeps[partitionType]
463 depProps := generateDepStruct(*deps)
mrziwang8f86c882024-10-03 12:34:33 -0700464
mrziwang4b0ca972024-10-17 14:56:19 -0700465 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -0700466 if err != nil {
467 ctx.ModuleErrorf(err.Error())
468 }
469
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000470 moduleType := "android_filesystem"
471 if partitionType == "system" {
472 moduleType = "android_system_image"
473 }
474
mrziwang8f86c882024-10-03 12:34:33 -0700475 file := &parser.File{
476 Defs: []parser.Definition{
477 &parser.Module{
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000478 Type: moduleType,
mrziwang8f86c882024-10-03 12:34:33 -0700479 Map: *result,
480 },
481 },
482 }
483 bytes, err := parser.Print(file)
484 if err != nil {
485 ctx.ModuleErrorf(err.Error())
486 }
487 return strings.TrimSpace(string(bytes))
488}