blob: 29e5ec3781674542268c8e207b0d759eb9b65d2b [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 Park65c49f52020-11-24 14:23:26 +090055 output android.OutputPath
56 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090057
Kiyoung Kim99a954d2024-06-21 14:22:20 +090058 fileListFile android.OutputPath
59
60 // Keeps the entries installed from this filesystem
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090061 entries []string
Kiyoung Kim67118212024-11-07 13:23:44 +090062
63 filesystemBuilder filesystemBuilder
Jiyong Park6f0f6882020-11-12 13:14:30 +090064}
65
Kiyoung Kim67118212024-11-07 13:23:44 +090066type filesystemBuilder interface {
67 BuildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath)
68 // Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs()
69 FilterPackagingSpec(spec android.PackagingSpec) bool
Inseob Kim3c0a0422024-11-05 17:21:37 +090070 // Function that modifies PackagingSpec in PackagingBase.GatherPackagingSpecs() to customize.
71 // For example, GSI system.img contains system_ext and product artifacts and their
72 // relPathInPackage need to be rebased to system/system_ext and system/system_product.
73 ModifyPackagingSpec(spec *android.PackagingSpec)
Kiyoung Kim67118212024-11-07 13:23:44 +090074}
75
76var _ filesystemBuilder = (*filesystem)(nil)
77
Spandan Das69464c32024-10-25 20:08:06 +000078type SymlinkDefinition struct {
Inseob Kim14199b02021-02-09 21:18:31 +090079 Target *string
80 Name *string
81}
82
Jihoon Kang98047cf2024-10-02 17:13:54 +000083type FilesystemProperties struct {
Jiyong Park71baa762021-01-18 21:11:03 +090084 // When set to true, sign the image with avbtool. Default is false.
85 Use_avb *bool
86
87 // Path to the private key that avbtool will use to sign this filesystem image.
88 // TODO(jiyong): allow apex_key to be specified here
89 Avb_private_key *string `android:"path"`
90
Shikha Panwar01403bb2022-12-22 12:22:57 +000091 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +090092 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090093
Shikha Panwar01403bb2022-12-22 12:22:57 +000094 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
95 // avbtool. Default used by avbtool is sha1.
Shikha Panware6f30632022-12-21 12:54:45 +000096 Avb_hash_algorithm *string
97
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +000098 // The index used to prevent rollback of the image. Only used if use_avb is true.
99 Rollback_index *int64
100
Jiyong Parkac4076d2021-03-15 23:21:30 +0900101 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
102 Partition_name *string
103
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000104 // Type of the filesystem. Currently, ext4, erofs, cpio, and compressed_cpio are supported. Default
Jiyong Park837cdb22021-02-05 00:17:14 +0900105 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +0900106 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +0900107
Cole Faust9a24d902024-03-18 15:38:12 -0700108 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
109 // checks, and will be used in the future for API surface checks.
110 Partition_type *string
111
Inseob Kimcc8e5362021-02-03 14:05:24 +0900112 // file_contexts file to make image. Currently, only ext4 is supported.
113 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900114
115 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
116 // (root).
117 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +0900118
119 // Directories to be created under root. e.g. /dev, /proc, etc.
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700120 Dirs proptools.Configurable[[]string]
Inseob Kim14199b02021-02-09 21:18:31 +0900121
122 // Symbolic links to be created under root with "ln -sf <target> <name>".
Spandan Das69464c32024-10-25 20:08:06 +0000123 Symlinks []SymlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900124
125 // Seconds since unix epoch to override timestamps of file entries
126 Fake_timestamp *string
127
128 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
129 // Otherwise, they'll be set as random which might cause indeterministic build output.
130 Uuid *string
Inseob Kim376d72f2023-11-01 15:40:25 +0900131
132 // Mount point for this image. Default is "/"
133 Mount_point *string
Cole Faust4a2a7c92024-03-12 12:44:40 -0700134
135 // If set to the name of a partition ("system", "vendor", etc), this filesystem module
136 // will also include the contents of the make-built staging directories. If any soong
137 // modules would be installed to the same location as a make module, they will overwrite
138 // the make version.
139 Include_make_built_files string
Inseob Kim53391842024-03-29 17:44:07 +0900140
Inseob Kimb7b84572024-04-30 10:51:47 +0900141 // When set, builds etc/event-log-tags file by merging logtags from all dependencies.
142 // Default is false
143 Build_logtags *bool
144
Justin Yun74f3f302024-05-07 14:32:14 +0900145 // Install aconfig_flags.pb file for the modules installed in this partition.
146 Gen_aconfig_flags_pb *bool
147
Inseob Kim53391842024-03-29 17:44:07 +0900148 Fsverity fsverityProperties
Cole Faust92ccbe22024-10-03 14:38:37 -0700149
150 // If this property is set to true, the filesystem will call ctx.UncheckedModule(), causing
151 // it to not be built on checkbuilds. Used for the automatic migration from make to soong
152 // build modules, where we want to emit some not-yet-working filesystems and we don't want them
153 // to be built.
154 Unchecked_module *bool `blueprint:"mutated"`
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000155
156 Erofs ErofsProperties
Jihoon Kang0d545b82024-10-11 00:21:57 +0000157
mrziwang1a6291f2024-11-07 14:29:25 -0800158 F2fs F2fsProperties
159
Spandan Das2047a4c2024-11-11 21:24:58 +0000160 Linker_config LinkerConfigProperties
Spandan Das92631882024-10-28 22:49:38 +0000161
Jihoon Kang0d545b82024-10-11 00:21:57 +0000162 // Determines if the module is auto-generated from Soong or not. If the module is
163 // auto-generated, its deps are exempted from visibility enforcement.
164 Is_auto_generated *bool
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000165}
166
167// Additional properties required to generate erofs FS partitions.
168type ErofsProperties struct {
169 // Compressor and Compression level passed to mkfs.erofs. e.g. (lz4hc,9)
170 // Please see external/erofs-utils/README for complete documentation.
171 Compressor *string
172
173 // Used as --compress-hints for mkfs.erofs
174 Compress_hints *string `android:"path"`
175
176 Sparse *bool
Jiyong Park71baa762021-01-18 21:11:03 +0900177}
178
mrziwang1a6291f2024-11-07 14:29:25 -0800179// Additional properties required to generate f2fs FS partitions.
180type F2fsProperties struct {
181 Sparse *bool
182}
183
Spandan Das173256b2024-10-31 19:59:30 +0000184type LinkerConfigProperties struct {
185
186 // Build a linker.config.pb file
187 Gen_linker_config *bool
188
189 // List of files (in .json format) that will be converted to a linker config file (in .pb format).
190 // The linker config file be installed in the filesystem at /etc/linker.config.pb
191 Linker_config_srcs []string `android:"path"`
192}
193
Jiyong Park65c49f52020-11-24 14:23:26 +0900194// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
195// image. The filesystem images are expected to be mounted in the target device, which means the
196// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
197// The modules are placed in the filesystem image just like they are installed to the ordinary
198// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Cole Faust92ccbe22024-10-03 14:38:37 -0700199func FilesystemFactory() android.Module {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900200 module := &filesystem{}
Kiyoung Kim67118212024-11-07 13:23:44 +0900201 module.filesystemBuilder = module
Cole Faust2cfe6962024-09-17 11:31:14 -0700202 initFilesystemModule(module, module)
Jiyong Parkfa616132021-04-20 11:36:40 +0900203 return module
204}
205
Cole Faust2cfe6962024-09-17 11:31:14 -0700206func initFilesystemModule(module android.DefaultableModule, filesystemModule *filesystem) {
207 module.AddProperties(&filesystemModule.properties)
208 android.InitPackageModule(filesystemModule)
209 filesystemModule.PackagingBase.DepsCollectFirstTargetOnly = true
Jihoon Kang79196c52024-10-30 18:49:47 +0000210 filesystemModule.PackagingBase.AllowHighPriorityDeps = true
Jiyong Park6f0f6882020-11-12 13:14:30 +0900211 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900212 android.InitDefaultableModule(module)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900213}
214
Jihoon Kang0d545b82024-10-11 00:21:57 +0000215type depTag struct {
Jiyong Park12a719c2021-01-07 15:31:24 +0900216 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900217 android.PackagingItemAlwaysDepTag
Jihoon Kang0d545b82024-10-11 00:21:57 +0000218}
219
220var dependencyTag = depTag{}
221
222type depTagWithVisibilityEnforcementBypass struct {
223 depTag
224}
225
226var _ android.ExcludeFromVisibilityEnforcementTag = (*depTagWithVisibilityEnforcementBypass)(nil)
227
228func (t depTagWithVisibilityEnforcementBypass) ExcludeFromVisibilityEnforcement() {}
229
230var dependencyTagWithVisibilityEnforcementBypass = depTagWithVisibilityEnforcementBypass{}
Jiyong Park65b62242020-11-25 12:44:59 +0900231
Jiyong Park6f0f6882020-11-12 13:14:30 +0900232func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000233 if proptools.Bool(f.properties.Is_auto_generated) {
234 f.AddDeps(ctx, dependencyTagWithVisibilityEnforcementBypass)
235 } else {
236 f.AddDeps(ctx, dependencyTag)
237 }
Jiyong Park6f0f6882020-11-12 13:14:30 +0900238}
239
Jiyong Park11a65972021-02-01 21:09:38 +0900240type fsType int
241
242const (
243 ext4Type fsType = iota
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000244 erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800245 f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900246 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900247 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900248 unknown
249)
250
Spandan Das7a46f6c2024-10-14 18:41:18 +0000251func (fs fsType) IsUnknown() bool {
252 return fs == unknown
253}
254
Cole Faust92ccbe22024-10-03 14:38:37 -0700255type FilesystemInfo struct {
256 // A text file containing the list of paths installed on the partition.
257 FileListFile android.Path
258}
259
260var FilesystemProvider = blueprint.NewProvider[FilesystemInfo]()
261
Spandan Das7a46f6c2024-10-14 18:41:18 +0000262func GetFsTypeFromString(ctx android.EarlyModuleContext, typeStr string) fsType {
Jiyong Park11a65972021-02-01 21:09:38 +0900263 switch typeStr {
264 case "ext4":
265 return ext4Type
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000266 case "erofs":
267 return erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800268 case "f2fs":
269 return f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900270 case "compressed_cpio":
271 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900272 case "cpio":
273 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900274 default:
Jiyong Park11a65972021-02-01 21:09:38 +0900275 return unknown
276 }
277}
278
Spandan Das7a46f6c2024-10-14 18:41:18 +0000279func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
280 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
281 fsType := GetFsTypeFromString(ctx, typeStr)
282 if fsType == unknown {
283 ctx.PropertyErrorf("type", "%q not supported", typeStr)
284 }
285 return fsType
286}
287
Jiyong Park65c49f52020-11-24 14:23:26 +0900288func (f *filesystem) installFileName() string {
289 return f.BaseModuleName() + ".img"
290}
291
Inseob Kim53391842024-03-29 17:44:07 +0900292func (f *filesystem) partitionName() string {
293 return proptools.StringDefault(f.properties.Partition_name, f.Name())
294}
295
Kiyoung Kim67118212024-11-07 13:23:44 +0900296func (f *filesystem) FilterPackagingSpec(ps android.PackagingSpec) bool {
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000297 // Filesystem module respects the installation semantic. A PackagingSpec from a module with
298 // IsSkipInstall() is skipped.
Cole Faust76a6e952024-11-07 16:56:45 -0800299 if ps.SkipInstall() {
300 return false
Spandan Das6d056502024-10-21 15:40:32 +0000301 }
Cole Faust76a6e952024-11-07 16:56:45 -0800302 if proptools.Bool(f.properties.Is_auto_generated) { // TODO (spandandas): Remove this.
303 pt := f.PartitionType()
304 return pt == "ramdisk" || ps.Partition() == pt
305 }
306 return true
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000307}
308
Inseob Kim3c0a0422024-11-05 17:21:37 +0900309func (f *filesystem) ModifyPackagingSpec(ps *android.PackagingSpec) {
310 // do nothing by default
311}
312
Jiyong Park6f0f6882020-11-12 13:14:30 +0900313var pctx = android.NewPackageContext("android/soong/filesystem")
314
315func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900316 validatePartitionType(ctx, f)
Jiyong Park11a65972021-02-01 21:09:38 +0900317 switch f.fsType(ctx) {
mrziwang1a6291f2024-11-07 14:29:25 -0800318 case ext4Type, erofsType, f2fsType:
Jiyong Park11a65972021-02-01 21:09:38 +0900319 f.output = f.buildImageUsingBuildImage(ctx)
320 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900321 f.output = f.buildCpioImage(ctx, true)
322 case cpioType:
323 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900324 default:
325 return
326 }
327
328 f.installDir = android.PathForModuleInstall(ctx, "etc")
329 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
mrziwang555d1332024-06-07 11:15:33 -0700330 ctx.SetOutputFiles([]android.Path{f.output}, "")
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900331
332 f.fileListFile = android.PathForModuleOut(ctx, "fileList").OutputPath
333 android.WriteFileRule(ctx, f.fileListFile, f.installedFilesList())
Cole Faust92ccbe22024-10-03 14:38:37 -0700334
335 android.SetProvider(ctx, FilesystemProvider, FilesystemInfo{
336 FileListFile: f.fileListFile,
337 })
338
339 if proptools.Bool(f.properties.Unchecked_module) {
340 ctx.UncheckedModule()
341 }
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900342}
343
344func (f *filesystem) appendToEntry(ctx android.ModuleContext, installedFile android.OutputPath) {
345 partitionBaseDir := android.PathForModuleOut(ctx, "root", f.partitionName()).String() + "/"
346
347 relPath, inTargetPartition := strings.CutPrefix(installedFile.String(), partitionBaseDir)
348 if inTargetPartition {
349 f.entries = append(f.entries, relPath)
350 }
351}
352
353func (f *filesystem) installedFilesList() string {
354 installedFilePaths := android.FirstUniqueStrings(f.entries)
355 slices.Sort(installedFilePaths)
356
357 return strings.Join(installedFilePaths, "\n")
Jiyong Park11a65972021-02-01 21:09:38 +0900358}
359
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900360func validatePartitionType(ctx android.ModuleContext, p partition) {
361 if !android.InList(p.PartitionType(), validPartitions) {
362 ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, p.PartitionType())
363 }
364
365 ctx.VisitDirectDepsWithTag(android.DefaultsDepTag, func(m android.Module) {
366 if fdm, ok := m.(*filesystemDefaults); ok {
367 if p.PartitionType() != fdm.PartitionType() {
368 ctx.PropertyErrorf("partition_type",
369 "%s doesn't match with the partition type %s of the filesystem default module %s",
370 p.PartitionType(), fdm.PartitionType(), m.Name())
371 }
372 }
373 })
374}
375
Cole Faust3b806d32024-03-11 15:15:03 -0700376// Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files
377// already in `rootDir`.
378func (f *filesystem) buildNonDepsFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.OutputPath) {
Inseob Kim14199b02021-02-09 21:18:31 +0900379 // create dirs and symlinks
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700380 for _, dir := range f.properties.Dirs.GetOrDefault(ctx, nil) {
Inseob Kim14199b02021-02-09 21:18:31 +0900381 // OutputPath.Join verifies dir
382 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
383 }
384
385 for _, symlink := range f.properties.Symlinks {
386 name := strings.TrimSpace(proptools.String(symlink.Name))
387 target := strings.TrimSpace(proptools.String(symlink.Target))
388
389 if name == "" {
390 ctx.PropertyErrorf("symlinks", "Name can't be empty")
391 continue
392 }
393
394 if target == "" {
395 ctx.PropertyErrorf("symlinks", "Target can't be empty")
396 continue
397 }
398
399 // OutputPath.Join verifies name. don't need to verify target.
400 dst := rootDir.Join(ctx, name)
Cole Faust3b806d32024-03-11 15:15:03 -0700401 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 +0900402 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
403 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900404 f.appendToEntry(ctx, dst)
Inseob Kim14199b02021-02-09 21:18:31 +0900405 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900406}
407
Inseob Kim33f95a92024-07-11 15:44:49 +0900408func (f *filesystem) copyPackagingSpecs(ctx android.ModuleContext, builder *android.RuleBuilder, specs map[string]android.PackagingSpec, rootDir, rebasedDir android.WritablePath) []string {
409 rootDirSpecs := make(map[string]android.PackagingSpec)
410 rebasedDirSpecs := make(map[string]android.PackagingSpec)
411
412 for rel, spec := range specs {
413 if spec.Partition() == "root" {
414 rootDirSpecs[rel] = spec
415 } else {
416 rebasedDirSpecs[rel] = spec
417 }
418 }
419
420 dirsToSpecs := make(map[android.WritablePath]map[string]android.PackagingSpec)
421 dirsToSpecs[rootDir] = rootDirSpecs
422 dirsToSpecs[rebasedDir] = rebasedDirSpecs
423
424 return f.CopySpecsToDirs(ctx, builder, dirsToSpecs)
425}
426
Justin Yun34baa2e2024-08-30 21:11:33 +0900427func (f *filesystem) copyFilesToProductOut(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
Justin Yun2cc42502024-09-11 14:10:20 +0900428 if f.Name() != ctx.Config().SoongDefinedSystemImage() {
Justin Yun34baa2e2024-08-30 21:11:33 +0900429 return
430 }
431 installPath := android.PathForModuleInPartitionInstall(ctx, f.partitionName())
432 builder.Command().Textf("cp -prf %s/* %s", rebasedDir, installPath)
433}
434
Jiyong Park11a65972021-02-01 21:09:38 +0900435func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900436 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700437 rebasedDir := rootDir
438 if f.properties.Base_dir != nil {
439 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
440 }
441 builder := android.NewRuleBuilder(pctx, ctx)
442 // Wipe the root dir to get rid of leftover files from prior builds
443 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900444 specs := f.gatherFilteredPackagingSpecs(ctx)
Inseob Kim33f95a92024-07-11 15:44:49 +0900445 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700446
447 f.buildNonDepsFiles(ctx, builder, rootDir)
Cole Faust4a2a7c92024-03-12 12:44:40 -0700448 f.addMakeBuiltFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900449 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Inseob Kimb7b84572024-04-30 10:51:47 +0900450 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
Justin Yun74f3f302024-05-07 14:32:14 +0900451 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
Kiyoung Kim67118212024-11-07 13:23:44 +0900452 f.filesystemBuilder.BuildLinkerConfigFile(ctx, builder, rebasedDir)
Justin Yun34baa2e2024-08-30 21:11:33 +0900453 f.copyFilesToProductOut(ctx, builder, rebasedDir)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900454
Nikita Ioffe519015f2022-12-23 15:36:29 +0000455 // run host_init_verifier
456 // Ideally we should have a concept of pluggable linters that verify the generated image.
457 // While such concept is not implement this will do.
458 // TODO(b/263574231): substitute with pluggable linter.
459 builder.Command().
460 BuiltTool("host_init_verifier").
461 FlagWithArg("--out_system=", rootDir.String()+"/system")
462
Jiyong Park72678312021-01-18 17:29:49 +0900463 propFile, toolDeps := f.buildPropFile(ctx)
Jiyong Park11a65972021-02-01 21:09:38 +0900464 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Colin Crossf1a035e2020-11-16 17:32:30 -0800465 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900466 Text(rootDir.String()). // input directory
467 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900468 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900469 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900470 Text(rootDir.String()) // directory where to find fs_config_files|dirs
471
472 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800473 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900474
Jiyong Park11a65972021-02-01 21:09:38 +0900475 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900476}
477
Inseob Kimcc8e5362021-02-03 14:05:24 +0900478func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
479 builder := android.NewRuleBuilder(pctx, ctx)
480 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
481 builder.Command().BuiltTool("sefcontext_compile").
482 FlagWithOutput("-o ", fcBin).
483 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
484 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
485 return fcBin.OutputPath
486}
487
Jooyung Han65f402b2022-04-21 14:24:04 +0900488// Calculates avb_salt from entry list (sorted) for deterministic output.
489func (f *filesystem) salt() string {
490 return sha1sum(f.entries)
491}
492
Jiyong Park72678312021-01-18 17:29:49 +0900493func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +0900494 var deps android.Paths
Cole Faustcec230a2024-03-07 15:51:12 -0800495 var propFileString strings.Builder
Jiyong Park72678312021-01-18 17:29:49 +0900496 addStr := func(name string, value string) {
Cole Faustcec230a2024-03-07 15:51:12 -0800497 propFileString.WriteString(name)
498 propFileString.WriteRune('=')
499 propFileString.WriteString(value)
500 propFileString.WriteRune('\n')
Jiyong Park72678312021-01-18 17:29:49 +0900501 }
502 addPath := func(name string, path android.Path) {
Cole Faustcec230a2024-03-07 15:51:12 -0800503 addStr(name, path.String())
Jiyong Park72678312021-01-18 17:29:49 +0900504 deps = append(deps, path)
505 }
506
Jiyong Park11a65972021-02-01 21:09:38 +0900507 // Type string that build_image.py accepts.
508 fsTypeStr := func(t fsType) string {
509 switch t {
Spandan Das94668822024-10-09 20:51:33 +0000510 // TODO(372522486): add more types like f2fs, erofs, etc.
Jiyong Park11a65972021-02-01 21:09:38 +0900511 case ext4Type:
512 return "ext4"
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000513 case erofsType:
514 return "erofs"
mrziwang1a6291f2024-11-07 14:29:25 -0800515 case f2fsType:
516 return "f2fs"
Jiyong Park11a65972021-02-01 21:09:38 +0900517 }
518 panic(fmt.Errorf("unsupported fs type %v", t))
519 }
520
521 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim376d72f2023-11-01 15:40:25 +0900522 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +0900523 addStr("use_dynamic_partition_size", "true")
524 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
525 // b/177813163 deps of the host tools have to be added. Remove this.
526 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
527 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
528 }
529
Jiyong Park71baa762021-01-18 21:11:03 +0900530 if proptools.Bool(f.properties.Use_avb) {
531 addStr("avb_hashtree_enable", "true")
532 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
533 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
534 addStr("avb_algorithm", algorithm)
535 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
536 addPath("avb_key_path", key)
Inseob Kim53391842024-03-29 17:44:07 +0900537 addStr("partition_name", f.partitionName())
Shikha Panware6f30632022-12-21 12:54:45 +0000538 avb_add_hashtree_footer_args := "--do_not_generate_fec"
539 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
540 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
541 }
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000542 if f.properties.Rollback_index != nil {
543 rollbackIndex := proptools.Int(f.properties.Rollback_index)
544 if rollbackIndex < 0 {
545 ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
546 }
547 avb_add_hashtree_footer_args += " --rollback_index " + strconv.Itoa(rollbackIndex)
548 }
Inseob Kim53391842024-03-29 17:44:07 +0900549 securityPatchKey := "com.android.build." + f.partitionName() + ".security_patch"
Seungjae Yooa30e4502023-11-09 14:55:44 +0900550 securityPatchValue := ctx.Config().PlatformSecurityPatch()
551 avb_add_hashtree_footer_args += " --prop " + securityPatchKey + ":" + securityPatchValue
Shikha Panware6f30632022-12-21 12:54:45 +0000552 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jooyung Han65f402b2022-04-21 14:24:04 +0900553 addStr("avb_salt", f.salt())
Jiyong Park71baa762021-01-18 21:11:03 +0900554 }
555
Inseob Kimcc8e5362021-02-03 14:05:24 +0900556 if proptools.String(f.properties.File_contexts) != "" {
557 addPath("selinux_fc", f.buildFileContexts(ctx))
558 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900559 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
560 addStr("timestamp", timestamp)
561 }
562 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
563 addStr("uuid", uuid)
564 addStr("hash_seed", uuid)
565 }
mrziwang1a6291f2024-11-07 14:29:25 -0800566
567 fst := f.fsType(ctx)
568 switch fst {
569 case erofsType:
570 // Add erofs properties
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000571 if compressor := f.properties.Erofs.Compressor; compressor != nil {
572 addStr("erofs_default_compressor", proptools.String(compressor))
573 }
574 if compressHints := f.properties.Erofs.Compress_hints; compressHints != nil {
Cole Faust7cd4bad2024-10-11 10:31:12 -0700575 addPath("erofs_default_compress_hints", android.PathForModuleSrc(ctx, *compressHints))
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000576 }
577 if proptools.BoolDefault(f.properties.Erofs.Sparse, true) {
578 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2292;bpv=1;bpt=0;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b
579 addStr("erofs_sparse_flag", "-s")
580 }
mrziwang1a6291f2024-11-07 14:29:25 -0800581 case f2fsType:
582 if proptools.BoolDefault(f.properties.F2fs.Sparse, true) {
583 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2294;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b;bpv=1;bpt=0
584 addStr("f2fs_sparse_flag", "-S")
585 }
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000586 }
mrziwang1a6291f2024-11-07 14:29:25 -0800587 f.checkFsTypePropertyError(ctx, fst, fsTypeStr(fst))
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000588
Jiyong Park72678312021-01-18 17:29:49 +0900589 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
Cole Faustcec230a2024-03-07 15:51:12 -0800590 android.WriteFileRuleVerbatim(ctx, propFile, propFileString.String())
Jiyong Park72678312021-01-18 17:29:49 +0900591 return propFile, deps
592}
593
mrziwang1a6291f2024-11-07 14:29:25 -0800594// This method checks if there is any property set for the fstype(s) other than
595// the current fstype.
596func (f *filesystem) checkFsTypePropertyError(ctx android.ModuleContext, t fsType, fs string) {
597 raiseError := func(otherFsType, currentFsType string) {
598 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)
599 ctx.PropertyErrorf(otherFsType, errMsg)
600 }
601
602 if t != erofsType {
603 if f.properties.Erofs.Compressor != nil || f.properties.Erofs.Compress_hints != nil || f.properties.Erofs.Sparse != nil {
604 raiseError("erofs", fs)
605 }
606 }
607 if t != f2fsType {
608 if f.properties.F2fs.Sparse != nil {
609 raiseError("f2fs", fs)
610 }
611 }
612}
613
Jiyong Park837cdb22021-02-05 00:17:14 +0900614func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
Jiyong Park11a65972021-02-01 21:09:38 +0900615 if proptools.Bool(f.properties.Use_avb) {
616 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
617 "Consider adding this to bootimg module and signing the entire boot image.")
618 }
619
Inseob Kimcc8e5362021-02-03 14:05:24 +0900620 if proptools.String(f.properties.File_contexts) != "" {
621 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
622 }
623
Cole Faust4a2a7c92024-03-12 12:44:40 -0700624 if f.properties.Include_make_built_files != "" {
625 ctx.PropertyErrorf("include_make_built_files", "include_make_built_files is not supported for compressed cpio image.")
626 }
627
Jiyong Park11a65972021-02-01 21:09:38 +0900628 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700629 rebasedDir := rootDir
630 if f.properties.Base_dir != nil {
631 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
632 }
633 builder := android.NewRuleBuilder(pctx, ctx)
634 // Wipe the root dir to get rid of leftover files from prior builds
635 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900636 specs := f.gatherFilteredPackagingSpecs(ctx)
Inseob Kim33f95a92024-07-11 15:44:49 +0900637 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700638
639 f.buildNonDepsFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900640 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Inseob Kimb7b84572024-04-30 10:51:47 +0900641 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
Justin Yun74f3f302024-05-07 14:32:14 +0900642 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
Kiyoung Kim67118212024-11-07 13:23:44 +0900643 f.filesystemBuilder.BuildLinkerConfigFile(ctx, builder, rebasedDir)
Justin Yun34baa2e2024-08-30 21:11:33 +0900644 f.copyFilesToProductOut(ctx, builder, rebasedDir)
Jiyong Park11a65972021-02-01 21:09:38 +0900645
646 output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Jiyong Park837cdb22021-02-05 00:17:14 +0900647 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900648 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900649 Text(rootDir.String()) // input directory
650 if compressed {
651 cmd.Text("|").
652 BuiltTool("lz4").
653 Flag("--favor-decSpeed"). // for faster boot
654 Flag("-12"). // maximum compression level
655 Flag("-l"). // legacy format for kernel
656 Text(">").Output(output)
657 } else {
658 cmd.Text(">").Output(output)
659 }
Jiyong Park11a65972021-02-01 21:09:38 +0900660
661 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900662 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900663
664 return output
665}
666
Cole Faust4a2a7c92024-03-12 12:44:40 -0700667var validPartitions = []string{
668 "system",
669 "userdata",
670 "cache",
671 "system_other",
672 "vendor",
673 "product",
674 "system_ext",
675 "odm",
676 "vendor_dlkm",
677 "odm_dlkm",
678 "system_dlkm",
Cole Faust76a6e952024-11-07 16:56:45 -0800679 "ramdisk",
Cole Faust4a2a7c92024-03-12 12:44:40 -0700680}
681
682func (f *filesystem) addMakeBuiltFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.Path) {
683 partition := f.properties.Include_make_built_files
684 if partition == "" {
685 return
686 }
687 if !slices.Contains(validPartitions, partition) {
688 ctx.PropertyErrorf("include_make_built_files", "Expected one of %#v, found %q", validPartitions, partition)
689 return
690 }
691 stampFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/staging_dir.stamp", ctx.Config().DeviceName(), partition)
692 fileListFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partition)
693 stagingDir := fmt.Sprintf("target/product/%s/%s", ctx.Config().DeviceName(), partition)
694
695 builder.Command().BuiltTool("merge_directories").
696 Implicit(android.PathForArbitraryOutput(ctx, stampFile)).
697 Text("--ignore-duplicates").
698 FlagWithInput("--file-list", android.PathForArbitraryOutput(ctx, fileListFile)).
699 Text(rootDir.String()).
700 Text(android.PathForArbitraryOutput(ctx, stagingDir).String())
701}
702
Inseob Kimb7b84572024-04-30 10:51:47 +0900703func (f *filesystem) buildEventLogtagsFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
704 if !proptools.Bool(f.properties.Build_logtags) {
705 return
706 }
707
708 logtagsFilePaths := make(map[string]bool)
709 ctx.WalkDeps(func(child, parent android.Module) bool {
710 if logtagsInfo, ok := android.OtherModuleProvider(ctx, child, android.LogtagsProviderKey); ok {
711 for _, path := range logtagsInfo.Logtags {
712 logtagsFilePaths[path.String()] = true
713 }
714 }
715 return true
716 })
717
718 if len(logtagsFilePaths) == 0 {
719 return
720 }
721
722 etcPath := rebasedDir.Join(ctx, "etc")
723 eventLogtagsPath := etcPath.Join(ctx, "event-log-tags")
724 builder.Command().Text("mkdir").Flag("-p").Text(etcPath.String())
725 cmd := builder.Command().BuiltTool("merge-event-log-tags").
726 FlagWithArg("-o ", eventLogtagsPath.String()).
727 FlagWithInput("-m ", android.MergedLogtagsPath(ctx))
728
729 for _, path := range android.SortedKeys(logtagsFilePaths) {
730 cmd.Text(path)
731 }
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900732
733 f.appendToEntry(ctx, eventLogtagsPath)
Inseob Kimb7b84572024-04-30 10:51:47 +0900734}
735
Kiyoung Kim67118212024-11-07 13:23:44 +0900736func (f *filesystem) BuildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
Spandan Das2047a4c2024-11-11 21:24:58 +0000737 if !proptools.Bool(f.properties.Linker_config.Gen_linker_config) {
Spandan Das92631882024-10-28 22:49:38 +0000738 return
739 }
740
Spandan Das918191e2024-10-31 18:27:23 +0000741 provideModules, _ := f.getLibsForLinkerConfig(ctx)
Spandan Das92631882024-10-28 22:49:38 +0000742 output := rebasedDir.Join(ctx, "etc", "linker.config.pb")
Spandan Das2047a4c2024-11-11 21:24:58 +0000743 linkerconfig.BuildLinkerConfig(ctx, builder, android.PathsForModuleSrc(ctx, f.properties.Linker_config.Linker_config_srcs), provideModules, nil, output)
Spandan Das92631882024-10-28 22:49:38 +0000744
745 f.appendToEntry(ctx, output)
746}
747
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900748type partition interface {
749 PartitionType() string
750}
751
Cole Faust9a24d902024-03-18 15:38:12 -0700752func (f *filesystem) PartitionType() string {
753 return proptools.StringDefault(f.properties.Partition_type, "system")
754}
755
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900756var _ partition = (*filesystem)(nil)
757
Jiyong Park65c49f52020-11-24 14:23:26 +0900758var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
759
760// Implements android.AndroidMkEntriesProvider
761func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
762 return []android.AndroidMkEntries{android.AndroidMkEntries{
763 Class: "ETC",
764 OutputFile: android.OptionalPathForPath(f.output),
765 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700766 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800767 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900768 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900769 entries.SetString("LOCAL_FILESYSTEM_FILELIST", f.fileListFile.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900770 },
771 },
772 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900773}
Jiyong Park12a719c2021-01-07 15:31:24 +0900774
775// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
776// package to have access to the output file.
777type Filesystem interface {
778 android.Module
779 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900780
781 // Returns the output file that is signed by avbtool. If this module is not signed, returns
782 // nil.
783 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900784}
785
786var _ Filesystem = (*filesystem)(nil)
787
788func (f *filesystem) OutputPath() android.Path {
789 return f.output
790}
Jiyong Park972e06c2021-03-15 23:32:49 +0900791
792func (f *filesystem) SignedOutputPath() android.Path {
793 if proptools.Bool(f.properties.Use_avb) {
794 return f.OutputPath()
795 }
796 return nil
797}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900798
799// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
800// Note that "apex" module installs its contents to "apex"(fake partition) as well
801// for symbol lookup by imitating "activated" paths.
802func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Inseob Kim3c0a0422024-11-05 17:21:37 +0900803 specs := f.PackagingBase.GatherPackagingSpecsWithFilterAndModifier(ctx, f.filesystemBuilder.FilterPackagingSpec, f.filesystemBuilder.ModifyPackagingSpec)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900804 return specs
805}
Jooyung Han65f402b2022-04-21 14:24:04 +0900806
807func sha1sum(values []string) string {
808 h := sha256.New()
809 for _, value := range values {
810 io.WriteString(h, value)
811 }
812 return fmt.Sprintf("%x", h.Sum(nil))
813}
Jooyung Hane6067592023-03-16 13:11:17 +0900814
815// Base cc.UseCoverage
816
817var _ cc.UseCoverage = (*filesystem)(nil)
818
Colin Crosse1a85552024-06-14 12:17:37 -0700819func (*filesystem) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +0900820 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
821}
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900822
823// android_filesystem_defaults
824
825type filesystemDefaults struct {
826 android.ModuleBase
827 android.DefaultsModuleBase
828
Inseob Kim3c0a0422024-11-05 17:21:37 +0900829 properties FilesystemProperties
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900830}
831
832// android_filesystem_defaults is a default module for android_filesystem and android_system_image
833func filesystemDefaultsFactory() android.Module {
834 module := &filesystemDefaults{}
835 module.AddProperties(&module.properties)
836 module.AddProperties(&android.PackagingProperties{})
837 android.InitDefaultsModule(module)
838 return module
839}
840
841func (f *filesystemDefaults) PartitionType() string {
842 return proptools.StringDefault(f.properties.Partition_type, "system")
843}
844
845var _ partition = (*filesystemDefaults)(nil)
846
847func (f *filesystemDefaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
848 validatePartitionType(ctx, f)
849}
Spandan Das918191e2024-10-31 18:27:23 +0000850
851// getLibsForLinkerConfig returns
852// 1. A list of libraries installed in this filesystem
853// 2. A list of dep libraries _not_ installed in this filesystem
854//
855// `linkerconfig.BuildLinkerConfig` will convert these two to a linker.config.pb for the filesystem
856// (1) will be added to --provideLibs if they are C libraries with a stable interface (has stubs)
857// (2) will be added to --requireLibs if they are C libraries with a stable interface (has stubs)
858func (f *filesystem) getLibsForLinkerConfig(ctx android.ModuleContext) ([]android.Module, []android.Module) {
859 // we need "Module"s for packaging items
860 modulesInPackageByModule := make(map[android.Module]bool)
861 modulesInPackageByName := make(map[string]bool)
862
863 deps := f.gatherFilteredPackagingSpecs(ctx)
864 ctx.WalkDeps(func(child, parent android.Module) bool {
865 for _, ps := range android.OtherModuleProviderOrDefault(
866 ctx, child, android.InstallFilesProvider).PackagingSpecs {
867 if _, ok := deps[ps.RelPathInPackage()]; ok {
868 modulesInPackageByModule[child] = true
869 modulesInPackageByName[child.Name()] = true
870 return true
871 }
872 }
873 return true
874 })
875
876 provideModules := make([]android.Module, 0, len(modulesInPackageByModule))
877 for mod := range modulesInPackageByModule {
878 provideModules = append(provideModules, mod)
879 }
880
881 var requireModules []android.Module
882 ctx.WalkDeps(func(child, parent android.Module) bool {
883 _, parentInPackage := modulesInPackageByModule[parent]
884 _, childInPackageName := modulesInPackageByName[child.Name()]
885
886 // When parent is in the package, and child (or its variant) is not, this can be from an interface.
887 if parentInPackage && !childInPackageName {
888 requireModules = append(requireModules, child)
889 }
890 return true
891 })
892
893 return provideModules, requireModules
894}