blob: 3b8bf9381c919bb7c41a93045cffbb65aa60b7a9 [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"`
Jihoon Kang3be17162025-01-09 20:51:54 +000090}
91
Jihoon Kangf2c53982024-10-09 17:32:52 +000092type androidDevice struct {
93 android.ModuleBase
94
95 partitionProps PartitionNameProperties
Jihoon Kang3be17162025-01-09 20:51:54 +000096
97 deviceProps DeviceProperties
Cole Fausta472a6f2025-02-10 16:10:04 -080098
99 allImagesZip android.Path
Jihoon Kangf2c53982024-10-09 17:32:52 +0000100}
101
102func AndroidDeviceFactory() android.Module {
103 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +0000104 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -0800105 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000106 return module
107}
108
Cole Faust11fda332025-01-14 16:47:19 -0800109var numMainAndroidDevicesOnceKey android.OnceKey = android.NewOnceKey("num_auto_generated_anroid_devices")
Cole Faustb55a41c2025-01-09 16:53:58 -0800110
Jihoon Kangf2c53982024-10-09 17:32:52 +0000111type partitionDepTagType struct {
112 blueprint.BaseDependencyTag
113}
114
Cole Faust2bdc5e52025-01-10 10:29:36 -0800115type superPartitionDepTagType struct {
116 blueprint.BaseDependencyTag
117}
Spandan Das29d44882025-01-15 21:12:36 +0000118type targetFilesMetadataDepTagType struct {
119 blueprint.BaseDependencyTag
120}
Cole Faust2bdc5e52025-01-10 10:29:36 -0800121
122var superPartitionDepTag superPartitionDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000123var filesystemDepTag partitionDepTagType
Spandan Das29d44882025-01-15 21:12:36 +0000124var targetFilesMetadataDepTag targetFilesMetadataDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000125
126func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
127 addDependencyIfDefined := func(dep *string) {
128 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -0800129 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +0000130 }
131 }
132
Cole Faust2bdc5e52025-01-10 10:29:36 -0800133 if a.partitionProps.Super_partition_name != nil {
134 ctx.AddDependency(ctx.Module(), superPartitionDepTag, *a.partitionProps.Super_partition_name)
135 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000136 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +0000137 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000138 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000139 addDependencyIfDefined(a.partitionProps.System_partition_name)
140 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
141 addDependencyIfDefined(a.partitionProps.Product_partition_name)
142 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000143 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800144 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000145 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
146 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
147 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000148 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800149 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
150 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
151 }
Spandan Das29d44882025-01-15 21:12:36 +0000152 a.addDepsForTargetFilesMetadata(ctx)
153}
154
155func (a *androidDevice) addDepsForTargetFilesMetadata(ctx android.BottomUpMutatorContext) {
156 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), targetFilesMetadataDepTag, "liblz4") // host variant
Jihoon Kangf2c53982024-10-09 17:32:52 +0000157}
158
Cole Faust11fda332025-01-14 16:47:19 -0800159func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
160 if proptools.Bool(a.deviceProps.Main_device) {
161 numMainAndroidDevices := ctx.Config().Once(numMainAndroidDevicesOnceKey, func() interface{} {
162 return &atomic.Int32{}
163 }).(*atomic.Int32)
164 total := numMainAndroidDevices.Add(1)
165 if total > 1 {
166 // There should only be 1 main android_device module. That one will be
167 // made the default thing to build in soong-only builds.
168 ctx.ModuleErrorf("There cannot be more than 1 main android_device module")
169 }
Jihoon Kang3be17162025-01-09 20:51:54 +0000170 }
171
Cole Faust1dcf9e42025-02-19 17:23:34 -0800172 allInstalledModules := a.allInstalledModules(ctx)
173
Spandan Das5ef1a9c2025-02-11 18:50:17 +0000174 a.buildTargetFilesZip(ctx)
Cole Faust1dcf9e42025-02-19 17:23:34 -0800175 a.buildProguardZips(ctx, allInstalledModules)
176
mrziwang2fd33a72025-01-08 12:22:08 -0800177 var deps []android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800178 if proptools.String(a.partitionProps.Super_partition_name) != "" {
Cole Faust19eb09d2025-01-14 13:27:00 -0800179 superImage := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Cole Faust2bdc5e52025-01-10 10:29:36 -0800180 if info, ok := android.OtherModuleProvider(ctx, superImage, SuperImageProvider); ok {
181 assertUnset := func(prop *string, propName string) {
182 if prop != nil && *prop != "" {
183 ctx.PropertyErrorf(propName, "Cannot be set because it's already part of the super image")
184 }
185 }
186 for _, subPartitionType := range android.SortedKeys(info.SubImageInfo) {
187 switch subPartitionType {
188 case "system":
189 assertUnset(a.partitionProps.System_partition_name, "system_partition_name")
190 case "system_ext":
191 assertUnset(a.partitionProps.System_ext_partition_name, "system_ext_partition_name")
192 case "system_dlkm":
193 assertUnset(a.partitionProps.System_dlkm_partition_name, "system_dlkm_partition_name")
194 case "system_other":
195 // TODO
196 case "product":
197 assertUnset(a.partitionProps.Product_partition_name, "product_partition_name")
198 case "vendor":
199 assertUnset(a.partitionProps.Vendor_partition_name, "vendor_partition_name")
200 case "vendor_dlkm":
201 assertUnset(a.partitionProps.Vendor_dlkm_partition_name, "vendor_dlkm_partition_name")
202 case "odm":
203 assertUnset(a.partitionProps.Odm_partition_name, "odm_partition_name")
204 case "odm_dlkm":
205 assertUnset(a.partitionProps.Odm_dlkm_partition_name, "odm_dlkm_partition_name")
206 default:
207 ctx.ModuleErrorf("Unsupported sub-partition of super partition: %q", subPartitionType)
208 }
209 }
210
211 deps = append(deps, info.SuperImage)
212 } else {
213 ctx.ModuleErrorf("Expected super image dep to provide SuperImageProvider")
214 }
215 }
Cole Faust19eb09d2025-01-14 13:27:00 -0800216 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(m android.ModuleProxy) {
mrziwang2fd33a72025-01-08 12:22:08 -0800217 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
218 if !ok {
219 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
220 }
221 if len(imageOutput.DefaultOutputFiles) != 1 {
222 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
223 }
224 deps = append(deps, imageOutput.DefaultOutputFiles[0])
225 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000226
Cole Fausta472a6f2025-02-10 16:10:04 -0800227 allImagesZip := android.PathForModuleOut(ctx, "all_images.zip")
228 allImagesZipBuilder := android.NewRuleBuilder(pctx, ctx)
Cole Faust8967d752025-02-19 17:27:29 -0800229 cmd := allImagesZipBuilder.Command().BuiltTool("soong_zip")
Cole Fausta472a6f2025-02-10 16:10:04 -0800230 for _, dep := range deps {
231 cmd.FlagWithArg("-e ", dep.Base())
232 cmd.FlagWithInput("-f ", dep)
233 }
234 cmd.FlagWithOutput("-o ", allImagesZip)
235 allImagesZipBuilder.Build("soong_all_images_zip", "all_images.zip")
236 a.allImagesZip = allImagesZip
237
Cole Faustb55a41c2025-01-09 16:53:58 -0800238 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
Cole Faust11fda332025-01-14 16:47:19 -0800239 var validations android.Paths
240 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Cole Faustb55a41c2025-01-09 16:53:58 -0800241 // In soong-only builds, build this module by default.
242 // This is the analogue to this make code:
243 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
244 ctx.Phony("droidcore-unbundled", allImagesStamp)
Cole Faust11fda332025-01-14 16:47:19 -0800245
Cole Faust19fbb072025-01-30 18:19:29 -0800246 deps = append(deps, a.copyFilesToProductOutForSoongOnly(ctx))
Cole Faustb55a41c2025-01-09 16:53:58 -0800247 }
Cole Faust11fda332025-01-14 16:47:19 -0800248
249 ctx.Build(pctx, android.BuildParams{
250 Rule: android.Touch,
251 Output: allImagesStamp,
252 Implicits: deps,
253 Validations: validations,
254 })
255
256 // Checkbuilding it causes soong to make a phony, so you can say `m <module name>`
257 ctx.CheckbuildFile(allImagesStamp)
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000258
259 a.setVbmetaPhonyTargets(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000260
261 a.distFiles(ctx)
262}
263
Jihoon Kangabec3ec2025-02-19 00:55:10 +0000264// Returns a list of modules that are installed, which are collected from the dependency
265// filesystem and super_image modules.
266func (a *androidDevice) allInstalledModules(ctx android.ModuleContext) []android.Module {
267 fsInfoMap := a.getFsInfos(ctx)
268 allOwners := make(map[string][]string)
269 for _, partition := range android.SortedKeys(fsInfoMap) {
270 fsInfo := fsInfoMap[partition]
271 for _, owner := range fsInfo.Owners {
272 allOwners[owner.Name] = append(allOwners[owner.Name], owner.Variation)
273 }
274 }
275
276 ret := []android.Module{}
277 ctx.WalkDepsProxy(func(mod, _ android.ModuleProxy) bool {
278 if variations, ok := allOwners[mod.Name()]; ok && android.InList(ctx.OtherModuleSubDir(mod), variations) {
279 ret = append(ret, mod)
280 }
281 return true
282 })
283
284 // Remove duplicates
285 ret = android.FirstUniqueFunc(ret, func(a, b android.Module) bool {
286 return a.String() == b.String()
287 })
288
289 // Sort the modules by their names and variants
290 slices.SortFunc(ret, func(a, b android.Module) int {
291 return cmp.Compare(a.String(), b.String())
292 })
293 return ret
294}
295
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000296func (a *androidDevice) distFiles(ctx android.ModuleContext) {
297 if !ctx.Config().KatiEnabled() {
298 if proptools.Bool(a.deviceProps.Main_device) {
299 fsInfoMap := a.getFsInfos(ctx)
300 for _, partition := range android.SortedKeys(fsInfoMap) {
301 fsInfo := fsInfoMap[partition]
302 if fsInfo.InstalledFiles.Json != nil {
303 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Json)
304 }
305 if fsInfo.InstalledFiles.Txt != nil {
306 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Txt)
307 }
308 }
309 }
310 }
311
Cole Faust44080412024-12-20 14:17:07 -0800312}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000313
Cole Fausta472a6f2025-02-10 16:10:04 -0800314func (a *androidDevice) MakeVars(ctx android.MakeVarsModuleContext) {
315 if proptools.Bool(a.deviceProps.Main_device) {
316 ctx.StrictRaw("SOONG_ONLY_ALL_IMAGES_ZIP", a.allImagesZip.String())
317 }
318}
319
Cole Faust1dcf9e42025-02-19 17:23:34 -0800320func (a *androidDevice) buildProguardZips(ctx android.ModuleContext, allInstalledModules []android.Module) {
321 dictZip := android.PathForModuleOut(ctx, "proguard-dict.zip")
322 dictZipBuilder := android.NewRuleBuilder(pctx, ctx)
323 dictZipCmd := dictZipBuilder.Command().BuiltTool("soong_zip").Flag("-d").FlagWithOutput("-o ", dictZip)
324
325 dictMapping := android.PathForModuleOut(ctx, "proguard-dict-mapping.textproto")
326 dictMappingBuilder := android.NewRuleBuilder(pctx, ctx)
327 dictMappingCmd := dictMappingBuilder.Command().BuiltTool("symbols_map").Flag("-merge").Output(dictMapping)
328
329 protosDir := android.PathForModuleOut(ctx, "proguard_mapping_protos")
330
331 usageZip := android.PathForModuleOut(ctx, "proguard-usage.zip")
332 usageZipBuilder := android.NewRuleBuilder(pctx, ctx)
333 usageZipCmd := usageZipBuilder.Command().BuiltTool("merge_zips").Output(usageZip)
334
335 for _, mod := range allInstalledModules {
336 if proguardInfo, ok := android.OtherModuleProvider(ctx, mod, java.ProguardProvider); ok {
337 // Maintain these out/target/common paths for backwards compatibility. They may be able
338 // to be changed if tools look up file locations from the protobuf, but I'm not
339 // exactly sure how that works.
340 dictionaryFakePath := fmt.Sprintf("out/target/common/obj/%s/%s_intermediates/proguard_dictionary", proguardInfo.Class, proguardInfo.ModuleName)
341 dictZipCmd.FlagWithArg("-e ", dictionaryFakePath)
342 dictZipCmd.FlagWithInput("-f ", proguardInfo.ProguardDictionary)
343 dictZipCmd.Textf("-e out/target/common/obj/%s/%s_intermediates/classes.jar", proguardInfo.Class, proguardInfo.ModuleName)
344 dictZipCmd.FlagWithInput("-f ", proguardInfo.ClassesJar)
345
346 protoFile := protosDir.Join(ctx, filepath.Dir(dictionaryFakePath), "proguard_dictionary.textproto")
347 ctx.Build(pctx, android.BuildParams{
348 Rule: proguardDictToProto,
349 Input: proguardInfo.ProguardDictionary,
350 Output: protoFile,
351 Args: map[string]string{
352 "location": dictionaryFakePath,
353 },
354 })
355 dictMappingCmd.Input(protoFile)
356
357 usageZipCmd.Input(proguardInfo.ProguardUsageZip)
358 }
359 }
360
361 dictZipBuilder.Build("proguard_dict_zip", "Building proguard dictionary zip")
362 dictMappingBuilder.Build("proguard_dict_mapping_proto", "Building proguard mapping proto")
363 usageZipBuilder.Build("proguard_usage_zip", "Building proguard usage zip")
364}
365
Spandan Dasef775742025-01-13 22:17:40 +0000366// Helper structs for target_files.zip creation
Spandan Dasef200ac2025-01-08 01:42:45 +0000367type targetFilesZipCopy struct {
368 srcModule *string
369 destSubdir string
370}
371
Spandan Dasef775742025-01-13 22:17:40 +0000372type targetFilesystemZipCopy struct {
373 fsInfo FilesystemInfo
374 destSubdir string
375}
376
Cole Faust44080412024-12-20 14:17:07 -0800377func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) {
378 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
379 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
380
381 builder := android.NewRuleBuilder(pctx, ctx)
382 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
383 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000384 toCopy := []targetFilesZipCopy{
385 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
386 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
387 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
388 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
389 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
390 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
391 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
392 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
393 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
394 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
395 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000396 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000397
Spandan Dasef775742025-01-13 22:17:40 +0000398 filesystemsToCopy := []targetFilesystemZipCopy{}
Spandan Dasef200ac2025-01-08 01:42:45 +0000399 for _, zipCopy := range toCopy {
400 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000401 continue
402 }
Spandan Dasef775742025-01-13 22:17:40 +0000403 filesystemsToCopy = append(
404 filesystemsToCopy,
405 targetFilesystemZipCopy{a.getFilesystemInfo(ctx, *zipCopy.srcModule), zipCopy.destSubdir},
406 )
407 }
408 // Get additional filesystems from super_partition dependency
409 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800410 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000411 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust310de662025-02-19 16:15:07 -0800412 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Dasef775742025-01-13 22:17:40 +0000413 filesystemsToCopy = append(
414 filesystemsToCopy,
415 targetFilesystemZipCopy{info.SubImageInfo[partition], strings.ToUpper(partition)},
416 )
417 }
418 } else {
419 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
420 }
421 }
422
423 for _, toCopy := range filesystemsToCopy {
424 rootDirString := toCopy.fsInfo.RootDir.String()
425 if toCopy.destSubdir == "SYSTEM" {
Spandan Dasef200ac2025-01-08 01:42:45 +0000426 rootDirString = rootDirString + "/system"
427 }
Spandan Dasef775742025-01-13 22:17:40 +0000428 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), toCopy.destSubdir)
Cole Faust44080412024-12-20 14:17:07 -0800429 builder.Command().
430 BuiltTool("acp").
Spandan Dasef775742025-01-13 22:17:40 +0000431 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, toCopy.destSubdir).
432 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
Cole Faustb36763e2025-02-18 15:21:44 -0800433 for _, extraRootDir := range toCopy.fsInfo.ExtraRootDirs {
434 builder.Command().
435 BuiltTool("acp").
436 Textf("-rd %s/. %s/%s", extraRootDir, targetFilesDir, toCopy.destSubdir).
437 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
438 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000439
Spandan Dasef775742025-01-13 22:17:40 +0000440 if toCopy.destSubdir == "SYSTEM" {
Spandan Das3ec6d062025-01-09 19:37:47 +0000441 // Create the ROOT partition in target_files.zip
Spandan Dasef775742025-01-13 22:17:40 +0000442 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", toCopy.fsInfo.RootDir, targetFilesDir.String())
Spandan Das3ec6d062025-01-09 19:37:47 +0000443 }
Cole Faust44080412024-12-20 14:17:07 -0800444 }
Spandan Das9b17df22025-01-08 23:30:45 +0000445 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000446 if a.partitionProps.Vendor_boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800447 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000448 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000449 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
450 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
451 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000452 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/VENDOR_BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000453 }
Spandan Das23511372025-01-08 23:30:45 +0000454 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000455 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/VENDOR_BOOT/vendor_bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000456 }
Spandan Das258c08f2025-01-08 23:30:45 +0000457 }
458 if a.partitionProps.Boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800459 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000460 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000461 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
462 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000463 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000464 }
465 if bootImgInfo.Kernel != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000466 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000467 // 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 +0000468 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/VENDOR_BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000469 }
Spandan Das23511372025-01-08 23:30:45 +0000470 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000471 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/BOOT/bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000472 }
Spandan Das258c08f2025-01-08 23:30:45 +0000473 }
474
Spandan Dase51ff952025-01-09 18:11:59 +0000475 if a.deviceProps.Android_info != nil {
476 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
Cole Faust11fda332025-01-14 16:47:19 -0800477 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 +0000478 }
479
Spandan Das0036fa32025-01-10 23:40:45 +0000480 a.copyImagesToTargetZip(ctx, builder, targetFilesDir)
Spandan Das29d44882025-01-15 21:12:36 +0000481 a.copyMetadataToTargetZip(ctx, builder, targetFilesDir)
Spandan Das0036fa32025-01-10 23:40:45 +0000482
Cole Faust44080412024-12-20 14:17:07 -0800483 builder.Command().
484 BuiltTool("soong_zip").
485 Text("-d").
486 FlagWithOutput("-o ", targetFilesZip).
487 FlagWithArg("-C ", targetFilesDir.String()).
488 FlagWithArg("-D ", targetFilesDir.String()).
489 Text("-sha256")
490 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
491}
492
Spandan Das0036fa32025-01-10 23:40:45 +0000493func (a *androidDevice) copyImagesToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
494 // Create an IMAGES/ subdirectory
495 builder.Command().Textf("mkdir -p %s/IMAGES", targetFilesDir.String())
496 if a.deviceProps.Bootloader != nil {
497 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader))).Textf(" %s/IMAGES/bootloader", targetFilesDir.String())
498 }
499 // Copy the filesystem ,boot and vbmeta img files to IMAGES/
500 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(child android.ModuleProxy) {
Spandan Dasa9db76d2025-01-14 01:34:43 +0000501 if strings.Contains(child.Name(), "recovery") {
502 return // skip recovery.img to match the make packaging behavior
503 }
Spandan Dasef775742025-01-13 22:17:40 +0000504 if info, ok := android.OtherModuleProvider(ctx, child, BootimgInfoProvider); ok {
505 // Check Boot img first so that the boot.img is copied and not its dep ramdisk.img
Spandan Das0036fa32025-01-10 23:40:45 +0000506 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
Spandan Dasef775742025-01-13 22:17:40 +0000507 } else if info, ok := android.OtherModuleProvider(ctx, child, FilesystemProvider); ok {
Spandan Das0036fa32025-01-10 23:40:45 +0000508 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
509 } else if info, ok := android.OtherModuleProvider(ctx, child, vbmetaPartitionProvider); ok {
510 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
511 } else {
512 ctx.ModuleErrorf("Module %s does not provide an .img file output for target_files.zip", child.Name())
513 }
514 })
Spandan Dasef775742025-01-13 22:17:40 +0000515
516 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800517 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000518 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust19eb09d2025-01-14 13:27:00 -0800519 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000520 if info.SubImageInfo[partition].OutputHermetic != nil {
521 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].OutputHermetic).Textf(" %s/IMAGES/", targetFilesDir.String())
522 }
523 if info.SubImageInfo[partition].MapFile != nil {
524 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].MapFile).Textf(" %s/IMAGES/", targetFilesDir.String())
525 }
Spandan Dasef775742025-01-13 22:17:40 +0000526 }
527 } else {
528 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
529 }
530 }
Spandan Das0036fa32025-01-10 23:40:45 +0000531}
532
Spandan Das29d44882025-01-15 21:12:36 +0000533func (a *androidDevice) copyMetadataToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
534 // Create a META/ subdirectory
535 builder.Command().Textf("mkdir -p %s/META", targetFilesDir.String())
536 if proptools.Bool(a.deviceProps.Ab_ota_updater) {
537 ctx.VisitDirectDepsProxyWithTag(targetFilesMetadataDepTag, func(child android.ModuleProxy) {
538 info, _ := android.OtherModuleProvider(ctx, child, android.OutputFilesProvider)
539 builder.Command().Textf("cp").Inputs(info.DefaultOutputFiles).Textf(" %s/META/", targetFilesDir.String())
540 })
Spandan Dasb5f40cf2025-02-12 19:36:03 +0000541 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "external/zucchini/version_info.h")).Textf(" %s/META/zucchini_config.txt", targetFilesDir.String())
542 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "system/update_engine/update_engine.conf")).Textf(" %s/META/update_engine_config.txt", targetFilesDir.String())
543 if a.getFsInfos(ctx)["system"].ErofsCompressHints != nil {
544 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].ErofsCompressHints).Textf(" %s/META/erofs_default_compress_hints.txt", targetFilesDir.String())
545 }
546 // ab_partitions.txt
547 abPartitionsSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_partitions)
548 abPartitionsSortedString := proptools.ShellEscape(strings.Join(abPartitionsSorted, "\\n"))
549 builder.Command().Textf("echo -e").Flag(abPartitionsSortedString).Textf(" > %s/META/ab_partitions.txt", targetFilesDir.String())
Spandan Das35b78742025-02-12 19:36:03 +0000550 // otakeys.txt
551 abOtaKeysSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_keys)
552 abOtaKeysSortedString := proptools.ShellEscape(strings.Join(abOtaKeysSorted, "\\n"))
553 builder.Command().Textf("echo -e").Flag(abOtaKeysSortedString).Textf(" > %s/META/otakeys.txt", targetFilesDir.String())
Spandan Das00948072025-02-12 19:36:03 +0000554 // postinstall_config.txt
555 abOtaPostInstallConfigString := proptools.ShellEscape(strings.Join(a.deviceProps.Ab_ota_postinstall_config, "\\n"))
556 builder.Command().Textf("echo -e").Flag(abOtaPostInstallConfigString).Textf(" > %s/META/postinstall_config.txt", targetFilesDir.String())
Spandan Dasf12ff9b2025-02-12 22:27:43 +0000557 // selinuxfc
558 if a.getFsInfos(ctx)["system"].SelinuxFc != nil {
559 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].SelinuxFc).Textf(" %s/META/file_contexts.bin", targetFilesDir.String())
560 }
Spandan Dasd71af182025-02-12 18:03:29 +0000561 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000562 // Copy $partition_filesystem_config.txt
563 fsInfos := a.getFsInfos(ctx)
564 for _, partition := range android.SortedKeys(fsInfos) {
565 if fsInfos[partition].FilesystemConfig == nil {
566 continue
567 }
568 if android.InList(partition, []string{"userdata"}) {
569 continue
570 }
571 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/%s", targetFilesDir.String(), a.filesystemConfigNameForTargetFiles(partition))
572 }
Spandan Das75955b12025-02-13 22:12:52 +0000573 // Copy ramdisk_node_list
Spandan Das37240d92025-02-14 00:18:41 +0000574 if ramdiskNodeList := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Ramdisk_node_list)); ramdiskNodeList != nil {
575 builder.Command().Textf("cp").Input(ramdiskNodeList).Textf(" %s/META/", targetFilesDir.String())
576 }
577 // Copy releasetools.py
578 if releaseTools := android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Releasetools_extension)); releaseTools != nil {
579 builder.Command().Textf("cp").Input(releaseTools).Textf(" %s/META/", targetFilesDir.String())
580 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000581}
582
583// Filenames for the partition specific fs_config files.
584// Hardcode the ramdisk files to their boot image prefix
585func (a *androidDevice) filesystemConfigNameForTargetFiles(partition string) string {
586 name := partition + "_filesystem_config.txt"
587 if partition == "system" {
588 name = "filesystem_config.txt"
589 } else if partition == "ramdisk" {
590 name = "init_boot_filesystem_config.txt"
591 }
592 return name
Spandan Das29d44882025-01-15 21:12:36 +0000593}
594
Cole Faust44080412024-12-20 14:17:07 -0800595func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
Cole Faust19eb09d2025-01-14 13:27:00 -0800596 fsMod := ctx.GetDirectDepProxyWithTag(depName, filesystemDepTag)
Cole Faust44080412024-12-20 14:17:07 -0800597 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
598 if !ok {
599 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
600 }
601 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000602}
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000603
604func (a *androidDevice) setVbmetaPhonyTargets(ctx android.ModuleContext) {
605 if !proptools.Bool(a.deviceProps.Main_device) {
606 return
607 }
608
609 if !ctx.Config().KatiEnabled() {
610 for _, vbmetaPartitionName := range a.partitionProps.Vbmeta_partitions {
611 img := ctx.GetDirectDepProxyWithTag(vbmetaPartitionName, filesystemDepTag)
612 if provider, ok := android.OtherModuleProvider(ctx, img, vbmetaPartitionProvider); ok {
613 // make generates `vbmetasystemimage` phony target instead of `vbmeta_systemimage` phony target.
614 partitionName := strings.ReplaceAll(provider.Name, "_", "")
615 ctx.Phony(fmt.Sprintf("%simage", partitionName), provider.Output)
616 }
617 }
618 }
619}