blob: 306710c7cf5e0e028ba657f4313daf25d16943b2 [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"
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +000023 "strconv"
Inseob Kim14199b02021-02-09 21:18:31 +090024 "strings"
Jiyong Park6f0f6882020-11-12 13:14:30 +090025
26 "android/soong/android"
Jooyung Hane6067592023-03-16 13:11:17 +090027 "android/soong/cc"
Spandan Das71be42d2024-11-20 18:34:16 +000028 "android/soong/java"
Spandan Das92631882024-10-28 22:49:38 +000029 "android/soong/linkerconfig"
Jiyong Park65b62242020-11-25 12:44:59 +090030
31 "github.com/google/blueprint"
Jiyong Park71baa762021-01-18 21:11:03 +090032 "github.com/google/blueprint/proptools"
Jiyong Park6f0f6882020-11-12 13:14:30 +090033)
34
35func init() {
Jooyung Han9706cbc2021-04-15 22:43:48 +090036 registerBuildComponents(android.InitRegistrationContext)
Spandan Das71be42d2024-11-20 18:34:16 +000037 registerMutators(android.InitRegistrationContext)
Jooyung Han9706cbc2021-04-15 22:43:48 +090038}
39
40func registerBuildComponents(ctx android.RegistrationContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -070041 ctx.RegisterModuleType("android_filesystem", FilesystemFactory)
Jiyong Parkf46b1af2024-04-05 18:13:33 +090042 ctx.RegisterModuleType("android_filesystem_defaults", filesystemDefaultsFactory)
Jihoon Kang98047cf2024-10-02 17:13:54 +000043 ctx.RegisterModuleType("android_system_image", SystemImageFactory)
Jiyong Parkbc485482022-11-15 22:31:49 +090044 ctx.RegisterModuleType("avb_add_hash_footer", avbAddHashFooterFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090045 ctx.RegisterModuleType("avb_add_hash_footer_defaults", avbAddHashFooterDefaultsFactory)
Alice Wang000e3a32023-01-03 16:11:20 +000046 ctx.RegisterModuleType("avb_gen_vbmeta_image", avbGenVbmetaImageFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090047 ctx.RegisterModuleType("avb_gen_vbmeta_image_defaults", avbGenVbmetaImageDefaultsFactory)
Jiyong Park6f0f6882020-11-12 13:14:30 +090048}
49
Spandan Das71be42d2024-11-20 18:34:16 +000050func registerMutators(ctx android.RegistrationContext) {
51 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
52 ctx.BottomUp("add_autogenerated_rro_deps", addAutogeneratedRroDeps)
53 })
54}
55
Cole Fauste1676122024-12-03 17:32:25 -080056// Remember to add referenced files to implicits!
57var textFileProcessorRule = pctx.AndroidStaticRule("text_file_processing", blueprint.RuleParams{
58 Command: "build/soong/scripts/text_file_processor.py $in $out",
59 CommandDeps: []string{"build/soong/scripts/text_file_processor.py"},
60})
61
Jiyong Park6f0f6882020-11-12 13:14:30 +090062type filesystem struct {
63 android.ModuleBase
64 android.PackagingBase
Jiyong Parkf46b1af2024-04-05 18:13:33 +090065 android.DefaultableModuleBase
Jiyong Park65c49f52020-11-24 14:23:26 +090066
Jihoon Kang98047cf2024-10-02 17:13:54 +000067 properties FilesystemProperties
Jiyong Park71baa762021-01-18 21:11:03 +090068
Cole Faust4e9f5922024-11-13 16:09:23 -080069 output android.Path
Jiyong Park65c49f52020-11-24 14:23:26 +090070 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090071
Cole Faust4e9f5922024-11-13 16:09:23 -080072 fileListFile android.Path
Kiyoung Kim99a954d2024-06-21 14:22:20 +090073
74 // Keeps the entries installed from this filesystem
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090075 entries []string
Kiyoung Kim67118212024-11-07 13:23:44 +090076
77 filesystemBuilder filesystemBuilder
Jiyong Park6f0f6882020-11-12 13:14:30 +090078}
79
Kiyoung Kim67118212024-11-07 13:23:44 +090080type filesystemBuilder interface {
81 BuildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath)
82 // Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs()
83 FilterPackagingSpec(spec android.PackagingSpec) bool
Inseob Kim3c0a0422024-11-05 17:21:37 +090084 // Function that modifies PackagingSpec in PackagingBase.GatherPackagingSpecs() to customize.
85 // For example, GSI system.img contains system_ext and product artifacts and their
86 // relPathInPackage need to be rebased to system/system_ext and system/system_product.
87 ModifyPackagingSpec(spec *android.PackagingSpec)
Kiyoung Kim23be5bb2024-11-27 00:50:30 +000088
89 // Function to check if the filesystem should not use `vintf_fragments` property,
90 // but use `vintf_fragment` module type instead
91 ShouldUseVintfFragmentModuleOnly() bool
Kiyoung Kim67118212024-11-07 13:23:44 +090092}
93
94var _ filesystemBuilder = (*filesystem)(nil)
95
Spandan Das69464c32024-10-25 20:08:06 +000096type SymlinkDefinition struct {
Inseob Kim14199b02021-02-09 21:18:31 +090097 Target *string
98 Name *string
99}
100
Jihoon Kang0a453892024-12-09 22:16:26 +0000101// CopyWithNamePrefix returns a new [SymlinkDefinition] with prefix added to Name.
102func (s *SymlinkDefinition) CopyWithNamePrefix(prefix string) SymlinkDefinition {
103 return SymlinkDefinition{
104 Target: s.Target,
105 Name: proptools.StringPtr(filepath.Join(prefix, proptools.String(s.Name))),
106 }
107}
108
Jihoon Kang98047cf2024-10-02 17:13:54 +0000109type FilesystemProperties struct {
Jiyong Park71baa762021-01-18 21:11:03 +0900110 // When set to true, sign the image with avbtool. Default is false.
111 Use_avb *bool
112
113 // Path to the private key that avbtool will use to sign this filesystem image.
114 // TODO(jiyong): allow apex_key to be specified here
115 Avb_private_key *string `android:"path"`
116
Shikha Panwar01403bb2022-12-22 12:22:57 +0000117 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +0900118 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +0900119
Shikha Panwar01403bb2022-12-22 12:22:57 +0000120 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
121 // avbtool. Default used by avbtool is sha1.
Shikha Panware6f30632022-12-21 12:54:45 +0000122 Avb_hash_algorithm *string
123
Spandan Dase5c393c2024-12-12 19:25:07 +0000124 // The security patch passed to as the com.android.build.<type>.security_patch avb property.
125 Security_patch *string
126
Cole Fauste1676122024-12-03 17:32:25 -0800127 // Whether or not to use forward-error-correction codes when signing with AVB. Defaults to true.
128 Use_fec *bool
129
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000130 // The index used to prevent rollback of the image. Only used if use_avb is true.
131 Rollback_index *int64
132
Jiyong Parkac4076d2021-03-15 23:21:30 +0900133 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
134 Partition_name *string
135
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000136 // Type of the filesystem. Currently, ext4, erofs, cpio, and compressed_cpio are supported. Default
Jiyong Park837cdb22021-02-05 00:17:14 +0900137 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +0900138 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +0900139
Cole Faust9a24d902024-03-18 15:38:12 -0700140 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
141 // checks, and will be used in the future for API surface checks.
142 Partition_type *string
143
Cole Faust0d467052024-12-04 17:19:19 -0800144 // file_contexts file to make image. Currently, only ext4 is supported. These file contexts
145 // will be compiled with sefcontext_compile
Inseob Kimcc8e5362021-02-03 14:05:24 +0900146 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900147
Cole Faust0d467052024-12-04 17:19:19 -0800148 // The selinux file contexts, after having already run them through sefcontext_compile
149 Precompiled_file_contexts *string `android:"path"`
150
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900151 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
152 // (root).
153 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +0900154
155 // Directories to be created under root. e.g. /dev, /proc, etc.
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700156 Dirs proptools.Configurable[[]string]
Inseob Kim14199b02021-02-09 21:18:31 +0900157
Jihoon Kang6da80752024-12-23 18:53:32 +0000158 // List of filesystem modules to include in creating the partition. The root directory of
159 // the provided filesystem modules are included in creating the partition.
160 // This is only supported for cpio and compressed cpio filesystem types.
161 Include_files_of []string
162
Inseob Kim14199b02021-02-09 21:18:31 +0900163 // Symbolic links to be created under root with "ln -sf <target> <name>".
Spandan Das69464c32024-10-25 20:08:06 +0000164 Symlinks []SymlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900165
166 // Seconds since unix epoch to override timestamps of file entries
167 Fake_timestamp *string
168
169 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
170 // Otherwise, they'll be set as random which might cause indeterministic build output.
171 Uuid *string
Inseob Kim376d72f2023-11-01 15:40:25 +0900172
173 // Mount point for this image. Default is "/"
174 Mount_point *string
Cole Faust4a2a7c92024-03-12 12:44:40 -0700175
176 // If set to the name of a partition ("system", "vendor", etc), this filesystem module
177 // will also include the contents of the make-built staging directories. If any soong
178 // modules would be installed to the same location as a make module, they will overwrite
179 // the make version.
180 Include_make_built_files string
Inseob Kim53391842024-03-29 17:44:07 +0900181
Inseob Kimb7b84572024-04-30 10:51:47 +0900182 // When set, builds etc/event-log-tags file by merging logtags from all dependencies.
183 // Default is false
184 Build_logtags *bool
185
Justin Yun74f3f302024-05-07 14:32:14 +0900186 // Install aconfig_flags.pb file for the modules installed in this partition.
187 Gen_aconfig_flags_pb *bool
188
Cole Faust34592c02024-12-13 11:20:24 -0800189 // List of names of other filesystem partitions to import their aconfig flags from.
190 // This is used for the system partition to import system_ext's aconfig flags, as currently
191 // those are considered one "container": aosp/3261300
192 Import_aconfig_flags_from []string
193
Inseob Kim53391842024-03-29 17:44:07 +0900194 Fsverity fsverityProperties
Cole Faust92ccbe22024-10-03 14:38:37 -0700195
196 // If this property is set to true, the filesystem will call ctx.UncheckedModule(), causing
197 // it to not be built on checkbuilds. Used for the automatic migration from make to soong
198 // build modules, where we want to emit some not-yet-working filesystems and we don't want them
199 // to be built.
200 Unchecked_module *bool `blueprint:"mutated"`
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000201
202 Erofs ErofsProperties
Jihoon Kang0d545b82024-10-11 00:21:57 +0000203
mrziwang1a6291f2024-11-07 14:29:25 -0800204 F2fs F2fsProperties
205
Spandan Das2047a4c2024-11-11 21:24:58 +0000206 Linker_config LinkerConfigProperties
Spandan Das92631882024-10-28 22:49:38 +0000207
Jihoon Kang0d545b82024-10-11 00:21:57 +0000208 // Determines if the module is auto-generated from Soong or not. If the module is
209 // auto-generated, its deps are exempted from visibility enforcement.
210 Is_auto_generated *bool
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000211
212 // Path to the dev nodes description file. This is only needed for building the ramdisk
213 // partition and should not be explicitly specified.
214 Dev_nodes_description_file *string `android:"path" blueprint:"mutated"`
Spandan Das71be42d2024-11-20 18:34:16 +0000215
216 // Additional dependencies used for building android products
217 Android_filesystem_deps AndroidFilesystemDeps
Spandan Dasc49b85e2025-01-10 00:51:25 +0000218
219 // Name of the output. Default is $(module_name).img
220 Stem *string
Jihoon Kang983dd882025-01-13 23:14:11 +0000221
222 // The size of the partition on the device. It will be a build error if this built partition
223 // image exceeds this size.
224 Partition_size *int64
Jihoon Kang6d08d922025-01-14 18:31:57 +0000225
226 // Whether to format f2fs and ext4 in a way that supports casefolding
227 Support_casefolding *bool
228
229 // Whether to format f2fs and ext4 in a way that supports project quotas
230 Support_project_quota *bool
231
232 // Whether to enable per-file compression in f2fs
233 Enable_compression *bool
Spandan Das71be42d2024-11-20 18:34:16 +0000234}
235
236type AndroidFilesystemDeps struct {
237 System *string
238 System_ext *string
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000239}
240
241// Additional properties required to generate erofs FS partitions.
242type ErofsProperties struct {
243 // Compressor and Compression level passed to mkfs.erofs. e.g. (lz4hc,9)
244 // Please see external/erofs-utils/README for complete documentation.
245 Compressor *string
246
247 // Used as --compress-hints for mkfs.erofs
248 Compress_hints *string `android:"path"`
249
250 Sparse *bool
Jiyong Park71baa762021-01-18 21:11:03 +0900251}
252
mrziwang1a6291f2024-11-07 14:29:25 -0800253// Additional properties required to generate f2fs FS partitions.
254type F2fsProperties struct {
255 Sparse *bool
256}
257
Spandan Das173256b2024-10-31 19:59:30 +0000258type LinkerConfigProperties struct {
259
260 // Build a linker.config.pb file
261 Gen_linker_config *bool
262
263 // List of files (in .json format) that will be converted to a linker config file (in .pb format).
264 // The linker config file be installed in the filesystem at /etc/linker.config.pb
265 Linker_config_srcs []string `android:"path"`
266}
267
Jiyong Park65c49f52020-11-24 14:23:26 +0900268// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
269// image. The filesystem images are expected to be mounted in the target device, which means the
270// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
271// The modules are placed in the filesystem image just like they are installed to the ordinary
272// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Cole Faust92ccbe22024-10-03 14:38:37 -0700273func FilesystemFactory() android.Module {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900274 module := &filesystem{}
Kiyoung Kim67118212024-11-07 13:23:44 +0900275 module.filesystemBuilder = module
Cole Faust2cfe6962024-09-17 11:31:14 -0700276 initFilesystemModule(module, module)
Jiyong Parkfa616132021-04-20 11:36:40 +0900277 return module
278}
279
Cole Faust2cfe6962024-09-17 11:31:14 -0700280func initFilesystemModule(module android.DefaultableModule, filesystemModule *filesystem) {
281 module.AddProperties(&filesystemModule.properties)
282 android.InitPackageModule(filesystemModule)
283 filesystemModule.PackagingBase.DepsCollectFirstTargetOnly = true
Jihoon Kang79196c52024-10-30 18:49:47 +0000284 filesystemModule.PackagingBase.AllowHighPriorityDeps = true
Jiyong Park6f0f6882020-11-12 13:14:30 +0900285 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900286 android.InitDefaultableModule(module)
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000287
288 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
289 filesystemModule.setDevNodesDescriptionProp()
290 })
Jiyong Park6f0f6882020-11-12 13:14:30 +0900291}
292
Jihoon Kang0d545b82024-10-11 00:21:57 +0000293type depTag struct {
Jiyong Park12a719c2021-01-07 15:31:24 +0900294 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900295 android.PackagingItemAlwaysDepTag
Jihoon Kang0d545b82024-10-11 00:21:57 +0000296}
297
298var dependencyTag = depTag{}
299
300type depTagWithVisibilityEnforcementBypass struct {
301 depTag
302}
303
Spandan Das71be42d2024-11-20 18:34:16 +0000304type interPartitionDepTag struct {
305 blueprint.BaseDependencyTag
306}
307
308var interPartitionDependencyTag = interPartitionDepTag{}
309
Jihoon Kang6da80752024-12-23 18:53:32 +0000310var interPartitionInstallDependencyTag = interPartitionDepTag{}
311
Jihoon Kang0d545b82024-10-11 00:21:57 +0000312var _ android.ExcludeFromVisibilityEnforcementTag = (*depTagWithVisibilityEnforcementBypass)(nil)
313
314func (t depTagWithVisibilityEnforcementBypass) ExcludeFromVisibilityEnforcement() {}
315
316var dependencyTagWithVisibilityEnforcementBypass = depTagWithVisibilityEnforcementBypass{}
Jiyong Park65b62242020-11-25 12:44:59 +0900317
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000318// ramdiskDevNodesDescription is the name of the filegroup module that provides the file that
319// contains the description of dev nodes added to the CPIO archive for the ramdisk partition.
320const ramdiskDevNodesDescription = "ramdisk_node_list"
321
322func (f *filesystem) setDevNodesDescriptionProp() {
323 if proptools.String(f.properties.Partition_name) == "ramdisk" {
324 f.properties.Dev_nodes_description_file = proptools.StringPtr(":" + ramdiskDevNodesDescription)
325 }
326}
327
Jiyong Park6f0f6882020-11-12 13:14:30 +0900328func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000329 if proptools.Bool(f.properties.Is_auto_generated) {
330 f.AddDeps(ctx, dependencyTagWithVisibilityEnforcementBypass)
331 } else {
332 f.AddDeps(ctx, dependencyTag)
333 }
Spandan Das71be42d2024-11-20 18:34:16 +0000334 if f.properties.Android_filesystem_deps.System != nil {
335 ctx.AddDependency(ctx.Module(), interPartitionDependencyTag, proptools.String(f.properties.Android_filesystem_deps.System))
336 }
337 if f.properties.Android_filesystem_deps.System_ext != nil {
338 ctx.AddDependency(ctx.Module(), interPartitionDependencyTag, proptools.String(f.properties.Android_filesystem_deps.System_ext))
339 }
Cole Faust34592c02024-12-13 11:20:24 -0800340 for _, partition := range f.properties.Import_aconfig_flags_from {
341 ctx.AddDependency(ctx.Module(), importAconfigDependencyTag, partition)
342 }
Jihoon Kang6da80752024-12-23 18:53:32 +0000343 for _, partition := range f.properties.Include_files_of {
344 ctx.AddDependency(ctx.Module(), interPartitionInstallDependencyTag, partition)
345 }
Jiyong Park6f0f6882020-11-12 13:14:30 +0900346}
347
Jiyong Park11a65972021-02-01 21:09:38 +0900348type fsType int
349
350const (
351 ext4Type fsType = iota
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000352 erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800353 f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900354 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900355 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900356 unknown
357)
358
Spandan Das7a46f6c2024-10-14 18:41:18 +0000359func (fs fsType) IsUnknown() bool {
360 return fs == unknown
361}
362
Cole Faust92ccbe22024-10-03 14:38:37 -0700363type FilesystemInfo struct {
Cole Faust44080412024-12-20 14:17:07 -0800364 // The built filesystem image
365 Output android.Path
Spandan Das1f0a5a12025-01-15 00:53:15 +0000366 // An additional hermetic filesystem image.
367 // e.g. this will contain inodes with pinned timestamps.
368 // This will be copied to target_files.zip
369 OutputHermetic android.Path
Cole Faust92ccbe22024-10-03 14:38:37 -0700370 // A text file containing the list of paths installed on the partition.
371 FileListFile android.Path
Cole Faust44080412024-12-20 14:17:07 -0800372 // The root staging directory used to build the output filesystem. If consuming this, make sure
373 // to add a dependency on the Output file, as you cannot add dependencies on directories
374 // in ninja.
375 RootDir android.Path
Cole Faust11fda332025-01-14 16:47:19 -0800376 // The rebased staging directory used to build the output filesystem. If consuming this, make
377 // sure to add a dependency on the Output file, as you cannot add dependencies on directories
378 // in ninja. In many cases this is the same as RootDir, only in the system partition is it
379 // different. There, it points to the "system" sub-directory of RootDir.
380 RebasedDir android.Path
Spandan Das33c9c472025-01-14 19:26:23 +0000381 // A text file with block data of the .img file
382 // This is an implicit output of `build_image`
383 MapFile android.Path
Cole Faust11fda332025-01-14 16:47:19 -0800384 // Name of the module that produced this FilesystemInfo origionally. (though it may be
385 // re-exported by super images or boot images)
386 ModuleName string
Cole Faust92ccbe22024-10-03 14:38:37 -0700387}
388
389var FilesystemProvider = blueprint.NewProvider[FilesystemInfo]()
390
Yu Liufc8d5c12025-01-09 00:19:06 +0000391type FilesystemDefaultsInfo struct {
392 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
393 // checks, and will be used in the future for API surface checks.
394 PartitionType string
395}
396
397var FilesystemDefaultsInfoProvider = blueprint.NewProvider[FilesystemDefaultsInfo]()
398
Spandan Das7a46f6c2024-10-14 18:41:18 +0000399func GetFsTypeFromString(ctx android.EarlyModuleContext, typeStr string) fsType {
Jiyong Park11a65972021-02-01 21:09:38 +0900400 switch typeStr {
401 case "ext4":
402 return ext4Type
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000403 case "erofs":
404 return erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800405 case "f2fs":
406 return f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900407 case "compressed_cpio":
408 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900409 case "cpio":
410 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900411 default:
Jiyong Park11a65972021-02-01 21:09:38 +0900412 return unknown
413 }
414}
415
Spandan Das7a46f6c2024-10-14 18:41:18 +0000416func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
417 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
418 fsType := GetFsTypeFromString(ctx, typeStr)
419 if fsType == unknown {
420 ctx.PropertyErrorf("type", "%q not supported", typeStr)
421 }
422 return fsType
423}
424
Jiyong Park65c49f52020-11-24 14:23:26 +0900425func (f *filesystem) installFileName() string {
Spandan Dasc49b85e2025-01-10 00:51:25 +0000426 return proptools.StringDefault(f.properties.Stem, f.BaseModuleName()+".img")
Jiyong Park65c49f52020-11-24 14:23:26 +0900427}
428
Inseob Kim53391842024-03-29 17:44:07 +0900429func (f *filesystem) partitionName() string {
430 return proptools.StringDefault(f.properties.Partition_name, f.Name())
431}
432
Kiyoung Kim67118212024-11-07 13:23:44 +0900433func (f *filesystem) FilterPackagingSpec(ps android.PackagingSpec) bool {
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000434 // Filesystem module respects the installation semantic. A PackagingSpec from a module with
435 // IsSkipInstall() is skipped.
Cole Faust76a6e952024-11-07 16:56:45 -0800436 if ps.SkipInstall() {
437 return false
Spandan Das6d056502024-10-21 15:40:32 +0000438 }
Cole Faust76a6e952024-11-07 16:56:45 -0800439 if proptools.Bool(f.properties.Is_auto_generated) { // TODO (spandandas): Remove this.
440 pt := f.PartitionType()
Cole Faustc88cff12024-11-12 13:24:05 -0800441 return ps.Partition() == pt || strings.HasPrefix(ps.Partition(), pt+"/")
Cole Faust76a6e952024-11-07 16:56:45 -0800442 }
443 return true
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000444}
445
Inseob Kim3c0a0422024-11-05 17:21:37 +0900446func (f *filesystem) ModifyPackagingSpec(ps *android.PackagingSpec) {
Cole Faustc88cff12024-11-12 13:24:05 -0800447 // Sometimes, android.modulePartition() returns a path with >1 path components.
448 // This makes the partition field of packagingSpecs have multiple components, like
449 // "system/product". Right now, the filesystem module doesn't look at the partition field
450 // when deciding what path to install the file under, only the RelPathInPackage field, so
451 // we move the later path components from partition to relPathInPackage. This should probably
452 // be revisited in the future.
453 prefix := f.PartitionType() + "/"
454 if strings.HasPrefix(ps.Partition(), prefix) {
455 subPartition := strings.TrimPrefix(ps.Partition(), prefix)
456 ps.SetPartition(f.PartitionType())
457 ps.SetRelPathInPackage(filepath.Join(subPartition, ps.RelPathInPackage()))
458 }
Inseob Kim3c0a0422024-11-05 17:21:37 +0900459}
460
Jiyong Park6f0f6882020-11-12 13:14:30 +0900461var pctx = android.NewPackageContext("android/soong/filesystem")
462
463func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900464 validatePartitionType(ctx, f)
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000465 if f.filesystemBuilder.ShouldUseVintfFragmentModuleOnly() {
466 f.validateVintfFragments(ctx)
467 }
Jihoon Kang6da80752024-12-23 18:53:32 +0000468
469 if len(f.properties.Include_files_of) > 0 && !android.InList(f.fsType(ctx), []fsType{compressedCpioType, cpioType}) {
470 ctx.PropertyErrorf("include_files_of", "include_files_of is only supported for cpio and compressed cpio filesystem types.")
471 }
472
473 var rootDir android.OutputPath
Cole Faust11fda332025-01-14 16:47:19 -0800474 var rebasedDir android.OutputPath
Spandan Das33c9c472025-01-14 19:26:23 +0000475 var mapFile android.Path
Spandan Das1f0a5a12025-01-15 00:53:15 +0000476 var outputHermetic android.Path
Jiyong Park11a65972021-02-01 21:09:38 +0900477 switch f.fsType(ctx) {
mrziwang1a6291f2024-11-07 14:29:25 -0800478 case ext4Type, erofsType, f2fsType:
Cole Faust11fda332025-01-14 16:47:19 -0800479 f.output, outputHermetic, rootDir, rebasedDir = f.buildImageUsingBuildImage(ctx)
Spandan Das33c9c472025-01-14 19:26:23 +0000480 mapFile = f.getMapFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900481 case compressedCpioType:
Cole Faust11fda332025-01-14 16:47:19 -0800482 f.output, rootDir, rebasedDir = f.buildCpioImage(ctx, true)
Jiyong Park837cdb22021-02-05 00:17:14 +0900483 case cpioType:
Cole Faust11fda332025-01-14 16:47:19 -0800484 f.output, rootDir, rebasedDir = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900485 default:
486 return
487 }
488
489 f.installDir = android.PathForModuleInstall(ctx, "etc")
490 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
mrziwang555d1332024-06-07 11:15:33 -0700491 ctx.SetOutputFiles([]android.Path{f.output}, "")
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900492
Jihoon Kang6da80752024-12-23 18:53:32 +0000493 if f.partitionName() == "recovery" {
494 rootDir = rootDir.Join(ctx, "root")
495 }
496
Cole Faust4e9f5922024-11-13 16:09:23 -0800497 fileListFile := android.PathForModuleOut(ctx, "fileList")
498 android.WriteFileRule(ctx, fileListFile, f.installedFilesList())
Cole Faust92ccbe22024-10-03 14:38:37 -0700499
Spandan Das33c9c472025-01-14 19:26:23 +0000500 fsInfo := FilesystemInfo{
Cole Faust44080412024-12-20 14:17:07 -0800501 Output: f.output,
Naresh Kumar Podishetty (xWF)7d89e662024-12-19 23:40:02 -0800502 FileListFile: fileListFile,
Cole Faust44080412024-12-20 14:17:07 -0800503 RootDir: rootDir,
Cole Faust11fda332025-01-14 16:47:19 -0800504 RebasedDir: rebasedDir,
505 ModuleName: ctx.ModuleName(),
Spandan Das33c9c472025-01-14 19:26:23 +0000506 }
507 if mapFile != nil {
508 fsInfo.MapFile = mapFile
509 }
Spandan Das1f0a5a12025-01-15 00:53:15 +0000510 if outputHermetic != nil {
511 fsInfo.OutputHermetic = outputHermetic
512 }
Spandan Das33c9c472025-01-14 19:26:23 +0000513
514 android.SetProvider(ctx, FilesystemProvider, fsInfo)
Spandan Das3ec6d062025-01-09 19:37:47 +0000515
Cole Faust4e9f5922024-11-13 16:09:23 -0800516 f.fileListFile = fileListFile
Cole Faust92ccbe22024-10-03 14:38:37 -0700517
518 if proptools.Bool(f.properties.Unchecked_module) {
519 ctx.UncheckedModule()
520 }
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900521}
522
Spandan Das33c9c472025-01-14 19:26:23 +0000523func (f *filesystem) getMapFile(ctx android.ModuleContext) android.WritablePath {
524 // create the filepath by replacing the extension of the corresponding img file
525 return android.PathForModuleOut(ctx, f.installFileName()).ReplaceExtension(ctx, "map")
526}
527
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000528func (f *filesystem) validateVintfFragments(ctx android.ModuleContext) {
529 visitedModule := map[string]bool{}
530 packagingSpecs := f.gatherFilteredPackagingSpecs(ctx)
531
532 moduleInFileSystem := func(mod android.Module) bool {
533 for _, ps := range android.OtherModuleProviderOrDefault(
534 ctx, mod, android.InstallFilesProvider).PackagingSpecs {
535 if _, ok := packagingSpecs[ps.RelPathInPackage()]; ok {
536 return true
537 }
538 }
539 return false
540 }
541
542 ctx.WalkDeps(func(child, parent android.Module) bool {
543 if visitedModule[child.Name()] {
544 return false
545 }
546 if !moduleInFileSystem(child) {
547 visitedModule[child.Name()] = true
548 return true
549 }
550 if vintfFragments := child.VintfFragments(ctx); vintfFragments != nil {
551 ctx.PropertyErrorf(
552 "vintf_fragments",
553 "Module %s is referenced by soong-defined filesystem %s with property vintf_fragments(%s) in use."+
554 " Use vintf_fragment_modules property instead.",
555 child.Name(),
556 f.BaseModuleName(),
557 strings.Join(vintfFragments, ", "),
558 )
559 }
560 visitedModule[child.Name()] = true
561 return true
562 })
563}
564
Cole Faust4e9f5922024-11-13 16:09:23 -0800565func (f *filesystem) appendToEntry(ctx android.ModuleContext, installedFile android.Path) {
Spandan Das420e16a2024-12-11 18:10:52 +0000566 partitionBaseDir := android.PathForModuleOut(ctx, f.rootDirString(), proptools.String(f.properties.Base_dir)).String() + "/"
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900567
568 relPath, inTargetPartition := strings.CutPrefix(installedFile.String(), partitionBaseDir)
569 if inTargetPartition {
570 f.entries = append(f.entries, relPath)
571 }
572}
573
574func (f *filesystem) installedFilesList() string {
575 installedFilePaths := android.FirstUniqueStrings(f.entries)
576 slices.Sort(installedFilePaths)
577
578 return strings.Join(installedFilePaths, "\n")
Jiyong Park11a65972021-02-01 21:09:38 +0900579}
580
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900581func validatePartitionType(ctx android.ModuleContext, p partition) {
582 if !android.InList(p.PartitionType(), validPartitions) {
583 ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, p.PartitionType())
584 }
585
Yu Liufc8d5c12025-01-09 00:19:06 +0000586 ctx.VisitDirectDepsProxyWithTag(android.DefaultsDepTag, func(m android.ModuleProxy) {
587 if fdm, ok := android.OtherModuleProvider(ctx, m, FilesystemDefaultsInfoProvider); ok {
588 if p.PartitionType() != fdm.PartitionType {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900589 ctx.PropertyErrorf("partition_type",
590 "%s doesn't match with the partition type %s of the filesystem default module %s",
Yu Liufc8d5c12025-01-09 00:19:06 +0000591 p.PartitionType(), fdm.PartitionType, m.Name())
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900592 }
593 }
594 })
595}
596
Cole Faust3b806d32024-03-11 15:15:03 -0700597// Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files
598// already in `rootDir`.
599func (f *filesystem) buildNonDepsFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.OutputPath) {
Inseob Kim14199b02021-02-09 21:18:31 +0900600 // create dirs and symlinks
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700601 for _, dir := range f.properties.Dirs.GetOrDefault(ctx, nil) {
Inseob Kim14199b02021-02-09 21:18:31 +0900602 // OutputPath.Join verifies dir
603 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
604 }
605
606 for _, symlink := range f.properties.Symlinks {
607 name := strings.TrimSpace(proptools.String(symlink.Name))
608 target := strings.TrimSpace(proptools.String(symlink.Target))
609
610 if name == "" {
611 ctx.PropertyErrorf("symlinks", "Name can't be empty")
612 continue
613 }
614
615 if target == "" {
616 ctx.PropertyErrorf("symlinks", "Target can't be empty")
617 continue
618 }
619
620 // OutputPath.Join verifies name. don't need to verify target.
621 dst := rootDir.Join(ctx, name)
Cole Faust3b806d32024-03-11 15:15:03 -0700622 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 +0900623 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
624 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900625 f.appendToEntry(ctx, dst)
Inseob Kim14199b02021-02-09 21:18:31 +0900626 }
Jihoon Kang89e8a692024-12-18 19:28:33 +0000627
628 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=2835;drc=b186569ef00ff2f2a1fab28aedc75ebc32bcd67b
629 if f.partitionName() == "recovery" {
630 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, "root/linkerconfig").String())
631 builder.Command().Text("touch").Text(rootDir.Join(ctx, "root/linkerconfig/ld.config.txt").String())
632 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900633}
634
Inseob Kim33f95a92024-07-11 15:44:49 +0900635func (f *filesystem) copyPackagingSpecs(ctx android.ModuleContext, builder *android.RuleBuilder, specs map[string]android.PackagingSpec, rootDir, rebasedDir android.WritablePath) []string {
636 rootDirSpecs := make(map[string]android.PackagingSpec)
637 rebasedDirSpecs := make(map[string]android.PackagingSpec)
638
639 for rel, spec := range specs {
640 if spec.Partition() == "root" {
641 rootDirSpecs[rel] = spec
642 } else {
643 rebasedDirSpecs[rel] = spec
644 }
645 }
646
647 dirsToSpecs := make(map[android.WritablePath]map[string]android.PackagingSpec)
648 dirsToSpecs[rootDir] = rootDirSpecs
649 dirsToSpecs[rebasedDir] = rebasedDirSpecs
650
651 return f.CopySpecsToDirs(ctx, builder, dirsToSpecs)
652}
653
Spandan Das420e16a2024-12-11 18:10:52 +0000654func (f *filesystem) rootDirString() string {
655 return f.partitionName()
656}
657
Cole Faust11fda332025-01-14 16:47:19 -0800658func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) (android.Path, android.Path, android.OutputPath, android.OutputPath) {
Spandan Das420e16a2024-12-11 18:10:52 +0000659 rootDir := android.PathForModuleOut(ctx, f.rootDirString()).OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700660 rebasedDir := rootDir
661 if f.properties.Base_dir != nil {
662 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
663 }
664 builder := android.NewRuleBuilder(pctx, ctx)
665 // Wipe the root dir to get rid of leftover files from prior builds
666 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900667 specs := f.gatherFilteredPackagingSpecs(ctx)
Inseob Kim33f95a92024-07-11 15:44:49 +0900668 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700669
670 f.buildNonDepsFiles(ctx, builder, rootDir)
Cole Faust4a2a7c92024-03-12 12:44:40 -0700671 f.addMakeBuiltFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900672 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Inseob Kimb7b84572024-04-30 10:51:47 +0900673 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
Justin Yun74f3f302024-05-07 14:32:14 +0900674 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
Kiyoung Kim67118212024-11-07 13:23:44 +0900675 f.filesystemBuilder.BuildLinkerConfigFile(ctx, builder, rebasedDir)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900676
Nikita Ioffe519015f2022-12-23 15:36:29 +0000677 // run host_init_verifier
678 // Ideally we should have a concept of pluggable linters that verify the generated image.
679 // While such concept is not implement this will do.
680 // TODO(b/263574231): substitute with pluggable linter.
681 builder.Command().
682 BuiltTool("host_init_verifier").
683 FlagWithArg("--out_system=", rootDir.String()+"/system")
684
Jiyong Park72678312021-01-18 17:29:49 +0900685 propFile, toolDeps := f.buildPropFile(ctx)
Cole Fauste1676122024-12-03 17:32:25 -0800686
687 // Most of the time, if build_image were to call a host tool, it accepts the path to the
688 // host tool in a field in the prop file. However, it doesn't have that option for fec, which
689 // it expects to just be on the PATH. Add fec to the PATH.
690 fec := ctx.Config().HostToolPath(ctx, "fec")
691 pathToolDirs := []string{filepath.Dir(fec.String())}
692
Cole Faust4e9f5922024-11-13 16:09:23 -0800693 output := android.PathForModuleOut(ctx, f.installFileName())
Spandan Das33c9c472025-01-14 19:26:23 +0000694 builder.Command().Text("touch").Output(f.getMapFile(ctx))
Cole Fauste1676122024-12-03 17:32:25 -0800695 builder.Command().
696 Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")).
697 BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900698 Text(rootDir.String()). // input directory
699 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900700 Implicits(toolDeps).
Cole Fauste1676122024-12-03 17:32:25 -0800701 Implicit(fec).
Jiyong Park11a65972021-02-01 21:09:38 +0900702 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900703 Text(rootDir.String()) // directory where to find fs_config_files|dirs
704
Spandan Das1f0a5a12025-01-15 00:53:15 +0000705 // Add an additional cmd to create a hermetic img file. This will contain pinned timestamps e.g.
706 propFilePinnedTimestamp := android.PathForModuleOut(ctx, "for_target_files", "prop")
707 builder.Command().Textf("cat").Input(propFile).Flag(">").Output(propFilePinnedTimestamp).Textf(" && echo use_fixed_timestamp=true >> %s", propFilePinnedTimestamp)
708
709 outputHermetic := android.PathForModuleOut(ctx, "for_target_files", f.installFileName())
710 builder.Command().
711 Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")).
712 BuiltTool("build_image").
713 Text(rootDir.String()). // input directory
714 Flag(propFilePinnedTimestamp.String()).
715 Implicits(toolDeps).
716 Implicit(fec).
717 Output(outputHermetic).
718 Text(rootDir.String()) // directory where to find fs_config_files|dirs
719
Jihoon Kang983dd882025-01-13 23:14:11 +0000720 if f.properties.Partition_size != nil {
721 assertMaxImageSize(builder, output, *f.properties.Partition_size, false)
722 }
723
Jiyong Park6f0f6882020-11-12 13:14:30 +0900724 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800725 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900726
Cole Faust11fda332025-01-14 16:47:19 -0800727 return output, outputHermetic, rootDir, rebasedDir
Jiyong Park65c49f52020-11-24 14:23:26 +0900728}
729
Cole Faust4e9f5922024-11-13 16:09:23 -0800730func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.Path {
Inseob Kimcc8e5362021-02-03 14:05:24 +0900731 builder := android.NewRuleBuilder(pctx, ctx)
732 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
733 builder.Command().BuiltTool("sefcontext_compile").
734 FlagWithOutput("-o ", fcBin).
735 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
736 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
Cole Faust4e9f5922024-11-13 16:09:23 -0800737 return fcBin
Inseob Kimcc8e5362021-02-03 14:05:24 +0900738}
739
Cole Faust4e9f5922024-11-13 16:09:23 -0800740func (f *filesystem) buildPropFile(ctx android.ModuleContext) (android.Path, android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +0900741 var deps android.Paths
Cole Faustcec230a2024-03-07 15:51:12 -0800742 var propFileString strings.Builder
Jiyong Park72678312021-01-18 17:29:49 +0900743 addStr := func(name string, value string) {
Cole Faustcec230a2024-03-07 15:51:12 -0800744 propFileString.WriteString(name)
745 propFileString.WriteRune('=')
746 propFileString.WriteString(value)
747 propFileString.WriteRune('\n')
Jiyong Park72678312021-01-18 17:29:49 +0900748 }
749 addPath := func(name string, path android.Path) {
Cole Faustcec230a2024-03-07 15:51:12 -0800750 addStr(name, path.String())
Jiyong Park72678312021-01-18 17:29:49 +0900751 deps = append(deps, path)
752 }
753
Jiyong Park11a65972021-02-01 21:09:38 +0900754 // Type string that build_image.py accepts.
755 fsTypeStr := func(t fsType) string {
756 switch t {
Spandan Das94668822024-10-09 20:51:33 +0000757 // TODO(372522486): add more types like f2fs, erofs, etc.
Jiyong Park11a65972021-02-01 21:09:38 +0900758 case ext4Type:
759 return "ext4"
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000760 case erofsType:
761 return "erofs"
mrziwang1a6291f2024-11-07 14:29:25 -0800762 case f2fsType:
763 return "f2fs"
Jiyong Park11a65972021-02-01 21:09:38 +0900764 }
765 panic(fmt.Errorf("unsupported fs type %v", t))
766 }
Spandan Das33c9c472025-01-14 19:26:23 +0000767 addStr("block_list", f.getMapFile(ctx).String()) // This will be an implicit output
Jiyong Park11a65972021-02-01 21:09:38 +0900768
769 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim376d72f2023-11-01 15:40:25 +0900770 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +0900771 addStr("use_dynamic_partition_size", "true")
772 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
773 // b/177813163 deps of the host tools have to be added. Remove this.
774 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
775 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
776 }
777
Jiyong Park71baa762021-01-18 21:11:03 +0900778 if proptools.Bool(f.properties.Use_avb) {
779 addStr("avb_hashtree_enable", "true")
780 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
781 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
782 addStr("avb_algorithm", algorithm)
Cole Fauste1676122024-12-03 17:32:25 -0800783 if f.properties.Avb_private_key != nil {
784 key := android.PathForModuleSrc(ctx, *f.properties.Avb_private_key)
785 addPath("avb_key_path", key)
786 }
Inseob Kim53391842024-03-29 17:44:07 +0900787 addStr("partition_name", f.partitionName())
Cole Fauste1676122024-12-03 17:32:25 -0800788 avb_add_hashtree_footer_args := ""
789 if !proptools.BoolDefault(f.properties.Use_fec, true) {
790 avb_add_hashtree_footer_args += " --do_not_generate_fec"
791 }
Shikha Panware6f30632022-12-21 12:54:45 +0000792 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
793 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
794 }
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000795 if f.properties.Rollback_index != nil {
796 rollbackIndex := proptools.Int(f.properties.Rollback_index)
797 if rollbackIndex < 0 {
798 ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
799 }
800 avb_add_hashtree_footer_args += " --rollback_index " + strconv.Itoa(rollbackIndex)
801 }
Cole Fauste1676122024-12-03 17:32:25 -0800802 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 -0800803 // We're not going to add BuildFingerPrintFile as a dep. If it changed, it's likely because
804 // the build number changed, and we don't want to trigger rebuilds solely based on the build
805 // number.
Cole Fauste1676122024-12-03 17:32:25 -0800806 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 +0000807 if f.properties.Security_patch != nil && proptools.String(f.properties.Security_patch) != "" {
808 avb_add_hashtree_footer_args += fmt.Sprintf(" --prop com.android.build.%s.security_patch:%s", f.partitionName(), proptools.String(f.properties.Security_patch))
809 }
Shikha Panware6f30632022-12-21 12:54:45 +0000810 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jiyong Park71baa762021-01-18 21:11:03 +0900811 }
812
Cole Faust0d467052024-12-04 17:19:19 -0800813 if f.properties.File_contexts != nil && f.properties.Precompiled_file_contexts != nil {
814 ctx.ModuleErrorf("file_contexts and precompiled_file_contexts cannot both be set")
815 } else if f.properties.File_contexts != nil {
Inseob Kimcc8e5362021-02-03 14:05:24 +0900816 addPath("selinux_fc", f.buildFileContexts(ctx))
Cole Faust0d467052024-12-04 17:19:19 -0800817 } else if f.properties.Precompiled_file_contexts != nil {
818 src := android.PathForModuleSrc(ctx, *f.properties.Precompiled_file_contexts)
819 if src != nil {
820 addPath("selinux_fc", src)
821 }
Inseob Kimcc8e5362021-02-03 14:05:24 +0900822 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900823 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
824 addStr("timestamp", timestamp)
Spandan Dasa0ddc512025-01-06 20:23:55 +0000825 } else if ctx.Config().Getenv("USE_FIXED_TIMESTAMP_IMG_FILES") == "true" {
826 addStr("use_fixed_timestamp", "true")
Jooyung Han65f402b2022-04-21 14:24:04 +0900827 }
Spandan Dasa0ddc512025-01-06 20:23:55 +0000828
Jooyung Han65f402b2022-04-21 14:24:04 +0900829 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
830 addStr("uuid", uuid)
831 addStr("hash_seed", uuid)
832 }
mrziwang1a6291f2024-11-07 14:29:25 -0800833
Jihoon Kang40551e62025-01-14 21:55:08 +0000834 // Disable sparse only when partition size is not defined. disable_sparse has the same
835 // effect as <partition name>_disable_sparse.
836 if f.properties.Partition_size == nil {
837 addStr("disable_sparse", "true")
838 }
Cole Faust43a52c72024-11-26 12:46:08 -0800839
mrziwang1a6291f2024-11-07 14:29:25 -0800840 fst := f.fsType(ctx)
841 switch fst {
842 case erofsType:
843 // Add erofs properties
Cole Faust3e730972024-12-03 13:12:08 -0800844 addStr("erofs_default_compressor", proptools.StringDefault(f.properties.Erofs.Compressor, "lz4hc,9"))
845 if f.properties.Erofs.Compress_hints != nil {
846 src := android.PathForModuleSrc(ctx, *f.properties.Erofs.Compress_hints)
847 addPath("erofs_default_compress_hints", src)
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000848 }
849 if proptools.BoolDefault(f.properties.Erofs.Sparse, true) {
850 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2292;bpv=1;bpt=0;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b
851 addStr("erofs_sparse_flag", "-s")
852 }
mrziwang1a6291f2024-11-07 14:29:25 -0800853 case f2fsType:
854 if proptools.BoolDefault(f.properties.F2fs.Sparse, true) {
855 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2294;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b;bpv=1;bpt=0
856 addStr("f2fs_sparse_flag", "-S")
857 }
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000858 }
mrziwang1a6291f2024-11-07 14:29:25 -0800859 f.checkFsTypePropertyError(ctx, fst, fsTypeStr(fst))
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000860
Jihoon Kang983dd882025-01-13 23:14:11 +0000861 if f.properties.Partition_size != nil {
862 addStr("partition_size", strconv.FormatInt(*f.properties.Partition_size, 10))
863 }
864
Jihoon Kang6d08d922025-01-14 18:31:57 +0000865 if proptools.BoolDefault(f.properties.Support_casefolding, false) {
866 addStr("needs_casefold", "1")
867 }
868
869 if proptools.BoolDefault(f.properties.Support_project_quota, false) {
870 addStr("needs_projid", "1")
871 }
872
873 if proptools.BoolDefault(f.properties.Enable_compression, false) {
874 addStr("needs_compress", "1")
875 }
876
Cole Fauste1676122024-12-03 17:32:25 -0800877 propFilePreProcessing := android.PathForModuleOut(ctx, "prop_pre_processing")
878 android.WriteFileRuleVerbatim(ctx, propFilePreProcessing, propFileString.String())
Cole Faust4e9f5922024-11-13 16:09:23 -0800879 propFile := android.PathForModuleOut(ctx, "prop")
Cole Fauste1676122024-12-03 17:32:25 -0800880 ctx.Build(pctx, android.BuildParams{
Cole Faustefeb5c42024-12-16 10:47:26 -0800881 Rule: textFileProcessorRule,
882 Input: propFilePreProcessing,
883 Output: propFile,
Cole Fauste1676122024-12-03 17:32:25 -0800884 })
Jiyong Park72678312021-01-18 17:29:49 +0900885 return propFile, deps
886}
887
mrziwang1a6291f2024-11-07 14:29:25 -0800888// This method checks if there is any property set for the fstype(s) other than
889// the current fstype.
890func (f *filesystem) checkFsTypePropertyError(ctx android.ModuleContext, t fsType, fs string) {
891 raiseError := func(otherFsType, currentFsType string) {
892 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)
893 ctx.PropertyErrorf(otherFsType, errMsg)
894 }
895
896 if t != erofsType {
897 if f.properties.Erofs.Compressor != nil || f.properties.Erofs.Compress_hints != nil || f.properties.Erofs.Sparse != nil {
898 raiseError("erofs", fs)
899 }
900 }
901 if t != f2fsType {
902 if f.properties.F2fs.Sparse != nil {
903 raiseError("f2fs", fs)
904 }
905 }
906}
907
Jihoon Kang6da80752024-12-23 18:53:32 +0000908func includeFilesRootDir(ctx android.ModuleContext) (rootDirs android.Paths, partitions android.Paths) {
909 ctx.VisitDirectDepsWithTag(interPartitionInstallDependencyTag, func(m android.Module) {
910 if fsProvider, ok := android.OtherModuleProvider(ctx, m, FilesystemProvider); ok {
911 rootDirs = append(rootDirs, fsProvider.RootDir)
912 partitions = append(partitions, fsProvider.Output)
913 } else {
914 ctx.PropertyErrorf("include_files_of", "only filesystem modules can be listed in "+
915 "include_files_of but %s is not a filesystem module", m.Name())
916 }
917 })
918 return rootDirs, partitions
919}
920
Cole Faust11fda332025-01-14 16:47:19 -0800921func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) (android.Path, android.OutputPath, android.OutputPath) {
Jiyong Park11a65972021-02-01 21:09:38 +0900922 if proptools.Bool(f.properties.Use_avb) {
923 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
924 "Consider adding this to bootimg module and signing the entire boot image.")
925 }
926
Inseob Kimcc8e5362021-02-03 14:05:24 +0900927 if proptools.String(f.properties.File_contexts) != "" {
928 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
929 }
930
Cole Faust4a2a7c92024-03-12 12:44:40 -0700931 if f.properties.Include_make_built_files != "" {
932 ctx.PropertyErrorf("include_make_built_files", "include_make_built_files is not supported for compressed cpio image.")
933 }
934
Spandan Das420e16a2024-12-11 18:10:52 +0000935 rootDir := android.PathForModuleOut(ctx, f.rootDirString()).OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700936 rebasedDir := rootDir
937 if f.properties.Base_dir != nil {
938 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
939 }
940 builder := android.NewRuleBuilder(pctx, ctx)
941 // Wipe the root dir to get rid of leftover files from prior builds
942 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900943 specs := f.gatherFilteredPackagingSpecs(ctx)
Inseob Kim33f95a92024-07-11 15:44:49 +0900944 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700945
946 f.buildNonDepsFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900947 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Inseob Kimb7b84572024-04-30 10:51:47 +0900948 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
Justin Yun74f3f302024-05-07 14:32:14 +0900949 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
Kiyoung Kim67118212024-11-07 13:23:44 +0900950 f.filesystemBuilder.BuildLinkerConfigFile(ctx, builder, rebasedDir)
Jiyong Park11a65972021-02-01 21:09:38 +0900951
Jihoon Kang6da80752024-12-23 18:53:32 +0000952 rootDirs, partitions := includeFilesRootDir(ctx)
953
Cole Faust4e9f5922024-11-13 16:09:23 -0800954 output := android.PathForModuleOut(ctx, f.installFileName())
Jiyong Park837cdb22021-02-05 00:17:14 +0900955 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900956 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900957 Text(rootDir.String()) // input directory
Jihoon Kang6da80752024-12-23 18:53:32 +0000958
959 for i := range len(rootDirs) {
960 cmd.Text(rootDirs[i].String())
961 }
962 cmd.Implicits(partitions)
963
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000964 if nodeList := f.properties.Dev_nodes_description_file; nodeList != nil {
965 cmd.FlagWithInput("-n ", android.PathForModuleSrc(ctx, proptools.String(nodeList)))
966 }
Jiyong Park837cdb22021-02-05 00:17:14 +0900967 if compressed {
968 cmd.Text("|").
969 BuiltTool("lz4").
970 Flag("--favor-decSpeed"). // for faster boot
971 Flag("-12"). // maximum compression level
972 Flag("-l"). // legacy format for kernel
973 Text(">").Output(output)
974 } else {
975 cmd.Text(">").Output(output)
976 }
Jiyong Park11a65972021-02-01 21:09:38 +0900977
978 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900979 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900980
Cole Faust11fda332025-01-14 16:47:19 -0800981 return output, rootDir, rebasedDir
Jiyong Park11a65972021-02-01 21:09:38 +0900982}
983
Cole Faust4a2a7c92024-03-12 12:44:40 -0700984var validPartitions = []string{
985 "system",
986 "userdata",
987 "cache",
988 "system_other",
989 "vendor",
990 "product",
991 "system_ext",
992 "odm",
993 "vendor_dlkm",
994 "odm_dlkm",
995 "system_dlkm",
Cole Faust76a6e952024-11-07 16:56:45 -0800996 "ramdisk",
Cole Faust24938e22024-11-18 14:01:58 -0800997 "vendor_ramdisk",
Jihoon Kang3216c982024-12-02 19:42:20 +0000998 "recovery",
Cole Faust4a2a7c92024-03-12 12:44:40 -0700999}
1000
1001func (f *filesystem) addMakeBuiltFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.Path) {
1002 partition := f.properties.Include_make_built_files
1003 if partition == "" {
1004 return
1005 }
1006 if !slices.Contains(validPartitions, partition) {
1007 ctx.PropertyErrorf("include_make_built_files", "Expected one of %#v, found %q", validPartitions, partition)
1008 return
1009 }
1010 stampFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/staging_dir.stamp", ctx.Config().DeviceName(), partition)
1011 fileListFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partition)
1012 stagingDir := fmt.Sprintf("target/product/%s/%s", ctx.Config().DeviceName(), partition)
1013
1014 builder.Command().BuiltTool("merge_directories").
1015 Implicit(android.PathForArbitraryOutput(ctx, stampFile)).
1016 Text("--ignore-duplicates").
1017 FlagWithInput("--file-list", android.PathForArbitraryOutput(ctx, fileListFile)).
1018 Text(rootDir.String()).
1019 Text(android.PathForArbitraryOutput(ctx, stagingDir).String())
1020}
1021
Inseob Kimb7b84572024-04-30 10:51:47 +09001022func (f *filesystem) buildEventLogtagsFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
1023 if !proptools.Bool(f.properties.Build_logtags) {
1024 return
1025 }
1026
Inseob Kimb7b84572024-04-30 10:51:47 +09001027 etcPath := rebasedDir.Join(ctx, "etc")
1028 eventLogtagsPath := etcPath.Join(ctx, "event-log-tags")
1029 builder.Command().Text("mkdir").Flag("-p").Text(etcPath.String())
Cole Fauste4506af2024-12-11 14:14:50 -08001030 builder.Command().Text("cp").Input(android.MergedLogtagsPath(ctx)).Text(eventLogtagsPath.String())
Kiyoung Kim99a954d2024-06-21 14:22:20 +09001031
1032 f.appendToEntry(ctx, eventLogtagsPath)
Inseob Kimb7b84572024-04-30 10:51:47 +09001033}
1034
Kiyoung Kim67118212024-11-07 13:23:44 +09001035func (f *filesystem) BuildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
Spandan Das2047a4c2024-11-11 21:24:58 +00001036 if !proptools.Bool(f.properties.Linker_config.Gen_linker_config) {
Spandan Das92631882024-10-28 22:49:38 +00001037 return
1038 }
1039
Spandan Das918191e2024-10-31 18:27:23 +00001040 provideModules, _ := f.getLibsForLinkerConfig(ctx)
Cole Faustfee27012024-12-13 14:10:31 -08001041 intermediateOutput := android.PathForModuleOut(ctx, "linker.config.pb")
1042 linkerconfig.BuildLinkerConfig(ctx, android.PathsForModuleSrc(ctx, f.properties.Linker_config.Linker_config_srcs), provideModules, nil, intermediateOutput)
Spandan Das92631882024-10-28 22:49:38 +00001043 output := rebasedDir.Join(ctx, "etc", "linker.config.pb")
Cole Faustfee27012024-12-13 14:10:31 -08001044 builder.Command().Text("cp").Input(intermediateOutput).Output(output)
Spandan Das92631882024-10-28 22:49:38 +00001045
1046 f.appendToEntry(ctx, output)
1047}
1048
Kiyoung Kim23be5bb2024-11-27 00:50:30 +00001049func (f *filesystem) ShouldUseVintfFragmentModuleOnly() bool {
1050 return false
1051}
1052
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001053type partition interface {
1054 PartitionType() string
1055}
1056
Cole Faust9a24d902024-03-18 15:38:12 -07001057func (f *filesystem) PartitionType() string {
1058 return proptools.StringDefault(f.properties.Partition_type, "system")
1059}
1060
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001061var _ partition = (*filesystem)(nil)
1062
Jiyong Park65c49f52020-11-24 14:23:26 +09001063var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
1064
1065// Implements android.AndroidMkEntriesProvider
1066func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
1067 return []android.AndroidMkEntries{android.AndroidMkEntries{
1068 Class: "ETC",
1069 OutputFile: android.OptionalPathForPath(f.output),
1070 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07001071 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -08001072 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +09001073 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
Kiyoung Kim99a954d2024-06-21 14:22:20 +09001074 entries.SetString("LOCAL_FILESYSTEM_FILELIST", f.fileListFile.String())
Jiyong Park65c49f52020-11-24 14:23:26 +09001075 },
1076 },
1077 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +09001078}
Jiyong Park12a719c2021-01-07 15:31:24 +09001079
1080// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
1081// package to have access to the output file.
1082type Filesystem interface {
1083 android.Module
1084 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +09001085
1086 // Returns the output file that is signed by avbtool. If this module is not signed, returns
1087 // nil.
1088 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +09001089}
1090
1091var _ Filesystem = (*filesystem)(nil)
1092
1093func (f *filesystem) OutputPath() android.Path {
1094 return f.output
1095}
Jiyong Park972e06c2021-03-15 23:32:49 +09001096
1097func (f *filesystem) SignedOutputPath() android.Path {
1098 if proptools.Bool(f.properties.Use_avb) {
1099 return f.OutputPath()
1100 }
1101 return nil
1102}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +09001103
1104// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
1105// Note that "apex" module installs its contents to "apex"(fake partition) as well
1106// for symbol lookup by imitating "activated" paths.
1107func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Inseob Kim3c0a0422024-11-05 17:21:37 +09001108 specs := f.PackagingBase.GatherPackagingSpecsWithFilterAndModifier(ctx, f.filesystemBuilder.FilterPackagingSpec, f.filesystemBuilder.ModifyPackagingSpec)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +09001109 return specs
1110}
Jooyung Han65f402b2022-04-21 14:24:04 +09001111
1112func sha1sum(values []string) string {
1113 h := sha256.New()
1114 for _, value := range values {
1115 io.WriteString(h, value)
1116 }
1117 return fmt.Sprintf("%x", h.Sum(nil))
1118}
Jooyung Hane6067592023-03-16 13:11:17 +09001119
1120// Base cc.UseCoverage
1121
1122var _ cc.UseCoverage = (*filesystem)(nil)
1123
Colin Crosse1a85552024-06-14 12:17:37 -07001124func (*filesystem) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +09001125 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
1126}
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001127
1128// android_filesystem_defaults
1129
1130type filesystemDefaults struct {
1131 android.ModuleBase
1132 android.DefaultsModuleBase
1133
Inseob Kim3c0a0422024-11-05 17:21:37 +09001134 properties FilesystemProperties
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001135}
1136
1137// android_filesystem_defaults is a default module for android_filesystem and android_system_image
1138func filesystemDefaultsFactory() android.Module {
1139 module := &filesystemDefaults{}
1140 module.AddProperties(&module.properties)
1141 module.AddProperties(&android.PackagingProperties{})
1142 android.InitDefaultsModule(module)
1143 return module
1144}
1145
1146func (f *filesystemDefaults) PartitionType() string {
1147 return proptools.StringDefault(f.properties.Partition_type, "system")
1148}
1149
1150var _ partition = (*filesystemDefaults)(nil)
1151
1152func (f *filesystemDefaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1153 validatePartitionType(ctx, f)
Yu Liufc8d5c12025-01-09 00:19:06 +00001154 android.SetProvider(ctx, FilesystemDefaultsInfoProvider, FilesystemDefaultsInfo{
1155 PartitionType: f.PartitionType(),
1156 })
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001157}
Spandan Das918191e2024-10-31 18:27:23 +00001158
1159// getLibsForLinkerConfig returns
1160// 1. A list of libraries installed in this filesystem
1161// 2. A list of dep libraries _not_ installed in this filesystem
1162//
1163// `linkerconfig.BuildLinkerConfig` will convert these two to a linker.config.pb for the filesystem
1164// (1) will be added to --provideLibs if they are C libraries with a stable interface (has stubs)
1165// (2) will be added to --requireLibs if they are C libraries with a stable interface (has stubs)
Yu Liu68a70b72025-01-08 22:54:44 +00001166func (f *filesystem) getLibsForLinkerConfig(ctx android.ModuleContext) ([]android.ModuleProxy, []android.ModuleProxy) {
Spandan Das918191e2024-10-31 18:27:23 +00001167 // we need "Module"s for packaging items
Yu Liu68a70b72025-01-08 22:54:44 +00001168 modulesInPackageByModule := make(map[android.ModuleProxy]bool)
Spandan Das918191e2024-10-31 18:27:23 +00001169 modulesInPackageByName := make(map[string]bool)
1170
1171 deps := f.gatherFilteredPackagingSpecs(ctx)
Yu Liu68a70b72025-01-08 22:54:44 +00001172 ctx.WalkDepsProxy(func(child, parent android.ModuleProxy) bool {
1173 if !android.OtherModuleProviderOrDefault(ctx, child, android.CommonModuleInfoKey).Enabled {
Yu Liu9c6b6762025-01-08 22:04:35 +00001174 return false
1175 }
Spandan Das918191e2024-10-31 18:27:23 +00001176 for _, ps := range android.OtherModuleProviderOrDefault(
1177 ctx, child, android.InstallFilesProvider).PackagingSpecs {
Spandan Dasecf667f2024-12-05 00:58:56 +00001178 if _, ok := deps[ps.RelPathInPackage()]; ok && ps.Partition() == f.PartitionType() {
Spandan Das918191e2024-10-31 18:27:23 +00001179 modulesInPackageByModule[child] = true
1180 modulesInPackageByName[child.Name()] = true
1181 return true
1182 }
1183 }
1184 return true
1185 })
1186
Yu Liu68a70b72025-01-08 22:54:44 +00001187 provideModules := make([]android.ModuleProxy, 0, len(modulesInPackageByModule))
Spandan Das918191e2024-10-31 18:27:23 +00001188 for mod := range modulesInPackageByModule {
1189 provideModules = append(provideModules, mod)
1190 }
1191
Yu Liu68a70b72025-01-08 22:54:44 +00001192 var requireModules []android.ModuleProxy
1193 ctx.WalkDepsProxy(func(child, parent android.ModuleProxy) bool {
1194 if !android.OtherModuleProviderOrDefault(ctx, child, android.CommonModuleInfoKey).Enabled {
Yu Liu9c6b6762025-01-08 22:04:35 +00001195 return false
1196 }
Spandan Das918191e2024-10-31 18:27:23 +00001197 _, parentInPackage := modulesInPackageByModule[parent]
1198 _, childInPackageName := modulesInPackageByName[child.Name()]
1199
1200 // When parent is in the package, and child (or its variant) is not, this can be from an interface.
1201 if parentInPackage && !childInPackageName {
1202 requireModules = append(requireModules, child)
1203 }
1204 return true
1205 })
1206
1207 return provideModules, requireModules
1208}
Cole Faust26bdac52024-11-19 13:37:53 -08001209
1210// Checks that the given file doesn't exceed the given size, and will also print a warning
1211// if it's nearing the maximum size. Equivalent to assert-max-image-size in make:
1212// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/definitions.mk;l=3455;drc=993c4de29a02a6accd60ceaaee153307e1a18d10
1213func assertMaxImageSize(builder *android.RuleBuilder, image android.Path, maxSize int64, addAvbLater bool) {
1214 if addAvbLater {
1215 // The value 69632 is derived from MAX_VBMETA_SIZE + MAX_FOOTER_SIZE in avbtool.
1216 // Logic copied from make:
1217 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=228;drc=a6a0007ef24e16c0b79f439beac4a118416717e6
1218 maxSize -= 69632
1219 }
1220 cmd := builder.Command()
1221 cmd.Textf(`file="%s"; maxsize="%d";`+
1222 `total=$(stat -c "%%s" "$file" | tr -d '\n');`+
1223 `if [ "$total" -gt "$maxsize" ]; then `+
1224 ` echo "error: $file too large ($total > $maxsize)";`+
1225 ` false;`+
1226 `elif [ "$total" -gt $((maxsize - 32768)) ]; then `+
1227 ` echo "WARNING: $file approaching size limit ($total now; limit $maxsize)";`+
1228 `fi`,
1229 image, maxSize)
1230 cmd.Implicit(image)
1231}
Spandan Das71be42d2024-11-20 18:34:16 +00001232
1233// addAutogeneratedRroDeps walks the transitive closure of vendor and product partitions.
1234// It visits apps installed in system and system_ext partitions, and adds the autogenerated
1235// RRO modules to its own deps.
1236func addAutogeneratedRroDeps(ctx android.BottomUpMutatorContext) {
1237 f, ok := ctx.Module().(*filesystem)
1238 if !ok {
1239 return
1240 }
1241 thisPartition := f.PartitionType()
1242 if thisPartition != "vendor" && thisPartition != "product" {
Cole Faust34592c02024-12-13 11:20:24 -08001243 if f.properties.Android_filesystem_deps.System != nil {
1244 ctx.PropertyErrorf("android_filesystem_deps.system", "only vendor or product partitions can use android_filesystem_deps")
1245 }
1246 if f.properties.Android_filesystem_deps.System_ext != nil {
1247 ctx.PropertyErrorf("android_filesystem_deps.system_ext", "only vendor or product partitions can use android_filesystem_deps")
1248 }
Spandan Das71be42d2024-11-20 18:34:16 +00001249 return
1250 }
1251 ctx.WalkDeps(func(child, parent android.Module) bool {
1252 depTag := ctx.OtherModuleDependencyTag(child)
1253 if parent.Name() == f.Name() && depTag != interPartitionDependencyTag {
1254 return false // This is a module listed in deps of vendor/product filesystem
1255 }
1256 if vendorOverlay := java.AutogeneratedRroModuleName(ctx, child.Name(), "vendor"); ctx.OtherModuleExists(vendorOverlay) && thisPartition == "vendor" {
1257 ctx.AddFarVariationDependencies(nil, dependencyTagWithVisibilityEnforcementBypass, vendorOverlay)
1258 }
1259 if productOverlay := java.AutogeneratedRroModuleName(ctx, child.Name(), "product"); ctx.OtherModuleExists(productOverlay) && thisPartition == "product" {
1260 ctx.AddFarVariationDependencies(nil, dependencyTagWithVisibilityEnforcementBypass, productOverlay)
1261 }
1262 return true
1263 })
1264}