blob: 56a0a84439f5c0416f40454f0517f08dd1805b06 [file] [log] [blame]
Jihoon Kangf2c53982024-10-09 17:32:52 +00001// Copyright (C) 2024 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package filesystem
16
17import (
Jihoon Kangabec3ec2025-02-19 00:55:10 +000018 "cmp"
Jihoon Kang0a6315b2025-01-30 01:14:49 +000019 "fmt"
Cole Faust1dcf9e42025-02-19 17:23:34 -080020 "path/filepath"
Jihoon Kangabec3ec2025-02-19 00:55:10 +000021 "slices"
Spandan Das258c08f2025-01-08 23:30:45 +000022 "strings"
Cole Faustb55a41c2025-01-09 16:53:58 -080023 "sync/atomic"
Spandan Das258c08f2025-01-08 23:30:45 +000024
Jihoon Kangf2c53982024-10-09 17:32:52 +000025 "android/soong/android"
Cole Faust1dcf9e42025-02-19 17:23:34 -080026 "android/soong/java"
Jihoon Kangf2c53982024-10-09 17:32:52 +000027
28 "github.com/google/blueprint"
29 "github.com/google/blueprint/proptools"
30)
31
Cole Faust1dcf9e42025-02-19 17:23:34 -080032var proguardDictToProto = pctx.AndroidStaticRule("proguard_dict_to_proto", blueprint.RuleParams{
33 Command: `${symbols_map} -r8 $in -location $location -write_if_changed $out`,
34 Restat: true,
35 CommandDeps: []string{"${symbols_map}"},
36}, "location")
37
Jihoon Kangf2c53982024-10-09 17:32:52 +000038type PartitionNameProperties struct {
Cole Faust2bdc5e52025-01-10 10:29:36 -080039 // Name of the super partition filesystem module
40 Super_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000041 // Name of the boot partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000042 Boot_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000043 // Name of the vendor boot partition filesystem module
44 Vendor_boot_partition_name *string
45 // Name of the init boot partition filesystem module
46 Init_boot_partition_name *string
47 // Name of the system partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000048 System_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000049 // Name of the system_ext partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000050 System_ext_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000051 // Name of the product partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000052 Product_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000053 // Name of the vendor partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000054 Vendor_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000055 // Name of the odm partition filesystem module
Spandan Dasc5717162024-11-01 18:33:57 +000056 Odm_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000057 // Name of the recovery partition filesystem module
58 Recovery_partition_name *string
Cole Faust3552eb62024-11-06 18:07:26 -080059 // The vbmeta partition and its "chained" partitions
60 Vbmeta_partitions []string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000061 // Name of the userdata partition filesystem module
mrziwang23ba8762024-11-07 16:21:53 -080062 Userdata_partition_name *string
Spandan Dasa0394002025-01-07 18:38:34 +000063 // Name of the system_dlkm partition filesystem module
64 System_dlkm_partition_name *string
65 // Name of the vendor_dlkm partition filesystem module
66 Vendor_dlkm_partition_name *string
67 // Name of the odm_dlkm partition filesystem module
68 Odm_dlkm_partition_name *string
Jihoon Kangf2c53982024-10-09 17:32:52 +000069}
70
Jihoon Kang3be17162025-01-09 20:51:54 +000071type DeviceProperties struct {
72 // Path to the prebuilt bootloader that would be copied to PRODUCT_OUT
73 Bootloader *string `android:"path"`
Spandan Dase51ff952025-01-09 18:11:59 +000074 // Path to android-info.txt file containing board specific info.
75 Android_info *string `android:"path"`
Cole Faust11fda332025-01-14 16:47:19 -080076 // If this is the "main" android_device target for the build, i.e. the one that gets built
77 // when running a plain `m` command. Currently, this is the autogenerated android_device module
78 // in soong-only builds, but in the future when we check in android_device modules, the main
79 // one will be determined based on the lunch product. TODO: Figure out how to make this
80 // blueprint:"mutated" and still set it from filesystem_creator
81 Main_device *bool
Spandan Das29d44882025-01-15 21:12:36 +000082
Spandan Das00948072025-02-12 19:36:03 +000083 Ab_ota_updater *bool
84 Ab_ota_partitions []string
85 Ab_ota_keys []string
86 Ab_ota_postinstall_config []string
Spandan Das75955b12025-02-13 22:12:52 +000087
Spandan Das37240d92025-02-14 00:18:41 +000088 Ramdisk_node_list *string `android:"path"`
89 Releasetools_extension *string `android:"path"`
Spandan Das3dfa17f2025-02-28 09:48:28 +000090 FastbootInfo *string `android:"path"`
Cole Faust21c11462025-03-03 14:13:17 -080091
92 // The kernel version in the build. Will be verified against the actual kernel.
93 // If not provided, will attempt to extract it from the loose kernel or the kernel inside
94 // the boot image. The version is later used to decide whether or not to enable uffd_gc
95 // when dexpreopting apps. So setting this doesn't really do anything except enforce that the
96 // actual kernel version is as specified here.
97 Kernel_version *string
Jihoon Kang3be17162025-01-09 20:51:54 +000098}
99
Jihoon Kangf2c53982024-10-09 17:32:52 +0000100type androidDevice struct {
101 android.ModuleBase
102
103 partitionProps PartitionNameProperties
Jihoon Kang3be17162025-01-09 20:51:54 +0000104
105 deviceProps DeviceProperties
Cole Fausta472a6f2025-02-10 16:10:04 -0800106
107 allImagesZip android.Path
Cole Faust54124c82025-02-21 14:46:02 -0800108
109 proguardDictZip android.Path
110 proguardDictMapping android.Path
111 proguardUsageZip android.Path
Jihoon Kangf2c53982024-10-09 17:32:52 +0000112}
113
114func AndroidDeviceFactory() android.Module {
115 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +0000116 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -0800117 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000118 return module
119}
120
Cole Faust11fda332025-01-14 16:47:19 -0800121var numMainAndroidDevicesOnceKey android.OnceKey = android.NewOnceKey("num_auto_generated_anroid_devices")
Cole Faustb55a41c2025-01-09 16:53:58 -0800122
Jihoon Kangf2c53982024-10-09 17:32:52 +0000123type partitionDepTagType struct {
124 blueprint.BaseDependencyTag
125}
126
Cole Faust2bdc5e52025-01-10 10:29:36 -0800127type superPartitionDepTagType struct {
128 blueprint.BaseDependencyTag
129}
Spandan Das29d44882025-01-15 21:12:36 +0000130type targetFilesMetadataDepTagType struct {
131 blueprint.BaseDependencyTag
132}
Cole Faust2bdc5e52025-01-10 10:29:36 -0800133
134var superPartitionDepTag superPartitionDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000135var filesystemDepTag partitionDepTagType
Spandan Das29d44882025-01-15 21:12:36 +0000136var targetFilesMetadataDepTag targetFilesMetadataDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000137
138func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
139 addDependencyIfDefined := func(dep *string) {
140 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -0800141 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +0000142 }
143 }
144
Cole Faust2bdc5e52025-01-10 10:29:36 -0800145 if a.partitionProps.Super_partition_name != nil {
146 ctx.AddDependency(ctx.Module(), superPartitionDepTag, *a.partitionProps.Super_partition_name)
147 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000148 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +0000149 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000150 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000151 addDependencyIfDefined(a.partitionProps.System_partition_name)
152 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
153 addDependencyIfDefined(a.partitionProps.Product_partition_name)
154 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000155 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800156 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000157 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
158 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
159 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000160 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800161 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
162 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
163 }
Spandan Das29d44882025-01-15 21:12:36 +0000164 a.addDepsForTargetFilesMetadata(ctx)
165}
166
167func (a *androidDevice) addDepsForTargetFilesMetadata(ctx android.BottomUpMutatorContext) {
168 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), targetFilesMetadataDepTag, "liblz4") // host variant
Jihoon Kangf2c53982024-10-09 17:32:52 +0000169}
170
Cole Faust11fda332025-01-14 16:47:19 -0800171func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
172 if proptools.Bool(a.deviceProps.Main_device) {
173 numMainAndroidDevices := ctx.Config().Once(numMainAndroidDevicesOnceKey, func() interface{} {
174 return &atomic.Int32{}
175 }).(*atomic.Int32)
176 total := numMainAndroidDevices.Add(1)
177 if total > 1 {
178 // There should only be 1 main android_device module. That one will be
179 // made the default thing to build in soong-only builds.
180 ctx.ModuleErrorf("There cannot be more than 1 main android_device module")
181 }
Jihoon Kang3be17162025-01-09 20:51:54 +0000182 }
183
Cole Faust1dcf9e42025-02-19 17:23:34 -0800184 allInstalledModules := a.allInstalledModules(ctx)
185
Spandan Dasdad98702025-02-26 14:32:28 +0000186 a.buildTargetFilesZip(ctx, allInstalledModules)
Cole Faust1dcf9e42025-02-19 17:23:34 -0800187 a.buildProguardZips(ctx, allInstalledModules)
Cole Faust21c11462025-03-03 14:13:17 -0800188 a.extractKernelVersionAndConfigs(ctx)
Cole Faust1dcf9e42025-02-19 17:23:34 -0800189
mrziwang2fd33a72025-01-08 12:22:08 -0800190 var deps []android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800191 if proptools.String(a.partitionProps.Super_partition_name) != "" {
Cole Faust19eb09d2025-01-14 13:27:00 -0800192 superImage := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Cole Faust2bdc5e52025-01-10 10:29:36 -0800193 if info, ok := android.OtherModuleProvider(ctx, superImage, SuperImageProvider); ok {
194 assertUnset := func(prop *string, propName string) {
195 if prop != nil && *prop != "" {
196 ctx.PropertyErrorf(propName, "Cannot be set because it's already part of the super image")
197 }
198 }
199 for _, subPartitionType := range android.SortedKeys(info.SubImageInfo) {
200 switch subPartitionType {
201 case "system":
202 assertUnset(a.partitionProps.System_partition_name, "system_partition_name")
203 case "system_ext":
204 assertUnset(a.partitionProps.System_ext_partition_name, "system_ext_partition_name")
205 case "system_dlkm":
206 assertUnset(a.partitionProps.System_dlkm_partition_name, "system_dlkm_partition_name")
207 case "system_other":
208 // TODO
209 case "product":
210 assertUnset(a.partitionProps.Product_partition_name, "product_partition_name")
211 case "vendor":
212 assertUnset(a.partitionProps.Vendor_partition_name, "vendor_partition_name")
213 case "vendor_dlkm":
214 assertUnset(a.partitionProps.Vendor_dlkm_partition_name, "vendor_dlkm_partition_name")
215 case "odm":
216 assertUnset(a.partitionProps.Odm_partition_name, "odm_partition_name")
217 case "odm_dlkm":
218 assertUnset(a.partitionProps.Odm_dlkm_partition_name, "odm_dlkm_partition_name")
219 default:
220 ctx.ModuleErrorf("Unsupported sub-partition of super partition: %q", subPartitionType)
221 }
222 }
223
224 deps = append(deps, info.SuperImage)
225 } else {
226 ctx.ModuleErrorf("Expected super image dep to provide SuperImageProvider")
227 }
228 }
Cole Faust19eb09d2025-01-14 13:27:00 -0800229 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(m android.ModuleProxy) {
mrziwang2fd33a72025-01-08 12:22:08 -0800230 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
231 if !ok {
232 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
233 }
234 if len(imageOutput.DefaultOutputFiles) != 1 {
235 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
236 }
237 deps = append(deps, imageOutput.DefaultOutputFiles[0])
238 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000239
Cole Fausta472a6f2025-02-10 16:10:04 -0800240 allImagesZip := android.PathForModuleOut(ctx, "all_images.zip")
241 allImagesZipBuilder := android.NewRuleBuilder(pctx, ctx)
Cole Faust8967d752025-02-19 17:27:29 -0800242 cmd := allImagesZipBuilder.Command().BuiltTool("soong_zip")
Cole Fausta472a6f2025-02-10 16:10:04 -0800243 for _, dep := range deps {
244 cmd.FlagWithArg("-e ", dep.Base())
245 cmd.FlagWithInput("-f ", dep)
246 }
247 cmd.FlagWithOutput("-o ", allImagesZip)
248 allImagesZipBuilder.Build("soong_all_images_zip", "all_images.zip")
249 a.allImagesZip = allImagesZip
250
Cole Faustb55a41c2025-01-09 16:53:58 -0800251 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
Cole Faust11fda332025-01-14 16:47:19 -0800252 var validations android.Paths
253 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Cole Faustb55a41c2025-01-09 16:53:58 -0800254 // In soong-only builds, build this module by default.
255 // This is the analogue to this make code:
256 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
257 ctx.Phony("droidcore-unbundled", allImagesStamp)
Cole Faust11fda332025-01-14 16:47:19 -0800258
Cole Faust19fbb072025-01-30 18:19:29 -0800259 deps = append(deps, a.copyFilesToProductOutForSoongOnly(ctx))
Cole Faustb55a41c2025-01-09 16:53:58 -0800260 }
Cole Faust11fda332025-01-14 16:47:19 -0800261
262 ctx.Build(pctx, android.BuildParams{
263 Rule: android.Touch,
264 Output: allImagesStamp,
265 Implicits: deps,
266 Validations: validations,
267 })
268
269 // Checkbuilding it causes soong to make a phony, so you can say `m <module name>`
270 ctx.CheckbuildFile(allImagesStamp)
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000271
272 a.setVbmetaPhonyTargets(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000273
274 a.distFiles(ctx)
275}
276
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000277// Returns a list of modules that are installed, which are collected from the dependency
278// filesystem and super_image modules.
279func (a *androidDevice) allInstalledModules(ctx android.ModuleContext) []android.Module {
280 fsInfoMap := a.getFsInfos(ctx)
281 allOwners := make(map[string][]string)
282 for _, partition := range android.SortedKeys(fsInfoMap) {
283 fsInfo := fsInfoMap[partition]
284 for _, owner := range fsInfo.Owners {
285 allOwners[owner.Name] = append(allOwners[owner.Name], owner.Variation)
286 }
287 }
288
289 ret := []android.Module{}
290 ctx.WalkDepsProxy(func(mod, _ android.ModuleProxy) bool {
Spandan Das5c78fe92025-02-26 14:25:56 +0000291 if variations, ok := allOwners[ctx.OtherModuleName(mod)]; ok && android.InList(ctx.OtherModuleSubDir(mod), variations) {
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000292 ret = append(ret, mod)
293 }
294 return true
295 })
296
297 // Remove duplicates
298 ret = android.FirstUniqueFunc(ret, func(a, b android.Module) bool {
299 return a.String() == b.String()
300 })
301
302 // Sort the modules by their names and variants
303 slices.SortFunc(ret, func(a, b android.Module) int {
304 return cmp.Compare(a.String(), b.String())
305 })
306 return ret
307}
308
Cole Faust54124c82025-02-21 14:46:02 -0800309func insertBeforeExtension(file, insertion string) string {
310 ext := filepath.Ext(file)
311 return strings.TrimSuffix(file, ext) + insertion + ext
312}
313
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000314func (a *androidDevice) distFiles(ctx android.ModuleContext) {
Cole Faust54124c82025-02-21 14:46:02 -0800315 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
316 fsInfoMap := a.getFsInfos(ctx)
317 for _, partition := range android.SortedKeys(fsInfoMap) {
318 fsInfo := fsInfoMap[partition]
319 if fsInfo.InstalledFiles.Json != nil {
320 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Json)
321 }
322 if fsInfo.InstalledFiles.Txt != nil {
323 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Txt)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000324 }
325 }
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000326
Cole Faust54124c82025-02-21 14:46:02 -0800327 namePrefix := ""
328 if ctx.Config().HasDeviceProduct() {
329 namePrefix = ctx.Config().DeviceProduct() + "-"
330 }
331 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardDictZip, namePrefix+insertBeforeExtension(a.proguardDictZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
332 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardDictMapping, namePrefix+insertBeforeExtension(a.proguardDictMapping.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
333 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardUsageZip, namePrefix+insertBeforeExtension(a.proguardUsageZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
334 }
Cole Faust44080412024-12-20 14:17:07 -0800335}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000336
Yu Liu2a815b62025-02-21 20:46:25 +0000337func (a *androidDevice) MakeVars(_ android.MakeVarsModuleContext) []android.ModuleMakeVarsValue {
Cole Fausta472a6f2025-02-10 16:10:04 -0800338 if proptools.Bool(a.deviceProps.Main_device) {
Yu Liu2a815b62025-02-21 20:46:25 +0000339 return []android.ModuleMakeVarsValue{{"SOONG_ONLY_ALL_IMAGES_ZIP", a.allImagesZip.String()}}
Cole Fausta472a6f2025-02-10 16:10:04 -0800340 }
Yu Liu2a815b62025-02-21 20:46:25 +0000341 return nil
Cole Fausta472a6f2025-02-10 16:10:04 -0800342}
343
Cole Faust1dcf9e42025-02-19 17:23:34 -0800344func (a *androidDevice) buildProguardZips(ctx android.ModuleContext, allInstalledModules []android.Module) {
345 dictZip := android.PathForModuleOut(ctx, "proguard-dict.zip")
346 dictZipBuilder := android.NewRuleBuilder(pctx, ctx)
347 dictZipCmd := dictZipBuilder.Command().BuiltTool("soong_zip").Flag("-d").FlagWithOutput("-o ", dictZip)
348
349 dictMapping := android.PathForModuleOut(ctx, "proguard-dict-mapping.textproto")
350 dictMappingBuilder := android.NewRuleBuilder(pctx, ctx)
351 dictMappingCmd := dictMappingBuilder.Command().BuiltTool("symbols_map").Flag("-merge").Output(dictMapping)
352
353 protosDir := android.PathForModuleOut(ctx, "proguard_mapping_protos")
354
355 usageZip := android.PathForModuleOut(ctx, "proguard-usage.zip")
356 usageZipBuilder := android.NewRuleBuilder(pctx, ctx)
357 usageZipCmd := usageZipBuilder.Command().BuiltTool("merge_zips").Output(usageZip)
358
359 for _, mod := range allInstalledModules {
360 if proguardInfo, ok := android.OtherModuleProvider(ctx, mod, java.ProguardProvider); ok {
361 // Maintain these out/target/common paths for backwards compatibility. They may be able
362 // to be changed if tools look up file locations from the protobuf, but I'm not
363 // exactly sure how that works.
364 dictionaryFakePath := fmt.Sprintf("out/target/common/obj/%s/%s_intermediates/proguard_dictionary", proguardInfo.Class, proguardInfo.ModuleName)
365 dictZipCmd.FlagWithArg("-e ", dictionaryFakePath)
366 dictZipCmd.FlagWithInput("-f ", proguardInfo.ProguardDictionary)
367 dictZipCmd.Textf("-e out/target/common/obj/%s/%s_intermediates/classes.jar", proguardInfo.Class, proguardInfo.ModuleName)
368 dictZipCmd.FlagWithInput("-f ", proguardInfo.ClassesJar)
369
370 protoFile := protosDir.Join(ctx, filepath.Dir(dictionaryFakePath), "proguard_dictionary.textproto")
371 ctx.Build(pctx, android.BuildParams{
372 Rule: proguardDictToProto,
373 Input: proguardInfo.ProguardDictionary,
374 Output: protoFile,
375 Args: map[string]string{
376 "location": dictionaryFakePath,
377 },
378 })
379 dictMappingCmd.Input(protoFile)
380
381 usageZipCmd.Input(proguardInfo.ProguardUsageZip)
382 }
383 }
384
385 dictZipBuilder.Build("proguard_dict_zip", "Building proguard dictionary zip")
386 dictMappingBuilder.Build("proguard_dict_mapping_proto", "Building proguard mapping proto")
387 usageZipBuilder.Build("proguard_usage_zip", "Building proguard usage zip")
Cole Faust54124c82025-02-21 14:46:02 -0800388
389 a.proguardDictZip = dictZip
390 a.proguardDictMapping = dictMapping
391 a.proguardUsageZip = usageZip
Cole Faust1dcf9e42025-02-19 17:23:34 -0800392}
393
Spandan Dasef775742025-01-13 22:17:40 +0000394// Helper structs for target_files.zip creation
Spandan Dasef200ac2025-01-08 01:42:45 +0000395type targetFilesZipCopy struct {
396 srcModule *string
397 destSubdir string
398}
399
Spandan Dasef775742025-01-13 22:17:40 +0000400type targetFilesystemZipCopy struct {
401 fsInfo FilesystemInfo
402 destSubdir string
403}
404
Spandan Dasdad98702025-02-26 14:32:28 +0000405func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext, allInstalledModules []android.Module) {
Cole Faust44080412024-12-20 14:17:07 -0800406 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
407 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
408
409 builder := android.NewRuleBuilder(pctx, ctx)
410 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
411 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000412 toCopy := []targetFilesZipCopy{
413 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
414 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
415 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
416 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
417 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
418 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
419 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
420 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
421 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
422 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
423 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000424 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000425
Spandan Dasef775742025-01-13 22:17:40 +0000426 filesystemsToCopy := []targetFilesystemZipCopy{}
Spandan Dasef200ac2025-01-08 01:42:45 +0000427 for _, zipCopy := range toCopy {
428 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000429 continue
430 }
Spandan Dasef775742025-01-13 22:17:40 +0000431 filesystemsToCopy = append(
432 filesystemsToCopy,
433 targetFilesystemZipCopy{a.getFilesystemInfo(ctx, *zipCopy.srcModule), zipCopy.destSubdir},
434 )
435 }
436 // Get additional filesystems from super_partition dependency
437 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800438 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000439 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust310de662025-02-19 16:15:07 -0800440 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Dasef775742025-01-13 22:17:40 +0000441 filesystemsToCopy = append(
442 filesystemsToCopy,
443 targetFilesystemZipCopy{info.SubImageInfo[partition], strings.ToUpper(partition)},
444 )
445 }
446 } else {
447 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
448 }
449 }
450
451 for _, toCopy := range filesystemsToCopy {
452 rootDirString := toCopy.fsInfo.RootDir.String()
453 if toCopy.destSubdir == "SYSTEM" {
Spandan Dasef200ac2025-01-08 01:42:45 +0000454 rootDirString = rootDirString + "/system"
455 }
Spandan Dasef775742025-01-13 22:17:40 +0000456 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), toCopy.destSubdir)
Cole Faust44080412024-12-20 14:17:07 -0800457 builder.Command().
458 BuiltTool("acp").
Spandan Dasef775742025-01-13 22:17:40 +0000459 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, toCopy.destSubdir).
460 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
Cole Faustb36763e2025-02-18 15:21:44 -0800461 for _, extraRootDir := range toCopy.fsInfo.ExtraRootDirs {
462 builder.Command().
463 BuiltTool("acp").
464 Textf("-rd %s/. %s/%s", extraRootDir, targetFilesDir, toCopy.destSubdir).
465 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
466 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000467
Spandan Dasef775742025-01-13 22:17:40 +0000468 if toCopy.destSubdir == "SYSTEM" {
Spandan Das3ec6d062025-01-09 19:37:47 +0000469 // Create the ROOT partition in target_files.zip
Spandan Dasef775742025-01-13 22:17:40 +0000470 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", toCopy.fsInfo.RootDir, targetFilesDir.String())
Spandan Das3ec6d062025-01-09 19:37:47 +0000471 }
Cole Faust44080412024-12-20 14:17:07 -0800472 }
Spandan Das9b17df22025-01-08 23:30:45 +0000473 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000474 if a.partitionProps.Vendor_boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800475 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000476 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000477 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
478 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
479 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000480 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/VENDOR_BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000481 }
Spandan Das23511372025-01-08 23:30:45 +0000482 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000483 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/VENDOR_BOOT/vendor_bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000484 }
Spandan Das258c08f2025-01-08 23:30:45 +0000485 }
486 if a.partitionProps.Boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800487 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000488 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000489 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
490 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000491 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000492 }
493 if bootImgInfo.Kernel != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000494 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000495 // 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 +0000496 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/VENDOR_BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000497 }
Spandan Das23511372025-01-08 23:30:45 +0000498 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000499 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/BOOT/bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000500 }
Spandan Das258c08f2025-01-08 23:30:45 +0000501 }
502
Spandan Dase51ff952025-01-09 18:11:59 +0000503 if a.deviceProps.Android_info != nil {
504 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
Cole Faust11fda332025-01-14 16:47:19 -0800505 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 +0000506 }
507
Spandan Das0036fa32025-01-10 23:40:45 +0000508 a.copyImagesToTargetZip(ctx, builder, targetFilesDir)
Spandan Dasdad98702025-02-26 14:32:28 +0000509 a.copyMetadataToTargetZip(ctx, builder, targetFilesDir, allInstalledModules)
Spandan Das0036fa32025-01-10 23:40:45 +0000510
Cole Faust44080412024-12-20 14:17:07 -0800511 builder.Command().
512 BuiltTool("soong_zip").
513 Text("-d").
514 FlagWithOutput("-o ", targetFilesZip).
515 FlagWithArg("-C ", targetFilesDir.String()).
516 FlagWithArg("-D ", targetFilesDir.String()).
517 Text("-sha256")
518 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
519}
520
Spandan Das0036fa32025-01-10 23:40:45 +0000521func (a *androidDevice) copyImagesToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
522 // Create an IMAGES/ subdirectory
523 builder.Command().Textf("mkdir -p %s/IMAGES", targetFilesDir.String())
524 if a.deviceProps.Bootloader != nil {
525 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader))).Textf(" %s/IMAGES/bootloader", targetFilesDir.String())
526 }
527 // Copy the filesystem ,boot and vbmeta img files to IMAGES/
528 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(child android.ModuleProxy) {
Spandan Dasa9db76d2025-01-14 01:34:43 +0000529 if strings.Contains(child.Name(), "recovery") {
530 return // skip recovery.img to match the make packaging behavior
531 }
Spandan Dasef775742025-01-13 22:17:40 +0000532 if info, ok := android.OtherModuleProvider(ctx, child, BootimgInfoProvider); ok {
533 // Check Boot img first so that the boot.img is copied and not its dep ramdisk.img
Spandan Das0036fa32025-01-10 23:40:45 +0000534 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
Spandan Dasef775742025-01-13 22:17:40 +0000535 } else if info, ok := android.OtherModuleProvider(ctx, child, FilesystemProvider); ok {
Spandan Das0036fa32025-01-10 23:40:45 +0000536 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
537 } else if info, ok := android.OtherModuleProvider(ctx, child, vbmetaPartitionProvider); ok {
538 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
539 } else {
540 ctx.ModuleErrorf("Module %s does not provide an .img file output for target_files.zip", child.Name())
541 }
542 })
Spandan Dasef775742025-01-13 22:17:40 +0000543
544 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800545 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000546 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust19eb09d2025-01-14 13:27:00 -0800547 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000548 if info.SubImageInfo[partition].OutputHermetic != nil {
549 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].OutputHermetic).Textf(" %s/IMAGES/", targetFilesDir.String())
550 }
551 if info.SubImageInfo[partition].MapFile != nil {
552 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].MapFile).Textf(" %s/IMAGES/", targetFilesDir.String())
553 }
Spandan Dasef775742025-01-13 22:17:40 +0000554 }
555 } else {
556 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
557 }
558 }
Spandan Das0036fa32025-01-10 23:40:45 +0000559}
560
Spandan Dasdad98702025-02-26 14:32:28 +0000561func (a *androidDevice) copyMetadataToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath, allInstalledModules []android.Module) {
Spandan Das29d44882025-01-15 21:12:36 +0000562 // Create a META/ subdirectory
563 builder.Command().Textf("mkdir -p %s/META", targetFilesDir.String())
564 if proptools.Bool(a.deviceProps.Ab_ota_updater) {
565 ctx.VisitDirectDepsProxyWithTag(targetFilesMetadataDepTag, func(child android.ModuleProxy) {
566 info, _ := android.OtherModuleProvider(ctx, child, android.OutputFilesProvider)
567 builder.Command().Textf("cp").Inputs(info.DefaultOutputFiles).Textf(" %s/META/", targetFilesDir.String())
568 })
Spandan Dasb5f40cf2025-02-12 19:36:03 +0000569 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "external/zucchini/version_info.h")).Textf(" %s/META/zucchini_config.txt", targetFilesDir.String())
570 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "system/update_engine/update_engine.conf")).Textf(" %s/META/update_engine_config.txt", targetFilesDir.String())
571 if a.getFsInfos(ctx)["system"].ErofsCompressHints != nil {
572 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].ErofsCompressHints).Textf(" %s/META/erofs_default_compress_hints.txt", targetFilesDir.String())
573 }
574 // ab_partitions.txt
575 abPartitionsSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_partitions)
576 abPartitionsSortedString := proptools.ShellEscape(strings.Join(abPartitionsSorted, "\\n"))
577 builder.Command().Textf("echo -e").Flag(abPartitionsSortedString).Textf(" > %s/META/ab_partitions.txt", targetFilesDir.String())
Spandan Das35b78742025-02-12 19:36:03 +0000578 // otakeys.txt
579 abOtaKeysSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_keys)
580 abOtaKeysSortedString := proptools.ShellEscape(strings.Join(abOtaKeysSorted, "\\n"))
581 builder.Command().Textf("echo -e").Flag(abOtaKeysSortedString).Textf(" > %s/META/otakeys.txt", targetFilesDir.String())
Spandan Das00948072025-02-12 19:36:03 +0000582 // postinstall_config.txt
583 abOtaPostInstallConfigString := proptools.ShellEscape(strings.Join(a.deviceProps.Ab_ota_postinstall_config, "\\n"))
584 builder.Command().Textf("echo -e").Flag(abOtaPostInstallConfigString).Textf(" > %s/META/postinstall_config.txt", targetFilesDir.String())
Spandan Dasf12ff9b2025-02-12 22:27:43 +0000585 // selinuxfc
586 if a.getFsInfos(ctx)["system"].SelinuxFc != nil {
587 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].SelinuxFc).Textf(" %s/META/file_contexts.bin", targetFilesDir.String())
588 }
Spandan Dasd71af182025-02-12 18:03:29 +0000589 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000590 // Copy $partition_filesystem_config.txt
591 fsInfos := a.getFsInfos(ctx)
592 for _, partition := range android.SortedKeys(fsInfos) {
593 if fsInfos[partition].FilesystemConfig == nil {
594 continue
595 }
596 if android.InList(partition, []string{"userdata"}) {
597 continue
598 }
Spandan Das853a7a42025-02-27 07:44:51 +0000599 if partition != "vendor_ramdisk" {
600 // vendor_ramdisk will be handled separately.
601 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/%s", targetFilesDir.String(), a.filesystemConfigNameForTargetFiles(partition))
602 }
Spandan Das54af3572025-02-27 07:42:08 +0000603 if partition == "ramdisk" {
604 // Create an additional copy at boot_filesystem_config.txt
605 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/boot_filesystem_config.txt", targetFilesDir.String())
606 }
Spandan Das853a7a42025-02-27 07:44:51 +0000607 if partition == "system" {
608 // Create root_filesystem_config from the assembled ROOT/ intermediates directory
609 a.generateFilesystemConfigForTargetFiles(ctx, builder, targetFilesDir.String(), targetFilesDir.String()+"/ROOT", "root_filesystem_config.txt")
610 }
611 if partition == "vendor_ramdisk" {
612 // Create vendor_boot_filesystem_config from the assembled VENDOR_BOOT/RAMDISK intermediates directory
613 a.generateFilesystemConfigForTargetFiles(ctx, builder, targetFilesDir.String(), targetFilesDir.String()+"/VENDOR_BOOT/RAMDISK", "vendor_boot_filesystem_config.txt")
614 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000615 }
Spandan Das75955b12025-02-13 22:12:52 +0000616 // Copy ramdisk_node_list
Spandan Das37240d92025-02-14 00:18:41 +0000617 if ramdiskNodeList := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Ramdisk_node_list)); ramdiskNodeList != nil {
618 builder.Command().Textf("cp").Input(ramdiskNodeList).Textf(" %s/META/", targetFilesDir.String())
619 }
620 // Copy releasetools.py
621 if releaseTools := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Releasetools_extension)); releaseTools != nil {
622 builder.Command().Textf("cp").Input(releaseTools).Textf(" %s/META/", targetFilesDir.String())
623 }
Spandan Dasdad98702025-02-26 14:32:28 +0000624 // apexkeys.txt
625 var installedApexKeys []android.Path
626 for _, installedModule := range allInstalledModules {
627 if info, ok := android.OtherModuleProvider(ctx, installedModule, ApexKeyPathInfoProvider); ok {
628 installedApexKeys = append(installedApexKeys, info.ApexKeyPath)
629 }
630 }
631 installedApexKeys = android.SortedUniquePaths(installedApexKeys) // Sort by keypath to match make
632 builder.Command().Text("cat").Inputs(installedApexKeys).Textf(" >> %s/META/apexkeys.txt", targetFilesDir.String())
Spandan Das3dfa17f2025-02-28 09:48:28 +0000633 // Copy fastboot-info.txt
634 if fastbootInfo := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.FastbootInfo)); fastbootInfo != nil {
635 // TODO (b/399788523): Autogenerate fastboot-info.txt if there is no source fastboot-info.txt
636 // https://cs.android.com/android/_/android/platform/build/+/80b9546f8f69e78b8fe1870e0e745d70fc18dfcd:core/Makefile;l=5831-5893;drc=077490384423dff9eac954da5c001c6f0be3fa6e;bpv=0;bpt=0
637 builder.Command().Textf("cp").Input(fastbootInfo).Textf(" %s/META/fastboot-info.txt", targetFilesDir.String())
638 }
Spandan Dasdad98702025-02-26 14:32:28 +0000639
Spandan Dasf079f0d2025-02-27 13:13:50 +0000640 if a.partitionProps.Super_partition_name != nil {
641 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
642 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
643 // dynamic_partitions_info.txt
644 // TODO (b/390192334): Add `building_super_empty_partition=true`
645 builder.Command().Text("cp").Input(info.DynamicPartitionsInfo).Textf(" %s/META/", targetFilesDir.String())
646 } else {
647 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
648 }
649 }
650
Spandan Dasdd262fb2025-02-13 00:15:59 +0000651}
652
Spandan Dasdad98702025-02-26 14:32:28 +0000653type ApexKeyPathInfo struct {
654 ApexKeyPath android.Path
655}
656
657var ApexKeyPathInfoProvider = blueprint.NewProvider[ApexKeyPathInfo]()
658
Spandan Das853a7a42025-02-27 07:44:51 +0000659func (a *androidDevice) generateFilesystemConfigForTargetFiles(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir, stagingDir, filename string) {
660 fsConfigOut := android.PathForModuleOut(ctx, filename)
661 ctx.Build(pctx, android.BuildParams{
662 Rule: fsConfigRule,
663 Output: fsConfigOut,
664 Args: map[string]string{
665 "rootDir": stagingDir,
666 "prefix": "",
667 },
668 })
669 builder.Command().Textf("cp").Input(fsConfigOut).Textf(" %s/META/", targetFilesDir)
670}
671
Spandan Dasdd262fb2025-02-13 00:15:59 +0000672// Filenames for the partition specific fs_config files.
673// Hardcode the ramdisk files to their boot image prefix
674func (a *androidDevice) filesystemConfigNameForTargetFiles(partition string) string {
675 name := partition + "_filesystem_config.txt"
676 if partition == "system" {
677 name = "filesystem_config.txt"
678 } else if partition == "ramdisk" {
679 name = "init_boot_filesystem_config.txt"
680 }
681 return name
Spandan Das29d44882025-01-15 21:12:36 +0000682}
683
Cole Faust44080412024-12-20 14:17:07 -0800684func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
Cole Faust19eb09d2025-01-14 13:27:00 -0800685 fsMod := ctx.GetDirectDepProxyWithTag(depName, filesystemDepTag)
Cole Faust44080412024-12-20 14:17:07 -0800686 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
687 if !ok {
688 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
689 }
690 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000691}
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000692
693func (a *androidDevice) setVbmetaPhonyTargets(ctx android.ModuleContext) {
694 if !proptools.Bool(a.deviceProps.Main_device) {
695 return
696 }
697
698 if !ctx.Config().KatiEnabled() {
699 for _, vbmetaPartitionName := range a.partitionProps.Vbmeta_partitions {
700 img := ctx.GetDirectDepProxyWithTag(vbmetaPartitionName, filesystemDepTag)
701 if provider, ok := android.OtherModuleProvider(ctx, img, vbmetaPartitionProvider); ok {
702 // make generates `vbmetasystemimage` phony target instead of `vbmeta_systemimage` phony target.
703 partitionName := strings.ReplaceAll(provider.Name, "_", "")
704 ctx.Phony(fmt.Sprintf("%simage", partitionName), provider.Output)
705 }
706 }
707 }
708}
Cole Faust21c11462025-03-03 14:13:17 -0800709
710func (a *androidDevice) getKernel(ctx android.ModuleContext) android.Path {
711 if a.partitionProps.Boot_partition_name != nil {
712 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
713 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
714 return bootImgInfo.Kernel
715 }
716 return nil
717}
718
719// Gets the kernel version and configs from the actual kernel file itself. Roughly equivalent to
720// this make code: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=5443;drc=c0b66fc59de069e06ce0ffd703d4d21613be30c6
721// However, it is a simplified version of that make code. Differences include:
722// - Not handling BOARD_KERNEL_CONFIG_FILE because BOARD_KERNEL_CONFIG_FILE was never used.
723// - Not unpacking the bootimage, as we should be able to just always export the kernel directly
724// in the BootimgInfo. We don't currently support prebuilt boot images, but even if we add that
725// in the future, it can be done in a prebuilt_bootimage module type that still exports the same
726// BootimgInfo.
727// - We don't print a warning and output '<unknown-kernel>' to kernel_version_for_uffd_gc.txt
728// because we expect the kernel to always be present. If it's not, we will get an error that
729// kernel_version_for_uffd_gc.txt doesn't exist. This may require later tweaking to the
730// dexpreopt rules so that they don't attempt to access that file in builds that don't have
731// a kernel.
732func (a *androidDevice) extractKernelVersionAndConfigs(ctx android.ModuleContext) (android.Path, android.Path) {
733 kernel := a.getKernel(ctx)
734 // If there's no kernel, don't create kernel version / kernel config files. Reverse dependencies
735 // on those files have to account for this, for example by disabling dexpreopt in unbundled
736 // builds.
737 if kernel == nil {
738 return nil, nil
739 }
740
741 lz4tool := ctx.Config().HostToolPath(ctx, "lz4")
742
743 extractedVersionFile := android.PathForModuleOut(ctx, "kernel_version.txt")
744 extractedConfigsFile := android.PathForModuleOut(ctx, "kernel_configs.txt")
745 builder := android.NewRuleBuilder(pctx, ctx)
746 builder.Command().BuiltTool("extract_kernel").
747 Flag("--tools lz4:"+lz4tool.String()).Implicit(lz4tool).
748 FlagWithInput("--input ", kernel).
749 FlagWithOutput("--output-release ", extractedVersionFile).
750 FlagWithOutput("--output-configs ", extractedConfigsFile)
751
752 if specifiedVersion := proptools.String(a.deviceProps.Kernel_version); specifiedVersion != "" {
753 specifiedVersionFile := android.PathForModuleOut(ctx, "specified_kernel_version.txt")
754 android.WriteFileRuleVerbatim(ctx, specifiedVersionFile, specifiedVersion)
755 builder.Command().Text("diff -q").
756 Input(specifiedVersionFile).
757 Input(extractedVersionFile).
758 Textf(`|| (echo "Specified kernel version '$(cat %s)' does not match actual kernel version '$(cat %s)'"; exit 1)`, specifiedVersionFile, extractedVersionFile)
759 }
760
761 builder.Build("extract_kernel_info", "Extract kernel version and configs")
762
763 if proptools.Bool(a.deviceProps.Main_device) && !ctx.Config().KatiEnabled() {
764 if ctx.Config().EnableUffdGc() == "default" {
765 kernelVersionFile := android.PathForOutput(ctx, "dexpreopt/kernel_version_for_uffd_gc.txt")
766 ctx.Build(pctx, android.BuildParams{
767 Rule: android.CpIfChanged,
768 Input: extractedVersionFile,
769 Output: kernelVersionFile,
770 })
771 }
772
773 ctx.DistForGoal("droid_targets", extractedVersionFile)
774 }
775
776 return extractedVersionFile, extractedConfigsFile
777}