blob: 68cbee95fc10d004bd0c9ccb6d99cb116142498d [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"
Jiyong Park71baa762021-01-18 21:11:03 +090033 "github.com/google/blueprint/proptools"
Jiyong Park6f0f6882020-11-12 13:14:30 +090034)
35
36func init() {
Jooyung Han9706cbc2021-04-15 22:43:48 +090037 registerBuildComponents(android.InitRegistrationContext)
Spandan Das71be42d2024-11-20 18:34:16 +000038 registerMutators(android.InitRegistrationContext)
Jooyung Han9706cbc2021-04-15 22:43:48 +090039}
40
41func registerBuildComponents(ctx android.RegistrationContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -070042 ctx.RegisterModuleType("android_filesystem", FilesystemFactory)
Jiyong Parkf46b1af2024-04-05 18:13:33 +090043 ctx.RegisterModuleType("android_filesystem_defaults", filesystemDefaultsFactory)
Jihoon Kang98047cf2024-10-02 17:13:54 +000044 ctx.RegisterModuleType("android_system_image", SystemImageFactory)
Jiyong Parkbc485482022-11-15 22:31:49 +090045 ctx.RegisterModuleType("avb_add_hash_footer", avbAddHashFooterFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090046 ctx.RegisterModuleType("avb_add_hash_footer_defaults", avbAddHashFooterDefaultsFactory)
Alice Wang000e3a32023-01-03 16:11:20 +000047 ctx.RegisterModuleType("avb_gen_vbmeta_image", avbGenVbmetaImageFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090048 ctx.RegisterModuleType("avb_gen_vbmeta_image_defaults", avbGenVbmetaImageDefaultsFactory)
Jiyong Park6f0f6882020-11-12 13:14:30 +090049}
50
Spandan Das71be42d2024-11-20 18:34:16 +000051func registerMutators(ctx android.RegistrationContext) {
52 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
53 ctx.BottomUp("add_autogenerated_rro_deps", addAutogeneratedRroDeps)
54 })
55}
56
Cole Fauste1676122024-12-03 17:32:25 -080057// Remember to add referenced files to implicits!
58var textFileProcessorRule = pctx.AndroidStaticRule("text_file_processing", blueprint.RuleParams{
59 Command: "build/soong/scripts/text_file_processor.py $in $out",
60 CommandDeps: []string{"build/soong/scripts/text_file_processor.py"},
61})
62
Jiyong Park6f0f6882020-11-12 13:14:30 +090063type filesystem struct {
64 android.ModuleBase
65 android.PackagingBase
Jiyong Parkf46b1af2024-04-05 18:13:33 +090066 android.DefaultableModuleBase
Jiyong Park65c49f52020-11-24 14:23:26 +090067
Jihoon Kang98047cf2024-10-02 17:13:54 +000068 properties FilesystemProperties
Jiyong Park71baa762021-01-18 21:11:03 +090069
Cole Faust4e9f5922024-11-13 16:09:23 -080070 output android.Path
Jiyong Park65c49f52020-11-24 14:23:26 +090071 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090072
Cole Faust4e9f5922024-11-13 16:09:23 -080073 fileListFile android.Path
Kiyoung Kim99a954d2024-06-21 14:22:20 +090074
75 // Keeps the entries installed from this filesystem
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090076 entries []string
Kiyoung Kim67118212024-11-07 13:23:44 +090077
78 filesystemBuilder filesystemBuilder
Jiyong Park6f0f6882020-11-12 13:14:30 +090079}
80
Kiyoung Kim67118212024-11-07 13:23:44 +090081type filesystemBuilder interface {
82 BuildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath)
83 // Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs()
84 FilterPackagingSpec(spec android.PackagingSpec) bool
Inseob Kim3c0a0422024-11-05 17:21:37 +090085 // Function that modifies PackagingSpec in PackagingBase.GatherPackagingSpecs() to customize.
86 // For example, GSI system.img contains system_ext and product artifacts and their
87 // relPathInPackage need to be rebased to system/system_ext and system/system_product.
88 ModifyPackagingSpec(spec *android.PackagingSpec)
Kiyoung Kim23be5bb2024-11-27 00:50:30 +000089
90 // Function to check if the filesystem should not use `vintf_fragments` property,
91 // but use `vintf_fragment` module type instead
92 ShouldUseVintfFragmentModuleOnly() bool
Kiyoung Kim67118212024-11-07 13:23:44 +090093}
94
95var _ filesystemBuilder = (*filesystem)(nil)
96
Spandan Das69464c32024-10-25 20:08:06 +000097type SymlinkDefinition struct {
Inseob Kim14199b02021-02-09 21:18:31 +090098 Target *string
99 Name *string
100}
101
Jihoon Kang0a453892024-12-09 22:16:26 +0000102// CopyWithNamePrefix returns a new [SymlinkDefinition] with prefix added to Name.
103func (s *SymlinkDefinition) CopyWithNamePrefix(prefix string) SymlinkDefinition {
104 return SymlinkDefinition{
105 Target: s.Target,
106 Name: proptools.StringPtr(filepath.Join(prefix, proptools.String(s.Name))),
107 }
108}
109
Jihoon Kang98047cf2024-10-02 17:13:54 +0000110type FilesystemProperties struct {
Jiyong Park71baa762021-01-18 21:11:03 +0900111 // When set to true, sign the image with avbtool. Default is false.
112 Use_avb *bool
113
114 // Path to the private key that avbtool will use to sign this filesystem image.
115 // TODO(jiyong): allow apex_key to be specified here
116 Avb_private_key *string `android:"path"`
117
Shikha Panwar01403bb2022-12-22 12:22:57 +0000118 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +0900119 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +0900120
Shikha Panwar01403bb2022-12-22 12:22:57 +0000121 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
122 // avbtool. Default used by avbtool is sha1.
Shikha Panware6f30632022-12-21 12:54:45 +0000123 Avb_hash_algorithm *string
124
Spandan Dase5c393c2024-12-12 19:25:07 +0000125 // The security patch passed to as the com.android.build.<type>.security_patch avb property.
126 Security_patch *string
127
Cole Fauste1676122024-12-03 17:32:25 -0800128 // Whether or not to use forward-error-correction codes when signing with AVB. Defaults to true.
129 Use_fec *bool
130
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000131 // The index used to prevent rollback of the image. Only used if use_avb is true.
132 Rollback_index *int64
133
Jiyong Parkac4076d2021-03-15 23:21:30 +0900134 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
135 Partition_name *string
136
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000137 // Type of the filesystem. Currently, ext4, erofs, cpio, and compressed_cpio are supported. Default
Jiyong Park837cdb22021-02-05 00:17:14 +0900138 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +0900139 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +0900140
Cole Faust9a24d902024-03-18 15:38:12 -0700141 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
142 // checks, and will be used in the future for API surface checks.
143 Partition_type *string
144
Cole Faust0d467052024-12-04 17:19:19 -0800145 // file_contexts file to make image. Currently, only ext4 is supported. These file contexts
146 // will be compiled with sefcontext_compile
Inseob Kimcc8e5362021-02-03 14:05:24 +0900147 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900148
Cole Faust0d467052024-12-04 17:19:19 -0800149 // The selinux file contexts, after having already run them through sefcontext_compile
150 Precompiled_file_contexts *string `android:"path"`
151
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900152 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
153 // (root).
154 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +0900155
156 // Directories to be created under root. e.g. /dev, /proc, etc.
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700157 Dirs proptools.Configurable[[]string]
Inseob Kim14199b02021-02-09 21:18:31 +0900158
Jihoon Kang6da80752024-12-23 18:53:32 +0000159 // List of filesystem modules to include in creating the partition. The root directory of
160 // the provided filesystem modules are included in creating the partition.
161 // This is only supported for cpio and compressed cpio filesystem types.
162 Include_files_of []string
163
Inseob Kim14199b02021-02-09 21:18:31 +0900164 // Symbolic links to be created under root with "ln -sf <target> <name>".
Spandan Das69464c32024-10-25 20:08:06 +0000165 Symlinks []SymlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900166
167 // Seconds since unix epoch to override timestamps of file entries
168 Fake_timestamp *string
169
170 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
171 // Otherwise, they'll be set as random which might cause indeterministic build output.
172 Uuid *string
Inseob Kim376d72f2023-11-01 15:40:25 +0900173
174 // Mount point for this image. Default is "/"
175 Mount_point *string
Cole Faust4a2a7c92024-03-12 12:44:40 -0700176
Inseob Kimb7b84572024-04-30 10:51:47 +0900177 // When set, builds etc/event-log-tags file by merging logtags from all dependencies.
178 // Default is false
179 Build_logtags *bool
180
Justin Yun74f3f302024-05-07 14:32:14 +0900181 // Install aconfig_flags.pb file for the modules installed in this partition.
182 Gen_aconfig_flags_pb *bool
183
Cole Faust34592c02024-12-13 11:20:24 -0800184 // List of names of other filesystem partitions to import their aconfig flags from.
185 // This is used for the system partition to import system_ext's aconfig flags, as currently
186 // those are considered one "container": aosp/3261300
187 Import_aconfig_flags_from []string
188
Inseob Kim53391842024-03-29 17:44:07 +0900189 Fsverity fsverityProperties
Cole Faust92ccbe22024-10-03 14:38:37 -0700190
191 // If this property is set to true, the filesystem will call ctx.UncheckedModule(), causing
192 // it to not be built on checkbuilds. Used for the automatic migration from make to soong
193 // build modules, where we want to emit some not-yet-working filesystems and we don't want them
194 // to be built.
195 Unchecked_module *bool `blueprint:"mutated"`
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000196
197 Erofs ErofsProperties
Jihoon Kang0d545b82024-10-11 00:21:57 +0000198
mrziwang1a6291f2024-11-07 14:29:25 -0800199 F2fs F2fsProperties
200
Spandan Das2047a4c2024-11-11 21:24:58 +0000201 Linker_config LinkerConfigProperties
Spandan Das92631882024-10-28 22:49:38 +0000202
Jihoon Kang0d545b82024-10-11 00:21:57 +0000203 // Determines if the module is auto-generated from Soong or not. If the module is
204 // auto-generated, its deps are exempted from visibility enforcement.
205 Is_auto_generated *bool
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000206
207 // Path to the dev nodes description file. This is only needed for building the ramdisk
208 // partition and should not be explicitly specified.
209 Dev_nodes_description_file *string `android:"path" blueprint:"mutated"`
Spandan Das71be42d2024-11-20 18:34:16 +0000210
211 // Additional dependencies used for building android products
212 Android_filesystem_deps AndroidFilesystemDeps
Spandan Dasc49b85e2025-01-10 00:51:25 +0000213
214 // Name of the output. Default is $(module_name).img
215 Stem *string
Jihoon Kang983dd882025-01-13 23:14:11 +0000216
217 // The size of the partition on the device. It will be a build error if this built partition
218 // image exceeds this size.
219 Partition_size *int64
Jihoon Kang6d08d922025-01-14 18:31:57 +0000220
221 // Whether to format f2fs and ext4 in a way that supports casefolding
222 Support_casefolding *bool
223
224 // Whether to format f2fs and ext4 in a way that supports project quotas
225 Support_project_quota *bool
226
227 // Whether to enable per-file compression in f2fs
228 Enable_compression *bool
Spandan Das71be42d2024-11-20 18:34:16 +0000229}
230
231type AndroidFilesystemDeps struct {
232 System *string
233 System_ext *string
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000234}
235
236// Additional properties required to generate erofs FS partitions.
237type ErofsProperties struct {
238 // Compressor and Compression level passed to mkfs.erofs. e.g. (lz4hc,9)
239 // Please see external/erofs-utils/README for complete documentation.
240 Compressor *string
241
242 // Used as --compress-hints for mkfs.erofs
243 Compress_hints *string `android:"path"`
244
245 Sparse *bool
Jiyong Park71baa762021-01-18 21:11:03 +0900246}
247
mrziwang1a6291f2024-11-07 14:29:25 -0800248// Additional properties required to generate f2fs FS partitions.
249type F2fsProperties struct {
250 Sparse *bool
251}
252
Spandan Das173256b2024-10-31 19:59:30 +0000253type LinkerConfigProperties struct {
254
255 // Build a linker.config.pb file
256 Gen_linker_config *bool
257
258 // List of files (in .json format) that will be converted to a linker config file (in .pb format).
259 // The linker config file be installed in the filesystem at /etc/linker.config.pb
260 Linker_config_srcs []string `android:"path"`
261}
262
Jiyong Park65c49f52020-11-24 14:23:26 +0900263// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
264// image. The filesystem images are expected to be mounted in the target device, which means the
265// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
266// The modules are placed in the filesystem image just like they are installed to the ordinary
267// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Cole Faust92ccbe22024-10-03 14:38:37 -0700268func FilesystemFactory() android.Module {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900269 module := &filesystem{}
Kiyoung Kim67118212024-11-07 13:23:44 +0900270 module.filesystemBuilder = module
Cole Faust2cfe6962024-09-17 11:31:14 -0700271 initFilesystemModule(module, module)
Jiyong Parkfa616132021-04-20 11:36:40 +0900272 return module
273}
274
Cole Faust2cfe6962024-09-17 11:31:14 -0700275func initFilesystemModule(module android.DefaultableModule, filesystemModule *filesystem) {
276 module.AddProperties(&filesystemModule.properties)
277 android.InitPackageModule(filesystemModule)
278 filesystemModule.PackagingBase.DepsCollectFirstTargetOnly = true
Jihoon Kang79196c52024-10-30 18:49:47 +0000279 filesystemModule.PackagingBase.AllowHighPriorityDeps = true
Jiyong Park6f0f6882020-11-12 13:14:30 +0900280 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900281 android.InitDefaultableModule(module)
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000282
283 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
284 filesystemModule.setDevNodesDescriptionProp()
285 })
Jiyong Park6f0f6882020-11-12 13:14:30 +0900286}
287
Jihoon Kang0d545b82024-10-11 00:21:57 +0000288type depTag struct {
Jiyong Park12a719c2021-01-07 15:31:24 +0900289 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900290 android.PackagingItemAlwaysDepTag
Jihoon Kang0d545b82024-10-11 00:21:57 +0000291}
292
293var dependencyTag = depTag{}
294
295type depTagWithVisibilityEnforcementBypass struct {
296 depTag
297}
298
Spandan Das71be42d2024-11-20 18:34:16 +0000299type interPartitionDepTag struct {
300 blueprint.BaseDependencyTag
301}
302
303var interPartitionDependencyTag = interPartitionDepTag{}
304
Jihoon Kang6da80752024-12-23 18:53:32 +0000305var interPartitionInstallDependencyTag = interPartitionDepTag{}
306
Jihoon Kang0d545b82024-10-11 00:21:57 +0000307var _ android.ExcludeFromVisibilityEnforcementTag = (*depTagWithVisibilityEnforcementBypass)(nil)
308
309func (t depTagWithVisibilityEnforcementBypass) ExcludeFromVisibilityEnforcement() {}
310
311var dependencyTagWithVisibilityEnforcementBypass = depTagWithVisibilityEnforcementBypass{}
Jiyong Park65b62242020-11-25 12:44:59 +0900312
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000313// ramdiskDevNodesDescription is the name of the filegroup module that provides the file that
314// contains the description of dev nodes added to the CPIO archive for the ramdisk partition.
315const ramdiskDevNodesDescription = "ramdisk_node_list"
316
317func (f *filesystem) setDevNodesDescriptionProp() {
318 if proptools.String(f.properties.Partition_name) == "ramdisk" {
319 f.properties.Dev_nodes_description_file = proptools.StringPtr(":" + ramdiskDevNodesDescription)
320 }
321}
322
Jiyong Park6f0f6882020-11-12 13:14:30 +0900323func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000324 if proptools.Bool(f.properties.Is_auto_generated) {
325 f.AddDeps(ctx, dependencyTagWithVisibilityEnforcementBypass)
326 } else {
327 f.AddDeps(ctx, dependencyTag)
328 }
Spandan Das71be42d2024-11-20 18:34:16 +0000329 if f.properties.Android_filesystem_deps.System != nil {
330 ctx.AddDependency(ctx.Module(), interPartitionDependencyTag, proptools.String(f.properties.Android_filesystem_deps.System))
331 }
332 if f.properties.Android_filesystem_deps.System_ext != nil {
333 ctx.AddDependency(ctx.Module(), interPartitionDependencyTag, proptools.String(f.properties.Android_filesystem_deps.System_ext))
334 }
Cole Faust34592c02024-12-13 11:20:24 -0800335 for _, partition := range f.properties.Import_aconfig_flags_from {
336 ctx.AddDependency(ctx.Module(), importAconfigDependencyTag, partition)
337 }
Jihoon Kang6da80752024-12-23 18:53:32 +0000338 for _, partition := range f.properties.Include_files_of {
339 ctx.AddDependency(ctx.Module(), interPartitionInstallDependencyTag, partition)
340 }
Jiyong Park6f0f6882020-11-12 13:14:30 +0900341}
342
Jiyong Park11a65972021-02-01 21:09:38 +0900343type fsType int
344
345const (
346 ext4Type fsType = iota
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000347 erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800348 f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900349 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900350 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900351 unknown
352)
353
Spandan Das7a46f6c2024-10-14 18:41:18 +0000354func (fs fsType) IsUnknown() bool {
355 return fs == unknown
356}
357
Cole Faust92ccbe22024-10-03 14:38:37 -0700358type FilesystemInfo struct {
Cole Faust44080412024-12-20 14:17:07 -0800359 // The built filesystem image
360 Output android.Path
Spandan Das1f0a5a12025-01-15 00:53:15 +0000361 // An additional hermetic filesystem image.
362 // e.g. this will contain inodes with pinned timestamps.
363 // This will be copied to target_files.zip
364 OutputHermetic android.Path
Cole Faust92ccbe22024-10-03 14:38:37 -0700365 // A text file containing the list of paths installed on the partition.
366 FileListFile android.Path
Cole Faust44080412024-12-20 14:17:07 -0800367 // The root staging directory used to build the output filesystem. If consuming this, make sure
368 // to add a dependency on the Output file, as you cannot add dependencies on directories
369 // in ninja.
370 RootDir android.Path
Cole Faust11fda332025-01-14 16:47:19 -0800371 // The rebased staging directory used to build the output filesystem. If consuming this, make
372 // sure to add a dependency on the Output file, as you cannot add dependencies on directories
373 // in ninja. In many cases this is the same as RootDir, only in the system partition is it
374 // different. There, it points to the "system" sub-directory of RootDir.
375 RebasedDir android.Path
Spandan Das33c9c472025-01-14 19:26:23 +0000376 // A text file with block data of the .img file
377 // This is an implicit output of `build_image`
378 MapFile android.Path
Cole Faust11fda332025-01-14 16:47:19 -0800379 // Name of the module that produced this FilesystemInfo origionally. (though it may be
380 // re-exported by super images or boot images)
381 ModuleName string
Cole Faust74ee4e02025-01-16 14:55:35 -0800382 // The property file generated by this module and passed to build_image.
383 // It's exported here so that system_other can reuse system's property file.
384 BuildImagePropFile android.Path
385 // Paths to all the tools referenced inside of the build image property file.
386 BuildImagePropFileDeps android.Paths
Cole Faustb8e280f2025-01-16 16:33:26 -0800387 // Packaging specs to be installed on the system_other image, for the initial boot's dexpreopt.
388 SpecsForSystemOther map[string]android.PackagingSpec
Cole Faust92ccbe22024-10-03 14:38:37 -0700389}
390
391var FilesystemProvider = blueprint.NewProvider[FilesystemInfo]()
392
Yu Liufc8d5c12025-01-09 00:19:06 +0000393type FilesystemDefaultsInfo struct {
394 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
395 // checks, and will be used in the future for API surface checks.
396 PartitionType string
397}
398
399var FilesystemDefaultsInfoProvider = blueprint.NewProvider[FilesystemDefaultsInfo]()
400
Spandan Das7a46f6c2024-10-14 18:41:18 +0000401func GetFsTypeFromString(ctx android.EarlyModuleContext, typeStr string) fsType {
Jiyong Park11a65972021-02-01 21:09:38 +0900402 switch typeStr {
403 case "ext4":
404 return ext4Type
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000405 case "erofs":
406 return erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800407 case "f2fs":
408 return f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900409 case "compressed_cpio":
410 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900411 case "cpio":
412 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900413 default:
Jiyong Park11a65972021-02-01 21:09:38 +0900414 return unknown
415 }
416}
417
Spandan Das7a46f6c2024-10-14 18:41:18 +0000418func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
419 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
420 fsType := GetFsTypeFromString(ctx, typeStr)
421 if fsType == unknown {
422 ctx.PropertyErrorf("type", "%q not supported", typeStr)
423 }
424 return fsType
425}
426
Jiyong Park65c49f52020-11-24 14:23:26 +0900427func (f *filesystem) installFileName() string {
Spandan Dasc49b85e2025-01-10 00:51:25 +0000428 return proptools.StringDefault(f.properties.Stem, f.BaseModuleName()+".img")
Jiyong Park65c49f52020-11-24 14:23:26 +0900429}
430
Inseob Kim53391842024-03-29 17:44:07 +0900431func (f *filesystem) partitionName() string {
432 return proptools.StringDefault(f.properties.Partition_name, f.Name())
433}
434
Kiyoung Kim67118212024-11-07 13:23:44 +0900435func (f *filesystem) FilterPackagingSpec(ps android.PackagingSpec) bool {
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000436 // Filesystem module respects the installation semantic. A PackagingSpec from a module with
437 // IsSkipInstall() is skipped.
Cole Faust76a6e952024-11-07 16:56:45 -0800438 if ps.SkipInstall() {
439 return false
Spandan Das6d056502024-10-21 15:40:32 +0000440 }
Cole Faust76a6e952024-11-07 16:56:45 -0800441 if proptools.Bool(f.properties.Is_auto_generated) { // TODO (spandandas): Remove this.
442 pt := f.PartitionType()
Cole Faustc88cff12024-11-12 13:24:05 -0800443 return ps.Partition() == pt || strings.HasPrefix(ps.Partition(), pt+"/")
Cole Faust76a6e952024-11-07 16:56:45 -0800444 }
445 return true
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000446}
447
Inseob Kim3c0a0422024-11-05 17:21:37 +0900448func (f *filesystem) ModifyPackagingSpec(ps *android.PackagingSpec) {
Cole Faustc88cff12024-11-12 13:24:05 -0800449 // Sometimes, android.modulePartition() returns a path with >1 path components.
450 // This makes the partition field of packagingSpecs have multiple components, like
451 // "system/product". Right now, the filesystem module doesn't look at the partition field
452 // when deciding what path to install the file under, only the RelPathInPackage field, so
453 // we move the later path components from partition to relPathInPackage. This should probably
454 // be revisited in the future.
455 prefix := f.PartitionType() + "/"
456 if strings.HasPrefix(ps.Partition(), prefix) {
457 subPartition := strings.TrimPrefix(ps.Partition(), prefix)
458 ps.SetPartition(f.PartitionType())
459 ps.SetRelPathInPackage(filepath.Join(subPartition, ps.RelPathInPackage()))
460 }
Inseob Kim3c0a0422024-11-05 17:21:37 +0900461}
462
Jiyong Park6f0f6882020-11-12 13:14:30 +0900463var pctx = android.NewPackageContext("android/soong/filesystem")
464
465func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900466 validatePartitionType(ctx, f)
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000467 if f.filesystemBuilder.ShouldUseVintfFragmentModuleOnly() {
468 f.validateVintfFragments(ctx)
469 }
Jihoon Kang6da80752024-12-23 18:53:32 +0000470
471 if len(f.properties.Include_files_of) > 0 && !android.InList(f.fsType(ctx), []fsType{compressedCpioType, cpioType}) {
472 ctx.PropertyErrorf("include_files_of", "include_files_of is only supported for cpio and compressed cpio filesystem types.")
473 }
474
Cole Faust62cfaeb2025-01-15 18:06:40 -0800475 rootDir := android.PathForModuleOut(ctx, f.rootDirString()).OutputPath
476 rebasedDir := rootDir
477 if f.properties.Base_dir != nil {
478 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
479 }
480 builder := android.NewRuleBuilder(pctx, ctx)
481
482 // Wipe the root dir to get rid of leftover files from prior builds
483 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
484 specs := f.gatherFilteredPackagingSpecs(ctx)
485 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
486
487 f.buildNonDepsFiles(ctx, builder, rootDir)
488 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
489 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
490 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
491 f.filesystemBuilder.BuildLinkerConfigFile(ctx, builder, rebasedDir)
492
Spandan Das33c9c472025-01-14 19:26:23 +0000493 var mapFile android.Path
Spandan Das1f0a5a12025-01-15 00:53:15 +0000494 var outputHermetic android.Path
Cole Faust74ee4e02025-01-16 14:55:35 -0800495 var buildImagePropFile android.Path
496 var buildImagePropFileDeps android.Paths
Jiyong Park11a65972021-02-01 21:09:38 +0900497 switch f.fsType(ctx) {
mrziwang1a6291f2024-11-07 14:29:25 -0800498 case ext4Type, erofsType, f2fsType:
Cole Faust74ee4e02025-01-16 14:55:35 -0800499 f.output, outputHermetic, buildImagePropFile, buildImagePropFileDeps = f.buildImageUsingBuildImage(ctx, builder, rootDir, rebasedDir)
Spandan Das33c9c472025-01-14 19:26:23 +0000500 mapFile = f.getMapFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900501 case compressedCpioType:
Cole Faust62cfaeb2025-01-15 18:06:40 -0800502 f.output = f.buildCpioImage(ctx, builder, rootDir, true)
Jiyong Park837cdb22021-02-05 00:17:14 +0900503 case cpioType:
Cole Faust62cfaeb2025-01-15 18:06:40 -0800504 f.output = f.buildCpioImage(ctx, builder, rootDir, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900505 default:
506 return
507 }
508
509 f.installDir = android.PathForModuleInstall(ctx, "etc")
510 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
mrziwang555d1332024-06-07 11:15:33 -0700511 ctx.SetOutputFiles([]android.Path{f.output}, "")
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900512
Jihoon Kang6da80752024-12-23 18:53:32 +0000513 if f.partitionName() == "recovery" {
514 rootDir = rootDir.Join(ctx, "root")
515 }
516
Cole Faust4e9f5922024-11-13 16:09:23 -0800517 fileListFile := android.PathForModuleOut(ctx, "fileList")
518 android.WriteFileRule(ctx, fileListFile, f.installedFilesList())
Cole Faust92ccbe22024-10-03 14:38:37 -0700519
Spandan Das33c9c472025-01-14 19:26:23 +0000520 fsInfo := FilesystemInfo{
Cole Faust74ee4e02025-01-16 14:55:35 -0800521 Output: f.output,
522 OutputHermetic: outputHermetic,
523 FileListFile: fileListFile,
524 RootDir: rootDir,
525 RebasedDir: rebasedDir,
526 MapFile: mapFile,
527 ModuleName: ctx.ModuleName(),
528 BuildImagePropFile: buildImagePropFile,
529 BuildImagePropFileDeps: buildImagePropFileDeps,
Cole Faustb8e280f2025-01-16 16:33:26 -0800530 SpecsForSystemOther: f.systemOtherFiles(ctx),
Spandan Das1f0a5a12025-01-15 00:53:15 +0000531 }
Spandan Das33c9c472025-01-14 19:26:23 +0000532
533 android.SetProvider(ctx, FilesystemProvider, fsInfo)
Spandan Das3ec6d062025-01-09 19:37:47 +0000534
Cole Faust4e9f5922024-11-13 16:09:23 -0800535 f.fileListFile = fileListFile
Cole Faust92ccbe22024-10-03 14:38:37 -0700536
537 if proptools.Bool(f.properties.Unchecked_module) {
538 ctx.UncheckedModule()
539 }
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900540}
541
Spandan Das33c9c472025-01-14 19:26:23 +0000542func (f *filesystem) getMapFile(ctx android.ModuleContext) android.WritablePath {
543 // create the filepath by replacing the extension of the corresponding img file
544 return android.PathForModuleOut(ctx, f.installFileName()).ReplaceExtension(ctx, "map")
545}
546
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000547func (f *filesystem) validateVintfFragments(ctx android.ModuleContext) {
548 visitedModule := map[string]bool{}
549 packagingSpecs := f.gatherFilteredPackagingSpecs(ctx)
550
551 moduleInFileSystem := func(mod android.Module) bool {
552 for _, ps := range android.OtherModuleProviderOrDefault(
553 ctx, mod, android.InstallFilesProvider).PackagingSpecs {
554 if _, ok := packagingSpecs[ps.RelPathInPackage()]; ok {
555 return true
556 }
557 }
558 return false
559 }
560
561 ctx.WalkDeps(func(child, parent android.Module) bool {
562 if visitedModule[child.Name()] {
563 return false
564 }
565 if !moduleInFileSystem(child) {
566 visitedModule[child.Name()] = true
567 return true
568 }
569 if vintfFragments := child.VintfFragments(ctx); vintfFragments != nil {
570 ctx.PropertyErrorf(
571 "vintf_fragments",
572 "Module %s is referenced by soong-defined filesystem %s with property vintf_fragments(%s) in use."+
573 " Use vintf_fragment_modules property instead.",
574 child.Name(),
575 f.BaseModuleName(),
576 strings.Join(vintfFragments, ", "),
577 )
578 }
579 visitedModule[child.Name()] = true
580 return true
581 })
582}
583
Cole Faust4e9f5922024-11-13 16:09:23 -0800584func (f *filesystem) appendToEntry(ctx android.ModuleContext, installedFile android.Path) {
Spandan Das420e16a2024-12-11 18:10:52 +0000585 partitionBaseDir := android.PathForModuleOut(ctx, f.rootDirString(), proptools.String(f.properties.Base_dir)).String() + "/"
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900586
587 relPath, inTargetPartition := strings.CutPrefix(installedFile.String(), partitionBaseDir)
588 if inTargetPartition {
589 f.entries = append(f.entries, relPath)
590 }
591}
592
593func (f *filesystem) installedFilesList() string {
594 installedFilePaths := android.FirstUniqueStrings(f.entries)
595 slices.Sort(installedFilePaths)
596
597 return strings.Join(installedFilePaths, "\n")
Jiyong Park11a65972021-02-01 21:09:38 +0900598}
599
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900600func validatePartitionType(ctx android.ModuleContext, p partition) {
601 if !android.InList(p.PartitionType(), validPartitions) {
602 ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, p.PartitionType())
603 }
604
Yu Liufc8d5c12025-01-09 00:19:06 +0000605 ctx.VisitDirectDepsProxyWithTag(android.DefaultsDepTag, func(m android.ModuleProxy) {
606 if fdm, ok := android.OtherModuleProvider(ctx, m, FilesystemDefaultsInfoProvider); ok {
607 if p.PartitionType() != fdm.PartitionType {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900608 ctx.PropertyErrorf("partition_type",
609 "%s doesn't match with the partition type %s of the filesystem default module %s",
Yu Liufc8d5c12025-01-09 00:19:06 +0000610 p.PartitionType(), fdm.PartitionType, m.Name())
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900611 }
612 }
613 })
614}
615
Cole Faust3b806d32024-03-11 15:15:03 -0700616// Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files
617// already in `rootDir`.
618func (f *filesystem) buildNonDepsFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.OutputPath) {
Inseob Kim14199b02021-02-09 21:18:31 +0900619 // create dirs and symlinks
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700620 for _, dir := range f.properties.Dirs.GetOrDefault(ctx, nil) {
Inseob Kim14199b02021-02-09 21:18:31 +0900621 // OutputPath.Join verifies dir
622 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
623 }
624
625 for _, symlink := range f.properties.Symlinks {
626 name := strings.TrimSpace(proptools.String(symlink.Name))
627 target := strings.TrimSpace(proptools.String(symlink.Target))
628
629 if name == "" {
630 ctx.PropertyErrorf("symlinks", "Name can't be empty")
631 continue
632 }
633
634 if target == "" {
635 ctx.PropertyErrorf("symlinks", "Target can't be empty")
636 continue
637 }
638
639 // OutputPath.Join verifies name. don't need to verify target.
640 dst := rootDir.Join(ctx, name)
Cole Faust3b806d32024-03-11 15:15:03 -0700641 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 +0900642 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
643 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900644 f.appendToEntry(ctx, dst)
Inseob Kim14199b02021-02-09 21:18:31 +0900645 }
Jihoon Kang89e8a692024-12-18 19:28:33 +0000646
647 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=2835;drc=b186569ef00ff2f2a1fab28aedc75ebc32bcd67b
648 if f.partitionName() == "recovery" {
649 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, "root/linkerconfig").String())
650 builder.Command().Text("touch").Text(rootDir.Join(ctx, "root/linkerconfig/ld.config.txt").String())
651 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900652}
653
Inseob Kim33f95a92024-07-11 15:44:49 +0900654func (f *filesystem) copyPackagingSpecs(ctx android.ModuleContext, builder *android.RuleBuilder, specs map[string]android.PackagingSpec, rootDir, rebasedDir android.WritablePath) []string {
655 rootDirSpecs := make(map[string]android.PackagingSpec)
656 rebasedDirSpecs := make(map[string]android.PackagingSpec)
657
658 for rel, spec := range specs {
659 if spec.Partition() == "root" {
660 rootDirSpecs[rel] = spec
661 } else {
662 rebasedDirSpecs[rel] = spec
663 }
664 }
665
666 dirsToSpecs := make(map[android.WritablePath]map[string]android.PackagingSpec)
667 dirsToSpecs[rootDir] = rootDirSpecs
668 dirsToSpecs[rebasedDir] = rebasedDirSpecs
669
670 return f.CopySpecsToDirs(ctx, builder, dirsToSpecs)
671}
672
Spandan Das420e16a2024-12-11 18:10:52 +0000673func (f *filesystem) rootDirString() string {
674 return f.partitionName()
675}
676
Cole Faust62cfaeb2025-01-15 18:06:40 -0800677func (f *filesystem) buildImageUsingBuildImage(
678 ctx android.ModuleContext,
679 builder *android.RuleBuilder,
680 rootDir android.OutputPath,
681 rebasedDir android.OutputPath,
Cole Faust74ee4e02025-01-16 14:55:35 -0800682) (android.Path, android.Path, android.Path, android.Paths) {
Nikita Ioffe519015f2022-12-23 15:36:29 +0000683 // run host_init_verifier
684 // Ideally we should have a concept of pluggable linters that verify the generated image.
685 // While such concept is not implement this will do.
686 // TODO(b/263574231): substitute with pluggable linter.
687 builder.Command().
688 BuiltTool("host_init_verifier").
689 FlagWithArg("--out_system=", rootDir.String()+"/system")
690
Jiyong Park72678312021-01-18 17:29:49 +0900691 propFile, toolDeps := f.buildPropFile(ctx)
Cole Fauste1676122024-12-03 17:32:25 -0800692
693 // Most of the time, if build_image were to call a host tool, it accepts the path to the
694 // host tool in a field in the prop file. However, it doesn't have that option for fec, which
695 // it expects to just be on the PATH. Add fec to the PATH.
696 fec := ctx.Config().HostToolPath(ctx, "fec")
697 pathToolDirs := []string{filepath.Dir(fec.String())}
698
Cole Faust4e9f5922024-11-13 16:09:23 -0800699 output := android.PathForModuleOut(ctx, f.installFileName())
Spandan Das33c9c472025-01-14 19:26:23 +0000700 builder.Command().Text("touch").Output(f.getMapFile(ctx))
Cole Fauste1676122024-12-03 17:32:25 -0800701 builder.Command().
702 Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")).
703 BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900704 Text(rootDir.String()). // input directory
705 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900706 Implicits(toolDeps).
Cole Fauste1676122024-12-03 17:32:25 -0800707 Implicit(fec).
Jiyong Park11a65972021-02-01 21:09:38 +0900708 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900709 Text(rootDir.String()) // directory where to find fs_config_files|dirs
710
Spandan Das1f0a5a12025-01-15 00:53:15 +0000711 // Add an additional cmd to create a hermetic img file. This will contain pinned timestamps e.g.
712 propFilePinnedTimestamp := android.PathForModuleOut(ctx, "for_target_files", "prop")
Spandan Das0c1a68b2025-01-17 20:11:35 +0000713 builder.Command().Textf("cat").Input(propFile).Flag(">").Output(propFilePinnedTimestamp).
714 Textf(" && echo use_fixed_timestamp=true >> %s", propFilePinnedTimestamp).
715 Textf(" && echo block_list=%s >> %s", f.getMapFile(ctx).String(), propFilePinnedTimestamp) // mapfile will be an implicit output
Spandan Das1f0a5a12025-01-15 00:53:15 +0000716
717 outputHermetic := android.PathForModuleOut(ctx, "for_target_files", f.installFileName())
718 builder.Command().
719 Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")).
720 BuiltTool("build_image").
721 Text(rootDir.String()). // input directory
722 Flag(propFilePinnedTimestamp.String()).
723 Implicits(toolDeps).
724 Implicit(fec).
725 Output(outputHermetic).
726 Text(rootDir.String()) // directory where to find fs_config_files|dirs
727
Jihoon Kang983dd882025-01-13 23:14:11 +0000728 if f.properties.Partition_size != nil {
729 assertMaxImageSize(builder, output, *f.properties.Partition_size, false)
730 }
731
Jiyong Park6f0f6882020-11-12 13:14:30 +0900732 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800733 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900734
Cole Faust74ee4e02025-01-16 14:55:35 -0800735 return output, outputHermetic, propFile, toolDeps
Jiyong Park65c49f52020-11-24 14:23:26 +0900736}
737
Cole Faust4e9f5922024-11-13 16:09:23 -0800738func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.Path {
Inseob Kimcc8e5362021-02-03 14:05:24 +0900739 builder := android.NewRuleBuilder(pctx, ctx)
740 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
741 builder.Command().BuiltTool("sefcontext_compile").
742 FlagWithOutput("-o ", fcBin).
743 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
744 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
Cole Faust4e9f5922024-11-13 16:09:23 -0800745 return fcBin
Inseob Kimcc8e5362021-02-03 14:05:24 +0900746}
747
Cole Faust4e9f5922024-11-13 16:09:23 -0800748func (f *filesystem) buildPropFile(ctx android.ModuleContext) (android.Path, android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +0900749 var deps android.Paths
Cole Fauste03ab892025-01-17 13:55:04 -0800750 var lines []string
Jiyong Park72678312021-01-18 17:29:49 +0900751 addStr := func(name string, value string) {
Cole Fauste03ab892025-01-17 13:55:04 -0800752 lines = append(lines, fmt.Sprintf("%s=%s", name, value))
Jiyong Park72678312021-01-18 17:29:49 +0900753 }
754 addPath := func(name string, path android.Path) {
Cole Faustcec230a2024-03-07 15:51:12 -0800755 addStr(name, path.String())
Jiyong Park72678312021-01-18 17:29:49 +0900756 deps = append(deps, path)
757 }
758
Jiyong Park11a65972021-02-01 21:09:38 +0900759 // Type string that build_image.py accepts.
760 fsTypeStr := func(t fsType) string {
761 switch t {
Spandan Das94668822024-10-09 20:51:33 +0000762 // TODO(372522486): add more types like f2fs, erofs, etc.
Jiyong Park11a65972021-02-01 21:09:38 +0900763 case ext4Type:
764 return "ext4"
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000765 case erofsType:
766 return "erofs"
mrziwang1a6291f2024-11-07 14:29:25 -0800767 case f2fsType:
768 return "f2fs"
Jiyong Park11a65972021-02-01 21:09:38 +0900769 }
770 panic(fmt.Errorf("unsupported fs type %v", t))
771 }
772
773 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim376d72f2023-11-01 15:40:25 +0900774 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +0900775 addStr("use_dynamic_partition_size", "true")
776 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
777 // b/177813163 deps of the host tools have to be added. Remove this.
778 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
779 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
780 }
781
Jiyong Park71baa762021-01-18 21:11:03 +0900782 if proptools.Bool(f.properties.Use_avb) {
783 addStr("avb_hashtree_enable", "true")
784 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
785 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
786 addStr("avb_algorithm", algorithm)
Cole Fauste1676122024-12-03 17:32:25 -0800787 if f.properties.Avb_private_key != nil {
788 key := android.PathForModuleSrc(ctx, *f.properties.Avb_private_key)
789 addPath("avb_key_path", key)
790 }
Inseob Kim53391842024-03-29 17:44:07 +0900791 addStr("partition_name", f.partitionName())
Cole Fauste1676122024-12-03 17:32:25 -0800792 avb_add_hashtree_footer_args := ""
793 if !proptools.BoolDefault(f.properties.Use_fec, true) {
794 avb_add_hashtree_footer_args += " --do_not_generate_fec"
795 }
Shikha Panware6f30632022-12-21 12:54:45 +0000796 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
797 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
798 }
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000799 if f.properties.Rollback_index != nil {
800 rollbackIndex := proptools.Int(f.properties.Rollback_index)
801 if rollbackIndex < 0 {
802 ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
803 }
804 avb_add_hashtree_footer_args += " --rollback_index " + strconv.Itoa(rollbackIndex)
805 }
Cole Fauste1676122024-12-03 17:32:25 -0800806 avb_add_hashtree_footer_args += fmt.Sprintf(" --prop com.android.build.%s.os_version:%s", f.partitionName(), ctx.Config().PlatformVersionLastStable())
Cole Faustefeb5c42024-12-16 10:47:26 -0800807 // We're not going to add BuildFingerPrintFile as a dep. If it changed, it's likely because
808 // the build number changed, and we don't want to trigger rebuilds solely based on the build
809 // number.
Cole Fauste1676122024-12-03 17:32:25 -0800810 avb_add_hashtree_footer_args += fmt.Sprintf(" --prop com.android.build.%s.fingerprint:{CONTENTS_OF:%s}", f.partitionName(), ctx.Config().BuildFingerprintFile(ctx))
Spandan Dase5c393c2024-12-12 19:25:07 +0000811 if f.properties.Security_patch != nil && proptools.String(f.properties.Security_patch) != "" {
812 avb_add_hashtree_footer_args += fmt.Sprintf(" --prop com.android.build.%s.security_patch:%s", f.partitionName(), proptools.String(f.properties.Security_patch))
813 }
Shikha Panware6f30632022-12-21 12:54:45 +0000814 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jiyong Park71baa762021-01-18 21:11:03 +0900815 }
816
Cole Faust0d467052024-12-04 17:19:19 -0800817 if f.properties.File_contexts != nil && f.properties.Precompiled_file_contexts != nil {
818 ctx.ModuleErrorf("file_contexts and precompiled_file_contexts cannot both be set")
819 } else if f.properties.File_contexts != nil {
Inseob Kimcc8e5362021-02-03 14:05:24 +0900820 addPath("selinux_fc", f.buildFileContexts(ctx))
Cole Faust0d467052024-12-04 17:19:19 -0800821 } else if f.properties.Precompiled_file_contexts != nil {
822 src := android.PathForModuleSrc(ctx, *f.properties.Precompiled_file_contexts)
823 if src != nil {
824 addPath("selinux_fc", src)
825 }
Inseob Kimcc8e5362021-02-03 14:05:24 +0900826 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900827 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
828 addStr("timestamp", timestamp)
Spandan Dasa0ddc512025-01-06 20:23:55 +0000829 } else if ctx.Config().Getenv("USE_FIXED_TIMESTAMP_IMG_FILES") == "true" {
830 addStr("use_fixed_timestamp", "true")
Jooyung Han65f402b2022-04-21 14:24:04 +0900831 }
Spandan Dasa0ddc512025-01-06 20:23:55 +0000832
Jooyung Han65f402b2022-04-21 14:24:04 +0900833 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
834 addStr("uuid", uuid)
835 addStr("hash_seed", uuid)
836 }
mrziwang1a6291f2024-11-07 14:29:25 -0800837
Jihoon Kang40551e62025-01-14 21:55:08 +0000838 // Disable sparse only when partition size is not defined. disable_sparse has the same
839 // effect as <partition name>_disable_sparse.
840 if f.properties.Partition_size == nil {
841 addStr("disable_sparse", "true")
842 }
Cole Faust43a52c72024-11-26 12:46:08 -0800843
mrziwang1a6291f2024-11-07 14:29:25 -0800844 fst := f.fsType(ctx)
845 switch fst {
846 case erofsType:
847 // Add erofs properties
Cole Faust3e730972024-12-03 13:12:08 -0800848 addStr("erofs_default_compressor", proptools.StringDefault(f.properties.Erofs.Compressor, "lz4hc,9"))
849 if f.properties.Erofs.Compress_hints != nil {
850 src := android.PathForModuleSrc(ctx, *f.properties.Erofs.Compress_hints)
851 addPath("erofs_default_compress_hints", src)
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000852 }
853 if proptools.BoolDefault(f.properties.Erofs.Sparse, true) {
854 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2292;bpv=1;bpt=0;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b
855 addStr("erofs_sparse_flag", "-s")
856 }
mrziwang1a6291f2024-11-07 14:29:25 -0800857 case f2fsType:
858 if proptools.BoolDefault(f.properties.F2fs.Sparse, true) {
859 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2294;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b;bpv=1;bpt=0
860 addStr("f2fs_sparse_flag", "-S")
861 }
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000862 }
mrziwang1a6291f2024-11-07 14:29:25 -0800863 f.checkFsTypePropertyError(ctx, fst, fsTypeStr(fst))
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000864
Jihoon Kang983dd882025-01-13 23:14:11 +0000865 if f.properties.Partition_size != nil {
866 addStr("partition_size", strconv.FormatInt(*f.properties.Partition_size, 10))
867 }
868
Jihoon Kang6d08d922025-01-14 18:31:57 +0000869 if proptools.BoolDefault(f.properties.Support_casefolding, false) {
870 addStr("needs_casefold", "1")
871 }
872
873 if proptools.BoolDefault(f.properties.Support_project_quota, false) {
874 addStr("needs_projid", "1")
875 }
876
877 if proptools.BoolDefault(f.properties.Enable_compression, false) {
878 addStr("needs_compress", "1")
879 }
880
Cole Fauste03ab892025-01-17 13:55:04 -0800881 sort.Strings(lines)
882
Cole Fauste1676122024-12-03 17:32:25 -0800883 propFilePreProcessing := android.PathForModuleOut(ctx, "prop_pre_processing")
Cole Fauste03ab892025-01-17 13:55:04 -0800884 android.WriteFileRule(ctx, propFilePreProcessing, strings.Join(lines, "\n"))
Cole Faust4e9f5922024-11-13 16:09:23 -0800885 propFile := android.PathForModuleOut(ctx, "prop")
Cole Fauste1676122024-12-03 17:32:25 -0800886 ctx.Build(pctx, android.BuildParams{
Cole Faustefeb5c42024-12-16 10:47:26 -0800887 Rule: textFileProcessorRule,
888 Input: propFilePreProcessing,
889 Output: propFile,
Cole Fauste1676122024-12-03 17:32:25 -0800890 })
Jiyong Park72678312021-01-18 17:29:49 +0900891 return propFile, deps
892}
893
mrziwang1a6291f2024-11-07 14:29:25 -0800894// This method checks if there is any property set for the fstype(s) other than
895// the current fstype.
896func (f *filesystem) checkFsTypePropertyError(ctx android.ModuleContext, t fsType, fs string) {
897 raiseError := func(otherFsType, currentFsType string) {
898 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)
899 ctx.PropertyErrorf(otherFsType, errMsg)
900 }
901
902 if t != erofsType {
903 if f.properties.Erofs.Compressor != nil || f.properties.Erofs.Compress_hints != nil || f.properties.Erofs.Sparse != nil {
904 raiseError("erofs", fs)
905 }
906 }
907 if t != f2fsType {
908 if f.properties.F2fs.Sparse != nil {
909 raiseError("f2fs", fs)
910 }
911 }
912}
913
Jihoon Kang6da80752024-12-23 18:53:32 +0000914func includeFilesRootDir(ctx android.ModuleContext) (rootDirs android.Paths, partitions android.Paths) {
915 ctx.VisitDirectDepsWithTag(interPartitionInstallDependencyTag, func(m android.Module) {
916 if fsProvider, ok := android.OtherModuleProvider(ctx, m, FilesystemProvider); ok {
917 rootDirs = append(rootDirs, fsProvider.RootDir)
918 partitions = append(partitions, fsProvider.Output)
919 } else {
920 ctx.PropertyErrorf("include_files_of", "only filesystem modules can be listed in "+
921 "include_files_of but %s is not a filesystem module", m.Name())
922 }
923 })
924 return rootDirs, partitions
925}
926
Cole Faust62cfaeb2025-01-15 18:06:40 -0800927func (f *filesystem) buildCpioImage(
928 ctx android.ModuleContext,
929 builder *android.RuleBuilder,
930 rootDir android.OutputPath,
931 compressed bool,
932) android.Path {
Jiyong Park11a65972021-02-01 21:09:38 +0900933 if proptools.Bool(f.properties.Use_avb) {
934 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
935 "Consider adding this to bootimg module and signing the entire boot image.")
936 }
937
Inseob Kimcc8e5362021-02-03 14:05:24 +0900938 if proptools.String(f.properties.File_contexts) != "" {
939 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
940 }
941
Jihoon Kang6da80752024-12-23 18:53:32 +0000942 rootDirs, partitions := includeFilesRootDir(ctx)
943
Cole Faust4e9f5922024-11-13 16:09:23 -0800944 output := android.PathForModuleOut(ctx, f.installFileName())
Jiyong Park837cdb22021-02-05 00:17:14 +0900945 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900946 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900947 Text(rootDir.String()) // input directory
Jihoon Kang6da80752024-12-23 18:53:32 +0000948
949 for i := range len(rootDirs) {
950 cmd.Text(rootDirs[i].String())
951 }
952 cmd.Implicits(partitions)
953
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000954 if nodeList := f.properties.Dev_nodes_description_file; nodeList != nil {
955 cmd.FlagWithInput("-n ", android.PathForModuleSrc(ctx, proptools.String(nodeList)))
956 }
Jiyong Park837cdb22021-02-05 00:17:14 +0900957 if compressed {
958 cmd.Text("|").
959 BuiltTool("lz4").
960 Flag("--favor-decSpeed"). // for faster boot
961 Flag("-12"). // maximum compression level
962 Flag("-l"). // legacy format for kernel
963 Text(">").Output(output)
964 } else {
965 cmd.Text(">").Output(output)
966 }
Jiyong Park11a65972021-02-01 21:09:38 +0900967
968 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900969 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900970
Cole Faust62cfaeb2025-01-15 18:06:40 -0800971 return output
Jiyong Park11a65972021-02-01 21:09:38 +0900972}
973
Cole Faust4a2a7c92024-03-12 12:44:40 -0700974var validPartitions = []string{
975 "system",
976 "userdata",
977 "cache",
978 "system_other",
979 "vendor",
980 "product",
981 "system_ext",
982 "odm",
983 "vendor_dlkm",
984 "odm_dlkm",
985 "system_dlkm",
Cole Faust76a6e952024-11-07 16:56:45 -0800986 "ramdisk",
Cole Faust24938e22024-11-18 14:01:58 -0800987 "vendor_ramdisk",
Jihoon Kang3216c982024-12-02 19:42:20 +0000988 "recovery",
Cole Faust4a2a7c92024-03-12 12:44:40 -0700989}
990
Inseob Kimb7b84572024-04-30 10:51:47 +0900991func (f *filesystem) buildEventLogtagsFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
992 if !proptools.Bool(f.properties.Build_logtags) {
993 return
994 }
995
Inseob Kimb7b84572024-04-30 10:51:47 +0900996 etcPath := rebasedDir.Join(ctx, "etc")
997 eventLogtagsPath := etcPath.Join(ctx, "event-log-tags")
998 builder.Command().Text("mkdir").Flag("-p").Text(etcPath.String())
Cole Fauste4506af2024-12-11 14:14:50 -0800999 builder.Command().Text("cp").Input(android.MergedLogtagsPath(ctx)).Text(eventLogtagsPath.String())
Kiyoung Kim99a954d2024-06-21 14:22:20 +09001000
1001 f.appendToEntry(ctx, eventLogtagsPath)
Inseob Kimb7b84572024-04-30 10:51:47 +09001002}
1003
Kiyoung Kim67118212024-11-07 13:23:44 +09001004func (f *filesystem) BuildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
Spandan Das2047a4c2024-11-11 21:24:58 +00001005 if !proptools.Bool(f.properties.Linker_config.Gen_linker_config) {
Spandan Das92631882024-10-28 22:49:38 +00001006 return
1007 }
1008
Spandan Das918191e2024-10-31 18:27:23 +00001009 provideModules, _ := f.getLibsForLinkerConfig(ctx)
Cole Faustfee27012024-12-13 14:10:31 -08001010 intermediateOutput := android.PathForModuleOut(ctx, "linker.config.pb")
1011 linkerconfig.BuildLinkerConfig(ctx, android.PathsForModuleSrc(ctx, f.properties.Linker_config.Linker_config_srcs), provideModules, nil, intermediateOutput)
Spandan Das92631882024-10-28 22:49:38 +00001012 output := rebasedDir.Join(ctx, "etc", "linker.config.pb")
Cole Faustfee27012024-12-13 14:10:31 -08001013 builder.Command().Text("cp").Input(intermediateOutput).Output(output)
Spandan Das92631882024-10-28 22:49:38 +00001014
1015 f.appendToEntry(ctx, output)
1016}
1017
Kiyoung Kim23be5bb2024-11-27 00:50:30 +00001018func (f *filesystem) ShouldUseVintfFragmentModuleOnly() bool {
1019 return false
1020}
1021
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001022type partition interface {
1023 PartitionType() string
1024}
1025
Cole Faust9a24d902024-03-18 15:38:12 -07001026func (f *filesystem) PartitionType() string {
1027 return proptools.StringDefault(f.properties.Partition_type, "system")
1028}
1029
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001030var _ partition = (*filesystem)(nil)
1031
Jiyong Park65c49f52020-11-24 14:23:26 +09001032var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
1033
1034// Implements android.AndroidMkEntriesProvider
1035func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
1036 return []android.AndroidMkEntries{android.AndroidMkEntries{
1037 Class: "ETC",
1038 OutputFile: android.OptionalPathForPath(f.output),
1039 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07001040 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -08001041 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +09001042 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
Kiyoung Kim99a954d2024-06-21 14:22:20 +09001043 entries.SetString("LOCAL_FILESYSTEM_FILELIST", f.fileListFile.String())
Jiyong Park65c49f52020-11-24 14:23:26 +09001044 },
1045 },
1046 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +09001047}
Jiyong Park12a719c2021-01-07 15:31:24 +09001048
1049// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
1050// package to have access to the output file.
1051type Filesystem interface {
1052 android.Module
1053 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +09001054
1055 // Returns the output file that is signed by avbtool. If this module is not signed, returns
1056 // nil.
1057 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +09001058}
1059
1060var _ Filesystem = (*filesystem)(nil)
1061
1062func (f *filesystem) OutputPath() android.Path {
1063 return f.output
1064}
Jiyong Park972e06c2021-03-15 23:32:49 +09001065
1066func (f *filesystem) SignedOutputPath() android.Path {
1067 if proptools.Bool(f.properties.Use_avb) {
1068 return f.OutputPath()
1069 }
1070 return nil
1071}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +09001072
1073// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
1074// Note that "apex" module installs its contents to "apex"(fake partition) as well
1075// for symbol lookup by imitating "activated" paths.
1076func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Cole Faustb8e280f2025-01-16 16:33:26 -08001077 return f.PackagingBase.GatherPackagingSpecsWithFilterAndModifier(ctx, f.filesystemBuilder.FilterPackagingSpec, f.filesystemBuilder.ModifyPackagingSpec)
1078}
1079
1080// Dexpreopt files are installed to system_other. Collect the packaingSpecs for the dexpreopt files
1081// from this partition to export to the system_other partition later.
1082func (f *filesystem) systemOtherFiles(ctx android.ModuleContext) map[string]android.PackagingSpec {
1083 filter := func(spec android.PackagingSpec) bool {
1084 // For some reason system_other packaging specs don't set the partition field.
1085 return strings.HasPrefix(spec.RelPathInPackage(), "system_other/")
1086 }
1087 modifier := func(spec *android.PackagingSpec) {
1088 spec.SetRelPathInPackage(strings.TrimPrefix(spec.RelPathInPackage(), "system_other/"))
1089 spec.SetPartition("system_other")
1090 }
1091 return f.PackagingBase.GatherPackagingSpecsWithFilterAndModifier(ctx, filter, modifier)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +09001092}
Jooyung Han65f402b2022-04-21 14:24:04 +09001093
1094func sha1sum(values []string) string {
1095 h := sha256.New()
1096 for _, value := range values {
1097 io.WriteString(h, value)
1098 }
1099 return fmt.Sprintf("%x", h.Sum(nil))
1100}
Jooyung Hane6067592023-03-16 13:11:17 +09001101
1102// Base cc.UseCoverage
1103
1104var _ cc.UseCoverage = (*filesystem)(nil)
1105
Colin Crosse1a85552024-06-14 12:17:37 -07001106func (*filesystem) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +09001107 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
1108}
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001109
1110// android_filesystem_defaults
1111
1112type filesystemDefaults struct {
1113 android.ModuleBase
1114 android.DefaultsModuleBase
1115
Inseob Kim3c0a0422024-11-05 17:21:37 +09001116 properties FilesystemProperties
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001117}
1118
1119// android_filesystem_defaults is a default module for android_filesystem and android_system_image
1120func filesystemDefaultsFactory() android.Module {
1121 module := &filesystemDefaults{}
1122 module.AddProperties(&module.properties)
1123 module.AddProperties(&android.PackagingProperties{})
1124 android.InitDefaultsModule(module)
1125 return module
1126}
1127
1128func (f *filesystemDefaults) PartitionType() string {
1129 return proptools.StringDefault(f.properties.Partition_type, "system")
1130}
1131
1132var _ partition = (*filesystemDefaults)(nil)
1133
1134func (f *filesystemDefaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1135 validatePartitionType(ctx, f)
Yu Liufc8d5c12025-01-09 00:19:06 +00001136 android.SetProvider(ctx, FilesystemDefaultsInfoProvider, FilesystemDefaultsInfo{
1137 PartitionType: f.PartitionType(),
1138 })
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001139}
Spandan Das918191e2024-10-31 18:27:23 +00001140
1141// getLibsForLinkerConfig returns
1142// 1. A list of libraries installed in this filesystem
1143// 2. A list of dep libraries _not_ installed in this filesystem
1144//
1145// `linkerconfig.BuildLinkerConfig` will convert these two to a linker.config.pb for the filesystem
1146// (1) will be added to --provideLibs if they are C libraries with a stable interface (has stubs)
1147// (2) will be added to --requireLibs if they are C libraries with a stable interface (has stubs)
Yu Liu68a70b72025-01-08 22:54:44 +00001148func (f *filesystem) getLibsForLinkerConfig(ctx android.ModuleContext) ([]android.ModuleProxy, []android.ModuleProxy) {
Spandan Das918191e2024-10-31 18:27:23 +00001149 // we need "Module"s for packaging items
Yu Liu68a70b72025-01-08 22:54:44 +00001150 modulesInPackageByModule := make(map[android.ModuleProxy]bool)
Spandan Das918191e2024-10-31 18:27:23 +00001151 modulesInPackageByName := make(map[string]bool)
1152
1153 deps := f.gatherFilteredPackagingSpecs(ctx)
Yu Liu68a70b72025-01-08 22:54:44 +00001154 ctx.WalkDepsProxy(func(child, parent android.ModuleProxy) bool {
1155 if !android.OtherModuleProviderOrDefault(ctx, child, android.CommonModuleInfoKey).Enabled {
Yu Liu9c6b6762025-01-08 22:04:35 +00001156 return false
1157 }
Spandan Das918191e2024-10-31 18:27:23 +00001158 for _, ps := range android.OtherModuleProviderOrDefault(
1159 ctx, child, android.InstallFilesProvider).PackagingSpecs {
Spandan Dasecf667f2024-12-05 00:58:56 +00001160 if _, ok := deps[ps.RelPathInPackage()]; ok && ps.Partition() == f.PartitionType() {
Spandan Das918191e2024-10-31 18:27:23 +00001161 modulesInPackageByModule[child] = true
1162 modulesInPackageByName[child.Name()] = true
1163 return true
1164 }
1165 }
1166 return true
1167 })
1168
Yu Liu68a70b72025-01-08 22:54:44 +00001169 provideModules := make([]android.ModuleProxy, 0, len(modulesInPackageByModule))
Spandan Das918191e2024-10-31 18:27:23 +00001170 for mod := range modulesInPackageByModule {
1171 provideModules = append(provideModules, mod)
1172 }
1173
Yu Liu68a70b72025-01-08 22:54:44 +00001174 var requireModules []android.ModuleProxy
1175 ctx.WalkDepsProxy(func(child, parent android.ModuleProxy) bool {
1176 if !android.OtherModuleProviderOrDefault(ctx, child, android.CommonModuleInfoKey).Enabled {
Yu Liu9c6b6762025-01-08 22:04:35 +00001177 return false
1178 }
Spandan Das918191e2024-10-31 18:27:23 +00001179 _, parentInPackage := modulesInPackageByModule[parent]
1180 _, childInPackageName := modulesInPackageByName[child.Name()]
1181
1182 // When parent is in the package, and child (or its variant) is not, this can be from an interface.
1183 if parentInPackage && !childInPackageName {
1184 requireModules = append(requireModules, child)
1185 }
1186 return true
1187 })
1188
1189 return provideModules, requireModules
1190}
Cole Faust26bdac52024-11-19 13:37:53 -08001191
1192// Checks that the given file doesn't exceed the given size, and will also print a warning
1193// if it's nearing the maximum size. Equivalent to assert-max-image-size in make:
1194// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/definitions.mk;l=3455;drc=993c4de29a02a6accd60ceaaee153307e1a18d10
1195func assertMaxImageSize(builder *android.RuleBuilder, image android.Path, maxSize int64, addAvbLater bool) {
1196 if addAvbLater {
1197 // The value 69632 is derived from MAX_VBMETA_SIZE + MAX_FOOTER_SIZE in avbtool.
1198 // Logic copied from make:
1199 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=228;drc=a6a0007ef24e16c0b79f439beac4a118416717e6
1200 maxSize -= 69632
1201 }
1202 cmd := builder.Command()
1203 cmd.Textf(`file="%s"; maxsize="%d";`+
1204 `total=$(stat -c "%%s" "$file" | tr -d '\n');`+
1205 `if [ "$total" -gt "$maxsize" ]; then `+
1206 ` echo "error: $file too large ($total > $maxsize)";`+
1207 ` false;`+
1208 `elif [ "$total" -gt $((maxsize - 32768)) ]; then `+
1209 ` echo "WARNING: $file approaching size limit ($total now; limit $maxsize)";`+
1210 `fi`,
1211 image, maxSize)
1212 cmd.Implicit(image)
1213}
Spandan Das71be42d2024-11-20 18:34:16 +00001214
1215// addAutogeneratedRroDeps walks the transitive closure of vendor and product partitions.
1216// It visits apps installed in system and system_ext partitions, and adds the autogenerated
1217// RRO modules to its own deps.
1218func addAutogeneratedRroDeps(ctx android.BottomUpMutatorContext) {
1219 f, ok := ctx.Module().(*filesystem)
1220 if !ok {
1221 return
1222 }
1223 thisPartition := f.PartitionType()
1224 if thisPartition != "vendor" && thisPartition != "product" {
Cole Faust34592c02024-12-13 11:20:24 -08001225 if f.properties.Android_filesystem_deps.System != nil {
1226 ctx.PropertyErrorf("android_filesystem_deps.system", "only vendor or product partitions can use android_filesystem_deps")
1227 }
1228 if f.properties.Android_filesystem_deps.System_ext != nil {
1229 ctx.PropertyErrorf("android_filesystem_deps.system_ext", "only vendor or product partitions can use android_filesystem_deps")
1230 }
Spandan Das71be42d2024-11-20 18:34:16 +00001231 return
1232 }
1233 ctx.WalkDeps(func(child, parent android.Module) bool {
1234 depTag := ctx.OtherModuleDependencyTag(child)
1235 if parent.Name() == f.Name() && depTag != interPartitionDependencyTag {
1236 return false // This is a module listed in deps of vendor/product filesystem
1237 }
1238 if vendorOverlay := java.AutogeneratedRroModuleName(ctx, child.Name(), "vendor"); ctx.OtherModuleExists(vendorOverlay) && thisPartition == "vendor" {
1239 ctx.AddFarVariationDependencies(nil, dependencyTagWithVisibilityEnforcementBypass, vendorOverlay)
1240 }
1241 if productOverlay := java.AutogeneratedRroModuleName(ctx, child.Name(), "product"); ctx.OtherModuleExists(productOverlay) && thisPartition == "product" {
1242 ctx.AddFarVariationDependencies(nil, dependencyTagWithVisibilityEnforcementBypass, productOverlay)
1243 }
1244 return true
1245 })
1246}