blob: a2519426130ae461ac30952db8f70607137b7b75 [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"
Wei Li7b8455f2025-03-05 16:05:51 -080022 "sort"
Spandan Das258c08f2025-01-08 23:30:45 +000023 "strings"
Cole Faustb55a41c2025-01-09 16:53:58 -080024 "sync/atomic"
Spandan Das258c08f2025-01-08 23:30:45 +000025
Jihoon Kangf2c53982024-10-09 17:32:52 +000026 "android/soong/android"
Cole Faust1dcf9e42025-02-19 17:23:34 -080027 "android/soong/java"
Jihoon Kangf2c53982024-10-09 17:32:52 +000028
29 "github.com/google/blueprint"
30 "github.com/google/blueprint/proptools"
31)
32
Cole Faust1dcf9e42025-02-19 17:23:34 -080033var proguardDictToProto = pctx.AndroidStaticRule("proguard_dict_to_proto", blueprint.RuleParams{
34 Command: `${symbols_map} -r8 $in -location $location -write_if_changed $out`,
35 Restat: true,
36 CommandDeps: []string{"${symbols_map}"},
37}, "location")
38
Jihoon Kangf2c53982024-10-09 17:32:52 +000039type PartitionNameProperties struct {
Cole Faust2bdc5e52025-01-10 10:29:36 -080040 // Name of the super partition filesystem module
41 Super_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000042 // Name of the boot partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000043 Boot_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000044 // Name of the vendor boot partition filesystem module
45 Vendor_boot_partition_name *string
46 // Name of the init boot partition filesystem module
47 Init_boot_partition_name *string
48 // Name of the system partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000049 System_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000050 // Name of the system_ext partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000051 System_ext_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000052 // Name of the product partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000053 Product_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000054 // Name of the vendor partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000055 Vendor_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000056 // Name of the odm partition filesystem module
Spandan Dasc5717162024-11-01 18:33:57 +000057 Odm_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000058 // Name of the recovery partition filesystem module
59 Recovery_partition_name *string
Cole Faust3552eb62024-11-06 18:07:26 -080060 // The vbmeta partition and its "chained" partitions
61 Vbmeta_partitions []string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000062 // Name of the userdata partition filesystem module
mrziwang23ba8762024-11-07 16:21:53 -080063 Userdata_partition_name *string
Spandan Dasa0394002025-01-07 18:38:34 +000064 // Name of the system_dlkm partition filesystem module
65 System_dlkm_partition_name *string
66 // Name of the vendor_dlkm partition filesystem module
67 Vendor_dlkm_partition_name *string
68 // Name of the odm_dlkm partition filesystem module
69 Odm_dlkm_partition_name *string
Jihoon Kangf2c53982024-10-09 17:32:52 +000070}
71
Jihoon Kang3be17162025-01-09 20:51:54 +000072type DeviceProperties struct {
73 // Path to the prebuilt bootloader that would be copied to PRODUCT_OUT
74 Bootloader *string `android:"path"`
Spandan Dase51ff952025-01-09 18:11:59 +000075 // Path to android-info.txt file containing board specific info.
76 Android_info *string `android:"path"`
Cole Faust11fda332025-01-14 16:47:19 -080077 // If this is the "main" android_device target for the build, i.e. the one that gets built
78 // when running a plain `m` command. Currently, this is the autogenerated android_device module
79 // in soong-only builds, but in the future when we check in android_device modules, the main
80 // one will be determined based on the lunch product. TODO: Figure out how to make this
81 // blueprint:"mutated" and still set it from filesystem_creator
82 Main_device *bool
Spandan Das29d44882025-01-15 21:12:36 +000083
Spandan Das00948072025-02-12 19:36:03 +000084 Ab_ota_updater *bool
85 Ab_ota_partitions []string
86 Ab_ota_keys []string
87 Ab_ota_postinstall_config []string
Spandan Das75955b12025-02-13 22:12:52 +000088
Spandan Das37240d92025-02-14 00:18:41 +000089 Ramdisk_node_list *string `android:"path"`
90 Releasetools_extension *string `android:"path"`
Spandan Das3dfa17f2025-02-28 09:48:28 +000091 FastbootInfo *string `android:"path"`
Cole Faust21c11462025-03-03 14:13:17 -080092
Spandan Dasa85dbb22025-03-21 17:49:52 +000093 Partial_ota_update_partitions []string
94 Flash_block_size *string
95 Bootloader_in_update_package *bool
96
Cole Faust21c11462025-03-03 14:13:17 -080097 // The kernel version in the build. Will be verified against the actual kernel.
98 // If not provided, will attempt to extract it from the loose kernel or the kernel inside
99 // the boot image. The version is later used to decide whether or not to enable uffd_gc
100 // when dexpreopting apps. So setting this doesn't really do anything except enforce that the
101 // actual kernel version is as specified here.
102 Kernel_version *string
Jihoon Kang3be17162025-01-09 20:51:54 +0000103}
104
Jihoon Kangf2c53982024-10-09 17:32:52 +0000105type androidDevice struct {
106 android.ModuleBase
107
108 partitionProps PartitionNameProperties
Jihoon Kang3be17162025-01-09 20:51:54 +0000109
110 deviceProps DeviceProperties
Cole Fausta472a6f2025-02-10 16:10:04 -0800111
112 allImagesZip android.Path
Cole Faust54124c82025-02-21 14:46:02 -0800113
Spandan Dase06dd112025-03-05 01:29:04 +0000114 proguardDictZip android.Path
115 proguardDictMapping android.Path
116 proguardUsageZip android.Path
117 kernelConfig android.Path
118 kernelVersion android.Path
119 miscInfo android.Path
120 rootDirForFsConfig string
121 rootDirForFsConfigTimestamp android.Path
Spandan Das38afe712025-03-05 23:27:55 +0000122 apkCertsInfo android.Path
Spandan Das27452ec2025-03-24 17:22:55 +0000123 targetFilesZip android.Path
Jihoon Kangf2c53982024-10-09 17:32:52 +0000124}
125
126func AndroidDeviceFactory() android.Module {
127 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +0000128 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -0800129 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000130 return module
131}
132
Cole Faust11fda332025-01-14 16:47:19 -0800133var numMainAndroidDevicesOnceKey android.OnceKey = android.NewOnceKey("num_auto_generated_anroid_devices")
Cole Faustb55a41c2025-01-09 16:53:58 -0800134
Jihoon Kangf2c53982024-10-09 17:32:52 +0000135type partitionDepTagType struct {
136 blueprint.BaseDependencyTag
137}
138
Cole Faust2bdc5e52025-01-10 10:29:36 -0800139type superPartitionDepTagType struct {
140 blueprint.BaseDependencyTag
141}
Spandan Das29d44882025-01-15 21:12:36 +0000142type targetFilesMetadataDepTagType struct {
143 blueprint.BaseDependencyTag
144}
Cole Faust2bdc5e52025-01-10 10:29:36 -0800145
146var superPartitionDepTag superPartitionDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000147var filesystemDepTag partitionDepTagType
Spandan Das29d44882025-01-15 21:12:36 +0000148var targetFilesMetadataDepTag targetFilesMetadataDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000149
150func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
151 addDependencyIfDefined := func(dep *string) {
152 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -0800153 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +0000154 }
155 }
156
Cole Faust2bdc5e52025-01-10 10:29:36 -0800157 if a.partitionProps.Super_partition_name != nil {
158 ctx.AddDependency(ctx.Module(), superPartitionDepTag, *a.partitionProps.Super_partition_name)
159 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000160 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +0000161 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000162 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000163 addDependencyIfDefined(a.partitionProps.System_partition_name)
164 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
165 addDependencyIfDefined(a.partitionProps.Product_partition_name)
166 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000167 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800168 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000169 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
170 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
171 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000172 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800173 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
174 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
175 }
Spandan Das29d44882025-01-15 21:12:36 +0000176 a.addDepsForTargetFilesMetadata(ctx)
177}
178
179func (a *androidDevice) addDepsForTargetFilesMetadata(ctx android.BottomUpMutatorContext) {
180 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), targetFilesMetadataDepTag, "liblz4") // host variant
Jihoon Kangf2c53982024-10-09 17:32:52 +0000181}
182
Cole Faust11fda332025-01-14 16:47:19 -0800183func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
184 if proptools.Bool(a.deviceProps.Main_device) {
185 numMainAndroidDevices := ctx.Config().Once(numMainAndroidDevicesOnceKey, func() interface{} {
186 return &atomic.Int32{}
187 }).(*atomic.Int32)
188 total := numMainAndroidDevices.Add(1)
189 if total > 1 {
190 // There should only be 1 main android_device module. That one will be
191 // made the default thing to build in soong-only builds.
192 ctx.ModuleErrorf("There cannot be more than 1 main android_device module")
193 }
Jihoon Kang3be17162025-01-09 20:51:54 +0000194 }
195
Cole Faust1dcf9e42025-02-19 17:23:34 -0800196 allInstalledModules := a.allInstalledModules(ctx)
197
Spandan Das38afe712025-03-05 23:27:55 +0000198 a.apkCertsInfo = a.buildApkCertsInfo(ctx, allInstalledModules)
Spandan Das2dee3f72025-03-07 18:32:06 +0000199 a.kernelVersion, a.kernelConfig = a.extractKernelVersionAndConfigs(ctx)
Spandan Das447a0ab2025-03-04 23:10:19 +0000200 a.miscInfo = a.addMiscInfo(ctx)
Spandan Dasdad98702025-02-26 14:32:28 +0000201 a.buildTargetFilesZip(ctx, allInstalledModules)
Cole Faust1dcf9e42025-02-19 17:23:34 -0800202 a.buildProguardZips(ctx, allInstalledModules)
203
mrziwang2fd33a72025-01-08 12:22:08 -0800204 var deps []android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800205 if proptools.String(a.partitionProps.Super_partition_name) != "" {
Cole Faust19eb09d2025-01-14 13:27:00 -0800206 superImage := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Cole Faust2bdc5e52025-01-10 10:29:36 -0800207 if info, ok := android.OtherModuleProvider(ctx, superImage, SuperImageProvider); ok {
208 assertUnset := func(prop *string, propName string) {
209 if prop != nil && *prop != "" {
210 ctx.PropertyErrorf(propName, "Cannot be set because it's already part of the super image")
211 }
212 }
213 for _, subPartitionType := range android.SortedKeys(info.SubImageInfo) {
214 switch subPartitionType {
215 case "system":
216 assertUnset(a.partitionProps.System_partition_name, "system_partition_name")
217 case "system_ext":
218 assertUnset(a.partitionProps.System_ext_partition_name, "system_ext_partition_name")
219 case "system_dlkm":
220 assertUnset(a.partitionProps.System_dlkm_partition_name, "system_dlkm_partition_name")
221 case "system_other":
222 // TODO
223 case "product":
224 assertUnset(a.partitionProps.Product_partition_name, "product_partition_name")
225 case "vendor":
226 assertUnset(a.partitionProps.Vendor_partition_name, "vendor_partition_name")
227 case "vendor_dlkm":
228 assertUnset(a.partitionProps.Vendor_dlkm_partition_name, "vendor_dlkm_partition_name")
229 case "odm":
230 assertUnset(a.partitionProps.Odm_partition_name, "odm_partition_name")
231 case "odm_dlkm":
232 assertUnset(a.partitionProps.Odm_dlkm_partition_name, "odm_dlkm_partition_name")
233 default:
234 ctx.ModuleErrorf("Unsupported sub-partition of super partition: %q", subPartitionType)
235 }
236 }
237
238 deps = append(deps, info.SuperImage)
239 } else {
240 ctx.ModuleErrorf("Expected super image dep to provide SuperImageProvider")
241 }
242 }
Cole Faust19eb09d2025-01-14 13:27:00 -0800243 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(m android.ModuleProxy) {
mrziwang2fd33a72025-01-08 12:22:08 -0800244 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
245 if !ok {
246 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
247 }
248 if len(imageOutput.DefaultOutputFiles) != 1 {
249 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
250 }
251 deps = append(deps, imageOutput.DefaultOutputFiles[0])
252 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000253
Cole Fausta472a6f2025-02-10 16:10:04 -0800254 allImagesZip := android.PathForModuleOut(ctx, "all_images.zip")
255 allImagesZipBuilder := android.NewRuleBuilder(pctx, ctx)
Cole Faust8967d752025-02-19 17:27:29 -0800256 cmd := allImagesZipBuilder.Command().BuiltTool("soong_zip")
Cole Fausta472a6f2025-02-10 16:10:04 -0800257 for _, dep := range deps {
258 cmd.FlagWithArg("-e ", dep.Base())
259 cmd.FlagWithInput("-f ", dep)
260 }
261 cmd.FlagWithOutput("-o ", allImagesZip)
262 allImagesZipBuilder.Build("soong_all_images_zip", "all_images.zip")
263 a.allImagesZip = allImagesZip
264
Cole Faustb55a41c2025-01-09 16:53:58 -0800265 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
Cole Faust11fda332025-01-14 16:47:19 -0800266 var validations android.Paths
267 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Cole Faustb55a41c2025-01-09 16:53:58 -0800268 // In soong-only builds, build this module by default.
269 // This is the analogue to this make code:
270 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
271 ctx.Phony("droidcore-unbundled", allImagesStamp)
Cole Faust11fda332025-01-14 16:47:19 -0800272
Cole Faust19fbb072025-01-30 18:19:29 -0800273 deps = append(deps, a.copyFilesToProductOutForSoongOnly(ctx))
Cole Faustb55a41c2025-01-09 16:53:58 -0800274 }
Cole Faust11fda332025-01-14 16:47:19 -0800275
276 ctx.Build(pctx, android.BuildParams{
277 Rule: android.Touch,
278 Output: allImagesStamp,
279 Implicits: deps,
280 Validations: validations,
281 })
282
283 // Checkbuilding it causes soong to make a phony, so you can say `m <module name>`
284 ctx.CheckbuildFile(allImagesStamp)
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000285
286 a.setVbmetaPhonyTargets(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000287
288 a.distFiles(ctx)
Wei Li7b8455f2025-03-05 16:05:51 -0800289
290 android.SetProvider(ctx, android.AndroidDeviceInfoProvider, android.AndroidDeviceInfo{
291 Main_device: android.Bool(a.deviceProps.Main_device),
292 })
293
294 if proptools.String(a.partitionProps.Super_partition_name) != "" {
295 buildComplianceMetadata(ctx, superPartitionDepTag, filesystemDepTag)
296 } else {
297 buildComplianceMetadata(ctx, filesystemDepTag)
298 }
299}
300
301func buildComplianceMetadata(ctx android.ModuleContext, tags ...blueprint.DependencyTag) {
Wei Li583bc362025-03-07 14:17:16 -0800302 // Collect metadata from deps
Wei Li7b8455f2025-03-05 16:05:51 -0800303 filesContained := make([]string, 0)
Wei Li583bc362025-03-07 14:17:16 -0800304 prebuiltFilesCopied := make([]string, 0)
Wei Li7b8455f2025-03-05 16:05:51 -0800305 for _, tag := range tags {
306 ctx.VisitDirectDepsProxyWithTag(tag, func(m android.ModuleProxy) {
307 if complianceMetadataInfo, ok := android.OtherModuleProvider(ctx, m, android.ComplianceMetadataProvider); ok {
308 filesContained = append(filesContained, complianceMetadataInfo.GetFilesContained()...)
Wei Li583bc362025-03-07 14:17:16 -0800309 prebuiltFilesCopied = append(prebuiltFilesCopied, complianceMetadataInfo.GetPrebuiltFilesCopied()...)
Wei Li7b8455f2025-03-05 16:05:51 -0800310 }
311 })
312 }
Wei Li583bc362025-03-07 14:17:16 -0800313 // Merge to module's ComplianceMetadataInfo
Wei Li7b8455f2025-03-05 16:05:51 -0800314 complianceMetadataInfo := ctx.ComplianceMetadataInfo()
Wei Li583bc362025-03-07 14:17:16 -0800315 filesContained = append(filesContained, complianceMetadataInfo.GetFilesContained()...)
316 sort.Strings(filesContained)
Wei Li7b8455f2025-03-05 16:05:51 -0800317 complianceMetadataInfo.SetFilesContained(filesContained)
Wei Li583bc362025-03-07 14:17:16 -0800318
319 prebuiltFilesCopied = append(prebuiltFilesCopied, complianceMetadataInfo.GetPrebuiltFilesCopied()...)
320 sort.Strings(prebuiltFilesCopied)
321 complianceMetadataInfo.SetPrebuiltFilesCopied(prebuiltFilesCopied)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000322}
323
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000324// Returns a list of modules that are installed, which are collected from the dependency
325// filesystem and super_image modules.
326func (a *androidDevice) allInstalledModules(ctx android.ModuleContext) []android.Module {
327 fsInfoMap := a.getFsInfos(ctx)
328 allOwners := make(map[string][]string)
329 for _, partition := range android.SortedKeys(fsInfoMap) {
330 fsInfo := fsInfoMap[partition]
331 for _, owner := range fsInfo.Owners {
332 allOwners[owner.Name] = append(allOwners[owner.Name], owner.Variation)
333 }
334 }
335
336 ret := []android.Module{}
337 ctx.WalkDepsProxy(func(mod, _ android.ModuleProxy) bool {
Spandan Das5c78fe92025-02-26 14:25:56 +0000338 if variations, ok := allOwners[ctx.OtherModuleName(mod)]; ok && android.InList(ctx.OtherModuleSubDir(mod), variations) {
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000339 ret = append(ret, mod)
340 }
341 return true
342 })
343
344 // Remove duplicates
345 ret = android.FirstUniqueFunc(ret, func(a, b android.Module) bool {
346 return a.String() == b.String()
347 })
348
349 // Sort the modules by their names and variants
350 slices.SortFunc(ret, func(a, b android.Module) int {
351 return cmp.Compare(a.String(), b.String())
352 })
353 return ret
354}
355
Cole Faust54124c82025-02-21 14:46:02 -0800356func insertBeforeExtension(file, insertion string) string {
357 ext := filepath.Ext(file)
358 return strings.TrimSuffix(file, ext) + insertion + ext
359}
360
Jihoon Kangdd49f412025-03-07 01:30:43 +0000361func (a *androidDevice) distInstalledFiles(ctx android.ModuleContext) {
362 distInstalledFilesJsonAndTxt := func(installedFiles InstalledFilesStruct) {
363 if installedFiles.Json != nil {
364 ctx.DistForGoal("droidcore-unbundled", installedFiles.Json)
365 }
366 if installedFiles.Txt != nil {
367 ctx.DistForGoal("droidcore-unbundled", installedFiles.Txt)
368 }
369 }
370
371 fsInfoMap := a.getFsInfos(ctx)
372 for _, partition := range android.SortedKeys(fsInfoMap) {
373 // installed-files-*{.txt | .json} is not disted for userdata partition
374 if partition == "userdata" {
375 continue
376 }
377 fsInfo := fsInfoMap[partition]
378 for _, installedFiles := range fsInfo.InstalledFilesDepSet.ToList() {
379 distInstalledFilesJsonAndTxt(installedFiles)
380 }
381 }
382}
383
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000384func (a *androidDevice) distFiles(ctx android.ModuleContext) {
Cole Faust54124c82025-02-21 14:46:02 -0800385 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Jihoon Kangdd49f412025-03-07 01:30:43 +0000386 a.distInstalledFiles(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000387
Cole Faust54124c82025-02-21 14:46:02 -0800388 namePrefix := ""
389 if ctx.Config().HasDeviceProduct() {
390 namePrefix = ctx.Config().DeviceProduct() + "-"
391 }
392 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardDictZip, namePrefix+insertBeforeExtension(a.proguardDictZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
393 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardDictMapping, namePrefix+insertBeforeExtension(a.proguardDictMapping.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
394 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardUsageZip, namePrefix+insertBeforeExtension(a.proguardUsageZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
Cole Faustd8d570c2025-03-04 14:20:16 -0800395
396 if a.deviceProps.Android_info != nil {
397 ctx.DistForGoal("droidcore-unbundled", android.PathForModuleSrc(ctx, *a.deviceProps.Android_info))
398 }
Spandan Das9329c672025-03-21 21:04:29 +0000399 if a.miscInfo != nil {
400 ctx.DistForGoal("droidcore-unbundled", a.miscInfo)
401 if a.partitionProps.Super_partition_name != nil {
402 ctx.DistForGoalWithFilename("dist_files", a.miscInfo, "super_misc_info.txt")
403 }
404 }
Spandan Das27452ec2025-03-24 17:22:55 +0000405 if a.targetFilesZip != nil {
406 ctx.DistForGoalWithFilename("target-files-package", a.targetFilesZip, namePrefix+insertBeforeExtension(a.targetFilesZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
407 }
Spandan Das9329c672025-03-21 21:04:29 +0000408
Cole Faust54124c82025-02-21 14:46:02 -0800409 }
Cole Faust44080412024-12-20 14:17:07 -0800410}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000411
Yu Liu2a815b62025-02-21 20:46:25 +0000412func (a *androidDevice) MakeVars(_ android.MakeVarsModuleContext) []android.ModuleMakeVarsValue {
Cole Fausta472a6f2025-02-10 16:10:04 -0800413 if proptools.Bool(a.deviceProps.Main_device) {
Yu Liu2a815b62025-02-21 20:46:25 +0000414 return []android.ModuleMakeVarsValue{{"SOONG_ONLY_ALL_IMAGES_ZIP", a.allImagesZip.String()}}
Cole Fausta472a6f2025-02-10 16:10:04 -0800415 }
Yu Liu2a815b62025-02-21 20:46:25 +0000416 return nil
Cole Fausta472a6f2025-02-10 16:10:04 -0800417}
418
Cole Faust1dcf9e42025-02-19 17:23:34 -0800419func (a *androidDevice) buildProguardZips(ctx android.ModuleContext, allInstalledModules []android.Module) {
420 dictZip := android.PathForModuleOut(ctx, "proguard-dict.zip")
421 dictZipBuilder := android.NewRuleBuilder(pctx, ctx)
422 dictZipCmd := dictZipBuilder.Command().BuiltTool("soong_zip").Flag("-d").FlagWithOutput("-o ", dictZip)
423
424 dictMapping := android.PathForModuleOut(ctx, "proguard-dict-mapping.textproto")
425 dictMappingBuilder := android.NewRuleBuilder(pctx, ctx)
426 dictMappingCmd := dictMappingBuilder.Command().BuiltTool("symbols_map").Flag("-merge").Output(dictMapping)
427
428 protosDir := android.PathForModuleOut(ctx, "proguard_mapping_protos")
429
430 usageZip := android.PathForModuleOut(ctx, "proguard-usage.zip")
431 usageZipBuilder := android.NewRuleBuilder(pctx, ctx)
432 usageZipCmd := usageZipBuilder.Command().BuiltTool("merge_zips").Output(usageZip)
433
434 for _, mod := range allInstalledModules {
435 if proguardInfo, ok := android.OtherModuleProvider(ctx, mod, java.ProguardProvider); ok {
436 // Maintain these out/target/common paths for backwards compatibility. They may be able
437 // to be changed if tools look up file locations from the protobuf, but I'm not
438 // exactly sure how that works.
439 dictionaryFakePath := fmt.Sprintf("out/target/common/obj/%s/%s_intermediates/proguard_dictionary", proguardInfo.Class, proguardInfo.ModuleName)
440 dictZipCmd.FlagWithArg("-e ", dictionaryFakePath)
441 dictZipCmd.FlagWithInput("-f ", proguardInfo.ProguardDictionary)
442 dictZipCmd.Textf("-e out/target/common/obj/%s/%s_intermediates/classes.jar", proguardInfo.Class, proguardInfo.ModuleName)
443 dictZipCmd.FlagWithInput("-f ", proguardInfo.ClassesJar)
444
445 protoFile := protosDir.Join(ctx, filepath.Dir(dictionaryFakePath), "proguard_dictionary.textproto")
446 ctx.Build(pctx, android.BuildParams{
447 Rule: proguardDictToProto,
448 Input: proguardInfo.ProguardDictionary,
449 Output: protoFile,
450 Args: map[string]string{
451 "location": dictionaryFakePath,
452 },
453 })
454 dictMappingCmd.Input(protoFile)
455
456 usageZipCmd.Input(proguardInfo.ProguardUsageZip)
457 }
458 }
459
460 dictZipBuilder.Build("proguard_dict_zip", "Building proguard dictionary zip")
461 dictMappingBuilder.Build("proguard_dict_mapping_proto", "Building proguard mapping proto")
462 usageZipBuilder.Build("proguard_usage_zip", "Building proguard usage zip")
Cole Faust54124c82025-02-21 14:46:02 -0800463
464 a.proguardDictZip = dictZip
465 a.proguardDictMapping = dictMapping
466 a.proguardUsageZip = usageZip
Cole Faust1dcf9e42025-02-19 17:23:34 -0800467}
468
Spandan Dasef775742025-01-13 22:17:40 +0000469// Helper structs for target_files.zip creation
Spandan Dasef200ac2025-01-08 01:42:45 +0000470type targetFilesZipCopy struct {
471 srcModule *string
472 destSubdir string
473}
474
Spandan Dasef775742025-01-13 22:17:40 +0000475type targetFilesystemZipCopy struct {
476 fsInfo FilesystemInfo
477 destSubdir string
478}
479
Spandan Dasdad98702025-02-26 14:32:28 +0000480func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext, allInstalledModules []android.Module) {
Cole Faust44080412024-12-20 14:17:07 -0800481 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
482 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
483
484 builder := android.NewRuleBuilder(pctx, ctx)
485 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
486 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000487 toCopy := []targetFilesZipCopy{
488 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
489 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
490 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
491 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
492 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
493 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
494 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
495 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
496 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
497 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
498 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000499 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000500
Spandan Dasef775742025-01-13 22:17:40 +0000501 filesystemsToCopy := []targetFilesystemZipCopy{}
Spandan Dasef200ac2025-01-08 01:42:45 +0000502 for _, zipCopy := range toCopy {
503 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000504 continue
505 }
Spandan Dasef775742025-01-13 22:17:40 +0000506 filesystemsToCopy = append(
507 filesystemsToCopy,
508 targetFilesystemZipCopy{a.getFilesystemInfo(ctx, *zipCopy.srcModule), zipCopy.destSubdir},
509 )
510 }
511 // Get additional filesystems from super_partition dependency
512 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800513 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000514 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust310de662025-02-19 16:15:07 -0800515 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Dasef775742025-01-13 22:17:40 +0000516 filesystemsToCopy = append(
517 filesystemsToCopy,
518 targetFilesystemZipCopy{info.SubImageInfo[partition], strings.ToUpper(partition)},
519 )
520 }
521 } else {
522 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
523 }
524 }
525
526 for _, toCopy := range filesystemsToCopy {
527 rootDirString := toCopy.fsInfo.RootDir.String()
528 if toCopy.destSubdir == "SYSTEM" {
Spandan Dasef200ac2025-01-08 01:42:45 +0000529 rootDirString = rootDirString + "/system"
530 }
Spandan Dasef775742025-01-13 22:17:40 +0000531 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), toCopy.destSubdir)
Cole Faust44080412024-12-20 14:17:07 -0800532 builder.Command().
533 BuiltTool("acp").
Spandan Dasef775742025-01-13 22:17:40 +0000534 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, toCopy.destSubdir).
535 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
Cole Faustb36763e2025-02-18 15:21:44 -0800536 for _, extraRootDir := range toCopy.fsInfo.ExtraRootDirs {
537 builder.Command().
538 BuiltTool("acp").
539 Textf("-rd %s/. %s/%s", extraRootDir, targetFilesDir, toCopy.destSubdir).
540 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
541 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000542
Spandan Dasef775742025-01-13 22:17:40 +0000543 if toCopy.destSubdir == "SYSTEM" {
Spandan Das3ec6d062025-01-09 19:37:47 +0000544 // Create the ROOT partition in target_files.zip
Spandan Dasef775742025-01-13 22:17:40 +0000545 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", toCopy.fsInfo.RootDir, targetFilesDir.String())
Spandan Dase06dd112025-03-05 01:29:04 +0000546 // Add a duplicate rule to assemble the ROOT/ directory in separate intermediates.
547 // The output timestamp will be an input to a separate fs_config call.
548 a.rootDirForFsConfig = android.PathForModuleOut(ctx, "root_dir_for_fs_config").String()
549 rootDirBuilder := android.NewRuleBuilder(pctx, ctx)
550 rootDirForFsConfigTimestamp := android.PathForModuleOut(ctx, "root_dir_for_fs_config.timestamp")
551 rootDirBuilder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s", toCopy.fsInfo.RootDir, a.rootDirForFsConfig).
552 Implicit(toCopy.fsInfo.Output).
553 Text("&& touch").
554 Output(rootDirForFsConfigTimestamp)
555 rootDirBuilder.Build("assemble_root_dir_for_fs_config", "Assemble ROOT/ for fs_config")
556 a.rootDirForFsConfigTimestamp = rootDirForFsConfigTimestamp
Spandan Das3ec6d062025-01-09 19:37:47 +0000557 }
Cole Faust44080412024-12-20 14:17:07 -0800558 }
Spandan Das9b17df22025-01-08 23:30:45 +0000559 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000560 if a.partitionProps.Vendor_boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800561 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000562 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000563 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
564 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
565 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000566 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/VENDOR_BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000567 }
Spandan Das23511372025-01-08 23:30:45 +0000568 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000569 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/VENDOR_BOOT/vendor_bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000570 }
Spandan Das258c08f2025-01-08 23:30:45 +0000571 }
572 if a.partitionProps.Boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800573 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000574 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000575 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
576 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000577 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000578 }
579 if bootImgInfo.Kernel != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000580 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000581 // 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 +0000582 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/VENDOR_BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000583 }
Spandan Das23511372025-01-08 23:30:45 +0000584 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000585 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/BOOT/bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000586 }
Spandan Das258c08f2025-01-08 23:30:45 +0000587 }
588
Spandan Dase51ff952025-01-09 18:11:59 +0000589 if a.deviceProps.Android_info != nil {
590 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
Cole Faust11fda332025-01-14 16:47:19 -0800591 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 +0000592 }
593
Spandan Das0036fa32025-01-10 23:40:45 +0000594 a.copyImagesToTargetZip(ctx, builder, targetFilesDir)
Spandan Dasdad98702025-02-26 14:32:28 +0000595 a.copyMetadataToTargetZip(ctx, builder, targetFilesDir, allInstalledModules)
Spandan Das0036fa32025-01-10 23:40:45 +0000596
Spandan Das27452ec2025-03-24 17:22:55 +0000597 a.targetFilesZip = targetFilesZip
Cole Faust44080412024-12-20 14:17:07 -0800598 builder.Command().
599 BuiltTool("soong_zip").
600 Text("-d").
601 FlagWithOutput("-o ", targetFilesZip).
602 FlagWithArg("-C ", targetFilesDir.String()).
603 FlagWithArg("-D ", targetFilesDir.String()).
604 Text("-sha256")
605 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
606}
607
Spandan Das0036fa32025-01-10 23:40:45 +0000608func (a *androidDevice) copyImagesToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
609 // Create an IMAGES/ subdirectory
610 builder.Command().Textf("mkdir -p %s/IMAGES", targetFilesDir.String())
611 if a.deviceProps.Bootloader != nil {
612 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader))).Textf(" %s/IMAGES/bootloader", targetFilesDir.String())
613 }
614 // Copy the filesystem ,boot and vbmeta img files to IMAGES/
615 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(child android.ModuleProxy) {
Spandan Dasa9db76d2025-01-14 01:34:43 +0000616 if strings.Contains(child.Name(), "recovery") {
617 return // skip recovery.img to match the make packaging behavior
618 }
Spandan Dasef775742025-01-13 22:17:40 +0000619 if info, ok := android.OtherModuleProvider(ctx, child, BootimgInfoProvider); ok {
620 // Check Boot img first so that the boot.img is copied and not its dep ramdisk.img
Spandan Das0036fa32025-01-10 23:40:45 +0000621 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
Spandan Dasef775742025-01-13 22:17:40 +0000622 } else if info, ok := android.OtherModuleProvider(ctx, child, FilesystemProvider); ok {
Spandan Das0036fa32025-01-10 23:40:45 +0000623 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
624 } else if info, ok := android.OtherModuleProvider(ctx, child, vbmetaPartitionProvider); ok {
625 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
626 } else {
627 ctx.ModuleErrorf("Module %s does not provide an .img file output for target_files.zip", child.Name())
628 }
629 })
Spandan Dasef775742025-01-13 22:17:40 +0000630
631 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800632 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000633 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust19eb09d2025-01-14 13:27:00 -0800634 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000635 if info.SubImageInfo[partition].OutputHermetic != nil {
636 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].OutputHermetic).Textf(" %s/IMAGES/", targetFilesDir.String())
637 }
638 if info.SubImageInfo[partition].MapFile != nil {
639 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].MapFile).Textf(" %s/IMAGES/", targetFilesDir.String())
640 }
Spandan Dasef775742025-01-13 22:17:40 +0000641 }
Spandan Dasd73441e2025-03-10 22:34:25 +0000642 // super_empty.img
643 if info.SuperEmptyImage != nil {
644 builder.Command().Textf("cp ").Input(info.SuperEmptyImage).Textf(" %s/IMAGES/", targetFilesDir.String())
645 }
Spandan Dasef775742025-01-13 22:17:40 +0000646 } else {
647 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
648 }
649 }
Spandan Das0036fa32025-01-10 23:40:45 +0000650}
651
Spandan Dasdad98702025-02-26 14:32:28 +0000652func (a *androidDevice) copyMetadataToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath, allInstalledModules []android.Module) {
Spandan Das29d44882025-01-15 21:12:36 +0000653 // Create a META/ subdirectory
654 builder.Command().Textf("mkdir -p %s/META", targetFilesDir.String())
655 if proptools.Bool(a.deviceProps.Ab_ota_updater) {
656 ctx.VisitDirectDepsProxyWithTag(targetFilesMetadataDepTag, func(child android.ModuleProxy) {
657 info, _ := android.OtherModuleProvider(ctx, child, android.OutputFilesProvider)
658 builder.Command().Textf("cp").Inputs(info.DefaultOutputFiles).Textf(" %s/META/", targetFilesDir.String())
659 })
Spandan Dasb5f40cf2025-02-12 19:36:03 +0000660 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "external/zucchini/version_info.h")).Textf(" %s/META/zucchini_config.txt", targetFilesDir.String())
661 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "system/update_engine/update_engine.conf")).Textf(" %s/META/update_engine_config.txt", targetFilesDir.String())
662 if a.getFsInfos(ctx)["system"].ErofsCompressHints != nil {
663 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].ErofsCompressHints).Textf(" %s/META/erofs_default_compress_hints.txt", targetFilesDir.String())
664 }
665 // ab_partitions.txt
666 abPartitionsSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_partitions)
667 abPartitionsSortedString := proptools.ShellEscape(strings.Join(abPartitionsSorted, "\\n"))
668 builder.Command().Textf("echo -e").Flag(abPartitionsSortedString).Textf(" > %s/META/ab_partitions.txt", targetFilesDir.String())
Spandan Das35b78742025-02-12 19:36:03 +0000669 // otakeys.txt
670 abOtaKeysSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_keys)
671 abOtaKeysSortedString := proptools.ShellEscape(strings.Join(abOtaKeysSorted, "\\n"))
672 builder.Command().Textf("echo -e").Flag(abOtaKeysSortedString).Textf(" > %s/META/otakeys.txt", targetFilesDir.String())
Spandan Das00948072025-02-12 19:36:03 +0000673 // postinstall_config.txt
674 abOtaPostInstallConfigString := proptools.ShellEscape(strings.Join(a.deviceProps.Ab_ota_postinstall_config, "\\n"))
675 builder.Command().Textf("echo -e").Flag(abOtaPostInstallConfigString).Textf(" > %s/META/postinstall_config.txt", targetFilesDir.String())
Spandan Dasf12ff9b2025-02-12 22:27:43 +0000676 // selinuxfc
677 if a.getFsInfos(ctx)["system"].SelinuxFc != nil {
678 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].SelinuxFc).Textf(" %s/META/file_contexts.bin", targetFilesDir.String())
679 }
Spandan Dasd71af182025-02-12 18:03:29 +0000680 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000681 // Copy $partition_filesystem_config.txt
682 fsInfos := a.getFsInfos(ctx)
683 for _, partition := range android.SortedKeys(fsInfos) {
684 if fsInfos[partition].FilesystemConfig == nil {
685 continue
686 }
687 if android.InList(partition, []string{"userdata"}) {
688 continue
689 }
Spandan Das853a7a42025-02-27 07:44:51 +0000690 if partition != "vendor_ramdisk" {
691 // vendor_ramdisk will be handled separately.
692 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/%s", targetFilesDir.String(), a.filesystemConfigNameForTargetFiles(partition))
693 }
Spandan Das54af3572025-02-27 07:42:08 +0000694 if partition == "ramdisk" {
695 // Create an additional copy at boot_filesystem_config.txt
696 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/boot_filesystem_config.txt", targetFilesDir.String())
697 }
Spandan Das853a7a42025-02-27 07:44:51 +0000698 if partition == "system" {
699 // Create root_filesystem_config from the assembled ROOT/ intermediates directory
Spandan Dase06dd112025-03-05 01:29:04 +0000700 a.generateFilesystemConfigForTargetFiles(ctx, builder, a.rootDirForFsConfigTimestamp, targetFilesDir.String(), a.rootDirForFsConfig, "root_filesystem_config.txt")
Spandan Das853a7a42025-02-27 07:44:51 +0000701 }
702 if partition == "vendor_ramdisk" {
703 // Create vendor_boot_filesystem_config from the assembled VENDOR_BOOT/RAMDISK intermediates directory
Spandan Dasf7b7cc32025-03-07 23:09:29 +0000704 vendorRamdiskStagingDir := targetFilesDir.String() + "/VENDOR_BOOT/RAMDISK"
705 vendorRamdiskFsConfigOut := targetFilesDir.String() + "/META/vendor_boot_filesystem_config.txt"
706 fsConfigBin := ctx.Config().HostToolPath(ctx, "fs_config")
707 builder.Command().Textf(
708 `(cd %s; find . -type d | sed 's,$,/,'; find . \! -type d) | cut -c 3- | sort | sed 's,^,,' | %s -C -D %s -R \"\" > %s`,
709 vendorRamdiskStagingDir, fsConfigBin, vendorRamdiskStagingDir, vendorRamdiskFsConfigOut).
710 Implicit(fsConfigBin)
Spandan Das853a7a42025-02-27 07:44:51 +0000711 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000712 }
Spandan Das75955b12025-02-13 22:12:52 +0000713 // Copy ramdisk_node_list
Spandan Das37240d92025-02-14 00:18:41 +0000714 if ramdiskNodeList := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Ramdisk_node_list)); ramdiskNodeList != nil {
715 builder.Command().Textf("cp").Input(ramdiskNodeList).Textf(" %s/META/", targetFilesDir.String())
716 }
717 // Copy releasetools.py
718 if releaseTools := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Releasetools_extension)); releaseTools != nil {
719 builder.Command().Textf("cp").Input(releaseTools).Textf(" %s/META/", targetFilesDir.String())
720 }
Spandan Dasdad98702025-02-26 14:32:28 +0000721 // apexkeys.txt
722 var installedApexKeys []android.Path
723 for _, installedModule := range allInstalledModules {
724 if info, ok := android.OtherModuleProvider(ctx, installedModule, ApexKeyPathInfoProvider); ok {
725 installedApexKeys = append(installedApexKeys, info.ApexKeyPath)
726 }
727 }
728 installedApexKeys = android.SortedUniquePaths(installedApexKeys) // Sort by keypath to match make
729 builder.Command().Text("cat").Inputs(installedApexKeys).Textf(" >> %s/META/apexkeys.txt", targetFilesDir.String())
Spandan Das38afe712025-03-05 23:27:55 +0000730 // apkcerts.txt
731 builder.Command().Textf("cp").Input(a.apkCertsInfo).Textf(" %s/META/", targetFilesDir.String())
732
Spandan Das3dfa17f2025-02-28 09:48:28 +0000733 // Copy fastboot-info.txt
734 if fastbootInfo := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.FastbootInfo)); fastbootInfo != nil {
735 // TODO (b/399788523): Autogenerate fastboot-info.txt if there is no source fastboot-info.txt
736 // https://cs.android.com/android/_/android/platform/build/+/80b9546f8f69e78b8fe1870e0e745d70fc18dfcd:core/Makefile;l=5831-5893;drc=077490384423dff9eac954da5c001c6f0be3fa6e;bpv=0;bpt=0
737 builder.Command().Textf("cp").Input(fastbootInfo).Textf(" %s/META/fastboot-info.txt", targetFilesDir.String())
738 }
Spandan Dasdad98702025-02-26 14:32:28 +0000739
Spandan Dasc09353c2025-03-04 00:11:21 +0000740 // kernel_configs.txt and kernel_version.txt
741 if a.kernelConfig != nil {
742 builder.Command().Textf("cp").Input(a.kernelConfig).Textf(" %s/META/", targetFilesDir.String())
743 }
744 if a.kernelVersion != nil {
745 builder.Command().Textf("cp").Input(a.kernelVersion).Textf(" %s/META/", targetFilesDir.String())
746 }
Spandan Das447a0ab2025-03-04 23:10:19 +0000747 // misc_info.txt
748 if a.miscInfo != nil {
749 builder.Command().Textf("cp").Input(a.miscInfo).Textf(" %s/META/", targetFilesDir.String())
750 }
Spandan Das4100dd02025-03-04 23:14:34 +0000751 // apex_info.pb, care_map.pb, vbmeta_digest.txt
752 a.addImgToTargetFiles(ctx, builder, targetFilesDir.String())
Spandan Dasc09353c2025-03-04 00:11:21 +0000753
Spandan Dasf079f0d2025-02-27 13:13:50 +0000754 if a.partitionProps.Super_partition_name != nil {
755 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
756 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
757 // dynamic_partitions_info.txt
758 // TODO (b/390192334): Add `building_super_empty_partition=true`
759 builder.Command().Text("cp").Input(info.DynamicPartitionsInfo).Textf(" %s/META/", targetFilesDir.String())
760 } else {
761 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
762 }
763 }
764
Spandan Dasdd262fb2025-02-13 00:15:59 +0000765}
766
Spandan Das8d1d4e02025-03-18 20:07:51 +0000767var (
768 // https://cs.android.com/android/_/android/platform/build/+/30f05352c3e6f4333c77d4af66c253572d3ea6c9:core/Makefile;l=2111-2120;drc=519f75666431ee2926e0ec8991c682b28a4c9521;bpv=1;bpt=0
769 defaultTargetRecoveryFstypeMountOptions = "ext4=max_batch_time=0,commit=1,data=ordered,barrier=1,errors=panic,nodelalloc"
770)
771
Spandan Das447a0ab2025-03-04 23:10:19 +0000772// A partial implementation of make's $PRODUCT_OUT/misc_info.txt
773// 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
774// This file is subsequently used by add_img_to_target_files to create additioanl metadata files like apex_info.pb
775// TODO (b/399788119): Complete the migration of misc_info.txt
776func (a *androidDevice) addMiscInfo(ctx android.ModuleContext) android.Path {
Spandan Dasbb15b742025-03-18 03:02:03 +0000777 buildType := func() string {
778 if ctx.Config().Debuggable() {
779 return "userdebug"
780 } else if ctx.Config().Eng() {
781 return "eng"
782 } else {
783 return "user"
784 }
785 }
786 defaultAppCertificate := func() string {
787 pem, _ := ctx.Config().DefaultAppCertificate(ctx)
788 return strings.TrimSuffix(pem.String(), ".x509.pem")
789 }
790
Spandan Das447a0ab2025-03-04 23:10:19 +0000791 builder := android.NewRuleBuilder(pctx, ctx)
792 miscInfo := android.PathForModuleOut(ctx, "misc_info.txt")
793 builder.Command().
794 Textf("rm -f %s", miscInfo).
795 Textf("&& echo recovery_api_version=%s >> %s", ctx.Config().VendorConfig("recovery").String("recovery_api_version"), miscInfo).
796 Textf("&& echo fstab_version=%s >> %s", ctx.Config().VendorConfig("recovery").String("recovery_fstab_version"), miscInfo).
Spandan Dasbb15b742025-03-18 03:02:03 +0000797 Textf("&& echo build_type=%s >> %s", buildType(), miscInfo).
798 Textf("&& echo default_system_dev_certificate=%s >> %s", defaultAppCertificate(), miscInfo).
Spandan Das8d1d4e02025-03-18 20:07:51 +0000799 Textf("&& echo root_dir=%s >> %s", android.PathForModuleInPartitionInstall(ctx, "root"), miscInfo).
Spandan Das447a0ab2025-03-04 23:10:19 +0000800 ImplicitOutput(miscInfo)
Spandan Das65bcd972025-03-18 00:14:47 +0000801 if len(ctx.Config().ExtraOtaRecoveryKeys()) > 0 {
802 builder.Command().Textf(`echo "extra_recovery_keys=%s" >> %s`, strings.Join(ctx.Config().ExtraOtaRecoveryKeys(), ""), miscInfo)
803 } else {
804 if a.partitionProps.Boot_partition_name != nil {
805 builder.Command().
806 Textf("echo mkbootimg_args='--header_version %s' >> %s", a.getBootimgHeaderVersion(ctx, a.partitionProps.Boot_partition_name), miscInfo).
807 // TODO: Use boot's header version for recovery for now since cuttlefish does not set `BOARD_RECOVERY_MKBOOTIMG_ARGS`
808 Textf(" && echo recovery_mkbootimg_args='--header_version %s' >> %s", a.getBootimgHeaderVersion(ctx, a.partitionProps.Boot_partition_name), miscInfo)
809 }
810 if a.partitionProps.Init_boot_partition_name != nil {
811 builder.Command().
812 Textf("echo mkbootimg_init_args='--header_version' %s >> %s", a.getBootimgHeaderVersion(ctx, a.partitionProps.Init_boot_partition_name), miscInfo)
813 }
814 builder.Command().
815 Textf("echo mkbootimg_version_args='--os_version %s --os_patch_level %s' >> %s", ctx.Config().PlatformVersionLastStable(), ctx.Config().PlatformSecurityPatch(), miscInfo).
816 Textf(" && echo multistage_support=1 >> %s", miscInfo).
817 Textf(" && echo blockimgdiff_versions=3,4 >> %s", miscInfo)
818 }
Spandan Dasbb15b742025-03-18 03:02:03 +0000819 fsInfos := a.getFsInfos(ctx)
820 if _, ok := fsInfos["vendor"]; ok {
821 builder.Command().Textf("echo board_uses_vendorimage=true >> %s", miscInfo)
822 }
823 if fsInfos["system"].ErofsCompressHints != nil {
824 builder.Command().Textf("echo erofs_default_compress_hints=%s >> %s", fsInfos["system"].ErofsCompressHints, miscInfo)
825 }
Spandan Das8d1d4e02025-03-18 20:07:51 +0000826 if releaseTools := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Releasetools_extension)); releaseTools != nil {
827 builder.Command().Textf("echo tool_extensions=%s >> %s", filepath.Dir(releaseTools.String()), miscInfo)
828 }
829 // ramdisk uses `compressed_cpio` fs_type
830 // https://cs.android.com/android/_/android/platform/build/+/30f05352c3e6f4333c77d4af66c253572d3ea6c9:core/Makefile;l=5923-5925;drc=519f75666431ee2926e0ec8991c682b28a4c9521;bpv=1;bpt=0
831 if _, ok := fsInfos["ramdisk"]; ok {
832 builder.Command().Textf("echo lz4_ramdisks=true >> %s", miscInfo)
833 }
834 // recovery_mount_options
835 // TODO: Add support for TARGET_RECOVERY_FSTYPE_MOUNT_OPTIONS which can be used to override the default
836 builder.Command().Textf("echo recovery_mount_options=%s >> %s", defaultTargetRecoveryFstypeMountOptions, miscInfo)
Spandan Das447a0ab2025-03-04 23:10:19 +0000837
Spandan Das62eacae2025-03-18 20:37:42 +0000838 // vintf information
839 if proptools.Bool(ctx.Config().ProductVariables().Enforce_vintf_manifest) {
840 builder.Command().Textf("echo vintf_enforce=true >> %s", miscInfo)
841 }
842 if len(ctx.Config().DeviceManifestFiles()) > 0 {
843 builder.Command().Textf("echo vintf_include_empty_vendor_sku=true >> %s", miscInfo)
844 }
845
Spandan Das447a0ab2025-03-04 23:10:19 +0000846 if a.partitionProps.Recovery_partition_name == nil {
847 builder.Command().Textf("echo no_recovery=true >> %s", miscInfo)
848 }
Spandan Das447a0ab2025-03-04 23:10:19 +0000849 for _, partition := range android.SortedKeys(fsInfos) {
Spandan Das227c9492025-03-17 20:14:00 +0000850 if fsInfos[partition].PropFileForMiscInfo != nil {
851 builder.Command().Text("cat").Input(fsInfos[partition].PropFileForMiscInfo).Textf(" >> %s", miscInfo)
Spandan Das447a0ab2025-03-04 23:10:19 +0000852 }
853 }
854 if len(a.partitionProps.Vbmeta_partitions) > 0 {
855 builder.Command().
856 Textf("echo avb_enable=true >> %s", miscInfo).
857 Textf("&& echo avb_building_vbmeta_image=true >> %s", miscInfo).
858 Textf("&& echo avb_avbtool=avbtool >> %s", miscInfo)
Spandan Das1bf169a2025-03-21 16:56:06 +0000859
860 var allChainedVbmetaPartitionTypes []string
Spandan Das2e1338e2025-03-19 23:57:36 +0000861 for _, vbmetaPartitionName := range a.partitionProps.Vbmeta_partitions {
862 img := ctx.GetDirectDepProxyWithTag(vbmetaPartitionName, filesystemDepTag)
863 if provider, ok := android.OtherModuleProvider(ctx, img, vbmetaPartitionProvider); ok {
864 builder.Command().Text("cat").Input(provider.PropFileForMiscInfo).Textf(" >> %s", miscInfo)
Spandan Das1bf169a2025-03-21 16:56:06 +0000865 if provider.FilesystemPartitionType != "" { // the top-level vbmeta.img
866 allChainedVbmetaPartitionTypes = append(allChainedVbmetaPartitionTypes, provider.FilesystemPartitionType)
867 }
Spandan Das2e1338e2025-03-19 23:57:36 +0000868 } else {
869 ctx.ModuleErrorf("vbmeta dep %s does not set vbmetaPartitionProvider\n", vbmetaPartitionName)
870 }
871 }
Spandan Das1bf169a2025-03-21 16:56:06 +0000872 // Determine the custom vbmeta partitions by removing system and vendor
873 customVbmetaPartitionTypes := android.RemoveListFromList(allChainedVbmetaPartitionTypes, []string{"system", "vendor"})
874 builder.Command().Textf("echo avb_custom_vbmeta_images_partition_list=%s >> %s",
875 strings.Join(android.SortedUniqueStrings(customVbmetaPartitionTypes), " "),
876 miscInfo,
877 )
Spandan Das2e1338e2025-03-19 23:57:36 +0000878
Spandan Das447a0ab2025-03-04 23:10:19 +0000879 }
880 if a.partitionProps.Boot_partition_name != nil {
881 builder.Command().Textf("echo boot_images=boot.img >> %s", miscInfo)
882 }
883
Spandan Dase0411032025-03-13 21:36:19 +0000884 if a.partitionProps.Super_partition_name != nil {
885 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
886 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
887 // cat dynamic_partition_info.txt
888 builder.Command().Text("cat").Input(info.DynamicPartitionsInfo).Textf(" >> %s", miscInfo)
Spandan Das8d1d4e02025-03-18 20:07:51 +0000889 if info.AbUpdate {
890 builder.Command().Textf("echo ab_update=true >> %s", miscInfo)
891 }
892
Spandan Dase0411032025-03-13 21:36:19 +0000893 } else {
894 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
895 }
896 }
Spandan Dasf8346b52025-03-13 23:12:04 +0000897 bootImgNames := []*string{
898 a.partitionProps.Boot_partition_name,
899 a.partitionProps.Init_boot_partition_name,
900 a.partitionProps.Vendor_boot_partition_name,
901 }
902 for _, bootImgName := range bootImgNames {
903 if bootImgName == nil {
904 continue
905 }
906
907 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(bootImgName), filesystemDepTag)
908 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
909 // cat avb_ metadata of the boot images
910 builder.Command().Text("cat").Input(bootImgInfo.PropFileForMiscInfo).Textf(" >> %s", miscInfo)
911 }
Spandan Dase0411032025-03-13 21:36:19 +0000912
Spandan Dasa85dbb22025-03-21 17:49:52 +0000913 builder.Command().Textf("echo blocksize=%s >> %s", proptools.String(a.deviceProps.Flash_block_size), miscInfo)
914 if proptools.Bool(a.deviceProps.Bootloader_in_update_package) {
915 builder.Command().Textf("echo bootloader_in_update_package=true >> %s", miscInfo)
916 }
917 if len(a.deviceProps.Partial_ota_update_partitions) > 0 {
918 builder.Command().Textf("echo partial_ota_update_partitions_list=%s >> %s", strings.Join(a.deviceProps.Partial_ota_update_partitions, " "), miscInfo)
919 }
920
Spandan Das227c9492025-03-17 20:14:00 +0000921 // Sort and dedup
922 builder.Command().Textf("sort -u %s -o %s", miscInfo, miscInfo)
923
Spandan Das447a0ab2025-03-04 23:10:19 +0000924 builder.Build("misc_info", "Building misc_info")
925
926 return miscInfo
927}
928
Spandan Das65bcd972025-03-18 00:14:47 +0000929func (a *androidDevice) getBootimgHeaderVersion(ctx android.ModuleContext, bootImgName *string) string {
930 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(bootImgName), filesystemDepTag)
931 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
932 return bootImgInfo.HeaderVersion
933}
934
Spandan Das4100dd02025-03-04 23:14:34 +0000935// addImgToTargetFiles invokes `add_img_to_target_files` and creates the following files in META/
936// - apex_info.pb
937// - care_map.pb
938// - vbmeta_digest.txt
939func (a *androidDevice) addImgToTargetFiles(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir string) {
940 mkbootimg := ctx.Config().HostToolPath(ctx, "mkbootimg")
941 builder.Command().
942 Textf("PATH=%s:$PATH", ctx.Config().HostToolDir()).
943 Textf("MKBOOTIMG=%s", mkbootimg).
944 Implicit(mkbootimg).
945 BuiltTool("add_img_to_target_files").
946 Flag("-a -v -p").
947 Flag(ctx.Config().HostToolDir()).
948 Text(targetFilesDir)
949}
950
Spandan Dasdad98702025-02-26 14:32:28 +0000951type ApexKeyPathInfo struct {
952 ApexKeyPath android.Path
953}
954
955var ApexKeyPathInfoProvider = blueprint.NewProvider[ApexKeyPathInfo]()
956
Spandan Dase06dd112025-03-05 01:29:04 +0000957func (a *androidDevice) generateFilesystemConfigForTargetFiles(ctx android.ModuleContext, builder *android.RuleBuilder, stagingDirTimestamp android.Path, targetFilesDir, stagingDir, filename string) {
Spandan Das853a7a42025-02-27 07:44:51 +0000958 fsConfigOut := android.PathForModuleOut(ctx, filename)
959 ctx.Build(pctx, android.BuildParams{
Spandan Dase06dd112025-03-05 01:29:04 +0000960 Rule: fsConfigRule,
961 Implicit: stagingDirTimestamp,
962 Output: fsConfigOut,
Spandan Das853a7a42025-02-27 07:44:51 +0000963 Args: map[string]string{
964 "rootDir": stagingDir,
965 "prefix": "",
966 },
967 })
968 builder.Command().Textf("cp").Input(fsConfigOut).Textf(" %s/META/", targetFilesDir)
969}
970
Spandan Dasdd262fb2025-02-13 00:15:59 +0000971// Filenames for the partition specific fs_config files.
972// Hardcode the ramdisk files to their boot image prefix
973func (a *androidDevice) filesystemConfigNameForTargetFiles(partition string) string {
974 name := partition + "_filesystem_config.txt"
975 if partition == "system" {
976 name = "filesystem_config.txt"
977 } else if partition == "ramdisk" {
978 name = "init_boot_filesystem_config.txt"
979 }
980 return name
Spandan Das29d44882025-01-15 21:12:36 +0000981}
982
Cole Faust44080412024-12-20 14:17:07 -0800983func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
Cole Faust19eb09d2025-01-14 13:27:00 -0800984 fsMod := ctx.GetDirectDepProxyWithTag(depName, filesystemDepTag)
Cole Faust44080412024-12-20 14:17:07 -0800985 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
986 if !ok {
987 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
988 }
989 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000990}
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000991
992func (a *androidDevice) setVbmetaPhonyTargets(ctx android.ModuleContext) {
993 if !proptools.Bool(a.deviceProps.Main_device) {
994 return
995 }
996
997 if !ctx.Config().KatiEnabled() {
998 for _, vbmetaPartitionName := range a.partitionProps.Vbmeta_partitions {
999 img := ctx.GetDirectDepProxyWithTag(vbmetaPartitionName, filesystemDepTag)
1000 if provider, ok := android.OtherModuleProvider(ctx, img, vbmetaPartitionProvider); ok {
1001 // make generates `vbmetasystemimage` phony target instead of `vbmeta_systemimage` phony target.
1002 partitionName := strings.ReplaceAll(provider.Name, "_", "")
1003 ctx.Phony(fmt.Sprintf("%simage", partitionName), provider.Output)
1004 }
1005 }
1006 }
1007}
Cole Faust21c11462025-03-03 14:13:17 -08001008
1009func (a *androidDevice) getKernel(ctx android.ModuleContext) android.Path {
1010 if a.partitionProps.Boot_partition_name != nil {
1011 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
1012 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
1013 return bootImgInfo.Kernel
1014 }
1015 return nil
1016}
1017
1018// Gets the kernel version and configs from the actual kernel file itself. Roughly equivalent to
1019// this make code: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=5443;drc=c0b66fc59de069e06ce0ffd703d4d21613be30c6
1020// However, it is a simplified version of that make code. Differences include:
1021// - Not handling BOARD_KERNEL_CONFIG_FILE because BOARD_KERNEL_CONFIG_FILE was never used.
1022// - Not unpacking the bootimage, as we should be able to just always export the kernel directly
1023// in the BootimgInfo. We don't currently support prebuilt boot images, but even if we add that
1024// in the future, it can be done in a prebuilt_bootimage module type that still exports the same
1025// BootimgInfo.
1026// - We don't print a warning and output '<unknown-kernel>' to kernel_version_for_uffd_gc.txt
1027// because we expect the kernel to always be present. If it's not, we will get an error that
1028// kernel_version_for_uffd_gc.txt doesn't exist. This may require later tweaking to the
1029// dexpreopt rules so that they don't attempt to access that file in builds that don't have
1030// a kernel.
1031func (a *androidDevice) extractKernelVersionAndConfigs(ctx android.ModuleContext) (android.Path, android.Path) {
1032 kernel := a.getKernel(ctx)
1033 // If there's no kernel, don't create kernel version / kernel config files. Reverse dependencies
1034 // on those files have to account for this, for example by disabling dexpreopt in unbundled
1035 // builds.
1036 if kernel == nil {
1037 return nil, nil
1038 }
1039
1040 lz4tool := ctx.Config().HostToolPath(ctx, "lz4")
1041
1042 extractedVersionFile := android.PathForModuleOut(ctx, "kernel_version.txt")
1043 extractedConfigsFile := android.PathForModuleOut(ctx, "kernel_configs.txt")
1044 builder := android.NewRuleBuilder(pctx, ctx)
1045 builder.Command().BuiltTool("extract_kernel").
1046 Flag("--tools lz4:"+lz4tool.String()).Implicit(lz4tool).
1047 FlagWithInput("--input ", kernel).
1048 FlagWithOutput("--output-release ", extractedVersionFile).
Spandan Das2dee3f72025-03-07 18:32:06 +00001049 FlagWithOutput("--output-configs ", extractedConfigsFile).
1050 Textf(`&& printf "\n" >> %s`, extractedVersionFile)
Cole Faust21c11462025-03-03 14:13:17 -08001051
1052 if specifiedVersion := proptools.String(a.deviceProps.Kernel_version); specifiedVersion != "" {
1053 specifiedVersionFile := android.PathForModuleOut(ctx, "specified_kernel_version.txt")
Spandan Das2dee3f72025-03-07 18:32:06 +00001054 android.WriteFileRule(ctx, specifiedVersionFile, specifiedVersion)
Cole Faust21c11462025-03-03 14:13:17 -08001055 builder.Command().Text("diff -q").
1056 Input(specifiedVersionFile).
1057 Input(extractedVersionFile).
1058 Textf(`|| (echo "Specified kernel version '$(cat %s)' does not match actual kernel version '$(cat %s)'"; exit 1)`, specifiedVersionFile, extractedVersionFile)
1059 }
1060
1061 builder.Build("extract_kernel_info", "Extract kernel version and configs")
1062
1063 if proptools.Bool(a.deviceProps.Main_device) && !ctx.Config().KatiEnabled() {
1064 if ctx.Config().EnableUffdGc() == "default" {
1065 kernelVersionFile := android.PathForOutput(ctx, "dexpreopt/kernel_version_for_uffd_gc.txt")
1066 ctx.Build(pctx, android.BuildParams{
1067 Rule: android.CpIfChanged,
1068 Input: extractedVersionFile,
1069 Output: kernelVersionFile,
1070 })
1071 }
1072
1073 ctx.DistForGoal("droid_targets", extractedVersionFile)
1074 }
1075
1076 return extractedVersionFile, extractedConfigsFile
1077}
Spandan Das38afe712025-03-05 23:27:55 +00001078
1079func (a *androidDevice) buildApkCertsInfo(ctx android.ModuleContext, allInstalledModules []android.Module) android.Path {
1080 // TODO (spandandas): Add compressed
1081 formatLine := func(cert java.Certificate, name, partition string) string {
1082 pem := cert.AndroidMkString()
1083 var key string
1084 if cert.Key == nil {
1085 key = ""
1086 } else {
1087 key = cert.Key.String()
1088 }
1089 return fmt.Sprintf(`name="%s" certificate="%s" private_key="%s" partition="%s"`, name, pem, key, partition)
1090 }
1091
1092 apkCerts := []string{}
1093 for _, installedModule := range allInstalledModules {
1094 partition := ""
Yu Liuef9e63e2025-03-04 19:01:28 +00001095 if commonInfo, ok := android.OtherModuleProvider(ctx, installedModule, android.CommonModuleInfoProvider); ok {
Spandan Das38afe712025-03-05 23:27:55 +00001096 partition = commonInfo.PartitionTag
1097 } else {
1098 ctx.ModuleErrorf("%s does not set CommonModuleInfoKey", installedModule.Name())
1099 }
1100 if info, ok := android.OtherModuleProvider(ctx, installedModule, java.AppInfoProvider); ok {
1101 apkCerts = append(apkCerts, formatLine(info.Certificate, info.InstallApkName+".apk", partition))
1102 } else if info, ok := android.OtherModuleProvider(ctx, installedModule, java.AppInfosProvider); ok {
1103 for _, certInfo := range info {
Spandan Das0284ed42025-03-08 00:43:08 +00001104 // Partition information of apk-in-apex is not exported to the legacy Make packaging system.
1105 // Hardcode the partition to "system"
1106 apkCerts = append(apkCerts, formatLine(certInfo.Certificate, certInfo.InstallApkName+".apk", "system"))
Spandan Das38afe712025-03-05 23:27:55 +00001107 }
1108 } else if info, ok := android.OtherModuleProvider(ctx, installedModule, java.RuntimeResourceOverlayInfoProvider); ok {
1109 apkCerts = append(apkCerts, formatLine(info.Certificate, info.OutputFile.Base(), partition))
1110 }
1111 }
1112 slices.Sort(apkCerts) // sort by name
1113 fsInfos := a.getFsInfos(ctx)
1114 if fsInfos["system"].HasFsverity {
1115 defaultPem, defaultKey := ctx.Config().DefaultAppCertificate(ctx)
1116 apkCerts = append(apkCerts, formatLine(java.Certificate{Pem: defaultPem, Key: defaultKey}, "BuildManifest.apk", "system"))
1117 if info, ok := fsInfos["system_ext"]; ok && info.HasFsverity {
1118 apkCerts = append(apkCerts, formatLine(java.Certificate{Pem: defaultPem, Key: defaultKey}, "BuildManifestSystemExt.apk", "system_ext"))
1119 }
1120 }
1121
1122 apkCertsInfo := android.PathForModuleOut(ctx, "apkcerts.txt")
1123 android.WriteFileRuleVerbatim(ctx, apkCertsInfo, strings.Join(apkCerts, "\n")+"\n")
1124 return apkCertsInfo
1125}