blob: ea46556f0d99c79b522bbc503c68be010ce1c043 [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 (
18 "android/soong/android"
19
20 "github.com/google/blueprint"
21 "github.com/google/blueprint/proptools"
22)
23
24type PartitionNameProperties struct {
25 // Name of the Boot_partition_name partition filesystem module
26 Boot_partition_name *string
27 // Name of the System partition filesystem module
28 System_partition_name *string
29 // Name of the System_ext partition filesystem module
30 System_ext_partition_name *string
31 // Name of the Product partition filesystem module
32 Product_partition_name *string
33 // Name of the Vendor partition filesystem module
34 Vendor_partition_name *string
Spandan Dasc5717162024-11-01 18:33:57 +000035 // Name of the Odm partition filesystem module
36 Odm_partition_name *string
Cole Faust3552eb62024-11-06 18:07:26 -080037 // The vbmeta partition and its "chained" partitions
38 Vbmeta_partitions []string
mrziwang23ba8762024-11-07 16:21:53 -080039 // Name of the Userdata partition filesystem module
40 Userdata_partition_name *string
Jihoon Kangf2c53982024-10-09 17:32:52 +000041}
42
43type androidDevice struct {
44 android.ModuleBase
45
46 partitionProps PartitionNameProperties
47}
48
49func AndroidDeviceFactory() android.Module {
50 module := &androidDevice{}
51 module.AddProperties(&module.partitionProps)
52 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jihoon Kangf2c53982024-10-09 17:32:52 +000053 return module
54}
55
56type partitionDepTagType struct {
57 blueprint.BaseDependencyTag
58}
59
60var filesystemDepTag partitionDepTagType
61
62func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
63 addDependencyIfDefined := func(dep *string) {
64 if dep != nil {
65 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
66 }
67 }
68
69 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
70 addDependencyIfDefined(a.partitionProps.System_partition_name)
71 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
72 addDependencyIfDefined(a.partitionProps.Product_partition_name)
73 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +000074 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -080075 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -080076 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
77 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
78 }
Jihoon Kangf2c53982024-10-09 17:32:52 +000079}
80
81func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust44080412024-12-20 14:17:07 -080082 a.buildTargetFilesZip(ctx)
83}
Jihoon Kangf2c53982024-10-09 17:32:52 +000084
Cole Faust44080412024-12-20 14:17:07 -080085func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) {
86 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
87 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
88
89 builder := android.NewRuleBuilder(pctx, ctx)
90 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
91 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
92 if a.partitionProps.Vendor_partition_name != nil {
93 fsInfo := a.getFilesystemInfo(ctx, *a.partitionProps.Vendor_partition_name)
94 builder.Command().Textf("mkdir -p %s/VENDOR", targetFilesDir.String())
95 builder.Command().
96 BuiltTool("acp").
97 Textf("-rd %s/. %s/VENDOR", fsInfo.RootDir, targetFilesDir).
98 Implicit(fsInfo.Output) // so that the staging dir is built
99 }
100 builder.Command().
101 BuiltTool("soong_zip").
102 Text("-d").
103 FlagWithOutput("-o ", targetFilesZip).
104 FlagWithArg("-C ", targetFilesDir.String()).
105 FlagWithArg("-D ", targetFilesDir.String()).
106 Text("-sha256")
107 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
108}
109
110func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
111 fsMod := ctx.GetDirectDepWithTag(depName, filesystemDepTag)
112 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
113 if !ok {
114 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
115 }
116 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000117}