blob: e84139b9a8d154976ecc38061998a3dacdc340dc [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 Das92631882024-10-28 22:49:38 +000028 "android/soong/linkerconfig"
Jiyong Park65b62242020-11-25 12:44:59 +090029
30 "github.com/google/blueprint"
Jiyong Park71baa762021-01-18 21:11:03 +090031 "github.com/google/blueprint/proptools"
Jiyong Park6f0f6882020-11-12 13:14:30 +090032)
33
34func init() {
Jooyung Han9706cbc2021-04-15 22:43:48 +090035 registerBuildComponents(android.InitRegistrationContext)
36}
37
38func registerBuildComponents(ctx android.RegistrationContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -070039 ctx.RegisterModuleType("android_filesystem", FilesystemFactory)
Jiyong Parkf46b1af2024-04-05 18:13:33 +090040 ctx.RegisterModuleType("android_filesystem_defaults", filesystemDefaultsFactory)
Jihoon Kang98047cf2024-10-02 17:13:54 +000041 ctx.RegisterModuleType("android_system_image", SystemImageFactory)
Jiyong Parkbc485482022-11-15 22:31:49 +090042 ctx.RegisterModuleType("avb_add_hash_footer", avbAddHashFooterFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090043 ctx.RegisterModuleType("avb_add_hash_footer_defaults", avbAddHashFooterDefaultsFactory)
Alice Wang000e3a32023-01-03 16:11:20 +000044 ctx.RegisterModuleType("avb_gen_vbmeta_image", avbGenVbmetaImageFactory)
Inseob Kim87230e62023-11-22 18:55:07 +090045 ctx.RegisterModuleType("avb_gen_vbmeta_image_defaults", avbGenVbmetaImageDefaultsFactory)
Jiyong Park6f0f6882020-11-12 13:14:30 +090046}
47
48type filesystem struct {
49 android.ModuleBase
50 android.PackagingBase
Jiyong Parkf46b1af2024-04-05 18:13:33 +090051 android.DefaultableModuleBase
Jiyong Park65c49f52020-11-24 14:23:26 +090052
Jihoon Kang98047cf2024-10-02 17:13:54 +000053 properties FilesystemProperties
Jiyong Park71baa762021-01-18 21:11:03 +090054
Jiyong Parkfa616132021-04-20 11:36:40 +090055 // Function that builds extra files under the root directory and returns the files
56 buildExtraFiles func(ctx android.ModuleContext, root android.OutputPath) android.OutputPaths
57
Jeongik Cha54bf8752024-02-08 10:44:37 +090058 // Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs()
59 filterPackagingSpec func(spec android.PackagingSpec) bool
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090060
Jiyong Park65c49f52020-11-24 14:23:26 +090061 output android.OutputPath
62 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090063
Kiyoung Kim99a954d2024-06-21 14:22:20 +090064 fileListFile android.OutputPath
65
66 // Keeps the entries installed from this filesystem
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090067 entries []string
Jiyong Park6f0f6882020-11-12 13:14:30 +090068}
69
Spandan Das69464c32024-10-25 20:08:06 +000070type SymlinkDefinition struct {
Inseob Kim14199b02021-02-09 21:18:31 +090071 Target *string
72 Name *string
73}
74
Jihoon Kang98047cf2024-10-02 17:13:54 +000075type FilesystemProperties struct {
Jiyong Park71baa762021-01-18 21:11:03 +090076 // When set to true, sign the image with avbtool. Default is false.
77 Use_avb *bool
78
79 // Path to the private key that avbtool will use to sign this filesystem image.
80 // TODO(jiyong): allow apex_key to be specified here
81 Avb_private_key *string `android:"path"`
82
Shikha Panwar01403bb2022-12-22 12:22:57 +000083 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +090084 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090085
Shikha Panwar01403bb2022-12-22 12:22:57 +000086 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
87 // avbtool. Default used by avbtool is sha1.
Shikha Panware6f30632022-12-21 12:54:45 +000088 Avb_hash_algorithm *string
89
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +000090 // The index used to prevent rollback of the image. Only used if use_avb is true.
91 Rollback_index *int64
92
Jiyong Parkac4076d2021-03-15 23:21:30 +090093 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
94 Partition_name *string
95
Spandan Dasc35d6fb2024-10-10 17:51:14 +000096 // Type of the filesystem. Currently, ext4, erofs, cpio, and compressed_cpio are supported. Default
Jiyong Park837cdb22021-02-05 00:17:14 +090097 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +090098 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +090099
Cole Faust9a24d902024-03-18 15:38:12 -0700100 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
101 // checks, and will be used in the future for API surface checks.
102 Partition_type *string
103
Inseob Kimcc8e5362021-02-03 14:05:24 +0900104 // file_contexts file to make image. Currently, only ext4 is supported.
105 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900106
107 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
108 // (root).
109 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +0900110
111 // Directories to be created under root. e.g. /dev, /proc, etc.
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700112 Dirs proptools.Configurable[[]string]
Inseob Kim14199b02021-02-09 21:18:31 +0900113
114 // Symbolic links to be created under root with "ln -sf <target> <name>".
Spandan Das69464c32024-10-25 20:08:06 +0000115 Symlinks []SymlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900116
117 // Seconds since unix epoch to override timestamps of file entries
118 Fake_timestamp *string
119
120 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
121 // Otherwise, they'll be set as random which might cause indeterministic build output.
122 Uuid *string
Inseob Kim376d72f2023-11-01 15:40:25 +0900123
124 // Mount point for this image. Default is "/"
125 Mount_point *string
Cole Faust4a2a7c92024-03-12 12:44:40 -0700126
127 // If set to the name of a partition ("system", "vendor", etc), this filesystem module
128 // will also include the contents of the make-built staging directories. If any soong
129 // modules would be installed to the same location as a make module, they will overwrite
130 // the make version.
131 Include_make_built_files string
Inseob Kim53391842024-03-29 17:44:07 +0900132
Inseob Kimb7b84572024-04-30 10:51:47 +0900133 // When set, builds etc/event-log-tags file by merging logtags from all dependencies.
134 // Default is false
135 Build_logtags *bool
136
Justin Yun74f3f302024-05-07 14:32:14 +0900137 // Install aconfig_flags.pb file for the modules installed in this partition.
138 Gen_aconfig_flags_pb *bool
139
Inseob Kim53391842024-03-29 17:44:07 +0900140 Fsverity fsverityProperties
Cole Faust92ccbe22024-10-03 14:38:37 -0700141
142 // If this property is set to true, the filesystem will call ctx.UncheckedModule(), causing
143 // it to not be built on checkbuilds. Used for the automatic migration from make to soong
144 // build modules, where we want to emit some not-yet-working filesystems and we don't want them
145 // to be built.
146 Unchecked_module *bool `blueprint:"mutated"`
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000147
148 Erofs ErofsProperties
Jihoon Kang0d545b82024-10-11 00:21:57 +0000149
mrziwang1a6291f2024-11-07 14:29:25 -0800150 F2fs F2fsProperties
151
Spandan Das173256b2024-10-31 19:59:30 +0000152 Linkerconfig LinkerConfigProperties
Spandan Das92631882024-10-28 22:49:38 +0000153
Jihoon Kang0d545b82024-10-11 00:21:57 +0000154 // Determines if the module is auto-generated from Soong or not. If the module is
155 // auto-generated, its deps are exempted from visibility enforcement.
156 Is_auto_generated *bool
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000157}
158
159// Additional properties required to generate erofs FS partitions.
160type ErofsProperties struct {
161 // Compressor and Compression level passed to mkfs.erofs. e.g. (lz4hc,9)
162 // Please see external/erofs-utils/README for complete documentation.
163 Compressor *string
164
165 // Used as --compress-hints for mkfs.erofs
166 Compress_hints *string `android:"path"`
167
168 Sparse *bool
Jiyong Park71baa762021-01-18 21:11:03 +0900169}
170
mrziwang1a6291f2024-11-07 14:29:25 -0800171// Additional properties required to generate f2fs FS partitions.
172type F2fsProperties struct {
173 Sparse *bool
174}
175
Spandan Das173256b2024-10-31 19:59:30 +0000176type LinkerConfigProperties struct {
177
178 // Build a linker.config.pb file
179 Gen_linker_config *bool
180
181 // List of files (in .json format) that will be converted to a linker config file (in .pb format).
182 // The linker config file be installed in the filesystem at /etc/linker.config.pb
183 Linker_config_srcs []string `android:"path"`
184}
185
Jiyong Park65c49f52020-11-24 14:23:26 +0900186// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
187// image. The filesystem images are expected to be mounted in the target device, which means the
188// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
189// The modules are placed in the filesystem image just like they are installed to the ordinary
190// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Cole Faust92ccbe22024-10-03 14:38:37 -0700191func FilesystemFactory() android.Module {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900192 module := &filesystem{}
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000193 module.filterPackagingSpec = module.filterInstallablePackagingSpec
Cole Faust2cfe6962024-09-17 11:31:14 -0700194 initFilesystemModule(module, module)
Jiyong Parkfa616132021-04-20 11:36:40 +0900195 return module
196}
197
Cole Faust2cfe6962024-09-17 11:31:14 -0700198func initFilesystemModule(module android.DefaultableModule, filesystemModule *filesystem) {
199 module.AddProperties(&filesystemModule.properties)
200 android.InitPackageModule(filesystemModule)
201 filesystemModule.PackagingBase.DepsCollectFirstTargetOnly = true
Jihoon Kang79196c52024-10-30 18:49:47 +0000202 filesystemModule.PackagingBase.AllowHighPriorityDeps = true
Jiyong Park6f0f6882020-11-12 13:14:30 +0900203 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900204 android.InitDefaultableModule(module)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900205}
206
Jihoon Kang0d545b82024-10-11 00:21:57 +0000207type depTag struct {
Jiyong Park12a719c2021-01-07 15:31:24 +0900208 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900209 android.PackagingItemAlwaysDepTag
Jihoon Kang0d545b82024-10-11 00:21:57 +0000210}
211
212var dependencyTag = depTag{}
213
214type depTagWithVisibilityEnforcementBypass struct {
215 depTag
216}
217
218var _ android.ExcludeFromVisibilityEnforcementTag = (*depTagWithVisibilityEnforcementBypass)(nil)
219
220func (t depTagWithVisibilityEnforcementBypass) ExcludeFromVisibilityEnforcement() {}
221
222var dependencyTagWithVisibilityEnforcementBypass = depTagWithVisibilityEnforcementBypass{}
Jiyong Park65b62242020-11-25 12:44:59 +0900223
Jiyong Park6f0f6882020-11-12 13:14:30 +0900224func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000225 if proptools.Bool(f.properties.Is_auto_generated) {
226 f.AddDeps(ctx, dependencyTagWithVisibilityEnforcementBypass)
227 } else {
228 f.AddDeps(ctx, dependencyTag)
229 }
Jiyong Park6f0f6882020-11-12 13:14:30 +0900230}
231
Jiyong Park11a65972021-02-01 21:09:38 +0900232type fsType int
233
234const (
235 ext4Type fsType = iota
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000236 erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800237 f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900238 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900239 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900240 unknown
241)
242
Spandan Das7a46f6c2024-10-14 18:41:18 +0000243func (fs fsType) IsUnknown() bool {
244 return fs == unknown
245}
246
Cole Faust92ccbe22024-10-03 14:38:37 -0700247type FilesystemInfo struct {
248 // A text file containing the list of paths installed on the partition.
249 FileListFile android.Path
250}
251
252var FilesystemProvider = blueprint.NewProvider[FilesystemInfo]()
253
Spandan Das7a46f6c2024-10-14 18:41:18 +0000254func GetFsTypeFromString(ctx android.EarlyModuleContext, typeStr string) fsType {
Jiyong Park11a65972021-02-01 21:09:38 +0900255 switch typeStr {
256 case "ext4":
257 return ext4Type
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000258 case "erofs":
259 return erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800260 case "f2fs":
261 return f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900262 case "compressed_cpio":
263 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900264 case "cpio":
265 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900266 default:
Jiyong Park11a65972021-02-01 21:09:38 +0900267 return unknown
268 }
269}
270
Spandan Das7a46f6c2024-10-14 18:41:18 +0000271func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
272 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
273 fsType := GetFsTypeFromString(ctx, typeStr)
274 if fsType == unknown {
275 ctx.PropertyErrorf("type", "%q not supported", typeStr)
276 }
277 return fsType
278}
279
Jiyong Park65c49f52020-11-24 14:23:26 +0900280func (f *filesystem) installFileName() string {
281 return f.BaseModuleName() + ".img"
282}
283
Inseob Kim53391842024-03-29 17:44:07 +0900284func (f *filesystem) partitionName() string {
285 return proptools.StringDefault(f.properties.Partition_name, f.Name())
286}
287
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000288func (f *filesystem) filterInstallablePackagingSpec(ps android.PackagingSpec) bool {
289 // Filesystem module respects the installation semantic. A PackagingSpec from a module with
290 // IsSkipInstall() is skipped.
Spandan Das6d056502024-10-21 15:40:32 +0000291 if proptools.Bool(f.properties.Is_auto_generated) { // TODO (spandandas): Remove this.
292 return !ps.SkipInstall() && (ps.Partition() == f.PartitionType())
293 }
294 return !ps.SkipInstall()
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000295}
296
Jiyong Park6f0f6882020-11-12 13:14:30 +0900297var pctx = android.NewPackageContext("android/soong/filesystem")
298
299func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900300 validatePartitionType(ctx, f)
Jiyong Park11a65972021-02-01 21:09:38 +0900301 switch f.fsType(ctx) {
mrziwang1a6291f2024-11-07 14:29:25 -0800302 case ext4Type, erofsType, f2fsType:
Jiyong Park11a65972021-02-01 21:09:38 +0900303 f.output = f.buildImageUsingBuildImage(ctx)
304 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900305 f.output = f.buildCpioImage(ctx, true)
306 case cpioType:
307 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900308 default:
309 return
310 }
311
312 f.installDir = android.PathForModuleInstall(ctx, "etc")
313 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
mrziwang555d1332024-06-07 11:15:33 -0700314 ctx.SetOutputFiles([]android.Path{f.output}, "")
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900315
316 f.fileListFile = android.PathForModuleOut(ctx, "fileList").OutputPath
317 android.WriteFileRule(ctx, f.fileListFile, f.installedFilesList())
Cole Faust92ccbe22024-10-03 14:38:37 -0700318
319 android.SetProvider(ctx, FilesystemProvider, FilesystemInfo{
320 FileListFile: f.fileListFile,
321 })
322
323 if proptools.Bool(f.properties.Unchecked_module) {
324 ctx.UncheckedModule()
325 }
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900326}
327
328func (f *filesystem) appendToEntry(ctx android.ModuleContext, installedFile android.OutputPath) {
329 partitionBaseDir := android.PathForModuleOut(ctx, "root", f.partitionName()).String() + "/"
330
331 relPath, inTargetPartition := strings.CutPrefix(installedFile.String(), partitionBaseDir)
332 if inTargetPartition {
333 f.entries = append(f.entries, relPath)
334 }
335}
336
337func (f *filesystem) installedFilesList() string {
338 installedFilePaths := android.FirstUniqueStrings(f.entries)
339 slices.Sort(installedFilePaths)
340
341 return strings.Join(installedFilePaths, "\n")
Jiyong Park11a65972021-02-01 21:09:38 +0900342}
343
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900344func validatePartitionType(ctx android.ModuleContext, p partition) {
345 if !android.InList(p.PartitionType(), validPartitions) {
346 ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, p.PartitionType())
347 }
348
349 ctx.VisitDirectDepsWithTag(android.DefaultsDepTag, func(m android.Module) {
350 if fdm, ok := m.(*filesystemDefaults); ok {
351 if p.PartitionType() != fdm.PartitionType() {
352 ctx.PropertyErrorf("partition_type",
353 "%s doesn't match with the partition type %s of the filesystem default module %s",
354 p.PartitionType(), fdm.PartitionType(), m.Name())
355 }
356 }
357 })
358}
359
Cole Faust3b806d32024-03-11 15:15:03 -0700360// Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files
361// already in `rootDir`.
362func (f *filesystem) buildNonDepsFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.OutputPath) {
Inseob Kim14199b02021-02-09 21:18:31 +0900363 // create dirs and symlinks
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700364 for _, dir := range f.properties.Dirs.GetOrDefault(ctx, nil) {
Inseob Kim14199b02021-02-09 21:18:31 +0900365 // OutputPath.Join verifies dir
366 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
367 }
368
369 for _, symlink := range f.properties.Symlinks {
370 name := strings.TrimSpace(proptools.String(symlink.Name))
371 target := strings.TrimSpace(proptools.String(symlink.Target))
372
373 if name == "" {
374 ctx.PropertyErrorf("symlinks", "Name can't be empty")
375 continue
376 }
377
378 if target == "" {
379 ctx.PropertyErrorf("symlinks", "Target can't be empty")
380 continue
381 }
382
383 // OutputPath.Join verifies name. don't need to verify target.
384 dst := rootDir.Join(ctx, name)
Cole Faust3b806d32024-03-11 15:15:03 -0700385 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 +0900386 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
387 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900388 f.appendToEntry(ctx, dst)
Inseob Kim14199b02021-02-09 21:18:31 +0900389 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900390
Jiyong Parkfa616132021-04-20 11:36:40 +0900391 // create extra files if there's any
Jiyong Parkfa616132021-04-20 11:36:40 +0900392 if f.buildExtraFiles != nil {
Cole Faust4a2a7c92024-03-12 12:44:40 -0700393 rootForExtraFiles := android.PathForModuleGen(ctx, "root-extra").OutputPath
394 extraFiles := f.buildExtraFiles(ctx, rootForExtraFiles)
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900395 for _, extraFile := range extraFiles {
396 rel, err := filepath.Rel(rootForExtraFiles.String(), extraFile.String())
Cole Faust4a2a7c92024-03-12 12:44:40 -0700397 if err != nil || strings.HasPrefix(rel, "..") {
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900398 ctx.ModuleErrorf("can't make %q relative to %q", extraFile, rootForExtraFiles)
Cole Faust4a2a7c92024-03-12 12:44:40 -0700399 }
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900400 f.appendToEntry(ctx, rootDir.Join(ctx, rel))
Jiyong Parkfa616132021-04-20 11:36:40 +0900401 }
Cole Faust4a2a7c92024-03-12 12:44:40 -0700402 if len(extraFiles) > 0 {
403 builder.Command().BuiltTool("merge_directories").
404 Implicits(extraFiles.Paths()).
405 Text(rootDir.String()).
406 Text(rootForExtraFiles.String())
407 }
Jiyong Parkfa616132021-04-20 11:36:40 +0900408 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900409}
410
Inseob Kim33f95a92024-07-11 15:44:49 +0900411func (f *filesystem) copyPackagingSpecs(ctx android.ModuleContext, builder *android.RuleBuilder, specs map[string]android.PackagingSpec, rootDir, rebasedDir android.WritablePath) []string {
412 rootDirSpecs := make(map[string]android.PackagingSpec)
413 rebasedDirSpecs := make(map[string]android.PackagingSpec)
414
415 for rel, spec := range specs {
416 if spec.Partition() == "root" {
417 rootDirSpecs[rel] = spec
418 } else {
419 rebasedDirSpecs[rel] = spec
420 }
421 }
422
423 dirsToSpecs := make(map[android.WritablePath]map[string]android.PackagingSpec)
424 dirsToSpecs[rootDir] = rootDirSpecs
425 dirsToSpecs[rebasedDir] = rebasedDirSpecs
426
427 return f.CopySpecsToDirs(ctx, builder, dirsToSpecs)
428}
429
Justin Yun34baa2e2024-08-30 21:11:33 +0900430func (f *filesystem) copyFilesToProductOut(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
Justin Yun2cc42502024-09-11 14:10:20 +0900431 if f.Name() != ctx.Config().SoongDefinedSystemImage() {
Justin Yun34baa2e2024-08-30 21:11:33 +0900432 return
433 }
434 installPath := android.PathForModuleInPartitionInstall(ctx, f.partitionName())
435 builder.Command().Textf("cp -prf %s/* %s", rebasedDir, installPath)
436}
437
Jiyong Park11a65972021-02-01 21:09:38 +0900438func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900439 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700440 rebasedDir := rootDir
441 if f.properties.Base_dir != nil {
442 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
443 }
444 builder := android.NewRuleBuilder(pctx, ctx)
445 // Wipe the root dir to get rid of leftover files from prior builds
446 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900447 specs := f.gatherFilteredPackagingSpecs(ctx)
Inseob Kim33f95a92024-07-11 15:44:49 +0900448 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700449
450 f.buildNonDepsFiles(ctx, builder, rootDir)
Cole Faust4a2a7c92024-03-12 12:44:40 -0700451 f.addMakeBuiltFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900452 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Inseob Kimb7b84572024-04-30 10:51:47 +0900453 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
Justin Yun74f3f302024-05-07 14:32:14 +0900454 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
Spandan Das92631882024-10-28 22:49:38 +0000455 f.buildLinkerConfigFile(ctx, builder, rebasedDir)
Justin Yun34baa2e2024-08-30 21:11:33 +0900456 f.copyFilesToProductOut(ctx, builder, rebasedDir)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900457
Nikita Ioffe519015f2022-12-23 15:36:29 +0000458 // run host_init_verifier
459 // Ideally we should have a concept of pluggable linters that verify the generated image.
460 // While such concept is not implement this will do.
461 // TODO(b/263574231): substitute with pluggable linter.
462 builder.Command().
463 BuiltTool("host_init_verifier").
464 FlagWithArg("--out_system=", rootDir.String()+"/system")
465
Jiyong Park72678312021-01-18 17:29:49 +0900466 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900467 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800468 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900469 Text(rootDir.String()). // input directory
470 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900471 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900472 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900473 Text(rootDir.String()) // directory where to find fs_config_files|dirs
474
475 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800476 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900477
Jiyong Park11a65972021-02-01 21:09:38 +0900478 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900479}
480
Inseob Kimcc8e5362021-02-03 14:05:24 +0900481func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
482 builder := android.NewRuleBuilder(pctx, ctx)
483 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
484 builder.Command().BuiltTool("sefcontext_compile").
485 FlagWithOutput("-o ", fcBin).
486 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
487 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
488 return fcBin.OutputPath
489}
490
Jooyung Han65f402b2022-04-21 14:24:04 +0900491// Calculates avb_salt from entry list (sorted) for deterministic output.
492func (f *filesystem) salt() string {
493 return sha1sum(f.entries)
494}
495
Jiyong Park72678312021-01-18 17:29:49 +0900496func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +0900497 var deps android.Paths
Cole Faustcec230a2024-03-07 15:51:12 -0800498 var propFileString strings.Builder
Jiyong Park72678312021-01-18 17:29:49 +0900499 addStr := func(name string, value string) {
Cole Faustcec230a2024-03-07 15:51:12 -0800500 propFileString.WriteString(name)
501 propFileString.WriteRune('=')
502 propFileString.WriteString(value)
503 propFileString.WriteRune('\n')
Jiyong Park72678312021-01-18 17:29:49 +0900504 }
505 addPath := func(name string, path android.Path) {
Cole Faustcec230a2024-03-07 15:51:12 -0800506 addStr(name, path.String())
Jiyong Park72678312021-01-18 17:29:49 +0900507 deps = append(deps, path)
508 }
509
Jiyong Park11a65972021-02-01 21:09:38 +0900510 // Type string that build_image.py accepts.
511 fsTypeStr := func(t fsType) string {
512 switch t {
Spandan Das94668822024-10-09 20:51:33 +0000513 // TODO(372522486): add more types like f2fs, erofs, etc.
Jiyong Park11a65972021-02-01 21:09:38 +0900514 case ext4Type:
515 return "ext4"
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000516 case erofsType:
517 return "erofs"
mrziwang1a6291f2024-11-07 14:29:25 -0800518 case f2fsType:
519 return "f2fs"
Jiyong Park11a65972021-02-01 21:09:38 +0900520 }
521 panic(fmt.Errorf("unsupported fs type %v", t))
522 }
523
524 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim376d72f2023-11-01 15:40:25 +0900525 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +0900526 addStr("use_dynamic_partition_size", "true")
527 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
528 // b/177813163 deps of the host tools have to be added. Remove this.
529 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
530 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
531 }
532
Jiyong Park71baa762021-01-18 21:11:03 +0900533 if proptools.Bool(f.properties.Use_avb) {
534 addStr("avb_hashtree_enable", "true")
535 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
536 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
537 addStr("avb_algorithm", algorithm)
538 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
539 addPath("avb_key_path", key)
Inseob Kim53391842024-03-29 17:44:07 +0900540 addStr("partition_name", f.partitionName())
Shikha Panware6f30632022-12-21 12:54:45 +0000541 avb_add_hashtree_footer_args := "--do_not_generate_fec"
542 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
543 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
544 }
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000545 if f.properties.Rollback_index != nil {
546 rollbackIndex := proptools.Int(f.properties.Rollback_index)
547 if rollbackIndex < 0 {
548 ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
549 }
550 avb_add_hashtree_footer_args += " --rollback_index " + strconv.Itoa(rollbackIndex)
551 }
Inseob Kim53391842024-03-29 17:44:07 +0900552 securityPatchKey := "com.android.build." + f.partitionName() + ".security_patch"
Seungjae Yooa30e4502023-11-09 14:55:44 +0900553 securityPatchValue := ctx.Config().PlatformSecurityPatch()
554 avb_add_hashtree_footer_args += " --prop " + securityPatchKey + ":" + securityPatchValue
Shikha Panware6f30632022-12-21 12:54:45 +0000555 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jooyung Han65f402b2022-04-21 14:24:04 +0900556 addStr("avb_salt", f.salt())
Jiyong Park71baa762021-01-18 21:11:03 +0900557 }
558
Inseob Kimcc8e5362021-02-03 14:05:24 +0900559 if proptools.String(f.properties.File_contexts) != "" {
560 addPath("selinux_fc", f.buildFileContexts(ctx))
561 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900562 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
563 addStr("timestamp", timestamp)
564 }
565 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
566 addStr("uuid", uuid)
567 addStr("hash_seed", uuid)
568 }
mrziwang1a6291f2024-11-07 14:29:25 -0800569
570 fst := f.fsType(ctx)
571 switch fst {
572 case erofsType:
573 // Add erofs properties
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000574 if compressor := f.properties.Erofs.Compressor; compressor != nil {
575 addStr("erofs_default_compressor", proptools.String(compressor))
576 }
577 if compressHints := f.properties.Erofs.Compress_hints; compressHints != nil {
Cole Faust7cd4bad2024-10-11 10:31:12 -0700578 addPath("erofs_default_compress_hints", android.PathForModuleSrc(ctx, *compressHints))
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000579 }
580 if proptools.BoolDefault(f.properties.Erofs.Sparse, true) {
581 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2292;bpv=1;bpt=0;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b
582 addStr("erofs_sparse_flag", "-s")
583 }
mrziwang1a6291f2024-11-07 14:29:25 -0800584 case f2fsType:
585 if proptools.BoolDefault(f.properties.F2fs.Sparse, true) {
586 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2294;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b;bpv=1;bpt=0
587 addStr("f2fs_sparse_flag", "-S")
588 }
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000589 }
mrziwang1a6291f2024-11-07 14:29:25 -0800590 f.checkFsTypePropertyError(ctx, fst, fsTypeStr(fst))
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000591
Jiyong Park72678312021-01-18 17:29:49 +0900592 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
Cole Faustcec230a2024-03-07 15:51:12 -0800593 android.WriteFileRuleVerbatim(ctx, propFile, propFileString.String())
Jiyong Park72678312021-01-18 17:29:49 +0900594 return propFile, deps
595}
596
mrziwang1a6291f2024-11-07 14:29:25 -0800597// This method checks if there is any property set for the fstype(s) other than
598// the current fstype.
599func (f *filesystem) checkFsTypePropertyError(ctx android.ModuleContext, t fsType, fs string) {
600 raiseError := func(otherFsType, currentFsType string) {
601 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)
602 ctx.PropertyErrorf(otherFsType, errMsg)
603 }
604
605 if t != erofsType {
606 if f.properties.Erofs.Compressor != nil || f.properties.Erofs.Compress_hints != nil || f.properties.Erofs.Sparse != nil {
607 raiseError("erofs", fs)
608 }
609 }
610 if t != f2fsType {
611 if f.properties.F2fs.Sparse != nil {
612 raiseError("f2fs", fs)
613 }
614 }
615}
616
Jiyong Park837cdb22021-02-05 00:17:14 +0900617func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900618 if proptools.Bool(f.properties.Use_avb) {
619 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
620 "Consider adding this to bootimg module and signing the entire boot image.")
621 }
622
Inseob Kimcc8e5362021-02-03 14:05:24 +0900623 if proptools.String(f.properties.File_contexts) != "" {
624 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
625 }
626
Cole Faust4a2a7c92024-03-12 12:44:40 -0700627 if f.properties.Include_make_built_files != "" {
628 ctx.PropertyErrorf("include_make_built_files", "include_make_built_files is not supported for compressed cpio image.")
629 }
630
Jiyong Park11a65972021-02-01 21:09:38 +0900631 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700632 rebasedDir := rootDir
633 if f.properties.Base_dir != nil {
634 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
635 }
636 builder := android.NewRuleBuilder(pctx, ctx)
637 // Wipe the root dir to get rid of leftover files from prior builds
638 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900639 specs := f.gatherFilteredPackagingSpecs(ctx)
Inseob Kim33f95a92024-07-11 15:44:49 +0900640 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700641
642 f.buildNonDepsFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900643 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Inseob Kimb7b84572024-04-30 10:51:47 +0900644 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
Justin Yun74f3f302024-05-07 14:32:14 +0900645 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
Spandan Das92631882024-10-28 22:49:38 +0000646 f.buildLinkerConfigFile(ctx, builder, rebasedDir)
Justin Yun34baa2e2024-08-30 21:11:33 +0900647 f.copyFilesToProductOut(ctx, builder, rebasedDir)
Jiyong Park11a65972021-02-01 21:09:38 +0900648
649 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900650 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900651 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900652 Text(rootDir.String()) // input directory
653 if compressed {
654 cmd.Text("|").
655 BuiltTool("lz4").
656 Flag("--favor-decSpeed"). // for faster boot
657 Flag("-12"). // maximum compression level
658 Flag("-l"). // legacy format for kernel
659 Text(">").Output(output)
660 } else {
661 cmd.Text(">").Output(output)
662 }
Jiyong Park11a65972021-02-01 21:09:38 +0900663
664 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900665 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900666
667 return output
668}
669
Cole Faust4a2a7c92024-03-12 12:44:40 -0700670var validPartitions = []string{
671 "system",
672 "userdata",
673 "cache",
674 "system_other",
675 "vendor",
676 "product",
677 "system_ext",
678 "odm",
679 "vendor_dlkm",
680 "odm_dlkm",
681 "system_dlkm",
682}
683
684func (f *filesystem) addMakeBuiltFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.Path) {
685 partition := f.properties.Include_make_built_files
686 if partition == "" {
687 return
688 }
689 if !slices.Contains(validPartitions, partition) {
690 ctx.PropertyErrorf("include_make_built_files", "Expected one of %#v, found %q", validPartitions, partition)
691 return
692 }
693 stampFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/staging_dir.stamp", ctx.Config().DeviceName(), partition)
694 fileListFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partition)
695 stagingDir := fmt.Sprintf("target/product/%s/%s", ctx.Config().DeviceName(), partition)
696
697 builder.Command().BuiltTool("merge_directories").
698 Implicit(android.PathForArbitraryOutput(ctx, stampFile)).
699 Text("--ignore-duplicates").
700 FlagWithInput("--file-list", android.PathForArbitraryOutput(ctx, fileListFile)).
701 Text(rootDir.String()).
702 Text(android.PathForArbitraryOutput(ctx, stagingDir).String())
703}
704
Inseob Kimb7b84572024-04-30 10:51:47 +0900705func (f *filesystem) buildEventLogtagsFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
706 if !proptools.Bool(f.properties.Build_logtags) {
707 return
708 }
709
710 logtagsFilePaths := make(map[string]bool)
711 ctx.WalkDeps(func(child, parent android.Module) bool {
712 if logtagsInfo, ok := android.OtherModuleProvider(ctx, child, android.LogtagsProviderKey); ok {
713 for _, path := range logtagsInfo.Logtags {
714 logtagsFilePaths[path.String()] = true
715 }
716 }
717 return true
718 })
719
720 if len(logtagsFilePaths) == 0 {
721 return
722 }
723
724 etcPath := rebasedDir.Join(ctx, "etc")
725 eventLogtagsPath := etcPath.Join(ctx, "event-log-tags")
726 builder.Command().Text("mkdir").Flag("-p").Text(etcPath.String())
727 cmd := builder.Command().BuiltTool("merge-event-log-tags").
728 FlagWithArg("-o ", eventLogtagsPath.String()).
729 FlagWithInput("-m ", android.MergedLogtagsPath(ctx))
730
731 for _, path := range android.SortedKeys(logtagsFilePaths) {
732 cmd.Text(path)
733 }
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900734
735 f.appendToEntry(ctx, eventLogtagsPath)
Inseob Kimb7b84572024-04-30 10:51:47 +0900736}
737
Spandan Das92631882024-10-28 22:49:38 +0000738func (f *filesystem) buildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
Spandan Das173256b2024-10-31 19:59:30 +0000739 if !proptools.Bool(f.properties.Linkerconfig.Gen_linker_config) {
Spandan Das92631882024-10-28 22:49:38 +0000740 return
741 }
742
Spandan Das918191e2024-10-31 18:27:23 +0000743 provideModules, _ := f.getLibsForLinkerConfig(ctx)
Spandan Das92631882024-10-28 22:49:38 +0000744 output := rebasedDir.Join(ctx, "etc", "linker.config.pb")
Spandan Das173256b2024-10-31 19:59:30 +0000745 linkerconfig.BuildLinkerConfig(ctx, builder, android.PathsForModuleSrc(ctx, f.properties.Linkerconfig.Linker_config_srcs), provideModules, nil, output)
Spandan Das92631882024-10-28 22:49:38 +0000746
747 f.appendToEntry(ctx, output)
748}
749
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900750type partition interface {
751 PartitionType() string
752}
753
Cole Faust9a24d902024-03-18 15:38:12 -0700754func (f *filesystem) PartitionType() string {
755 return proptools.StringDefault(f.properties.Partition_type, "system")
756}
757
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900758var _ partition = (*filesystem)(nil)
759
Jiyong Park65c49f52020-11-24 14:23:26 +0900760var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
761
762// Implements android.AndroidMkEntriesProvider
763func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
764 return []android.AndroidMkEntries{android.AndroidMkEntries{
765 Class: "ETC",
766 OutputFile: android.OptionalPathForPath(f.output),
767 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700768 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800769 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900770 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900771 entries.SetString("LOCAL_FILESYSTEM_FILELIST", f.fileListFile.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900772 },
773 },
774 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900775}
Jiyong Park12a719c2021-01-07 15:31:24 +0900776
777// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
778// package to have access to the output file.
779type Filesystem interface {
780 android.Module
781 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900782
783 // Returns the output file that is signed by avbtool. If this module is not signed, returns
784 // nil.
785 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900786}
787
788var _ Filesystem = (*filesystem)(nil)
789
790func (f *filesystem) OutputPath() android.Path {
791 return f.output
792}
Jiyong Park972e06c2021-03-15 23:32:49 +0900793
794func (f *filesystem) SignedOutputPath() android.Path {
795 if proptools.Bool(f.properties.Use_avb) {
796 return f.OutputPath()
797 }
798 return nil
799}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900800
801// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
802// Note that "apex" module installs its contents to "apex"(fake partition) as well
803// for symbol lookup by imitating "activated" paths.
804func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Jeongik Cha54bf8752024-02-08 10:44:37 +0900805 specs := f.PackagingBase.GatherPackagingSpecsWithFilter(ctx, f.filterPackagingSpec)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900806 return specs
807}
Jooyung Han65f402b2022-04-21 14:24:04 +0900808
809func sha1sum(values []string) string {
810 h := sha256.New()
811 for _, value := range values {
812 io.WriteString(h, value)
813 }
814 return fmt.Sprintf("%x", h.Sum(nil))
815}
Jooyung Hane6067592023-03-16 13:11:17 +0900816
817// Base cc.UseCoverage
818
819var _ cc.UseCoverage = (*filesystem)(nil)
820
Colin Crosse1a85552024-06-14 12:17:37 -0700821func (*filesystem) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +0900822 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
823}
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900824
825// android_filesystem_defaults
826
827type filesystemDefaults struct {
828 android.ModuleBase
829 android.DefaultsModuleBase
830
831 properties filesystemDefaultsProperties
832}
833
834type filesystemDefaultsProperties struct {
835 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
836 // checks, and will be used in the future for API surface checks.
837 Partition_type *string
838}
839
840// android_filesystem_defaults is a default module for android_filesystem and android_system_image
841func filesystemDefaultsFactory() android.Module {
842 module := &filesystemDefaults{}
843 module.AddProperties(&module.properties)
844 module.AddProperties(&android.PackagingProperties{})
845 android.InitDefaultsModule(module)
846 return module
847}
848
849func (f *filesystemDefaults) PartitionType() string {
850 return proptools.StringDefault(f.properties.Partition_type, "system")
851}
852
853var _ partition = (*filesystemDefaults)(nil)
854
855func (f *filesystemDefaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
856 validatePartitionType(ctx, f)
857}
Spandan Das918191e2024-10-31 18:27:23 +0000858
859// getLibsForLinkerConfig returns
860// 1. A list of libraries installed in this filesystem
861// 2. A list of dep libraries _not_ installed in this filesystem
862//
863// `linkerconfig.BuildLinkerConfig` will convert these two to a linker.config.pb for the filesystem
864// (1) will be added to --provideLibs if they are C libraries with a stable interface (has stubs)
865// (2) will be added to --requireLibs if they are C libraries with a stable interface (has stubs)
866func (f *filesystem) getLibsForLinkerConfig(ctx android.ModuleContext) ([]android.Module, []android.Module) {
867 // we need "Module"s for packaging items
868 modulesInPackageByModule := make(map[android.Module]bool)
869 modulesInPackageByName := make(map[string]bool)
870
871 deps := f.gatherFilteredPackagingSpecs(ctx)
872 ctx.WalkDeps(func(child, parent android.Module) bool {
873 for _, ps := range android.OtherModuleProviderOrDefault(
874 ctx, child, android.InstallFilesProvider).PackagingSpecs {
875 if _, ok := deps[ps.RelPathInPackage()]; ok {
876 modulesInPackageByModule[child] = true
877 modulesInPackageByName[child.Name()] = true
878 return true
879 }
880 }
881 return true
882 })
883
884 provideModules := make([]android.Module, 0, len(modulesInPackageByModule))
885 for mod := range modulesInPackageByModule {
886 provideModules = append(provideModules, mod)
887 }
888
889 var requireModules []android.Module
890 ctx.WalkDeps(func(child, parent android.Module) bool {
891 _, parentInPackage := modulesInPackageByModule[parent]
892 _, childInPackageName := modulesInPackageByName[child.Name()]
893
894 // When parent is in the package, and child (or its variant) is not, this can be from an interface.
895 if parentInPackage && !childInPackageName {
896 requireModules = append(requireModules, child)
897 }
898 return true
899 })
900
901 return provideModules, requireModules
902}