blob: 78afbf587c420ca47385f6d05af971964c2de439 [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
Spandan Dasc09353c2025-03-04 00:11:21 +0000112 kernelConfig android.Path
113 kernelVersion android.Path
Spandan Das447a0ab2025-03-04 23:10:19 +0000114 miscInfo android.Path
Jihoon Kangf2c53982024-10-09 17:32:52 +0000115}
116
117func AndroidDeviceFactory() android.Module {
118 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +0000119 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -0800120 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000121 return module
122}
123
Cole Faust11fda332025-01-14 16:47:19 -0800124var numMainAndroidDevicesOnceKey android.OnceKey = android.NewOnceKey("num_auto_generated_anroid_devices")
Cole Faustb55a41c2025-01-09 16:53:58 -0800125
Jihoon Kangf2c53982024-10-09 17:32:52 +0000126type partitionDepTagType struct {
127 blueprint.BaseDependencyTag
128}
129
Cole Faust2bdc5e52025-01-10 10:29:36 -0800130type superPartitionDepTagType struct {
131 blueprint.BaseDependencyTag
132}
Spandan Das29d44882025-01-15 21:12:36 +0000133type targetFilesMetadataDepTagType struct {
134 blueprint.BaseDependencyTag
135}
Cole Faust2bdc5e52025-01-10 10:29:36 -0800136
137var superPartitionDepTag superPartitionDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000138var filesystemDepTag partitionDepTagType
Spandan Das29d44882025-01-15 21:12:36 +0000139var targetFilesMetadataDepTag targetFilesMetadataDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000140
141func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
142 addDependencyIfDefined := func(dep *string) {
143 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -0800144 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +0000145 }
146 }
147
Cole Faust2bdc5e52025-01-10 10:29:36 -0800148 if a.partitionProps.Super_partition_name != nil {
149 ctx.AddDependency(ctx.Module(), superPartitionDepTag, *a.partitionProps.Super_partition_name)
150 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000151 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +0000152 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000153 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000154 addDependencyIfDefined(a.partitionProps.System_partition_name)
155 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
156 addDependencyIfDefined(a.partitionProps.Product_partition_name)
157 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000158 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800159 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000160 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
161 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
162 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000163 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800164 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
165 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
166 }
Spandan Das29d44882025-01-15 21:12:36 +0000167 a.addDepsForTargetFilesMetadata(ctx)
168}
169
170func (a *androidDevice) addDepsForTargetFilesMetadata(ctx android.BottomUpMutatorContext) {
171 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), targetFilesMetadataDepTag, "liblz4") // host variant
Jihoon Kangf2c53982024-10-09 17:32:52 +0000172}
173
Cole Faust11fda332025-01-14 16:47:19 -0800174func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
175 if proptools.Bool(a.deviceProps.Main_device) {
176 numMainAndroidDevices := ctx.Config().Once(numMainAndroidDevicesOnceKey, func() interface{} {
177 return &atomic.Int32{}
178 }).(*atomic.Int32)
179 total := numMainAndroidDevices.Add(1)
180 if total > 1 {
181 // There should only be 1 main android_device module. That one will be
182 // made the default thing to build in soong-only builds.
183 ctx.ModuleErrorf("There cannot be more than 1 main android_device module")
184 }
Jihoon Kang3be17162025-01-09 20:51:54 +0000185 }
186
Cole Faust1dcf9e42025-02-19 17:23:34 -0800187 allInstalledModules := a.allInstalledModules(ctx)
188
Spandan Dasc09353c2025-03-04 00:11:21 +0000189 a.kernelConfig, a.kernelVersion = a.extractKernelVersionAndConfigs(ctx)
Spandan Das447a0ab2025-03-04 23:10:19 +0000190 a.miscInfo = a.addMiscInfo(ctx)
Spandan Dasdad98702025-02-26 14:32:28 +0000191 a.buildTargetFilesZip(ctx, allInstalledModules)
Cole Faust1dcf9e42025-02-19 17:23:34 -0800192 a.buildProguardZips(ctx, allInstalledModules)
193
mrziwang2fd33a72025-01-08 12:22:08 -0800194 var deps []android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800195 if proptools.String(a.partitionProps.Super_partition_name) != "" {
Cole Faust19eb09d2025-01-14 13:27:00 -0800196 superImage := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Cole Faust2bdc5e52025-01-10 10:29:36 -0800197 if info, ok := android.OtherModuleProvider(ctx, superImage, SuperImageProvider); ok {
198 assertUnset := func(prop *string, propName string) {
199 if prop != nil && *prop != "" {
200 ctx.PropertyErrorf(propName, "Cannot be set because it's already part of the super image")
201 }
202 }
203 for _, subPartitionType := range android.SortedKeys(info.SubImageInfo) {
204 switch subPartitionType {
205 case "system":
206 assertUnset(a.partitionProps.System_partition_name, "system_partition_name")
207 case "system_ext":
208 assertUnset(a.partitionProps.System_ext_partition_name, "system_ext_partition_name")
209 case "system_dlkm":
210 assertUnset(a.partitionProps.System_dlkm_partition_name, "system_dlkm_partition_name")
211 case "system_other":
212 // TODO
213 case "product":
214 assertUnset(a.partitionProps.Product_partition_name, "product_partition_name")
215 case "vendor":
216 assertUnset(a.partitionProps.Vendor_partition_name, "vendor_partition_name")
217 case "vendor_dlkm":
218 assertUnset(a.partitionProps.Vendor_dlkm_partition_name, "vendor_dlkm_partition_name")
219 case "odm":
220 assertUnset(a.partitionProps.Odm_partition_name, "odm_partition_name")
221 case "odm_dlkm":
222 assertUnset(a.partitionProps.Odm_dlkm_partition_name, "odm_dlkm_partition_name")
223 default:
224 ctx.ModuleErrorf("Unsupported sub-partition of super partition: %q", subPartitionType)
225 }
226 }
227
228 deps = append(deps, info.SuperImage)
229 } else {
230 ctx.ModuleErrorf("Expected super image dep to provide SuperImageProvider")
231 }
232 }
Cole Faust19eb09d2025-01-14 13:27:00 -0800233 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(m android.ModuleProxy) {
mrziwang2fd33a72025-01-08 12:22:08 -0800234 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
235 if !ok {
236 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
237 }
238 if len(imageOutput.DefaultOutputFiles) != 1 {
239 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
240 }
241 deps = append(deps, imageOutput.DefaultOutputFiles[0])
242 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000243
Cole Fausta472a6f2025-02-10 16:10:04 -0800244 allImagesZip := android.PathForModuleOut(ctx, "all_images.zip")
245 allImagesZipBuilder := android.NewRuleBuilder(pctx, ctx)
Cole Faust8967d752025-02-19 17:27:29 -0800246 cmd := allImagesZipBuilder.Command().BuiltTool("soong_zip")
Cole Fausta472a6f2025-02-10 16:10:04 -0800247 for _, dep := range deps {
248 cmd.FlagWithArg("-e ", dep.Base())
249 cmd.FlagWithInput("-f ", dep)
250 }
251 cmd.FlagWithOutput("-o ", allImagesZip)
252 allImagesZipBuilder.Build("soong_all_images_zip", "all_images.zip")
253 a.allImagesZip = allImagesZip
254
Cole Faustb55a41c2025-01-09 16:53:58 -0800255 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
Cole Faust11fda332025-01-14 16:47:19 -0800256 var validations android.Paths
257 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Cole Faustb55a41c2025-01-09 16:53:58 -0800258 // In soong-only builds, build this module by default.
259 // This is the analogue to this make code:
260 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
261 ctx.Phony("droidcore-unbundled", allImagesStamp)
Cole Faust11fda332025-01-14 16:47:19 -0800262
Cole Faust19fbb072025-01-30 18:19:29 -0800263 deps = append(deps, a.copyFilesToProductOutForSoongOnly(ctx))
Cole Faustb55a41c2025-01-09 16:53:58 -0800264 }
Cole Faust11fda332025-01-14 16:47:19 -0800265
266 ctx.Build(pctx, android.BuildParams{
267 Rule: android.Touch,
268 Output: allImagesStamp,
269 Implicits: deps,
270 Validations: validations,
271 })
272
273 // Checkbuilding it causes soong to make a phony, so you can say `m <module name>`
274 ctx.CheckbuildFile(allImagesStamp)
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000275
276 a.setVbmetaPhonyTargets(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000277
278 a.distFiles(ctx)
279}
280
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000281// Returns a list of modules that are installed, which are collected from the dependency
282// filesystem and super_image modules.
283func (a *androidDevice) allInstalledModules(ctx android.ModuleContext) []android.Module {
284 fsInfoMap := a.getFsInfos(ctx)
285 allOwners := make(map[string][]string)
286 for _, partition := range android.SortedKeys(fsInfoMap) {
287 fsInfo := fsInfoMap[partition]
288 for _, owner := range fsInfo.Owners {
289 allOwners[owner.Name] = append(allOwners[owner.Name], owner.Variation)
290 }
291 }
292
293 ret := []android.Module{}
294 ctx.WalkDepsProxy(func(mod, _ android.ModuleProxy) bool {
Spandan Das5c78fe92025-02-26 14:25:56 +0000295 if variations, ok := allOwners[ctx.OtherModuleName(mod)]; ok && android.InList(ctx.OtherModuleSubDir(mod), variations) {
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000296 ret = append(ret, mod)
297 }
298 return true
299 })
300
301 // Remove duplicates
302 ret = android.FirstUniqueFunc(ret, func(a, b android.Module) bool {
303 return a.String() == b.String()
304 })
305
306 // Sort the modules by their names and variants
307 slices.SortFunc(ret, func(a, b android.Module) int {
308 return cmp.Compare(a.String(), b.String())
309 })
310 return ret
311}
312
Cole Faust54124c82025-02-21 14:46:02 -0800313func insertBeforeExtension(file, insertion string) string {
314 ext := filepath.Ext(file)
315 return strings.TrimSuffix(file, ext) + insertion + ext
316}
317
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000318func (a *androidDevice) distFiles(ctx android.ModuleContext) {
Cole Faust54124c82025-02-21 14:46:02 -0800319 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
320 fsInfoMap := a.getFsInfos(ctx)
321 for _, partition := range android.SortedKeys(fsInfoMap) {
322 fsInfo := fsInfoMap[partition]
323 if fsInfo.InstalledFiles.Json != nil {
324 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Json)
325 }
326 if fsInfo.InstalledFiles.Txt != nil {
327 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Txt)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000328 }
329 }
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000330
Cole Faust54124c82025-02-21 14:46:02 -0800331 namePrefix := ""
332 if ctx.Config().HasDeviceProduct() {
333 namePrefix = ctx.Config().DeviceProduct() + "-"
334 }
335 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardDictZip, namePrefix+insertBeforeExtension(a.proguardDictZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
336 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardDictMapping, namePrefix+insertBeforeExtension(a.proguardDictMapping.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
337 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardUsageZip, namePrefix+insertBeforeExtension(a.proguardUsageZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
338 }
Cole Faust44080412024-12-20 14:17:07 -0800339}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000340
Yu Liu2a815b62025-02-21 20:46:25 +0000341func (a *androidDevice) MakeVars(_ android.MakeVarsModuleContext) []android.ModuleMakeVarsValue {
Cole Fausta472a6f2025-02-10 16:10:04 -0800342 if proptools.Bool(a.deviceProps.Main_device) {
Yu Liu2a815b62025-02-21 20:46:25 +0000343 return []android.ModuleMakeVarsValue{{"SOONG_ONLY_ALL_IMAGES_ZIP", a.allImagesZip.String()}}
Cole Fausta472a6f2025-02-10 16:10:04 -0800344 }
Yu Liu2a815b62025-02-21 20:46:25 +0000345 return nil
Cole Fausta472a6f2025-02-10 16:10:04 -0800346}
347
Cole Faust1dcf9e42025-02-19 17:23:34 -0800348func (a *androidDevice) buildProguardZips(ctx android.ModuleContext, allInstalledModules []android.Module) {
349 dictZip := android.PathForModuleOut(ctx, "proguard-dict.zip")
350 dictZipBuilder := android.NewRuleBuilder(pctx, ctx)
351 dictZipCmd := dictZipBuilder.Command().BuiltTool("soong_zip").Flag("-d").FlagWithOutput("-o ", dictZip)
352
353 dictMapping := android.PathForModuleOut(ctx, "proguard-dict-mapping.textproto")
354 dictMappingBuilder := android.NewRuleBuilder(pctx, ctx)
355 dictMappingCmd := dictMappingBuilder.Command().BuiltTool("symbols_map").Flag("-merge").Output(dictMapping)
356
357 protosDir := android.PathForModuleOut(ctx, "proguard_mapping_protos")
358
359 usageZip := android.PathForModuleOut(ctx, "proguard-usage.zip")
360 usageZipBuilder := android.NewRuleBuilder(pctx, ctx)
361 usageZipCmd := usageZipBuilder.Command().BuiltTool("merge_zips").Output(usageZip)
362
363 for _, mod := range allInstalledModules {
364 if proguardInfo, ok := android.OtherModuleProvider(ctx, mod, java.ProguardProvider); ok {
365 // Maintain these out/target/common paths for backwards compatibility. They may be able
366 // to be changed if tools look up file locations from the protobuf, but I'm not
367 // exactly sure how that works.
368 dictionaryFakePath := fmt.Sprintf("out/target/common/obj/%s/%s_intermediates/proguard_dictionary", proguardInfo.Class, proguardInfo.ModuleName)
369 dictZipCmd.FlagWithArg("-e ", dictionaryFakePath)
370 dictZipCmd.FlagWithInput("-f ", proguardInfo.ProguardDictionary)
371 dictZipCmd.Textf("-e out/target/common/obj/%s/%s_intermediates/classes.jar", proguardInfo.Class, proguardInfo.ModuleName)
372 dictZipCmd.FlagWithInput("-f ", proguardInfo.ClassesJar)
373
374 protoFile := protosDir.Join(ctx, filepath.Dir(dictionaryFakePath), "proguard_dictionary.textproto")
375 ctx.Build(pctx, android.BuildParams{
376 Rule: proguardDictToProto,
377 Input: proguardInfo.ProguardDictionary,
378 Output: protoFile,
379 Args: map[string]string{
380 "location": dictionaryFakePath,
381 },
382 })
383 dictMappingCmd.Input(protoFile)
384
385 usageZipCmd.Input(proguardInfo.ProguardUsageZip)
386 }
387 }
388
389 dictZipBuilder.Build("proguard_dict_zip", "Building proguard dictionary zip")
390 dictMappingBuilder.Build("proguard_dict_mapping_proto", "Building proguard mapping proto")
391 usageZipBuilder.Build("proguard_usage_zip", "Building proguard usage zip")
Cole Faust54124c82025-02-21 14:46:02 -0800392
393 a.proguardDictZip = dictZip
394 a.proguardDictMapping = dictMapping
395 a.proguardUsageZip = usageZip
Cole Faust1dcf9e42025-02-19 17:23:34 -0800396}
397
Spandan Dasef775742025-01-13 22:17:40 +0000398// Helper structs for target_files.zip creation
Spandan Dasef200ac2025-01-08 01:42:45 +0000399type targetFilesZipCopy struct {
400 srcModule *string
401 destSubdir string
402}
403
Spandan Dasef775742025-01-13 22:17:40 +0000404type targetFilesystemZipCopy struct {
405 fsInfo FilesystemInfo
406 destSubdir string
407}
408
Spandan Dasdad98702025-02-26 14:32:28 +0000409func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext, allInstalledModules []android.Module) {
Cole Faust44080412024-12-20 14:17:07 -0800410 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
411 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
412
413 builder := android.NewRuleBuilder(pctx, ctx)
414 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
415 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000416 toCopy := []targetFilesZipCopy{
417 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
418 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
419 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
420 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
421 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
422 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
423 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
424 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
425 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
426 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
427 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000428 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000429
Spandan Dasef775742025-01-13 22:17:40 +0000430 filesystemsToCopy := []targetFilesystemZipCopy{}
Spandan Dasef200ac2025-01-08 01:42:45 +0000431 for _, zipCopy := range toCopy {
432 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000433 continue
434 }
Spandan Dasef775742025-01-13 22:17:40 +0000435 filesystemsToCopy = append(
436 filesystemsToCopy,
437 targetFilesystemZipCopy{a.getFilesystemInfo(ctx, *zipCopy.srcModule), zipCopy.destSubdir},
438 )
439 }
440 // Get additional filesystems from super_partition dependency
441 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800442 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000443 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust310de662025-02-19 16:15:07 -0800444 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Dasef775742025-01-13 22:17:40 +0000445 filesystemsToCopy = append(
446 filesystemsToCopy,
447 targetFilesystemZipCopy{info.SubImageInfo[partition], strings.ToUpper(partition)},
448 )
449 }
450 } else {
451 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
452 }
453 }
454
455 for _, toCopy := range filesystemsToCopy {
456 rootDirString := toCopy.fsInfo.RootDir.String()
457 if toCopy.destSubdir == "SYSTEM" {
Spandan Dasef200ac2025-01-08 01:42:45 +0000458 rootDirString = rootDirString + "/system"
459 }
Spandan Dasef775742025-01-13 22:17:40 +0000460 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), toCopy.destSubdir)
Cole Faust44080412024-12-20 14:17:07 -0800461 builder.Command().
462 BuiltTool("acp").
Spandan Dasef775742025-01-13 22:17:40 +0000463 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, toCopy.destSubdir).
464 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
Cole Faustb36763e2025-02-18 15:21:44 -0800465 for _, extraRootDir := range toCopy.fsInfo.ExtraRootDirs {
466 builder.Command().
467 BuiltTool("acp").
468 Textf("-rd %s/. %s/%s", extraRootDir, targetFilesDir, toCopy.destSubdir).
469 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
470 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000471
Spandan Dasef775742025-01-13 22:17:40 +0000472 if toCopy.destSubdir == "SYSTEM" {
Spandan Das3ec6d062025-01-09 19:37:47 +0000473 // Create the ROOT partition in target_files.zip
Spandan Dasef775742025-01-13 22:17:40 +0000474 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", toCopy.fsInfo.RootDir, targetFilesDir.String())
Spandan Das3ec6d062025-01-09 19:37:47 +0000475 }
Cole Faust44080412024-12-20 14:17:07 -0800476 }
Spandan Das9b17df22025-01-08 23:30:45 +0000477 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000478 if a.partitionProps.Vendor_boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800479 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000480 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000481 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
482 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
483 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000484 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/VENDOR_BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000485 }
Spandan Das23511372025-01-08 23:30:45 +0000486 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000487 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/VENDOR_BOOT/vendor_bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000488 }
Spandan Das258c08f2025-01-08 23:30:45 +0000489 }
490 if a.partitionProps.Boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800491 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000492 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000493 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
494 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000495 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000496 }
497 if bootImgInfo.Kernel != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000498 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000499 // 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 +0000500 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/VENDOR_BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000501 }
Spandan Das23511372025-01-08 23:30:45 +0000502 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000503 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/BOOT/bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000504 }
Spandan Das258c08f2025-01-08 23:30:45 +0000505 }
506
Spandan Dase51ff952025-01-09 18:11:59 +0000507 if a.deviceProps.Android_info != nil {
508 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
Cole Faust11fda332025-01-14 16:47:19 -0800509 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 +0000510 }
511
Spandan Das0036fa32025-01-10 23:40:45 +0000512 a.copyImagesToTargetZip(ctx, builder, targetFilesDir)
Spandan Dasdad98702025-02-26 14:32:28 +0000513 a.copyMetadataToTargetZip(ctx, builder, targetFilesDir, allInstalledModules)
Spandan Das0036fa32025-01-10 23:40:45 +0000514
Cole Faust44080412024-12-20 14:17:07 -0800515 builder.Command().
516 BuiltTool("soong_zip").
517 Text("-d").
518 FlagWithOutput("-o ", targetFilesZip).
519 FlagWithArg("-C ", targetFilesDir.String()).
520 FlagWithArg("-D ", targetFilesDir.String()).
521 Text("-sha256")
522 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
523}
524
Spandan Das0036fa32025-01-10 23:40:45 +0000525func (a *androidDevice) copyImagesToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
526 // Create an IMAGES/ subdirectory
527 builder.Command().Textf("mkdir -p %s/IMAGES", targetFilesDir.String())
528 if a.deviceProps.Bootloader != nil {
529 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader))).Textf(" %s/IMAGES/bootloader", targetFilesDir.String())
530 }
531 // Copy the filesystem ,boot and vbmeta img files to IMAGES/
532 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(child android.ModuleProxy) {
Spandan Dasa9db76d2025-01-14 01:34:43 +0000533 if strings.Contains(child.Name(), "recovery") {
534 return // skip recovery.img to match the make packaging behavior
535 }
Spandan Dasef775742025-01-13 22:17:40 +0000536 if info, ok := android.OtherModuleProvider(ctx, child, BootimgInfoProvider); ok {
537 // Check Boot img first so that the boot.img is copied and not its dep ramdisk.img
Spandan Das0036fa32025-01-10 23:40:45 +0000538 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
Spandan Dasef775742025-01-13 22:17:40 +0000539 } else if info, ok := android.OtherModuleProvider(ctx, child, FilesystemProvider); ok {
Spandan Das0036fa32025-01-10 23:40:45 +0000540 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
541 } else if info, ok := android.OtherModuleProvider(ctx, child, vbmetaPartitionProvider); ok {
542 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
543 } else {
544 ctx.ModuleErrorf("Module %s does not provide an .img file output for target_files.zip", child.Name())
545 }
546 })
Spandan Dasef775742025-01-13 22:17:40 +0000547
548 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800549 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000550 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust19eb09d2025-01-14 13:27:00 -0800551 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000552 if info.SubImageInfo[partition].OutputHermetic != nil {
553 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].OutputHermetic).Textf(" %s/IMAGES/", targetFilesDir.String())
554 }
555 if info.SubImageInfo[partition].MapFile != nil {
556 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].MapFile).Textf(" %s/IMAGES/", targetFilesDir.String())
557 }
Spandan Dasef775742025-01-13 22:17:40 +0000558 }
559 } else {
560 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
561 }
562 }
Spandan Das0036fa32025-01-10 23:40:45 +0000563}
564
Spandan Dasdad98702025-02-26 14:32:28 +0000565func (a *androidDevice) copyMetadataToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath, allInstalledModules []android.Module) {
Spandan Das29d44882025-01-15 21:12:36 +0000566 // Create a META/ subdirectory
567 builder.Command().Textf("mkdir -p %s/META", targetFilesDir.String())
568 if proptools.Bool(a.deviceProps.Ab_ota_updater) {
569 ctx.VisitDirectDepsProxyWithTag(targetFilesMetadataDepTag, func(child android.ModuleProxy) {
570 info, _ := android.OtherModuleProvider(ctx, child, android.OutputFilesProvider)
571 builder.Command().Textf("cp").Inputs(info.DefaultOutputFiles).Textf(" %s/META/", targetFilesDir.String())
572 })
Spandan Dasb5f40cf2025-02-12 19:36:03 +0000573 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "external/zucchini/version_info.h")).Textf(" %s/META/zucchini_config.txt", targetFilesDir.String())
574 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "system/update_engine/update_engine.conf")).Textf(" %s/META/update_engine_config.txt", targetFilesDir.String())
575 if a.getFsInfos(ctx)["system"].ErofsCompressHints != nil {
576 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].ErofsCompressHints).Textf(" %s/META/erofs_default_compress_hints.txt", targetFilesDir.String())
577 }
578 // ab_partitions.txt
579 abPartitionsSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_partitions)
580 abPartitionsSortedString := proptools.ShellEscape(strings.Join(abPartitionsSorted, "\\n"))
581 builder.Command().Textf("echo -e").Flag(abPartitionsSortedString).Textf(" > %s/META/ab_partitions.txt", targetFilesDir.String())
Spandan Das35b78742025-02-12 19:36:03 +0000582 // otakeys.txt
583 abOtaKeysSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_keys)
584 abOtaKeysSortedString := proptools.ShellEscape(strings.Join(abOtaKeysSorted, "\\n"))
585 builder.Command().Textf("echo -e").Flag(abOtaKeysSortedString).Textf(" > %s/META/otakeys.txt", targetFilesDir.String())
Spandan Das00948072025-02-12 19:36:03 +0000586 // postinstall_config.txt
587 abOtaPostInstallConfigString := proptools.ShellEscape(strings.Join(a.deviceProps.Ab_ota_postinstall_config, "\\n"))
588 builder.Command().Textf("echo -e").Flag(abOtaPostInstallConfigString).Textf(" > %s/META/postinstall_config.txt", targetFilesDir.String())
Spandan Dasf12ff9b2025-02-12 22:27:43 +0000589 // selinuxfc
590 if a.getFsInfos(ctx)["system"].SelinuxFc != nil {
591 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].SelinuxFc).Textf(" %s/META/file_contexts.bin", targetFilesDir.String())
592 }
Spandan Dasd71af182025-02-12 18:03:29 +0000593 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000594 // Copy $partition_filesystem_config.txt
595 fsInfos := a.getFsInfos(ctx)
596 for _, partition := range android.SortedKeys(fsInfos) {
597 if fsInfos[partition].FilesystemConfig == nil {
598 continue
599 }
600 if android.InList(partition, []string{"userdata"}) {
601 continue
602 }
Spandan Das853a7a42025-02-27 07:44:51 +0000603 if partition != "vendor_ramdisk" {
604 // vendor_ramdisk will be handled separately.
605 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/%s", targetFilesDir.String(), a.filesystemConfigNameForTargetFiles(partition))
606 }
Spandan Das54af3572025-02-27 07:42:08 +0000607 if partition == "ramdisk" {
608 // Create an additional copy at boot_filesystem_config.txt
609 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/boot_filesystem_config.txt", targetFilesDir.String())
610 }
Spandan Das853a7a42025-02-27 07:44:51 +0000611 if partition == "system" {
612 // Create root_filesystem_config from the assembled ROOT/ intermediates directory
613 a.generateFilesystemConfigForTargetFiles(ctx, builder, targetFilesDir.String(), targetFilesDir.String()+"/ROOT", "root_filesystem_config.txt")
614 }
615 if partition == "vendor_ramdisk" {
616 // Create vendor_boot_filesystem_config from the assembled VENDOR_BOOT/RAMDISK intermediates directory
617 a.generateFilesystemConfigForTargetFiles(ctx, builder, targetFilesDir.String(), targetFilesDir.String()+"/VENDOR_BOOT/RAMDISK", "vendor_boot_filesystem_config.txt")
618 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000619 }
Spandan Das75955b12025-02-13 22:12:52 +0000620 // Copy ramdisk_node_list
Spandan Das37240d92025-02-14 00:18:41 +0000621 if ramdiskNodeList := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Ramdisk_node_list)); ramdiskNodeList != nil {
622 builder.Command().Textf("cp").Input(ramdiskNodeList).Textf(" %s/META/", targetFilesDir.String())
623 }
624 // Copy releasetools.py
625 if releaseTools := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Releasetools_extension)); releaseTools != nil {
626 builder.Command().Textf("cp").Input(releaseTools).Textf(" %s/META/", targetFilesDir.String())
627 }
Spandan Dasdad98702025-02-26 14:32:28 +0000628 // apexkeys.txt
629 var installedApexKeys []android.Path
630 for _, installedModule := range allInstalledModules {
631 if info, ok := android.OtherModuleProvider(ctx, installedModule, ApexKeyPathInfoProvider); ok {
632 installedApexKeys = append(installedApexKeys, info.ApexKeyPath)
633 }
634 }
635 installedApexKeys = android.SortedUniquePaths(installedApexKeys) // Sort by keypath to match make
636 builder.Command().Text("cat").Inputs(installedApexKeys).Textf(" >> %s/META/apexkeys.txt", targetFilesDir.String())
Spandan Das3dfa17f2025-02-28 09:48:28 +0000637 // Copy fastboot-info.txt
638 if fastbootInfo := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.FastbootInfo)); fastbootInfo != nil {
639 // TODO (b/399788523): Autogenerate fastboot-info.txt if there is no source fastboot-info.txt
640 // https://cs.android.com/android/_/android/platform/build/+/80b9546f8f69e78b8fe1870e0e745d70fc18dfcd:core/Makefile;l=5831-5893;drc=077490384423dff9eac954da5c001c6f0be3fa6e;bpv=0;bpt=0
641 builder.Command().Textf("cp").Input(fastbootInfo).Textf(" %s/META/fastboot-info.txt", targetFilesDir.String())
642 }
Spandan Dasdad98702025-02-26 14:32:28 +0000643
Spandan Dasc09353c2025-03-04 00:11:21 +0000644 // kernel_configs.txt and kernel_version.txt
645 if a.kernelConfig != nil {
646 builder.Command().Textf("cp").Input(a.kernelConfig).Textf(" %s/META/", targetFilesDir.String())
647 }
648 if a.kernelVersion != nil {
649 builder.Command().Textf("cp").Input(a.kernelVersion).Textf(" %s/META/", targetFilesDir.String())
650 }
Spandan Das447a0ab2025-03-04 23:10:19 +0000651 // misc_info.txt
652 if a.miscInfo != nil {
653 builder.Command().Textf("cp").Input(a.miscInfo).Textf(" %s/META/", targetFilesDir.String())
654 }
Spandan Dasc09353c2025-03-04 00:11:21 +0000655
Spandan Dasf079f0d2025-02-27 13:13:50 +0000656 if a.partitionProps.Super_partition_name != nil {
657 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
658 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
659 // dynamic_partitions_info.txt
660 // TODO (b/390192334): Add `building_super_empty_partition=true`
661 builder.Command().Text("cp").Input(info.DynamicPartitionsInfo).Textf(" %s/META/", targetFilesDir.String())
662 } else {
663 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
664 }
665 }
666
Spandan Dasdd262fb2025-02-13 00:15:59 +0000667}
668
Spandan Das447a0ab2025-03-04 23:10:19 +0000669// A partial implementation of make's $PRODUCT_OUT/misc_info.txt
670// 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
671// This file is subsequently used by add_img_to_target_files to create additioanl metadata files like apex_info.pb
672// TODO (b/399788119): Complete the migration of misc_info.txt
673func (a *androidDevice) addMiscInfo(ctx android.ModuleContext) android.Path {
674 builder := android.NewRuleBuilder(pctx, ctx)
675 miscInfo := android.PathForModuleOut(ctx, "misc_info.txt")
676 builder.Command().
677 Textf("rm -f %s", miscInfo).
678 Textf("&& echo recovery_api_version=%s >> %s", ctx.Config().VendorConfig("recovery").String("recovery_api_version"), miscInfo).
679 Textf("&& echo fstab_version=%s >> %s", ctx.Config().VendorConfig("recovery").String("recovery_fstab_version"), miscInfo).
680 ImplicitOutput(miscInfo)
681
682 if a.partitionProps.Recovery_partition_name == nil {
683 builder.Command().Textf("echo no_recovery=true >> %s", miscInfo)
684 }
685 fsInfos := a.getFsInfos(ctx)
686 for _, partition := range android.SortedKeys(fsInfos) {
687 if fsInfos[partition].UseAvb {
688 builder.Command().Textf("echo 'avb_%s_hashtree_enable=true' >> %s", partition, miscInfo)
689 }
690 }
691 if len(a.partitionProps.Vbmeta_partitions) > 0 {
692 builder.Command().
693 Textf("echo avb_enable=true >> %s", miscInfo).
694 Textf("&& echo avb_building_vbmeta_image=true >> %s", miscInfo).
695 Textf("&& echo avb_avbtool=avbtool >> %s", miscInfo)
696 }
697 if a.partitionProps.Boot_partition_name != nil {
698 builder.Command().Textf("echo boot_images=boot.img >> %s", miscInfo)
699 }
700
701 builder.Build("misc_info", "Building misc_info")
702
703 return miscInfo
704}
705
Spandan Dasdad98702025-02-26 14:32:28 +0000706type ApexKeyPathInfo struct {
707 ApexKeyPath android.Path
708}
709
710var ApexKeyPathInfoProvider = blueprint.NewProvider[ApexKeyPathInfo]()
711
Spandan Das853a7a42025-02-27 07:44:51 +0000712func (a *androidDevice) generateFilesystemConfigForTargetFiles(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir, stagingDir, filename string) {
713 fsConfigOut := android.PathForModuleOut(ctx, filename)
714 ctx.Build(pctx, android.BuildParams{
715 Rule: fsConfigRule,
716 Output: fsConfigOut,
717 Args: map[string]string{
718 "rootDir": stagingDir,
719 "prefix": "",
720 },
721 })
722 builder.Command().Textf("cp").Input(fsConfigOut).Textf(" %s/META/", targetFilesDir)
723}
724
Spandan Dasdd262fb2025-02-13 00:15:59 +0000725// Filenames for the partition specific fs_config files.
726// Hardcode the ramdisk files to their boot image prefix
727func (a *androidDevice) filesystemConfigNameForTargetFiles(partition string) string {
728 name := partition + "_filesystem_config.txt"
729 if partition == "system" {
730 name = "filesystem_config.txt"
731 } else if partition == "ramdisk" {
732 name = "init_boot_filesystem_config.txt"
733 }
734 return name
Spandan Das29d44882025-01-15 21:12:36 +0000735}
736
Cole Faust44080412024-12-20 14:17:07 -0800737func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
Cole Faust19eb09d2025-01-14 13:27:00 -0800738 fsMod := ctx.GetDirectDepProxyWithTag(depName, filesystemDepTag)
Cole Faust44080412024-12-20 14:17:07 -0800739 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
740 if !ok {
741 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
742 }
743 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000744}
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000745
746func (a *androidDevice) setVbmetaPhonyTargets(ctx android.ModuleContext) {
747 if !proptools.Bool(a.deviceProps.Main_device) {
748 return
749 }
750
751 if !ctx.Config().KatiEnabled() {
752 for _, vbmetaPartitionName := range a.partitionProps.Vbmeta_partitions {
753 img := ctx.GetDirectDepProxyWithTag(vbmetaPartitionName, filesystemDepTag)
754 if provider, ok := android.OtherModuleProvider(ctx, img, vbmetaPartitionProvider); ok {
755 // make generates `vbmetasystemimage` phony target instead of `vbmeta_systemimage` phony target.
756 partitionName := strings.ReplaceAll(provider.Name, "_", "")
757 ctx.Phony(fmt.Sprintf("%simage", partitionName), provider.Output)
758 }
759 }
760 }
761}
Cole Faust21c11462025-03-03 14:13:17 -0800762
763func (a *androidDevice) getKernel(ctx android.ModuleContext) android.Path {
764 if a.partitionProps.Boot_partition_name != nil {
765 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
766 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
767 return bootImgInfo.Kernel
768 }
769 return nil
770}
771
772// Gets the kernel version and configs from the actual kernel file itself. Roughly equivalent to
773// this make code: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=5443;drc=c0b66fc59de069e06ce0ffd703d4d21613be30c6
774// However, it is a simplified version of that make code. Differences include:
775// - Not handling BOARD_KERNEL_CONFIG_FILE because BOARD_KERNEL_CONFIG_FILE was never used.
776// - Not unpacking the bootimage, as we should be able to just always export the kernel directly
777// in the BootimgInfo. We don't currently support prebuilt boot images, but even if we add that
778// in the future, it can be done in a prebuilt_bootimage module type that still exports the same
779// BootimgInfo.
780// - We don't print a warning and output '<unknown-kernel>' to kernel_version_for_uffd_gc.txt
781// because we expect the kernel to always be present. If it's not, we will get an error that
782// kernel_version_for_uffd_gc.txt doesn't exist. This may require later tweaking to the
783// dexpreopt rules so that they don't attempt to access that file in builds that don't have
784// a kernel.
785func (a *androidDevice) extractKernelVersionAndConfigs(ctx android.ModuleContext) (android.Path, android.Path) {
786 kernel := a.getKernel(ctx)
787 // If there's no kernel, don't create kernel version / kernel config files. Reverse dependencies
788 // on those files have to account for this, for example by disabling dexpreopt in unbundled
789 // builds.
790 if kernel == nil {
791 return nil, nil
792 }
793
794 lz4tool := ctx.Config().HostToolPath(ctx, "lz4")
795
796 extractedVersionFile := android.PathForModuleOut(ctx, "kernel_version.txt")
797 extractedConfigsFile := android.PathForModuleOut(ctx, "kernel_configs.txt")
798 builder := android.NewRuleBuilder(pctx, ctx)
799 builder.Command().BuiltTool("extract_kernel").
800 Flag("--tools lz4:"+lz4tool.String()).Implicit(lz4tool).
801 FlagWithInput("--input ", kernel).
802 FlagWithOutput("--output-release ", extractedVersionFile).
803 FlagWithOutput("--output-configs ", extractedConfigsFile)
804
805 if specifiedVersion := proptools.String(a.deviceProps.Kernel_version); specifiedVersion != "" {
806 specifiedVersionFile := android.PathForModuleOut(ctx, "specified_kernel_version.txt")
807 android.WriteFileRuleVerbatim(ctx, specifiedVersionFile, specifiedVersion)
808 builder.Command().Text("diff -q").
809 Input(specifiedVersionFile).
810 Input(extractedVersionFile).
811 Textf(`|| (echo "Specified kernel version '$(cat %s)' does not match actual kernel version '$(cat %s)'"; exit 1)`, specifiedVersionFile, extractedVersionFile)
812 }
813
814 builder.Build("extract_kernel_info", "Extract kernel version and configs")
815
816 if proptools.Bool(a.deviceProps.Main_device) && !ctx.Config().KatiEnabled() {
817 if ctx.Config().EnableUffdGc() == "default" {
818 kernelVersionFile := android.PathForOutput(ctx, "dexpreopt/kernel_version_for_uffd_gc.txt")
819 ctx.Build(pctx, android.BuildParams{
820 Rule: android.CpIfChanged,
821 Input: extractedVersionFile,
822 Output: kernelVersionFile,
823 })
824 }
825
826 ctx.DistForGoal("droid_targets", extractedVersionFile)
827 }
828
829 return extractedVersionFile, extractedConfigsFile
830}