blob: c471da170479451636bfbceedb6656f486520f30 [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
Cole Faust4e9f5922024-11-13 16:09:23 -080055 output android.Path
Jiyong Park65c49f52020-11-24 14:23:26 +090056 installDir android.InstallPath
Jooyung Han0fbbc2b2022-03-25 12:35:46 +090057
Cole Faust4e9f5922024-11-13 16:09:23 -080058 fileListFile android.Path
Kiyoung Kim99a954d2024-06-21 14:22:20 +090059
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 Kim23be5bb2024-11-27 00:50:30 +000074
75 // Function to check if the filesystem should not use `vintf_fragments` property,
76 // but use `vintf_fragment` module type instead
77 ShouldUseVintfFragmentModuleOnly() bool
Kiyoung Kim67118212024-11-07 13:23:44 +090078}
79
80var _ filesystemBuilder = (*filesystem)(nil)
81
Spandan Das69464c32024-10-25 20:08:06 +000082type SymlinkDefinition struct {
Inseob Kim14199b02021-02-09 21:18:31 +090083 Target *string
84 Name *string
85}
86
Jihoon Kang98047cf2024-10-02 17:13:54 +000087type FilesystemProperties struct {
Jiyong Park71baa762021-01-18 21:11:03 +090088 // When set to true, sign the image with avbtool. Default is false.
89 Use_avb *bool
90
91 // Path to the private key that avbtool will use to sign this filesystem image.
92 // TODO(jiyong): allow apex_key to be specified here
93 Avb_private_key *string `android:"path"`
94
Shikha Panwar01403bb2022-12-22 12:22:57 +000095 // Signing algorithm for avbtool. Default is SHA256_RSA4096.
Jiyong Park71baa762021-01-18 21:11:03 +090096 Avb_algorithm *string
Jiyong Park11a65972021-02-01 21:09:38 +090097
Shikha Panwar01403bb2022-12-22 12:22:57 +000098 // Hash algorithm used for avbtool (for descriptors). This is passed as hash_algorithm to
99 // avbtool. Default used by avbtool is sha1.
Shikha Panware6f30632022-12-21 12:54:45 +0000100 Avb_hash_algorithm *string
101
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000102 // The index used to prevent rollback of the image. Only used if use_avb is true.
103 Rollback_index *int64
104
Jiyong Parkac4076d2021-03-15 23:21:30 +0900105 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
106 Partition_name *string
107
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000108 // Type of the filesystem. Currently, ext4, erofs, cpio, and compressed_cpio are supported. Default
Jiyong Park837cdb22021-02-05 00:17:14 +0900109 // is ext4.
Jiyong Park11a65972021-02-01 21:09:38 +0900110 Type *string
Inseob Kimcc8e5362021-02-03 14:05:24 +0900111
Cole Faust9a24d902024-03-18 15:38:12 -0700112 // Identifies which partition this is for //visibility:any_system_image (and others) visibility
113 // checks, and will be used in the future for API surface checks.
114 Partition_type *string
115
Inseob Kimcc8e5362021-02-03 14:05:24 +0900116 // file_contexts file to make image. Currently, only ext4 is supported.
117 File_contexts *string `android:"path"`
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900118
119 // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
120 // (root).
121 Base_dir *string
Inseob Kim14199b02021-02-09 21:18:31 +0900122
123 // Directories to be created under root. e.g. /dev, /proc, etc.
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700124 Dirs proptools.Configurable[[]string]
Inseob Kim14199b02021-02-09 21:18:31 +0900125
126 // Symbolic links to be created under root with "ln -sf <target> <name>".
Spandan Das69464c32024-10-25 20:08:06 +0000127 Symlinks []SymlinkDefinition
Jooyung Han65f402b2022-04-21 14:24:04 +0900128
129 // Seconds since unix epoch to override timestamps of file entries
130 Fake_timestamp *string
131
132 // When set, passed to mkuserimg_mke2fs --mke2fs_uuid & --mke2fs_hash_seed.
133 // Otherwise, they'll be set as random which might cause indeterministic build output.
134 Uuid *string
Inseob Kim376d72f2023-11-01 15:40:25 +0900135
136 // Mount point for this image. Default is "/"
137 Mount_point *string
Cole Faust4a2a7c92024-03-12 12:44:40 -0700138
139 // If set to the name of a partition ("system", "vendor", etc), this filesystem module
140 // will also include the contents of the make-built staging directories. If any soong
141 // modules would be installed to the same location as a make module, they will overwrite
142 // the make version.
143 Include_make_built_files string
Inseob Kim53391842024-03-29 17:44:07 +0900144
Inseob Kimb7b84572024-04-30 10:51:47 +0900145 // When set, builds etc/event-log-tags file by merging logtags from all dependencies.
146 // Default is false
147 Build_logtags *bool
148
Justin Yun74f3f302024-05-07 14:32:14 +0900149 // Install aconfig_flags.pb file for the modules installed in this partition.
150 Gen_aconfig_flags_pb *bool
151
Inseob Kim53391842024-03-29 17:44:07 +0900152 Fsverity fsverityProperties
Cole Faust92ccbe22024-10-03 14:38:37 -0700153
154 // If this property is set to true, the filesystem will call ctx.UncheckedModule(), causing
155 // it to not be built on checkbuilds. Used for the automatic migration from make to soong
156 // build modules, where we want to emit some not-yet-working filesystems and we don't want them
157 // to be built.
158 Unchecked_module *bool `blueprint:"mutated"`
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000159
160 Erofs ErofsProperties
Jihoon Kang0d545b82024-10-11 00:21:57 +0000161
mrziwang1a6291f2024-11-07 14:29:25 -0800162 F2fs F2fsProperties
163
Spandan Das2047a4c2024-11-11 21:24:58 +0000164 Linker_config LinkerConfigProperties
Spandan Das92631882024-10-28 22:49:38 +0000165
Jihoon Kang0d545b82024-10-11 00:21:57 +0000166 // Determines if the module is auto-generated from Soong or not. If the module is
167 // auto-generated, its deps are exempted from visibility enforcement.
168 Is_auto_generated *bool
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000169}
170
171// Additional properties required to generate erofs FS partitions.
172type ErofsProperties struct {
173 // Compressor and Compression level passed to mkfs.erofs. e.g. (lz4hc,9)
174 // Please see external/erofs-utils/README for complete documentation.
175 Compressor *string
176
177 // Used as --compress-hints for mkfs.erofs
178 Compress_hints *string `android:"path"`
179
180 Sparse *bool
Jiyong Park71baa762021-01-18 21:11:03 +0900181}
182
mrziwang1a6291f2024-11-07 14:29:25 -0800183// Additional properties required to generate f2fs FS partitions.
184type F2fsProperties struct {
185 Sparse *bool
186}
187
Spandan Das173256b2024-10-31 19:59:30 +0000188type LinkerConfigProperties struct {
189
190 // Build a linker.config.pb file
191 Gen_linker_config *bool
192
193 // List of files (in .json format) that will be converted to a linker config file (in .pb format).
194 // The linker config file be installed in the filesystem at /etc/linker.config.pb
195 Linker_config_srcs []string `android:"path"`
196}
197
Jiyong Park65c49f52020-11-24 14:23:26 +0900198// android_filesystem packages a set of modules and their transitive dependencies into a filesystem
199// image. The filesystem images are expected to be mounted in the target device, which means the
200// modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
201// The modules are placed in the filesystem image just like they are installed to the ordinary
202// partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
Cole Faust92ccbe22024-10-03 14:38:37 -0700203func FilesystemFactory() android.Module {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900204 module := &filesystem{}
Kiyoung Kim67118212024-11-07 13:23:44 +0900205 module.filesystemBuilder = module
Cole Faust2cfe6962024-09-17 11:31:14 -0700206 initFilesystemModule(module, module)
Jiyong Parkfa616132021-04-20 11:36:40 +0900207 return module
208}
209
Cole Faust2cfe6962024-09-17 11:31:14 -0700210func initFilesystemModule(module android.DefaultableModule, filesystemModule *filesystem) {
211 module.AddProperties(&filesystemModule.properties)
212 android.InitPackageModule(filesystemModule)
213 filesystemModule.PackagingBase.DepsCollectFirstTargetOnly = true
Jihoon Kang79196c52024-10-30 18:49:47 +0000214 filesystemModule.PackagingBase.AllowHighPriorityDeps = true
Jiyong Park6f0f6882020-11-12 13:14:30 +0900215 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900216 android.InitDefaultableModule(module)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900217}
218
Jihoon Kang0d545b82024-10-11 00:21:57 +0000219type depTag struct {
Jiyong Park12a719c2021-01-07 15:31:24 +0900220 blueprint.BaseDependencyTag
Jooyung Han092ef812021-03-10 15:40:34 +0900221 android.PackagingItemAlwaysDepTag
Jihoon Kang0d545b82024-10-11 00:21:57 +0000222}
223
224var dependencyTag = depTag{}
225
226type depTagWithVisibilityEnforcementBypass struct {
227 depTag
228}
229
230var _ android.ExcludeFromVisibilityEnforcementTag = (*depTagWithVisibilityEnforcementBypass)(nil)
231
232func (t depTagWithVisibilityEnforcementBypass) ExcludeFromVisibilityEnforcement() {}
233
234var dependencyTagWithVisibilityEnforcementBypass = depTagWithVisibilityEnforcementBypass{}
Jiyong Park65b62242020-11-25 12:44:59 +0900235
Jiyong Park6f0f6882020-11-12 13:14:30 +0900236func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000237 if proptools.Bool(f.properties.Is_auto_generated) {
238 f.AddDeps(ctx, dependencyTagWithVisibilityEnforcementBypass)
239 } else {
240 f.AddDeps(ctx, dependencyTag)
241 }
Jiyong Park6f0f6882020-11-12 13:14:30 +0900242}
243
Jiyong Park11a65972021-02-01 21:09:38 +0900244type fsType int
245
246const (
247 ext4Type fsType = iota
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000248 erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800249 f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900250 compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900251 cpioType // uncompressed
Jiyong Park11a65972021-02-01 21:09:38 +0900252 unknown
253)
254
Spandan Das7a46f6c2024-10-14 18:41:18 +0000255func (fs fsType) IsUnknown() bool {
256 return fs == unknown
257}
258
Cole Faust92ccbe22024-10-03 14:38:37 -0700259type FilesystemInfo struct {
260 // A text file containing the list of paths installed on the partition.
261 FileListFile android.Path
262}
263
264var FilesystemProvider = blueprint.NewProvider[FilesystemInfo]()
265
Spandan Das7a46f6c2024-10-14 18:41:18 +0000266func GetFsTypeFromString(ctx android.EarlyModuleContext, typeStr string) fsType {
Jiyong Park11a65972021-02-01 21:09:38 +0900267 switch typeStr {
268 case "ext4":
269 return ext4Type
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000270 case "erofs":
271 return erofsType
mrziwang1a6291f2024-11-07 14:29:25 -0800272 case "f2fs":
273 return f2fsType
Jiyong Park11a65972021-02-01 21:09:38 +0900274 case "compressed_cpio":
275 return compressedCpioType
Jiyong Park837cdb22021-02-05 00:17:14 +0900276 case "cpio":
277 return cpioType
Jiyong Park11a65972021-02-01 21:09:38 +0900278 default:
Jiyong Park11a65972021-02-01 21:09:38 +0900279 return unknown
280 }
281}
282
Spandan Das7a46f6c2024-10-14 18:41:18 +0000283func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
284 typeStr := proptools.StringDefault(f.properties.Type, "ext4")
285 fsType := GetFsTypeFromString(ctx, typeStr)
286 if fsType == unknown {
287 ctx.PropertyErrorf("type", "%q not supported", typeStr)
288 }
289 return fsType
290}
291
Jiyong Park65c49f52020-11-24 14:23:26 +0900292func (f *filesystem) installFileName() string {
293 return f.BaseModuleName() + ".img"
294}
295
Inseob Kim53391842024-03-29 17:44:07 +0900296func (f *filesystem) partitionName() string {
297 return proptools.StringDefault(f.properties.Partition_name, f.Name())
298}
299
Kiyoung Kim67118212024-11-07 13:23:44 +0900300func (f *filesystem) FilterPackagingSpec(ps android.PackagingSpec) bool {
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000301 // Filesystem module respects the installation semantic. A PackagingSpec from a module with
302 // IsSkipInstall() is skipped.
Cole Faust76a6e952024-11-07 16:56:45 -0800303 if ps.SkipInstall() {
304 return false
Spandan Das6d056502024-10-21 15:40:32 +0000305 }
Cole Faust76a6e952024-11-07 16:56:45 -0800306 if proptools.Bool(f.properties.Is_auto_generated) { // TODO (spandandas): Remove this.
307 pt := f.PartitionType()
Cole Faustc88cff12024-11-12 13:24:05 -0800308 return ps.Partition() == pt || strings.HasPrefix(ps.Partition(), pt+"/")
Cole Faust76a6e952024-11-07 16:56:45 -0800309 }
310 return true
Jiyong Park7e7d4af2024-05-01 12:36:10 +0000311}
312
Inseob Kim3c0a0422024-11-05 17:21:37 +0900313func (f *filesystem) ModifyPackagingSpec(ps *android.PackagingSpec) {
Cole Faustc88cff12024-11-12 13:24:05 -0800314 // Sometimes, android.modulePartition() returns a path with >1 path components.
315 // This makes the partition field of packagingSpecs have multiple components, like
316 // "system/product". Right now, the filesystem module doesn't look at the partition field
317 // when deciding what path to install the file under, only the RelPathInPackage field, so
318 // we move the later path components from partition to relPathInPackage. This should probably
319 // be revisited in the future.
320 prefix := f.PartitionType() + "/"
321 if strings.HasPrefix(ps.Partition(), prefix) {
322 subPartition := strings.TrimPrefix(ps.Partition(), prefix)
323 ps.SetPartition(f.PartitionType())
324 ps.SetRelPathInPackage(filepath.Join(subPartition, ps.RelPathInPackage()))
325 }
Inseob Kim3c0a0422024-11-05 17:21:37 +0900326}
327
Jiyong Park6f0f6882020-11-12 13:14:30 +0900328var pctx = android.NewPackageContext("android/soong/filesystem")
329
330func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900331 validatePartitionType(ctx, f)
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000332 if f.filesystemBuilder.ShouldUseVintfFragmentModuleOnly() {
333 f.validateVintfFragments(ctx)
334 }
Jiyong Park11a65972021-02-01 21:09:38 +0900335 switch f.fsType(ctx) {
mrziwang1a6291f2024-11-07 14:29:25 -0800336 case ext4Type, erofsType, f2fsType:
Jiyong Park11a65972021-02-01 21:09:38 +0900337 f.output = f.buildImageUsingBuildImage(ctx)
338 case compressedCpioType:
Jiyong Park837cdb22021-02-05 00:17:14 +0900339 f.output = f.buildCpioImage(ctx, true)
340 case cpioType:
341 f.output = f.buildCpioImage(ctx, false)
Jiyong Park11a65972021-02-01 21:09:38 +0900342 default:
343 return
344 }
345
346 f.installDir = android.PathForModuleInstall(ctx, "etc")
347 ctx.InstallFile(f.installDir, f.installFileName(), f.output)
mrziwang555d1332024-06-07 11:15:33 -0700348 ctx.SetOutputFiles([]android.Path{f.output}, "")
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900349
Cole Faust4e9f5922024-11-13 16:09:23 -0800350 fileListFile := android.PathForModuleOut(ctx, "fileList")
351 android.WriteFileRule(ctx, fileListFile, f.installedFilesList())
Cole Faust92ccbe22024-10-03 14:38:37 -0700352
353 android.SetProvider(ctx, FilesystemProvider, FilesystemInfo{
Cole Faust4e9f5922024-11-13 16:09:23 -0800354 FileListFile: fileListFile,
Cole Faust92ccbe22024-10-03 14:38:37 -0700355 })
Cole Faust4e9f5922024-11-13 16:09:23 -0800356 f.fileListFile = fileListFile
Cole Faust92ccbe22024-10-03 14:38:37 -0700357
358 if proptools.Bool(f.properties.Unchecked_module) {
359 ctx.UncheckedModule()
360 }
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900361}
362
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000363func (f *filesystem) validateVintfFragments(ctx android.ModuleContext) {
364 visitedModule := map[string]bool{}
365 packagingSpecs := f.gatherFilteredPackagingSpecs(ctx)
366
367 moduleInFileSystem := func(mod android.Module) bool {
368 for _, ps := range android.OtherModuleProviderOrDefault(
369 ctx, mod, android.InstallFilesProvider).PackagingSpecs {
370 if _, ok := packagingSpecs[ps.RelPathInPackage()]; ok {
371 return true
372 }
373 }
374 return false
375 }
376
377 ctx.WalkDeps(func(child, parent android.Module) bool {
378 if visitedModule[child.Name()] {
379 return false
380 }
381 if !moduleInFileSystem(child) {
382 visitedModule[child.Name()] = true
383 return true
384 }
385 if vintfFragments := child.VintfFragments(ctx); vintfFragments != nil {
386 ctx.PropertyErrorf(
387 "vintf_fragments",
388 "Module %s is referenced by soong-defined filesystem %s with property vintf_fragments(%s) in use."+
389 " Use vintf_fragment_modules property instead.",
390 child.Name(),
391 f.BaseModuleName(),
392 strings.Join(vintfFragments, ", "),
393 )
394 }
395 visitedModule[child.Name()] = true
396 return true
397 })
398}
399
Cole Faust4e9f5922024-11-13 16:09:23 -0800400func (f *filesystem) appendToEntry(ctx android.ModuleContext, installedFile android.Path) {
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900401 partitionBaseDir := android.PathForModuleOut(ctx, "root", f.partitionName()).String() + "/"
402
403 relPath, inTargetPartition := strings.CutPrefix(installedFile.String(), partitionBaseDir)
404 if inTargetPartition {
405 f.entries = append(f.entries, relPath)
406 }
407}
408
409func (f *filesystem) installedFilesList() string {
410 installedFilePaths := android.FirstUniqueStrings(f.entries)
411 slices.Sort(installedFilePaths)
412
413 return strings.Join(installedFilePaths, "\n")
Jiyong Park11a65972021-02-01 21:09:38 +0900414}
415
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900416func validatePartitionType(ctx android.ModuleContext, p partition) {
417 if !android.InList(p.PartitionType(), validPartitions) {
418 ctx.PropertyErrorf("partition_type", "partition_type must be one of %s, found: %s", validPartitions, p.PartitionType())
419 }
420
421 ctx.VisitDirectDepsWithTag(android.DefaultsDepTag, func(m android.Module) {
422 if fdm, ok := m.(*filesystemDefaults); ok {
423 if p.PartitionType() != fdm.PartitionType() {
424 ctx.PropertyErrorf("partition_type",
425 "%s doesn't match with the partition type %s of the filesystem default module %s",
426 p.PartitionType(), fdm.PartitionType(), m.Name())
427 }
428 }
429 })
430}
431
Cole Faust3b806d32024-03-11 15:15:03 -0700432// Copy extra files/dirs that are not from the `deps` property to `rootDir`, checking for conflicts with files
433// already in `rootDir`.
434func (f *filesystem) buildNonDepsFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.OutputPath) {
Inseob Kim14199b02021-02-09 21:18:31 +0900435 // create dirs and symlinks
Cole Faustd9c6a5b2024-05-21 14:54:00 -0700436 for _, dir := range f.properties.Dirs.GetOrDefault(ctx, nil) {
Inseob Kim14199b02021-02-09 21:18:31 +0900437 // OutputPath.Join verifies dir
438 builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
439 }
440
441 for _, symlink := range f.properties.Symlinks {
442 name := strings.TrimSpace(proptools.String(symlink.Name))
443 target := strings.TrimSpace(proptools.String(symlink.Target))
444
445 if name == "" {
446 ctx.PropertyErrorf("symlinks", "Name can't be empty")
447 continue
448 }
449
450 if target == "" {
451 ctx.PropertyErrorf("symlinks", "Target can't be empty")
452 continue
453 }
454
455 // OutputPath.Join verifies name. don't need to verify target.
456 dst := rootDir.Join(ctx, name)
Cole Faust3b806d32024-03-11 15:15:03 -0700457 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 +0900458 builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
459 builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900460 f.appendToEntry(ctx, dst)
Inseob Kim14199b02021-02-09 21:18:31 +0900461 }
Inseob Kim2ce1b5d2021-02-15 17:01:04 +0900462}
463
Inseob Kim33f95a92024-07-11 15:44:49 +0900464func (f *filesystem) copyPackagingSpecs(ctx android.ModuleContext, builder *android.RuleBuilder, specs map[string]android.PackagingSpec, rootDir, rebasedDir android.WritablePath) []string {
465 rootDirSpecs := make(map[string]android.PackagingSpec)
466 rebasedDirSpecs := make(map[string]android.PackagingSpec)
467
468 for rel, spec := range specs {
469 if spec.Partition() == "root" {
470 rootDirSpecs[rel] = spec
471 } else {
472 rebasedDirSpecs[rel] = spec
473 }
474 }
475
476 dirsToSpecs := make(map[android.WritablePath]map[string]android.PackagingSpec)
477 dirsToSpecs[rootDir] = rootDirSpecs
478 dirsToSpecs[rebasedDir] = rebasedDirSpecs
479
480 return f.CopySpecsToDirs(ctx, builder, dirsToSpecs)
481}
482
Justin Yun34baa2e2024-08-30 21:11:33 +0900483func (f *filesystem) copyFilesToProductOut(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
Justin Yun2cc42502024-09-11 14:10:20 +0900484 if f.Name() != ctx.Config().SoongDefinedSystemImage() {
Justin Yun34baa2e2024-08-30 21:11:33 +0900485 return
486 }
487 installPath := android.PathForModuleInPartitionInstall(ctx, f.partitionName())
488 builder.Command().Textf("cp -prf %s/* %s", rebasedDir, installPath)
489}
490
Cole Faust4e9f5922024-11-13 16:09:23 -0800491func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.Path {
Jiyong Park6f0f6882020-11-12 13:14:30 +0900492 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700493 rebasedDir := rootDir
494 if f.properties.Base_dir != nil {
495 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
496 }
497 builder := android.NewRuleBuilder(pctx, ctx)
498 // Wipe the root dir to get rid of leftover files from prior builds
499 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900500 specs := f.gatherFilteredPackagingSpecs(ctx)
Inseob Kim33f95a92024-07-11 15:44:49 +0900501 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700502
503 f.buildNonDepsFiles(ctx, builder, rootDir)
Cole Faust4a2a7c92024-03-12 12:44:40 -0700504 f.addMakeBuiltFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900505 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Inseob Kimb7b84572024-04-30 10:51:47 +0900506 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
Justin Yun74f3f302024-05-07 14:32:14 +0900507 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
Kiyoung Kim67118212024-11-07 13:23:44 +0900508 f.filesystemBuilder.BuildLinkerConfigFile(ctx, builder, rebasedDir)
Justin Yun34baa2e2024-08-30 21:11:33 +0900509 f.copyFilesToProductOut(ctx, builder, rebasedDir)
Jiyong Park6f0f6882020-11-12 13:14:30 +0900510
Nikita Ioffe519015f2022-12-23 15:36:29 +0000511 // run host_init_verifier
512 // Ideally we should have a concept of pluggable linters that verify the generated image.
513 // While such concept is not implement this will do.
514 // TODO(b/263574231): substitute with pluggable linter.
515 builder.Command().
516 BuiltTool("host_init_verifier").
517 FlagWithArg("--out_system=", rootDir.String()+"/system")
518
Jiyong Park72678312021-01-18 17:29:49 +0900519 propFile, toolDeps := f.buildPropFile(ctx)
Cole Faust4e9f5922024-11-13 16:09:23 -0800520 output := android.PathForModuleOut(ctx, f.installFileName())
Colin Crossf1a035e2020-11-16 17:32:30 -0800521 builder.Command().BuiltTool("build_image").
Jiyong Park6f0f6882020-11-12 13:14:30 +0900522 Text(rootDir.String()). // input directory
523 Input(propFile).
Jiyong Park72678312021-01-18 17:29:49 +0900524 Implicits(toolDeps).
Jiyong Park11a65972021-02-01 21:09:38 +0900525 Output(output).
Jiyong Park6f0f6882020-11-12 13:14:30 +0900526 Text(rootDir.String()) // directory where to find fs_config_files|dirs
527
528 // rootDir is not deleted. Might be useful for quick inspection.
Colin Crossf1a035e2020-11-16 17:32:30 -0800529 builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park65c49f52020-11-24 14:23:26 +0900530
Jiyong Park11a65972021-02-01 21:09:38 +0900531 return output
Jiyong Park65c49f52020-11-24 14:23:26 +0900532}
533
Cole Faust4e9f5922024-11-13 16:09:23 -0800534func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.Path {
Inseob Kimcc8e5362021-02-03 14:05:24 +0900535 builder := android.NewRuleBuilder(pctx, ctx)
536 fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
537 builder.Command().BuiltTool("sefcontext_compile").
538 FlagWithOutput("-o ", fcBin).
539 Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
540 builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
Cole Faust4e9f5922024-11-13 16:09:23 -0800541 return fcBin
Inseob Kimcc8e5362021-02-03 14:05:24 +0900542}
543
Jooyung Han65f402b2022-04-21 14:24:04 +0900544// Calculates avb_salt from entry list (sorted) for deterministic output.
545func (f *filesystem) salt() string {
546 return sha1sum(f.entries)
547}
548
Cole Faust4e9f5922024-11-13 16:09:23 -0800549func (f *filesystem) buildPropFile(ctx android.ModuleContext) (android.Path, android.Paths) {
Jiyong Park72678312021-01-18 17:29:49 +0900550 var deps android.Paths
Cole Faustcec230a2024-03-07 15:51:12 -0800551 var propFileString strings.Builder
Jiyong Park72678312021-01-18 17:29:49 +0900552 addStr := func(name string, value string) {
Cole Faustcec230a2024-03-07 15:51:12 -0800553 propFileString.WriteString(name)
554 propFileString.WriteRune('=')
555 propFileString.WriteString(value)
556 propFileString.WriteRune('\n')
Jiyong Park72678312021-01-18 17:29:49 +0900557 }
558 addPath := func(name string, path android.Path) {
Cole Faustcec230a2024-03-07 15:51:12 -0800559 addStr(name, path.String())
Jiyong Park72678312021-01-18 17:29:49 +0900560 deps = append(deps, path)
561 }
562
Jiyong Park11a65972021-02-01 21:09:38 +0900563 // Type string that build_image.py accepts.
564 fsTypeStr := func(t fsType) string {
565 switch t {
Spandan Das94668822024-10-09 20:51:33 +0000566 // TODO(372522486): add more types like f2fs, erofs, etc.
Jiyong Park11a65972021-02-01 21:09:38 +0900567 case ext4Type:
568 return "ext4"
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000569 case erofsType:
570 return "erofs"
mrziwang1a6291f2024-11-07 14:29:25 -0800571 case f2fsType:
572 return "f2fs"
Jiyong Park11a65972021-02-01 21:09:38 +0900573 }
574 panic(fmt.Errorf("unsupported fs type %v", t))
575 }
576
577 addStr("fs_type", fsTypeStr(f.fsType(ctx)))
Inseob Kim376d72f2023-11-01 15:40:25 +0900578 addStr("mount_point", proptools.StringDefault(f.properties.Mount_point, "/"))
Jiyong Park72678312021-01-18 17:29:49 +0900579 addStr("use_dynamic_partition_size", "true")
580 addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
581 // b/177813163 deps of the host tools have to be added. Remove this.
582 for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
583 deps = append(deps, ctx.Config().HostToolPath(ctx, t))
584 }
585
Jiyong Park71baa762021-01-18 21:11:03 +0900586 if proptools.Bool(f.properties.Use_avb) {
587 addStr("avb_hashtree_enable", "true")
588 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
589 algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
590 addStr("avb_algorithm", algorithm)
591 key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
592 addPath("avb_key_path", key)
Inseob Kim53391842024-03-29 17:44:07 +0900593 addStr("partition_name", f.partitionName())
Shikha Panware6f30632022-12-21 12:54:45 +0000594 avb_add_hashtree_footer_args := "--do_not_generate_fec"
595 if hashAlgorithm := proptools.String(f.properties.Avb_hash_algorithm); hashAlgorithm != "" {
596 avb_add_hashtree_footer_args += " --hash_algorithm " + hashAlgorithm
597 }
Nikita Ioffe2c8cdc62024-03-27 22:19:30 +0000598 if f.properties.Rollback_index != nil {
599 rollbackIndex := proptools.Int(f.properties.Rollback_index)
600 if rollbackIndex < 0 {
601 ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
602 }
603 avb_add_hashtree_footer_args += " --rollback_index " + strconv.Itoa(rollbackIndex)
604 }
Inseob Kim53391842024-03-29 17:44:07 +0900605 securityPatchKey := "com.android.build." + f.partitionName() + ".security_patch"
Seungjae Yooa30e4502023-11-09 14:55:44 +0900606 securityPatchValue := ctx.Config().PlatformSecurityPatch()
607 avb_add_hashtree_footer_args += " --prop " + securityPatchKey + ":" + securityPatchValue
Shikha Panware6f30632022-12-21 12:54:45 +0000608 addStr("avb_add_hashtree_footer_args", avb_add_hashtree_footer_args)
Jooyung Han65f402b2022-04-21 14:24:04 +0900609 addStr("avb_salt", f.salt())
Jiyong Park71baa762021-01-18 21:11:03 +0900610 }
611
Inseob Kimcc8e5362021-02-03 14:05:24 +0900612 if proptools.String(f.properties.File_contexts) != "" {
613 addPath("selinux_fc", f.buildFileContexts(ctx))
614 }
Jooyung Han65f402b2022-04-21 14:24:04 +0900615 if timestamp := proptools.String(f.properties.Fake_timestamp); timestamp != "" {
616 addStr("timestamp", timestamp)
617 }
618 if uuid := proptools.String(f.properties.Uuid); uuid != "" {
619 addStr("uuid", uuid)
620 addStr("hash_seed", uuid)
621 }
mrziwang1a6291f2024-11-07 14:29:25 -0800622
623 fst := f.fsType(ctx)
624 switch fst {
625 case erofsType:
626 // Add erofs properties
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000627 if compressor := f.properties.Erofs.Compressor; compressor != nil {
628 addStr("erofs_default_compressor", proptools.String(compressor))
629 }
630 if compressHints := f.properties.Erofs.Compress_hints; compressHints != nil {
Cole Faust7cd4bad2024-10-11 10:31:12 -0700631 addPath("erofs_default_compress_hints", android.PathForModuleSrc(ctx, *compressHints))
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000632 }
633 if proptools.BoolDefault(f.properties.Erofs.Sparse, true) {
634 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2292;bpv=1;bpt=0;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b
635 addStr("erofs_sparse_flag", "-s")
636 }
mrziwang1a6291f2024-11-07 14:29:25 -0800637 case f2fsType:
638 if proptools.BoolDefault(f.properties.F2fs.Sparse, true) {
639 // https://source.corp.google.com/h/googleplex-android/platform/build/+/88b1c67239ca545b11580237242774b411f2fed9:core/Makefile;l=2294;drc=ea8f34bc1d6e63656b4ec32f2391e9d54b3ebb6b;bpv=1;bpt=0
640 addStr("f2fs_sparse_flag", "-S")
641 }
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000642 }
mrziwang1a6291f2024-11-07 14:29:25 -0800643 f.checkFsTypePropertyError(ctx, fst, fsTypeStr(fst))
Spandan Dasc35d6fb2024-10-10 17:51:14 +0000644
Cole Faust4e9f5922024-11-13 16:09:23 -0800645 propFile := android.PathForModuleOut(ctx, "prop")
Cole Faustcec230a2024-03-07 15:51:12 -0800646 android.WriteFileRuleVerbatim(ctx, propFile, propFileString.String())
Jiyong Park72678312021-01-18 17:29:49 +0900647 return propFile, deps
648}
649
mrziwang1a6291f2024-11-07 14:29:25 -0800650// This method checks if there is any property set for the fstype(s) other than
651// the current fstype.
652func (f *filesystem) checkFsTypePropertyError(ctx android.ModuleContext, t fsType, fs string) {
653 raiseError := func(otherFsType, currentFsType string) {
654 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)
655 ctx.PropertyErrorf(otherFsType, errMsg)
656 }
657
658 if t != erofsType {
659 if f.properties.Erofs.Compressor != nil || f.properties.Erofs.Compress_hints != nil || f.properties.Erofs.Sparse != nil {
660 raiseError("erofs", fs)
661 }
662 }
663 if t != f2fsType {
664 if f.properties.F2fs.Sparse != nil {
665 raiseError("f2fs", fs)
666 }
667 }
668}
669
Cole Faust4e9f5922024-11-13 16:09:23 -0800670func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.Path {
Jiyong Park11a65972021-02-01 21:09:38 +0900671 if proptools.Bool(f.properties.Use_avb) {
672 ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
673 "Consider adding this to bootimg module and signing the entire boot image.")
674 }
675
Inseob Kimcc8e5362021-02-03 14:05:24 +0900676 if proptools.String(f.properties.File_contexts) != "" {
677 ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
678 }
679
Cole Faust4a2a7c92024-03-12 12:44:40 -0700680 if f.properties.Include_make_built_files != "" {
681 ctx.PropertyErrorf("include_make_built_files", "include_make_built_files is not supported for compressed cpio image.")
682 }
683
Jiyong Park11a65972021-02-01 21:09:38 +0900684 rootDir := android.PathForModuleOut(ctx, "root").OutputPath
Cole Faust3b806d32024-03-11 15:15:03 -0700685 rebasedDir := rootDir
686 if f.properties.Base_dir != nil {
687 rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
688 }
689 builder := android.NewRuleBuilder(pctx, ctx)
690 // Wipe the root dir to get rid of leftover files from prior builds
691 builder.Command().Textf("rm -rf %s && mkdir -p %s", rootDir, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900692 specs := f.gatherFilteredPackagingSpecs(ctx)
Inseob Kim33f95a92024-07-11 15:44:49 +0900693 f.entries = f.copyPackagingSpecs(ctx, builder, specs, rootDir, rebasedDir)
Cole Faust3b806d32024-03-11 15:15:03 -0700694
695 f.buildNonDepsFiles(ctx, builder, rootDir)
Inseob Kim53391842024-03-29 17:44:07 +0900696 f.buildFsverityMetadataFiles(ctx, builder, specs, rootDir, rebasedDir)
Inseob Kimb7b84572024-04-30 10:51:47 +0900697 f.buildEventLogtagsFile(ctx, builder, rebasedDir)
Justin Yun74f3f302024-05-07 14:32:14 +0900698 f.buildAconfigFlagsFiles(ctx, builder, specs, rebasedDir)
Kiyoung Kim67118212024-11-07 13:23:44 +0900699 f.filesystemBuilder.BuildLinkerConfigFile(ctx, builder, rebasedDir)
Justin Yun34baa2e2024-08-30 21:11:33 +0900700 f.copyFilesToProductOut(ctx, builder, rebasedDir)
Jiyong Park11a65972021-02-01 21:09:38 +0900701
Cole Faust4e9f5922024-11-13 16:09:23 -0800702 output := android.PathForModuleOut(ctx, f.installFileName())
Jiyong Park837cdb22021-02-05 00:17:14 +0900703 cmd := builder.Command().
Jiyong Park11a65972021-02-01 21:09:38 +0900704 BuiltTool("mkbootfs").
Jiyong Park837cdb22021-02-05 00:17:14 +0900705 Text(rootDir.String()) // input directory
706 if compressed {
707 cmd.Text("|").
708 BuiltTool("lz4").
709 Flag("--favor-decSpeed"). // for faster boot
710 Flag("-12"). // maximum compression level
711 Flag("-l"). // legacy format for kernel
712 Text(">").Output(output)
713 } else {
714 cmd.Text(">").Output(output)
715 }
Jiyong Park11a65972021-02-01 21:09:38 +0900716
717 // rootDir is not deleted. Might be useful for quick inspection.
Jiyong Park837cdb22021-02-05 00:17:14 +0900718 builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
Jiyong Park11a65972021-02-01 21:09:38 +0900719
720 return output
721}
722
Cole Faust4a2a7c92024-03-12 12:44:40 -0700723var validPartitions = []string{
724 "system",
725 "userdata",
726 "cache",
727 "system_other",
728 "vendor",
729 "product",
730 "system_ext",
731 "odm",
732 "vendor_dlkm",
733 "odm_dlkm",
734 "system_dlkm",
Cole Faust76a6e952024-11-07 16:56:45 -0800735 "ramdisk",
Cole Faust4a2a7c92024-03-12 12:44:40 -0700736}
737
738func (f *filesystem) addMakeBuiltFiles(ctx android.ModuleContext, builder *android.RuleBuilder, rootDir android.Path) {
739 partition := f.properties.Include_make_built_files
740 if partition == "" {
741 return
742 }
743 if !slices.Contains(validPartitions, partition) {
744 ctx.PropertyErrorf("include_make_built_files", "Expected one of %#v, found %q", validPartitions, partition)
745 return
746 }
747 stampFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/staging_dir.stamp", ctx.Config().DeviceName(), partition)
748 fileListFile := fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partition)
749 stagingDir := fmt.Sprintf("target/product/%s/%s", ctx.Config().DeviceName(), partition)
750
751 builder.Command().BuiltTool("merge_directories").
752 Implicit(android.PathForArbitraryOutput(ctx, stampFile)).
753 Text("--ignore-duplicates").
754 FlagWithInput("--file-list", android.PathForArbitraryOutput(ctx, fileListFile)).
755 Text(rootDir.String()).
756 Text(android.PathForArbitraryOutput(ctx, stagingDir).String())
757}
758
Inseob Kimb7b84572024-04-30 10:51:47 +0900759func (f *filesystem) buildEventLogtagsFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
760 if !proptools.Bool(f.properties.Build_logtags) {
761 return
762 }
763
764 logtagsFilePaths := make(map[string]bool)
765 ctx.WalkDeps(func(child, parent android.Module) bool {
766 if logtagsInfo, ok := android.OtherModuleProvider(ctx, child, android.LogtagsProviderKey); ok {
767 for _, path := range logtagsInfo.Logtags {
768 logtagsFilePaths[path.String()] = true
769 }
770 }
771 return true
772 })
773
774 if len(logtagsFilePaths) == 0 {
775 return
776 }
777
778 etcPath := rebasedDir.Join(ctx, "etc")
779 eventLogtagsPath := etcPath.Join(ctx, "event-log-tags")
780 builder.Command().Text("mkdir").Flag("-p").Text(etcPath.String())
781 cmd := builder.Command().BuiltTool("merge-event-log-tags").
782 FlagWithArg("-o ", eventLogtagsPath.String()).
783 FlagWithInput("-m ", android.MergedLogtagsPath(ctx))
784
785 for _, path := range android.SortedKeys(logtagsFilePaths) {
786 cmd.Text(path)
787 }
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900788
789 f.appendToEntry(ctx, eventLogtagsPath)
Inseob Kimb7b84572024-04-30 10:51:47 +0900790}
791
Kiyoung Kim67118212024-11-07 13:23:44 +0900792func (f *filesystem) BuildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath) {
Spandan Das2047a4c2024-11-11 21:24:58 +0000793 if !proptools.Bool(f.properties.Linker_config.Gen_linker_config) {
Spandan Das92631882024-10-28 22:49:38 +0000794 return
795 }
796
Spandan Das918191e2024-10-31 18:27:23 +0000797 provideModules, _ := f.getLibsForLinkerConfig(ctx)
Spandan Das92631882024-10-28 22:49:38 +0000798 output := rebasedDir.Join(ctx, "etc", "linker.config.pb")
Spandan Das2047a4c2024-11-11 21:24:58 +0000799 linkerconfig.BuildLinkerConfig(ctx, builder, android.PathsForModuleSrc(ctx, f.properties.Linker_config.Linker_config_srcs), provideModules, nil, output)
Spandan Das92631882024-10-28 22:49:38 +0000800
801 f.appendToEntry(ctx, output)
802}
803
Kiyoung Kim23be5bb2024-11-27 00:50:30 +0000804func (f *filesystem) ShouldUseVintfFragmentModuleOnly() bool {
805 return false
806}
807
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900808type partition interface {
809 PartitionType() string
810}
811
Cole Faust9a24d902024-03-18 15:38:12 -0700812func (f *filesystem) PartitionType() string {
813 return proptools.StringDefault(f.properties.Partition_type, "system")
814}
815
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900816var _ partition = (*filesystem)(nil)
817
Jiyong Park65c49f52020-11-24 14:23:26 +0900818var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
819
820// Implements android.AndroidMkEntriesProvider
821func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
822 return []android.AndroidMkEntries{android.AndroidMkEntries{
823 Class: "ETC",
824 OutputFile: android.OptionalPathForPath(f.output),
825 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700826 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800827 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900828 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
Kiyoung Kim99a954d2024-06-21 14:22:20 +0900829 entries.SetString("LOCAL_FILESYSTEM_FILELIST", f.fileListFile.String())
Jiyong Park65c49f52020-11-24 14:23:26 +0900830 },
831 },
832 }}
Jiyong Park6f0f6882020-11-12 13:14:30 +0900833}
Jiyong Park12a719c2021-01-07 15:31:24 +0900834
835// Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
836// package to have access to the output file.
837type Filesystem interface {
838 android.Module
839 OutputPath() android.Path
Jiyong Park972e06c2021-03-15 23:32:49 +0900840
841 // Returns the output file that is signed by avbtool. If this module is not signed, returns
842 // nil.
843 SignedOutputPath() android.Path
Jiyong Park12a719c2021-01-07 15:31:24 +0900844}
845
846var _ Filesystem = (*filesystem)(nil)
847
848func (f *filesystem) OutputPath() android.Path {
849 return f.output
850}
Jiyong Park972e06c2021-03-15 23:32:49 +0900851
852func (f *filesystem) SignedOutputPath() android.Path {
853 if proptools.Bool(f.properties.Use_avb) {
854 return f.OutputPath()
855 }
856 return nil
857}
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900858
859// Filter the result of GatherPackagingSpecs to discard items targeting outside "system" partition.
860// Note that "apex" module installs its contents to "apex"(fake partition) as well
861// for symbol lookup by imitating "activated" paths.
862func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
Inseob Kim3c0a0422024-11-05 17:21:37 +0900863 specs := f.PackagingBase.GatherPackagingSpecsWithFilterAndModifier(ctx, f.filesystemBuilder.FilterPackagingSpec, f.filesystemBuilder.ModifyPackagingSpec)
Jooyung Han0fbbc2b2022-03-25 12:35:46 +0900864 return specs
865}
Jooyung Han65f402b2022-04-21 14:24:04 +0900866
867func sha1sum(values []string) string {
868 h := sha256.New()
869 for _, value := range values {
870 io.WriteString(h, value)
871 }
872 return fmt.Sprintf("%x", h.Sum(nil))
873}
Jooyung Hane6067592023-03-16 13:11:17 +0900874
875// Base cc.UseCoverage
876
877var _ cc.UseCoverage = (*filesystem)(nil)
878
Colin Crosse1a85552024-06-14 12:17:37 -0700879func (*filesystem) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Jooyung Hane6067592023-03-16 13:11:17 +0900880 return ctx.Device() && ctx.DeviceConfig().NativeCoverageEnabled()
881}
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900882
883// android_filesystem_defaults
884
885type filesystemDefaults struct {
886 android.ModuleBase
887 android.DefaultsModuleBase
888
Inseob Kim3c0a0422024-11-05 17:21:37 +0900889 properties FilesystemProperties
Jiyong Parkf46b1af2024-04-05 18:13:33 +0900890}
891
892// android_filesystem_defaults is a default module for android_filesystem and android_system_image
893func filesystemDefaultsFactory() android.Module {
894 module := &filesystemDefaults{}
895 module.AddProperties(&module.properties)
896 module.AddProperties(&android.PackagingProperties{})
897 android.InitDefaultsModule(module)
898 return module
899}
900
901func (f *filesystemDefaults) PartitionType() string {
902 return proptools.StringDefault(f.properties.Partition_type, "system")
903}
904
905var _ partition = (*filesystemDefaults)(nil)
906
907func (f *filesystemDefaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
908 validatePartitionType(ctx, f)
909}
Spandan Das918191e2024-10-31 18:27:23 +0000910
911// getLibsForLinkerConfig returns
912// 1. A list of libraries installed in this filesystem
913// 2. A list of dep libraries _not_ installed in this filesystem
914//
915// `linkerconfig.BuildLinkerConfig` will convert these two to a linker.config.pb for the filesystem
916// (1) will be added to --provideLibs if they are C libraries with a stable interface (has stubs)
917// (2) will be added to --requireLibs if they are C libraries with a stable interface (has stubs)
918func (f *filesystem) getLibsForLinkerConfig(ctx android.ModuleContext) ([]android.Module, []android.Module) {
919 // we need "Module"s for packaging items
920 modulesInPackageByModule := make(map[android.Module]bool)
921 modulesInPackageByName := make(map[string]bool)
922
923 deps := f.gatherFilteredPackagingSpecs(ctx)
924 ctx.WalkDeps(func(child, parent android.Module) bool {
925 for _, ps := range android.OtherModuleProviderOrDefault(
926 ctx, child, android.InstallFilesProvider).PackagingSpecs {
927 if _, ok := deps[ps.RelPathInPackage()]; ok {
928 modulesInPackageByModule[child] = true
929 modulesInPackageByName[child.Name()] = true
930 return true
931 }
932 }
933 return true
934 })
935
936 provideModules := make([]android.Module, 0, len(modulesInPackageByModule))
937 for mod := range modulesInPackageByModule {
938 provideModules = append(provideModules, mod)
939 }
940
941 var requireModules []android.Module
942 ctx.WalkDeps(func(child, parent android.Module) bool {
943 _, parentInPackage := modulesInPackageByModule[parent]
944 _, childInPackageName := modulesInPackageByName[child.Name()]
945
946 // When parent is in the package, and child (or its variant) is not, this can be from an interface.
947 if parentInPackage && !childInPackageName {
948 requireModules = append(requireModules, child)
949 }
950 return true
951 })
952
953 return provideModules, requireModules
954}