blob: 2807446c3351b38eadd5e2948ae9681d00dfb04a [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
Spandan Dase06dd112025-03-05 01:29:04 +0000109 proguardDictZip android.Path
110 proguardDictMapping android.Path
111 proguardUsageZip android.Path
112 kernelConfig android.Path
113 kernelVersion android.Path
114 miscInfo android.Path
115 rootDirForFsConfig string
116 rootDirForFsConfigTimestamp android.Path
Jihoon Kangf2c53982024-10-09 17:32:52 +0000117}
118
119func AndroidDeviceFactory() android.Module {
120 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +0000121 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -0800122 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000123 return module
124}
125
Cole Faust11fda332025-01-14 16:47:19 -0800126var numMainAndroidDevicesOnceKey android.OnceKey = android.NewOnceKey("num_auto_generated_anroid_devices")
Cole Faustb55a41c2025-01-09 16:53:58 -0800127
Jihoon Kangf2c53982024-10-09 17:32:52 +0000128type partitionDepTagType struct {
129 blueprint.BaseDependencyTag
130}
131
Cole Faust2bdc5e52025-01-10 10:29:36 -0800132type superPartitionDepTagType struct {
133 blueprint.BaseDependencyTag
134}
Spandan Das29d44882025-01-15 21:12:36 +0000135type targetFilesMetadataDepTagType struct {
136 blueprint.BaseDependencyTag
137}
Cole Faust2bdc5e52025-01-10 10:29:36 -0800138
139var superPartitionDepTag superPartitionDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000140var filesystemDepTag partitionDepTagType
Spandan Das29d44882025-01-15 21:12:36 +0000141var targetFilesMetadataDepTag targetFilesMetadataDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000142
143func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
144 addDependencyIfDefined := func(dep *string) {
145 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -0800146 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +0000147 }
148 }
149
Cole Faust2bdc5e52025-01-10 10:29:36 -0800150 if a.partitionProps.Super_partition_name != nil {
151 ctx.AddDependency(ctx.Module(), superPartitionDepTag, *a.partitionProps.Super_partition_name)
152 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000153 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +0000154 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000155 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000156 addDependencyIfDefined(a.partitionProps.System_partition_name)
157 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
158 addDependencyIfDefined(a.partitionProps.Product_partition_name)
159 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000160 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800161 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000162 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
163 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
164 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000165 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800166 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
167 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
168 }
Spandan Das29d44882025-01-15 21:12:36 +0000169 a.addDepsForTargetFilesMetadata(ctx)
170}
171
172func (a *androidDevice) addDepsForTargetFilesMetadata(ctx android.BottomUpMutatorContext) {
173 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), targetFilesMetadataDepTag, "liblz4") // host variant
Jihoon Kangf2c53982024-10-09 17:32:52 +0000174}
175
Cole Faust11fda332025-01-14 16:47:19 -0800176func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
177 if proptools.Bool(a.deviceProps.Main_device) {
178 numMainAndroidDevices := ctx.Config().Once(numMainAndroidDevicesOnceKey, func() interface{} {
179 return &atomic.Int32{}
180 }).(*atomic.Int32)
181 total := numMainAndroidDevices.Add(1)
182 if total > 1 {
183 // There should only be 1 main android_device module. That one will be
184 // made the default thing to build in soong-only builds.
185 ctx.ModuleErrorf("There cannot be more than 1 main android_device module")
186 }
Jihoon Kang3be17162025-01-09 20:51:54 +0000187 }
188
Cole Faust1dcf9e42025-02-19 17:23:34 -0800189 allInstalledModules := a.allInstalledModules(ctx)
190
Spandan Dasc09353c2025-03-04 00:11:21 +0000191 a.kernelConfig, a.kernelVersion = a.extractKernelVersionAndConfigs(ctx)
Spandan Das447a0ab2025-03-04 23:10:19 +0000192 a.miscInfo = a.addMiscInfo(ctx)
Spandan Dasdad98702025-02-26 14:32:28 +0000193 a.buildTargetFilesZip(ctx, allInstalledModules)
Cole Faust1dcf9e42025-02-19 17:23:34 -0800194 a.buildProguardZips(ctx, allInstalledModules)
195
mrziwang2fd33a72025-01-08 12:22:08 -0800196 var deps []android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800197 if proptools.String(a.partitionProps.Super_partition_name) != "" {
Cole Faust19eb09d2025-01-14 13:27:00 -0800198 superImage := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Cole Faust2bdc5e52025-01-10 10:29:36 -0800199 if info, ok := android.OtherModuleProvider(ctx, superImage, SuperImageProvider); ok {
200 assertUnset := func(prop *string, propName string) {
201 if prop != nil && *prop != "" {
202 ctx.PropertyErrorf(propName, "Cannot be set because it's already part of the super image")
203 }
204 }
205 for _, subPartitionType := range android.SortedKeys(info.SubImageInfo) {
206 switch subPartitionType {
207 case "system":
208 assertUnset(a.partitionProps.System_partition_name, "system_partition_name")
209 case "system_ext":
210 assertUnset(a.partitionProps.System_ext_partition_name, "system_ext_partition_name")
211 case "system_dlkm":
212 assertUnset(a.partitionProps.System_dlkm_partition_name, "system_dlkm_partition_name")
213 case "system_other":
214 // TODO
215 case "product":
216 assertUnset(a.partitionProps.Product_partition_name, "product_partition_name")
217 case "vendor":
218 assertUnset(a.partitionProps.Vendor_partition_name, "vendor_partition_name")
219 case "vendor_dlkm":
220 assertUnset(a.partitionProps.Vendor_dlkm_partition_name, "vendor_dlkm_partition_name")
221 case "odm":
222 assertUnset(a.partitionProps.Odm_partition_name, "odm_partition_name")
223 case "odm_dlkm":
224 assertUnset(a.partitionProps.Odm_dlkm_partition_name, "odm_dlkm_partition_name")
225 default:
226 ctx.ModuleErrorf("Unsupported sub-partition of super partition: %q", subPartitionType)
227 }
228 }
229
230 deps = append(deps, info.SuperImage)
231 } else {
232 ctx.ModuleErrorf("Expected super image dep to provide SuperImageProvider")
233 }
234 }
Cole Faust19eb09d2025-01-14 13:27:00 -0800235 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(m android.ModuleProxy) {
mrziwang2fd33a72025-01-08 12:22:08 -0800236 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
237 if !ok {
238 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
239 }
240 if len(imageOutput.DefaultOutputFiles) != 1 {
241 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
242 }
243 deps = append(deps, imageOutput.DefaultOutputFiles[0])
244 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000245
Cole Fausta472a6f2025-02-10 16:10:04 -0800246 allImagesZip := android.PathForModuleOut(ctx, "all_images.zip")
247 allImagesZipBuilder := android.NewRuleBuilder(pctx, ctx)
Cole Faust8967d752025-02-19 17:27:29 -0800248 cmd := allImagesZipBuilder.Command().BuiltTool("soong_zip")
Cole Fausta472a6f2025-02-10 16:10:04 -0800249 for _, dep := range deps {
250 cmd.FlagWithArg("-e ", dep.Base())
251 cmd.FlagWithInput("-f ", dep)
252 }
253 cmd.FlagWithOutput("-o ", allImagesZip)
254 allImagesZipBuilder.Build("soong_all_images_zip", "all_images.zip")
255 a.allImagesZip = allImagesZip
256
Cole Faustb55a41c2025-01-09 16:53:58 -0800257 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
Cole Faust11fda332025-01-14 16:47:19 -0800258 var validations android.Paths
259 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Cole Faustb55a41c2025-01-09 16:53:58 -0800260 // In soong-only builds, build this module by default.
261 // This is the analogue to this make code:
262 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
263 ctx.Phony("droidcore-unbundled", allImagesStamp)
Cole Faust11fda332025-01-14 16:47:19 -0800264
Cole Faust19fbb072025-01-30 18:19:29 -0800265 deps = append(deps, a.copyFilesToProductOutForSoongOnly(ctx))
Cole Faustb55a41c2025-01-09 16:53:58 -0800266 }
Cole Faust11fda332025-01-14 16:47:19 -0800267
268 ctx.Build(pctx, android.BuildParams{
269 Rule: android.Touch,
270 Output: allImagesStamp,
271 Implicits: deps,
272 Validations: validations,
273 })
274
275 // Checkbuilding it causes soong to make a phony, so you can say `m <module name>`
276 ctx.CheckbuildFile(allImagesStamp)
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000277
278 a.setVbmetaPhonyTargets(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000279
280 a.distFiles(ctx)
281}
282
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000283// Returns a list of modules that are installed, which are collected from the dependency
284// filesystem and super_image modules.
285func (a *androidDevice) allInstalledModules(ctx android.ModuleContext) []android.Module {
286 fsInfoMap := a.getFsInfos(ctx)
287 allOwners := make(map[string][]string)
288 for _, partition := range android.SortedKeys(fsInfoMap) {
289 fsInfo := fsInfoMap[partition]
290 for _, owner := range fsInfo.Owners {
291 allOwners[owner.Name] = append(allOwners[owner.Name], owner.Variation)
292 }
293 }
294
295 ret := []android.Module{}
296 ctx.WalkDepsProxy(func(mod, _ android.ModuleProxy) bool {
Spandan Das5c78fe92025-02-26 14:25:56 +0000297 if variations, ok := allOwners[ctx.OtherModuleName(mod)]; ok && android.InList(ctx.OtherModuleSubDir(mod), variations) {
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000298 ret = append(ret, mod)
299 }
300 return true
301 })
302
303 // Remove duplicates
304 ret = android.FirstUniqueFunc(ret, func(a, b android.Module) bool {
305 return a.String() == b.String()
306 })
307
308 // Sort the modules by their names and variants
309 slices.SortFunc(ret, func(a, b android.Module) int {
310 return cmp.Compare(a.String(), b.String())
311 })
312 return ret
313}
314
Cole Faust54124c82025-02-21 14:46:02 -0800315func insertBeforeExtension(file, insertion string) string {
316 ext := filepath.Ext(file)
317 return strings.TrimSuffix(file, ext) + insertion + ext
318}
319
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000320func (a *androidDevice) distFiles(ctx android.ModuleContext) {
Cole Faust54124c82025-02-21 14:46:02 -0800321 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
322 fsInfoMap := a.getFsInfos(ctx)
323 for _, partition := range android.SortedKeys(fsInfoMap) {
324 fsInfo := fsInfoMap[partition]
325 if fsInfo.InstalledFiles.Json != nil {
326 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Json)
327 }
328 if fsInfo.InstalledFiles.Txt != nil {
329 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Txt)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000330 }
331 }
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000332
Cole Faust54124c82025-02-21 14:46:02 -0800333 namePrefix := ""
334 if ctx.Config().HasDeviceProduct() {
335 namePrefix = ctx.Config().DeviceProduct() + "-"
336 }
337 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardDictZip, namePrefix+insertBeforeExtension(a.proguardDictZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
338 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardDictMapping, namePrefix+insertBeforeExtension(a.proguardDictMapping.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
339 ctx.DistForGoalWithFilename("droidcore-unbundled", a.proguardUsageZip, namePrefix+insertBeforeExtension(a.proguardUsageZip.Base(), "-FILE_NAME_TAG_PLACEHOLDER"))
340 }
Cole Faust44080412024-12-20 14:17:07 -0800341}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000342
Yu Liu2a815b62025-02-21 20:46:25 +0000343func (a *androidDevice) MakeVars(_ android.MakeVarsModuleContext) []android.ModuleMakeVarsValue {
Cole Fausta472a6f2025-02-10 16:10:04 -0800344 if proptools.Bool(a.deviceProps.Main_device) {
Yu Liu2a815b62025-02-21 20:46:25 +0000345 return []android.ModuleMakeVarsValue{{"SOONG_ONLY_ALL_IMAGES_ZIP", a.allImagesZip.String()}}
Cole Fausta472a6f2025-02-10 16:10:04 -0800346 }
Yu Liu2a815b62025-02-21 20:46:25 +0000347 return nil
Cole Fausta472a6f2025-02-10 16:10:04 -0800348}
349
Cole Faust1dcf9e42025-02-19 17:23:34 -0800350func (a *androidDevice) buildProguardZips(ctx android.ModuleContext, allInstalledModules []android.Module) {
351 dictZip := android.PathForModuleOut(ctx, "proguard-dict.zip")
352 dictZipBuilder := android.NewRuleBuilder(pctx, ctx)
353 dictZipCmd := dictZipBuilder.Command().BuiltTool("soong_zip").Flag("-d").FlagWithOutput("-o ", dictZip)
354
355 dictMapping := android.PathForModuleOut(ctx, "proguard-dict-mapping.textproto")
356 dictMappingBuilder := android.NewRuleBuilder(pctx, ctx)
357 dictMappingCmd := dictMappingBuilder.Command().BuiltTool("symbols_map").Flag("-merge").Output(dictMapping)
358
359 protosDir := android.PathForModuleOut(ctx, "proguard_mapping_protos")
360
361 usageZip := android.PathForModuleOut(ctx, "proguard-usage.zip")
362 usageZipBuilder := android.NewRuleBuilder(pctx, ctx)
363 usageZipCmd := usageZipBuilder.Command().BuiltTool("merge_zips").Output(usageZip)
364
365 for _, mod := range allInstalledModules {
366 if proguardInfo, ok := android.OtherModuleProvider(ctx, mod, java.ProguardProvider); ok {
367 // Maintain these out/target/common paths for backwards compatibility. They may be able
368 // to be changed if tools look up file locations from the protobuf, but I'm not
369 // exactly sure how that works.
370 dictionaryFakePath := fmt.Sprintf("out/target/common/obj/%s/%s_intermediates/proguard_dictionary", proguardInfo.Class, proguardInfo.ModuleName)
371 dictZipCmd.FlagWithArg("-e ", dictionaryFakePath)
372 dictZipCmd.FlagWithInput("-f ", proguardInfo.ProguardDictionary)
373 dictZipCmd.Textf("-e out/target/common/obj/%s/%s_intermediates/classes.jar", proguardInfo.Class, proguardInfo.ModuleName)
374 dictZipCmd.FlagWithInput("-f ", proguardInfo.ClassesJar)
375
376 protoFile := protosDir.Join(ctx, filepath.Dir(dictionaryFakePath), "proguard_dictionary.textproto")
377 ctx.Build(pctx, android.BuildParams{
378 Rule: proguardDictToProto,
379 Input: proguardInfo.ProguardDictionary,
380 Output: protoFile,
381 Args: map[string]string{
382 "location": dictionaryFakePath,
383 },
384 })
385 dictMappingCmd.Input(protoFile)
386
387 usageZipCmd.Input(proguardInfo.ProguardUsageZip)
388 }
389 }
390
391 dictZipBuilder.Build("proguard_dict_zip", "Building proguard dictionary zip")
392 dictMappingBuilder.Build("proguard_dict_mapping_proto", "Building proguard mapping proto")
393 usageZipBuilder.Build("proguard_usage_zip", "Building proguard usage zip")
Cole Faust54124c82025-02-21 14:46:02 -0800394
395 a.proguardDictZip = dictZip
396 a.proguardDictMapping = dictMapping
397 a.proguardUsageZip = usageZip
Cole Faust1dcf9e42025-02-19 17:23:34 -0800398}
399
Spandan Dasef775742025-01-13 22:17:40 +0000400// Helper structs for target_files.zip creation
Spandan Dasef200ac2025-01-08 01:42:45 +0000401type targetFilesZipCopy struct {
402 srcModule *string
403 destSubdir string
404}
405
Spandan Dasef775742025-01-13 22:17:40 +0000406type targetFilesystemZipCopy struct {
407 fsInfo FilesystemInfo
408 destSubdir string
409}
410
Spandan Dasdad98702025-02-26 14:32:28 +0000411func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext, allInstalledModules []android.Module) {
Cole Faust44080412024-12-20 14:17:07 -0800412 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
413 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
414
415 builder := android.NewRuleBuilder(pctx, ctx)
416 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
417 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000418 toCopy := []targetFilesZipCopy{
419 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
420 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
421 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
422 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
423 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
424 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
425 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
426 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
427 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
428 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
429 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000430 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000431
Spandan Dasef775742025-01-13 22:17:40 +0000432 filesystemsToCopy := []targetFilesystemZipCopy{}
Spandan Dasef200ac2025-01-08 01:42:45 +0000433 for _, zipCopy := range toCopy {
434 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000435 continue
436 }
Spandan Dasef775742025-01-13 22:17:40 +0000437 filesystemsToCopy = append(
438 filesystemsToCopy,
439 targetFilesystemZipCopy{a.getFilesystemInfo(ctx, *zipCopy.srcModule), zipCopy.destSubdir},
440 )
441 }
442 // Get additional filesystems from super_partition dependency
443 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800444 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000445 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust310de662025-02-19 16:15:07 -0800446 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Dasef775742025-01-13 22:17:40 +0000447 filesystemsToCopy = append(
448 filesystemsToCopy,
449 targetFilesystemZipCopy{info.SubImageInfo[partition], strings.ToUpper(partition)},
450 )
451 }
452 } else {
453 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
454 }
455 }
456
457 for _, toCopy := range filesystemsToCopy {
458 rootDirString := toCopy.fsInfo.RootDir.String()
459 if toCopy.destSubdir == "SYSTEM" {
Spandan Dasef200ac2025-01-08 01:42:45 +0000460 rootDirString = rootDirString + "/system"
461 }
Spandan Dasef775742025-01-13 22:17:40 +0000462 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), toCopy.destSubdir)
Cole Faust44080412024-12-20 14:17:07 -0800463 builder.Command().
464 BuiltTool("acp").
Spandan Dasef775742025-01-13 22:17:40 +0000465 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, toCopy.destSubdir).
466 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
Cole Faustb36763e2025-02-18 15:21:44 -0800467 for _, extraRootDir := range toCopy.fsInfo.ExtraRootDirs {
468 builder.Command().
469 BuiltTool("acp").
470 Textf("-rd %s/. %s/%s", extraRootDir, targetFilesDir, toCopy.destSubdir).
471 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
472 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000473
Spandan Dasef775742025-01-13 22:17:40 +0000474 if toCopy.destSubdir == "SYSTEM" {
Spandan Das3ec6d062025-01-09 19:37:47 +0000475 // Create the ROOT partition in target_files.zip
Spandan Dasef775742025-01-13 22:17:40 +0000476 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", toCopy.fsInfo.RootDir, targetFilesDir.String())
Spandan Dase06dd112025-03-05 01:29:04 +0000477 // Add a duplicate rule to assemble the ROOT/ directory in separate intermediates.
478 // The output timestamp will be an input to a separate fs_config call.
479 a.rootDirForFsConfig = android.PathForModuleOut(ctx, "root_dir_for_fs_config").String()
480 rootDirBuilder := android.NewRuleBuilder(pctx, ctx)
481 rootDirForFsConfigTimestamp := android.PathForModuleOut(ctx, "root_dir_for_fs_config.timestamp")
482 rootDirBuilder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s", toCopy.fsInfo.RootDir, a.rootDirForFsConfig).
483 Implicit(toCopy.fsInfo.Output).
484 Text("&& touch").
485 Output(rootDirForFsConfigTimestamp)
486 rootDirBuilder.Build("assemble_root_dir_for_fs_config", "Assemble ROOT/ for fs_config")
487 a.rootDirForFsConfigTimestamp = rootDirForFsConfigTimestamp
Spandan Das3ec6d062025-01-09 19:37:47 +0000488 }
Cole Faust44080412024-12-20 14:17:07 -0800489 }
Spandan Das9b17df22025-01-08 23:30:45 +0000490 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000491 if a.partitionProps.Vendor_boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800492 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000493 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000494 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
495 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
496 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000497 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/VENDOR_BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000498 }
Spandan Das23511372025-01-08 23:30:45 +0000499 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000500 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/VENDOR_BOOT/vendor_bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000501 }
Spandan Das258c08f2025-01-08 23:30:45 +0000502 }
503 if a.partitionProps.Boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800504 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000505 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000506 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
507 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000508 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000509 }
510 if bootImgInfo.Kernel != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000511 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000512 // 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 +0000513 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/VENDOR_BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000514 }
Spandan Das23511372025-01-08 23:30:45 +0000515 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000516 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/BOOT/bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000517 }
Spandan Das258c08f2025-01-08 23:30:45 +0000518 }
519
Spandan Dase51ff952025-01-09 18:11:59 +0000520 if a.deviceProps.Android_info != nil {
521 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
Cole Faust11fda332025-01-14 16:47:19 -0800522 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 +0000523 }
524
Spandan Das0036fa32025-01-10 23:40:45 +0000525 a.copyImagesToTargetZip(ctx, builder, targetFilesDir)
Spandan Dasdad98702025-02-26 14:32:28 +0000526 a.copyMetadataToTargetZip(ctx, builder, targetFilesDir, allInstalledModules)
Spandan Das0036fa32025-01-10 23:40:45 +0000527
Cole Faust44080412024-12-20 14:17:07 -0800528 builder.Command().
529 BuiltTool("soong_zip").
530 Text("-d").
531 FlagWithOutput("-o ", targetFilesZip).
532 FlagWithArg("-C ", targetFilesDir.String()).
533 FlagWithArg("-D ", targetFilesDir.String()).
534 Text("-sha256")
535 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
536}
537
Spandan Das0036fa32025-01-10 23:40:45 +0000538func (a *androidDevice) copyImagesToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
539 // Create an IMAGES/ subdirectory
540 builder.Command().Textf("mkdir -p %s/IMAGES", targetFilesDir.String())
541 if a.deviceProps.Bootloader != nil {
542 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader))).Textf(" %s/IMAGES/bootloader", targetFilesDir.String())
543 }
544 // Copy the filesystem ,boot and vbmeta img files to IMAGES/
545 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(child android.ModuleProxy) {
Spandan Dasa9db76d2025-01-14 01:34:43 +0000546 if strings.Contains(child.Name(), "recovery") {
547 return // skip recovery.img to match the make packaging behavior
548 }
Spandan Dasef775742025-01-13 22:17:40 +0000549 if info, ok := android.OtherModuleProvider(ctx, child, BootimgInfoProvider); ok {
550 // Check Boot img first so that the boot.img is copied and not its dep ramdisk.img
Spandan Das0036fa32025-01-10 23:40:45 +0000551 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
Spandan Dasef775742025-01-13 22:17:40 +0000552 } else if info, ok := android.OtherModuleProvider(ctx, child, FilesystemProvider); ok {
Spandan Das0036fa32025-01-10 23:40:45 +0000553 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
554 } else if info, ok := android.OtherModuleProvider(ctx, child, vbmetaPartitionProvider); ok {
555 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
556 } else {
557 ctx.ModuleErrorf("Module %s does not provide an .img file output for target_files.zip", child.Name())
558 }
559 })
Spandan Dasef775742025-01-13 22:17:40 +0000560
561 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800562 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000563 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust19eb09d2025-01-14 13:27:00 -0800564 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000565 if info.SubImageInfo[partition].OutputHermetic != nil {
566 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].OutputHermetic).Textf(" %s/IMAGES/", targetFilesDir.String())
567 }
568 if info.SubImageInfo[partition].MapFile != nil {
569 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].MapFile).Textf(" %s/IMAGES/", targetFilesDir.String())
570 }
Spandan Dasef775742025-01-13 22:17:40 +0000571 }
572 } else {
573 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
574 }
575 }
Spandan Das0036fa32025-01-10 23:40:45 +0000576}
577
Spandan Dasdad98702025-02-26 14:32:28 +0000578func (a *androidDevice) copyMetadataToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath, allInstalledModules []android.Module) {
Spandan Das29d44882025-01-15 21:12:36 +0000579 // Create a META/ subdirectory
580 builder.Command().Textf("mkdir -p %s/META", targetFilesDir.String())
581 if proptools.Bool(a.deviceProps.Ab_ota_updater) {
582 ctx.VisitDirectDepsProxyWithTag(targetFilesMetadataDepTag, func(child android.ModuleProxy) {
583 info, _ := android.OtherModuleProvider(ctx, child, android.OutputFilesProvider)
584 builder.Command().Textf("cp").Inputs(info.DefaultOutputFiles).Textf(" %s/META/", targetFilesDir.String())
585 })
Spandan Dasb5f40cf2025-02-12 19:36:03 +0000586 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "external/zucchini/version_info.h")).Textf(" %s/META/zucchini_config.txt", targetFilesDir.String())
587 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "system/update_engine/update_engine.conf")).Textf(" %s/META/update_engine_config.txt", targetFilesDir.String())
588 if a.getFsInfos(ctx)["system"].ErofsCompressHints != nil {
589 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].ErofsCompressHints).Textf(" %s/META/erofs_default_compress_hints.txt", targetFilesDir.String())
590 }
591 // ab_partitions.txt
592 abPartitionsSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_partitions)
593 abPartitionsSortedString := proptools.ShellEscape(strings.Join(abPartitionsSorted, "\\n"))
594 builder.Command().Textf("echo -e").Flag(abPartitionsSortedString).Textf(" > %s/META/ab_partitions.txt", targetFilesDir.String())
Spandan Das35b78742025-02-12 19:36:03 +0000595 // otakeys.txt
596 abOtaKeysSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_keys)
597 abOtaKeysSortedString := proptools.ShellEscape(strings.Join(abOtaKeysSorted, "\\n"))
598 builder.Command().Textf("echo -e").Flag(abOtaKeysSortedString).Textf(" > %s/META/otakeys.txt", targetFilesDir.String())
Spandan Das00948072025-02-12 19:36:03 +0000599 // postinstall_config.txt
600 abOtaPostInstallConfigString := proptools.ShellEscape(strings.Join(a.deviceProps.Ab_ota_postinstall_config, "\\n"))
601 builder.Command().Textf("echo -e").Flag(abOtaPostInstallConfigString).Textf(" > %s/META/postinstall_config.txt", targetFilesDir.String())
Spandan Dasf12ff9b2025-02-12 22:27:43 +0000602 // selinuxfc
603 if a.getFsInfos(ctx)["system"].SelinuxFc != nil {
604 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].SelinuxFc).Textf(" %s/META/file_contexts.bin", targetFilesDir.String())
605 }
Spandan Dasd71af182025-02-12 18:03:29 +0000606 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000607 // Copy $partition_filesystem_config.txt
608 fsInfos := a.getFsInfos(ctx)
609 for _, partition := range android.SortedKeys(fsInfos) {
610 if fsInfos[partition].FilesystemConfig == nil {
611 continue
612 }
613 if android.InList(partition, []string{"userdata"}) {
614 continue
615 }
Spandan Das853a7a42025-02-27 07:44:51 +0000616 if partition != "vendor_ramdisk" {
617 // vendor_ramdisk will be handled separately.
618 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/%s", targetFilesDir.String(), a.filesystemConfigNameForTargetFiles(partition))
619 }
Spandan Das54af3572025-02-27 07:42:08 +0000620 if partition == "ramdisk" {
621 // Create an additional copy at boot_filesystem_config.txt
622 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/boot_filesystem_config.txt", targetFilesDir.String())
623 }
Spandan Das853a7a42025-02-27 07:44:51 +0000624 if partition == "system" {
625 // Create root_filesystem_config from the assembled ROOT/ intermediates directory
Spandan Dase06dd112025-03-05 01:29:04 +0000626 a.generateFilesystemConfigForTargetFiles(ctx, builder, a.rootDirForFsConfigTimestamp, targetFilesDir.String(), a.rootDirForFsConfig, "root_filesystem_config.txt")
Spandan Das853a7a42025-02-27 07:44:51 +0000627 }
628 if partition == "vendor_ramdisk" {
629 // Create vendor_boot_filesystem_config from the assembled VENDOR_BOOT/RAMDISK intermediates directory
Spandan Dase06dd112025-03-05 01:29:04 +0000630 a.generateFilesystemConfigForTargetFiles(ctx, builder, nil, targetFilesDir.String(), targetFilesDir.String()+"/VENDOR_BOOT/RAMDISK", "vendor_boot_filesystem_config.txt")
Spandan Das853a7a42025-02-27 07:44:51 +0000631 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000632 }
Spandan Das75955b12025-02-13 22:12:52 +0000633 // Copy ramdisk_node_list
Spandan Das37240d92025-02-14 00:18:41 +0000634 if ramdiskNodeList := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Ramdisk_node_list)); ramdiskNodeList != nil {
635 builder.Command().Textf("cp").Input(ramdiskNodeList).Textf(" %s/META/", targetFilesDir.String())
636 }
637 // Copy releasetools.py
638 if releaseTools := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Releasetools_extension)); releaseTools != nil {
639 builder.Command().Textf("cp").Input(releaseTools).Textf(" %s/META/", targetFilesDir.String())
640 }
Spandan Dasdad98702025-02-26 14:32:28 +0000641 // apexkeys.txt
642 var installedApexKeys []android.Path
643 for _, installedModule := range allInstalledModules {
644 if info, ok := android.OtherModuleProvider(ctx, installedModule, ApexKeyPathInfoProvider); ok {
645 installedApexKeys = append(installedApexKeys, info.ApexKeyPath)
646 }
647 }
648 installedApexKeys = android.SortedUniquePaths(installedApexKeys) // Sort by keypath to match make
649 builder.Command().Text("cat").Inputs(installedApexKeys).Textf(" >> %s/META/apexkeys.txt", targetFilesDir.String())
Spandan Das3dfa17f2025-02-28 09:48:28 +0000650 // Copy fastboot-info.txt
651 if fastbootInfo := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.FastbootInfo)); fastbootInfo != nil {
652 // TODO (b/399788523): Autogenerate fastboot-info.txt if there is no source fastboot-info.txt
653 // https://cs.android.com/android/_/android/platform/build/+/80b9546f8f69e78b8fe1870e0e745d70fc18dfcd:core/Makefile;l=5831-5893;drc=077490384423dff9eac954da5c001c6f0be3fa6e;bpv=0;bpt=0
654 builder.Command().Textf("cp").Input(fastbootInfo).Textf(" %s/META/fastboot-info.txt", targetFilesDir.String())
655 }
Spandan Dasdad98702025-02-26 14:32:28 +0000656
Spandan Dasc09353c2025-03-04 00:11:21 +0000657 // kernel_configs.txt and kernel_version.txt
658 if a.kernelConfig != nil {
659 builder.Command().Textf("cp").Input(a.kernelConfig).Textf(" %s/META/", targetFilesDir.String())
660 }
661 if a.kernelVersion != nil {
662 builder.Command().Textf("cp").Input(a.kernelVersion).Textf(" %s/META/", targetFilesDir.String())
663 }
Spandan Das447a0ab2025-03-04 23:10:19 +0000664 // misc_info.txt
665 if a.miscInfo != nil {
666 builder.Command().Textf("cp").Input(a.miscInfo).Textf(" %s/META/", targetFilesDir.String())
667 }
Spandan Das4100dd02025-03-04 23:14:34 +0000668 // apex_info.pb, care_map.pb, vbmeta_digest.txt
669 a.addImgToTargetFiles(ctx, builder, targetFilesDir.String())
Spandan Dasc09353c2025-03-04 00:11:21 +0000670
Spandan Dasf079f0d2025-02-27 13:13:50 +0000671 if a.partitionProps.Super_partition_name != nil {
672 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
673 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
674 // dynamic_partitions_info.txt
675 // TODO (b/390192334): Add `building_super_empty_partition=true`
676 builder.Command().Text("cp").Input(info.DynamicPartitionsInfo).Textf(" %s/META/", targetFilesDir.String())
677 } else {
678 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
679 }
680 }
681
Spandan Dasdd262fb2025-02-13 00:15:59 +0000682}
683
Spandan Das447a0ab2025-03-04 23:10:19 +0000684// A partial implementation of make's $PRODUCT_OUT/misc_info.txt
685// 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
686// This file is subsequently used by add_img_to_target_files to create additioanl metadata files like apex_info.pb
687// TODO (b/399788119): Complete the migration of misc_info.txt
688func (a *androidDevice) addMiscInfo(ctx android.ModuleContext) android.Path {
689 builder := android.NewRuleBuilder(pctx, ctx)
690 miscInfo := android.PathForModuleOut(ctx, "misc_info.txt")
691 builder.Command().
692 Textf("rm -f %s", miscInfo).
693 Textf("&& echo recovery_api_version=%s >> %s", ctx.Config().VendorConfig("recovery").String("recovery_api_version"), miscInfo).
694 Textf("&& echo fstab_version=%s >> %s", ctx.Config().VendorConfig("recovery").String("recovery_fstab_version"), miscInfo).
695 ImplicitOutput(miscInfo)
696
697 if a.partitionProps.Recovery_partition_name == nil {
698 builder.Command().Textf("echo no_recovery=true >> %s", miscInfo)
699 }
700 fsInfos := a.getFsInfos(ctx)
701 for _, partition := range android.SortedKeys(fsInfos) {
702 if fsInfos[partition].UseAvb {
703 builder.Command().Textf("echo 'avb_%s_hashtree_enable=true' >> %s", partition, miscInfo)
704 }
705 }
706 if len(a.partitionProps.Vbmeta_partitions) > 0 {
707 builder.Command().
708 Textf("echo avb_enable=true >> %s", miscInfo).
709 Textf("&& echo avb_building_vbmeta_image=true >> %s", miscInfo).
710 Textf("&& echo avb_avbtool=avbtool >> %s", miscInfo)
711 }
712 if a.partitionProps.Boot_partition_name != nil {
713 builder.Command().Textf("echo boot_images=boot.img >> %s", miscInfo)
714 }
715
716 builder.Build("misc_info", "Building misc_info")
717
718 return miscInfo
719}
720
Spandan Das4100dd02025-03-04 23:14:34 +0000721// addImgToTargetFiles invokes `add_img_to_target_files` and creates the following files in META/
722// - apex_info.pb
723// - care_map.pb
724// - vbmeta_digest.txt
725func (a *androidDevice) addImgToTargetFiles(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir string) {
726 mkbootimg := ctx.Config().HostToolPath(ctx, "mkbootimg")
727 builder.Command().
728 Textf("PATH=%s:$PATH", ctx.Config().HostToolDir()).
729 Textf("MKBOOTIMG=%s", mkbootimg).
730 Implicit(mkbootimg).
731 BuiltTool("add_img_to_target_files").
732 Flag("-a -v -p").
733 Flag(ctx.Config().HostToolDir()).
734 Text(targetFilesDir)
735}
736
Spandan Dasdad98702025-02-26 14:32:28 +0000737type ApexKeyPathInfo struct {
738 ApexKeyPath android.Path
739}
740
741var ApexKeyPathInfoProvider = blueprint.NewProvider[ApexKeyPathInfo]()
742
Spandan Dase06dd112025-03-05 01:29:04 +0000743func (a *androidDevice) generateFilesystemConfigForTargetFiles(ctx android.ModuleContext, builder *android.RuleBuilder, stagingDirTimestamp android.Path, targetFilesDir, stagingDir, filename string) {
Spandan Das853a7a42025-02-27 07:44:51 +0000744 fsConfigOut := android.PathForModuleOut(ctx, filename)
745 ctx.Build(pctx, android.BuildParams{
Spandan Dase06dd112025-03-05 01:29:04 +0000746 Rule: fsConfigRule,
747 Implicit: stagingDirTimestamp,
748 Output: fsConfigOut,
Spandan Das853a7a42025-02-27 07:44:51 +0000749 Args: map[string]string{
750 "rootDir": stagingDir,
751 "prefix": "",
752 },
753 })
754 builder.Command().Textf("cp").Input(fsConfigOut).Textf(" %s/META/", targetFilesDir)
755}
756
Spandan Dasdd262fb2025-02-13 00:15:59 +0000757// Filenames for the partition specific fs_config files.
758// Hardcode the ramdisk files to their boot image prefix
759func (a *androidDevice) filesystemConfigNameForTargetFiles(partition string) string {
760 name := partition + "_filesystem_config.txt"
761 if partition == "system" {
762 name = "filesystem_config.txt"
763 } else if partition == "ramdisk" {
764 name = "init_boot_filesystem_config.txt"
765 }
766 return name
Spandan Das29d44882025-01-15 21:12:36 +0000767}
768
Cole Faust44080412024-12-20 14:17:07 -0800769func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
Cole Faust19eb09d2025-01-14 13:27:00 -0800770 fsMod := ctx.GetDirectDepProxyWithTag(depName, filesystemDepTag)
Cole Faust44080412024-12-20 14:17:07 -0800771 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
772 if !ok {
773 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
774 }
775 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000776}
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000777
778func (a *androidDevice) setVbmetaPhonyTargets(ctx android.ModuleContext) {
779 if !proptools.Bool(a.deviceProps.Main_device) {
780 return
781 }
782
783 if !ctx.Config().KatiEnabled() {
784 for _, vbmetaPartitionName := range a.partitionProps.Vbmeta_partitions {
785 img := ctx.GetDirectDepProxyWithTag(vbmetaPartitionName, filesystemDepTag)
786 if provider, ok := android.OtherModuleProvider(ctx, img, vbmetaPartitionProvider); ok {
787 // make generates `vbmetasystemimage` phony target instead of `vbmeta_systemimage` phony target.
788 partitionName := strings.ReplaceAll(provider.Name, "_", "")
789 ctx.Phony(fmt.Sprintf("%simage", partitionName), provider.Output)
790 }
791 }
792 }
793}
Cole Faust21c11462025-03-03 14:13:17 -0800794
795func (a *androidDevice) getKernel(ctx android.ModuleContext) android.Path {
796 if a.partitionProps.Boot_partition_name != nil {
797 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
798 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
799 return bootImgInfo.Kernel
800 }
801 return nil
802}
803
804// Gets the kernel version and configs from the actual kernel file itself. Roughly equivalent to
805// this make code: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=5443;drc=c0b66fc59de069e06ce0ffd703d4d21613be30c6
806// However, it is a simplified version of that make code. Differences include:
807// - Not handling BOARD_KERNEL_CONFIG_FILE because BOARD_KERNEL_CONFIG_FILE was never used.
808// - Not unpacking the bootimage, as we should be able to just always export the kernel directly
809// in the BootimgInfo. We don't currently support prebuilt boot images, but even if we add that
810// in the future, it can be done in a prebuilt_bootimage module type that still exports the same
811// BootimgInfo.
812// - We don't print a warning and output '<unknown-kernel>' to kernel_version_for_uffd_gc.txt
813// because we expect the kernel to always be present. If it's not, we will get an error that
814// kernel_version_for_uffd_gc.txt doesn't exist. This may require later tweaking to the
815// dexpreopt rules so that they don't attempt to access that file in builds that don't have
816// a kernel.
817func (a *androidDevice) extractKernelVersionAndConfigs(ctx android.ModuleContext) (android.Path, android.Path) {
818 kernel := a.getKernel(ctx)
819 // If there's no kernel, don't create kernel version / kernel config files. Reverse dependencies
820 // on those files have to account for this, for example by disabling dexpreopt in unbundled
821 // builds.
822 if kernel == nil {
823 return nil, nil
824 }
825
826 lz4tool := ctx.Config().HostToolPath(ctx, "lz4")
827
828 extractedVersionFile := android.PathForModuleOut(ctx, "kernel_version.txt")
829 extractedConfigsFile := android.PathForModuleOut(ctx, "kernel_configs.txt")
830 builder := android.NewRuleBuilder(pctx, ctx)
831 builder.Command().BuiltTool("extract_kernel").
832 Flag("--tools lz4:"+lz4tool.String()).Implicit(lz4tool).
833 FlagWithInput("--input ", kernel).
834 FlagWithOutput("--output-release ", extractedVersionFile).
835 FlagWithOutput("--output-configs ", extractedConfigsFile)
836
837 if specifiedVersion := proptools.String(a.deviceProps.Kernel_version); specifiedVersion != "" {
838 specifiedVersionFile := android.PathForModuleOut(ctx, "specified_kernel_version.txt")
839 android.WriteFileRuleVerbatim(ctx, specifiedVersionFile, specifiedVersion)
840 builder.Command().Text("diff -q").
841 Input(specifiedVersionFile).
842 Input(extractedVersionFile).
843 Textf(`|| (echo "Specified kernel version '$(cat %s)' does not match actual kernel version '$(cat %s)'"; exit 1)`, specifiedVersionFile, extractedVersionFile)
844 }
845
846 builder.Build("extract_kernel_info", "Extract kernel version and configs")
847
848 if proptools.Bool(a.deviceProps.Main_device) && !ctx.Config().KatiEnabled() {
849 if ctx.Config().EnableUffdGc() == "default" {
850 kernelVersionFile := android.PathForOutput(ctx, "dexpreopt/kernel_version_for_uffd_gc.txt")
851 ctx.Build(pctx, android.BuildParams{
852 Rule: android.CpIfChanged,
853 Input: extractedVersionFile,
854 Output: kernelVersionFile,
855 })
856 }
857
858 ctx.DistForGoal("droid_targets", extractedVersionFile)
859 }
860
861 return extractedVersionFile, extractedConfigsFile
862}