blob: a2181c1a1eb74b46ae335b02f814c6eca07bc5a1 [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
Jihoon Kangf2c53982024-10-09 17:32:52 +0000123}
124
125func AndroidDeviceFactory() android.Module {
126 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +0000127 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -0800128 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000129 return module
130}
131
Cole Faust11fda332025-01-14 16:47:19 -0800132var numMainAndroidDevicesOnceKey android.OnceKey = android.NewOnceKey("num_auto_generated_anroid_devices")
Cole Faustb55a41c2025-01-09 16:53:58 -0800133
Jihoon Kangf2c53982024-10-09 17:32:52 +0000134type partitionDepTagType struct {
135 blueprint.BaseDependencyTag
136}
137
Cole Faust2bdc5e52025-01-10 10:29:36 -0800138type superPartitionDepTagType struct {
139 blueprint.BaseDependencyTag
140}
Spandan Das29d44882025-01-15 21:12:36 +0000141type targetFilesMetadataDepTagType struct {
142 blueprint.BaseDependencyTag
143}
Cole Faust2bdc5e52025-01-10 10:29:36 -0800144
145var superPartitionDepTag superPartitionDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000146var filesystemDepTag partitionDepTagType
Spandan Das29d44882025-01-15 21:12:36 +0000147var targetFilesMetadataDepTag targetFilesMetadataDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000148
149func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
150 addDependencyIfDefined := func(dep *string) {
151 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -0800152 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +0000153 }
154 }
155
Cole Faust2bdc5e52025-01-10 10:29:36 -0800156 if a.partitionProps.Super_partition_name != nil {
157 ctx.AddDependency(ctx.Module(), superPartitionDepTag, *a.partitionProps.Super_partition_name)
158 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000159 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +0000160 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000161 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000162 addDependencyIfDefined(a.partitionProps.System_partition_name)
163 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
164 addDependencyIfDefined(a.partitionProps.Product_partition_name)
165 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000166 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800167 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000168 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
169 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
170 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000171 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800172 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
173 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
174 }
Spandan Das29d44882025-01-15 21:12:36 +0000175 a.addDepsForTargetFilesMetadata(ctx)
176}
177
178func (a *androidDevice) addDepsForTargetFilesMetadata(ctx android.BottomUpMutatorContext) {
179 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), targetFilesMetadataDepTag, "liblz4") // host variant
Jihoon Kangf2c53982024-10-09 17:32:52 +0000180}
181
Cole Faust11fda332025-01-14 16:47:19 -0800182func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
183 if proptools.Bool(a.deviceProps.Main_device) {
184 numMainAndroidDevices := ctx.Config().Once(numMainAndroidDevicesOnceKey, func() interface{} {
185 return &atomic.Int32{}
186 }).(*atomic.Int32)
187 total := numMainAndroidDevices.Add(1)
188 if total > 1 {
189 // There should only be 1 main android_device module. That one will be
190 // made the default thing to build in soong-only builds.
191 ctx.ModuleErrorf("There cannot be more than 1 main android_device module")
192 }
Jihoon Kang3be17162025-01-09 20:51:54 +0000193 }
194
Cole Faust1dcf9e42025-02-19 17:23:34 -0800195 allInstalledModules := a.allInstalledModules(ctx)
196
Spandan Das38afe712025-03-05 23:27:55 +0000197 a.apkCertsInfo = a.buildApkCertsInfo(ctx, allInstalledModules)
Spandan Das2dee3f72025-03-07 18:32:06 +0000198 a.kernelVersion, a.kernelConfig = a.extractKernelVersionAndConfigs(ctx)
Spandan Das447a0ab2025-03-04 23:10:19 +0000199 a.miscInfo = a.addMiscInfo(ctx)
Spandan Dasdad98702025-02-26 14:32:28 +0000200 a.buildTargetFilesZip(ctx, allInstalledModules)
Cole Faust1dcf9e42025-02-19 17:23:34 -0800201 a.buildProguardZips(ctx, allInstalledModules)
202
mrziwang2fd33a72025-01-08 12:22:08 -0800203 var deps []android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800204 if proptools.String(a.partitionProps.Super_partition_name) != "" {
Cole Faust19eb09d2025-01-14 13:27:00 -0800205 superImage := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Cole Faust2bdc5e52025-01-10 10:29:36 -0800206 if info, ok := android.OtherModuleProvider(ctx, superImage, SuperImageProvider); ok {
207 assertUnset := func(prop *string, propName string) {
208 if prop != nil && *prop != "" {
209 ctx.PropertyErrorf(propName, "Cannot be set because it's already part of the super image")
210 }
211 }
212 for _, subPartitionType := range android.SortedKeys(info.SubImageInfo) {
213 switch subPartitionType {
214 case "system":
215 assertUnset(a.partitionProps.System_partition_name, "system_partition_name")
216 case "system_ext":
217 assertUnset(a.partitionProps.System_ext_partition_name, "system_ext_partition_name")
218 case "system_dlkm":
219 assertUnset(a.partitionProps.System_dlkm_partition_name, "system_dlkm_partition_name")
220 case "system_other":
221 // TODO
222 case "product":
223 assertUnset(a.partitionProps.Product_partition_name, "product_partition_name")
224 case "vendor":
225 assertUnset(a.partitionProps.Vendor_partition_name, "vendor_partition_name")
226 case "vendor_dlkm":
227 assertUnset(a.partitionProps.Vendor_dlkm_partition_name, "vendor_dlkm_partition_name")
228 case "odm":
229 assertUnset(a.partitionProps.Odm_partition_name, "odm_partition_name")
230 case "odm_dlkm":
231 assertUnset(a.partitionProps.Odm_dlkm_partition_name, "odm_dlkm_partition_name")
232 default:
233 ctx.ModuleErrorf("Unsupported sub-partition of super partition: %q", subPartitionType)
234 }
235 }
236
237 deps = append(deps, info.SuperImage)
238 } else {
239 ctx.ModuleErrorf("Expected super image dep to provide SuperImageProvider")
240 }
241 }
Cole Faust19eb09d2025-01-14 13:27:00 -0800242 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(m android.ModuleProxy) {
mrziwang2fd33a72025-01-08 12:22:08 -0800243 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
244 if !ok {
245 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
246 }
247 if len(imageOutput.DefaultOutputFiles) != 1 {
248 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
249 }
250 deps = append(deps, imageOutput.DefaultOutputFiles[0])
251 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000252
Cole Fausta472a6f2025-02-10 16:10:04 -0800253 allImagesZip := android.PathForModuleOut(ctx, "all_images.zip")
254 allImagesZipBuilder := android.NewRuleBuilder(pctx, ctx)
Cole Faust8967d752025-02-19 17:27:29 -0800255 cmd := allImagesZipBuilder.Command().BuiltTool("soong_zip")
Cole Fausta472a6f2025-02-10 16:10:04 -0800256 for _, dep := range deps {
257 cmd.FlagWithArg("-e ", dep.Base())
258 cmd.FlagWithInput("-f ", dep)
259 }
260 cmd.FlagWithOutput("-o ", allImagesZip)
261 allImagesZipBuilder.Build("soong_all_images_zip", "all_images.zip")
262 a.allImagesZip = allImagesZip
263
Cole Faustb55a41c2025-01-09 16:53:58 -0800264 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
Cole Faust11fda332025-01-14 16:47:19 -0800265 var validations android.Paths
266 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Cole Faustb55a41c2025-01-09 16:53:58 -0800267 // In soong-only builds, build this module by default.
268 // This is the analogue to this make code:
269 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
270 ctx.Phony("droidcore-unbundled", allImagesStamp)
Cole Faust11fda332025-01-14 16:47:19 -0800271
Cole Faust19fbb072025-01-30 18:19:29 -0800272 deps = append(deps, a.copyFilesToProductOutForSoongOnly(ctx))
Cole Faustb55a41c2025-01-09 16:53:58 -0800273 }
Cole Faust11fda332025-01-14 16:47:19 -0800274
275 ctx.Build(pctx, android.BuildParams{
276 Rule: android.Touch,
277 Output: allImagesStamp,
278 Implicits: deps,
279 Validations: validations,
280 })
281
282 // Checkbuilding it causes soong to make a phony, so you can say `m <module name>`
283 ctx.CheckbuildFile(allImagesStamp)
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000284
285 a.setVbmetaPhonyTargets(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000286
287 a.distFiles(ctx)
Wei Li7b8455f2025-03-05 16:05:51 -0800288
289 android.SetProvider(ctx, android.AndroidDeviceInfoProvider, android.AndroidDeviceInfo{
290 Main_device: android.Bool(a.deviceProps.Main_device),
291 })
292
293 if proptools.String(a.partitionProps.Super_partition_name) != "" {
294 buildComplianceMetadata(ctx, superPartitionDepTag, filesystemDepTag)
295 } else {
296 buildComplianceMetadata(ctx, filesystemDepTag)
297 }
298}
299
300func buildComplianceMetadata(ctx android.ModuleContext, tags ...blueprint.DependencyTag) {
Wei Li583bc362025-03-07 14:17:16 -0800301 // Collect metadata from deps
Wei Li7b8455f2025-03-05 16:05:51 -0800302 filesContained := make([]string, 0)
Wei Li583bc362025-03-07 14:17:16 -0800303 prebuiltFilesCopied := make([]string, 0)
Wei Li7b8455f2025-03-05 16:05:51 -0800304 for _, tag := range tags {
305 ctx.VisitDirectDepsProxyWithTag(tag, func(m android.ModuleProxy) {
306 if complianceMetadataInfo, ok := android.OtherModuleProvider(ctx, m, android.ComplianceMetadataProvider); ok {
307 filesContained = append(filesContained, complianceMetadataInfo.GetFilesContained()...)
Wei Li583bc362025-03-07 14:17:16 -0800308 prebuiltFilesCopied = append(prebuiltFilesCopied, complianceMetadataInfo.GetPrebuiltFilesCopied()...)
Wei Li7b8455f2025-03-05 16:05:51 -0800309 }
310 })
311 }
Wei Li583bc362025-03-07 14:17:16 -0800312 // Merge to module's ComplianceMetadataInfo
Wei Li7b8455f2025-03-05 16:05:51 -0800313 complianceMetadataInfo := ctx.ComplianceMetadataInfo()
Wei Li583bc362025-03-07 14:17:16 -0800314 filesContained = append(filesContained, complianceMetadataInfo.GetFilesContained()...)
315 sort.Strings(filesContained)
Wei Li7b8455f2025-03-05 16:05:51 -0800316 complianceMetadataInfo.SetFilesContained(filesContained)
Wei Li583bc362025-03-07 14:17:16 -0800317
318 prebuiltFilesCopied = append(prebuiltFilesCopied, complianceMetadataInfo.GetPrebuiltFilesCopied()...)
319 sort.Strings(prebuiltFilesCopied)
320 complianceMetadataInfo.SetPrebuiltFilesCopied(prebuiltFilesCopied)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000321}
322
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000323// Returns a list of modules that are installed, which are collected from the dependency
324// filesystem and super_image modules.
325func (a *androidDevice) allInstalledModules(ctx android.ModuleContext) []android.Module {
326 fsInfoMap := a.getFsInfos(ctx)
327 allOwners := make(map[string][]string)
328 for _, partition := range android.SortedKeys(fsInfoMap) {
329 fsInfo := fsInfoMap[partition]
330 for _, owner := range fsInfo.Owners {
331 allOwners[owner.Name] = append(allOwners[owner.Name], owner.Variation)
332 }
333 }
334
335 ret := []android.Module{}
336 ctx.WalkDepsProxy(func(mod, _ android.ModuleProxy) bool {
Spandan Das5c78fe92025-02-26 14:25:56 +0000337 if variations, ok := allOwners[ctx.OtherModuleName(mod)]; ok && android.InList(ctx.OtherModuleSubDir(mod), variations) {
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000338 ret = append(ret, mod)
339 }
340 return true
341 })
342
343 // Remove duplicates
344 ret = android.FirstUniqueFunc(ret, func(a, b android.Module) bool {
345 return a.String() == b.String()
346 })
347
348 // Sort the modules by their names and variants
349 slices.SortFunc(ret, func(a, b android.Module) int {
350 return cmp.Compare(a.String(), b.String())
351 })
352 return ret
353}
354
Cole Faust54124c82025-02-21 14:46:02 -0800355func insertBeforeExtension(file, insertion string) string {
356 ext := filepath.Ext(file)
357 return strings.TrimSuffix(file, ext) + insertion + ext
358}
359
Jihoon Kangdd49f412025-03-07 01:30:43 +0000360func (a *androidDevice) distInstalledFiles(ctx android.ModuleContext) {
361 distInstalledFilesJsonAndTxt := func(installedFiles InstalledFilesStruct) {
362 if installedFiles.Json != nil {
363 ctx.DistForGoal("droidcore-unbundled", installedFiles.Json)
364 }
365 if installedFiles.Txt != nil {
366 ctx.DistForGoal("droidcore-unbundled", installedFiles.Txt)
367 }
368 }
369
370 fsInfoMap := a.getFsInfos(ctx)
371 for _, partition := range android.SortedKeys(fsInfoMap) {
372 // installed-files-*{.txt | .json} is not disted for userdata partition
373 if partition == "userdata" {
374 continue
375 }
376 fsInfo := fsInfoMap[partition]
377 for _, installedFiles := range fsInfo.InstalledFilesDepSet.ToList() {
378 distInstalledFilesJsonAndTxt(installedFiles)
379 }
380 }
381}
382
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000383func (a *androidDevice) distFiles(ctx android.ModuleContext) {
Cole Faust54124c82025-02-21 14:46:02 -0800384 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Jihoon Kangdd49f412025-03-07 01:30:43 +0000385 a.distInstalledFiles(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000386
Cole Faust54124c82025-02-21 14:46:02 -0800387 namePrefix := ""
388 if ctx.Config().HasDeviceProduct() {
389 namePrefix = ctx.Config().DeviceProduct() + "-"
390 }
391 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardDictZip, namePrefix+insertBeforeExtension(a.proguardDictZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
392 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardDictMapping, namePrefix+insertBeforeExtension(a.proguardDictMapping.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
393 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardUsageZip, namePrefix+insertBeforeExtension(a.proguardUsageZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
Cole Faustd8d570c2025-03-04 14:20:16 -0800394
395 if a.deviceProps.Android_info != nil {
396 ctx.DistForGoal("droidcore-unbundled", android.PathForModuleSrc(ctx, *a.deviceProps.Android_info))
397 }
Spandan Das9329c672025-03-21 21:04:29 +0000398 if a.miscInfo != nil {
399 ctx.DistForGoal("droidcore-unbundled", a.miscInfo)
400 if a.partitionProps.Super_partition_name != nil {
401 ctx.DistForGoalWithFilename("dist_files", a.miscInfo, "super_misc_info.txt")
402 }
403 }
404
Cole Faust54124c82025-02-21 14:46:02 -0800405 }
Cole Faust44080412024-12-20 14:17:07 -0800406}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000407
Yu Liu2a815b62025-02-21 20:46:25 +0000408func (a *androidDevice) MakeVars(_ android.MakeVarsModuleContext) []android.ModuleMakeVarsValue {
Cole Fausta472a6f2025-02-10 16:10:04 -0800409 if proptools.Bool(a.deviceProps.Main_device) {
Yu Liu2a815b62025-02-21 20:46:25 +0000410 return []android.ModuleMakeVarsValue{{"SOONG_ONLY_ALL_IMAGES_ZIP", a.allImagesZip.String()}}
Cole Fausta472a6f2025-02-10 16:10:04 -0800411 }
Yu Liu2a815b62025-02-21 20:46:25 +0000412 return nil
Cole Fausta472a6f2025-02-10 16:10:04 -0800413}
414
Cole Faust1dcf9e42025-02-19 17:23:34 -0800415func (a *androidDevice) buildProguardZips(ctx android.ModuleContext, allInstalledModules []android.Module) {
416 dictZip := android.PathForModuleOut(ctx, "proguard-dict.zip")
417 dictZipBuilder := android.NewRuleBuilder(pctx, ctx)
418 dictZipCmd := dictZipBuilder.Command().BuiltTool("soong_zip").Flag("-d").FlagWithOutput("-o ", dictZip)
419
420 dictMapping := android.PathForModuleOut(ctx, "proguard-dict-mapping.textproto")
421 dictMappingBuilder := android.NewRuleBuilder(pctx, ctx)
422 dictMappingCmd := dictMappingBuilder.Command().BuiltTool("symbols_map").Flag("-merge").Output(dictMapping)
423
424 protosDir := android.PathForModuleOut(ctx, "proguard_mapping_protos")
425
426 usageZip := android.PathForModuleOut(ctx, "proguard-usage.zip")
427 usageZipBuilder := android.NewRuleBuilder(pctx, ctx)
428 usageZipCmd := usageZipBuilder.Command().BuiltTool("merge_zips").Output(usageZip)
429
430 for _, mod := range allInstalledModules {
431 if proguardInfo, ok := android.OtherModuleProvider(ctx, mod, java.ProguardProvider); ok {
432 // Maintain these out/target/common paths for backwards compatibility. They may be able
433 // to be changed if tools look up file locations from the protobuf, but I'm not
434 // exactly sure how that works.
435 dictionaryFakePath := fmt.Sprintf("out/target/common/obj/%s/%s_intermediates/proguard_dictionary", proguardInfo.Class, proguardInfo.ModuleName)
436 dictZipCmd.FlagWithArg("-e ", dictionaryFakePath)
437 dictZipCmd.FlagWithInput("-f ", proguardInfo.ProguardDictionary)
438 dictZipCmd.Textf("-e out/target/common/obj/%s/%s_intermediates/classes.jar", proguardInfo.Class, proguardInfo.ModuleName)
439 dictZipCmd.FlagWithInput("-f ", proguardInfo.ClassesJar)
440
441 protoFile := protosDir.Join(ctx, filepath.Dir(dictionaryFakePath), "proguard_dictionary.textproto")
442 ctx.Build(pctx, android.BuildParams{
443 Rule: proguardDictToProto,
444 Input: proguardInfo.ProguardDictionary,
445 Output: protoFile,
446 Args: map[string]string{
447 "location": dictionaryFakePath,
448 },
449 })
450 dictMappingCmd.Input(protoFile)
451
452 usageZipCmd.Input(proguardInfo.ProguardUsageZip)
453 }
454 }
455
456 dictZipBuilder.Build("proguard_dict_zip", "Building proguard dictionary zip")
457 dictMappingBuilder.Build("proguard_dict_mapping_proto", "Building proguard mapping proto")
458 usageZipBuilder.Build("proguard_usage_zip", "Building proguard usage zip")
Cole Faust54124c82025-02-21 14:46:02 -0800459
460 a.proguardDictZip = dictZip
461 a.proguardDictMapping = dictMapping
462 a.proguardUsageZip = usageZip
Cole Faust1dcf9e42025-02-19 17:23:34 -0800463}
464
Spandan Dasef775742025-01-13 22:17:40 +0000465// Helper structs for target_files.zip creation
Spandan Dasef200ac2025-01-08 01:42:45 +0000466type targetFilesZipCopy struct {
467 srcModule *string
468 destSubdir string
469}
470
Spandan Dasef775742025-01-13 22:17:40 +0000471type targetFilesystemZipCopy struct {
472 fsInfo FilesystemInfo
473 destSubdir string
474}
475
Spandan Dasdad98702025-02-26 14:32:28 +0000476func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext, allInstalledModules []android.Module) {
Cole Faust44080412024-12-20 14:17:07 -0800477 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
478 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
479
480 builder := android.NewRuleBuilder(pctx, ctx)
481 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
482 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000483 toCopy := []targetFilesZipCopy{
484 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
485 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
486 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
487 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
488 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
489 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
490 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
491 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
492 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
493 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
494 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000495 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000496
Spandan Dasef775742025-01-13 22:17:40 +0000497 filesystemsToCopy := []targetFilesystemZipCopy{}
Spandan Dasef200ac2025-01-08 01:42:45 +0000498 for _, zipCopy := range toCopy {
499 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000500 continue
501 }
Spandan Dasef775742025-01-13 22:17:40 +0000502 filesystemsToCopy = append(
503 filesystemsToCopy,
504 targetFilesystemZipCopy{a.getFilesystemInfo(ctx, *zipCopy.srcModule), zipCopy.destSubdir},
505 )
506 }
507 // Get additional filesystems from super_partition dependency
508 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800509 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000510 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust310de662025-02-19 16:15:07 -0800511 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Dasef775742025-01-13 22:17:40 +0000512 filesystemsToCopy = append(
513 filesystemsToCopy,
514 targetFilesystemZipCopy{info.SubImageInfo[partition], strings.ToUpper(partition)},
515 )
516 }
517 } else {
518 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
519 }
520 }
521
522 for _, toCopy := range filesystemsToCopy {
523 rootDirString := toCopy.fsInfo.RootDir.String()
524 if toCopy.destSubdir == "SYSTEM" {
Spandan Dasef200ac2025-01-08 01:42:45 +0000525 rootDirString = rootDirString + "/system"
526 }
Spandan Dasef775742025-01-13 22:17:40 +0000527 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), toCopy.destSubdir)
Cole Faust44080412024-12-20 14:17:07 -0800528 builder.Command().
529 BuiltTool("acp").
Spandan Dasef775742025-01-13 22:17:40 +0000530 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, toCopy.destSubdir).
531 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
Cole Faustb36763e2025-02-18 15:21:44 -0800532 for _, extraRootDir := range toCopy.fsInfo.ExtraRootDirs {
533 builder.Command().
534 BuiltTool("acp").
535 Textf("-rd %s/. %s/%s", extraRootDir, targetFilesDir, toCopy.destSubdir).
536 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
537 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000538
Spandan Dasef775742025-01-13 22:17:40 +0000539 if toCopy.destSubdir == "SYSTEM" {
Spandan Das3ec6d062025-01-09 19:37:47 +0000540 // Create the ROOT partition in target_files.zip
Spandan Dasef775742025-01-13 22:17:40 +0000541 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", toCopy.fsInfo.RootDir, targetFilesDir.String())
Spandan Dase06dd112025-03-05 01:29:04 +0000542 // Add a duplicate rule to assemble the ROOT/ directory in separate intermediates.
543 // The output timestamp will be an input to a separate fs_config call.
544 a.rootDirForFsConfig = android.PathForModuleOut(ctx, "root_dir_for_fs_config").String()
545 rootDirBuilder := android.NewRuleBuilder(pctx, ctx)
546 rootDirForFsConfigTimestamp := android.PathForModuleOut(ctx, "root_dir_for_fs_config.timestamp")
547 rootDirBuilder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s", toCopy.fsInfo.RootDir, a.rootDirForFsConfig).
548 Implicit(toCopy.fsInfo.Output).
549 Text("&& touch").
550 Output(rootDirForFsConfigTimestamp)
551 rootDirBuilder.Build("assemble_root_dir_for_fs_config", "Assemble ROOT/ for fs_config")
552 a.rootDirForFsConfigTimestamp = rootDirForFsConfigTimestamp
Spandan Das3ec6d062025-01-09 19:37:47 +0000553 }
Cole Faust44080412024-12-20 14:17:07 -0800554 }
Spandan Das9b17df22025-01-08 23:30:45 +0000555 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000556 if a.partitionProps.Vendor_boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800557 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000558 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000559 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
560 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
561 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000562 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/VENDOR_BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000563 }
Spandan Das23511372025-01-08 23:30:45 +0000564 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000565 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/VENDOR_BOOT/vendor_bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000566 }
Spandan Das258c08f2025-01-08 23:30:45 +0000567 }
568 if a.partitionProps.Boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800569 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000570 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000571 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
572 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000573 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000574 }
575 if bootImgInfo.Kernel != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000576 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000577 // 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 +0000578 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/VENDOR_BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000579 }
Spandan Das23511372025-01-08 23:30:45 +0000580 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000581 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/BOOT/bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000582 }
Spandan Das258c08f2025-01-08 23:30:45 +0000583 }
584
Spandan Dase51ff952025-01-09 18:11:59 +0000585 if a.deviceProps.Android_info != nil {
586 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
Cole Faust11fda332025-01-14 16:47:19 -0800587 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 +0000588 }
589
Spandan Das0036fa32025-01-10 23:40:45 +0000590 a.copyImagesToTargetZip(ctx, builder, targetFilesDir)
Spandan Dasdad98702025-02-26 14:32:28 +0000591 a.copyMetadataToTargetZip(ctx, builder, targetFilesDir, allInstalledModules)
Spandan Das0036fa32025-01-10 23:40:45 +0000592
Cole Faust44080412024-12-20 14:17:07 -0800593 builder.Command().
594 BuiltTool("soong_zip").
595 Text("-d").
596 FlagWithOutput("-o ", targetFilesZip).
597 FlagWithArg("-C ", targetFilesDir.String()).
598 FlagWithArg("-D ", targetFilesDir.String()).
599 Text("-sha256")
600 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
601}
602
Spandan Das0036fa32025-01-10 23:40:45 +0000603func (a *androidDevice) copyImagesToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
604 // Create an IMAGES/ subdirectory
605 builder.Command().Textf("mkdir -p %s/IMAGES", targetFilesDir.String())
606 if a.deviceProps.Bootloader != nil {
607 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader))).Textf(" %s/IMAGES/bootloader", targetFilesDir.String())
608 }
609 // Copy the filesystem ,boot and vbmeta img files to IMAGES/
610 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(child android.ModuleProxy) {
Spandan Dasa9db76d2025-01-14 01:34:43 +0000611 if strings.Contains(child.Name(), "recovery") {
612 return // skip recovery.img to match the make packaging behavior
613 }
Spandan Dasef775742025-01-13 22:17:40 +0000614 if info, ok := android.OtherModuleProvider(ctx, child, BootimgInfoProvider); ok {
615 // Check Boot img first so that the boot.img is copied and not its dep ramdisk.img
Spandan Das0036fa32025-01-10 23:40:45 +0000616 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
Spandan Dasef775742025-01-13 22:17:40 +0000617 } else if info, ok := android.OtherModuleProvider(ctx, child, FilesystemProvider); ok {
Spandan Das0036fa32025-01-10 23:40:45 +0000618 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
619 } else if info, ok := android.OtherModuleProvider(ctx, child, vbmetaPartitionProvider); ok {
620 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
621 } else {
622 ctx.ModuleErrorf("Module %s does not provide an .img file output for target_files.zip", child.Name())
623 }
624 })
Spandan Dasef775742025-01-13 22:17:40 +0000625
626 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800627 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000628 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust19eb09d2025-01-14 13:27:00 -0800629 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000630 if info.SubImageInfo[partition].OutputHermetic != nil {
631 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].OutputHermetic).Textf(" %s/IMAGES/", targetFilesDir.String())
632 }
633 if info.SubImageInfo[partition].MapFile != nil {
634 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].MapFile).Textf(" %s/IMAGES/", targetFilesDir.String())
635 }
Spandan Dasef775742025-01-13 22:17:40 +0000636 }
Spandan Dasd73441e2025-03-10 22:34:25 +0000637 // super_empty.img
638 if info.SuperEmptyImage != nil {
639 builder.Command().Textf("cp ").Input(info.SuperEmptyImage).Textf(" %s/IMAGES/", targetFilesDir.String())
640 }
Spandan Dasef775742025-01-13 22:17:40 +0000641 } else {
642 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
643 }
644 }
Spandan Das0036fa32025-01-10 23:40:45 +0000645}
646
Spandan Dasdad98702025-02-26 14:32:28 +0000647func (a *androidDevice) copyMetadataToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath, allInstalledModules []android.Module) {
Spandan Das29d44882025-01-15 21:12:36 +0000648 // Create a META/ subdirectory
649 builder.Command().Textf("mkdir -p %s/META", targetFilesDir.String())
650 if proptools.Bool(a.deviceProps.Ab_ota_updater) {
651 ctx.VisitDirectDepsProxyWithTag(targetFilesMetadataDepTag, func(child android.ModuleProxy) {
652 info, _ := android.OtherModuleProvider(ctx, child, android.OutputFilesProvider)
653 builder.Command().Textf("cp").Inputs(info.DefaultOutputFiles).Textf(" %s/META/", targetFilesDir.String())
654 })
Spandan Dasb5f40cf2025-02-12 19:36:03 +0000655 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "external/zucchini/version_info.h")).Textf(" %s/META/zucchini_config.txt", targetFilesDir.String())
656 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "system/update_engine/update_engine.conf")).Textf(" %s/META/update_engine_config.txt", targetFilesDir.String())
657 if a.getFsInfos(ctx)["system"].ErofsCompressHints != nil {
658 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].ErofsCompressHints).Textf(" %s/META/erofs_default_compress_hints.txt", targetFilesDir.String())
659 }
660 // ab_partitions.txt
661 abPartitionsSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_partitions)
662 abPartitionsSortedString := proptools.ShellEscape(strings.Join(abPartitionsSorted, "\\n"))
663 builder.Command().Textf("echo -e").Flag(abPartitionsSortedString).Textf(" > %s/META/ab_partitions.txt", targetFilesDir.String())
Spandan Das35b78742025-02-12 19:36:03 +0000664 // otakeys.txt
665 abOtaKeysSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_keys)
666 abOtaKeysSortedString := proptools.ShellEscape(strings.Join(abOtaKeysSorted, "\\n"))
667 builder.Command().Textf("echo -e").Flag(abOtaKeysSortedString).Textf(" > %s/META/otakeys.txt", targetFilesDir.String())
Spandan Das00948072025-02-12 19:36:03 +0000668 // postinstall_config.txt
669 abOtaPostInstallConfigString := proptools.ShellEscape(strings.Join(a.deviceProps.Ab_ota_postinstall_config, "\\n"))
670 builder.Command().Textf("echo -e").Flag(abOtaPostInstallConfigString).Textf(" > %s/META/postinstall_config.txt", targetFilesDir.String())
Spandan Dasf12ff9b2025-02-12 22:27:43 +0000671 // selinuxfc
672 if a.getFsInfos(ctx)["system"].SelinuxFc != nil {
673 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].SelinuxFc).Textf(" %s/META/file_contexts.bin", targetFilesDir.String())
674 }
Spandan Dasd71af182025-02-12 18:03:29 +0000675 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000676 // Copy $partition_filesystem_config.txt
677 fsInfos := a.getFsInfos(ctx)
678 for _, partition := range android.SortedKeys(fsInfos) {
679 if fsInfos[partition].FilesystemConfig == nil {
680 continue
681 }
682 if android.InList(partition, []string{"userdata"}) {
683 continue
684 }
Spandan Das853a7a42025-02-27 07:44:51 +0000685 if partition != "vendor_ramdisk" {
686 // vendor_ramdisk will be handled separately.
687 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/%s", targetFilesDir.String(), a.filesystemConfigNameForTargetFiles(partition))
688 }
Spandan Das54af3572025-02-27 07:42:08 +0000689 if partition == "ramdisk" {
690 // Create an additional copy at boot_filesystem_config.txt
691 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/boot_filesystem_config.txt", targetFilesDir.String())
692 }
Spandan Das853a7a42025-02-27 07:44:51 +0000693 if partition == "system" {
694 // Create root_filesystem_config from the assembled ROOT/ intermediates directory
Spandan Dase06dd112025-03-05 01:29:04 +0000695 a.generateFilesystemConfigForTargetFiles(ctx, builder, a.rootDirForFsConfigTimestamp, targetFilesDir.String(), a.rootDirForFsConfig, "root_filesystem_config.txt")
Spandan Das853a7a42025-02-27 07:44:51 +0000696 }
697 if partition == "vendor_ramdisk" {
698 // Create vendor_boot_filesystem_config from the assembled VENDOR_BOOT/RAMDISK intermediates directory
Spandan Dasf7b7cc32025-03-07 23:09:29 +0000699 vendorRamdiskStagingDir := targetFilesDir.String() + "/VENDOR_BOOT/RAMDISK"
700 vendorRamdiskFsConfigOut := targetFilesDir.String() + "/META/vendor_boot_filesystem_config.txt"
701 fsConfigBin := ctx.Config().HostToolPath(ctx, "fs_config")
702 builder.Command().Textf(
703 `(cd %s; find . -type d | sed 's,$,/,'; find . \! -type d) | cut -c 3- | sort | sed 's,^,,' | %s -C -D %s -R \"\" > %s`,
704 vendorRamdiskStagingDir, fsConfigBin, vendorRamdiskStagingDir, vendorRamdiskFsConfigOut).
705 Implicit(fsConfigBin)
Spandan Das853a7a42025-02-27 07:44:51 +0000706 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000707 }
Spandan Das75955b12025-02-13 22:12:52 +0000708 // Copy ramdisk_node_list
Spandan Das37240d92025-02-14 00:18:41 +0000709 if ramdiskNodeList := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Ramdisk_node_list)); ramdiskNodeList != nil {
710 builder.Command().Textf("cp").Input(ramdiskNodeList).Textf(" %s/META/", targetFilesDir.String())
711 }
712 // Copy releasetools.py
713 if releaseTools := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Releasetools_extension)); releaseTools != nil {
714 builder.Command().Textf("cp").Input(releaseTools).Textf(" %s/META/", targetFilesDir.String())
715 }
Spandan Dasdad98702025-02-26 14:32:28 +0000716 // apexkeys.txt
717 var installedApexKeys []android.Path
718 for _, installedModule := range allInstalledModules {
719 if info, ok := android.OtherModuleProvider(ctx, installedModule, ApexKeyPathInfoProvider); ok {
720 installedApexKeys = append(installedApexKeys, info.ApexKeyPath)
721 }
722 }
723 installedApexKeys = android.SortedUniquePaths(installedApexKeys) // Sort by keypath to match make
724 builder.Command().Text("cat").Inputs(installedApexKeys).Textf(" >> %s/META/apexkeys.txt", targetFilesDir.String())
Spandan Das38afe712025-03-05 23:27:55 +0000725 // apkcerts.txt
726 builder.Command().Textf("cp").Input(a.apkCertsInfo).Textf(" %s/META/", targetFilesDir.String())
727
Spandan Das3dfa17f2025-02-28 09:48:28 +0000728 // Copy fastboot-info.txt
729 if fastbootInfo := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.FastbootInfo)); fastbootInfo != nil {
730 // TODO (b/399788523): Autogenerate fastboot-info.txt if there is no source fastboot-info.txt
731 // https://cs.android.com/android/_/android/platform/build/+/80b9546f8f69e78b8fe1870e0e745d70fc18dfcd:core/Makefile;l=5831-5893;drc=077490384423dff9eac954da5c001c6f0be3fa6e;bpv=0;bpt=0
732 builder.Command().Textf("cp").Input(fastbootInfo).Textf(" %s/META/fastboot-info.txt", targetFilesDir.String())
733 }
Spandan Dasdad98702025-02-26 14:32:28 +0000734
Spandan Dasc09353c2025-03-04 00:11:21 +0000735 // kernel_configs.txt and kernel_version.txt
736 if a.kernelConfig != nil {
737 builder.Command().Textf("cp").Input(a.kernelConfig).Textf(" %s/META/", targetFilesDir.String())
738 }
739 if a.kernelVersion != nil {
740 builder.Command().Textf("cp").Input(a.kernelVersion).Textf(" %s/META/", targetFilesDir.String())
741 }
Spandan Das447a0ab2025-03-04 23:10:19 +0000742 // misc_info.txt
743 if a.miscInfo != nil {
744 builder.Command().Textf("cp").Input(a.miscInfo).Textf(" %s/META/", targetFilesDir.String())
745 }
Spandan Das4100dd02025-03-04 23:14:34 +0000746 // apex_info.pb, care_map.pb, vbmeta_digest.txt
747 a.addImgToTargetFiles(ctx, builder, targetFilesDir.String())
Spandan Dasc09353c2025-03-04 00:11:21 +0000748
Spandan Dasf079f0d2025-02-27 13:13:50 +0000749 if a.partitionProps.Super_partition_name != nil {
750 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
751 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
752 // dynamic_partitions_info.txt
753 // TODO (b/390192334): Add `building_super_empty_partition=true`
754 builder.Command().Text("cp").Input(info.DynamicPartitionsInfo).Textf(" %s/META/", targetFilesDir.String())
755 } else {
756 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
757 }
758 }
759
Spandan Dasdd262fb2025-02-13 00:15:59 +0000760}
761
Spandan Das8d1d4e02025-03-18 20:07:51 +0000762var (
763 // https://cs.android.com/android/_/android/platform/build/+/30f05352c3e6f4333c77d4af66c253572d3ea6c9:core/Makefile;l=2111-2120;drc=519f75666431ee2926e0ec8991c682b28a4c9521;bpv=1;bpt=0
764 defaultTargetRecoveryFstypeMountOptions = "ext4=max_batch_time=0,commit=1,data=ordered,barrier=1,errors=panic,nodelalloc"
765)
766
Spandan Das447a0ab2025-03-04 23:10:19 +0000767// A partial implementation of make's $PRODUCT_OUT/misc_info.txt
768// 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
769// This file is subsequently used by add_img_to_target_files to create additioanl metadata files like apex_info.pb
770// TODO (b/399788119): Complete the migration of misc_info.txt
771func (a *androidDevice) addMiscInfo(ctx android.ModuleContext) android.Path {
Spandan Dasbb15b742025-03-18 03:02:03 +0000772 buildType := func() string {
773 if ctx.Config().Debuggable() {
774 return "userdebug"
775 } else if ctx.Config().Eng() {
776 return "eng"
777 } else {
778 return "user"
779 }
780 }
781 defaultAppCertificate := func() string {
782 pem, _ := ctx.Config().DefaultAppCertificate(ctx)
783 return strings.TrimSuffix(pem.String(), ".x509.pem")
784 }
785
Spandan Das447a0ab2025-03-04 23:10:19 +0000786 builder := android.NewRuleBuilder(pctx, ctx)
787 miscInfo := android.PathForModuleOut(ctx, "misc_info.txt")
788 builder.Command().
789 Textf("rm -f %s", miscInfo).
790 Textf("&& echo recovery_api_version=%s >> %s", ctx.Config().VendorConfig("recovery").String("recovery_api_version"), miscInfo).
791 Textf("&& echo fstab_version=%s >> %s", ctx.Config().VendorConfig("recovery").String("recovery_fstab_version"), miscInfo).
Spandan Dasbb15b742025-03-18 03:02:03 +0000792 Textf("&& echo build_type=%s >> %s", buildType(), miscInfo).
793 Textf("&& echo default_system_dev_certificate=%s >> %s", defaultAppCertificate(), miscInfo).
Spandan Das8d1d4e02025-03-18 20:07:51 +0000794 Textf("&& echo root_dir=%s >> %s", android.PathForModuleInPartitionInstall(ctx, "root"), miscInfo).
Spandan Das447a0ab2025-03-04 23:10:19 +0000795 ImplicitOutput(miscInfo)
Spandan Das65bcd972025-03-18 00:14:47 +0000796 if len(ctx.Config().ExtraOtaRecoveryKeys()) > 0 {
797 builder.Command().Textf(`echo "extra_recovery_keys=%s" >> %s`, strings.Join(ctx.Config().ExtraOtaRecoveryKeys(), ""), miscInfo)
798 } else {
799 if a.partitionProps.Boot_partition_name != nil {
800 builder.Command().
801 Textf("echo mkbootimg_args='--header_version %s' >> %s", a.getBootimgHeaderVersion(ctx, a.partitionProps.Boot_partition_name), miscInfo).
802 // TODO: Use boot's header version for recovery for now since cuttlefish does not set `BOARD_RECOVERY_MKBOOTIMG_ARGS`
803 Textf(" && echo recovery_mkbootimg_args='--header_version %s' >> %s", a.getBootimgHeaderVersion(ctx, a.partitionProps.Boot_partition_name), miscInfo)
804 }
805 if a.partitionProps.Init_boot_partition_name != nil {
806 builder.Command().
807 Textf("echo mkbootimg_init_args='--header_version' %s >> %s", a.getBootimgHeaderVersion(ctx, a.partitionProps.Init_boot_partition_name), miscInfo)
808 }
809 builder.Command().
810 Textf("echo mkbootimg_version_args='--os_version %s --os_patch_level %s' >> %s", ctx.Config().PlatformVersionLastStable(), ctx.Config().PlatformSecurityPatch(), miscInfo).
811 Textf(" && echo multistage_support=1 >> %s", miscInfo).
812 Textf(" && echo blockimgdiff_versions=3,4 >> %s", miscInfo)
813 }
Spandan Dasbb15b742025-03-18 03:02:03 +0000814 fsInfos := a.getFsInfos(ctx)
815 if _, ok := fsInfos["vendor"]; ok {
816 builder.Command().Textf("echo board_uses_vendorimage=true >> %s", miscInfo)
817 }
818 if fsInfos["system"].ErofsCompressHints != nil {
819 builder.Command().Textf("echo erofs_default_compress_hints=%s >> %s", fsInfos["system"].ErofsCompressHints, miscInfo)
820 }
Spandan Das8d1d4e02025-03-18 20:07:51 +0000821 if releaseTools := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Releasetools_extension)); releaseTools != nil {
822 builder.Command().Textf("echo tool_extensions=%s >> %s", filepath.Dir(releaseTools.String()), miscInfo)
823 }
824 // ramdisk uses `compressed_cpio` fs_type
825 // https://cs.android.com/android/_/android/platform/build/+/30f05352c3e6f4333c77d4af66c253572d3ea6c9:core/Makefile;l=5923-5925;drc=519f75666431ee2926e0ec8991c682b28a4c9521;bpv=1;bpt=0
826 if _, ok := fsInfos["ramdisk"]; ok {
827 builder.Command().Textf("echo lz4_ramdisks=true >> %s", miscInfo)
828 }
829 // recovery_mount_options
830 // TODO: Add support for TARGET_RECOVERY_FSTYPE_MOUNT_OPTIONS which can be used to override the default
831 builder.Command().Textf("echo recovery_mount_options=%s >> %s", defaultTargetRecoveryFstypeMountOptions, miscInfo)
Spandan Das447a0ab2025-03-04 23:10:19 +0000832
Spandan Das62eacae2025-03-18 20:37:42 +0000833 // vintf information
834 if proptools.Bool(ctx.Config().ProductVariables().Enforce_vintf_manifest) {
835 builder.Command().Textf("echo vintf_enforce=true >> %s", miscInfo)
836 }
837 if len(ctx.Config().DeviceManifestFiles()) > 0 {
838 builder.Command().Textf("echo vintf_include_empty_vendor_sku=true >> %s", miscInfo)
839 }
840
Spandan Das447a0ab2025-03-04 23:10:19 +0000841 if a.partitionProps.Recovery_partition_name == nil {
842 builder.Command().Textf("echo no_recovery=true >> %s", miscInfo)
843 }
Spandan Das447a0ab2025-03-04 23:10:19 +0000844 for _, partition := range android.SortedKeys(fsInfos) {
Spandan Das227c9492025-03-17 20:14:00 +0000845 if fsInfos[partition].PropFileForMiscInfo != nil {
846 builder.Command().Text("cat").Input(fsInfos[partition].PropFileForMiscInfo).Textf(" >> %s", miscInfo)
Spandan Das447a0ab2025-03-04 23:10:19 +0000847 }
848 }
849 if len(a.partitionProps.Vbmeta_partitions) > 0 {
850 builder.Command().
851 Textf("echo avb_enable=true >> %s", miscInfo).
852 Textf("&& echo avb_building_vbmeta_image=true >> %s", miscInfo).
853 Textf("&& echo avb_avbtool=avbtool >> %s", miscInfo)
Spandan Das1bf169a2025-03-21 16:56:06 +0000854
855 var allChainedVbmetaPartitionTypes []string
Spandan Das2e1338e2025-03-19 23:57:36 +0000856 for _, vbmetaPartitionName := range a.partitionProps.Vbmeta_partitions {
857 img := ctx.GetDirectDepProxyWithTag(vbmetaPartitionName, filesystemDepTag)
858 if provider, ok := android.OtherModuleProvider(ctx, img, vbmetaPartitionProvider); ok {
859 builder.Command().Text("cat").Input(provider.PropFileForMiscInfo).Textf(" >> %s", miscInfo)
Spandan Das1bf169a2025-03-21 16:56:06 +0000860 if provider.FilesystemPartitionType != "" { // the top-level vbmeta.img
861 allChainedVbmetaPartitionTypes = append(allChainedVbmetaPartitionTypes, provider.FilesystemPartitionType)
862 }
Spandan Das2e1338e2025-03-19 23:57:36 +0000863 } else {
864 ctx.ModuleErrorf("vbmeta dep %s does not set vbmetaPartitionProvider\n", vbmetaPartitionName)
865 }
866 }
Spandan Das1bf169a2025-03-21 16:56:06 +0000867 // Determine the custom vbmeta partitions by removing system and vendor
868 customVbmetaPartitionTypes := android.RemoveListFromList(allChainedVbmetaPartitionTypes, []string{"system", "vendor"})
869 builder.Command().Textf("echo avb_custom_vbmeta_images_partition_list=%s >> %s",
870 strings.Join(android.SortedUniqueStrings(customVbmetaPartitionTypes), " "),
871 miscInfo,
872 )
Spandan Das2e1338e2025-03-19 23:57:36 +0000873
Spandan Das447a0ab2025-03-04 23:10:19 +0000874 }
875 if a.partitionProps.Boot_partition_name != nil {
876 builder.Command().Textf("echo boot_images=boot.img >> %s", miscInfo)
877 }
878
Spandan Dase0411032025-03-13 21:36:19 +0000879 if a.partitionProps.Super_partition_name != nil {
880 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
881 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
882 // cat dynamic_partition_info.txt
883 builder.Command().Text("cat").Input(info.DynamicPartitionsInfo).Textf(" >> %s", miscInfo)
Spandan Das8d1d4e02025-03-18 20:07:51 +0000884 if info.AbUpdate {
885 builder.Command().Textf("echo ab_update=true >> %s", miscInfo)
886 }
887
Spandan Dase0411032025-03-13 21:36:19 +0000888 } else {
889 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
890 }
891 }
Spandan Dasf8346b52025-03-13 23:12:04 +0000892 bootImgNames := []*string{
893 a.partitionProps.Boot_partition_name,
894 a.partitionProps.Init_boot_partition_name,
895 a.partitionProps.Vendor_boot_partition_name,
896 }
897 for _, bootImgName := range bootImgNames {
898 if bootImgName == nil {
899 continue
900 }
901
902 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(bootImgName), filesystemDepTag)
903 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
904 // cat avb_ metadata of the boot images
905 builder.Command().Text("cat").Input(bootImgInfo.PropFileForMiscInfo).Textf(" >> %s", miscInfo)
906 }
Spandan Dase0411032025-03-13 21:36:19 +0000907
Spandan Dasa85dbb22025-03-21 17:49:52 +0000908 builder.Command().Textf("echo blocksize=%s >> %s", proptools.String(a.deviceProps.Flash_block_size), miscInfo)
909 if proptools.Bool(a.deviceProps.Bootloader_in_update_package) {
910 builder.Command().Textf("echo bootloader_in_update_package=true >> %s", miscInfo)
911 }
912 if len(a.deviceProps.Partial_ota_update_partitions) > 0 {
913 builder.Command().Textf("echo partial_ota_update_partitions_list=%s >> %s", strings.Join(a.deviceProps.Partial_ota_update_partitions, " "), miscInfo)
914 }
915
Spandan Das227c9492025-03-17 20:14:00 +0000916 // Sort and dedup
917 builder.Command().Textf("sort -u %s -o %s", miscInfo, miscInfo)
918
Spandan Das447a0ab2025-03-04 23:10:19 +0000919 builder.Build("misc_info", "Building misc_info")
920
921 return miscInfo
922}
923
Spandan Das65bcd972025-03-18 00:14:47 +0000924func (a *androidDevice) getBootimgHeaderVersion(ctx android.ModuleContext, bootImgName *string) string {
925 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(bootImgName), filesystemDepTag)
926 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
927 return bootImgInfo.HeaderVersion
928}
929
Spandan Das4100dd02025-03-04 23:14:34 +0000930// addImgToTargetFiles invokes `add_img_to_target_files` and creates the following files in META/
931// - apex_info.pb
932// - care_map.pb
933// - vbmeta_digest.txt
934func (a *androidDevice) addImgToTargetFiles(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir string) {
935 mkbootimg := ctx.Config().HostToolPath(ctx, "mkbootimg")
936 builder.Command().
937 Textf("PATH=%s:$PATH", ctx.Config().HostToolDir()).
938 Textf("MKBOOTIMG=%s", mkbootimg).
939 Implicit(mkbootimg).
940 BuiltTool("add_img_to_target_files").
941 Flag("-a -v -p").
942 Flag(ctx.Config().HostToolDir()).
943 Text(targetFilesDir)
944}
945
Spandan Dasdad98702025-02-26 14:32:28 +0000946type ApexKeyPathInfo struct {
947 ApexKeyPath android.Path
948}
949
950var ApexKeyPathInfoProvider = blueprint.NewProvider[ApexKeyPathInfo]()
951
Spandan Dase06dd112025-03-05 01:29:04 +0000952func (a *androidDevice) generateFilesystemConfigForTargetFiles(ctx android.ModuleContext, builder *android.RuleBuilder, stagingDirTimestamp android.Path, targetFilesDir, stagingDir, filename string) {
Spandan Das853a7a42025-02-27 07:44:51 +0000953 fsConfigOut := android.PathForModuleOut(ctx, filename)
954 ctx.Build(pctx, android.BuildParams{
Spandan Dase06dd112025-03-05 01:29:04 +0000955 Rule: fsConfigRule,
956 Implicit: stagingDirTimestamp,
957 Output: fsConfigOut,
Spandan Das853a7a42025-02-27 07:44:51 +0000958 Args: map[string]string{
959 "rootDir": stagingDir,
960 "prefix": "",
961 },
962 })
963 builder.Command().Textf("cp").Input(fsConfigOut).Textf(" %s/META/", targetFilesDir)
964}
965
Spandan Dasdd262fb2025-02-13 00:15:59 +0000966// Filenames for the partition specific fs_config files.
967// Hardcode the ramdisk files to their boot image prefix
968func (a *androidDevice) filesystemConfigNameForTargetFiles(partition string) string {
969 name := partition + "_filesystem_config.txt"
970 if partition == "system" {
971 name = "filesystem_config.txt"
972 } else if partition == "ramdisk" {
973 name = "init_boot_filesystem_config.txt"
974 }
975 return name
Spandan Das29d44882025-01-15 21:12:36 +0000976}
977
Cole Faust44080412024-12-20 14:17:07 -0800978func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
Cole Faust19eb09d2025-01-14 13:27:00 -0800979 fsMod := ctx.GetDirectDepProxyWithTag(depName, filesystemDepTag)
Cole Faust44080412024-12-20 14:17:07 -0800980 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
981 if !ok {
982 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
983 }
984 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000985}
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000986
987func (a *androidDevice) setVbmetaPhonyTargets(ctx android.ModuleContext) {
988 if !proptools.Bool(a.deviceProps.Main_device) {
989 return
990 }
991
992 if !ctx.Config().KatiEnabled() {
993 for _, vbmetaPartitionName := range a.partitionProps.Vbmeta_partitions {
994 img := ctx.GetDirectDepProxyWithTag(vbmetaPartitionName, filesystemDepTag)
995 if provider, ok := android.OtherModuleProvider(ctx, img, vbmetaPartitionProvider); ok {
996 // make generates `vbmetasystemimage` phony target instead of `vbmeta_systemimage` phony target.
997 partitionName := strings.ReplaceAll(provider.Name, "_", "")
998 ctx.Phony(fmt.Sprintf("%simage", partitionName), provider.Output)
999 }
1000 }
1001 }
1002}
Cole Faust21c11462025-03-03 14:13:17 -08001003
1004func (a *androidDevice) getKernel(ctx android.ModuleContext) android.Path {
1005 if a.partitionProps.Boot_partition_name != nil {
1006 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
1007 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
1008 return bootImgInfo.Kernel
1009 }
1010 return nil
1011}
1012
1013// Gets the kernel version and configs from the actual kernel file itself. Roughly equivalent to
1014// this make code: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=5443;drc=c0b66fc59de069e06ce0ffd703d4d21613be30c6
1015// However, it is a simplified version of that make code. Differences include:
1016// - Not handling BOARD_KERNEL_CONFIG_FILE because BOARD_KERNEL_CONFIG_FILE was never used.
1017// - Not unpacking the bootimage, as we should be able to just always export the kernel directly
1018// in the BootimgInfo. We don't currently support prebuilt boot images, but even if we add that
1019// in the future, it can be done in a prebuilt_bootimage module type that still exports the same
1020// BootimgInfo.
1021// - We don't print a warning and output '<unknown-kernel>' to kernel_version_for_uffd_gc.txt
1022// because we expect the kernel to always be present. If it's not, we will get an error that
1023// kernel_version_for_uffd_gc.txt doesn't exist. This may require later tweaking to the
1024// dexpreopt rules so that they don't attempt to access that file in builds that don't have
1025// a kernel.
1026func (a *androidDevice) extractKernelVersionAndConfigs(ctx android.ModuleContext) (android.Path, android.Path) {
1027 kernel := a.getKernel(ctx)
1028 // If there's no kernel, don't create kernel version / kernel config files. Reverse dependencies
1029 // on those files have to account for this, for example by disabling dexpreopt in unbundled
1030 // builds.
1031 if kernel == nil {
1032 return nil, nil
1033 }
1034
1035 lz4tool := ctx.Config().HostToolPath(ctx, "lz4")
1036
1037 extractedVersionFile := android.PathForModuleOut(ctx, "kernel_version.txt")
1038 extractedConfigsFile := android.PathForModuleOut(ctx, "kernel_configs.txt")
1039 builder := android.NewRuleBuilder(pctx, ctx)
1040 builder.Command().BuiltTool("extract_kernel").
1041 Flag("--tools lz4:"+lz4tool.String()).Implicit(lz4tool).
1042 FlagWithInput("--input ", kernel).
1043 FlagWithOutput("--output-release ", extractedVersionFile).
Spandan Das2dee3f72025-03-07 18:32:06 +00001044 FlagWithOutput("--output-configs ", extractedConfigsFile).
1045 Textf(`&& printf "\n" >> %s`, extractedVersionFile)
Cole Faust21c11462025-03-03 14:13:17 -08001046
1047 if specifiedVersion := proptools.String(a.deviceProps.Kernel_version); specifiedVersion != "" {
1048 specifiedVersionFile := android.PathForModuleOut(ctx, "specified_kernel_version.txt")
Spandan Das2dee3f72025-03-07 18:32:06 +00001049 android.WriteFileRule(ctx, specifiedVersionFile, specifiedVersion)
Cole Faust21c11462025-03-03 14:13:17 -08001050 builder.Command().Text("diff -q").
1051 Input(specifiedVersionFile).
1052 Input(extractedVersionFile).
1053 Textf(`|| (echo "Specified kernel version '$(cat %s)' does not match actual kernel version '$(cat %s)'"; exit 1)`, specifiedVersionFile, extractedVersionFile)
1054 }
1055
1056 builder.Build("extract_kernel_info", "Extract kernel version and configs")
1057
1058 if proptools.Bool(a.deviceProps.Main_device) && !ctx.Config().KatiEnabled() {
1059 if ctx.Config().EnableUffdGc() == "default" {
1060 kernelVersionFile := android.PathForOutput(ctx, "dexpreopt/kernel_version_for_uffd_gc.txt")
1061 ctx.Build(pctx, android.BuildParams{
1062 Rule: android.CpIfChanged,
1063 Input: extractedVersionFile,
1064 Output: kernelVersionFile,
1065 })
1066 }
1067
1068 ctx.DistForGoal("droid_targets", extractedVersionFile)
1069 }
1070
1071 return extractedVersionFile, extractedConfigsFile
1072}
Spandan Das38afe712025-03-05 23:27:55 +00001073
1074func (a *androidDevice) buildApkCertsInfo(ctx android.ModuleContext, allInstalledModules []android.Module) android.Path {
1075 // TODO (spandandas): Add compressed
1076 formatLine := func(cert java.Certificate, name, partition string) string {
1077 pem := cert.AndroidMkString()
1078 var key string
1079 if cert.Key == nil {
1080 key = ""
1081 } else {
1082 key = cert.Key.String()
1083 }
1084 return fmt.Sprintf(`name="%s" certificate="%s" private_key="%s" partition="%s"`, name, pem, key, partition)
1085 }
1086
1087 apkCerts := []string{}
1088 for _, installedModule := range allInstalledModules {
1089 partition := ""
Yu Liuef9e63e2025-03-04 19:01:28 +00001090 if commonInfo, ok := android.OtherModuleProvider(ctx, installedModule, android.CommonModuleInfoProvider); ok {
Spandan Das38afe712025-03-05 23:27:55 +00001091 partition = commonInfo.PartitionTag
1092 } else {
1093 ctx.ModuleErrorf("%s does not set CommonModuleInfoKey", installedModule.Name())
1094 }
1095 if info, ok := android.OtherModuleProvider(ctx, installedModule, java.AppInfoProvider); ok {
1096 apkCerts = append(apkCerts, formatLine(info.Certificate, info.InstallApkName+".apk", partition))
1097 } else if info, ok := android.OtherModuleProvider(ctx, installedModule, java.AppInfosProvider); ok {
1098 for _, certInfo := range info {
Spandan Das0284ed42025-03-08 00:43:08 +00001099 // Partition information of apk-in-apex is not exported to the legacy Make packaging system.
1100 // Hardcode the partition to "system"
1101 apkCerts = append(apkCerts, formatLine(certInfo.Certificate, certInfo.InstallApkName+".apk", "system"))
Spandan Das38afe712025-03-05 23:27:55 +00001102 }
1103 } else if info, ok := android.OtherModuleProvider(ctx, installedModule, java.RuntimeResourceOverlayInfoProvider); ok {
1104 apkCerts = append(apkCerts, formatLine(info.Certificate, info.OutputFile.Base(), partition))
1105 }
1106 }
1107 slices.Sort(apkCerts) // sort by name
1108 fsInfos := a.getFsInfos(ctx)
1109 if fsInfos["system"].HasFsverity {
1110 defaultPem, defaultKey := ctx.Config().DefaultAppCertificate(ctx)
1111 apkCerts = append(apkCerts, formatLine(java.Certificate{Pem: defaultPem, Key: defaultKey}, "BuildManifest.apk", "system"))
1112 if info, ok := fsInfos["system_ext"]; ok && info.HasFsverity {
1113 apkCerts = append(apkCerts, formatLine(java.Certificate{Pem: defaultPem, Key: defaultKey}, "BuildManifestSystemExt.apk", "system_ext"))
1114 }
1115 }
1116
1117 apkCertsInfo := android.PathForModuleOut(ctx, "apkcerts.txt")
1118 android.WriteFileRuleVerbatim(ctx, apkCertsInfo, strings.Join(apkCerts, "\n")+"\n")
1119 return apkCertsInfo
1120}