blob: 931adfd437ff5aae83f66c95574e9a16e407b943 [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
93 // The kernel version in the build. Will be verified against the actual kernel.
94 // If not provided, will attempt to extract it from the loose kernel or the kernel inside
95 // the boot image. The version is later used to decide whether or not to enable uffd_gc
96 // when dexpreopting apps. So setting this doesn't really do anything except enforce that the
97 // actual kernel version is as specified here.
98 Kernel_version *string
Jihoon Kang3be17162025-01-09 20:51:54 +000099}
100
Jihoon Kangf2c53982024-10-09 17:32:52 +0000101type androidDevice struct {
102 android.ModuleBase
103
104 partitionProps PartitionNameProperties
Jihoon Kang3be17162025-01-09 20:51:54 +0000105
106 deviceProps DeviceProperties
Cole Fausta472a6f2025-02-10 16:10:04 -0800107
108 allImagesZip android.Path
Cole Faust54124c82025-02-21 14:46:02 -0800109
Spandan Dase06dd112025-03-05 01:29:04 +0000110 proguardDictZip android.Path
111 proguardDictMapping android.Path
112 proguardUsageZip android.Path
113 kernelConfig android.Path
114 kernelVersion android.Path
115 miscInfo android.Path
116 rootDirForFsConfig string
117 rootDirForFsConfigTimestamp android.Path
Spandan Das38afe712025-03-05 23:27:55 +0000118 apkCertsInfo android.Path
Jihoon Kangf2c53982024-10-09 17:32:52 +0000119}
120
121func AndroidDeviceFactory() android.Module {
122 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +0000123 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -0800124 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000125 return module
126}
127
Cole Faust11fda332025-01-14 16:47:19 -0800128var numMainAndroidDevicesOnceKey android.OnceKey = android.NewOnceKey("num_auto_generated_anroid_devices")
Cole Faustb55a41c2025-01-09 16:53:58 -0800129
Jihoon Kangf2c53982024-10-09 17:32:52 +0000130type partitionDepTagType struct {
131 blueprint.BaseDependencyTag
132}
133
Cole Faust2bdc5e52025-01-10 10:29:36 -0800134type superPartitionDepTagType struct {
135 blueprint.BaseDependencyTag
136}
Spandan Das29d44882025-01-15 21:12:36 +0000137type targetFilesMetadataDepTagType struct {
138 blueprint.BaseDependencyTag
139}
Cole Faust2bdc5e52025-01-10 10:29:36 -0800140
141var superPartitionDepTag superPartitionDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000142var filesystemDepTag partitionDepTagType
Spandan Das29d44882025-01-15 21:12:36 +0000143var targetFilesMetadataDepTag targetFilesMetadataDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000144
145func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
146 addDependencyIfDefined := func(dep *string) {
147 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -0800148 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +0000149 }
150 }
151
Cole Faust2bdc5e52025-01-10 10:29:36 -0800152 if a.partitionProps.Super_partition_name != nil {
153 ctx.AddDependency(ctx.Module(), superPartitionDepTag, *a.partitionProps.Super_partition_name)
154 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000155 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +0000156 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000157 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000158 addDependencyIfDefined(a.partitionProps.System_partition_name)
159 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
160 addDependencyIfDefined(a.partitionProps.Product_partition_name)
161 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000162 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800163 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000164 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
165 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
166 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000167 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800168 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
169 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
170 }
Spandan Das29d44882025-01-15 21:12:36 +0000171 a.addDepsForTargetFilesMetadata(ctx)
172}
173
174func (a *androidDevice) addDepsForTargetFilesMetadata(ctx android.BottomUpMutatorContext) {
175 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), targetFilesMetadataDepTag, "liblz4") // host variant
Jihoon Kangf2c53982024-10-09 17:32:52 +0000176}
177
Cole Faust11fda332025-01-14 16:47:19 -0800178func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
179 if proptools.Bool(a.deviceProps.Main_device) {
180 numMainAndroidDevices := ctx.Config().Once(numMainAndroidDevicesOnceKey, func() interface{} {
181 return &atomic.Int32{}
182 }).(*atomic.Int32)
183 total := numMainAndroidDevices.Add(1)
184 if total > 1 {
185 // There should only be 1 main android_device module. That one will be
186 // made the default thing to build in soong-only builds.
187 ctx.ModuleErrorf("There cannot be more than 1 main android_device module")
188 }
Jihoon Kang3be17162025-01-09 20:51:54 +0000189 }
190
Cole Faust1dcf9e42025-02-19 17:23:34 -0800191 allInstalledModules := a.allInstalledModules(ctx)
192
Spandan Das38afe712025-03-05 23:27:55 +0000193 a.apkCertsInfo = a.buildApkCertsInfo(ctx, allInstalledModules)
Spandan Das2dee3f72025-03-07 18:32:06 +0000194 a.kernelVersion, a.kernelConfig = a.extractKernelVersionAndConfigs(ctx)
Spandan Das447a0ab2025-03-04 23:10:19 +0000195 a.miscInfo = a.addMiscInfo(ctx)
Spandan Dasdad98702025-02-26 14:32:28 +0000196 a.buildTargetFilesZip(ctx, allInstalledModules)
Cole Faust1dcf9e42025-02-19 17:23:34 -0800197 a.buildProguardZips(ctx, allInstalledModules)
198
mrziwang2fd33a72025-01-08 12:22:08 -0800199 var deps []android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800200 if proptools.String(a.partitionProps.Super_partition_name) != "" {
Cole Faust19eb09d2025-01-14 13:27:00 -0800201 superImage := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Cole Faust2bdc5e52025-01-10 10:29:36 -0800202 if info, ok := android.OtherModuleProvider(ctx, superImage, SuperImageProvider); ok {
203 assertUnset := func(prop *string, propName string) {
204 if prop != nil && *prop != "" {
205 ctx.PropertyErrorf(propName, "Cannot be set because it's already part of the super image")
206 }
207 }
208 for _, subPartitionType := range android.SortedKeys(info.SubImageInfo) {
209 switch subPartitionType {
210 case "system":
211 assertUnset(a.partitionProps.System_partition_name, "system_partition_name")
212 case "system_ext":
213 assertUnset(a.partitionProps.System_ext_partition_name, "system_ext_partition_name")
214 case "system_dlkm":
215 assertUnset(a.partitionProps.System_dlkm_partition_name, "system_dlkm_partition_name")
216 case "system_other":
217 // TODO
218 case "product":
219 assertUnset(a.partitionProps.Product_partition_name, "product_partition_name")
220 case "vendor":
221 assertUnset(a.partitionProps.Vendor_partition_name, "vendor_partition_name")
222 case "vendor_dlkm":
223 assertUnset(a.partitionProps.Vendor_dlkm_partition_name, "vendor_dlkm_partition_name")
224 case "odm":
225 assertUnset(a.partitionProps.Odm_partition_name, "odm_partition_name")
226 case "odm_dlkm":
227 assertUnset(a.partitionProps.Odm_dlkm_partition_name, "odm_dlkm_partition_name")
228 default:
229 ctx.ModuleErrorf("Unsupported sub-partition of super partition: %q", subPartitionType)
230 }
231 }
232
233 deps = append(deps, info.SuperImage)
234 } else {
235 ctx.ModuleErrorf("Expected super image dep to provide SuperImageProvider")
236 }
237 }
Cole Faust19eb09d2025-01-14 13:27:00 -0800238 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(m android.ModuleProxy) {
mrziwang2fd33a72025-01-08 12:22:08 -0800239 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
240 if !ok {
241 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
242 }
243 if len(imageOutput.DefaultOutputFiles) != 1 {
244 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
245 }
246 deps = append(deps, imageOutput.DefaultOutputFiles[0])
247 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000248
Cole Fausta472a6f2025-02-10 16:10:04 -0800249 allImagesZip := android.PathForModuleOut(ctx, "all_images.zip")
250 allImagesZipBuilder := android.NewRuleBuilder(pctx, ctx)
Cole Faust8967d752025-02-19 17:27:29 -0800251 cmd := allImagesZipBuilder.Command().BuiltTool("soong_zip")
Cole Fausta472a6f2025-02-10 16:10:04 -0800252 for _, dep := range deps {
253 cmd.FlagWithArg("-e ", dep.Base())
254 cmd.FlagWithInput("-f ", dep)
255 }
256 cmd.FlagWithOutput("-o ", allImagesZip)
257 allImagesZipBuilder.Build("soong_all_images_zip", "all_images.zip")
258 a.allImagesZip = allImagesZip
259
Cole Faustb55a41c2025-01-09 16:53:58 -0800260 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
Cole Faust11fda332025-01-14 16:47:19 -0800261 var validations android.Paths
262 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Cole Faustb55a41c2025-01-09 16:53:58 -0800263 // In soong-only builds, build this module by default.
264 // This is the analogue to this make code:
265 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
266 ctx.Phony("droidcore-unbundled", allImagesStamp)
Cole Faust11fda332025-01-14 16:47:19 -0800267
Cole Faust19fbb072025-01-30 18:19:29 -0800268 deps = append(deps, a.copyFilesToProductOutForSoongOnly(ctx))
Cole Faustb55a41c2025-01-09 16:53:58 -0800269 }
Cole Faust11fda332025-01-14 16:47:19 -0800270
271 ctx.Build(pctx, android.BuildParams{
272 Rule: android.Touch,
273 Output: allImagesStamp,
274 Implicits: deps,
275 Validations: validations,
276 })
277
278 // Checkbuilding it causes soong to make a phony, so you can say `m <module name>`
279 ctx.CheckbuildFile(allImagesStamp)
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000280
281 a.setVbmetaPhonyTargets(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000282
283 a.distFiles(ctx)
Wei Li7b8455f2025-03-05 16:05:51 -0800284
285 android.SetProvider(ctx, android.AndroidDeviceInfoProvider, android.AndroidDeviceInfo{
286 Main_device: android.Bool(a.deviceProps.Main_device),
287 })
288
289 if proptools.String(a.partitionProps.Super_partition_name) != "" {
290 buildComplianceMetadata(ctx, superPartitionDepTag, filesystemDepTag)
291 } else {
292 buildComplianceMetadata(ctx, filesystemDepTag)
293 }
294}
295
296func buildComplianceMetadata(ctx android.ModuleContext, tags ...blueprint.DependencyTag) {
Wei Li583bc362025-03-07 14:17:16 -0800297 // Collect metadata from deps
Wei Li7b8455f2025-03-05 16:05:51 -0800298 filesContained := make([]string, 0)
Wei Li583bc362025-03-07 14:17:16 -0800299 prebuiltFilesCopied := make([]string, 0)
Wei Li7b8455f2025-03-05 16:05:51 -0800300 for _, tag := range tags {
301 ctx.VisitDirectDepsProxyWithTag(tag, func(m android.ModuleProxy) {
302 if complianceMetadataInfo, ok := android.OtherModuleProvider(ctx, m, android.ComplianceMetadataProvider); ok {
303 filesContained = append(filesContained, complianceMetadataInfo.GetFilesContained()...)
Wei Li583bc362025-03-07 14:17:16 -0800304 prebuiltFilesCopied = append(prebuiltFilesCopied, complianceMetadataInfo.GetPrebuiltFilesCopied()...)
Wei Li7b8455f2025-03-05 16:05:51 -0800305 }
306 })
307 }
Wei Li583bc362025-03-07 14:17:16 -0800308 // Merge to module's ComplianceMetadataInfo
Wei Li7b8455f2025-03-05 16:05:51 -0800309 complianceMetadataInfo := ctx.ComplianceMetadataInfo()
Wei Li583bc362025-03-07 14:17:16 -0800310 filesContained = append(filesContained, complianceMetadataInfo.GetFilesContained()...)
311 sort.Strings(filesContained)
Wei Li7b8455f2025-03-05 16:05:51 -0800312 complianceMetadataInfo.SetFilesContained(filesContained)
Wei Li583bc362025-03-07 14:17:16 -0800313
314 prebuiltFilesCopied = append(prebuiltFilesCopied, complianceMetadataInfo.GetPrebuiltFilesCopied()...)
315 sort.Strings(prebuiltFilesCopied)
316 complianceMetadataInfo.SetPrebuiltFilesCopied(prebuiltFilesCopied)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000317}
318
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000319// Returns a list of modules that are installed, which are collected from the dependency
320// filesystem and super_image modules.
321func (a *androidDevice) allInstalledModules(ctx android.ModuleContext) []android.Module {
322 fsInfoMap := a.getFsInfos(ctx)
323 allOwners := make(map[string][]string)
324 for _, partition := range android.SortedKeys(fsInfoMap) {
325 fsInfo := fsInfoMap[partition]
326 for _, owner := range fsInfo.Owners {
327 allOwners[owner.Name] = append(allOwners[owner.Name], owner.Variation)
328 }
329 }
330
331 ret := []android.Module{}
332 ctx.WalkDepsProxy(func(mod, _ android.ModuleProxy) bool {
Spandan Das5c78fe92025-02-26 14:25:56 +0000333 if variations, ok := allOwners[ctx.OtherModuleName(mod)]; ok && android.InList(ctx.OtherModuleSubDir(mod), variations) {
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000334 ret = append(ret, mod)
335 }
336 return true
337 })
338
339 // Remove duplicates
340 ret = android.FirstUniqueFunc(ret, func(a, b android.Module) bool {
341 return a.String() == b.String()
342 })
343
344 // Sort the modules by their names and variants
345 slices.SortFunc(ret, func(a, b android.Module) int {
346 return cmp.Compare(a.String(), b.String())
347 })
348 return ret
349}
350
Cole Faust54124c82025-02-21 14:46:02 -0800351func insertBeforeExtension(file, insertion string) string {
352 ext := filepath.Ext(file)
353 return strings.TrimSuffix(file, ext) + insertion + ext
354}
355
Jihoon Kangdd49f412025-03-07 01:30:43 +0000356func (a *androidDevice) distInstalledFiles(ctx android.ModuleContext) {
357 distInstalledFilesJsonAndTxt := func(installedFiles InstalledFilesStruct) {
358 if installedFiles.Json != nil {
359 ctx.DistForGoal("droidcore-unbundled", installedFiles.Json)
360 }
361 if installedFiles.Txt != nil {
362 ctx.DistForGoal("droidcore-unbundled", installedFiles.Txt)
363 }
364 }
365
366 fsInfoMap := a.getFsInfos(ctx)
367 for _, partition := range android.SortedKeys(fsInfoMap) {
368 // installed-files-*{.txt | .json} is not disted for userdata partition
369 if partition == "userdata" {
370 continue
371 }
372 fsInfo := fsInfoMap[partition]
373 for _, installedFiles := range fsInfo.InstalledFilesDepSet.ToList() {
374 distInstalledFilesJsonAndTxt(installedFiles)
375 }
376 }
377}
378
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000379func (a *androidDevice) distFiles(ctx android.ModuleContext) {
Cole Faust54124c82025-02-21 14:46:02 -0800380 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Jihoon Kangdd49f412025-03-07 01:30:43 +0000381 a.distInstalledFiles(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000382
Cole Faust54124c82025-02-21 14:46:02 -0800383 namePrefix := ""
384 if ctx.Config().HasDeviceProduct() {
385 namePrefix = ctx.Config().DeviceProduct() + "-"
386 }
387 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardDictZip, namePrefix+insertBeforeExtension(a.proguardDictZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
388 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardDictMapping, namePrefix+insertBeforeExtension(a.proguardDictMapping.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
389 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardUsageZip, namePrefix+insertBeforeExtension(a.proguardUsageZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
Cole Faustd8d570c2025-03-04 14:20:16 -0800390
391 if a.deviceProps.Android_info != nil {
392 ctx.DistForGoal("droidcore-unbundled", android.PathForModuleSrc(ctx, *a.deviceProps.Android_info))
393 }
Cole Faust54124c82025-02-21 14:46:02 -0800394 }
Cole Faust44080412024-12-20 14:17:07 -0800395}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000396
Yu Liu2a815b62025-02-21 20:46:25 +0000397func (a *androidDevice) MakeVars(_ android.MakeVarsModuleContext) []android.ModuleMakeVarsValue {
Cole Fausta472a6f2025-02-10 16:10:04 -0800398 if proptools.Bool(a.deviceProps.Main_device) {
Yu Liu2a815b62025-02-21 20:46:25 +0000399 return []android.ModuleMakeVarsValue{{"SOONG_ONLY_ALL_IMAGES_ZIP", a.allImagesZip.String()}}
Cole Fausta472a6f2025-02-10 16:10:04 -0800400 }
Yu Liu2a815b62025-02-21 20:46:25 +0000401 return nil
Cole Fausta472a6f2025-02-10 16:10:04 -0800402}
403
Cole Faust1dcf9e42025-02-19 17:23:34 -0800404func (a *androidDevice) buildProguardZips(ctx android.ModuleContext, allInstalledModules []android.Module) {
405 dictZip := android.PathForModuleOut(ctx, "proguard-dict.zip")
406 dictZipBuilder := android.NewRuleBuilder(pctx, ctx)
407 dictZipCmd := dictZipBuilder.Command().BuiltTool("soong_zip").Flag("-d").FlagWithOutput("-o ", dictZip)
408
409 dictMapping := android.PathForModuleOut(ctx, "proguard-dict-mapping.textproto")
410 dictMappingBuilder := android.NewRuleBuilder(pctx, ctx)
411 dictMappingCmd := dictMappingBuilder.Command().BuiltTool("symbols_map").Flag("-merge").Output(dictMapping)
412
413 protosDir := android.PathForModuleOut(ctx, "proguard_mapping_protos")
414
415 usageZip := android.PathForModuleOut(ctx, "proguard-usage.zip")
416 usageZipBuilder := android.NewRuleBuilder(pctx, ctx)
417 usageZipCmd := usageZipBuilder.Command().BuiltTool("merge_zips").Output(usageZip)
418
419 for _, mod := range allInstalledModules {
420 if proguardInfo, ok := android.OtherModuleProvider(ctx, mod, java.ProguardProvider); ok {
421 // Maintain these out/target/common paths for backwards compatibility. They may be able
422 // to be changed if tools look up file locations from the protobuf, but I'm not
423 // exactly sure how that works.
424 dictionaryFakePath := fmt.Sprintf("out/target/common/obj/%s/%s_intermediates/proguard_dictionary", proguardInfo.Class, proguardInfo.ModuleName)
425 dictZipCmd.FlagWithArg("-e ", dictionaryFakePath)
426 dictZipCmd.FlagWithInput("-f ", proguardInfo.ProguardDictionary)
427 dictZipCmd.Textf("-e out/target/common/obj/%s/%s_intermediates/classes.jar", proguardInfo.Class, proguardInfo.ModuleName)
428 dictZipCmd.FlagWithInput("-f ", proguardInfo.ClassesJar)
429
430 protoFile := protosDir.Join(ctx, filepath.Dir(dictionaryFakePath), "proguard_dictionary.textproto")
431 ctx.Build(pctx, android.BuildParams{
432 Rule: proguardDictToProto,
433 Input: proguardInfo.ProguardDictionary,
434 Output: protoFile,
435 Args: map[string]string{
436 "location": dictionaryFakePath,
437 },
438 })
439 dictMappingCmd.Input(protoFile)
440
441 usageZipCmd.Input(proguardInfo.ProguardUsageZip)
442 }
443 }
444
445 dictZipBuilder.Build("proguard_dict_zip", "Building proguard dictionary zip")
446 dictMappingBuilder.Build("proguard_dict_mapping_proto", "Building proguard mapping proto")
447 usageZipBuilder.Build("proguard_usage_zip", "Building proguard usage zip")
Cole Faust54124c82025-02-21 14:46:02 -0800448
449 a.proguardDictZip = dictZip
450 a.proguardDictMapping = dictMapping
451 a.proguardUsageZip = usageZip
Cole Faust1dcf9e42025-02-19 17:23:34 -0800452}
453
Spandan Dasef775742025-01-13 22:17:40 +0000454// Helper structs for target_files.zip creation
Spandan Dasef200ac2025-01-08 01:42:45 +0000455type targetFilesZipCopy struct {
456 srcModule *string
457 destSubdir string
458}
459
Spandan Dasef775742025-01-13 22:17:40 +0000460type targetFilesystemZipCopy struct {
461 fsInfo FilesystemInfo
462 destSubdir string
463}
464
Spandan Dasdad98702025-02-26 14:32:28 +0000465func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext, allInstalledModules []android.Module) {
Cole Faust44080412024-12-20 14:17:07 -0800466 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
467 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
468
469 builder := android.NewRuleBuilder(pctx, ctx)
470 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
471 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000472 toCopy := []targetFilesZipCopy{
473 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
474 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
475 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
476 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
477 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
478 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
479 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
480 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
481 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
482 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
483 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000484 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000485
Spandan Dasef775742025-01-13 22:17:40 +0000486 filesystemsToCopy := []targetFilesystemZipCopy{}
Spandan Dasef200ac2025-01-08 01:42:45 +0000487 for _, zipCopy := range toCopy {
488 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000489 continue
490 }
Spandan Dasef775742025-01-13 22:17:40 +0000491 filesystemsToCopy = append(
492 filesystemsToCopy,
493 targetFilesystemZipCopy{a.getFilesystemInfo(ctx, *zipCopy.srcModule), zipCopy.destSubdir},
494 )
495 }
496 // Get additional filesystems from super_partition dependency
497 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800498 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000499 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust310de662025-02-19 16:15:07 -0800500 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Dasef775742025-01-13 22:17:40 +0000501 filesystemsToCopy = append(
502 filesystemsToCopy,
503 targetFilesystemZipCopy{info.SubImageInfo[partition], strings.ToUpper(partition)},
504 )
505 }
506 } else {
507 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
508 }
509 }
510
511 for _, toCopy := range filesystemsToCopy {
512 rootDirString := toCopy.fsInfo.RootDir.String()
513 if toCopy.destSubdir == "SYSTEM" {
Spandan Dasef200ac2025-01-08 01:42:45 +0000514 rootDirString = rootDirString + "/system"
515 }
Spandan Dasef775742025-01-13 22:17:40 +0000516 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), toCopy.destSubdir)
Cole Faust44080412024-12-20 14:17:07 -0800517 builder.Command().
518 BuiltTool("acp").
Spandan Dasef775742025-01-13 22:17:40 +0000519 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, toCopy.destSubdir).
520 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
Cole Faustb36763e2025-02-18 15:21:44 -0800521 for _, extraRootDir := range toCopy.fsInfo.ExtraRootDirs {
522 builder.Command().
523 BuiltTool("acp").
524 Textf("-rd %s/. %s/%s", extraRootDir, targetFilesDir, toCopy.destSubdir).
525 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
526 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000527
Spandan Dasef775742025-01-13 22:17:40 +0000528 if toCopy.destSubdir == "SYSTEM" {
Spandan Das3ec6d062025-01-09 19:37:47 +0000529 // Create the ROOT partition in target_files.zip
Spandan Dasef775742025-01-13 22:17:40 +0000530 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", toCopy.fsInfo.RootDir, targetFilesDir.String())
Spandan Dase06dd112025-03-05 01:29:04 +0000531 // Add a duplicate rule to assemble the ROOT/ directory in separate intermediates.
532 // The output timestamp will be an input to a separate fs_config call.
533 a.rootDirForFsConfig = android.PathForModuleOut(ctx, "root_dir_for_fs_config").String()
534 rootDirBuilder := android.NewRuleBuilder(pctx, ctx)
535 rootDirForFsConfigTimestamp := android.PathForModuleOut(ctx, "root_dir_for_fs_config.timestamp")
536 rootDirBuilder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s", toCopy.fsInfo.RootDir, a.rootDirForFsConfig).
537 Implicit(toCopy.fsInfo.Output).
538 Text("&& touch").
539 Output(rootDirForFsConfigTimestamp)
540 rootDirBuilder.Build("assemble_root_dir_for_fs_config", "Assemble ROOT/ for fs_config")
541 a.rootDirForFsConfigTimestamp = rootDirForFsConfigTimestamp
Spandan Das3ec6d062025-01-09 19:37:47 +0000542 }
Cole Faust44080412024-12-20 14:17:07 -0800543 }
Spandan Das9b17df22025-01-08 23:30:45 +0000544 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000545 if a.partitionProps.Vendor_boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800546 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000547 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000548 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
549 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
550 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000551 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/VENDOR_BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000552 }
Spandan Das23511372025-01-08 23:30:45 +0000553 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000554 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/VENDOR_BOOT/vendor_bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000555 }
Spandan Das258c08f2025-01-08 23:30:45 +0000556 }
557 if a.partitionProps.Boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800558 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000559 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000560 builder.Command().Textf("echo %s > %s/BOOT/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/BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000563 }
564 if bootImgInfo.Kernel != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000565 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000566 // 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 +0000567 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/VENDOR_BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000568 }
Spandan Das23511372025-01-08 23:30:45 +0000569 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000570 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/BOOT/bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000571 }
Spandan Das258c08f2025-01-08 23:30:45 +0000572 }
573
Spandan Dase51ff952025-01-09 18:11:59 +0000574 if a.deviceProps.Android_info != nil {
575 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
Cole Faust11fda332025-01-14 16:47:19 -0800576 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 +0000577 }
578
Spandan Das0036fa32025-01-10 23:40:45 +0000579 a.copyImagesToTargetZip(ctx, builder, targetFilesDir)
Spandan Dasdad98702025-02-26 14:32:28 +0000580 a.copyMetadataToTargetZip(ctx, builder, targetFilesDir, allInstalledModules)
Spandan Das0036fa32025-01-10 23:40:45 +0000581
Cole Faust44080412024-12-20 14:17:07 -0800582 builder.Command().
583 BuiltTool("soong_zip").
584 Text("-d").
585 FlagWithOutput("-o ", targetFilesZip).
586 FlagWithArg("-C ", targetFilesDir.String()).
587 FlagWithArg("-D ", targetFilesDir.String()).
588 Text("-sha256")
589 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
590}
591
Spandan Das0036fa32025-01-10 23:40:45 +0000592func (a *androidDevice) copyImagesToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
593 // Create an IMAGES/ subdirectory
594 builder.Command().Textf("mkdir -p %s/IMAGES", targetFilesDir.String())
595 if a.deviceProps.Bootloader != nil {
596 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader))).Textf(" %s/IMAGES/bootloader", targetFilesDir.String())
597 }
598 // Copy the filesystem ,boot and vbmeta img files to IMAGES/
599 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(child android.ModuleProxy) {
Spandan Dasa9db76d2025-01-14 01:34:43 +0000600 if strings.Contains(child.Name(), "recovery") {
601 return // skip recovery.img to match the make packaging behavior
602 }
Spandan Dasef775742025-01-13 22:17:40 +0000603 if info, ok := android.OtherModuleProvider(ctx, child, BootimgInfoProvider); ok {
604 // Check Boot img first so that the boot.img is copied and not its dep ramdisk.img
Spandan Das0036fa32025-01-10 23:40:45 +0000605 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
Spandan Dasef775742025-01-13 22:17:40 +0000606 } else if info, ok := android.OtherModuleProvider(ctx, child, FilesystemProvider); ok {
Spandan Das0036fa32025-01-10 23:40:45 +0000607 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
608 } else if info, ok := android.OtherModuleProvider(ctx, child, vbmetaPartitionProvider); ok {
609 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
610 } else {
611 ctx.ModuleErrorf("Module %s does not provide an .img file output for target_files.zip", child.Name())
612 }
613 })
Spandan Dasef775742025-01-13 22:17:40 +0000614
615 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800616 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000617 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust19eb09d2025-01-14 13:27:00 -0800618 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000619 if info.SubImageInfo[partition].OutputHermetic != nil {
620 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].OutputHermetic).Textf(" %s/IMAGES/", targetFilesDir.String())
621 }
622 if info.SubImageInfo[partition].MapFile != nil {
623 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].MapFile).Textf(" %s/IMAGES/", targetFilesDir.String())
624 }
Spandan Dasef775742025-01-13 22:17:40 +0000625 }
626 } else {
627 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
628 }
629 }
Spandan Das0036fa32025-01-10 23:40:45 +0000630}
631
Spandan Dasdad98702025-02-26 14:32:28 +0000632func (a *androidDevice) copyMetadataToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath, allInstalledModules []android.Module) {
Spandan Das29d44882025-01-15 21:12:36 +0000633 // Create a META/ subdirectory
634 builder.Command().Textf("mkdir -p %s/META", targetFilesDir.String())
635 if proptools.Bool(a.deviceProps.Ab_ota_updater) {
636 ctx.VisitDirectDepsProxyWithTag(targetFilesMetadataDepTag, func(child android.ModuleProxy) {
637 info, _ := android.OtherModuleProvider(ctx, child, android.OutputFilesProvider)
638 builder.Command().Textf("cp").Inputs(info.DefaultOutputFiles).Textf(" %s/META/", targetFilesDir.String())
639 })
Spandan Dasb5f40cf2025-02-12 19:36:03 +0000640 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "external/zucchini/version_info.h")).Textf(" %s/META/zucchini_config.txt", targetFilesDir.String())
641 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "system/update_engine/update_engine.conf")).Textf(" %s/META/update_engine_config.txt", targetFilesDir.String())
642 if a.getFsInfos(ctx)["system"].ErofsCompressHints != nil {
643 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].ErofsCompressHints).Textf(" %s/META/erofs_default_compress_hints.txt", targetFilesDir.String())
644 }
645 // ab_partitions.txt
646 abPartitionsSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_partitions)
647 abPartitionsSortedString := proptools.ShellEscape(strings.Join(abPartitionsSorted, "\\n"))
648 builder.Command().Textf("echo -e").Flag(abPartitionsSortedString).Textf(" > %s/META/ab_partitions.txt", targetFilesDir.String())
Spandan Das35b78742025-02-12 19:36:03 +0000649 // otakeys.txt
650 abOtaKeysSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_keys)
651 abOtaKeysSortedString := proptools.ShellEscape(strings.Join(abOtaKeysSorted, "\\n"))
652 builder.Command().Textf("echo -e").Flag(abOtaKeysSortedString).Textf(" > %s/META/otakeys.txt", targetFilesDir.String())
Spandan Das00948072025-02-12 19:36:03 +0000653 // postinstall_config.txt
654 abOtaPostInstallConfigString := proptools.ShellEscape(strings.Join(a.deviceProps.Ab_ota_postinstall_config, "\\n"))
655 builder.Command().Textf("echo -e").Flag(abOtaPostInstallConfigString).Textf(" > %s/META/postinstall_config.txt", targetFilesDir.String())
Spandan Dasf12ff9b2025-02-12 22:27:43 +0000656 // selinuxfc
657 if a.getFsInfos(ctx)["system"].SelinuxFc != nil {
658 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].SelinuxFc).Textf(" %s/META/file_contexts.bin", targetFilesDir.String())
659 }
Spandan Dasd71af182025-02-12 18:03:29 +0000660 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000661 // Copy $partition_filesystem_config.txt
662 fsInfos := a.getFsInfos(ctx)
663 for _, partition := range android.SortedKeys(fsInfos) {
664 if fsInfos[partition].FilesystemConfig == nil {
665 continue
666 }
667 if android.InList(partition, []string{"userdata"}) {
668 continue
669 }
Spandan Das853a7a42025-02-27 07:44:51 +0000670 if partition != "vendor_ramdisk" {
671 // vendor_ramdisk will be handled separately.
672 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/%s", targetFilesDir.String(), a.filesystemConfigNameForTargetFiles(partition))
673 }
Spandan Das54af3572025-02-27 07:42:08 +0000674 if partition == "ramdisk" {
675 // Create an additional copy at boot_filesystem_config.txt
676 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/boot_filesystem_config.txt", targetFilesDir.String())
677 }
Spandan Das853a7a42025-02-27 07:44:51 +0000678 if partition == "system" {
679 // Create root_filesystem_config from the assembled ROOT/ intermediates directory
Spandan Dase06dd112025-03-05 01:29:04 +0000680 a.generateFilesystemConfigForTargetFiles(ctx, builder, a.rootDirForFsConfigTimestamp, targetFilesDir.String(), a.rootDirForFsConfig, "root_filesystem_config.txt")
Spandan Das853a7a42025-02-27 07:44:51 +0000681 }
682 if partition == "vendor_ramdisk" {
683 // Create vendor_boot_filesystem_config from the assembled VENDOR_BOOT/RAMDISK intermediates directory
Spandan Dase06dd112025-03-05 01:29:04 +0000684 a.generateFilesystemConfigForTargetFiles(ctx, builder, nil, targetFilesDir.String(), targetFilesDir.String()+"/VENDOR_BOOT/RAMDISK", "vendor_boot_filesystem_config.txt")
Spandan Das853a7a42025-02-27 07:44:51 +0000685 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000686 }
Spandan Das75955b12025-02-13 22:12:52 +0000687 // Copy ramdisk_node_list
Spandan Das37240d92025-02-14 00:18:41 +0000688 if ramdiskNodeList := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Ramdisk_node_list)); ramdiskNodeList != nil {
689 builder.Command().Textf("cp").Input(ramdiskNodeList).Textf(" %s/META/", targetFilesDir.String())
690 }
691 // Copy releasetools.py
692 if releaseTools := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Releasetools_extension)); releaseTools != nil {
693 builder.Command().Textf("cp").Input(releaseTools).Textf(" %s/META/", targetFilesDir.String())
694 }
Spandan Dasdad98702025-02-26 14:32:28 +0000695 // apexkeys.txt
696 var installedApexKeys []android.Path
697 for _, installedModule := range allInstalledModules {
698 if info, ok := android.OtherModuleProvider(ctx, installedModule, ApexKeyPathInfoProvider); ok {
699 installedApexKeys = append(installedApexKeys, info.ApexKeyPath)
700 }
701 }
702 installedApexKeys = android.SortedUniquePaths(installedApexKeys) // Sort by keypath to match make
703 builder.Command().Text("cat").Inputs(installedApexKeys).Textf(" >> %s/META/apexkeys.txt", targetFilesDir.String())
Spandan Das38afe712025-03-05 23:27:55 +0000704 // apkcerts.txt
705 builder.Command().Textf("cp").Input(a.apkCertsInfo).Textf(" %s/META/", targetFilesDir.String())
706
Spandan Das3dfa17f2025-02-28 09:48:28 +0000707 // Copy fastboot-info.txt
708 if fastbootInfo := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.FastbootInfo)); fastbootInfo != nil {
709 // TODO (b/399788523): Autogenerate fastboot-info.txt if there is no source fastboot-info.txt
710 // https://cs.android.com/android/_/android/platform/build/+/80b9546f8f69e78b8fe1870e0e745d70fc18dfcd:core/Makefile;l=5831-5893;drc=077490384423dff9eac954da5c001c6f0be3fa6e;bpv=0;bpt=0
711 builder.Command().Textf("cp").Input(fastbootInfo).Textf(" %s/META/fastboot-info.txt", targetFilesDir.String())
712 }
Spandan Dasdad98702025-02-26 14:32:28 +0000713
Spandan Dasc09353c2025-03-04 00:11:21 +0000714 // kernel_configs.txt and kernel_version.txt
715 if a.kernelConfig != nil {
716 builder.Command().Textf("cp").Input(a.kernelConfig).Textf(" %s/META/", targetFilesDir.String())
717 }
718 if a.kernelVersion != nil {
719 builder.Command().Textf("cp").Input(a.kernelVersion).Textf(" %s/META/", targetFilesDir.String())
720 }
Spandan Das447a0ab2025-03-04 23:10:19 +0000721 // misc_info.txt
722 if a.miscInfo != nil {
723 builder.Command().Textf("cp").Input(a.miscInfo).Textf(" %s/META/", targetFilesDir.String())
724 }
Spandan Das4100dd02025-03-04 23:14:34 +0000725 // apex_info.pb, care_map.pb, vbmeta_digest.txt
726 a.addImgToTargetFiles(ctx, builder, targetFilesDir.String())
Spandan Dasc09353c2025-03-04 00:11:21 +0000727
Spandan Dasf079f0d2025-02-27 13:13:50 +0000728 if a.partitionProps.Super_partition_name != nil {
729 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
730 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
731 // dynamic_partitions_info.txt
732 // TODO (b/390192334): Add `building_super_empty_partition=true`
733 builder.Command().Text("cp").Input(info.DynamicPartitionsInfo).Textf(" %s/META/", targetFilesDir.String())
734 } else {
735 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
736 }
737 }
738
Spandan Dasdd262fb2025-02-13 00:15:59 +0000739}
740
Spandan Das447a0ab2025-03-04 23:10:19 +0000741// A partial implementation of make's $PRODUCT_OUT/misc_info.txt
742// 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
743// This file is subsequently used by add_img_to_target_files to create additioanl metadata files like apex_info.pb
744// TODO (b/399788119): Complete the migration of misc_info.txt
745func (a *androidDevice) addMiscInfo(ctx android.ModuleContext) android.Path {
746 builder := android.NewRuleBuilder(pctx, ctx)
747 miscInfo := android.PathForModuleOut(ctx, "misc_info.txt")
748 builder.Command().
749 Textf("rm -f %s", miscInfo).
750 Textf("&& echo recovery_api_version=%s >> %s", ctx.Config().VendorConfig("recovery").String("recovery_api_version"), miscInfo).
751 Textf("&& echo fstab_version=%s >> %s", ctx.Config().VendorConfig("recovery").String("recovery_fstab_version"), miscInfo).
752 ImplicitOutput(miscInfo)
753
754 if a.partitionProps.Recovery_partition_name == nil {
755 builder.Command().Textf("echo no_recovery=true >> %s", miscInfo)
756 }
757 fsInfos := a.getFsInfos(ctx)
758 for _, partition := range android.SortedKeys(fsInfos) {
759 if fsInfos[partition].UseAvb {
760 builder.Command().Textf("echo 'avb_%s_hashtree_enable=true' >> %s", partition, miscInfo)
761 }
762 }
763 if len(a.partitionProps.Vbmeta_partitions) > 0 {
764 builder.Command().
765 Textf("echo avb_enable=true >> %s", miscInfo).
766 Textf("&& echo avb_building_vbmeta_image=true >> %s", miscInfo).
767 Textf("&& echo avb_avbtool=avbtool >> %s", miscInfo)
768 }
769 if a.partitionProps.Boot_partition_name != nil {
770 builder.Command().Textf("echo boot_images=boot.img >> %s", miscInfo)
771 }
772
773 builder.Build("misc_info", "Building misc_info")
774
775 return miscInfo
776}
777
Spandan Das4100dd02025-03-04 23:14:34 +0000778// addImgToTargetFiles invokes `add_img_to_target_files` and creates the following files in META/
779// - apex_info.pb
780// - care_map.pb
781// - vbmeta_digest.txt
782func (a *androidDevice) addImgToTargetFiles(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir string) {
783 mkbootimg := ctx.Config().HostToolPath(ctx, "mkbootimg")
784 builder.Command().
785 Textf("PATH=%s:$PATH", ctx.Config().HostToolDir()).
786 Textf("MKBOOTIMG=%s", mkbootimg).
787 Implicit(mkbootimg).
788 BuiltTool("add_img_to_target_files").
789 Flag("-a -v -p").
790 Flag(ctx.Config().HostToolDir()).
791 Text(targetFilesDir)
792}
793
Spandan Dasdad98702025-02-26 14:32:28 +0000794type ApexKeyPathInfo struct {
795 ApexKeyPath android.Path
796}
797
798var ApexKeyPathInfoProvider = blueprint.NewProvider[ApexKeyPathInfo]()
799
Spandan Dase06dd112025-03-05 01:29:04 +0000800func (a *androidDevice) generateFilesystemConfigForTargetFiles(ctx android.ModuleContext, builder *android.RuleBuilder, stagingDirTimestamp android.Path, targetFilesDir, stagingDir, filename string) {
Spandan Das853a7a42025-02-27 07:44:51 +0000801 fsConfigOut := android.PathForModuleOut(ctx, filename)
802 ctx.Build(pctx, android.BuildParams{
Spandan Dase06dd112025-03-05 01:29:04 +0000803 Rule: fsConfigRule,
804 Implicit: stagingDirTimestamp,
805 Output: fsConfigOut,
Spandan Das853a7a42025-02-27 07:44:51 +0000806 Args: map[string]string{
807 "rootDir": stagingDir,
808 "prefix": "",
809 },
810 })
811 builder.Command().Textf("cp").Input(fsConfigOut).Textf(" %s/META/", targetFilesDir)
812}
813
Spandan Dasdd262fb2025-02-13 00:15:59 +0000814// Filenames for the partition specific fs_config files.
815// Hardcode the ramdisk files to their boot image prefix
816func (a *androidDevice) filesystemConfigNameForTargetFiles(partition string) string {
817 name := partition + "_filesystem_config.txt"
818 if partition == "system" {
819 name = "filesystem_config.txt"
820 } else if partition == "ramdisk" {
821 name = "init_boot_filesystem_config.txt"
822 }
823 return name
Spandan Das29d44882025-01-15 21:12:36 +0000824}
825
Cole Faust44080412024-12-20 14:17:07 -0800826func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
Cole Faust19eb09d2025-01-14 13:27:00 -0800827 fsMod := ctx.GetDirectDepProxyWithTag(depName, filesystemDepTag)
Cole Faust44080412024-12-20 14:17:07 -0800828 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
829 if !ok {
830 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
831 }
832 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000833}
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000834
835func (a *androidDevice) setVbmetaPhonyTargets(ctx android.ModuleContext) {
836 if !proptools.Bool(a.deviceProps.Main_device) {
837 return
838 }
839
840 if !ctx.Config().KatiEnabled() {
841 for _, vbmetaPartitionName := range a.partitionProps.Vbmeta_partitions {
842 img := ctx.GetDirectDepProxyWithTag(vbmetaPartitionName, filesystemDepTag)
843 if provider, ok := android.OtherModuleProvider(ctx, img, vbmetaPartitionProvider); ok {
844 // make generates `vbmetasystemimage` phony target instead of `vbmeta_systemimage` phony target.
845 partitionName := strings.ReplaceAll(provider.Name, "_", "")
846 ctx.Phony(fmt.Sprintf("%simage", partitionName), provider.Output)
847 }
848 }
849 }
850}
Cole Faust21c11462025-03-03 14:13:17 -0800851
852func (a *androidDevice) getKernel(ctx android.ModuleContext) android.Path {
853 if a.partitionProps.Boot_partition_name != nil {
854 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
855 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
856 return bootImgInfo.Kernel
857 }
858 return nil
859}
860
861// Gets the kernel version and configs from the actual kernel file itself. Roughly equivalent to
862// this make code: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=5443;drc=c0b66fc59de069e06ce0ffd703d4d21613be30c6
863// However, it is a simplified version of that make code. Differences include:
864// - Not handling BOARD_KERNEL_CONFIG_FILE because BOARD_KERNEL_CONFIG_FILE was never used.
865// - Not unpacking the bootimage, as we should be able to just always export the kernel directly
866// in the BootimgInfo. We don't currently support prebuilt boot images, but even if we add that
867// in the future, it can be done in a prebuilt_bootimage module type that still exports the same
868// BootimgInfo.
869// - We don't print a warning and output '<unknown-kernel>' to kernel_version_for_uffd_gc.txt
870// because we expect the kernel to always be present. If it's not, we will get an error that
871// kernel_version_for_uffd_gc.txt doesn't exist. This may require later tweaking to the
872// dexpreopt rules so that they don't attempt to access that file in builds that don't have
873// a kernel.
874func (a *androidDevice) extractKernelVersionAndConfigs(ctx android.ModuleContext) (android.Path, android.Path) {
875 kernel := a.getKernel(ctx)
876 // If there's no kernel, don't create kernel version / kernel config files. Reverse dependencies
877 // on those files have to account for this, for example by disabling dexpreopt in unbundled
878 // builds.
879 if kernel == nil {
880 return nil, nil
881 }
882
883 lz4tool := ctx.Config().HostToolPath(ctx, "lz4")
884
885 extractedVersionFile := android.PathForModuleOut(ctx, "kernel_version.txt")
886 extractedConfigsFile := android.PathForModuleOut(ctx, "kernel_configs.txt")
887 builder := android.NewRuleBuilder(pctx, ctx)
888 builder.Command().BuiltTool("extract_kernel").
889 Flag("--tools lz4:"+lz4tool.String()).Implicit(lz4tool).
890 FlagWithInput("--input ", kernel).
891 FlagWithOutput("--output-release ", extractedVersionFile).
Spandan Das2dee3f72025-03-07 18:32:06 +0000892 FlagWithOutput("--output-configs ", extractedConfigsFile).
893 Textf(`&& printf "\n" >> %s`, extractedVersionFile)
Cole Faust21c11462025-03-03 14:13:17 -0800894
895 if specifiedVersion := proptools.String(a.deviceProps.Kernel_version); specifiedVersion != "" {
896 specifiedVersionFile := android.PathForModuleOut(ctx, "specified_kernel_version.txt")
Spandan Das2dee3f72025-03-07 18:32:06 +0000897 android.WriteFileRule(ctx, specifiedVersionFile, specifiedVersion)
Cole Faust21c11462025-03-03 14:13:17 -0800898 builder.Command().Text("diff -q").
899 Input(specifiedVersionFile).
900 Input(extractedVersionFile).
901 Textf(`|| (echo "Specified kernel version '$(cat %s)' does not match actual kernel version '$(cat %s)'"; exit 1)`, specifiedVersionFile, extractedVersionFile)
902 }
903
904 builder.Build("extract_kernel_info", "Extract kernel version and configs")
905
906 if proptools.Bool(a.deviceProps.Main_device) && !ctx.Config().KatiEnabled() {
907 if ctx.Config().EnableUffdGc() == "default" {
908 kernelVersionFile := android.PathForOutput(ctx, "dexpreopt/kernel_version_for_uffd_gc.txt")
909 ctx.Build(pctx, android.BuildParams{
910 Rule: android.CpIfChanged,
911 Input: extractedVersionFile,
912 Output: kernelVersionFile,
913 })
914 }
915
916 ctx.DistForGoal("droid_targets", extractedVersionFile)
917 }
918
919 return extractedVersionFile, extractedConfigsFile
920}
Spandan Das38afe712025-03-05 23:27:55 +0000921
922func (a *androidDevice) buildApkCertsInfo(ctx android.ModuleContext, allInstalledModules []android.Module) android.Path {
923 // TODO (spandandas): Add compressed
924 formatLine := func(cert java.Certificate, name, partition string) string {
925 pem := cert.AndroidMkString()
926 var key string
927 if cert.Key == nil {
928 key = ""
929 } else {
930 key = cert.Key.String()
931 }
932 return fmt.Sprintf(`name="%s" certificate="%s" private_key="%s" partition="%s"`, name, pem, key, partition)
933 }
934
935 apkCerts := []string{}
936 for _, installedModule := range allInstalledModules {
937 partition := ""
938 if commonInfo, ok := android.OtherModuleProvider(ctx, installedModule, android.CommonModuleInfoKey); ok {
939 partition = commonInfo.PartitionTag
940 } else {
941 ctx.ModuleErrorf("%s does not set CommonModuleInfoKey", installedModule.Name())
942 }
943 if info, ok := android.OtherModuleProvider(ctx, installedModule, java.AppInfoProvider); ok {
944 apkCerts = append(apkCerts, formatLine(info.Certificate, info.InstallApkName+".apk", partition))
945 } else if info, ok := android.OtherModuleProvider(ctx, installedModule, java.AppInfosProvider); ok {
946 for _, certInfo := range info {
947 apkCerts = append(apkCerts, formatLine(certInfo.Certificate, certInfo.InstallApkName+".apk", partition))
948 }
949 } else if info, ok := android.OtherModuleProvider(ctx, installedModule, java.RuntimeResourceOverlayInfoProvider); ok {
950 apkCerts = append(apkCerts, formatLine(info.Certificate, info.OutputFile.Base(), partition))
951 }
952 }
953 slices.Sort(apkCerts) // sort by name
954 fsInfos := a.getFsInfos(ctx)
955 if fsInfos["system"].HasFsverity {
956 defaultPem, defaultKey := ctx.Config().DefaultAppCertificate(ctx)
957 apkCerts = append(apkCerts, formatLine(java.Certificate{Pem: defaultPem, Key: defaultKey}, "BuildManifest.apk", "system"))
958 if info, ok := fsInfos["system_ext"]; ok && info.HasFsverity {
959 apkCerts = append(apkCerts, formatLine(java.Certificate{Pem: defaultPem, Key: defaultKey}, "BuildManifestSystemExt.apk", "system_ext"))
960 }
961 }
962
963 apkCertsInfo := android.PathForModuleOut(ctx, "apkcerts.txt")
964 android.WriteFileRuleVerbatim(ctx, apkCertsInfo, strings.Join(apkCerts, "\n")+"\n")
965 return apkCertsInfo
966}