blob: 17209ed4aecb4717a96ead5039c98cf5d37ed725 [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"
Cole Faust1dcf9e42025-02-19 17:23:34 -080020 "path/filepath"
Jihoon Kangabec3ec2025-02-19 00:55:10 +000021 "slices"
Spandan Das258c08f2025-01-08 23:30:45 +000022 "strings"
Cole Faustb55a41c2025-01-09 16:53:58 -080023 "sync/atomic"
Spandan Das258c08f2025-01-08 23:30:45 +000024
Jihoon Kangf2c53982024-10-09 17:32:52 +000025 "android/soong/android"
Cole Faust1dcf9e42025-02-19 17:23:34 -080026 "android/soong/java"
Jihoon Kangf2c53982024-10-09 17:32:52 +000027
28 "github.com/google/blueprint"
29 "github.com/google/blueprint/proptools"
30)
31
Cole Faust1dcf9e42025-02-19 17:23:34 -080032var proguardDictToProto = pctx.AndroidStaticRule("proguard_dict_to_proto", blueprint.RuleParams{
33 Command: `${symbols_map} -r8 $in -location $location -write_if_changed $out`,
34 Restat: true,
35 CommandDeps: []string{"${symbols_map}"},
36}, "location")
37
Jihoon Kangf2c53982024-10-09 17:32:52 +000038type PartitionNameProperties struct {
Cole Faust2bdc5e52025-01-10 10:29:36 -080039 // Name of the super partition filesystem module
40 Super_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000041 // Name of the boot partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000042 Boot_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000043 // Name of the vendor boot partition filesystem module
44 Vendor_boot_partition_name *string
45 // Name of the init boot partition filesystem module
46 Init_boot_partition_name *string
47 // Name of the system partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000048 System_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000049 // Name of the system_ext partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000050 System_ext_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000051 // Name of the product partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000052 Product_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000053 // Name of the vendor partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000054 Vendor_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000055 // Name of the odm partition filesystem module
Spandan Dasc5717162024-11-01 18:33:57 +000056 Odm_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000057 // Name of the recovery partition filesystem module
58 Recovery_partition_name *string
Cole Faust3552eb62024-11-06 18:07:26 -080059 // The vbmeta partition and its "chained" partitions
60 Vbmeta_partitions []string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000061 // Name of the userdata partition filesystem module
mrziwang23ba8762024-11-07 16:21:53 -080062 Userdata_partition_name *string
Spandan Dasa0394002025-01-07 18:38:34 +000063 // Name of the system_dlkm partition filesystem module
64 System_dlkm_partition_name *string
65 // Name of the vendor_dlkm partition filesystem module
66 Vendor_dlkm_partition_name *string
67 // Name of the odm_dlkm partition filesystem module
68 Odm_dlkm_partition_name *string
Jihoon Kangf2c53982024-10-09 17:32:52 +000069}
70
Jihoon Kang3be17162025-01-09 20:51:54 +000071type DeviceProperties struct {
72 // Path to the prebuilt bootloader that would be copied to PRODUCT_OUT
73 Bootloader *string `android:"path"`
Spandan Dase51ff952025-01-09 18:11:59 +000074 // Path to android-info.txt file containing board specific info.
75 Android_info *string `android:"path"`
Cole Faust11fda332025-01-14 16:47:19 -080076 // If this is the "main" android_device target for the build, i.e. the one that gets built
77 // when running a plain `m` command. Currently, this is the autogenerated android_device module
78 // in soong-only builds, but in the future when we check in android_device modules, the main
79 // one will be determined based on the lunch product. TODO: Figure out how to make this
80 // blueprint:"mutated" and still set it from filesystem_creator
81 Main_device *bool
Spandan Das29d44882025-01-15 21:12:36 +000082
Spandan Das00948072025-02-12 19:36:03 +000083 Ab_ota_updater *bool
84 Ab_ota_partitions []string
85 Ab_ota_keys []string
86 Ab_ota_postinstall_config []string
Spandan Das75955b12025-02-13 22:12:52 +000087
Spandan Das37240d92025-02-14 00:18:41 +000088 Ramdisk_node_list *string `android:"path"`
89 Releasetools_extension *string `android:"path"`
Spandan Das3dfa17f2025-02-28 09:48:28 +000090 FastbootInfo *string `android:"path"`
Cole Faust21c11462025-03-03 14:13:17 -080091
92 // The kernel version in the build. Will be verified against the actual kernel.
93 // If not provided, will attempt to extract it from the loose kernel or the kernel inside
94 // the boot image. The version is later used to decide whether or not to enable uffd_gc
95 // when dexpreopting apps. So setting this doesn't really do anything except enforce that the
96 // actual kernel version is as specified here.
97 Kernel_version *string
Jihoon Kang3be17162025-01-09 20:51:54 +000098}
99
Jihoon Kangf2c53982024-10-09 17:32:52 +0000100type androidDevice struct {
101 android.ModuleBase
102
103 partitionProps PartitionNameProperties
Jihoon Kang3be17162025-01-09 20:51:54 +0000104
105 deviceProps DeviceProperties
Cole Fausta472a6f2025-02-10 16:10:04 -0800106
107 allImagesZip android.Path
Cole Faust54124c82025-02-21 14:46:02 -0800108
Spandan Dase06dd112025-03-05 01:29:04 +0000109 proguardDictZip android.Path
110 proguardDictMapping android.Path
111 proguardUsageZip android.Path
112 kernelConfig android.Path
113 kernelVersion android.Path
114 miscInfo android.Path
115 rootDirForFsConfig string
116 rootDirForFsConfigTimestamp android.Path
Spandan Das38afe712025-03-05 23:27:55 +0000117 apkCertsInfo android.Path
Jihoon Kangf2c53982024-10-09 17:32:52 +0000118}
119
120func AndroidDeviceFactory() android.Module {
121 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +0000122 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -0800123 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000124 return module
125}
126
Cole Faust11fda332025-01-14 16:47:19 -0800127var numMainAndroidDevicesOnceKey android.OnceKey = android.NewOnceKey("num_auto_generated_anroid_devices")
Cole Faustb55a41c2025-01-09 16:53:58 -0800128
Jihoon Kangf2c53982024-10-09 17:32:52 +0000129type partitionDepTagType struct {
130 blueprint.BaseDependencyTag
131}
132
Cole Faust2bdc5e52025-01-10 10:29:36 -0800133type superPartitionDepTagType struct {
134 blueprint.BaseDependencyTag
135}
Spandan Das29d44882025-01-15 21:12:36 +0000136type targetFilesMetadataDepTagType struct {
137 blueprint.BaseDependencyTag
138}
Cole Faust2bdc5e52025-01-10 10:29:36 -0800139
140var superPartitionDepTag superPartitionDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000141var filesystemDepTag partitionDepTagType
Spandan Das29d44882025-01-15 21:12:36 +0000142var targetFilesMetadataDepTag targetFilesMetadataDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000143
144func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
145 addDependencyIfDefined := func(dep *string) {
146 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -0800147 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +0000148 }
149 }
150
Cole Faust2bdc5e52025-01-10 10:29:36 -0800151 if a.partitionProps.Super_partition_name != nil {
152 ctx.AddDependency(ctx.Module(), superPartitionDepTag, *a.partitionProps.Super_partition_name)
153 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000154 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +0000155 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000156 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000157 addDependencyIfDefined(a.partitionProps.System_partition_name)
158 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
159 addDependencyIfDefined(a.partitionProps.Product_partition_name)
160 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000161 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800162 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000163 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
164 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
165 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000166 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800167 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
168 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
169 }
Spandan Das29d44882025-01-15 21:12:36 +0000170 a.addDepsForTargetFilesMetadata(ctx)
171}
172
173func (a *androidDevice) addDepsForTargetFilesMetadata(ctx android.BottomUpMutatorContext) {
174 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), targetFilesMetadataDepTag, "liblz4") // host variant
Jihoon Kangf2c53982024-10-09 17:32:52 +0000175}
176
Cole Faust11fda332025-01-14 16:47:19 -0800177func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
178 if proptools.Bool(a.deviceProps.Main_device) {
179 numMainAndroidDevices := ctx.Config().Once(numMainAndroidDevicesOnceKey, func() interface{} {
180 return &atomic.Int32{}
181 }).(*atomic.Int32)
182 total := numMainAndroidDevices.Add(1)
183 if total > 1 {
184 // There should only be 1 main android_device module. That one will be
185 // made the default thing to build in soong-only builds.
186 ctx.ModuleErrorf("There cannot be more than 1 main android_device module")
187 }
Jihoon Kang3be17162025-01-09 20:51:54 +0000188 }
189
Cole Faust1dcf9e42025-02-19 17:23:34 -0800190 allInstalledModules := a.allInstalledModules(ctx)
191
Spandan Das38afe712025-03-05 23:27:55 +0000192 a.apkCertsInfo = a.buildApkCertsInfo(ctx, allInstalledModules)
Spandan Dasc09353c2025-03-04 00:11:21 +0000193 a.kernelConfig, a.kernelVersion = a.extractKernelVersionAndConfigs(ctx)
Spandan Das447a0ab2025-03-04 23:10:19 +0000194 a.miscInfo = a.addMiscInfo(ctx)
Spandan Dasdad98702025-02-26 14:32:28 +0000195 a.buildTargetFilesZip(ctx, allInstalledModules)
Cole Faust1dcf9e42025-02-19 17:23:34 -0800196 a.buildProguardZips(ctx, allInstalledModules)
197
mrziwang2fd33a72025-01-08 12:22:08 -0800198 var deps []android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800199 if proptools.String(a.partitionProps.Super_partition_name) != "" {
Cole Faust19eb09d2025-01-14 13:27:00 -0800200 superImage := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Cole Faust2bdc5e52025-01-10 10:29:36 -0800201 if info, ok := android.OtherModuleProvider(ctx, superImage, SuperImageProvider); ok {
202 assertUnset := func(prop *string, propName string) {
203 if prop != nil && *prop != "" {
204 ctx.PropertyErrorf(propName, "Cannot be set because it's already part of the super image")
205 }
206 }
207 for _, subPartitionType := range android.SortedKeys(info.SubImageInfo) {
208 switch subPartitionType {
209 case "system":
210 assertUnset(a.partitionProps.System_partition_name, "system_partition_name")
211 case "system_ext":
212 assertUnset(a.partitionProps.System_ext_partition_name, "system_ext_partition_name")
213 case "system_dlkm":
214 assertUnset(a.partitionProps.System_dlkm_partition_name, "system_dlkm_partition_name")
215 case "system_other":
216 // TODO
217 case "product":
218 assertUnset(a.partitionProps.Product_partition_name, "product_partition_name")
219 case "vendor":
220 assertUnset(a.partitionProps.Vendor_partition_name, "vendor_partition_name")
221 case "vendor_dlkm":
222 assertUnset(a.partitionProps.Vendor_dlkm_partition_name, "vendor_dlkm_partition_name")
223 case "odm":
224 assertUnset(a.partitionProps.Odm_partition_name, "odm_partition_name")
225 case "odm_dlkm":
226 assertUnset(a.partitionProps.Odm_dlkm_partition_name, "odm_dlkm_partition_name")
227 default:
228 ctx.ModuleErrorf("Unsupported sub-partition of super partition: %q", subPartitionType)
229 }
230 }
231
232 deps = append(deps, info.SuperImage)
233 } else {
234 ctx.ModuleErrorf("Expected super image dep to provide SuperImageProvider")
235 }
236 }
Cole Faust19eb09d2025-01-14 13:27:00 -0800237 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(m android.ModuleProxy) {
mrziwang2fd33a72025-01-08 12:22:08 -0800238 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
239 if !ok {
240 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
241 }
242 if len(imageOutput.DefaultOutputFiles) != 1 {
243 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
244 }
245 deps = append(deps, imageOutput.DefaultOutputFiles[0])
246 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000247
Cole Fausta472a6f2025-02-10 16:10:04 -0800248 allImagesZip := android.PathForModuleOut(ctx, "all_images.zip")
249 allImagesZipBuilder := android.NewRuleBuilder(pctx, ctx)
Cole Faust8967d752025-02-19 17:27:29 -0800250 cmd := allImagesZipBuilder.Command().BuiltTool("soong_zip")
Cole Fausta472a6f2025-02-10 16:10:04 -0800251 for _, dep := range deps {
252 cmd.FlagWithArg("-e ", dep.Base())
253 cmd.FlagWithInput("-f ", dep)
254 }
255 cmd.FlagWithOutput("-o ", allImagesZip)
256 allImagesZipBuilder.Build("soong_all_images_zip", "all_images.zip")
257 a.allImagesZip = allImagesZip
258
Cole Faustb55a41c2025-01-09 16:53:58 -0800259 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
Cole Faust11fda332025-01-14 16:47:19 -0800260 var validations android.Paths
261 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Cole Faustb55a41c2025-01-09 16:53:58 -0800262 // In soong-only builds, build this module by default.
263 // This is the analogue to this make code:
264 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
265 ctx.Phony("droidcore-unbundled", allImagesStamp)
Cole Faust11fda332025-01-14 16:47:19 -0800266
Cole Faust19fbb072025-01-30 18:19:29 -0800267 deps = append(deps, a.copyFilesToProductOutForSoongOnly(ctx))
Cole Faustb55a41c2025-01-09 16:53:58 -0800268 }
Cole Faust11fda332025-01-14 16:47:19 -0800269
270 ctx.Build(pctx, android.BuildParams{
271 Rule: android.Touch,
272 Output: allImagesStamp,
273 Implicits: deps,
274 Validations: validations,
275 })
276
277 // Checkbuilding it causes soong to make a phony, so you can say `m <module name>`
278 ctx.CheckbuildFile(allImagesStamp)
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000279
280 a.setVbmetaPhonyTargets(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000281
282 a.distFiles(ctx)
283}
284
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000285// Returns a list of modules that are installed, which are collected from the dependency
286// filesystem and super_image modules.
287func (a *androidDevice) allInstalledModules(ctx android.ModuleContext) []android.Module {
288 fsInfoMap := a.getFsInfos(ctx)
289 allOwners := make(map[string][]string)
290 for _, partition := range android.SortedKeys(fsInfoMap) {
291 fsInfo := fsInfoMap[partition]
292 for _, owner := range fsInfo.Owners {
293 allOwners[owner.Name] = append(allOwners[owner.Name], owner.Variation)
294 }
295 }
296
297 ret := []android.Module{}
298 ctx.WalkDepsProxy(func(mod, _ android.ModuleProxy) bool {
Spandan Das5c78fe92025-02-26 14:25:56 +0000299 if variations, ok := allOwners[ctx.OtherModuleName(mod)]; ok && android.InList(ctx.OtherModuleSubDir(mod), variations) {
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000300 ret = append(ret, mod)
301 }
302 return true
303 })
304
305 // Remove duplicates
306 ret = android.FirstUniqueFunc(ret, func(a, b android.Module) bool {
307 return a.String() == b.String()
308 })
309
310 // Sort the modules by their names and variants
311 slices.SortFunc(ret, func(a, b android.Module) int {
312 return cmp.Compare(a.String(), b.String())
313 })
314 return ret
315}
316
Cole Faust54124c82025-02-21 14:46:02 -0800317func insertBeforeExtension(file, insertion string) string {
318 ext := filepath.Ext(file)
319 return strings.TrimSuffix(file, ext) + insertion + ext
320}
321
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000322func (a *androidDevice) distFiles(ctx android.ModuleContext) {
Cole Faust54124c82025-02-21 14:46:02 -0800323 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
324 fsInfoMap := a.getFsInfos(ctx)
325 for _, partition := range android.SortedKeys(fsInfoMap) {
326 fsInfo := fsInfoMap[partition]
327 if fsInfo.InstalledFiles.Json != nil {
328 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Json)
329 }
330 if fsInfo.InstalledFiles.Txt != nil {
331 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Txt)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000332 }
333 }
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000334
Cole Faust54124c82025-02-21 14:46:02 -0800335 namePrefix := ""
336 if ctx.Config().HasDeviceProduct() {
337 namePrefix = ctx.Config().DeviceProduct() + "-"
338 }
339 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardDictZip, namePrefix+insertBeforeExtension(a.proguardDictZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
340 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardDictMapping, namePrefix+insertBeforeExtension(a.proguardDictMapping.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
341 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardUsageZip, namePrefix+insertBeforeExtension(a.proguardUsageZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
Cole Faustd8d570c2025-03-04 14:20:16 -0800342
343 if a.deviceProps.Android_info != nil {
344 ctx.DistForGoal("droidcore-unbundled", android.PathForModuleSrc(ctx, *a.deviceProps.Android_info))
345 }
Cole Faust54124c82025-02-21 14:46:02 -0800346 }
Cole Faust44080412024-12-20 14:17:07 -0800347}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000348
Yu Liu2a815b62025-02-21 20:46:25 +0000349func (a *androidDevice) MakeVars(_ android.MakeVarsModuleContext) []android.ModuleMakeVarsValue {
Cole Fausta472a6f2025-02-10 16:10:04 -0800350 if proptools.Bool(a.deviceProps.Main_device) {
Yu Liu2a815b62025-02-21 20:46:25 +0000351 return []android.ModuleMakeVarsValue{{"SOONG_ONLY_ALL_IMAGES_ZIP", a.allImagesZip.String()}}
Cole Fausta472a6f2025-02-10 16:10:04 -0800352 }
Yu Liu2a815b62025-02-21 20:46:25 +0000353 return nil
Cole Fausta472a6f2025-02-10 16:10:04 -0800354}
355
Cole Faust1dcf9e42025-02-19 17:23:34 -0800356func (a *androidDevice) buildProguardZips(ctx android.ModuleContext, allInstalledModules []android.Module) {
357 dictZip := android.PathForModuleOut(ctx, "proguard-dict.zip")
358 dictZipBuilder := android.NewRuleBuilder(pctx, ctx)
359 dictZipCmd := dictZipBuilder.Command().BuiltTool("soong_zip").Flag("-d").FlagWithOutput("-o ", dictZip)
360
361 dictMapping := android.PathForModuleOut(ctx, "proguard-dict-mapping.textproto")
362 dictMappingBuilder := android.NewRuleBuilder(pctx, ctx)
363 dictMappingCmd := dictMappingBuilder.Command().BuiltTool("symbols_map").Flag("-merge").Output(dictMapping)
364
365 protosDir := android.PathForModuleOut(ctx, "proguard_mapping_protos")
366
367 usageZip := android.PathForModuleOut(ctx, "proguard-usage.zip")
368 usageZipBuilder := android.NewRuleBuilder(pctx, ctx)
369 usageZipCmd := usageZipBuilder.Command().BuiltTool("merge_zips").Output(usageZip)
370
371 for _, mod := range allInstalledModules {
372 if proguardInfo, ok := android.OtherModuleProvider(ctx, mod, java.ProguardProvider); ok {
373 // Maintain these out/target/common paths for backwards compatibility. They may be able
374 // to be changed if tools look up file locations from the protobuf, but I'm not
375 // exactly sure how that works.
376 dictionaryFakePath := fmt.Sprintf("out/target/common/obj/%s/%s_intermediates/proguard_dictionary", proguardInfo.Class, proguardInfo.ModuleName)
377 dictZipCmd.FlagWithArg("-e ", dictionaryFakePath)
378 dictZipCmd.FlagWithInput("-f ", proguardInfo.ProguardDictionary)
379 dictZipCmd.Textf("-e out/target/common/obj/%s/%s_intermediates/classes.jar", proguardInfo.Class, proguardInfo.ModuleName)
380 dictZipCmd.FlagWithInput("-f ", proguardInfo.ClassesJar)
381
382 protoFile := protosDir.Join(ctx, filepath.Dir(dictionaryFakePath), "proguard_dictionary.textproto")
383 ctx.Build(pctx, android.BuildParams{
384 Rule: proguardDictToProto,
385 Input: proguardInfo.ProguardDictionary,
386 Output: protoFile,
387 Args: map[string]string{
388 "location": dictionaryFakePath,
389 },
390 })
391 dictMappingCmd.Input(protoFile)
392
393 usageZipCmd.Input(proguardInfo.ProguardUsageZip)
394 }
395 }
396
397 dictZipBuilder.Build("proguard_dict_zip", "Building proguard dictionary zip")
398 dictMappingBuilder.Build("proguard_dict_mapping_proto", "Building proguard mapping proto")
399 usageZipBuilder.Build("proguard_usage_zip", "Building proguard usage zip")
Cole Faust54124c82025-02-21 14:46:02 -0800400
401 a.proguardDictZip = dictZip
402 a.proguardDictMapping = dictMapping
403 a.proguardUsageZip = usageZip
Cole Faust1dcf9e42025-02-19 17:23:34 -0800404}
405
Spandan Dasef775742025-01-13 22:17:40 +0000406// Helper structs for target_files.zip creation
Spandan Dasef200ac2025-01-08 01:42:45 +0000407type targetFilesZipCopy struct {
408 srcModule *string
409 destSubdir string
410}
411
Spandan Dasef775742025-01-13 22:17:40 +0000412type targetFilesystemZipCopy struct {
413 fsInfo FilesystemInfo
414 destSubdir string
415}
416
Spandan Dasdad98702025-02-26 14:32:28 +0000417func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext, allInstalledModules []android.Module) {
Cole Faust44080412024-12-20 14:17:07 -0800418 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
419 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
420
421 builder := android.NewRuleBuilder(pctx, ctx)
422 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
423 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000424 toCopy := []targetFilesZipCopy{
425 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
426 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
427 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
428 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
429 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
430 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
431 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
432 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
433 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
434 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
435 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000436 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000437
Spandan Dasef775742025-01-13 22:17:40 +0000438 filesystemsToCopy := []targetFilesystemZipCopy{}
Spandan Dasef200ac2025-01-08 01:42:45 +0000439 for _, zipCopy := range toCopy {
440 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000441 continue
442 }
Spandan Dasef775742025-01-13 22:17:40 +0000443 filesystemsToCopy = append(
444 filesystemsToCopy,
445 targetFilesystemZipCopy{a.getFilesystemInfo(ctx, *zipCopy.srcModule), zipCopy.destSubdir},
446 )
447 }
448 // Get additional filesystems from super_partition dependency
449 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800450 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000451 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust310de662025-02-19 16:15:07 -0800452 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Dasef775742025-01-13 22:17:40 +0000453 filesystemsToCopy = append(
454 filesystemsToCopy,
455 targetFilesystemZipCopy{info.SubImageInfo[partition], strings.ToUpper(partition)},
456 )
457 }
458 } else {
459 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
460 }
461 }
462
463 for _, toCopy := range filesystemsToCopy {
464 rootDirString := toCopy.fsInfo.RootDir.String()
465 if toCopy.destSubdir == "SYSTEM" {
Spandan Dasef200ac2025-01-08 01:42:45 +0000466 rootDirString = rootDirString + "/system"
467 }
Spandan Dasef775742025-01-13 22:17:40 +0000468 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), toCopy.destSubdir)
Cole Faust44080412024-12-20 14:17:07 -0800469 builder.Command().
470 BuiltTool("acp").
Spandan Dasef775742025-01-13 22:17:40 +0000471 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, toCopy.destSubdir).
472 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
Cole Faustb36763e2025-02-18 15:21:44 -0800473 for _, extraRootDir := range toCopy.fsInfo.ExtraRootDirs {
474 builder.Command().
475 BuiltTool("acp").
476 Textf("-rd %s/. %s/%s", extraRootDir, targetFilesDir, toCopy.destSubdir).
477 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
478 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000479
Spandan Dasef775742025-01-13 22:17:40 +0000480 if toCopy.destSubdir == "SYSTEM" {
Spandan Das3ec6d062025-01-09 19:37:47 +0000481 // Create the ROOT partition in target_files.zip
Spandan Dasef775742025-01-13 22:17:40 +0000482 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", toCopy.fsInfo.RootDir, targetFilesDir.String())
Spandan Dase06dd112025-03-05 01:29:04 +0000483 // Add a duplicate rule to assemble the ROOT/ directory in separate intermediates.
484 // The output timestamp will be an input to a separate fs_config call.
485 a.rootDirForFsConfig = android.PathForModuleOut(ctx, "root_dir_for_fs_config").String()
486 rootDirBuilder := android.NewRuleBuilder(pctx, ctx)
487 rootDirForFsConfigTimestamp := android.PathForModuleOut(ctx, "root_dir_for_fs_config.timestamp")
488 rootDirBuilder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s", toCopy.fsInfo.RootDir, a.rootDirForFsConfig).
489 Implicit(toCopy.fsInfo.Output).
490 Text("&& touch").
491 Output(rootDirForFsConfigTimestamp)
492 rootDirBuilder.Build("assemble_root_dir_for_fs_config", "Assemble ROOT/ for fs_config")
493 a.rootDirForFsConfigTimestamp = rootDirForFsConfigTimestamp
Spandan Das3ec6d062025-01-09 19:37:47 +0000494 }
Cole Faust44080412024-12-20 14:17:07 -0800495 }
Spandan Das9b17df22025-01-08 23:30:45 +0000496 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000497 if a.partitionProps.Vendor_boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800498 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000499 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000500 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
501 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
502 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000503 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/VENDOR_BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000504 }
Spandan Das23511372025-01-08 23:30:45 +0000505 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000506 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/VENDOR_BOOT/vendor_bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000507 }
Spandan Das258c08f2025-01-08 23:30:45 +0000508 }
509 if a.partitionProps.Boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800510 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000511 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000512 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
513 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000514 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000515 }
516 if bootImgInfo.Kernel != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000517 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000518 // 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 +0000519 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/VENDOR_BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000520 }
Spandan Das23511372025-01-08 23:30:45 +0000521 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000522 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/BOOT/bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000523 }
Spandan Das258c08f2025-01-08 23:30:45 +0000524 }
525
Spandan Dase51ff952025-01-09 18:11:59 +0000526 if a.deviceProps.Android_info != nil {
527 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
Cole Faust11fda332025-01-14 16:47:19 -0800528 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 +0000529 }
530
Spandan Das0036fa32025-01-10 23:40:45 +0000531 a.copyImagesToTargetZip(ctx, builder, targetFilesDir)
Spandan Dasdad98702025-02-26 14:32:28 +0000532 a.copyMetadataToTargetZip(ctx, builder, targetFilesDir, allInstalledModules)
Spandan Das0036fa32025-01-10 23:40:45 +0000533
Cole Faust44080412024-12-20 14:17:07 -0800534 builder.Command().
535 BuiltTool("soong_zip").
536 Text("-d").
537 FlagWithOutput("-o ", targetFilesZip).
538 FlagWithArg("-C ", targetFilesDir.String()).
539 FlagWithArg("-D ", targetFilesDir.String()).
540 Text("-sha256")
541 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
542}
543
Spandan Das0036fa32025-01-10 23:40:45 +0000544func (a *androidDevice) copyImagesToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
545 // Create an IMAGES/ subdirectory
546 builder.Command().Textf("mkdir -p %s/IMAGES", targetFilesDir.String())
547 if a.deviceProps.Bootloader != nil {
548 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader))).Textf(" %s/IMAGES/bootloader", targetFilesDir.String())
549 }
550 // Copy the filesystem ,boot and vbmeta img files to IMAGES/
551 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(child android.ModuleProxy) {
Spandan Dasa9db76d2025-01-14 01:34:43 +0000552 if strings.Contains(child.Name(), "recovery") {
553 return // skip recovery.img to match the make packaging behavior
554 }
Spandan Dasef775742025-01-13 22:17:40 +0000555 if info, ok := android.OtherModuleProvider(ctx, child, BootimgInfoProvider); ok {
556 // Check Boot img first so that the boot.img is copied and not its dep ramdisk.img
Spandan Das0036fa32025-01-10 23:40:45 +0000557 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
Spandan Dasef775742025-01-13 22:17:40 +0000558 } else if info, ok := android.OtherModuleProvider(ctx, child, FilesystemProvider); ok {
Spandan Das0036fa32025-01-10 23:40:45 +0000559 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
560 } else if info, ok := android.OtherModuleProvider(ctx, child, vbmetaPartitionProvider); ok {
561 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
562 } else {
563 ctx.ModuleErrorf("Module %s does not provide an .img file output for target_files.zip", child.Name())
564 }
565 })
Spandan Dasef775742025-01-13 22:17:40 +0000566
567 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800568 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000569 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust19eb09d2025-01-14 13:27:00 -0800570 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000571 if info.SubImageInfo[partition].OutputHermetic != nil {
572 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].OutputHermetic).Textf(" %s/IMAGES/", targetFilesDir.String())
573 }
574 if info.SubImageInfo[partition].MapFile != nil {
575 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].MapFile).Textf(" %s/IMAGES/", targetFilesDir.String())
576 }
Spandan Dasef775742025-01-13 22:17:40 +0000577 }
578 } else {
579 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
580 }
581 }
Spandan Das0036fa32025-01-10 23:40:45 +0000582}
583
Spandan Dasdad98702025-02-26 14:32:28 +0000584func (a *androidDevice) copyMetadataToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath, allInstalledModules []android.Module) {
Spandan Das29d44882025-01-15 21:12:36 +0000585 // Create a META/ subdirectory
586 builder.Command().Textf("mkdir -p %s/META", targetFilesDir.String())
587 if proptools.Bool(a.deviceProps.Ab_ota_updater) {
588 ctx.VisitDirectDepsProxyWithTag(targetFilesMetadataDepTag, func(child android.ModuleProxy) {
589 info, _ := android.OtherModuleProvider(ctx, child, android.OutputFilesProvider)
590 builder.Command().Textf("cp").Inputs(info.DefaultOutputFiles).Textf(" %s/META/", targetFilesDir.String())
591 })
Spandan Dasb5f40cf2025-02-12 19:36:03 +0000592 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "external/zucchini/version_info.h")).Textf(" %s/META/zucchini_config.txt", targetFilesDir.String())
593 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "system/update_engine/update_engine.conf")).Textf(" %s/META/update_engine_config.txt", targetFilesDir.String())
594 if a.getFsInfos(ctx)["system"].ErofsCompressHints != nil {
595 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].ErofsCompressHints).Textf(" %s/META/erofs_default_compress_hints.txt", targetFilesDir.String())
596 }
597 // ab_partitions.txt
598 abPartitionsSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_partitions)
599 abPartitionsSortedString := proptools.ShellEscape(strings.Join(abPartitionsSorted, "\\n"))
600 builder.Command().Textf("echo -e").Flag(abPartitionsSortedString).Textf(" > %s/META/ab_partitions.txt", targetFilesDir.String())
Spandan Das35b78742025-02-12 19:36:03 +0000601 // otakeys.txt
602 abOtaKeysSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_keys)
603 abOtaKeysSortedString := proptools.ShellEscape(strings.Join(abOtaKeysSorted, "\\n"))
604 builder.Command().Textf("echo -e").Flag(abOtaKeysSortedString).Textf(" > %s/META/otakeys.txt", targetFilesDir.String())
Spandan Das00948072025-02-12 19:36:03 +0000605 // postinstall_config.txt
606 abOtaPostInstallConfigString := proptools.ShellEscape(strings.Join(a.deviceProps.Ab_ota_postinstall_config, "\\n"))
607 builder.Command().Textf("echo -e").Flag(abOtaPostInstallConfigString).Textf(" > %s/META/postinstall_config.txt", targetFilesDir.String())
Spandan Dasf12ff9b2025-02-12 22:27:43 +0000608 // selinuxfc
609 if a.getFsInfos(ctx)["system"].SelinuxFc != nil {
610 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].SelinuxFc).Textf(" %s/META/file_contexts.bin", targetFilesDir.String())
611 }
Spandan Dasd71af182025-02-12 18:03:29 +0000612 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000613 // Copy $partition_filesystem_config.txt
614 fsInfos := a.getFsInfos(ctx)
615 for _, partition := range android.SortedKeys(fsInfos) {
616 if fsInfos[partition].FilesystemConfig == nil {
617 continue
618 }
619 if android.InList(partition, []string{"userdata"}) {
620 continue
621 }
Spandan Das853a7a42025-02-27 07:44:51 +0000622 if partition != "vendor_ramdisk" {
623 // vendor_ramdisk will be handled separately.
624 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/%s", targetFilesDir.String(), a.filesystemConfigNameForTargetFiles(partition))
625 }
Spandan Das54af3572025-02-27 07:42:08 +0000626 if partition == "ramdisk" {
627 // Create an additional copy at boot_filesystem_config.txt
628 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/boot_filesystem_config.txt", targetFilesDir.String())
629 }
Spandan Das853a7a42025-02-27 07:44:51 +0000630 if partition == "system" {
631 // Create root_filesystem_config from the assembled ROOT/ intermediates directory
Spandan Dase06dd112025-03-05 01:29:04 +0000632 a.generateFilesystemConfigForTargetFiles(ctx, builder, a.rootDirForFsConfigTimestamp, targetFilesDir.String(), a.rootDirForFsConfig, "root_filesystem_config.txt")
Spandan Das853a7a42025-02-27 07:44:51 +0000633 }
634 if partition == "vendor_ramdisk" {
635 // Create vendor_boot_filesystem_config from the assembled VENDOR_BOOT/RAMDISK intermediates directory
Spandan Dase06dd112025-03-05 01:29:04 +0000636 a.generateFilesystemConfigForTargetFiles(ctx, builder, nil, targetFilesDir.String(), targetFilesDir.String()+"/VENDOR_BOOT/RAMDISK", "vendor_boot_filesystem_config.txt")
Spandan Das853a7a42025-02-27 07:44:51 +0000637 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000638 }
Spandan Das75955b12025-02-13 22:12:52 +0000639 // Copy ramdisk_node_list
Spandan Das37240d92025-02-14 00:18:41 +0000640 if ramdiskNodeList := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Ramdisk_node_list)); ramdiskNodeList != nil {
641 builder.Command().Textf("cp").Input(ramdiskNodeList).Textf(" %s/META/", targetFilesDir.String())
642 }
643 // Copy releasetools.py
644 if releaseTools := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Releasetools_extension)); releaseTools != nil {
645 builder.Command().Textf("cp").Input(releaseTools).Textf(" %s/META/", targetFilesDir.String())
646 }
Spandan Dasdad98702025-02-26 14:32:28 +0000647 // apexkeys.txt
648 var installedApexKeys []android.Path
649 for _, installedModule := range allInstalledModules {
650 if info, ok := android.OtherModuleProvider(ctx, installedModule, ApexKeyPathInfoProvider); ok {
651 installedApexKeys = append(installedApexKeys, info.ApexKeyPath)
652 }
653 }
654 installedApexKeys = android.SortedUniquePaths(installedApexKeys) // Sort by keypath to match make
655 builder.Command().Text("cat").Inputs(installedApexKeys).Textf(" >> %s/META/apexkeys.txt", targetFilesDir.String())
Spandan Das38afe712025-03-05 23:27:55 +0000656 // apkcerts.txt
657 builder.Command().Textf("cp").Input(a.apkCertsInfo).Textf(" %s/META/", targetFilesDir.String())
658
Spandan Das3dfa17f2025-02-28 09:48:28 +0000659 // Copy fastboot-info.txt
660 if fastbootInfo := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.FastbootInfo)); fastbootInfo != nil {
661 // TODO (b/399788523): Autogenerate fastboot-info.txt if there is no source fastboot-info.txt
662 // https://cs.android.com/android/_/android/platform/build/+/80b9546f8f69e78b8fe1870e0e745d70fc18dfcd:core/Makefile;l=5831-5893;drc=077490384423dff9eac954da5c001c6f0be3fa6e;bpv=0;bpt=0
663 builder.Command().Textf("cp").Input(fastbootInfo).Textf(" %s/META/fastboot-info.txt", targetFilesDir.String())
664 }
Spandan Dasdad98702025-02-26 14:32:28 +0000665
Spandan Dasc09353c2025-03-04 00:11:21 +0000666 // kernel_configs.txt and kernel_version.txt
667 if a.kernelConfig != nil {
668 builder.Command().Textf("cp").Input(a.kernelConfig).Textf(" %s/META/", targetFilesDir.String())
669 }
670 if a.kernelVersion != nil {
671 builder.Command().Textf("cp").Input(a.kernelVersion).Textf(" %s/META/", targetFilesDir.String())
672 }
Spandan Das447a0ab2025-03-04 23:10:19 +0000673 // misc_info.txt
674 if a.miscInfo != nil {
675 builder.Command().Textf("cp").Input(a.miscInfo).Textf(" %s/META/", targetFilesDir.String())
676 }
Spandan Das4100dd02025-03-04 23:14:34 +0000677 // apex_info.pb, care_map.pb, vbmeta_digest.txt
678 a.addImgToTargetFiles(ctx, builder, targetFilesDir.String())
Spandan Dasc09353c2025-03-04 00:11:21 +0000679
Spandan Dasf079f0d2025-02-27 13:13:50 +0000680 if a.partitionProps.Super_partition_name != nil {
681 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
682 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
683 // dynamic_partitions_info.txt
684 // TODO (b/390192334): Add `building_super_empty_partition=true`
685 builder.Command().Text("cp").Input(info.DynamicPartitionsInfo).Textf(" %s/META/", targetFilesDir.String())
686 } else {
687 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
688 }
689 }
690
Spandan Dasdd262fb2025-02-13 00:15:59 +0000691}
692
Spandan Das447a0ab2025-03-04 23:10:19 +0000693// A partial implementation of make's $PRODUCT_OUT/misc_info.txt
694// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=5894?q=misc_info.txt%20f:build%2Fmake%2Fcore%2FMakefile&ss=android%2Fplatform%2Fsuperproject%2Fmain
695// This file is subsequently used by add_img_to_target_files to create additioanl metadata files like apex_info.pb
696// TODO (b/399788119): Complete the migration of misc_info.txt
697func (a *androidDevice) addMiscInfo(ctx android.ModuleContext) android.Path {
698 builder := android.NewRuleBuilder(pctx, ctx)
699 miscInfo := android.PathForModuleOut(ctx, "misc_info.txt")
700 builder.Command().
701 Textf("rm -f %s", miscInfo).
702 Textf("&& echo recovery_api_version=%s >> %s", ctx.Config().VendorConfig("recovery").String("recovery_api_version"), miscInfo).
703 Textf("&& echo fstab_version=%s >> %s", ctx.Config().VendorConfig("recovery").String("recovery_fstab_version"), miscInfo).
704 ImplicitOutput(miscInfo)
705
706 if a.partitionProps.Recovery_partition_name == nil {
707 builder.Command().Textf("echo no_recovery=true >> %s", miscInfo)
708 }
709 fsInfos := a.getFsInfos(ctx)
710 for _, partition := range android.SortedKeys(fsInfos) {
711 if fsInfos[partition].UseAvb {
712 builder.Command().Textf("echo 'avb_%s_hashtree_enable=true' >> %s", partition, miscInfo)
713 }
714 }
715 if len(a.partitionProps.Vbmeta_partitions) > 0 {
716 builder.Command().
717 Textf("echo avb_enable=true >> %s", miscInfo).
718 Textf("&& echo avb_building_vbmeta_image=true >> %s", miscInfo).
719 Textf("&& echo avb_avbtool=avbtool >> %s", miscInfo)
720 }
721 if a.partitionProps.Boot_partition_name != nil {
722 builder.Command().Textf("echo boot_images=boot.img >> %s", miscInfo)
723 }
724
725 builder.Build("misc_info", "Building misc_info")
726
727 return miscInfo
728}
729
Spandan Das4100dd02025-03-04 23:14:34 +0000730// addImgToTargetFiles invokes `add_img_to_target_files` and creates the following files in META/
731// - apex_info.pb
732// - care_map.pb
733// - vbmeta_digest.txt
734func (a *androidDevice) addImgToTargetFiles(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir string) {
735 mkbootimg := ctx.Config().HostToolPath(ctx, "mkbootimg")
736 builder.Command().
737 Textf("PATH=%s:$PATH", ctx.Config().HostToolDir()).
738 Textf("MKBOOTIMG=%s", mkbootimg).
739 Implicit(mkbootimg).
740 BuiltTool("add_img_to_target_files").
741 Flag("-a -v -p").
742 Flag(ctx.Config().HostToolDir()).
743 Text(targetFilesDir)
744}
745
Spandan Dasdad98702025-02-26 14:32:28 +0000746type ApexKeyPathInfo struct {
747 ApexKeyPath android.Path
748}
749
750var ApexKeyPathInfoProvider = blueprint.NewProvider[ApexKeyPathInfo]()
751
Spandan Dase06dd112025-03-05 01:29:04 +0000752func (a *androidDevice) generateFilesystemConfigForTargetFiles(ctx android.ModuleContext, builder *android.RuleBuilder, stagingDirTimestamp android.Path, targetFilesDir, stagingDir, filename string) {
Spandan Das853a7a42025-02-27 07:44:51 +0000753 fsConfigOut := android.PathForModuleOut(ctx, filename)
754 ctx.Build(pctx, android.BuildParams{
Spandan Dase06dd112025-03-05 01:29:04 +0000755 Rule: fsConfigRule,
756 Implicit: stagingDirTimestamp,
757 Output: fsConfigOut,
Spandan Das853a7a42025-02-27 07:44:51 +0000758 Args: map[string]string{
759 "rootDir": stagingDir,
760 "prefix": "",
761 },
762 })
763 builder.Command().Textf("cp").Input(fsConfigOut).Textf(" %s/META/", targetFilesDir)
764}
765
Spandan Dasdd262fb2025-02-13 00:15:59 +0000766// Filenames for the partition specific fs_config files.
767// Hardcode the ramdisk files to their boot image prefix
768func (a *androidDevice) filesystemConfigNameForTargetFiles(partition string) string {
769 name := partition + "_filesystem_config.txt"
770 if partition == "system" {
771 name = "filesystem_config.txt"
772 } else if partition == "ramdisk" {
773 name = "init_boot_filesystem_config.txt"
774 }
775 return name
Spandan Das29d44882025-01-15 21:12:36 +0000776}
777
Cole Faust44080412024-12-20 14:17:07 -0800778func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
Cole Faust19eb09d2025-01-14 13:27:00 -0800779 fsMod := ctx.GetDirectDepProxyWithTag(depName, filesystemDepTag)
Cole Faust44080412024-12-20 14:17:07 -0800780 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
781 if !ok {
782 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
783 }
784 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000785}
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000786
787func (a *androidDevice) setVbmetaPhonyTargets(ctx android.ModuleContext) {
788 if !proptools.Bool(a.deviceProps.Main_device) {
789 return
790 }
791
792 if !ctx.Config().KatiEnabled() {
793 for _, vbmetaPartitionName := range a.partitionProps.Vbmeta_partitions {
794 img := ctx.GetDirectDepProxyWithTag(vbmetaPartitionName, filesystemDepTag)
795 if provider, ok := android.OtherModuleProvider(ctx, img, vbmetaPartitionProvider); ok {
796 // make generates `vbmetasystemimage` phony target instead of `vbmeta_systemimage` phony target.
797 partitionName := strings.ReplaceAll(provider.Name, "_", "")
798 ctx.Phony(fmt.Sprintf("%simage", partitionName), provider.Output)
799 }
800 }
801 }
802}
Cole Faust21c11462025-03-03 14:13:17 -0800803
804func (a *androidDevice) getKernel(ctx android.ModuleContext) android.Path {
805 if a.partitionProps.Boot_partition_name != nil {
806 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
807 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
808 return bootImgInfo.Kernel
809 }
810 return nil
811}
812
813// Gets the kernel version and configs from the actual kernel file itself. Roughly equivalent to
814// this make code: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=5443;drc=c0b66fc59de069e06ce0ffd703d4d21613be30c6
815// However, it is a simplified version of that make code. Differences include:
816// - Not handling BOARD_KERNEL_CONFIG_FILE because BOARD_KERNEL_CONFIG_FILE was never used.
817// - Not unpacking the bootimage, as we should be able to just always export the kernel directly
818// in the BootimgInfo. We don't currently support prebuilt boot images, but even if we add that
819// in the future, it can be done in a prebuilt_bootimage module type that still exports the same
820// BootimgInfo.
821// - We don't print a warning and output '<unknown-kernel>' to kernel_version_for_uffd_gc.txt
822// because we expect the kernel to always be present. If it's not, we will get an error that
823// kernel_version_for_uffd_gc.txt doesn't exist. This may require later tweaking to the
824// dexpreopt rules so that they don't attempt to access that file in builds that don't have
825// a kernel.
826func (a *androidDevice) extractKernelVersionAndConfigs(ctx android.ModuleContext) (android.Path, android.Path) {
827 kernel := a.getKernel(ctx)
828 // If there's no kernel, don't create kernel version / kernel config files. Reverse dependencies
829 // on those files have to account for this, for example by disabling dexpreopt in unbundled
830 // builds.
831 if kernel == nil {
832 return nil, nil
833 }
834
835 lz4tool := ctx.Config().HostToolPath(ctx, "lz4")
836
837 extractedVersionFile := android.PathForModuleOut(ctx, "kernel_version.txt")
838 extractedConfigsFile := android.PathForModuleOut(ctx, "kernel_configs.txt")
839 builder := android.NewRuleBuilder(pctx, ctx)
840 builder.Command().BuiltTool("extract_kernel").
841 Flag("--tools lz4:"+lz4tool.String()).Implicit(lz4tool).
842 FlagWithInput("--input ", kernel).
843 FlagWithOutput("--output-release ", extractedVersionFile).
844 FlagWithOutput("--output-configs ", extractedConfigsFile)
845
846 if specifiedVersion := proptools.String(a.deviceProps.Kernel_version); specifiedVersion != "" {
847 specifiedVersionFile := android.PathForModuleOut(ctx, "specified_kernel_version.txt")
848 android.WriteFileRuleVerbatim(ctx, specifiedVersionFile, specifiedVersion)
849 builder.Command().Text("diff -q").
850 Input(specifiedVersionFile).
851 Input(extractedVersionFile).
852 Textf(`|| (echo "Specified kernel version '$(cat %s)' does not match actual kernel version '$(cat %s)'"; exit 1)`, specifiedVersionFile, extractedVersionFile)
853 }
854
855 builder.Build("extract_kernel_info", "Extract kernel version and configs")
856
857 if proptools.Bool(a.deviceProps.Main_device) && !ctx.Config().KatiEnabled() {
858 if ctx.Config().EnableUffdGc() == "default" {
859 kernelVersionFile := android.PathForOutput(ctx, "dexpreopt/kernel_version_for_uffd_gc.txt")
860 ctx.Build(pctx, android.BuildParams{
861 Rule: android.CpIfChanged,
862 Input: extractedVersionFile,
863 Output: kernelVersionFile,
864 })
865 }
866
867 ctx.DistForGoal("droid_targets", extractedVersionFile)
868 }
869
870 return extractedVersionFile, extractedConfigsFile
871}
Spandan Das38afe712025-03-05 23:27:55 +0000872
873func (a *androidDevice) buildApkCertsInfo(ctx android.ModuleContext, allInstalledModules []android.Module) android.Path {
874 // TODO (spandandas): Add compressed
875 formatLine := func(cert java.Certificate, name, partition string) string {
876 pem := cert.AndroidMkString()
877 var key string
878 if cert.Key == nil {
879 key = ""
880 } else {
881 key = cert.Key.String()
882 }
883 return fmt.Sprintf(`name="%s" certificate="%s" private_key="%s" partition="%s"`, name, pem, key, partition)
884 }
885
886 apkCerts := []string{}
887 for _, installedModule := range allInstalledModules {
888 partition := ""
889 if commonInfo, ok := android.OtherModuleProvider(ctx, installedModule, android.CommonModuleInfoKey); ok {
890 partition = commonInfo.PartitionTag
891 } else {
892 ctx.ModuleErrorf("%s does not set CommonModuleInfoKey", installedModule.Name())
893 }
894 if info, ok := android.OtherModuleProvider(ctx, installedModule, java.AppInfoProvider); ok {
895 apkCerts = append(apkCerts, formatLine(info.Certificate, info.InstallApkName+".apk", partition))
896 } else if info, ok := android.OtherModuleProvider(ctx, installedModule, java.AppInfosProvider); ok {
897 for _, certInfo := range info {
898 apkCerts = append(apkCerts, formatLine(certInfo.Certificate, certInfo.InstallApkName+".apk", partition))
899 }
900 } else if info, ok := android.OtherModuleProvider(ctx, installedModule, java.RuntimeResourceOverlayInfoProvider); ok {
901 apkCerts = append(apkCerts, formatLine(info.Certificate, info.OutputFile.Base(), partition))
902 }
903 }
904 slices.Sort(apkCerts) // sort by name
905 fsInfos := a.getFsInfos(ctx)
906 if fsInfos["system"].HasFsverity {
907 defaultPem, defaultKey := ctx.Config().DefaultAppCertificate(ctx)
908 apkCerts = append(apkCerts, formatLine(java.Certificate{Pem: defaultPem, Key: defaultKey}, "BuildManifest.apk", "system"))
909 if info, ok := fsInfos["system_ext"]; ok && info.HasFsverity {
910 apkCerts = append(apkCerts, formatLine(java.Certificate{Pem: defaultPem, Key: defaultKey}, "BuildManifestSystemExt.apk", "system_ext"))
911 }
912 }
913
914 apkCertsInfo := android.PathForModuleOut(ctx, "apkcerts.txt")
915 android.WriteFileRuleVerbatim(ctx, apkCertsInfo, strings.Join(apkCerts, "\n")+"\n")
916 return apkCertsInfo
917}