blob: 959ef37e1b821eaeec803da6f0e7b108774e6c78 [file] [log] [blame]
Jihoon Kangf2c53982024-10-09 17:32:52 +00001// Copyright (C) 2024 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 (
Jihoon Kangabec3ec2025-02-19 00:55:10 +000018 "cmp"
Jihoon Kang0a6315b2025-01-30 01:14:49 +000019 "fmt"
Jihoon Kangabec3ec2025-02-19 00:55:10 +000020 "slices"
Spandan Das258c08f2025-01-08 23:30:45 +000021 "strings"
Cole Faustb55a41c2025-01-09 16:53:58 -080022 "sync/atomic"
Spandan Das258c08f2025-01-08 23:30:45 +000023
Jihoon Kangf2c53982024-10-09 17:32:52 +000024 "android/soong/android"
25
26 "github.com/google/blueprint"
27 "github.com/google/blueprint/proptools"
28)
29
30type PartitionNameProperties struct {
Cole Faust2bdc5e52025-01-10 10:29:36 -080031 // Name of the super partition filesystem module
32 Super_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000033 // Name of the boot partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000034 Boot_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000035 // Name of the vendor boot partition filesystem module
36 Vendor_boot_partition_name *string
37 // Name of the init boot partition filesystem module
38 Init_boot_partition_name *string
39 // Name of the system partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000040 System_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000041 // Name of the system_ext partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000042 System_ext_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000043 // Name of the product partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000044 Product_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000045 // Name of the vendor partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000046 Vendor_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000047 // Name of the odm partition filesystem module
Spandan Dasc5717162024-11-01 18:33:57 +000048 Odm_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000049 // Name of the recovery partition filesystem module
50 Recovery_partition_name *string
Cole Faust3552eb62024-11-06 18:07:26 -080051 // The vbmeta partition and its "chained" partitions
52 Vbmeta_partitions []string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000053 // Name of the userdata partition filesystem module
mrziwang23ba8762024-11-07 16:21:53 -080054 Userdata_partition_name *string
Spandan Dasa0394002025-01-07 18:38:34 +000055 // Name of the system_dlkm partition filesystem module
56 System_dlkm_partition_name *string
57 // Name of the vendor_dlkm partition filesystem module
58 Vendor_dlkm_partition_name *string
59 // Name of the odm_dlkm partition filesystem module
60 Odm_dlkm_partition_name *string
Jihoon Kangf2c53982024-10-09 17:32:52 +000061}
62
Jihoon Kang3be17162025-01-09 20:51:54 +000063type DeviceProperties struct {
64 // Path to the prebuilt bootloader that would be copied to PRODUCT_OUT
65 Bootloader *string `android:"path"`
Spandan Dase51ff952025-01-09 18:11:59 +000066 // Path to android-info.txt file containing board specific info.
67 Android_info *string `android:"path"`
Cole Faust11fda332025-01-14 16:47:19 -080068 // If this is the "main" android_device target for the build, i.e. the one that gets built
69 // when running a plain `m` command. Currently, this is the autogenerated android_device module
70 // in soong-only builds, but in the future when we check in android_device modules, the main
71 // one will be determined based on the lunch product. TODO: Figure out how to make this
72 // blueprint:"mutated" and still set it from filesystem_creator
73 Main_device *bool
Spandan Das29d44882025-01-15 21:12:36 +000074
Spandan Das00948072025-02-12 19:36:03 +000075 Ab_ota_updater *bool
76 Ab_ota_partitions []string
77 Ab_ota_keys []string
78 Ab_ota_postinstall_config []string
Spandan Das75955b12025-02-13 22:12:52 +000079
Spandan Das37240d92025-02-14 00:18:41 +000080 Ramdisk_node_list *string `android:"path"`
81 Releasetools_extension *string `android:"path"`
Jihoon Kang3be17162025-01-09 20:51:54 +000082}
83
Jihoon Kangf2c53982024-10-09 17:32:52 +000084type androidDevice struct {
85 android.ModuleBase
86
87 partitionProps PartitionNameProperties
Jihoon Kang3be17162025-01-09 20:51:54 +000088
89 deviceProps DeviceProperties
Cole Fausta472a6f2025-02-10 16:10:04 -080090
91 allImagesZip android.Path
Jihoon Kangf2c53982024-10-09 17:32:52 +000092}
93
94func AndroidDeviceFactory() android.Module {
95 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +000096 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -080097 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +000098 return module
99}
100
Cole Faust11fda332025-01-14 16:47:19 -0800101var numMainAndroidDevicesOnceKey android.OnceKey = android.NewOnceKey("num_auto_generated_anroid_devices")
Cole Faustb55a41c2025-01-09 16:53:58 -0800102
Jihoon Kangf2c53982024-10-09 17:32:52 +0000103type partitionDepTagType struct {
104 blueprint.BaseDependencyTag
105}
106
Cole Faust2bdc5e52025-01-10 10:29:36 -0800107type superPartitionDepTagType struct {
108 blueprint.BaseDependencyTag
109}
Spandan Das29d44882025-01-15 21:12:36 +0000110type targetFilesMetadataDepTagType struct {
111 blueprint.BaseDependencyTag
112}
Cole Faust2bdc5e52025-01-10 10:29:36 -0800113
114var superPartitionDepTag superPartitionDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000115var filesystemDepTag partitionDepTagType
Spandan Das29d44882025-01-15 21:12:36 +0000116var targetFilesMetadataDepTag targetFilesMetadataDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000117
118func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
119 addDependencyIfDefined := func(dep *string) {
120 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -0800121 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +0000122 }
123 }
124
Cole Faust2bdc5e52025-01-10 10:29:36 -0800125 if a.partitionProps.Super_partition_name != nil {
126 ctx.AddDependency(ctx.Module(), superPartitionDepTag, *a.partitionProps.Super_partition_name)
127 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000128 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +0000129 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000130 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000131 addDependencyIfDefined(a.partitionProps.System_partition_name)
132 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
133 addDependencyIfDefined(a.partitionProps.Product_partition_name)
134 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000135 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800136 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000137 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
138 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
139 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000140 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800141 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
142 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
143 }
Spandan Das29d44882025-01-15 21:12:36 +0000144 a.addDepsForTargetFilesMetadata(ctx)
145}
146
147func (a *androidDevice) addDepsForTargetFilesMetadata(ctx android.BottomUpMutatorContext) {
148 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), targetFilesMetadataDepTag, "liblz4") // host variant
Jihoon Kangf2c53982024-10-09 17:32:52 +0000149}
150
Cole Faust11fda332025-01-14 16:47:19 -0800151func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
152 if proptools.Bool(a.deviceProps.Main_device) {
153 numMainAndroidDevices := ctx.Config().Once(numMainAndroidDevicesOnceKey, func() interface{} {
154 return &atomic.Int32{}
155 }).(*atomic.Int32)
156 total := numMainAndroidDevices.Add(1)
157 if total > 1 {
158 // There should only be 1 main android_device module. That one will be
159 // made the default thing to build in soong-only builds.
160 ctx.ModuleErrorf("There cannot be more than 1 main android_device module")
161 }
Jihoon Kang3be17162025-01-09 20:51:54 +0000162 }
163
Spandan Das5ef1a9c2025-02-11 18:50:17 +0000164 a.buildTargetFilesZip(ctx)
mrziwang2fd33a72025-01-08 12:22:08 -0800165 var deps []android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800166 if proptools.String(a.partitionProps.Super_partition_name) != "" {
Cole Faust19eb09d2025-01-14 13:27:00 -0800167 superImage := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Cole Faust2bdc5e52025-01-10 10:29:36 -0800168 if info, ok := android.OtherModuleProvider(ctx, superImage, SuperImageProvider); ok {
169 assertUnset := func(prop *string, propName string) {
170 if prop != nil && *prop != "" {
171 ctx.PropertyErrorf(propName, "Cannot be set because it's already part of the super image")
172 }
173 }
174 for _, subPartitionType := range android.SortedKeys(info.SubImageInfo) {
175 switch subPartitionType {
176 case "system":
177 assertUnset(a.partitionProps.System_partition_name, "system_partition_name")
178 case "system_ext":
179 assertUnset(a.partitionProps.System_ext_partition_name, "system_ext_partition_name")
180 case "system_dlkm":
181 assertUnset(a.partitionProps.System_dlkm_partition_name, "system_dlkm_partition_name")
182 case "system_other":
183 // TODO
184 case "product":
185 assertUnset(a.partitionProps.Product_partition_name, "product_partition_name")
186 case "vendor":
187 assertUnset(a.partitionProps.Vendor_partition_name, "vendor_partition_name")
188 case "vendor_dlkm":
189 assertUnset(a.partitionProps.Vendor_dlkm_partition_name, "vendor_dlkm_partition_name")
190 case "odm":
191 assertUnset(a.partitionProps.Odm_partition_name, "odm_partition_name")
192 case "odm_dlkm":
193 assertUnset(a.partitionProps.Odm_dlkm_partition_name, "odm_dlkm_partition_name")
194 default:
195 ctx.ModuleErrorf("Unsupported sub-partition of super partition: %q", subPartitionType)
196 }
197 }
198
199 deps = append(deps, info.SuperImage)
200 } else {
201 ctx.ModuleErrorf("Expected super image dep to provide SuperImageProvider")
202 }
203 }
Cole Faust19eb09d2025-01-14 13:27:00 -0800204 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(m android.ModuleProxy) {
mrziwang2fd33a72025-01-08 12:22:08 -0800205 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
206 if !ok {
207 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
208 }
209 if len(imageOutput.DefaultOutputFiles) != 1 {
210 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
211 }
212 deps = append(deps, imageOutput.DefaultOutputFiles[0])
213 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000214
Cole Fausta472a6f2025-02-10 16:10:04 -0800215 allImagesZip := android.PathForModuleOut(ctx, "all_images.zip")
216 allImagesZipBuilder := android.NewRuleBuilder(pctx, ctx)
Cole Faust8967d752025-02-19 17:27:29 -0800217 cmd := allImagesZipBuilder.Command().BuiltTool("soong_zip")
Cole Fausta472a6f2025-02-10 16:10:04 -0800218 for _, dep := range deps {
219 cmd.FlagWithArg("-e ", dep.Base())
220 cmd.FlagWithInput("-f ", dep)
221 }
222 cmd.FlagWithOutput("-o ", allImagesZip)
223 allImagesZipBuilder.Build("soong_all_images_zip", "all_images.zip")
224 a.allImagesZip = allImagesZip
225
Cole Faustb55a41c2025-01-09 16:53:58 -0800226 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
Cole Faust11fda332025-01-14 16:47:19 -0800227 var validations android.Paths
228 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Cole Faustb55a41c2025-01-09 16:53:58 -0800229 // In soong-only builds, build this module by default.
230 // This is the analogue to this make code:
231 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
232 ctx.Phony("droidcore-unbundled", allImagesStamp)
Cole Faust11fda332025-01-14 16:47:19 -0800233
Cole Faust19fbb072025-01-30 18:19:29 -0800234 deps = append(deps, a.copyFilesToProductOutForSoongOnly(ctx))
Cole Faustb55a41c2025-01-09 16:53:58 -0800235 }
Cole Faust11fda332025-01-14 16:47:19 -0800236
237 ctx.Build(pctx, android.BuildParams{
238 Rule: android.Touch,
239 Output: allImagesStamp,
240 Implicits: deps,
241 Validations: validations,
242 })
243
244 // Checkbuilding it causes soong to make a phony, so you can say `m <module name>`
245 ctx.CheckbuildFile(allImagesStamp)
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000246
247 a.setVbmetaPhonyTargets(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000248
249 a.distFiles(ctx)
250}
251
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000252// Returns a list of modules that are installed, which are collected from the dependency
253// filesystem and super_image modules.
254func (a *androidDevice) allInstalledModules(ctx android.ModuleContext) []android.Module {
255 fsInfoMap := a.getFsInfos(ctx)
256 allOwners := make(map[string][]string)
257 for _, partition := range android.SortedKeys(fsInfoMap) {
258 fsInfo := fsInfoMap[partition]
259 for _, owner := range fsInfo.Owners {
260 allOwners[owner.Name] = append(allOwners[owner.Name], owner.Variation)
261 }
262 }
263
264 ret := []android.Module{}
265 ctx.WalkDepsProxy(func(mod, _ android.ModuleProxy) bool {
266 if variations, ok := allOwners[mod.Name()]; ok && android.InList(ctx.OtherModuleSubDir(mod), variations) {
267 ret = append(ret, mod)
268 }
269 return true
270 })
271
272 // Remove duplicates
273 ret = android.FirstUniqueFunc(ret, func(a, b android.Module) bool {
274 return a.String() == b.String()
275 })
276
277 // Sort the modules by their names and variants
278 slices.SortFunc(ret, func(a, b android.Module) int {
279 return cmp.Compare(a.String(), b.String())
280 })
281 return ret
282}
283
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000284func (a *androidDevice) distFiles(ctx android.ModuleContext) {
285 if !ctx.Config().KatiEnabled() {
286 if proptools.Bool(a.deviceProps.Main_device) {
287 fsInfoMap := a.getFsInfos(ctx)
288 for _, partition := range android.SortedKeys(fsInfoMap) {
289 fsInfo := fsInfoMap[partition]
290 if fsInfo.InstalledFiles.Json != nil {
291 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Json)
292 }
293 if fsInfo.InstalledFiles.Txt != nil {
294 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Txt)
295 }
296 }
297 }
298 }
299
Cole Faust44080412024-12-20 14:17:07 -0800300}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000301
Cole Fausta472a6f2025-02-10 16:10:04 -0800302func (a *androidDevice) MakeVars(ctx android.MakeVarsModuleContext) {
303 if proptools.Bool(a.deviceProps.Main_device) {
304 ctx.StrictRaw("SOONG_ONLY_ALL_IMAGES_ZIP", a.allImagesZip.String())
305 }
306}
307
Spandan Dasef775742025-01-13 22:17:40 +0000308// Helper structs for target_files.zip creation
Spandan Dasef200ac2025-01-08 01:42:45 +0000309type targetFilesZipCopy struct {
310 srcModule *string
311 destSubdir string
312}
313
Spandan Dasef775742025-01-13 22:17:40 +0000314type targetFilesystemZipCopy struct {
315 fsInfo FilesystemInfo
316 destSubdir string
317}
318
Cole Faust44080412024-12-20 14:17:07 -0800319func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) {
320 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
321 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
322
323 builder := android.NewRuleBuilder(pctx, ctx)
324 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
325 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000326 toCopy := []targetFilesZipCopy{
327 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
328 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
329 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
330 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
331 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
332 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
333 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
334 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
335 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
336 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
337 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000338 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000339
Spandan Dasef775742025-01-13 22:17:40 +0000340 filesystemsToCopy := []targetFilesystemZipCopy{}
Spandan Dasef200ac2025-01-08 01:42:45 +0000341 for _, zipCopy := range toCopy {
342 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000343 continue
344 }
Spandan Dasef775742025-01-13 22:17:40 +0000345 filesystemsToCopy = append(
346 filesystemsToCopy,
347 targetFilesystemZipCopy{a.getFilesystemInfo(ctx, *zipCopy.srcModule), zipCopy.destSubdir},
348 )
349 }
350 // Get additional filesystems from super_partition dependency
351 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800352 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000353 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust310de662025-02-19 16:15:07 -0800354 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Dasef775742025-01-13 22:17:40 +0000355 filesystemsToCopy = append(
356 filesystemsToCopy,
357 targetFilesystemZipCopy{info.SubImageInfo[partition], strings.ToUpper(partition)},
358 )
359 }
360 } else {
361 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
362 }
363 }
364
365 for _, toCopy := range filesystemsToCopy {
366 rootDirString := toCopy.fsInfo.RootDir.String()
367 if toCopy.destSubdir == "SYSTEM" {
Spandan Dasef200ac2025-01-08 01:42:45 +0000368 rootDirString = rootDirString + "/system"
369 }
Spandan Dasef775742025-01-13 22:17:40 +0000370 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), toCopy.destSubdir)
Cole Faust44080412024-12-20 14:17:07 -0800371 builder.Command().
372 BuiltTool("acp").
Spandan Dasef775742025-01-13 22:17:40 +0000373 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, toCopy.destSubdir).
374 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
Cole Faustb36763e2025-02-18 15:21:44 -0800375 for _, extraRootDir := range toCopy.fsInfo.ExtraRootDirs {
376 builder.Command().
377 BuiltTool("acp").
378 Textf("-rd %s/. %s/%s", extraRootDir, targetFilesDir, toCopy.destSubdir).
379 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
380 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000381
Spandan Dasef775742025-01-13 22:17:40 +0000382 if toCopy.destSubdir == "SYSTEM" {
Spandan Das3ec6d062025-01-09 19:37:47 +0000383 // Create the ROOT partition in target_files.zip
Spandan Dasef775742025-01-13 22:17:40 +0000384 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", toCopy.fsInfo.RootDir, targetFilesDir.String())
Spandan Das3ec6d062025-01-09 19:37:47 +0000385 }
Cole Faust44080412024-12-20 14:17:07 -0800386 }
Spandan Das9b17df22025-01-08 23:30:45 +0000387 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000388 if a.partitionProps.Vendor_boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800389 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000390 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000391 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
392 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
393 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000394 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/VENDOR_BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000395 }
Spandan Das23511372025-01-08 23:30:45 +0000396 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000397 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/VENDOR_BOOT/vendor_bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000398 }
Spandan Das258c08f2025-01-08 23:30:45 +0000399 }
400 if a.partitionProps.Boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800401 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000402 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000403 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
404 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000405 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000406 }
407 if bootImgInfo.Kernel != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000408 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000409 // Even though kernel is not used to build vendor_boot, copy the kernel to VENDOR_BOOT to match the behavior of make packaging.
Spandan Dasfed3d042025-01-13 21:38:47 +0000410 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/VENDOR_BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000411 }
Spandan Das23511372025-01-08 23:30:45 +0000412 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000413 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/BOOT/bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000414 }
Spandan Das258c08f2025-01-08 23:30:45 +0000415 }
416
Spandan Dase51ff952025-01-09 18:11:59 +0000417 if a.deviceProps.Android_info != nil {
418 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
Cole Faust11fda332025-01-14 16:47:19 -0800419 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, *a.deviceProps.Android_info)).Textf(" %s/OTA/android-info.txt", targetFilesDir)
Spandan Dase51ff952025-01-09 18:11:59 +0000420 }
421
Spandan Das0036fa32025-01-10 23:40:45 +0000422 a.copyImagesToTargetZip(ctx, builder, targetFilesDir)
Spandan Das29d44882025-01-15 21:12:36 +0000423 a.copyMetadataToTargetZip(ctx, builder, targetFilesDir)
Spandan Das0036fa32025-01-10 23:40:45 +0000424
Cole Faust44080412024-12-20 14:17:07 -0800425 builder.Command().
426 BuiltTool("soong_zip").
427 Text("-d").
428 FlagWithOutput("-o ", targetFilesZip).
429 FlagWithArg("-C ", targetFilesDir.String()).
430 FlagWithArg("-D ", targetFilesDir.String()).
431 Text("-sha256")
432 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
433}
434
Spandan Das0036fa32025-01-10 23:40:45 +0000435func (a *androidDevice) copyImagesToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
436 // Create an IMAGES/ subdirectory
437 builder.Command().Textf("mkdir -p %s/IMAGES", targetFilesDir.String())
438 if a.deviceProps.Bootloader != nil {
439 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader))).Textf(" %s/IMAGES/bootloader", targetFilesDir.String())
440 }
441 // Copy the filesystem ,boot and vbmeta img files to IMAGES/
442 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(child android.ModuleProxy) {
Spandan Dasa9db76d2025-01-14 01:34:43 +0000443 if strings.Contains(child.Name(), "recovery") {
444 return // skip recovery.img to match the make packaging behavior
445 }
Spandan Dasef775742025-01-13 22:17:40 +0000446 if info, ok := android.OtherModuleProvider(ctx, child, BootimgInfoProvider); ok {
447 // Check Boot img first so that the boot.img is copied and not its dep ramdisk.img
Spandan Das0036fa32025-01-10 23:40:45 +0000448 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
Spandan Dasef775742025-01-13 22:17:40 +0000449 } else if info, ok := android.OtherModuleProvider(ctx, child, FilesystemProvider); ok {
Spandan Das0036fa32025-01-10 23:40:45 +0000450 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
451 } else if info, ok := android.OtherModuleProvider(ctx, child, vbmetaPartitionProvider); ok {
452 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
453 } else {
454 ctx.ModuleErrorf("Module %s does not provide an .img file output for target_files.zip", child.Name())
455 }
456 })
Spandan Dasef775742025-01-13 22:17:40 +0000457
458 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800459 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000460 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust19eb09d2025-01-14 13:27:00 -0800461 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000462 if info.SubImageInfo[partition].OutputHermetic != nil {
463 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].OutputHermetic).Textf(" %s/IMAGES/", targetFilesDir.String())
464 }
465 if info.SubImageInfo[partition].MapFile != nil {
466 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].MapFile).Textf(" %s/IMAGES/", targetFilesDir.String())
467 }
Spandan Dasef775742025-01-13 22:17:40 +0000468 }
469 } else {
470 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
471 }
472 }
Spandan Das0036fa32025-01-10 23:40:45 +0000473}
474
Spandan Das29d44882025-01-15 21:12:36 +0000475func (a *androidDevice) copyMetadataToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
476 // Create a META/ subdirectory
477 builder.Command().Textf("mkdir -p %s/META", targetFilesDir.String())
478 if proptools.Bool(a.deviceProps.Ab_ota_updater) {
479 ctx.VisitDirectDepsProxyWithTag(targetFilesMetadataDepTag, func(child android.ModuleProxy) {
480 info, _ := android.OtherModuleProvider(ctx, child, android.OutputFilesProvider)
481 builder.Command().Textf("cp").Inputs(info.DefaultOutputFiles).Textf(" %s/META/", targetFilesDir.String())
482 })
Spandan Dasb5f40cf2025-02-12 19:36:03 +0000483 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "external/zucchini/version_info.h")).Textf(" %s/META/zucchini_config.txt", targetFilesDir.String())
484 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "system/update_engine/update_engine.conf")).Textf(" %s/META/update_engine_config.txt", targetFilesDir.String())
485 if a.getFsInfos(ctx)["system"].ErofsCompressHints != nil {
486 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].ErofsCompressHints).Textf(" %s/META/erofs_default_compress_hints.txt", targetFilesDir.String())
487 }
488 // ab_partitions.txt
489 abPartitionsSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_partitions)
490 abPartitionsSortedString := proptools.ShellEscape(strings.Join(abPartitionsSorted, "\\n"))
491 builder.Command().Textf("echo -e").Flag(abPartitionsSortedString).Textf(" > %s/META/ab_partitions.txt", targetFilesDir.String())
Spandan Das35b78742025-02-12 19:36:03 +0000492 // otakeys.txt
493 abOtaKeysSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_keys)
494 abOtaKeysSortedString := proptools.ShellEscape(strings.Join(abOtaKeysSorted, "\\n"))
495 builder.Command().Textf("echo -e").Flag(abOtaKeysSortedString).Textf(" > %s/META/otakeys.txt", targetFilesDir.String())
Spandan Das00948072025-02-12 19:36:03 +0000496 // postinstall_config.txt
497 abOtaPostInstallConfigString := proptools.ShellEscape(strings.Join(a.deviceProps.Ab_ota_postinstall_config, "\\n"))
498 builder.Command().Textf("echo -e").Flag(abOtaPostInstallConfigString).Textf(" > %s/META/postinstall_config.txt", targetFilesDir.String())
Spandan Dasf12ff9b2025-02-12 22:27:43 +0000499 // selinuxfc
500 if a.getFsInfos(ctx)["system"].SelinuxFc != nil {
501 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].SelinuxFc).Textf(" %s/META/file_contexts.bin", targetFilesDir.String())
502 }
Spandan Dasd71af182025-02-12 18:03:29 +0000503 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000504 // Copy $partition_filesystem_config.txt
505 fsInfos := a.getFsInfos(ctx)
506 for _, partition := range android.SortedKeys(fsInfos) {
507 if fsInfos[partition].FilesystemConfig == nil {
508 continue
509 }
510 if android.InList(partition, []string{"userdata"}) {
511 continue
512 }
513 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/%s", targetFilesDir.String(), a.filesystemConfigNameForTargetFiles(partition))
514 }
Spandan Das75955b12025-02-13 22:12:52 +0000515 // Copy ramdisk_node_list
Spandan Das37240d92025-02-14 00:18:41 +0000516 if ramdiskNodeList := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Ramdisk_node_list)); ramdiskNodeList != nil {
517 builder.Command().Textf("cp").Input(ramdiskNodeList).Textf(" %s/META/", targetFilesDir.String())
518 }
519 // Copy releasetools.py
520 if releaseTools := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Releasetools_extension)); releaseTools != nil {
521 builder.Command().Textf("cp").Input(releaseTools).Textf(" %s/META/", targetFilesDir.String())
522 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000523}
524
525// Filenames for the partition specific fs_config files.
526// Hardcode the ramdisk files to their boot image prefix
527func (a *androidDevice) filesystemConfigNameForTargetFiles(partition string) string {
528 name := partition + "_filesystem_config.txt"
529 if partition == "system" {
530 name = "filesystem_config.txt"
531 } else if partition == "ramdisk" {
532 name = "init_boot_filesystem_config.txt"
533 }
534 return name
Spandan Das29d44882025-01-15 21:12:36 +0000535}
536
Cole Faust44080412024-12-20 14:17:07 -0800537func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
Cole Faust19eb09d2025-01-14 13:27:00 -0800538 fsMod := ctx.GetDirectDepProxyWithTag(depName, filesystemDepTag)
Cole Faust44080412024-12-20 14:17:07 -0800539 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
540 if !ok {
541 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
542 }
543 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000544}
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000545
546func (a *androidDevice) setVbmetaPhonyTargets(ctx android.ModuleContext) {
547 if !proptools.Bool(a.deviceProps.Main_device) {
548 return
549 }
550
551 if !ctx.Config().KatiEnabled() {
552 for _, vbmetaPartitionName := range a.partitionProps.Vbmeta_partitions {
553 img := ctx.GetDirectDepProxyWithTag(vbmetaPartitionName, filesystemDepTag)
554 if provider, ok := android.OtherModuleProvider(ctx, img, vbmetaPartitionProvider); ok {
555 // make generates `vbmetasystemimage` phony target instead of `vbmeta_systemimage` phony target.
556 partitionName := strings.ReplaceAll(provider.Name, "_", "")
557 ctx.Phony(fmt.Sprintf("%simage", partitionName), provider.Output)
558 }
559 }
560 }
561}