blob: 411770be9ec5ba5b1fa49738bcc6cd6e96af4946 [file] [log] [blame]
Jiyong Park6f0f6882020-11-12 13:14:30 +09001// Copyright (C) 2020 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 filesystem
16
17import (
Jooyung Han65f402b2022-04-21 14:24:04 +090018 "crypto/sha256"
Jiyong Park6f0f6882020-11-12 13:14:30 +090019 "fmt"
Jooyung Han65f402b2022-04-21 14:24:04 +090020 "io"
Inseob Kim14199b02021-02-09 21:18:31 +090021 "path/filepath"
Cole Faust4a2a7c92024-03-12 12:44:40 -070022 "slices"
Cole Fauste03ab892025-01-17 13:55:04 -080023 "sort"
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +000024 "strconv"
Inseob Kim14199b02021-02-09 21:18:31 +090025 "strings"
Jiyong Park6f0f6882020-11-12 13:14:30 +090026
27 "android/soong/android"
Jooyung Hane6067592023-03-16 13:11:17 +090028 "android/soong/cc"
Spandan Das71be42d2024-11-20 18:34:16 +000029 "android/soong/java"
Spandan Das92631882024-10-28 22:49:38 +000030 "android/soong/linkerconfig"
Jiyong Park65b62242020-11-25 12:44:59 +090031
32 "github.com/google/blueprint"
Jihoon Kangdd49f412025-03-07 01:30:43 +000033 "github.com/google/blueprint/depset"
Jiyong Park71baa762021-01-18 21:11:03 +090034 "github.com/google/blueprint/proptools"
Jiyong Park6f0f6882020-11-12 13:14:30 +090035)
36
Cole Faust1dcf9e42025-02-19 17:23:34 -080037var pctx = android.NewPackageContext("android/soong/filesystem")
38
Jiyong Park6f0f6882020-11-12 13:14:30 +090039func init() {
Jooyung Han9706cbc2021-04-15 22:43:48 +090040 registerBuildComponents(android.InitRegistrationContext)
Spandan Das71be42d2024-11-20 18:34:16 +000041 registerMutators(android.InitRegistrationContext)
Jihoon Kangf67b7de2025-02-12 01:01:09 +000042 pctx.HostBinToolVariable("fileslist", "fileslist")
Spandan Dasdd262fb2025-02-13 00:15:59 +000043 pctx.HostBinToolVariable("fs_config", "fs_config")
Cole Faust1dcf9e42025-02-19 17:23:34 -080044 pctx.HostBinToolVariable("symbols_map", "symbols_map")
Jooyung Han9706cbc2021-04-15 22:43:48 +090045}
46
47func registerBuildComponents(ctx android.RegistrationContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -070048 ctx.RegisterModuleType("android_filesystem", FilesystemFactory)
Jiyong Parkf46b1af2024-04-05 18:13:33 +090049 ctx.RegisterModuleType("android_filesystem_defaults", filesystemDefaultsFactory)
Jihoon Kang98047cf2024-10-02 17:13:54 +000050 ctx.RegisterModuleType("android_system_image", SystemImageFactory)
Jiyong Parkbc485482022-11-15 22:31:49 +090051 ctx.RegisterModuleType("avb_add_hash_footer", avbAddHashFooterFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090052 ctx.RegisterModuleType("avb_add_hash_footer_defaults", avbAddHashFooterDefaultsFactory)
Alice Wang000e3a32023-01-03 16:11:20 +000053 ctx.RegisterModuleType("avb_gen_vbmeta_image", avbGenVbmetaImageFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090054 ctx.RegisterModuleType("avb_gen_vbmeta_image_defaults", avbGenVbmetaImageDefaultsFactory)
Jiyong Park6f0f6882020-11-12 13:14:30 +090055}
56
Spandan Das71be42d2024-11-20 18:34:16 +000057func registerMutators(ctx android.RegistrationContext) {
58 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
59 ctx.BottomUp("add_autogenerated_rro_deps", addAutogeneratedRroDeps)
60 })
61}
62
Jihoon Kangf67b7de2025-02-12 01:01:09 +000063var (
64 // Remember to add referenced files to implicits!
65 textFileProcessorRule = pctx.AndroidStaticRule("text_file_processing", blueprint.RuleParams{
66 Command: "build/soong/scripts/text_file_processor.py $in $out",
67 CommandDeps: []string{"build/soong/scripts/text_file_processor.py"},
68 })
69
70 // Remember to add the output image file as an implicit dependency!
71 installedFilesJsonRule = pctx.AndroidStaticRule("installed_files_json", blueprint.RuleParams{
72 Command: `${fileslist} ${rootDir} > ${out}`,
73 CommandDeps: []string{"${fileslist}"},
74 }, "rootDir")
75
76 installedFilesTxtRule = pctx.AndroidStaticRule("installed_files_txt", blueprint.RuleParams{
77 Command: `build/make/tools/fileslist_util.py -c ${in} > ${out}`,
78 CommandDeps: []string{"build/make/tools/fileslist_util.py"},
79 })
Spandan Dasdd262fb2025-02-13 00:15:59 +000080 fsConfigRule = pctx.AndroidStaticRule("fs_config_rule", blueprint.RuleParams{
81 Command: `(cd ${rootDir}; find . -type d | sed 's,$$,/,'; find . \! -type d) | cut -c 3- | sort | sed 's,^,${prefix},' | ${fs_config} -C -D ${rootDir} -R "${prefix}" > ${out}`,
82 CommandDeps: []string{"${fs_config}"},
83 }, "rootDir", "prefix")
Jihoon Kangf67b7de2025-02-12 01:01:09 +000084)
Cole Fauste1676122024-12-03 17:32:25 -080085
Jiyong Park6f0f6882020-11-12 13:14:30 +090086type filesystem struct {
87 android.ModuleBase
88 android.PackagingBase
Jiyong Parkf46b1af2024-04-05 18:13:33 +090089 android.DefaultableModuleBase
Jiyong Park65c49f52020-11-24 14:23:26 +090090
Jihoon Kang98047cf2024-10-02 17:13:54 +000091 properties FilesystemProperties
Jiyong Park71baa762021-01-18 21:11:03 +090092
Cole Faust4e9f5922024-11-13 16:09:23 -080093 output android.Path
Jiyong Park65c49f52020-11-24 14:23:26 +090094 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090095
Cole Faust4e9f5922024-11-13 16:09:23 -080096 fileListFile android.Path
Kiyoung Kim99a954d2024-06-21 14:22:20 +090097
98 // Keeps the entries installed from this filesystem
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090099 entries []string
Kiyoung Kim67118212024-11-07 13:23:44 +0900100
101 filesystemBuilder filesystemBuilder
Spandan Dasf12ff9b2025-02-12 22:27:43 +0000102
103 selinuxFc android.Path
Jiyong Park6f0f6882020-11-12 13:14:30 +0900104}
105
Kiyoung Kim67118212024-11-07 13:23:44 +0900106type filesystemBuilder interface {
Cole Faust19fbb072025-01-30 18:19:29 -0800107 BuildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath, fullInstallPaths *[]FullInstallPathInfo)
Kiyoung Kim67118212024-11-07 13:23:44 +0900108 // Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs()
109 FilterPackagingSpec(spec android.PackagingSpec) bool
Inseob Kim3c0a0422024-11-05 17:21:37 +0900110 // Function that modifies PackagingSpec in PackagingBase.GatherPackagingSpecs() to customize.
111 // For example, GSI system.img contains system_ext and product artifacts and their
112 // relPathInPackage need to be rebased to system/system_ext and system/system_product.
113 ModifyPackagingSpec(spec *android.PackagingSpec)
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000114
115 // Function to check if the filesystem should not use `vintf_fragments` property,
116 // but use `vintf_fragment` module type instead
117 ShouldUseVintfFragmentModuleOnly() bool
Kiyoung Kim67118212024-11-07 13:23:44 +0900118}
119
120var _ filesystemBuilder = (*filesystem)(nil)
121
Spandan Das69464c32024-10-25 20:08:06 +0000122type SymlinkDefinition struct {
Inseob Kim14199b02021-02-09 21:18:31 +0900123 Target *string
124 Name *string
125}
126
Jihoon Kang0a453892024-12-09 22:16:26 +0000127// CopyWithNamePrefix returns a new [SymlinkDefinition] with prefix added to Name.
128func (s *SymlinkDefinition) CopyWithNamePrefix(prefix string) SymlinkDefinition {
129 return SymlinkDefinition{
130 Target: s.Target,
131 Name: proptools.StringPtr(filepath.Join(prefix, proptools.String(s.Name))),
132 }
133}
134
Jihoon Kang98047cf2024-10-02 17:13:54 +0000135type FilesystemProperties struct {
Jiyong Park71baa762021-01-18 21:11:03 +0900136 // When set to true, sign the image with avbtool. Default is false.
137 Use_avb *bool
138
139 // Path to the private key that avbtool will use to sign this filesystem image.
140 // TODO(jiyong): allow apex_key to be specified here
141 Avb_private_key *string `android:"path"`
142
Shikha Panwar01403bb2022-12-22 12:22:57 +0000143 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +0900144 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +0900145
Shikha Panwar01403bb2022-12-22 12:22:57 +0000146 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
Nikita Ioffe50fb49c2025-01-24 13:49:00 +0000147 // avbtool. Default is sha256.
Shikha Panware6f30632022-12-21 12:54:45 +0000148 Avb_hash_algorithm *string
149
Spandan Dase5c393c2024-12-12 19:25:07 +0000150 // The security patch passed to as the com.android.build.<type>.security_patch avb property.
151 Security_patch *string
152
Cole Fauste1676122024-12-03 17:32:25 -0800153 // Whether or not to use forward-error-correction codes when signing with AVB. Defaults to true.
154 Use_fec *bool
155
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000156 // The index used to prevent rollback of the image. Only used if use_avb is true.
157 Rollback_index *int64
158
Luca Stefani9235f4c2025-02-08 12:09:34 +0100159 // Rollback index location of this image. Must be 1, 2, 3, etc.
Jihoon Kang2f0d1932025-01-17 19:22:44 +0000160 Rollback_index_location *int64
161
Jiyong Parkac4076d2021-03-15 23:21:30 +0900162 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
163 Partition_name *string
164
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000165 // Type of the filesystem. Currently, ext4, erofs, cpio, and compressed_cpio are supported. Default
Jiyong Park837cdb22021-02-05 00:17:14 +0900166 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +0900167 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +0900168
Cole Faust9a24d902024-03-18 15:38:12 -0700169 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
170 // checks, and will be used in the future for API surface checks.
171 Partition_type *string
172
Cole Faust0d467052024-12-04 17:19:19 -0800173 // file_contexts file to make image. Currently, only ext4 is supported. These file contexts
174 // will be compiled with sefcontext_compile
Inseob Kimcc8e5362021-02-03 14:05:24 +0900175 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900176
Cole Faust0d467052024-12-04 17:19:19 -0800177 // The selinux file contexts, after having already run them through sefcontext_compile
178 Precompiled_file_contexts *string `android:"path"`
179
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900180 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
181 // (root).
182 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +0900183
184 // Directories to be created under root. e.g. /dev, /proc, etc.
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700185 Dirs proptools.Configurable[[]string]
Inseob Kim14199b02021-02-09 21:18:31 +0900186
Jihoon Kang6da80752024-12-23 18:53:32 +0000187 // List of filesystem modules to include in creating the partition. The root directory of
188 // the provided filesystem modules are included in creating the partition.
189 // This is only supported for cpio and compressed cpio filesystem types.
190 Include_files_of []string
191
Inseob Kim14199b02021-02-09 21:18:31 +0900192 // Symbolic links to be created under root with "ln -sf <target> <name>".
Spandan Das69464c32024-10-25 20:08:06 +0000193 Symlinks []SymlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900194
195 // Seconds since unix epoch to override timestamps of file entries
196 Fake_timestamp *string
197
198 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
199 // Otherwise, they'll be set as random which might cause indeterministic build output.
200 Uuid *string
Inseob Kim376d72f2023-11-01 15:40:25 +0900201
202 // Mount point for this image. Default is "/"
203 Mount_point *string
Cole Faust4a2a7c92024-03-12 12:44:40 -0700204
Inseob Kimb7b84572024-04-30 10:51:47 +0900205 // When set, builds etc/event-log-tags file by merging logtags from all dependencies.
206 // Default is false
207 Build_logtags *bool
208
Justin Yun74f3f302024-05-07 14:32:14 +0900209 // Install aconfig_flags.pb file for the modules installed in this partition.
210 Gen_aconfig_flags_pb *bool
211
Inseob Kim53391842024-03-29 17:44:07 +0900212 Fsverity fsverityProperties
Cole Faust92ccbe22024-10-03 14:38:37 -0700213
214 // If this property is set to true, the filesystem will call ctx.UncheckedModule(), causing
215 // it to not be built on checkbuilds. Used for the automatic migration from make to soong
216 // build modules, where we want to emit some not-yet-working filesystems and we don't want them
217 // to be built.
218 Unchecked_module *bool `blueprint:"mutated"`
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000219
220 Erofs ErofsProperties
Jihoon Kang0d545b82024-10-11 00:21:57 +0000221
mrziwang1a6291f2024-11-07 14:29:25 -0800222 F2fs F2fsProperties
223
Spandan Das2047a4c2024-11-11 21:24:58 +0000224 Linker_config LinkerConfigProperties
Spandan Das92631882024-10-28 22:49:38 +0000225
Jihoon Kang0d545b82024-10-11 00:21:57 +0000226 // Determines if the module is auto-generated from Soong or not. If the module is
227 // auto-generated, its deps are exempted from visibility enforcement.
228 Is_auto_generated *bool
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000229
230 // Path to the dev nodes description file. This is only needed for building the ramdisk
231 // partition and should not be explicitly specified.
232 Dev_nodes_description_file *string `android:"path" blueprint:"mutated"`
Spandan Das71be42d2024-11-20 18:34:16 +0000233
234 // Additional dependencies used for building android products
235 Android_filesystem_deps AndroidFilesystemDeps
Spandan Dasc49b85e2025-01-10 00:51:25 +0000236
237 // Name of the output. Default is $(module_name).img
238 Stem *string
Jihoon Kang983dd882025-01-13 23:14:11 +0000239
240 // The size of the partition on the device. It will be a build error if this built partition
241 // image exceeds this size.
242 Partition_size *int64
Jihoon Kang6d08d922025-01-14 18:31:57 +0000243
244 // Whether to format f2fs and ext4 in a way that supports casefolding
245 Support_casefolding *bool
246
247 // Whether to format f2fs and ext4 in a way that supports project quotas
248 Support_project_quota *bool
249
250 // Whether to enable per-file compression in f2fs
251 Enable_compression *bool
Spandan Das71be42d2024-11-20 18:34:16 +0000252}
253
254type AndroidFilesystemDeps struct {
255 System *string
256 System_ext *string
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000257}
258
259// Additional properties required to generate erofs FS partitions.
260type ErofsProperties struct {
261 // Compressor and Compression level passed to mkfs.erofs. e.g. (lz4hc,9)
262 // Please see external/erofs-utils/README for complete documentation.
263 Compressor *string
264
265 // Used as --compress-hints for mkfs.erofs
266 Compress_hints *string `android:"path"`
267
268 Sparse *bool
Jiyong Park71baa762021-01-18 21:11:03 +0900269}
270
mrziwang1a6291f2024-11-07 14:29:25 -0800271// Additional properties required to generate f2fs FS partitions.
272type F2fsProperties struct {
273 Sparse *bool
274}
275
Spandan Das173256b2024-10-31 19:59:30 +0000276type LinkerConfigProperties struct {
277
278 // Build a linker.config.pb file
279 Gen_linker_config *bool
280
281 // List of files (in .json format) that will be converted to a linker config file (in .pb format).
282 // The linker config file be installed in the filesystem at /etc/linker.config.pb
283 Linker_config_srcs []string `android:"path"`
284}
285
Jiyong Park65c49f52020-11-24 14:23:26 +0900286// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
287// image. The filesystem images are expected to be mounted in the target device, which means the
288// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
289// The modules are placed in the filesystem image just like they are installed to the ordinary
290// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Cole Faust92ccbe22024-10-03 14:38:37 -0700291func FilesystemFactory() android.Module {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900292 module := &filesystem{}
Kiyoung Kim67118212024-11-07 13:23:44 +0900293 module.filesystemBuilder = module
Cole Faust2cfe6962024-09-17 11:31:14 -0700294 initFilesystemModule(module, module)
Jiyong Parkfa616132021-04-20 11:36:40 +0900295 return module
296}
297
Cole Faust2cfe6962024-09-17 11:31:14 -0700298func initFilesystemModule(module android.DefaultableModule, filesystemModule *filesystem) {
299 module.AddProperties(&filesystemModule.properties)
300 android.InitPackageModule(filesystemModule)
301 filesystemModule.PackagingBase.DepsCollectFirstTargetOnly = true
Jihoon Kang79196c52024-10-30 18:49:47 +0000302 filesystemModule.PackagingBase.AllowHighPriorityDeps = true
Jiyong Park6f0f6882020-11-12 13:14:30 +0900303 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900304 android.InitDefaultableModule(module)
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000305
306 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
307 filesystemModule.setDevNodesDescriptionProp()
308 })
Jiyong Park6f0f6882020-11-12 13:14:30 +0900309}
310
Jihoon Kang0d545b82024-10-11 00:21:57 +0000311type depTag struct {
Jiyong Park12a719c2021-01-07 15:31:24 +0900312 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900313 android.PackagingItemAlwaysDepTag
Jihoon Kang0d545b82024-10-11 00:21:57 +0000314}
315
316var dependencyTag = depTag{}
317
318type depTagWithVisibilityEnforcementBypass struct {
319 depTag
320}
321
Spandan Das71be42d2024-11-20 18:34:16 +0000322type interPartitionDepTag struct {
323 blueprint.BaseDependencyTag
324}
325
326var interPartitionDependencyTag = interPartitionDepTag{}
327
Jihoon Kang6da80752024-12-23 18:53:32 +0000328var interPartitionInstallDependencyTag = interPartitionDepTag{}
329
Jihoon Kang0d545b82024-10-11 00:21:57 +0000330var _ android.ExcludeFromVisibilityEnforcementTag = (*depTagWithVisibilityEnforcementBypass)(nil)
331
332func (t depTagWithVisibilityEnforcementBypass) ExcludeFromVisibilityEnforcement() {}
333
334var dependencyTagWithVisibilityEnforcementBypass = depTagWithVisibilityEnforcementBypass{}
Jiyong Park65b62242020-11-25 12:44:59 +0900335
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000336// ramdiskDevNodesDescription is the name of the filegroup module that provides the file that
337// contains the description of dev nodes added to the CPIO archive for the ramdisk partition.
338const ramdiskDevNodesDescription = "ramdisk_node_list"
339
340func (f *filesystem) setDevNodesDescriptionProp() {
341 if proptools.String(f.properties.Partition_name) == "ramdisk" {
342 f.properties.Dev_nodes_description_file = proptools.StringPtr(":" + ramdiskDevNodesDescription)
343 }
344}
345
Jiyong Park6f0f6882020-11-12 13:14:30 +0900346func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000347 if proptools.Bool(f.properties.Is_auto_generated) {
348 f.AddDeps(ctx, dependencyTagWithVisibilityEnforcementBypass)
349 } else {
350 f.AddDeps(ctx, dependencyTag)
351 }
Spandan Das71be42d2024-11-20 18:34:16 +0000352 if f.properties.Android_filesystem_deps.System != nil {
353 ctx.AddDependency(ctx.Module(), interPartitionDependencyTag, proptools.String(f.properties.Android_filesystem_deps.System))
354 }
355 if f.properties.Android_filesystem_deps.System_ext != nil {
356 ctx.AddDependency(ctx.Module(), interPartitionDependencyTag, proptools.String(f.properties.Android_filesystem_deps.System_ext))
357 }
Jihoon Kang6da80752024-12-23 18:53:32 +0000358 for _, partition := range f.properties.Include_files_of {
359 ctx.AddDependency(ctx.Module(), interPartitionInstallDependencyTag, partition)
360 }
Jiyong Park6f0f6882020-11-12 13:14:30 +0900361}
362
Jiyong Park11a65972021-02-01 21:09:38 +0900363type fsType int
364
365const (
366 ext4Type fsType = iota
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000367 erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800368 f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900369 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900370 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900371 unknown
372)
373
Spandan Das7a46f6c2024-10-14 18:41:18 +0000374func (fs fsType) IsUnknown() bool {
375 return fs == unknown
376}
377
Spandan Das8dd97102025-03-14 00:06:43 +0000378// Type string that build_image.py accepts.
379func (t fsType) String() string {
380 switch t {
381 // TODO(372522486): add more types like f2fs, erofs, etc.
382 case ext4Type:
383 return "ext4"
384 case erofsType:
385 return "erofs"
386 case f2fsType:
387 return "f2fs"
388 }
389 panic(fmt.Errorf("unsupported fs type %d", t))
390}
391
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000392type InstalledFilesStruct struct {
393 Txt android.Path
394 Json android.Path
395}
396
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000397type InstalledModuleInfo struct {
398 Name string
399 Variation string
400}
401
Cole Faust92ccbe22024-10-03 14:38:37 -0700402type FilesystemInfo struct {
Cole Faust44080412024-12-20 14:17:07 -0800403 // The built filesystem image
404 Output android.Path
Yu Liu0a37d422025-02-13 02:05:00 +0000405 // Returns the output file that is signed by avbtool. If this module is not signed, returns
406 // nil.
407 SignedOutputPath android.Path
Spandan Das1f0a5a12025-01-15 00:53:15 +0000408 // An additional hermetic filesystem image.
409 // e.g. this will contain inodes with pinned timestamps.
410 // This will be copied to target_files.zip
411 OutputHermetic android.Path
Cole Faust92ccbe22024-10-03 14:38:37 -0700412 // A text file containing the list of paths installed on the partition.
413 FileListFile android.Path
Cole Faust44080412024-12-20 14:17:07 -0800414 // The root staging directory used to build the output filesystem. If consuming this, make sure
415 // to add a dependency on the Output file, as you cannot add dependencies on directories
416 // in ninja.
417 RootDir android.Path
Cole Faustb36763e2025-02-18 15:21:44 -0800418 // Extra root directories that are also built into the partition. Currently only used for
419 // including the recovery partition files into the vendor_boot image.
420 ExtraRootDirs android.Paths
Cole Faust11fda332025-01-14 16:47:19 -0800421 // The rebased staging directory used to build the output filesystem. If consuming this, make
422 // sure to add a dependency on the Output file, as you cannot add dependencies on directories
423 // in ninja. In many cases this is the same as RootDir, only in the system partition is it
424 // different. There, it points to the "system" sub-directory of RootDir.
425 RebasedDir android.Path
Spandan Das33c9c472025-01-14 19:26:23 +0000426 // A text file with block data of the .img file
427 // This is an implicit output of `build_image`
428 MapFile android.Path
Cole Faust11fda332025-01-14 16:47:19 -0800429 // Name of the module that produced this FilesystemInfo origionally. (though it may be
430 // re-exported by super images or boot images)
431 ModuleName string
Cole Faust74ee4e02025-01-16 14:55:35 -0800432 // The property file generated by this module and passed to build_image.
433 // It's exported here so that system_other can reuse system's property file.
434 BuildImagePropFile android.Path
435 // Paths to all the tools referenced inside of the build image property file.
436 BuildImagePropFileDeps android.Paths
Cole Faustb8e280f2025-01-16 16:33:26 -0800437 // Packaging specs to be installed on the system_other image, for the initial boot's dexpreopt.
438 SpecsForSystemOther map[string]android.PackagingSpec
Cole Faust19fbb072025-01-30 18:19:29 -0800439
440 FullInstallPaths []FullInstallPathInfo
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000441
Jihoon Kangdd49f412025-03-07 01:30:43 +0000442 // Installed files dep set of this module and its dependency filesystem modules
443 InstalledFilesDepSet depset.DepSet[InstalledFilesStruct]
Spandan Dasd71af182025-02-12 18:03:29 +0000444
445 // Path to compress hints file for erofs filesystems
446 // This will be nil for other fileystems like ext4
447 ErofsCompressHints android.Path
Spandan Dasf12ff9b2025-02-12 22:27:43 +0000448
449 SelinuxFc android.Path
Spandan Dasdd262fb2025-02-13 00:15:59 +0000450
451 FilesystemConfig android.Path
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000452
453 Owners []InstalledModuleInfo
Spandan Das447a0ab2025-03-04 23:10:19 +0000454
Spandan Das38afe712025-03-05 23:27:55 +0000455 HasFsverity bool
Spandan Das227c9492025-03-17 20:14:00 +0000456
457 PropFileForMiscInfo android.Path
Spandan Das21643c62025-03-18 22:24:34 +0000458
459 // Additional avb and partition size information.
460 // `system_other` will use this information of `system` dep for misc_info.txt processing.
461 PartitionSize *int64
462 UseAvb bool
463 AvbAlgorithm string
464 AvbHashAlgorithm string
465 AvbKey android.Path
Cole Faust19fbb072025-01-30 18:19:29 -0800466}
467
468// FullInstallPathInfo contains information about the "full install" paths of all the files
469// inside this partition. The full install paths are the files installed in
470// out/target/product/<device>/<partition>. This is essentially legacy behavior, maintained for
471// tools like adb sync and adevice, but we should update them to query the build system for the
472// installed files no matter where they are.
473type FullInstallPathInfo struct {
474 // RequiresFullInstall tells us if the origional module did the install to FullInstallPath
475 // already. If it's false, the android_device module needs to emit the install rule.
476 RequiresFullInstall bool
477 // The "full install" paths for the files in this filesystem. This is the paths in the
478 // out/target/product/<device>/<partition> folder. They're not used by this filesystem,
479 // but can be depended on by the top-level android_device module to cause the staging
480 // directories to be built.
481 FullInstallPath android.InstallPath
482
483 // The file that's copied to FullInstallPath. May be nil if SymlinkTarget is set or IsDir is
484 // true.
485 SourcePath android.Path
486
487 // The target of the symlink, if this file is a symlink.
488 SymlinkTarget string
489
490 // If this file is a directory. Only used for empty directories, which are mostly mount points.
491 IsDir bool
Cole Faust92ccbe22024-10-03 14:38:37 -0700492}
493
494var FilesystemProvider = blueprint.NewProvider[FilesystemInfo]()
495
Yu Liu71f1ea32025-02-26 23:39:20 +0000496type FilesystemDefaultsInfo struct{}
Yu Liufc8d5c12025-01-09 00:19:06 +0000497
498var FilesystemDefaultsInfoProvider = blueprint.NewProvider[FilesystemDefaultsInfo]()
499
Spandan Das7a46f6c2024-10-14 18:41:18 +0000500func GetFsTypeFromString(ctx android.EarlyModuleContext, typeStr string) fsType {
Jiyong Park11a65972021-02-01 21:09:38 +0900501 switch typeStr {
502 case "ext4":
503 return ext4Type
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000504 case "erofs":
505 return erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800506 case "f2fs":
507 return f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900508 case "compressed_cpio":
509 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900510 case "cpio":
511 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900512 default:
Jiyong Park11a65972021-02-01 21:09:38 +0900513 return unknown
514 }
515}
516
Spandan Das7a46f6c2024-10-14 18:41:18 +0000517func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
518 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
519 fsType := GetFsTypeFromString(ctx, typeStr)
520 if fsType == unknown {
521 ctx.PropertyErrorf("type", "%q not supported", typeStr)
522 }
523 return fsType
524}
525
Jiyong Park65c49f52020-11-24 14:23:26 +0900526func (f *filesystem) installFileName() string {
Spandan Dasc49b85e2025-01-10 00:51:25 +0000527 return proptools.StringDefault(f.properties.Stem, f.BaseModuleName()+".img")
Jiyong Park65c49f52020-11-24 14:23:26 +0900528}
529
Inseob Kim53391842024-03-29 17:44:07 +0900530func (f *filesystem) partitionName() string {
531 return proptools.StringDefault(f.properties.Partition_name, f.Name())
532}
533
Kiyoung Kim67118212024-11-07 13:23:44 +0900534func (f *filesystem) FilterPackagingSpec(ps android.PackagingSpec) bool {
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000535 // Filesystem module respects the installation semantic. A PackagingSpec from a module with
536 // IsSkipInstall() is skipped.
Cole Faust76a6e952024-11-07 16:56:45 -0800537 if ps.SkipInstall() {
538 return false
Spandan Das6d056502024-10-21 15:40:32 +0000539 }
Cole Faust0d3fd562025-01-31 13:17:58 -0800540 // "apex" is a fake partition used to install files in out/target/product/<device>/apex/.
541 // Don't include these files in the partition. We should also look into removing the following
542 // TODO to check the PackagingSpec's partition against this filesystem's partition for all
543 // modules, not just autogenerated ones, which will fix this as well.
544 if ps.Partition() == "apex" {
545 return false
546 }
Cole Faust76a6e952024-11-07 16:56:45 -0800547 if proptools.Bool(f.properties.Is_auto_generated) { // TODO (spandandas): Remove this.
548 pt := f.PartitionType()
Cole Faustc88cff12024-11-12 13:24:05 -0800549 return ps.Partition() == pt || strings.HasPrefix(ps.Partition(), pt+"/")
Cole Faust76a6e952024-11-07 16:56:45 -0800550 }
551 return true
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000552}
553
Inseob Kim3c0a0422024-11-05 17:21:37 +0900554func (f *filesystem) ModifyPackagingSpec(ps *android.PackagingSpec) {
Cole Faustc88cff12024-11-12 13:24:05 -0800555 // Sometimes, android.modulePartition() returns a path with >1 path components.
556 // This makes the partition field of packagingSpecs have multiple components, like
557 // "system/product". Right now, the filesystem module doesn't look at the partition field
558 // when deciding what path to install the file under, only the RelPathInPackage field, so
559 // we move the later path components from partition to relPathInPackage. This should probably
560 // be revisited in the future.
561 prefix := f.PartitionType() + "/"
562 if strings.HasPrefix(ps.Partition(), prefix) {
563 subPartition := strings.TrimPrefix(ps.Partition(), prefix)
564 ps.SetPartition(f.PartitionType())
565 ps.SetRelPathInPackage(filepath.Join(subPartition, ps.RelPathInPackage()))
566 }
Inseob Kim3c0a0422024-11-05 17:21:37 +0900567}
568
Jihoon Kangdd49f412025-03-07 01:30:43 +0000569func buildInstalledFiles(ctx android.ModuleContext, partition string, rootDir android.Path, image android.Path) InstalledFilesStruct {
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000570 fileName := "installed-files"
571 if len(partition) > 0 {
572 fileName += fmt.Sprintf("-%s", partition)
573 }
Jihoon Kangdd49f412025-03-07 01:30:43 +0000574 txt := android.PathForModuleOut(ctx, fmt.Sprintf("%s.txt", fileName))
575 json := android.PathForModuleOut(ctx, fmt.Sprintf("%s.json", fileName))
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000576
577 ctx.Build(pctx, android.BuildParams{
578 Rule: installedFilesJsonRule,
579 Implicit: image,
580 Output: json,
581 Description: "Installed file list json",
582 Args: map[string]string{
583 "rootDir": rootDir.String(),
584 },
585 })
586
587 ctx.Build(pctx, android.BuildParams{
588 Rule: installedFilesTxtRule,
589 Input: json,
590 Output: txt,
591 Description: "Installed file list txt",
592 })
593
Jihoon Kangdd49f412025-03-07 01:30:43 +0000594 return InstalledFilesStruct{
595 Txt: txt,
596 Json: json,
597 }
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000598}
599
Jiyong Park6f0f6882020-11-12 13:14:30 +0900600func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900601 validatePartitionType(ctx, f)
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000602 if f.filesystemBuilder.ShouldUseVintfFragmentModuleOnly() {
603 f.validateVintfFragments(ctx)
604 }
Jihoon Kang6da80752024-12-23 18:53:32 +0000605
606 if len(f.properties.Include_files_of) > 0 && !android.InList(f.fsType(ctx), []fsType{compressedCpioType, cpioType}) {
607 ctx.PropertyErrorf("include_files_of", "include_files_of is only supported for cpio and compressed cpio filesystem types.")
608 }
609
Cole Faust62cfaeb2025-01-15 18:06:40 -0800610 rootDir := android.PathForModuleOut(ctx, f.rootDirString()).OutputPath
611 rebasedDir := rootDir
612 if f.properties.Base_dir != nil {
613 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
614 }
615 builder := android.NewRuleBuilder(pctx, ctx)
616
617 // Wipe the root dir to get rid of leftover files from prior builds
618 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
619 specs := f.gatherFilteredPackagingSpecs(ctx)
Cole Faust62cfaeb2025-01-15 18:06:40 -0800620
Cole Faust19fbb072025-01-30 18:19:29 -0800621 var fullInstallPaths []FullInstallPathInfo
Cole Faust5db2f3e2025-02-19 12:49:37 -0800622 for _, specRel := range android.SortedKeys(specs) {
623 spec := specs[specRel]
Cole Faust19fbb072025-01-30 18:19:29 -0800624 fullInstallPaths = append(fullInstallPaths, FullInstallPathInfo{
625 FullInstallPath: spec.FullInstallPath(),
626 RequiresFullInstall: spec.RequiresFullInstall(),
627 SourcePath: spec.SrcPath(),
628 SymlinkTarget: spec.ToGob().SymlinkTarget,
629 })
630 }
631
632 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
633 f.buildNonDepsFiles(ctx, builder, rootDir, rebasedDir, &fullInstallPaths)
634 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir, &fullInstallPaths)
635 f.buildEventLogtagsFile(ctx, builder, rebasedDir, &fullInstallPaths)
636 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir, &fullInstallPaths)
637 f.filesystemBuilder.BuildLinkerConfigFile(ctx, builder, rebasedDir, &fullInstallPaths)
Spandan Das5ef1a9c2025-02-11 18:50:17 +0000638 // Assemeble the staging dir and output a timestamp
639 builder.Command().Text("touch").Output(f.fileystemStagingDirTimestamp(ctx))
640 builder.Build("assemble_filesystem_staging_dir", fmt.Sprintf("Assemble filesystem staging dir %s", f.BaseModuleName()))
Cole Faust62cfaeb2025-01-15 18:06:40 -0800641
Spandan Das5ef1a9c2025-02-11 18:50:17 +0000642 // Create a new rule builder for build_image
643 builder = android.NewRuleBuilder(pctx, ctx)
Spandan Das33c9c472025-01-14 19:26:23 +0000644 var mapFile android.Path
Spandan Das5ef1a9c2025-02-11 18:50:17 +0000645 var outputHermetic android.WritablePath
Cole Faust74ee4e02025-01-16 14:55:35 -0800646 var buildImagePropFile android.Path
647 var buildImagePropFileDeps android.Paths
Cole Faustb36763e2025-02-18 15:21:44 -0800648 var extraRootDirs android.Paths
Spandan Das227c9492025-03-17 20:14:00 +0000649 var propFileForMiscInfo android.Path
Jiyong Park11a65972021-02-01 21:09:38 +0900650 switch f.fsType(ctx) {
mrziwang1a6291f2024-11-07 14:29:25 -0800651 case ext4Type, erofsType, f2fsType:
Spandan Das5ef1a9c2025-02-11 18:50:17 +0000652 buildImagePropFile, buildImagePropFileDeps = f.buildPropFile(ctx)
Spandan Das227c9492025-03-17 20:14:00 +0000653 propFileForMiscInfo = f.buildPropFileForMiscInfo(ctx)
Spandan Das5ef1a9c2025-02-11 18:50:17 +0000654 output := android.PathForModuleOut(ctx, f.installFileName())
655 f.buildImageUsingBuildImage(ctx, builder, buildImageParams{rootDir, buildImagePropFile, buildImagePropFileDeps, output})
656 f.output = output
657 // Create the hermetic img file using a separate rule builder so that it can be built independently
658 hermeticBuilder := android.NewRuleBuilder(pctx, ctx)
659 outputHermetic = android.PathForModuleOut(ctx, "for_target_files", f.installFileName())
660 propFileHermetic := f.propFileForHermeticImg(ctx, hermeticBuilder, buildImagePropFile)
661 f.buildImageUsingBuildImage(ctx, hermeticBuilder, buildImageParams{rootDir, propFileHermetic, buildImagePropFileDeps, outputHermetic})
Spandan Das33c9c472025-01-14 19:26:23 +0000662 mapFile = f.getMapFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900663 case compressedCpioType:
Cole Faustb36763e2025-02-18 15:21:44 -0800664 f.output, extraRootDirs = f.buildCpioImage(ctx, builder, rootDir, true)
Jiyong Park837cdb22021-02-05 00:17:14 +0900665 case cpioType:
Cole Faustb36763e2025-02-18 15:21:44 -0800666 f.output, extraRootDirs = f.buildCpioImage(ctx, builder, rootDir, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900667 default:
668 return
669 }
670
671 f.installDir = android.PathForModuleInstall(ctx, "etc")
672 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
mrziwang555d1332024-06-07 11:15:33 -0700673 ctx.SetOutputFiles([]android.Path{f.output}, "")
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900674
Jihoon Kang6da80752024-12-23 18:53:32 +0000675 if f.partitionName() == "recovery" {
676 rootDir = rootDir.Join(ctx, "root")
677 }
678
Cole Faust4e9f5922024-11-13 16:09:23 -0800679 fileListFile := android.PathForModuleOut(ctx, "fileList")
680 android.WriteFileRule(ctx, fileListFile, f.installedFilesList())
Cole Faust92ccbe22024-10-03 14:38:37 -0700681
Jihoon Kang52e53c62025-03-07 00:05:49 +0000682 var partitionNameForInstalledFiles string
683 switch f.partitionName() {
684 case "system":
685 partitionNameForInstalledFiles = ""
686 case "vendor_ramdisk":
687 partitionNameForInstalledFiles = "vendor-ramdisk"
688 default:
689 partitionNameForInstalledFiles = f.partitionName()
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000690 }
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000691
Spandan Dasd71af182025-02-12 18:03:29 +0000692 var erofsCompressHints android.Path
693 if f.properties.Erofs.Compress_hints != nil {
694 erofsCompressHints = android.PathForModuleSrc(ctx, *f.properties.Erofs.Compress_hints)
695 }
696
Spandan Das33c9c472025-01-14 19:26:23 +0000697 fsInfo := FilesystemInfo{
Yu Liu0a37d422025-02-13 02:05:00 +0000698 Output: f.OutputPath(),
699 SignedOutputPath: f.SignedOutputPath(),
Cole Faust74ee4e02025-01-16 14:55:35 -0800700 OutputHermetic: outputHermetic,
701 FileListFile: fileListFile,
702 RootDir: rootDir,
Cole Faustb36763e2025-02-18 15:21:44 -0800703 ExtraRootDirs: extraRootDirs,
Cole Faust74ee4e02025-01-16 14:55:35 -0800704 RebasedDir: rebasedDir,
705 MapFile: mapFile,
706 ModuleName: ctx.ModuleName(),
707 BuildImagePropFile: buildImagePropFile,
708 BuildImagePropFileDeps: buildImagePropFileDeps,
Cole Faustb8e280f2025-01-16 16:33:26 -0800709 SpecsForSystemOther: f.systemOtherFiles(ctx),
Cole Faust19fbb072025-01-30 18:19:29 -0800710 FullInstallPaths: fullInstallPaths,
Jihoon Kangdd49f412025-03-07 01:30:43 +0000711 InstalledFilesDepSet: depset.New(
712 depset.POSTORDER,
713 []InstalledFilesStruct{buildInstalledFiles(ctx, partitionNameForInstalledFiles, rootDir, f.output)},
714 includeFilesInstalledFiles(ctx),
715 ),
Spandan Das227c9492025-03-17 20:14:00 +0000716 ErofsCompressHints: erofsCompressHints,
717 SelinuxFc: f.selinuxFc,
718 FilesystemConfig: f.generateFilesystemConfig(ctx, rootDir, rebasedDir),
719 Owners: f.gatherOwners(specs),
720 HasFsverity: f.properties.Fsverity.Inputs.GetOrDefault(ctx, nil) != nil,
721 PropFileForMiscInfo: propFileForMiscInfo,
Spandan Das21643c62025-03-18 22:24:34 +0000722 PartitionSize: f.properties.Partition_size,
723 }
724 if proptools.Bool(f.properties.Use_avb) {
725 fsInfo.UseAvb = true
726 fsInfo.AvbAlgorithm = proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
727 fsInfo.AvbHashAlgorithm = proptools.StringDefault(f.properties.Avb_hash_algorithm, "sha256")
728 if f.properties.Avb_private_key != nil {
729 fsInfo.AvbKey = android.PathForModuleSrc(ctx, *f.properties.Avb_private_key)
730 }
Spandan Das1f0a5a12025-01-15 00:53:15 +0000731 }
Spandan Das33c9c472025-01-14 19:26:23 +0000732
733 android.SetProvider(ctx, FilesystemProvider, fsInfo)
Spandan Das3ec6d062025-01-09 19:37:47 +0000734
Yu Liu71f1ea32025-02-26 23:39:20 +0000735 android.SetProvider(ctx, android.PartitionTypeInfoProvider, android.PartitionTypeInfo{
736 PartitionType: f.PartitionType(),
737 })
738
Cole Faust4e9f5922024-11-13 16:09:23 -0800739 f.fileListFile = fileListFile
Cole Faust92ccbe22024-10-03 14:38:37 -0700740
741 if proptools.Bool(f.properties.Unchecked_module) {
742 ctx.UncheckedModule()
743 }
Jihoon Kang2f0d1932025-01-17 19:22:44 +0000744
745 f.setVbmetaPartitionProvider(ctx)
Wei Li7b8455f2025-03-05 16:05:51 -0800746
747 // Dump metadata that can not be done in android/compliance-metadata.go
748 complianceMetadataInfo := ctx.ComplianceMetadataInfo()
749 filesContained := make([]string, 0, len(fullInstallPaths))
750 for _, file := range fullInstallPaths {
751 filesContained = append(filesContained, file.FullInstallPath.String())
752 }
753 complianceMetadataInfo.SetFilesContained(filesContained)
Jihoon Kang2f0d1932025-01-17 19:22:44 +0000754}
755
Spandan Das5ef1a9c2025-02-11 18:50:17 +0000756func (f *filesystem) fileystemStagingDirTimestamp(ctx android.ModuleContext) android.WritablePath {
757 return android.PathForModuleOut(ctx, "staging_dir.timestamp")
758}
759
Spandan Dasdd262fb2025-02-13 00:15:59 +0000760func (f *filesystem) generateFilesystemConfig(ctx android.ModuleContext, rootDir android.Path, rebasedDir android.Path) android.Path {
761 rootDirString := rootDir.String()
762 prefix := f.partitionName() + "/"
763 if f.partitionName() == "system" {
764 rootDirString = rebasedDir.String()
765 }
766 if f.partitionName() == "ramdisk" || f.partitionName() == "recovery" {
767 // Hardcoded to match make behavior.
768 // https://cs.android.com/android/_/android/platform/build/+/2a0ef42a432d4da00201e8eb7697dcaa68fd2389:core/Makefile;l=6957-6962;drc=9ea8ad9232cef4d0a24d70133b1b9d2ce2defe5f;bpv=1;bpt=0
769 prefix = ""
770 }
771 out := android.PathForModuleOut(ctx, "filesystem_config.txt")
772 ctx.Build(pctx, android.BuildParams{
773 Rule: fsConfigRule,
774 Input: f.fileystemStagingDirTimestamp(ctx), // assemble the staging directory
775 Output: out,
776 Args: map[string]string{
777 "rootDir": rootDirString,
778 "prefix": prefix,
779 },
780 })
781 return out
782}
783
Jihoon Kang2f0d1932025-01-17 19:22:44 +0000784func (f *filesystem) setVbmetaPartitionProvider(ctx android.ModuleContext) {
785 var extractedPublicKey android.ModuleOutPath
786 if f.properties.Avb_private_key != nil {
787 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
788 extractedPublicKey = android.PathForModuleOut(ctx, f.partitionName()+".avbpubkey")
789 ctx.Build(pctx, android.BuildParams{
790 Rule: extractPublicKeyRule,
791 Input: key,
792 Output: extractedPublicKey,
793 })
794 }
795
796 var ril int
797 if f.properties.Rollback_index_location != nil {
798 ril = proptools.Int(f.properties.Rollback_index_location)
799 }
800
801 android.SetProvider(ctx, vbmetaPartitionProvider, vbmetaPartitionInfo{
802 Name: f.partitionName(),
803 RollbackIndexLocation: ril,
804 PublicKey: extractedPublicKey,
805 Output: f.output,
806 })
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900807}
808
Spandan Das33c9c472025-01-14 19:26:23 +0000809func (f *filesystem) getMapFile(ctx android.ModuleContext) android.WritablePath {
810 // create the filepath by replacing the extension of the corresponding img file
811 return android.PathForModuleOut(ctx, f.installFileName()).ReplaceExtension(ctx, "map")
812}
813
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000814func (f *filesystem) validateVintfFragments(ctx android.ModuleContext) {
815 visitedModule := map[string]bool{}
816 packagingSpecs := f.gatherFilteredPackagingSpecs(ctx)
817
818 moduleInFileSystem := func(mod android.Module) bool {
819 for _, ps := range android.OtherModuleProviderOrDefault(
820 ctx, mod, android.InstallFilesProvider).PackagingSpecs {
821 if _, ok := packagingSpecs[ps.RelPathInPackage()]; ok {
822 return true
823 }
824 }
825 return false
826 }
827
828 ctx.WalkDeps(func(child, parent android.Module) bool {
829 if visitedModule[child.Name()] {
830 return false
831 }
832 if !moduleInFileSystem(child) {
833 visitedModule[child.Name()] = true
834 return true
835 }
836 if vintfFragments := child.VintfFragments(ctx); vintfFragments != nil {
837 ctx.PropertyErrorf(
838 "vintf_fragments",
839 "Module %s is referenced by soong-defined filesystem %s with property vintf_fragments(%s) in use."+
840 " Use vintf_fragment_modules property instead.",
841 child.Name(),
842 f.BaseModuleName(),
843 strings.Join(vintfFragments, ", "),
844 )
845 }
846 visitedModule[child.Name()] = true
847 return true
848 })
849}
850
Cole Faust4e9f5922024-11-13 16:09:23 -0800851func (f *filesystem) appendToEntry(ctx android.ModuleContext, installedFile android.Path) {
Spandan Das420e16a2024-12-11 18:10:52 +0000852 partitionBaseDir := android.PathForModuleOut(ctx, f.rootDirString(), proptools.String(f.properties.Base_dir)).String() + "/"
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900853
854 relPath, inTargetPartition := strings.CutPrefix(installedFile.String(), partitionBaseDir)
855 if inTargetPartition {
856 f.entries = append(f.entries, relPath)
857 }
858}
859
860func (f *filesystem) installedFilesList() string {
861 installedFilePaths := android.FirstUniqueStrings(f.entries)
862 slices.Sort(installedFilePaths)
863
864 return strings.Join(installedFilePaths, "\n")
Jiyong Park11a65972021-02-01 21:09:38 +0900865}
866
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900867func validatePartitionType(ctx android.ModuleContext, p partition) {
868 if !android.InList(p.PartitionType(), validPartitions) {
869 ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, p.PartitionType())
870 }
871
Yu Liufc8d5c12025-01-09 00:19:06 +0000872 ctx.VisitDirectDepsProxyWithTag(android.DefaultsDepTag, func(m android.ModuleProxy) {
Yu Liu71f1ea32025-02-26 23:39:20 +0000873 if _, ok := android.OtherModuleProvider(ctx, m, FilesystemDefaultsInfoProvider); ok {
874 partitionInfo := android.OtherModuleProviderOrDefault(ctx, m, android.PartitionTypeInfoProvider)
875 if p.PartitionType() != partitionInfo.PartitionType {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900876 ctx.PropertyErrorf("partition_type",
877 "%s doesn't match with the partition type %s of the filesystem default module %s",
Yu Liu71f1ea32025-02-26 23:39:20 +0000878 p.PartitionType(), partitionInfo.PartitionType, m.Name())
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900879 }
880 }
881 })
882}
883
Cole Faust3b806d32024-03-11 15:15:03 -0700884// Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files
885// already in `rootDir`.
Cole Faust19fbb072025-01-30 18:19:29 -0800886func (f *filesystem) buildNonDepsFiles(
887 ctx android.ModuleContext,
888 builder *android.RuleBuilder,
889 rootDir android.OutputPath,
890 rebasedDir android.OutputPath,
891 fullInstallPaths *[]FullInstallPathInfo,
892) {
893 rebasedPrefix, err := filepath.Rel(rootDir.String(), rebasedDir.String())
894 if err != nil || strings.HasPrefix(rebasedPrefix, "../") {
895 panic("rebasedDir could not be made relative to rootDir")
896 }
897 if !strings.HasSuffix(rebasedPrefix, "/") {
898 rebasedPrefix += "/"
899 }
900 if rebasedPrefix == "./" {
901 rebasedPrefix = ""
902 }
903
Inseob Kim14199b02021-02-09 21:18:31 +0900904 // create dirs and symlinks
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700905 for _, dir := range f.properties.Dirs.GetOrDefault(ctx, nil) {
Inseob Kim14199b02021-02-09 21:18:31 +0900906 // OutputPath.Join verifies dir
907 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
Cole Faust19fbb072025-01-30 18:19:29 -0800908 // Only add the fullInstallPath logic for files in the rebased dir. The root dir
909 // is harder to install to.
910 if strings.HasPrefix(dir, rebasedPrefix) {
911 *fullInstallPaths = append(*fullInstallPaths, FullInstallPathInfo{
912 FullInstallPath: android.PathForModuleInPartitionInstall(ctx, f.PartitionType(), strings.TrimPrefix(dir, rebasedPrefix)),
913 IsDir: true,
914 })
915 }
Inseob Kim14199b02021-02-09 21:18:31 +0900916 }
917
918 for _, symlink := range f.properties.Symlinks {
919 name := strings.TrimSpace(proptools.String(symlink.Name))
920 target := strings.TrimSpace(proptools.String(symlink.Target))
921
922 if name == "" {
923 ctx.PropertyErrorf("symlinks", "Name can't be empty")
924 continue
925 }
926
927 if target == "" {
928 ctx.PropertyErrorf("symlinks", "Target can't be empty")
929 continue
930 }
931
932 // OutputPath.Join verifies name. don't need to verify target.
933 dst := rootDir.Join(ctx, name)
Cole Faust3b806d32024-03-11 15:15:03 -0700934 builder.Command().Textf("(! [ -e %s -o -L %s ] || (echo \"%s already exists from an earlier stage of the build\" && exit 1))", dst, dst, dst)
Inseob Kim14199b02021-02-09 21:18:31 +0900935 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
936 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900937 f.appendToEntry(ctx, dst)
Wei Li7b8455f2025-03-05 16:05:51 -0800938 // Add the fullInstallPath logic for files in the rebased dir, and for non-rebased files in "system" partition
939 // the fullInstallPath is changed to "root" which aligns to the behavior in Make.
940 if f.PartitionType() == "system" {
941 installPath := android.PathForModuleInPartitionInstall(ctx, f.PartitionType(), strings.TrimPrefix(name, rebasedPrefix))
942 if !strings.HasPrefix(name, rebasedPrefix) {
943 installPath = android.PathForModuleInPartitionInstall(ctx, "root", name)
944 }
Cole Faust19fbb072025-01-30 18:19:29 -0800945 *fullInstallPaths = append(*fullInstallPaths, FullInstallPathInfo{
Wei Li7b8455f2025-03-05 16:05:51 -0800946 FullInstallPath: installPath,
Cole Faust19fbb072025-01-30 18:19:29 -0800947 SymlinkTarget: target,
948 })
Wei Li7b8455f2025-03-05 16:05:51 -0800949 } else {
950 if strings.HasPrefix(name, rebasedPrefix) {
951 *fullInstallPaths = append(*fullInstallPaths, FullInstallPathInfo{
952 FullInstallPath: android.PathForModuleInPartitionInstall(ctx, f.PartitionType(), strings.TrimPrefix(name, rebasedPrefix)),
953 SymlinkTarget: target,
954 })
955 }
Cole Faust19fbb072025-01-30 18:19:29 -0800956 }
Inseob Kim14199b02021-02-09 21:18:31 +0900957 }
Jihoon Kang89e8a692024-12-18 19:28:33 +0000958
959 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=2835;drc=b186569ef00ff2f2a1fab28aedc75ebc32bcd67b
960 if f.partitionName() == "recovery" {
961 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, "root/linkerconfig").String())
962 builder.Command().Text("touch").Text(rootDir.Join(ctx, "root/linkerconfig/ld.config.txt").String())
963 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900964}
965
Inseob Kim33f95a92024-07-11 15:44:49 +0900966func (f *filesystem) copyPackagingSpecs(ctx android.ModuleContext, builder *android.RuleBuilder, specs map[string]android.PackagingSpec, rootDir, rebasedDir android.WritablePath) []string {
967 rootDirSpecs := make(map[string]android.PackagingSpec)
968 rebasedDirSpecs := make(map[string]android.PackagingSpec)
969
970 for rel, spec := range specs {
971 if spec.Partition() == "root" {
972 rootDirSpecs[rel] = spec
973 } else {
974 rebasedDirSpecs[rel] = spec
975 }
976 }
977
978 dirsToSpecs := make(map[android.WritablePath]map[string]android.PackagingSpec)
979 dirsToSpecs[rootDir] = rootDirSpecs
980 dirsToSpecs[rebasedDir] = rebasedDirSpecs
981
Cole Fauste3845052025-02-13 12:45:35 -0800982 // Preserve timestamps for adb sync, so that this staging dir file matches the timestamp in the
983 // out/target/product staging directory.
984 return f.CopySpecsToDirs(ctx, builder, dirsToSpecs, true)
Inseob Kim33f95a92024-07-11 15:44:49 +0900985}
986
Spandan Das420e16a2024-12-11 18:10:52 +0000987func (f *filesystem) rootDirString() string {
988 return f.partitionName()
989}
990
Spandan Das5ef1a9c2025-02-11 18:50:17 +0000991type buildImageParams struct {
992 // inputs
993 rootDir android.OutputPath
994 propFile android.Path
995 toolDeps android.Paths
996 // outputs
997 output android.WritablePath
998}
999
Cole Faust62cfaeb2025-01-15 18:06:40 -08001000func (f *filesystem) buildImageUsingBuildImage(
1001 ctx android.ModuleContext,
1002 builder *android.RuleBuilder,
Spandan Das5ef1a9c2025-02-11 18:50:17 +00001003 params buildImageParams) {
Nikita Ioffe519015f2022-12-23 15:36:29 +00001004 // run host_init_verifier
1005 // Ideally we should have a concept of pluggable linters that verify the generated image.
1006 // While such concept is not implement this will do.
1007 // TODO(b/263574231): substitute with pluggable linter.
1008 builder.Command().
1009 BuiltTool("host_init_verifier").
Spandan Das5ef1a9c2025-02-11 18:50:17 +00001010 FlagWithArg("--out_system=", params.rootDir.String()+"/system")
Cole Fauste1676122024-12-03 17:32:25 -08001011
1012 // Most of the time, if build_image were to call a host tool, it accepts the path to the
1013 // host tool in a field in the prop file. However, it doesn't have that option for fec, which
1014 // it expects to just be on the PATH. Add fec to the PATH.
1015 fec := ctx.Config().HostToolPath(ctx, "fec")
1016 pathToolDirs := []string{filepath.Dir(fec.String())}
1017
Cole Fauste1676122024-12-03 17:32:25 -08001018 builder.Command().
1019 Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")).
1020 BuiltTool("build_image").
Spandan Das5ef1a9c2025-02-11 18:50:17 +00001021 Text(params.rootDir.String()). // input directory
1022 Input(params.propFile).
1023 Implicits(params.toolDeps).
Cole Fauste1676122024-12-03 17:32:25 -08001024 Implicit(fec).
Spandan Das5ef1a9c2025-02-11 18:50:17 +00001025 Implicit(f.fileystemStagingDirTimestamp(ctx)). // assemble the staging directory
1026 Output(params.output).
1027 Text(params.rootDir.String()) // directory where to find fs_config_files|dirs
Spandan Das1f0a5a12025-01-15 00:53:15 +00001028
Jihoon Kang983dd882025-01-13 23:14:11 +00001029 if f.properties.Partition_size != nil {
Spandan Das5ef1a9c2025-02-11 18:50:17 +00001030 assertMaxImageSize(builder, params.output, *f.properties.Partition_size, false)
Jihoon Kang983dd882025-01-13 23:14:11 +00001031 }
1032
Jiyong Park6f0f6882020-11-12 13:14:30 +09001033 // rootDir is not deleted. Might be useful for quick inspection.
Spandan Das5ef1a9c2025-02-11 18:50:17 +00001034 builder.Build("build_"+params.output.String(), fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
1035}
Jiyong Park65c49f52020-11-24 14:23:26 +09001036
Spandan Das5ef1a9c2025-02-11 18:50:17 +00001037func (f *filesystem) propFileForHermeticImg(ctx android.ModuleContext, builder *android.RuleBuilder, inputPropFile android.Path) android.Path {
1038 propFilePinnedTimestamp := android.PathForModuleOut(ctx, "for_target_files", "prop")
1039 builder.Command().Textf("cat").Input(inputPropFile).Flag(">").Output(propFilePinnedTimestamp).
1040 Textf(" && echo use_fixed_timestamp=true >> %s", propFilePinnedTimestamp).
1041 Textf(" && echo block_list=%s >> %s", f.getMapFile(ctx).String(), propFilePinnedTimestamp) // mapfile will be an implicit output
1042 builder.Command().Text("touch").Output(f.getMapFile(ctx))
1043 return propFilePinnedTimestamp
Jiyong Park65c49f52020-11-24 14:23:26 +09001044}
1045
Cole Faust4e9f5922024-11-13 16:09:23 -08001046func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.Path {
Inseob Kimcc8e5362021-02-03 14:05:24 +09001047 builder := android.NewRuleBuilder(pctx, ctx)
1048 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
1049 builder.Command().BuiltTool("sefcontext_compile").
1050 FlagWithOutput("-o ", fcBin).
1051 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
1052 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
Cole Faust4e9f5922024-11-13 16:09:23 -08001053 return fcBin
Inseob Kimcc8e5362021-02-03 14:05:24 +09001054}
1055
Cole Faust4e9f5922024-11-13 16:09:23 -08001056func (f *filesystem) buildPropFile(ctx android.ModuleContext) (android.Path, android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +09001057 var deps android.Paths
Cole Fauste03ab892025-01-17 13:55:04 -08001058 var lines []string
Jiyong Park72678312021-01-18 17:29:49 +09001059 addStr := func(name string, value string) {
Cole Fauste03ab892025-01-17 13:55:04 -08001060 lines = append(lines, fmt.Sprintf("%s=%s", name, value))
Jiyong Park72678312021-01-18 17:29:49 +09001061 }
1062 addPath := func(name string, path android.Path) {
Cole Faustcec230a2024-03-07 15:51:12 -08001063 addStr(name, path.String())
Jiyong Park72678312021-01-18 17:29:49 +09001064 deps = append(deps, path)
1065 }
1066
Spandan Das8dd97102025-03-14 00:06:43 +00001067 addStr("fs_type", f.fsType(ctx).String())
Inseob Kim376d72f2023-11-01 15:40:25 +09001068 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +09001069 addStr("use_dynamic_partition_size", "true")
1070 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
1071 // b/177813163 deps of the host tools have to be added. Remove this.
1072 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
1073 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
1074 }
1075
Jiyong Park71baa762021-01-18 21:11:03 +09001076 if proptools.Bool(f.properties.Use_avb) {
1077 addStr("avb_hashtree_enable", "true")
1078 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
1079 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
1080 addStr("avb_algorithm", algorithm)
Cole Fauste1676122024-12-03 17:32:25 -08001081 if f.properties.Avb_private_key != nil {
1082 key := android.PathForModuleSrc(ctx, *f.properties.Avb_private_key)
1083 addPath("avb_key_path", key)
1084 }
Inseob Kim53391842024-03-29 17:44:07 +09001085 addStr("partition_name", f.partitionName())
Spandan Das72128502025-03-13 23:53:02 +00001086 addStr("avb_add_hashtree_footer_args", f.getAvbAddHashtreeFooterArgs(ctx))
Jiyong Park71baa762021-01-18 21:11:03 +09001087 }
1088
Cole Faust0d467052024-12-04 17:19:19 -08001089 if f.properties.File_contexts != nil && f.properties.Precompiled_file_contexts != nil {
1090 ctx.ModuleErrorf("file_contexts and precompiled_file_contexts cannot both be set")
1091 } else if f.properties.File_contexts != nil {
Spandan Dasf12ff9b2025-02-12 22:27:43 +00001092 f.selinuxFc = f.buildFileContexts(ctx)
Cole Faust0d467052024-12-04 17:19:19 -08001093 } else if f.properties.Precompiled_file_contexts != nil {
Spandan Dasf12ff9b2025-02-12 22:27:43 +00001094 f.selinuxFc = android.PathForModuleSrc(ctx, *f.properties.Precompiled_file_contexts)
1095 }
1096 if f.selinuxFc != nil {
1097 addPath("selinux_fc", f.selinuxFc)
Inseob Kimcc8e5362021-02-03 14:05:24 +09001098 }
Jooyung Han65f402b2022-04-21 14:24:04 +09001099 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
1100 addStr("timestamp", timestamp)
Spandan Dasa0ddc512025-01-06 20:23:55 +00001101 } else if ctx.Config().Getenv("USE_FIXED_TIMESTAMP_IMG_FILES") == "true" {
1102 addStr("use_fixed_timestamp", "true")
Jooyung Han65f402b2022-04-21 14:24:04 +09001103 }
Spandan Dasa0ddc512025-01-06 20:23:55 +00001104
Jooyung Han65f402b2022-04-21 14:24:04 +09001105 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
1106 addStr("uuid", uuid)
1107 addStr("hash_seed", uuid)
1108 }
mrziwang1a6291f2024-11-07 14:29:25 -08001109
Jihoon Kang40551e62025-01-14 21:55:08 +00001110 // Disable sparse only when partition size is not defined. disable_sparse has the same
1111 // effect as <partition name>_disable_sparse.
1112 if f.properties.Partition_size == nil {
1113 addStr("disable_sparse", "true")
1114 }
Cole Faust43a52c72024-11-26 12:46:08 -08001115
mrziwang1a6291f2024-11-07 14:29:25 -08001116 fst := f.fsType(ctx)
1117 switch fst {
1118 case erofsType:
1119 // Add erofs properties
Cole Faust3e730972024-12-03 13:12:08 -08001120 addStr("erofs_default_compressor", proptools.StringDefault(f.properties.Erofs.Compressor, "lz4hc,9"))
1121 if f.properties.Erofs.Compress_hints != nil {
1122 src := android.PathForModuleSrc(ctx, *f.properties.Erofs.Compress_hints)
1123 addPath("erofs_default_compress_hints", src)
Spandan Dasc35d6fb2024-10-10 17:51:14 +00001124 }
1125 if proptools.BoolDefault(f.properties.Erofs.Sparse, true) {
1126 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2292;bpv=1;bpt=0;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b
1127 addStr("erofs_sparse_flag", "-s")
1128 }
mrziwang1a6291f2024-11-07 14:29:25 -08001129 case f2fsType:
1130 if proptools.BoolDefault(f.properties.F2fs.Sparse, true) {
1131 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2294;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b;bpv=1;bpt=0
1132 addStr("f2fs_sparse_flag", "-S")
1133 }
Spandan Dasc35d6fb2024-10-10 17:51:14 +00001134 }
Spandan Das8dd97102025-03-14 00:06:43 +00001135 f.checkFsTypePropertyError(ctx, fst, fst.String())
Spandan Dasc35d6fb2024-10-10 17:51:14 +00001136
Jihoon Kang983dd882025-01-13 23:14:11 +00001137 if f.properties.Partition_size != nil {
1138 addStr("partition_size", strconv.FormatInt(*f.properties.Partition_size, 10))
1139 }
1140
Jihoon Kang6d08d922025-01-14 18:31:57 +00001141 if proptools.BoolDefault(f.properties.Support_casefolding, false) {
1142 addStr("needs_casefold", "1")
1143 }
1144
1145 if proptools.BoolDefault(f.properties.Support_project_quota, false) {
1146 addStr("needs_projid", "1")
1147 }
1148
1149 if proptools.BoolDefault(f.properties.Enable_compression, false) {
1150 addStr("needs_compress", "1")
1151 }
1152
Cole Fauste03ab892025-01-17 13:55:04 -08001153 sort.Strings(lines)
1154
Cole Fauste1676122024-12-03 17:32:25 -08001155 propFilePreProcessing := android.PathForModuleOut(ctx, "prop_pre_processing")
Cole Fauste03ab892025-01-17 13:55:04 -08001156 android.WriteFileRule(ctx, propFilePreProcessing, strings.Join(lines, "\n"))
Cole Faust4e9f5922024-11-13 16:09:23 -08001157 propFile := android.PathForModuleOut(ctx, "prop")
Cole Fauste1676122024-12-03 17:32:25 -08001158 ctx.Build(pctx, android.BuildParams{
Cole Faustefeb5c42024-12-16 10:47:26 -08001159 Rule: textFileProcessorRule,
1160 Input: propFilePreProcessing,
1161 Output: propFile,
Cole Fauste1676122024-12-03 17:32:25 -08001162 })
Jiyong Park72678312021-01-18 17:29:49 +09001163 return propFile, deps
1164}
1165
Spandan Das227c9492025-03-17 20:14:00 +00001166func (f *filesystem) buildPropFileForMiscInfo(ctx android.ModuleContext) android.Path {
1167 var lines []string
1168 addStr := func(name string, value string) {
1169 lines = append(lines, fmt.Sprintf("%s=%s", name, value))
1170 }
1171
1172 addStr("use_dynamic_partition_size", "true")
1173 addStr("ext_mkuserimg", "mkuserimg_mke2fs")
1174
1175 addStr("building_"+f.partitionName()+"_image", "true")
1176 addStr(f.partitionName()+"_fs_type", f.fsType(ctx).String())
1177
1178 if proptools.Bool(f.properties.Use_avb) {
1179 addStr("avb_"+f.partitionName()+"_hashtree_enable", "true")
1180 if f.properties.Avb_private_key != nil {
1181 key := android.PathForModuleSrc(ctx, *f.properties.Avb_private_key)
1182 addStr("avb_"+f.partitionName()+"_key_path", key.String())
1183 }
1184 addStr("avb_"+f.partitionName()+"_add_hashtree_footer_args", strings.TrimSpace(f.getAvbAddHashtreeFooterArgs(ctx)))
1185 }
1186
1187 if f.selinuxFc != nil {
1188 addStr(f.partitionName()+"_selinux_fc", f.selinuxFc.String())
1189 }
1190
1191 // Disable sparse only when partition size is not defined. disable_sparse has the same
1192 // effect as <partition name>_disable_sparse.
1193 if f.properties.Partition_size == nil {
1194 addStr(f.partitionName()+"_disable_sparse", "true")
Spandan Das62eacae2025-03-18 20:37:42 +00001195 } else if f.partitionName() == "userdata" {
1196 // Add userdata's partition size to misc_info.txt.
1197 // userdata has been special-cased to make the make packaging misc_info.txt implementation
1198 addStr("userdata_size", strconv.FormatInt(*f.properties.Partition_size, 10))
Spandan Das227c9492025-03-17 20:14:00 +00001199 }
1200
1201 fst := f.fsType(ctx)
1202 switch fst {
1203 case erofsType:
1204 // Add erofs properties
1205 addStr("erofs_default_compressor", proptools.StringDefault(f.properties.Erofs.Compressor, "lz4hc,9"))
1206 if proptools.BoolDefault(f.properties.Erofs.Sparse, true) {
1207 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2292;bpv=1;bpt=0;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b
1208 addStr("erofs_sparse_flag", "-s")
1209 }
1210 case f2fsType:
1211 if proptools.BoolDefault(f.properties.F2fs.Sparse, true) {
1212 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2294;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b;bpv=1;bpt=0
1213 addStr("f2fs_sparse_flag", "-S")
1214 }
1215 }
1216
1217 if proptools.BoolDefault(f.properties.Support_casefolding, false) {
1218 addStr("needs_casefold", "1")
1219 }
1220
1221 if proptools.BoolDefault(f.properties.Support_project_quota, false) {
1222 addStr("needs_projid", "1")
1223 }
1224
1225 if proptools.BoolDefault(f.properties.Enable_compression, false) {
1226 addStr("needs_compress", "1")
1227 }
1228
1229 sort.Strings(lines)
1230
1231 propFilePreProcessing := android.PathForModuleOut(ctx, "prop_misc_info_pre_processing")
1232 android.WriteFileRule(ctx, propFilePreProcessing, strings.Join(lines, "\n"))
1233 propFile := android.PathForModuleOut(ctx, "prop_file_for_misc_info")
1234 ctx.Build(pctx, android.BuildParams{
1235 Rule: textFileProcessorRule,
1236 Input: propFilePreProcessing,
1237 Output: propFile,
1238 })
1239
1240 return propFile
1241}
1242
Spandan Das72128502025-03-13 23:53:02 +00001243func (f *filesystem) getAvbAddHashtreeFooterArgs(ctx android.ModuleContext) string {
1244 avb_add_hashtree_footer_args := ""
1245 if !proptools.BoolDefault(f.properties.Use_fec, true) {
1246 avb_add_hashtree_footer_args += " --do_not_generate_fec"
1247 }
1248 hashAlgorithm := proptools.StringDefault(f.properties.Avb_hash_algorithm, "sha256")
1249 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
1250 if f.properties.Rollback_index != nil {
1251 rollbackIndex := proptools.Int(f.properties.Rollback_index)
1252 if rollbackIndex < 0 {
1253 ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
1254 }
1255 avb_add_hashtree_footer_args += " --rollback_index " + strconv.Itoa(rollbackIndex)
1256 }
1257 avb_add_hashtree_footer_args += fmt.Sprintf(" --prop com.android.build.%s.os_version:%s", f.partitionName(), ctx.Config().PlatformVersionLastStable())
1258 // We're not going to add BuildFingerPrintFile as a dep. If it changed, it's likely because
1259 // the build number changed, and we don't want to trigger rebuilds solely based on the build
1260 // number.
1261 avb_add_hashtree_footer_args += fmt.Sprintf(" --prop com.android.build.%s.fingerprint:{CONTENTS_OF:%s}", f.partitionName(), ctx.Config().BuildFingerprintFile(ctx))
1262 if f.properties.Security_patch != nil && proptools.String(f.properties.Security_patch) != "" {
1263 avb_add_hashtree_footer_args += fmt.Sprintf(" --prop com.android.build.%s.security_patch:%s", f.partitionName(), proptools.String(f.properties.Security_patch))
1264 }
1265 return avb_add_hashtree_footer_args
1266}
1267
mrziwang1a6291f2024-11-07 14:29:25 -08001268// This method checks if there is any property set for the fstype(s) other than
1269// the current fstype.
1270func (f *filesystem) checkFsTypePropertyError(ctx android.ModuleContext, t fsType, fs string) {
1271 raiseError := func(otherFsType, currentFsType string) {
1272 errMsg := fmt.Sprintf("%s is non-empty, but FS type is %s\n. Please delete %s properties if this partition should use %s\n", otherFsType, currentFsType, otherFsType, currentFsType)
1273 ctx.PropertyErrorf(otherFsType, errMsg)
1274 }
1275
1276 if t != erofsType {
1277 if f.properties.Erofs.Compressor != nil || f.properties.Erofs.Compress_hints != nil || f.properties.Erofs.Sparse != nil {
1278 raiseError("erofs", fs)
1279 }
1280 }
1281 if t != f2fsType {
1282 if f.properties.F2fs.Sparse != nil {
1283 raiseError("f2fs", fs)
1284 }
1285 }
1286}
1287
Jihoon Kang6da80752024-12-23 18:53:32 +00001288func includeFilesRootDir(ctx android.ModuleContext) (rootDirs android.Paths, partitions android.Paths) {
1289 ctx.VisitDirectDepsWithTag(interPartitionInstallDependencyTag, func(m android.Module) {
1290 if fsProvider, ok := android.OtherModuleProvider(ctx, m, FilesystemProvider); ok {
1291 rootDirs = append(rootDirs, fsProvider.RootDir)
1292 partitions = append(partitions, fsProvider.Output)
1293 } else {
1294 ctx.PropertyErrorf("include_files_of", "only filesystem modules can be listed in "+
1295 "include_files_of but %s is not a filesystem module", m.Name())
1296 }
1297 })
1298 return rootDirs, partitions
1299}
1300
Jihoon Kangdd49f412025-03-07 01:30:43 +00001301func includeFilesInstalledFiles(ctx android.ModuleContext) (ret []depset.DepSet[InstalledFilesStruct]) {
1302 ctx.VisitDirectDepsWithTag(interPartitionInstallDependencyTag, func(m android.Module) {
1303 if fsProvider, ok := android.OtherModuleProvider(ctx, m, FilesystemProvider); ok {
1304 ret = append(ret, fsProvider.InstalledFilesDepSet)
1305 }
1306 })
1307 return
1308}
1309
Cole Faust62cfaeb2025-01-15 18:06:40 -08001310func (f *filesystem) buildCpioImage(
1311 ctx android.ModuleContext,
1312 builder *android.RuleBuilder,
1313 rootDir android.OutputPath,
1314 compressed bool,
Cole Faustb36763e2025-02-18 15:21:44 -08001315) (android.Path, android.Paths) {
Jiyong Park11a65972021-02-01 21:09:38 +09001316 if proptools.Bool(f.properties.Use_avb) {
1317 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
1318 "Consider adding this to bootimg module and signing the entire boot image.")
1319 }
1320
Inseob Kimcc8e5362021-02-03 14:05:24 +09001321 if proptools.String(f.properties.File_contexts) != "" {
1322 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
1323 }
1324
Jihoon Kang6da80752024-12-23 18:53:32 +00001325 rootDirs, partitions := includeFilesRootDir(ctx)
1326
Cole Faust4e9f5922024-11-13 16:09:23 -08001327 output := android.PathForModuleOut(ctx, f.installFileName())
Jiyong Park837cdb22021-02-05 00:17:14 +09001328 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +09001329 BuiltTool("mkbootfs").
Spandan Das5ef1a9c2025-02-11 18:50:17 +00001330 Implicit(f.fileystemStagingDirTimestamp(ctx)).
Jiyong Park837cdb22021-02-05 00:17:14 +09001331 Text(rootDir.String()) // input directory
Jihoon Kang6da80752024-12-23 18:53:32 +00001332
1333 for i := range len(rootDirs) {
1334 cmd.Text(rootDirs[i].String())
1335 }
1336 cmd.Implicits(partitions)
1337
Jihoon Kang6c03c8e2024-11-18 21:30:22 +00001338 if nodeList := f.properties.Dev_nodes_description_file; nodeList != nil {
1339 cmd.FlagWithInput("-n ", android.PathForModuleSrc(ctx, proptools.String(nodeList)))
1340 }
Jiyong Park837cdb22021-02-05 00:17:14 +09001341 if compressed {
1342 cmd.Text("|").
1343 BuiltTool("lz4").
1344 Flag("--favor-decSpeed"). // for faster boot
1345 Flag("-12"). // maximum compression level
1346 Flag("-l"). // legacy format for kernel
1347 Text(">").Output(output)
1348 } else {
1349 cmd.Text(">").Output(output)
1350 }
Jiyong Park11a65972021-02-01 21:09:38 +09001351
1352 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +09001353 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +09001354
Cole Faustb36763e2025-02-18 15:21:44 -08001355 return output, rootDirs
Jiyong Park11a65972021-02-01 21:09:38 +09001356}
1357
Cole Faust4a2a7c92024-03-12 12:44:40 -07001358var validPartitions = []string{
1359 "system",
1360 "userdata",
1361 "cache",
1362 "system_other",
1363 "vendor",
1364 "product",
1365 "system_ext",
1366 "odm",
1367 "vendor_dlkm",
1368 "odm_dlkm",
1369 "system_dlkm",
Cole Faust76a6e952024-11-07 16:56:45 -08001370 "ramdisk",
Cole Faust24938e22024-11-18 14:01:58 -08001371 "vendor_ramdisk",
Jihoon Kang3216c982024-12-02 19:42:20 +00001372 "recovery",
Cole Faust4a2a7c92024-03-12 12:44:40 -07001373}
1374
Cole Faust19fbb072025-01-30 18:19:29 -08001375func (f *filesystem) buildEventLogtagsFile(
1376 ctx android.ModuleContext,
1377 builder *android.RuleBuilder,
1378 rebasedDir android.OutputPath,
1379 fullInstallPaths *[]FullInstallPathInfo,
1380) {
Inseob Kimb7b84572024-04-30 10:51:47 +09001381 if !proptools.Bool(f.properties.Build_logtags) {
1382 return
1383 }
1384
Inseob Kimb7b84572024-04-30 10:51:47 +09001385 etcPath := rebasedDir.Join(ctx, "etc")
1386 eventLogtagsPath := etcPath.Join(ctx, "event-log-tags")
1387 builder.Command().Text("mkdir").Flag("-p").Text(etcPath.String())
Cole Fauste4506af2024-12-11 14:14:50 -08001388 builder.Command().Text("cp").Input(android.MergedLogtagsPath(ctx)).Text(eventLogtagsPath.String())
Kiyoung Kim99a954d2024-06-21 14:22:20 +09001389
Cole Faust19fbb072025-01-30 18:19:29 -08001390 *fullInstallPaths = append(*fullInstallPaths, FullInstallPathInfo{
1391 FullInstallPath: android.PathForModuleInPartitionInstall(ctx, f.PartitionType(), "etc", "event-log-tags"),
1392 SourcePath: android.MergedLogtagsPath(ctx),
1393 })
1394
Kiyoung Kim99a954d2024-06-21 14:22:20 +09001395 f.appendToEntry(ctx, eventLogtagsPath)
Inseob Kimb7b84572024-04-30 10:51:47 +09001396}
1397
Cole Faust19fbb072025-01-30 18:19:29 -08001398func (f *filesystem) BuildLinkerConfigFile(
1399 ctx android.ModuleContext,
1400 builder *android.RuleBuilder,
1401 rebasedDir android.OutputPath,
1402 fullInstallPaths *[]FullInstallPathInfo,
1403) {
Spandan Das2047a4c2024-11-11 21:24:58 +00001404 if !proptools.Bool(f.properties.Linker_config.Gen_linker_config) {
Spandan Das92631882024-10-28 22:49:38 +00001405 return
1406 }
1407
Spandan Das918191e2024-10-31 18:27:23 +00001408 provideModules, _ := f.getLibsForLinkerConfig(ctx)
Cole Faustfee27012024-12-13 14:10:31 -08001409 intermediateOutput := android.PathForModuleOut(ctx, "linker.config.pb")
1410 linkerconfig.BuildLinkerConfig(ctx, android.PathsForModuleSrc(ctx, f.properties.Linker_config.Linker_config_srcs), provideModules, nil, intermediateOutput)
Spandan Das92631882024-10-28 22:49:38 +00001411 output := rebasedDir.Join(ctx, "etc", "linker.config.pb")
Cole Faustfee27012024-12-13 14:10:31 -08001412 builder.Command().Text("cp").Input(intermediateOutput).Output(output)
Spandan Das92631882024-10-28 22:49:38 +00001413
Cole Faust19fbb072025-01-30 18:19:29 -08001414 *fullInstallPaths = append(*fullInstallPaths, FullInstallPathInfo{
1415 FullInstallPath: android.PathForModuleInPartitionInstall(ctx, f.PartitionType(), "etc", "linker.config.pb"),
1416 SourcePath: intermediateOutput,
1417 })
1418
Spandan Das92631882024-10-28 22:49:38 +00001419 f.appendToEntry(ctx, output)
1420}
1421
Kiyoung Kim23be5bb2024-11-27 00:50:30 +00001422func (f *filesystem) ShouldUseVintfFragmentModuleOnly() bool {
1423 return false
1424}
1425
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001426type partition interface {
1427 PartitionType() string
1428}
1429
Cole Faust9a24d902024-03-18 15:38:12 -07001430func (f *filesystem) PartitionType() string {
1431 return proptools.StringDefault(f.properties.Partition_type, "system")
1432}
1433
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001434var _ partition = (*filesystem)(nil)
1435
Jiyong Park65c49f52020-11-24 14:23:26 +09001436var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
1437
1438// Implements android.AndroidMkEntriesProvider
1439func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
1440 return []android.AndroidMkEntries{android.AndroidMkEntries{
1441 Class: "ETC",
1442 OutputFile: android.OptionalPathForPath(f.output),
1443 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07001444 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -08001445 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +09001446 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
Kiyoung Kim99a954d2024-06-21 14:22:20 +09001447 entries.SetString("LOCAL_FILESYSTEM_FILELIST", f.fileListFile.String())
Jiyong Park65c49f52020-11-24 14:23:26 +09001448 },
1449 },
1450 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +09001451}
Jiyong Park12a719c2021-01-07 15:31:24 +09001452
1453// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
1454// package to have access to the output file.
1455type Filesystem interface {
1456 android.Module
1457 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +09001458
1459 // Returns the output file that is signed by avbtool. If this module is not signed, returns
1460 // nil.
1461 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +09001462}
1463
1464var _ Filesystem = (*filesystem)(nil)
1465
1466func (f *filesystem) OutputPath() android.Path {
1467 return f.output
1468}
Jiyong Park972e06c2021-03-15 23:32:49 +09001469
1470func (f *filesystem) SignedOutputPath() android.Path {
1471 if proptools.Bool(f.properties.Use_avb) {
1472 return f.OutputPath()
1473 }
1474 return nil
1475}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +09001476
1477// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
1478// Note that "apex" module installs its contents to "apex"(fake partition) as well
1479// for symbol lookup by imitating "activated" paths.
1480func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Cole Faustb8e280f2025-01-16 16:33:26 -08001481 return f.PackagingBase.GatherPackagingSpecsWithFilterAndModifier(ctx, f.filesystemBuilder.FilterPackagingSpec, f.filesystemBuilder.ModifyPackagingSpec)
1482}
1483
Jihoon Kangabec3ec2025-02-19 00:55:10 +00001484func (f *filesystem) gatherOwners(specs map[string]android.PackagingSpec) []InstalledModuleInfo {
1485 var owners []InstalledModuleInfo
1486 for _, p := range android.SortedKeys(specs) {
1487 spec := specs[p]
1488 owners = append(owners, InstalledModuleInfo{
1489 Name: spec.Owner(),
1490 Variation: spec.Variation(),
1491 })
1492 }
1493 return owners
1494}
1495
Cole Faustb8e280f2025-01-16 16:33:26 -08001496// Dexpreopt files are installed to system_other. Collect the packaingSpecs for the dexpreopt files
1497// from this partition to export to the system_other partition later.
1498func (f *filesystem) systemOtherFiles(ctx android.ModuleContext) map[string]android.PackagingSpec {
1499 filter := func(spec android.PackagingSpec) bool {
1500 // For some reason system_other packaging specs don't set the partition field.
1501 return strings.HasPrefix(spec.RelPathInPackage(), "system_other/")
1502 }
1503 modifier := func(spec *android.PackagingSpec) {
1504 spec.SetRelPathInPackage(strings.TrimPrefix(spec.RelPathInPackage(), "system_other/"))
1505 spec.SetPartition("system_other")
1506 }
1507 return f.PackagingBase.GatherPackagingSpecsWithFilterAndModifier(ctx, filter, modifier)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +09001508}
Jooyung Han65f402b2022-04-21 14:24:04 +09001509
1510func sha1sum(values []string) string {
1511 h := sha256.New()
1512 for _, value := range values {
1513 io.WriteString(h, value)
1514 }
1515 return fmt.Sprintf("%x", h.Sum(nil))
1516}
Jooyung Hane6067592023-03-16 13:11:17 +09001517
1518// Base cc.UseCoverage
1519
1520var _ cc.UseCoverage = (*filesystem)(nil)
1521
Colin Crosse1a85552024-06-14 12:17:37 -07001522func (*filesystem) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +09001523 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
1524}
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001525
1526// android_filesystem_defaults
1527
1528type filesystemDefaults struct {
1529 android.ModuleBase
1530 android.DefaultsModuleBase
1531
Inseob Kim3c0a0422024-11-05 17:21:37 +09001532 properties FilesystemProperties
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001533}
1534
1535// android_filesystem_defaults is a default module for android_filesystem and android_system_image
1536func filesystemDefaultsFactory() android.Module {
1537 module := &filesystemDefaults{}
1538 module.AddProperties(&module.properties)
1539 module.AddProperties(&android.PackagingProperties{})
1540 android.InitDefaultsModule(module)
1541 return module
1542}
1543
1544func (f *filesystemDefaults) PartitionType() string {
1545 return proptools.StringDefault(f.properties.Partition_type, "system")
1546}
1547
1548var _ partition = (*filesystemDefaults)(nil)
1549
1550func (f *filesystemDefaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1551 validatePartitionType(ctx, f)
Yu Liu71f1ea32025-02-26 23:39:20 +00001552 android.SetProvider(ctx, FilesystemDefaultsInfoProvider, FilesystemDefaultsInfo{})
1553 android.SetProvider(ctx, android.PartitionTypeInfoProvider, android.PartitionTypeInfo{
Yu Liufc8d5c12025-01-09 00:19:06 +00001554 PartitionType: f.PartitionType(),
1555 })
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001556}
Spandan Das918191e2024-10-31 18:27:23 +00001557
1558// getLibsForLinkerConfig returns
1559// 1. A list of libraries installed in this filesystem
1560// 2. A list of dep libraries _not_ installed in this filesystem
1561//
1562// `linkerconfig.BuildLinkerConfig` will convert these two to a linker.config.pb for the filesystem
1563// (1) will be added to --provideLibs if they are C libraries with a stable interface (has stubs)
1564// (2) will be added to --requireLibs if they are C libraries with a stable interface (has stubs)
Yu Liu68a70b72025-01-08 22:54:44 +00001565func (f *filesystem) getLibsForLinkerConfig(ctx android.ModuleContext) ([]android.ModuleProxy, []android.ModuleProxy) {
Spandan Das918191e2024-10-31 18:27:23 +00001566 // we need "Module"s for packaging items
Yu Liu68a70b72025-01-08 22:54:44 +00001567 modulesInPackageByModule := make(map[android.ModuleProxy]bool)
Spandan Das918191e2024-10-31 18:27:23 +00001568 modulesInPackageByName := make(map[string]bool)
1569
1570 deps := f.gatherFilteredPackagingSpecs(ctx)
Yu Liu68a70b72025-01-08 22:54:44 +00001571 ctx.WalkDepsProxy(func(child, parent android.ModuleProxy) bool {
Yu Liuef9e63e2025-03-04 19:01:28 +00001572 if !android.OtherModuleProviderOrDefault(ctx, child, android.CommonModuleInfoProvider).Enabled {
Yu Liu9c6b6762025-01-08 22:04:35 +00001573 return false
1574 }
Spandan Das918191e2024-10-31 18:27:23 +00001575 for _, ps := range android.OtherModuleProviderOrDefault(
1576 ctx, child, android.InstallFilesProvider).PackagingSpecs {
Spandan Dasecf667f2024-12-05 00:58:56 +00001577 if _, ok := deps[ps.RelPathInPackage()]; ok && ps.Partition() == f.PartitionType() {
Spandan Das918191e2024-10-31 18:27:23 +00001578 modulesInPackageByModule[child] = true
1579 modulesInPackageByName[child.Name()] = true
1580 return true
1581 }
1582 }
1583 return true
1584 })
1585
Yu Liu68a70b72025-01-08 22:54:44 +00001586 provideModules := make([]android.ModuleProxy, 0, len(modulesInPackageByModule))
Spandan Das918191e2024-10-31 18:27:23 +00001587 for mod := range modulesInPackageByModule {
1588 provideModules = append(provideModules, mod)
1589 }
1590
Yu Liu68a70b72025-01-08 22:54:44 +00001591 var requireModules []android.ModuleProxy
1592 ctx.WalkDepsProxy(func(child, parent android.ModuleProxy) bool {
Yu Liuef9e63e2025-03-04 19:01:28 +00001593 if !android.OtherModuleProviderOrDefault(ctx, child, android.CommonModuleInfoProvider).Enabled {
Yu Liu9c6b6762025-01-08 22:04:35 +00001594 return false
1595 }
Spandan Das918191e2024-10-31 18:27:23 +00001596 _, parentInPackage := modulesInPackageByModule[parent]
1597 _, childInPackageName := modulesInPackageByName[child.Name()]
1598
1599 // When parent is in the package, and child (or its variant) is not, this can be from an interface.
1600 if parentInPackage && !childInPackageName {
1601 requireModules = append(requireModules, child)
1602 }
1603 return true
1604 })
1605
1606 return provideModules, requireModules
1607}
Cole Faust26bdac52024-11-19 13:37:53 -08001608
1609// Checks that the given file doesn't exceed the given size, and will also print a warning
1610// if it's nearing the maximum size. Equivalent to assert-max-image-size in make:
1611// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/definitions.mk;l=3455;drc=993c4de29a02a6accd60ceaaee153307e1a18d10
1612func assertMaxImageSize(builder *android.RuleBuilder, image android.Path, maxSize int64, addAvbLater bool) {
1613 if addAvbLater {
1614 // The value 69632 is derived from MAX_VBMETA_SIZE + MAX_FOOTER_SIZE in avbtool.
1615 // Logic copied from make:
1616 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=228;drc=a6a0007ef24e16c0b79f439beac4a118416717e6
1617 maxSize -= 69632
1618 }
1619 cmd := builder.Command()
1620 cmd.Textf(`file="%s"; maxsize="%d";`+
1621 `total=$(stat -c "%%s" "$file" | tr -d '\n');`+
1622 `if [ "$total" -gt "$maxsize" ]; then `+
1623 ` echo "error: $file too large ($total > $maxsize)";`+
1624 ` false;`+
1625 `elif [ "$total" -gt $((maxsize - 32768)) ]; then `+
1626 ` echo "WARNING: $file approaching size limit ($total now; limit $maxsize)";`+
1627 `fi`,
1628 image, maxSize)
1629 cmd.Implicit(image)
1630}
Spandan Das71be42d2024-11-20 18:34:16 +00001631
1632// addAutogeneratedRroDeps walks the transitive closure of vendor and product partitions.
1633// It visits apps installed in system and system_ext partitions, and adds the autogenerated
1634// RRO modules to its own deps.
1635func addAutogeneratedRroDeps(ctx android.BottomUpMutatorContext) {
1636 f, ok := ctx.Module().(*filesystem)
1637 if !ok {
1638 return
1639 }
1640 thisPartition := f.PartitionType()
1641 if thisPartition != "vendor" && thisPartition != "product" {
Cole Faust34592c02024-12-13 11:20:24 -08001642 if f.properties.Android_filesystem_deps.System != nil {
1643 ctx.PropertyErrorf("android_filesystem_deps.system", "only vendor or product partitions can use android_filesystem_deps")
1644 }
1645 if f.properties.Android_filesystem_deps.System_ext != nil {
1646 ctx.PropertyErrorf("android_filesystem_deps.system_ext", "only vendor or product partitions can use android_filesystem_deps")
1647 }
Spandan Das71be42d2024-11-20 18:34:16 +00001648 return
1649 }
1650 ctx.WalkDeps(func(child, parent android.Module) bool {
1651 depTag := ctx.OtherModuleDependencyTag(child)
1652 if parent.Name() == f.Name() && depTag != interPartitionDependencyTag {
1653 return false // This is a module listed in deps of vendor/product filesystem
1654 }
1655 if vendorOverlay := java.AutogeneratedRroModuleName(ctx, child.Name(), "vendor"); ctx.OtherModuleExists(vendorOverlay) && thisPartition == "vendor" {
1656 ctx.AddFarVariationDependencies(nil, dependencyTagWithVisibilityEnforcementBypass, vendorOverlay)
1657 }
1658 if productOverlay := java.AutogeneratedRroModuleName(ctx, child.Name(), "product"); ctx.OtherModuleExists(productOverlay) && thisPartition == "product" {
1659 ctx.AddFarVariationDependencies(nil, dependencyTagWithVisibilityEnforcementBypass, productOverlay)
1660 }
1661 return true
1662 })
1663}
Spandan Das6ec1fcf2025-02-11 22:51:51 +00001664
Yu Liu2a815b62025-02-21 20:46:25 +00001665func (f *filesystem) MakeVars(ctx android.MakeVarsModuleContext) []android.ModuleMakeVarsValue {
Spandan Das6ec1fcf2025-02-11 22:51:51 +00001666 if f.Name() == ctx.Config().SoongDefinedSystemImage() {
Yu Liu2a815b62025-02-21 20:46:25 +00001667 return []android.ModuleMakeVarsValue{{"SOONG_DEFINED_SYSTEM_IMAGE_PATH", f.output.String()}}
Spandan Das6ec1fcf2025-02-11 22:51:51 +00001668 }
Yu Liu2a815b62025-02-21 20:46:25 +00001669 return nil
Spandan Das6ec1fcf2025-02-11 22:51:51 +00001670}
Yu Liu0a37d422025-02-13 02:05:00 +00001671
1672func setCommonFilesystemInfo(ctx android.ModuleContext, m Filesystem) {
1673 android.SetProvider(ctx, FilesystemProvider, FilesystemInfo{
1674 Output: m.OutputPath(),
1675 SignedOutputPath: m.SignedOutputPath(),
1676 })
1677}