blob: 822ba43ad865391e923cdfedf3da427b05791ed2 [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
Inseob Kimb7b84572024-04-30 10:51:47 +0900176 // When set, builds etc/event-log-tags file by merging logtags from all dependencies.
177 // Default is false
178 Build_logtags *bool
179
Justin Yun74f3f302024-05-07 14:32:14 +0900180 // Install aconfig_flags.pb file for the modules installed in this partition.
181 Gen_aconfig_flags_pb *bool
182
Cole Faust34592c02024-12-13 11:20:24 -0800183 // List of names of other filesystem partitions to import their aconfig flags from.
184 // This is used for the system partition to import system_ext's aconfig flags, as currently
185 // those are considered one "container": aosp/3261300
186 Import_aconfig_flags_from []string
187
Inseob Kim53391842024-03-29 17:44:07 +0900188 Fsverity fsverityProperties
Cole Faust92ccbe22024-10-03 14:38:37 -0700189
190 // If this property is set to true, the filesystem will call ctx.UncheckedModule(), causing
191 // it to not be built on checkbuilds. Used for the automatic migration from make to soong
192 // build modules, where we want to emit some not-yet-working filesystems and we don't want them
193 // to be built.
194 Unchecked_module *bool `blueprint:"mutated"`
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000195
196 Erofs ErofsProperties
Jihoon Kang0d545b82024-10-11 00:21:57 +0000197
mrziwang1a6291f2024-11-07 14:29:25 -0800198 F2fs F2fsProperties
199
Spandan Das2047a4c2024-11-11 21:24:58 +0000200 Linker_config LinkerConfigProperties
Spandan Das92631882024-10-28 22:49:38 +0000201
Jihoon Kang0d545b82024-10-11 00:21:57 +0000202 // Determines if the module is auto-generated from Soong or not. If the module is
203 // auto-generated, its deps are exempted from visibility enforcement.
204 Is_auto_generated *bool
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000205
206 // Path to the dev nodes description file. This is only needed for building the ramdisk
207 // partition and should not be explicitly specified.
208 Dev_nodes_description_file *string `android:"path" blueprint:"mutated"`
Spandan Das71be42d2024-11-20 18:34:16 +0000209
210 // Additional dependencies used for building android products
211 Android_filesystem_deps AndroidFilesystemDeps
Spandan Dasc49b85e2025-01-10 00:51:25 +0000212
213 // Name of the output. Default is $(module_name).img
214 Stem *string
Jihoon Kang983dd882025-01-13 23:14:11 +0000215
216 // The size of the partition on the device. It will be a build error if this built partition
217 // image exceeds this size.
218 Partition_size *int64
Jihoon Kang6d08d922025-01-14 18:31:57 +0000219
220 // Whether to format f2fs and ext4 in a way that supports casefolding
221 Support_casefolding *bool
222
223 // Whether to format f2fs and ext4 in a way that supports project quotas
224 Support_project_quota *bool
225
226 // Whether to enable per-file compression in f2fs
227 Enable_compression *bool
Spandan Das71be42d2024-11-20 18:34:16 +0000228}
229
230type AndroidFilesystemDeps struct {
231 System *string
232 System_ext *string
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000233}
234
235// Additional properties required to generate erofs FS partitions.
236type ErofsProperties struct {
237 // Compressor and Compression level passed to mkfs.erofs. e.g. (lz4hc,9)
238 // Please see external/erofs-utils/README for complete documentation.
239 Compressor *string
240
241 // Used as --compress-hints for mkfs.erofs
242 Compress_hints *string `android:"path"`
243
244 Sparse *bool
Jiyong Park71baa762021-01-18 21:11:03 +0900245}
246
mrziwang1a6291f2024-11-07 14:29:25 -0800247// Additional properties required to generate f2fs FS partitions.
248type F2fsProperties struct {
249 Sparse *bool
250}
251
Spandan Das173256b2024-10-31 19:59:30 +0000252type LinkerConfigProperties struct {
253
254 // Build a linker.config.pb file
255 Gen_linker_config *bool
256
257 // List of files (in .json format) that will be converted to a linker config file (in .pb format).
258 // The linker config file be installed in the filesystem at /etc/linker.config.pb
259 Linker_config_srcs []string `android:"path"`
260}
261
Jiyong Park65c49f52020-11-24 14:23:26 +0900262// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
263// image. The filesystem images are expected to be mounted in the target device, which means the
264// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
265// The modules are placed in the filesystem image just like they are installed to the ordinary
266// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Cole Faust92ccbe22024-10-03 14:38:37 -0700267func FilesystemFactory() android.Module {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900268 module := &filesystem{}
Kiyoung Kim67118212024-11-07 13:23:44 +0900269 module.filesystemBuilder = module
Cole Faust2cfe6962024-09-17 11:31:14 -0700270 initFilesystemModule(module, module)
Jiyong Parkfa616132021-04-20 11:36:40 +0900271 return module
272}
273
Cole Faust2cfe6962024-09-17 11:31:14 -0700274func initFilesystemModule(module android.DefaultableModule, filesystemModule *filesystem) {
275 module.AddProperties(&filesystemModule.properties)
276 android.InitPackageModule(filesystemModule)
277 filesystemModule.PackagingBase.DepsCollectFirstTargetOnly = true
Jihoon Kang79196c52024-10-30 18:49:47 +0000278 filesystemModule.PackagingBase.AllowHighPriorityDeps = true
Jiyong Park6f0f6882020-11-12 13:14:30 +0900279 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900280 android.InitDefaultableModule(module)
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000281
282 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
283 filesystemModule.setDevNodesDescriptionProp()
284 })
Jiyong Park6f0f6882020-11-12 13:14:30 +0900285}
286
Jihoon Kang0d545b82024-10-11 00:21:57 +0000287type depTag struct {
Jiyong Park12a719c2021-01-07 15:31:24 +0900288 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900289 android.PackagingItemAlwaysDepTag
Jihoon Kang0d545b82024-10-11 00:21:57 +0000290}
291
292var dependencyTag = depTag{}
293
294type depTagWithVisibilityEnforcementBypass struct {
295 depTag
296}
297
Spandan Das71be42d2024-11-20 18:34:16 +0000298type interPartitionDepTag struct {
299 blueprint.BaseDependencyTag
300}
301
302var interPartitionDependencyTag = interPartitionDepTag{}
303
Jihoon Kang6da80752024-12-23 18:53:32 +0000304var interPartitionInstallDependencyTag = interPartitionDepTag{}
305
Jihoon Kang0d545b82024-10-11 00:21:57 +0000306var _ android.ExcludeFromVisibilityEnforcementTag = (*depTagWithVisibilityEnforcementBypass)(nil)
307
308func (t depTagWithVisibilityEnforcementBypass) ExcludeFromVisibilityEnforcement() {}
309
310var dependencyTagWithVisibilityEnforcementBypass = depTagWithVisibilityEnforcementBypass{}
Jiyong Park65b62242020-11-25 12:44:59 +0900311
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000312// ramdiskDevNodesDescription is the name of the filegroup module that provides the file that
313// contains the description of dev nodes added to the CPIO archive for the ramdisk partition.
314const ramdiskDevNodesDescription = "ramdisk_node_list"
315
316func (f *filesystem) setDevNodesDescriptionProp() {
317 if proptools.String(f.properties.Partition_name) == "ramdisk" {
318 f.properties.Dev_nodes_description_file = proptools.StringPtr(":" + ramdiskDevNodesDescription)
319 }
320}
321
Jiyong Park6f0f6882020-11-12 13:14:30 +0900322func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000323 if proptools.Bool(f.properties.Is_auto_generated) {
324 f.AddDeps(ctx, dependencyTagWithVisibilityEnforcementBypass)
325 } else {
326 f.AddDeps(ctx, dependencyTag)
327 }
Spandan Das71be42d2024-11-20 18:34:16 +0000328 if f.properties.Android_filesystem_deps.System != nil {
329 ctx.AddDependency(ctx.Module(), interPartitionDependencyTag, proptools.String(f.properties.Android_filesystem_deps.System))
330 }
331 if f.properties.Android_filesystem_deps.System_ext != nil {
332 ctx.AddDependency(ctx.Module(), interPartitionDependencyTag, proptools.String(f.properties.Android_filesystem_deps.System_ext))
333 }
Cole Faust34592c02024-12-13 11:20:24 -0800334 for _, partition := range f.properties.Import_aconfig_flags_from {
335 ctx.AddDependency(ctx.Module(), importAconfigDependencyTag, partition)
336 }
Jihoon Kang6da80752024-12-23 18:53:32 +0000337 for _, partition := range f.properties.Include_files_of {
338 ctx.AddDependency(ctx.Module(), interPartitionInstallDependencyTag, partition)
339 }
Jiyong Park6f0f6882020-11-12 13:14:30 +0900340}
341
Jiyong Park11a65972021-02-01 21:09:38 +0900342type fsType int
343
344const (
345 ext4Type fsType = iota
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000346 erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800347 f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900348 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900349 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900350 unknown
351)
352
Spandan Das7a46f6c2024-10-14 18:41:18 +0000353func (fs fsType) IsUnknown() bool {
354 return fs == unknown
355}
356
Cole Faust92ccbe22024-10-03 14:38:37 -0700357type FilesystemInfo struct {
Cole Faust44080412024-12-20 14:17:07 -0800358 // The built filesystem image
359 Output android.Path
Spandan Das1f0a5a12025-01-15 00:53:15 +0000360 // An additional hermetic filesystem image.
361 // e.g. this will contain inodes with pinned timestamps.
362 // This will be copied to target_files.zip
363 OutputHermetic android.Path
Cole Faust92ccbe22024-10-03 14:38:37 -0700364 // A text file containing the list of paths installed on the partition.
365 FileListFile android.Path
Cole Faust44080412024-12-20 14:17:07 -0800366 // The root staging directory used to build the output filesystem. If consuming this, make sure
367 // to add a dependency on the Output file, as you cannot add dependencies on directories
368 // in ninja.
369 RootDir android.Path
Cole Faust11fda332025-01-14 16:47:19 -0800370 // The rebased staging directory used to build the output filesystem. If consuming this, make
371 // sure to add a dependency on the Output file, as you cannot add dependencies on directories
372 // in ninja. In many cases this is the same as RootDir, only in the system partition is it
373 // different. There, it points to the "system" sub-directory of RootDir.
374 RebasedDir android.Path
Spandan Das33c9c472025-01-14 19:26:23 +0000375 // A text file with block data of the .img file
376 // This is an implicit output of `build_image`
377 MapFile android.Path
Cole Faust11fda332025-01-14 16:47:19 -0800378 // Name of the module that produced this FilesystemInfo origionally. (though it may be
379 // re-exported by super images or boot images)
380 ModuleName string
Cole Faust74ee4e02025-01-16 14:55:35 -0800381 // The property file generated by this module and passed to build_image.
382 // It's exported here so that system_other can reuse system's property file.
383 BuildImagePropFile android.Path
384 // Paths to all the tools referenced inside of the build image property file.
385 BuildImagePropFileDeps android.Paths
Cole Faustb8e280f2025-01-16 16:33:26 -0800386 // Packaging specs to be installed on the system_other image, for the initial boot's dexpreopt.
387 SpecsForSystemOther map[string]android.PackagingSpec
Cole Faust92ccbe22024-10-03 14:38:37 -0700388}
389
390var FilesystemProvider = blueprint.NewProvider[FilesystemInfo]()
391
Yu Liufc8d5c12025-01-09 00:19:06 +0000392type FilesystemDefaultsInfo struct {
393 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
394 // checks, and will be used in the future for API surface checks.
395 PartitionType string
396}
397
398var FilesystemDefaultsInfoProvider = blueprint.NewProvider[FilesystemDefaultsInfo]()
399
Spandan Das7a46f6c2024-10-14 18:41:18 +0000400func GetFsTypeFromString(ctx android.EarlyModuleContext, typeStr string) fsType {
Jiyong Park11a65972021-02-01 21:09:38 +0900401 switch typeStr {
402 case "ext4":
403 return ext4Type
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000404 case "erofs":
405 return erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800406 case "f2fs":
407 return f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900408 case "compressed_cpio":
409 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900410 case "cpio":
411 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900412 default:
Jiyong Park11a65972021-02-01 21:09:38 +0900413 return unknown
414 }
415}
416
Spandan Das7a46f6c2024-10-14 18:41:18 +0000417func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
418 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
419 fsType := GetFsTypeFromString(ctx, typeStr)
420 if fsType == unknown {
421 ctx.PropertyErrorf("type", "%q not supported", typeStr)
422 }
423 return fsType
424}
425
Jiyong Park65c49f52020-11-24 14:23:26 +0900426func (f *filesystem) installFileName() string {
Spandan Dasc49b85e2025-01-10 00:51:25 +0000427 return proptools.StringDefault(f.properties.Stem, f.BaseModuleName()+".img")
Jiyong Park65c49f52020-11-24 14:23:26 +0900428}
429
Inseob Kim53391842024-03-29 17:44:07 +0900430func (f *filesystem) partitionName() string {
431 return proptools.StringDefault(f.properties.Partition_name, f.Name())
432}
433
Kiyoung Kim67118212024-11-07 13:23:44 +0900434func (f *filesystem) FilterPackagingSpec(ps android.PackagingSpec) bool {
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000435 // Filesystem module respects the installation semantic. A PackagingSpec from a module with
436 // IsSkipInstall() is skipped.
Cole Faust76a6e952024-11-07 16:56:45 -0800437 if ps.SkipInstall() {
438 return false
Spandan Das6d056502024-10-21 15:40:32 +0000439 }
Cole Faust76a6e952024-11-07 16:56:45 -0800440 if proptools.Bool(f.properties.Is_auto_generated) { // TODO (spandandas): Remove this.
441 pt := f.PartitionType()
Cole Faustc88cff12024-11-12 13:24:05 -0800442 return ps.Partition() == pt || strings.HasPrefix(ps.Partition(), pt+"/")
Cole Faust76a6e952024-11-07 16:56:45 -0800443 }
444 return true
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000445}
446
Inseob Kim3c0a0422024-11-05 17:21:37 +0900447func (f *filesystem) ModifyPackagingSpec(ps *android.PackagingSpec) {
Cole Faustc88cff12024-11-12 13:24:05 -0800448 // Sometimes, android.modulePartition() returns a path with >1 path components.
449 // This makes the partition field of packagingSpecs have multiple components, like
450 // "system/product". Right now, the filesystem module doesn't look at the partition field
451 // when deciding what path to install the file under, only the RelPathInPackage field, so
452 // we move the later path components from partition to relPathInPackage. This should probably
453 // be revisited in the future.
454 prefix := f.PartitionType() + "/"
455 if strings.HasPrefix(ps.Partition(), prefix) {
456 subPartition := strings.TrimPrefix(ps.Partition(), prefix)
457 ps.SetPartition(f.PartitionType())
458 ps.SetRelPathInPackage(filepath.Join(subPartition, ps.RelPathInPackage()))
459 }
Inseob Kim3c0a0422024-11-05 17:21:37 +0900460}
461
Jiyong Park6f0f6882020-11-12 13:14:30 +0900462var pctx = android.NewPackageContext("android/soong/filesystem")
463
464func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900465 validatePartitionType(ctx, f)
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000466 if f.filesystemBuilder.ShouldUseVintfFragmentModuleOnly() {
467 f.validateVintfFragments(ctx)
468 }
Jihoon Kang6da80752024-12-23 18:53:32 +0000469
470 if len(f.properties.Include_files_of) > 0 && !android.InList(f.fsType(ctx), []fsType{compressedCpioType, cpioType}) {
471 ctx.PropertyErrorf("include_files_of", "include_files_of is only supported for cpio and compressed cpio filesystem types.")
472 }
473
Cole Faust62cfaeb2025-01-15 18:06:40 -0800474 rootDir := android.PathForModuleOut(ctx, f.rootDirString()).OutputPath
475 rebasedDir := rootDir
476 if f.properties.Base_dir != nil {
477 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
478 }
479 builder := android.NewRuleBuilder(pctx, ctx)
480
481 // Wipe the root dir to get rid of leftover files from prior builds
482 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
483 specs := f.gatherFilteredPackagingSpecs(ctx)
484 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
485
486 f.buildNonDepsFiles(ctx, builder, rootDir)
487 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
488 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
489 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
490 f.filesystemBuilder.BuildLinkerConfigFile(ctx, builder, rebasedDir)
491
Spandan Das33c9c472025-01-14 19:26:23 +0000492 var mapFile android.Path
Spandan Das1f0a5a12025-01-15 00:53:15 +0000493 var outputHermetic android.Path
Cole Faust74ee4e02025-01-16 14:55:35 -0800494 var buildImagePropFile android.Path
495 var buildImagePropFileDeps android.Paths
Jiyong Park11a65972021-02-01 21:09:38 +0900496 switch f.fsType(ctx) {
mrziwang1a6291f2024-11-07 14:29:25 -0800497 case ext4Type, erofsType, f2fsType:
Cole Faust74ee4e02025-01-16 14:55:35 -0800498 f.output, outputHermetic, buildImagePropFile, buildImagePropFileDeps = f.buildImageUsingBuildImage(ctx, builder, rootDir, rebasedDir)
Spandan Das33c9c472025-01-14 19:26:23 +0000499 mapFile = f.getMapFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900500 case compressedCpioType:
Cole Faust62cfaeb2025-01-15 18:06:40 -0800501 f.output = f.buildCpioImage(ctx, builder, rootDir, true)
Jiyong Park837cdb22021-02-05 00:17:14 +0900502 case cpioType:
Cole Faust62cfaeb2025-01-15 18:06:40 -0800503 f.output = f.buildCpioImage(ctx, builder, rootDir, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900504 default:
505 return
506 }
507
508 f.installDir = android.PathForModuleInstall(ctx, "etc")
509 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
mrziwang555d1332024-06-07 11:15:33 -0700510 ctx.SetOutputFiles([]android.Path{f.output}, "")
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900511
Jihoon Kang6da80752024-12-23 18:53:32 +0000512 if f.partitionName() == "recovery" {
513 rootDir = rootDir.Join(ctx, "root")
514 }
515
Cole Faust4e9f5922024-11-13 16:09:23 -0800516 fileListFile := android.PathForModuleOut(ctx, "fileList")
517 android.WriteFileRule(ctx, fileListFile, f.installedFilesList())
Cole Faust92ccbe22024-10-03 14:38:37 -0700518
Spandan Das33c9c472025-01-14 19:26:23 +0000519 fsInfo := FilesystemInfo{
Cole Faust74ee4e02025-01-16 14:55:35 -0800520 Output: f.output,
521 OutputHermetic: outputHermetic,
522 FileListFile: fileListFile,
523 RootDir: rootDir,
524 RebasedDir: rebasedDir,
525 MapFile: mapFile,
526 ModuleName: ctx.ModuleName(),
527 BuildImagePropFile: buildImagePropFile,
528 BuildImagePropFileDeps: buildImagePropFileDeps,
Cole Faustb8e280f2025-01-16 16:33:26 -0800529 SpecsForSystemOther: f.systemOtherFiles(ctx),
Spandan Das1f0a5a12025-01-15 00:53:15 +0000530 }
Spandan Das33c9c472025-01-14 19:26:23 +0000531
532 android.SetProvider(ctx, FilesystemProvider, fsInfo)
Spandan Das3ec6d062025-01-09 19:37:47 +0000533
Cole Faust4e9f5922024-11-13 16:09:23 -0800534 f.fileListFile = fileListFile
Cole Faust92ccbe22024-10-03 14:38:37 -0700535
536 if proptools.Bool(f.properties.Unchecked_module) {
537 ctx.UncheckedModule()
538 }
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900539}
540
Spandan Das33c9c472025-01-14 19:26:23 +0000541func (f *filesystem) getMapFile(ctx android.ModuleContext) android.WritablePath {
542 // create the filepath by replacing the extension of the corresponding img file
543 return android.PathForModuleOut(ctx, f.installFileName()).ReplaceExtension(ctx, "map")
544}
545
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000546func (f *filesystem) validateVintfFragments(ctx android.ModuleContext) {
547 visitedModule := map[string]bool{}
548 packagingSpecs := f.gatherFilteredPackagingSpecs(ctx)
549
550 moduleInFileSystem := func(mod android.Module) bool {
551 for _, ps := range android.OtherModuleProviderOrDefault(
552 ctx, mod, android.InstallFilesProvider).PackagingSpecs {
553 if _, ok := packagingSpecs[ps.RelPathInPackage()]; ok {
554 return true
555 }
556 }
557 return false
558 }
559
560 ctx.WalkDeps(func(child, parent android.Module) bool {
561 if visitedModule[child.Name()] {
562 return false
563 }
564 if !moduleInFileSystem(child) {
565 visitedModule[child.Name()] = true
566 return true
567 }
568 if vintfFragments := child.VintfFragments(ctx); vintfFragments != nil {
569 ctx.PropertyErrorf(
570 "vintf_fragments",
571 "Module %s is referenced by soong-defined filesystem %s with property vintf_fragments(%s) in use."+
572 " Use vintf_fragment_modules property instead.",
573 child.Name(),
574 f.BaseModuleName(),
575 strings.Join(vintfFragments, ", "),
576 )
577 }
578 visitedModule[child.Name()] = true
579 return true
580 })
581}
582
Cole Faust4e9f5922024-11-13 16:09:23 -0800583func (f *filesystem) appendToEntry(ctx android.ModuleContext, installedFile android.Path) {
Spandan Das420e16a2024-12-11 18:10:52 +0000584 partitionBaseDir := android.PathForModuleOut(ctx, f.rootDirString(), proptools.String(f.properties.Base_dir)).String() + "/"
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900585
586 relPath, inTargetPartition := strings.CutPrefix(installedFile.String(), partitionBaseDir)
587 if inTargetPartition {
588 f.entries = append(f.entries, relPath)
589 }
590}
591
592func (f *filesystem) installedFilesList() string {
593 installedFilePaths := android.FirstUniqueStrings(f.entries)
594 slices.Sort(installedFilePaths)
595
596 return strings.Join(installedFilePaths, "\n")
Jiyong Park11a65972021-02-01 21:09:38 +0900597}
598
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900599func validatePartitionType(ctx android.ModuleContext, p partition) {
600 if !android.InList(p.PartitionType(), validPartitions) {
601 ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, p.PartitionType())
602 }
603
Yu Liufc8d5c12025-01-09 00:19:06 +0000604 ctx.VisitDirectDepsProxyWithTag(android.DefaultsDepTag, func(m android.ModuleProxy) {
605 if fdm, ok := android.OtherModuleProvider(ctx, m, FilesystemDefaultsInfoProvider); ok {
606 if p.PartitionType() != fdm.PartitionType {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900607 ctx.PropertyErrorf("partition_type",
608 "%s doesn't match with the partition type %s of the filesystem default module %s",
Yu Liufc8d5c12025-01-09 00:19:06 +0000609 p.PartitionType(), fdm.PartitionType, m.Name())
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900610 }
611 }
612 })
613}
614
Cole Faust3b806d32024-03-11 15:15:03 -0700615// Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files
616// already in `rootDir`.
617func (f *filesystem) buildNonDepsFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.OutputPath) {
Inseob Kim14199b02021-02-09 21:18:31 +0900618 // create dirs and symlinks
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700619 for _, dir := range f.properties.Dirs.GetOrDefault(ctx, nil) {
Inseob Kim14199b02021-02-09 21:18:31 +0900620 // OutputPath.Join verifies dir
621 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
622 }
623
624 for _, symlink := range f.properties.Symlinks {
625 name := strings.TrimSpace(proptools.String(symlink.Name))
626 target := strings.TrimSpace(proptools.String(symlink.Target))
627
628 if name == "" {
629 ctx.PropertyErrorf("symlinks", "Name can't be empty")
630 continue
631 }
632
633 if target == "" {
634 ctx.PropertyErrorf("symlinks", "Target can't be empty")
635 continue
636 }
637
638 // OutputPath.Join verifies name. don't need to verify target.
639 dst := rootDir.Join(ctx, name)
Cole Faust3b806d32024-03-11 15:15:03 -0700640 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 +0900641 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
642 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900643 f.appendToEntry(ctx, dst)
Inseob Kim14199b02021-02-09 21:18:31 +0900644 }
Jihoon Kang89e8a692024-12-18 19:28:33 +0000645
646 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=2835;drc=b186569ef00ff2f2a1fab28aedc75ebc32bcd67b
647 if f.partitionName() == "recovery" {
648 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, "root/linkerconfig").String())
649 builder.Command().Text("touch").Text(rootDir.Join(ctx, "root/linkerconfig/ld.config.txt").String())
650 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900651}
652
Inseob Kim33f95a92024-07-11 15:44:49 +0900653func (f *filesystem) copyPackagingSpecs(ctx android.ModuleContext, builder *android.RuleBuilder, specs map[string]android.PackagingSpec, rootDir, rebasedDir android.WritablePath) []string {
654 rootDirSpecs := make(map[string]android.PackagingSpec)
655 rebasedDirSpecs := make(map[string]android.PackagingSpec)
656
657 for rel, spec := range specs {
658 if spec.Partition() == "root" {
659 rootDirSpecs[rel] = spec
660 } else {
661 rebasedDirSpecs[rel] = spec
662 }
663 }
664
665 dirsToSpecs := make(map[android.WritablePath]map[string]android.PackagingSpec)
666 dirsToSpecs[rootDir] = rootDirSpecs
667 dirsToSpecs[rebasedDir] = rebasedDirSpecs
668
669 return f.CopySpecsToDirs(ctx, builder, dirsToSpecs)
670}
671
Spandan Das420e16a2024-12-11 18:10:52 +0000672func (f *filesystem) rootDirString() string {
673 return f.partitionName()
674}
675
Cole Faust62cfaeb2025-01-15 18:06:40 -0800676func (f *filesystem) buildImageUsingBuildImage(
677 ctx android.ModuleContext,
678 builder *android.RuleBuilder,
679 rootDir android.OutputPath,
680 rebasedDir android.OutputPath,
Cole Faust74ee4e02025-01-16 14:55:35 -0800681) (android.Path, android.Path, android.Path, android.Paths) {
Nikita Ioffe519015f2022-12-23 15:36:29 +0000682 // run host_init_verifier
683 // Ideally we should have a concept of pluggable linters that verify the generated image.
684 // While such concept is not implement this will do.
685 // TODO(b/263574231): substitute with pluggable linter.
686 builder.Command().
687 BuiltTool("host_init_verifier").
688 FlagWithArg("--out_system=", rootDir.String()+"/system")
689
Jiyong Park72678312021-01-18 17:29:49 +0900690 propFile, toolDeps := f.buildPropFile(ctx)
Cole Fauste1676122024-12-03 17:32:25 -0800691
692 // Most of the time, if build_image were to call a host tool, it accepts the path to the
693 // host tool in a field in the prop file. However, it doesn't have that option for fec, which
694 // it expects to just be on the PATH. Add fec to the PATH.
695 fec := ctx.Config().HostToolPath(ctx, "fec")
696 pathToolDirs := []string{filepath.Dir(fec.String())}
697
Cole Faust4e9f5922024-11-13 16:09:23 -0800698 output := android.PathForModuleOut(ctx, f.installFileName())
Spandan Das33c9c472025-01-14 19:26:23 +0000699 builder.Command().Text("touch").Output(f.getMapFile(ctx))
Cole Fauste1676122024-12-03 17:32:25 -0800700 builder.Command().
701 Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")).
702 BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900703 Text(rootDir.String()). // input directory
704 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900705 Implicits(toolDeps).
Cole Fauste1676122024-12-03 17:32:25 -0800706 Implicit(fec).
Jiyong Park11a65972021-02-01 21:09:38 +0900707 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900708 Text(rootDir.String()) // directory where to find fs_config_files|dirs
709
Spandan Das1f0a5a12025-01-15 00:53:15 +0000710 // Add an additional cmd to create a hermetic img file. This will contain pinned timestamps e.g.
711 propFilePinnedTimestamp := android.PathForModuleOut(ctx, "for_target_files", "prop")
712 builder.Command().Textf("cat").Input(propFile).Flag(">").Output(propFilePinnedTimestamp).Textf(" && echo use_fixed_timestamp=true >> %s", propFilePinnedTimestamp)
713
714 outputHermetic := android.PathForModuleOut(ctx, "for_target_files", f.installFileName())
715 builder.Command().
716 Textf("PATH=%s:$PATH", strings.Join(pathToolDirs, ":")).
717 BuiltTool("build_image").
718 Text(rootDir.String()). // input directory
719 Flag(propFilePinnedTimestamp.String()).
720 Implicits(toolDeps).
721 Implicit(fec).
722 Output(outputHermetic).
723 Text(rootDir.String()) // directory where to find fs_config_files|dirs
724
Jihoon Kang983dd882025-01-13 23:14:11 +0000725 if f.properties.Partition_size != nil {
726 assertMaxImageSize(builder, output, *f.properties.Partition_size, false)
727 }
728
Jiyong Park6f0f6882020-11-12 13:14:30 +0900729 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800730 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900731
Cole Faust74ee4e02025-01-16 14:55:35 -0800732 return output, outputHermetic, propFile, toolDeps
Jiyong Park65c49f52020-11-24 14:23:26 +0900733}
734
Cole Faust4e9f5922024-11-13 16:09:23 -0800735func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.Path {
Inseob Kimcc8e5362021-02-03 14:05:24 +0900736 builder := android.NewRuleBuilder(pctx, ctx)
737 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
738 builder.Command().BuiltTool("sefcontext_compile").
739 FlagWithOutput("-o ", fcBin).
740 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
741 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
Cole Faust4e9f5922024-11-13 16:09:23 -0800742 return fcBin
Inseob Kimcc8e5362021-02-03 14:05:24 +0900743}
744
Cole Faust4e9f5922024-11-13 16:09:23 -0800745func (f *filesystem) buildPropFile(ctx android.ModuleContext) (android.Path, android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +0900746 var deps android.Paths
Cole Faustcec230a2024-03-07 15:51:12 -0800747 var propFileString strings.Builder
Jiyong Park72678312021-01-18 17:29:49 +0900748 addStr := func(name string, value string) {
Cole Faustcec230a2024-03-07 15:51:12 -0800749 propFileString.WriteString(name)
750 propFileString.WriteRune('=')
751 propFileString.WriteString(value)
752 propFileString.WriteRune('\n')
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 }
Spandan Das33c9c472025-01-14 19:26:23 +0000772 addStr("block_list", f.getMapFile(ctx).String()) // This will be an implicit output
Jiyong Park11a65972021-02-01 21:09:38 +0900773
774 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim376d72f2023-11-01 15:40:25 +0900775 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +0900776 addStr("use_dynamic_partition_size", "true")
777 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
778 // b/177813163 deps of the host tools have to be added. Remove this.
779 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
780 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
781 }
782
Jiyong Park71baa762021-01-18 21:11:03 +0900783 if proptools.Bool(f.properties.Use_avb) {
784 addStr("avb_hashtree_enable", "true")
785 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
786 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
787 addStr("avb_algorithm", algorithm)
Cole Fauste1676122024-12-03 17:32:25 -0800788 if f.properties.Avb_private_key != nil {
789 key := android.PathForModuleSrc(ctx, *f.properties.Avb_private_key)
790 addPath("avb_key_path", key)
791 }
Inseob Kim53391842024-03-29 17:44:07 +0900792 addStr("partition_name", f.partitionName())
Cole Fauste1676122024-12-03 17:32:25 -0800793 avb_add_hashtree_footer_args := ""
794 if !proptools.BoolDefault(f.properties.Use_fec, true) {
795 avb_add_hashtree_footer_args += " --do_not_generate_fec"
796 }
Shikha Panware6f30632022-12-21 12:54:45 +0000797 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
798 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
799 }
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000800 if f.properties.Rollback_index != nil {
801 rollbackIndex := proptools.Int(f.properties.Rollback_index)
802 if rollbackIndex < 0 {
803 ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
804 }
805 avb_add_hashtree_footer_args += " --rollback_index " + strconv.Itoa(rollbackIndex)
806 }
Cole Fauste1676122024-12-03 17:32:25 -0800807 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 -0800808 // We're not going to add BuildFingerPrintFile as a dep. If it changed, it's likely because
809 // the build number changed, and we don't want to trigger rebuilds solely based on the build
810 // number.
Cole Fauste1676122024-12-03 17:32:25 -0800811 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 +0000812 if f.properties.Security_patch != nil && proptools.String(f.properties.Security_patch) != "" {
813 avb_add_hashtree_footer_args += fmt.Sprintf(" --prop com.android.build.%s.security_patch:%s", f.partitionName(), proptools.String(f.properties.Security_patch))
814 }
Shikha Panware6f30632022-12-21 12:54:45 +0000815 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jiyong Park71baa762021-01-18 21:11:03 +0900816 }
817
Cole Faust0d467052024-12-04 17:19:19 -0800818 if f.properties.File_contexts != nil && f.properties.Precompiled_file_contexts != nil {
819 ctx.ModuleErrorf("file_contexts and precompiled_file_contexts cannot both be set")
820 } else if f.properties.File_contexts != nil {
Inseob Kimcc8e5362021-02-03 14:05:24 +0900821 addPath("selinux_fc", f.buildFileContexts(ctx))
Cole Faust0d467052024-12-04 17:19:19 -0800822 } else if f.properties.Precompiled_file_contexts != nil {
823 src := android.PathForModuleSrc(ctx, *f.properties.Precompiled_file_contexts)
824 if src != nil {
825 addPath("selinux_fc", src)
826 }
Inseob Kimcc8e5362021-02-03 14:05:24 +0900827 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900828 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
829 addStr("timestamp", timestamp)
Spandan Dasa0ddc512025-01-06 20:23:55 +0000830 } else if ctx.Config().Getenv("USE_FIXED_TIMESTAMP_IMG_FILES") == "true" {
831 addStr("use_fixed_timestamp", "true")
Jooyung Han65f402b2022-04-21 14:24:04 +0900832 }
Spandan Dasa0ddc512025-01-06 20:23:55 +0000833
Jooyung Han65f402b2022-04-21 14:24:04 +0900834 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
835 addStr("uuid", uuid)
836 addStr("hash_seed", uuid)
837 }
mrziwang1a6291f2024-11-07 14:29:25 -0800838
Jihoon Kang40551e62025-01-14 21:55:08 +0000839 // Disable sparse only when partition size is not defined. disable_sparse has the same
840 // effect as <partition name>_disable_sparse.
841 if f.properties.Partition_size == nil {
842 addStr("disable_sparse", "true")
843 }
Cole Faust43a52c72024-11-26 12:46:08 -0800844
mrziwang1a6291f2024-11-07 14:29:25 -0800845 fst := f.fsType(ctx)
846 switch fst {
847 case erofsType:
848 // Add erofs properties
Cole Faust3e730972024-12-03 13:12:08 -0800849 addStr("erofs_default_compressor", proptools.StringDefault(f.properties.Erofs.Compressor, "lz4hc,9"))
850 if f.properties.Erofs.Compress_hints != nil {
851 src := android.PathForModuleSrc(ctx, *f.properties.Erofs.Compress_hints)
852 addPath("erofs_default_compress_hints", src)
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000853 }
854 if proptools.BoolDefault(f.properties.Erofs.Sparse, true) {
855 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2292;bpv=1;bpt=0;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b
856 addStr("erofs_sparse_flag", "-s")
857 }
mrziwang1a6291f2024-11-07 14:29:25 -0800858 case f2fsType:
859 if proptools.BoolDefault(f.properties.F2fs.Sparse, true) {
860 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2294;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b;bpv=1;bpt=0
861 addStr("f2fs_sparse_flag", "-S")
862 }
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000863 }
mrziwang1a6291f2024-11-07 14:29:25 -0800864 f.checkFsTypePropertyError(ctx, fst, fsTypeStr(fst))
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000865
Jihoon Kang983dd882025-01-13 23:14:11 +0000866 if f.properties.Partition_size != nil {
867 addStr("partition_size", strconv.FormatInt(*f.properties.Partition_size, 10))
868 }
869
Jihoon Kang6d08d922025-01-14 18:31:57 +0000870 if proptools.BoolDefault(f.properties.Support_casefolding, false) {
871 addStr("needs_casefold", "1")
872 }
873
874 if proptools.BoolDefault(f.properties.Support_project_quota, false) {
875 addStr("needs_projid", "1")
876 }
877
878 if proptools.BoolDefault(f.properties.Enable_compression, false) {
879 addStr("needs_compress", "1")
880 }
881
Cole Fauste1676122024-12-03 17:32:25 -0800882 propFilePreProcessing := android.PathForModuleOut(ctx, "prop_pre_processing")
883 android.WriteFileRuleVerbatim(ctx, propFilePreProcessing, propFileString.String())
Cole Faust4e9f5922024-11-13 16:09:23 -0800884 propFile := android.PathForModuleOut(ctx, "prop")
Cole Fauste1676122024-12-03 17:32:25 -0800885 ctx.Build(pctx, android.BuildParams{
Cole Faustefeb5c42024-12-16 10:47:26 -0800886 Rule: textFileProcessorRule,
887 Input: propFilePreProcessing,
888 Output: propFile,
Cole Fauste1676122024-12-03 17:32:25 -0800889 })
Jiyong Park72678312021-01-18 17:29:49 +0900890 return propFile, deps
891}
892
mrziwang1a6291f2024-11-07 14:29:25 -0800893// This method checks if there is any property set for the fstype(s) other than
894// the current fstype.
895func (f *filesystem) checkFsTypePropertyError(ctx android.ModuleContext, t fsType, fs string) {
896 raiseError := func(otherFsType, currentFsType string) {
897 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)
898 ctx.PropertyErrorf(otherFsType, errMsg)
899 }
900
901 if t != erofsType {
902 if f.properties.Erofs.Compressor != nil || f.properties.Erofs.Compress_hints != nil || f.properties.Erofs.Sparse != nil {
903 raiseError("erofs", fs)
904 }
905 }
906 if t != f2fsType {
907 if f.properties.F2fs.Sparse != nil {
908 raiseError("f2fs", fs)
909 }
910 }
911}
912
Jihoon Kang6da80752024-12-23 18:53:32 +0000913func includeFilesRootDir(ctx android.ModuleContext) (rootDirs android.Paths, partitions android.Paths) {
914 ctx.VisitDirectDepsWithTag(interPartitionInstallDependencyTag, func(m android.Module) {
915 if fsProvider, ok := android.OtherModuleProvider(ctx, m, FilesystemProvider); ok {
916 rootDirs = append(rootDirs, fsProvider.RootDir)
917 partitions = append(partitions, fsProvider.Output)
918 } else {
919 ctx.PropertyErrorf("include_files_of", "only filesystem modules can be listed in "+
920 "include_files_of but %s is not a filesystem module", m.Name())
921 }
922 })
923 return rootDirs, partitions
924}
925
Cole Faust62cfaeb2025-01-15 18:06:40 -0800926func (f *filesystem) buildCpioImage(
927 ctx android.ModuleContext,
928 builder *android.RuleBuilder,
929 rootDir android.OutputPath,
930 compressed bool,
931) android.Path {
Jiyong Park11a65972021-02-01 21:09:38 +0900932 if proptools.Bool(f.properties.Use_avb) {
933 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
934 "Consider adding this to bootimg module and signing the entire boot image.")
935 }
936
Inseob Kimcc8e5362021-02-03 14:05:24 +0900937 if proptools.String(f.properties.File_contexts) != "" {
938 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
939 }
940
Jihoon Kang6da80752024-12-23 18:53:32 +0000941 rootDirs, partitions := includeFilesRootDir(ctx)
942
Cole Faust4e9f5922024-11-13 16:09:23 -0800943 output := android.PathForModuleOut(ctx, f.installFileName())
Jiyong Park837cdb22021-02-05 00:17:14 +0900944 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900945 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900946 Text(rootDir.String()) // input directory
Jihoon Kang6da80752024-12-23 18:53:32 +0000947
948 for i := range len(rootDirs) {
949 cmd.Text(rootDirs[i].String())
950 }
951 cmd.Implicits(partitions)
952
Jihoon Kang6c03c8e2024-11-18 21:30:22 +0000953 if nodeList := f.properties.Dev_nodes_description_file; nodeList != nil {
954 cmd.FlagWithInput("-n ", android.PathForModuleSrc(ctx, proptools.String(nodeList)))
955 }
Jiyong Park837cdb22021-02-05 00:17:14 +0900956 if compressed {
957 cmd.Text("|").
958 BuiltTool("lz4").
959 Flag("--favor-decSpeed"). // for faster boot
960 Flag("-12"). // maximum compression level
961 Flag("-l"). // legacy format for kernel
962 Text(">").Output(output)
963 } else {
964 cmd.Text(">").Output(output)
965 }
Jiyong Park11a65972021-02-01 21:09:38 +0900966
967 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900968 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900969
Cole Faust62cfaeb2025-01-15 18:06:40 -0800970 return output
Jiyong Park11a65972021-02-01 21:09:38 +0900971}
972
Cole Faust4a2a7c92024-03-12 12:44:40 -0700973var validPartitions = []string{
974 "system",
975 "userdata",
976 "cache",
977 "system_other",
978 "vendor",
979 "product",
980 "system_ext",
981 "odm",
982 "vendor_dlkm",
983 "odm_dlkm",
984 "system_dlkm",
Cole Faust76a6e952024-11-07 16:56:45 -0800985 "ramdisk",
Cole Faust24938e22024-11-18 14:01:58 -0800986 "vendor_ramdisk",
Jihoon Kang3216c982024-12-02 19:42:20 +0000987 "recovery",
Cole Faust4a2a7c92024-03-12 12:44:40 -0700988}
989
Inseob Kimb7b84572024-04-30 10:51:47 +0900990func (f *filesystem) buildEventLogtagsFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
991 if !proptools.Bool(f.properties.Build_logtags) {
992 return
993 }
994
Inseob Kimb7b84572024-04-30 10:51:47 +0900995 etcPath := rebasedDir.Join(ctx, "etc")
996 eventLogtagsPath := etcPath.Join(ctx, "event-log-tags")
997 builder.Command().Text("mkdir").Flag("-p").Text(etcPath.String())
Cole Fauste4506af2024-12-11 14:14:50 -0800998 builder.Command().Text("cp").Input(android.MergedLogtagsPath(ctx)).Text(eventLogtagsPath.String())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900999
1000 f.appendToEntry(ctx, eventLogtagsPath)
Inseob Kimb7b84572024-04-30 10:51:47 +09001001}
1002
Kiyoung Kim67118212024-11-07 13:23:44 +09001003func (f *filesystem) BuildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
Spandan Das2047a4c2024-11-11 21:24:58 +00001004 if !proptools.Bool(f.properties.Linker_config.Gen_linker_config) {
Spandan Das92631882024-10-28 22:49:38 +00001005 return
1006 }
1007
Spandan Das918191e2024-10-31 18:27:23 +00001008 provideModules, _ := f.getLibsForLinkerConfig(ctx)
Cole Faustfee27012024-12-13 14:10:31 -08001009 intermediateOutput := android.PathForModuleOut(ctx, "linker.config.pb")
1010 linkerconfig.BuildLinkerConfig(ctx, android.PathsForModuleSrc(ctx, f.properties.Linker_config.Linker_config_srcs), provideModules, nil, intermediateOutput)
Spandan Das92631882024-10-28 22:49:38 +00001011 output := rebasedDir.Join(ctx, "etc", "linker.config.pb")
Cole Faustfee27012024-12-13 14:10:31 -08001012 builder.Command().Text("cp").Input(intermediateOutput).Output(output)
Spandan Das92631882024-10-28 22:49:38 +00001013
1014 f.appendToEntry(ctx, output)
1015}
1016
Kiyoung Kim23be5bb2024-11-27 00:50:30 +00001017func (f *filesystem) ShouldUseVintfFragmentModuleOnly() bool {
1018 return false
1019}
1020
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001021type partition interface {
1022 PartitionType() string
1023}
1024
Cole Faust9a24d902024-03-18 15:38:12 -07001025func (f *filesystem) PartitionType() string {
1026 return proptools.StringDefault(f.properties.Partition_type, "system")
1027}
1028
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001029var _ partition = (*filesystem)(nil)
1030
Jiyong Park65c49f52020-11-24 14:23:26 +09001031var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
1032
1033// Implements android.AndroidMkEntriesProvider
1034func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
1035 return []android.AndroidMkEntries{android.AndroidMkEntries{
1036 Class: "ETC",
1037 OutputFile: android.OptionalPathForPath(f.output),
1038 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -07001039 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -08001040 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +09001041 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
Kiyoung Kim99a954d2024-06-21 14:22:20 +09001042 entries.SetString("LOCAL_FILESYSTEM_FILELIST", f.fileListFile.String())
Jiyong Park65c49f52020-11-24 14:23:26 +09001043 },
1044 },
1045 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +09001046}
Jiyong Park12a719c2021-01-07 15:31:24 +09001047
1048// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
1049// package to have access to the output file.
1050type Filesystem interface {
1051 android.Module
1052 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +09001053
1054 // Returns the output file that is signed by avbtool. If this module is not signed, returns
1055 // nil.
1056 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +09001057}
1058
1059var _ Filesystem = (*filesystem)(nil)
1060
1061func (f *filesystem) OutputPath() android.Path {
1062 return f.output
1063}
Jiyong Park972e06c2021-03-15 23:32:49 +09001064
1065func (f *filesystem) SignedOutputPath() android.Path {
1066 if proptools.Bool(f.properties.Use_avb) {
1067 return f.OutputPath()
1068 }
1069 return nil
1070}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +09001071
1072// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
1073// Note that "apex" module installs its contents to "apex"(fake partition) as well
1074// for symbol lookup by imitating "activated" paths.
1075func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Cole Faustb8e280f2025-01-16 16:33:26 -08001076 return f.PackagingBase.GatherPackagingSpecsWithFilterAndModifier(ctx, f.filesystemBuilder.FilterPackagingSpec, f.filesystemBuilder.ModifyPackagingSpec)
1077}
1078
1079// Dexpreopt files are installed to system_other. Collect the packaingSpecs for the dexpreopt files
1080// from this partition to export to the system_other partition later.
1081func (f *filesystem) systemOtherFiles(ctx android.ModuleContext) map[string]android.PackagingSpec {
1082 filter := func(spec android.PackagingSpec) bool {
1083 // For some reason system_other packaging specs don't set the partition field.
1084 return strings.HasPrefix(spec.RelPathInPackage(), "system_other/")
1085 }
1086 modifier := func(spec *android.PackagingSpec) {
1087 spec.SetRelPathInPackage(strings.TrimPrefix(spec.RelPathInPackage(), "system_other/"))
1088 spec.SetPartition("system_other")
1089 }
1090 return f.PackagingBase.GatherPackagingSpecsWithFilterAndModifier(ctx, filter, modifier)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +09001091}
Jooyung Han65f402b2022-04-21 14:24:04 +09001092
1093func sha1sum(values []string) string {
1094 h := sha256.New()
1095 for _, value := range values {
1096 io.WriteString(h, value)
1097 }
1098 return fmt.Sprintf("%x", h.Sum(nil))
1099}
Jooyung Hane6067592023-03-16 13:11:17 +09001100
1101// Base cc.UseCoverage
1102
1103var _ cc.UseCoverage = (*filesystem)(nil)
1104
Colin Crosse1a85552024-06-14 12:17:37 -07001105func (*filesystem) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +09001106 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
1107}
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001108
1109// android_filesystem_defaults
1110
1111type filesystemDefaults struct {
1112 android.ModuleBase
1113 android.DefaultsModuleBase
1114
Inseob Kim3c0a0422024-11-05 17:21:37 +09001115 properties FilesystemProperties
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001116}
1117
1118// android_filesystem_defaults is a default module for android_filesystem and android_system_image
1119func filesystemDefaultsFactory() android.Module {
1120 module := &filesystemDefaults{}
1121 module.AddProperties(&module.properties)
1122 module.AddProperties(&android.PackagingProperties{})
1123 android.InitDefaultsModule(module)
1124 return module
1125}
1126
1127func (f *filesystemDefaults) PartitionType() string {
1128 return proptools.StringDefault(f.properties.Partition_type, "system")
1129}
1130
1131var _ partition = (*filesystemDefaults)(nil)
1132
1133func (f *filesystemDefaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1134 validatePartitionType(ctx, f)
Yu Liufc8d5c12025-01-09 00:19:06 +00001135 android.SetProvider(ctx, FilesystemDefaultsInfoProvider, FilesystemDefaultsInfo{
1136 PartitionType: f.PartitionType(),
1137 })
Jiyong Parkf46b1af2024-04-05 18:13:33 +09001138}
Spandan Das918191e2024-10-31 18:27:23 +00001139
1140// getLibsForLinkerConfig returns
1141// 1. A list of libraries installed in this filesystem
1142// 2. A list of dep libraries _not_ installed in this filesystem
1143//
1144// `linkerconfig.BuildLinkerConfig` will convert these two to a linker.config.pb for the filesystem
1145// (1) will be added to --provideLibs if they are C libraries with a stable interface (has stubs)
1146// (2) will be added to --requireLibs if they are C libraries with a stable interface (has stubs)
Yu Liu68a70b72025-01-08 22:54:44 +00001147func (f *filesystem) getLibsForLinkerConfig(ctx android.ModuleContext) ([]android.ModuleProxy, []android.ModuleProxy) {
Spandan Das918191e2024-10-31 18:27:23 +00001148 // we need "Module"s for packaging items
Yu Liu68a70b72025-01-08 22:54:44 +00001149 modulesInPackageByModule := make(map[android.ModuleProxy]bool)
Spandan Das918191e2024-10-31 18:27:23 +00001150 modulesInPackageByName := make(map[string]bool)
1151
1152 deps := f.gatherFilteredPackagingSpecs(ctx)
Yu Liu68a70b72025-01-08 22:54:44 +00001153 ctx.WalkDepsProxy(func(child, parent android.ModuleProxy) bool {
1154 if !android.OtherModuleProviderOrDefault(ctx, child, android.CommonModuleInfoKey).Enabled {
Yu Liu9c6b6762025-01-08 22:04:35 +00001155 return false
1156 }
Spandan Das918191e2024-10-31 18:27:23 +00001157 for _, ps := range android.OtherModuleProviderOrDefault(
1158 ctx, child, android.InstallFilesProvider).PackagingSpecs {
Spandan Dasecf667f2024-12-05 00:58:56 +00001159 if _, ok := deps[ps.RelPathInPackage()]; ok && ps.Partition() == f.PartitionType() {
Spandan Das918191e2024-10-31 18:27:23 +00001160 modulesInPackageByModule[child] = true
1161 modulesInPackageByName[child.Name()] = true
1162 return true
1163 }
1164 }
1165 return true
1166 })
1167
Yu Liu68a70b72025-01-08 22:54:44 +00001168 provideModules := make([]android.ModuleProxy, 0, len(modulesInPackageByModule))
Spandan Das918191e2024-10-31 18:27:23 +00001169 for mod := range modulesInPackageByModule {
1170 provideModules = append(provideModules, mod)
1171 }
1172
Yu Liu68a70b72025-01-08 22:54:44 +00001173 var requireModules []android.ModuleProxy
1174 ctx.WalkDepsProxy(func(child, parent android.ModuleProxy) bool {
1175 if !android.OtherModuleProviderOrDefault(ctx, child, android.CommonModuleInfoKey).Enabled {
Yu Liu9c6b6762025-01-08 22:04:35 +00001176 return false
1177 }
Spandan Das918191e2024-10-31 18:27:23 +00001178 _, parentInPackage := modulesInPackageByModule[parent]
1179 _, childInPackageName := modulesInPackageByName[child.Name()]
1180
1181 // When parent is in the package, and child (or its variant) is not, this can be from an interface.
1182 if parentInPackage && !childInPackageName {
1183 requireModules = append(requireModules, child)
1184 }
1185 return true
1186 })
1187
1188 return provideModules, requireModules
1189}
Cole Faust26bdac52024-11-19 13:37:53 -08001190
1191// Checks that the given file doesn't exceed the given size, and will also print a warning
1192// if it's nearing the maximum size. Equivalent to assert-max-image-size in make:
1193// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/definitions.mk;l=3455;drc=993c4de29a02a6accd60ceaaee153307e1a18d10
1194func assertMaxImageSize(builder *android.RuleBuilder, image android.Path, maxSize int64, addAvbLater bool) {
1195 if addAvbLater {
1196 // The value 69632 is derived from MAX_VBMETA_SIZE + MAX_FOOTER_SIZE in avbtool.
1197 // Logic copied from make:
1198 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=228;drc=a6a0007ef24e16c0b79f439beac4a118416717e6
1199 maxSize -= 69632
1200 }
1201 cmd := builder.Command()
1202 cmd.Textf(`file="%s"; maxsize="%d";`+
1203 `total=$(stat -c "%%s" "$file" | tr -d '\n');`+
1204 `if [ "$total" -gt "$maxsize" ]; then `+
1205 ` echo "error: $file too large ($total > $maxsize)";`+
1206 ` false;`+
1207 `elif [ "$total" -gt $((maxsize - 32768)) ]; then `+
1208 ` echo "WARNING: $file approaching size limit ($total now; limit $maxsize)";`+
1209 `fi`,
1210 image, maxSize)
1211 cmd.Implicit(image)
1212}
Spandan Das71be42d2024-11-20 18:34:16 +00001213
1214// addAutogeneratedRroDeps walks the transitive closure of vendor and product partitions.
1215// It visits apps installed in system and system_ext partitions, and adds the autogenerated
1216// RRO modules to its own deps.
1217func addAutogeneratedRroDeps(ctx android.BottomUpMutatorContext) {
1218 f, ok := ctx.Module().(*filesystem)
1219 if !ok {
1220 return
1221 }
1222 thisPartition := f.PartitionType()
1223 if thisPartition != "vendor" && thisPartition != "product" {
Cole Faust34592c02024-12-13 11:20:24 -08001224 if f.properties.Android_filesystem_deps.System != nil {
1225 ctx.PropertyErrorf("android_filesystem_deps.system", "only vendor or product partitions can use android_filesystem_deps")
1226 }
1227 if f.properties.Android_filesystem_deps.System_ext != nil {
1228 ctx.PropertyErrorf("android_filesystem_deps.system_ext", "only vendor or product partitions can use android_filesystem_deps")
1229 }
Spandan Das71be42d2024-11-20 18:34:16 +00001230 return
1231 }
1232 ctx.WalkDeps(func(child, parent android.Module) bool {
1233 depTag := ctx.OtherModuleDependencyTag(child)
1234 if parent.Name() == f.Name() && depTag != interPartitionDependencyTag {
1235 return false // This is a module listed in deps of vendor/product filesystem
1236 }
1237 if vendorOverlay := java.AutogeneratedRroModuleName(ctx, child.Name(), "vendor"); ctx.OtherModuleExists(vendorOverlay) && thisPartition == "vendor" {
1238 ctx.AddFarVariationDependencies(nil, dependencyTagWithVisibilityEnforcementBypass, vendorOverlay)
1239 }
1240 if productOverlay := java.AutogeneratedRroModuleName(ctx, child.Name(), "product"); ctx.OtherModuleExists(productOverlay) && thisPartition == "product" {
1241 ctx.AddFarVariationDependencies(nil, dependencyTagWithVisibilityEnforcementBypass, productOverlay)
1242 }
1243 return true
1244 })
1245}