blob: 20a195313550ec7ec3177bdaa26c75878ed34736 [file] [log] [blame]
Justin Yun74f3f302024-05-07 14:32:14 +09001// 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"
Marybeth Fairc8c74d62024-12-04 14:22:40 -050019 "strconv"
Justin Yund5df5182025-02-11 18:28:53 +090020 "strings"
Justin Yun74f3f302024-05-07 14:32:14 +090021
Cole Faust34592c02024-12-13 11:20:24 -080022 "github.com/google/blueprint"
Justin Yun74f3f302024-05-07 14:32:14 +090023 "github.com/google/blueprint/proptools"
24)
25
Cole Faust781eff32025-02-14 14:21:57 -080026func init() {
27 pctx.HostBinToolVariable("aconfig", "aconfig")
28}
29
30var (
31 aconfigCreateStorage = pctx.AndroidStaticRule("aconfig_create_storage", blueprint.RuleParams{
32 Command: `$aconfig create-storage --container $container --file $fileType --out $out --cache $in --version $version`,
33 CommandDeps: []string{"$aconfig"},
34 }, "container", "fileType", "version")
Justin Yund5df5182025-02-11 18:28:53 +090035
36 subPartitionsInPartition = map[string][]string{
37 "system": {"system_ext", "product", "vendor"},
38 "vendor": {"odm"},
39 }
Cole Faust781eff32025-02-14 14:21:57 -080040)
41
Cole Faust19fbb072025-01-30 18:19:29 -080042func (f *filesystem) buildAconfigFlagsFiles(
43 ctx android.ModuleContext,
44 builder *android.RuleBuilder,
45 specs map[string]android.PackagingSpec,
46 dir android.OutputPath,
47 fullInstallPaths *[]FullInstallPathInfo,
48) {
Cole Faust34592c02024-12-13 11:20:24 -080049 if !proptools.Bool(f.properties.Gen_aconfig_flags_pb) {
50 return
51 }
52
Justin Yund5df5182025-02-11 18:28:53 +090053 partition := f.PartitionType()
54 subPartitionsFound := map[string]bool{}
55 fullInstallPath := android.PathForModuleInPartitionInstall(ctx, partition)
Cole Faust34592c02024-12-13 11:20:24 -080056
Justin Yund5df5182025-02-11 18:28:53 +090057 for _, subPartition := range subPartitionsInPartition[partition] {
58 subPartitionsFound[subPartition] = false
Marybeth Fairc8c74d62024-12-04 14:22:40 -050059 }
60
Justin Yund5df5182025-02-11 18:28:53 +090061 var caches []android.Path
62 for _, ps := range specs {
63 caches = append(caches, ps.GetAconfigPaths()...)
64 for subPartition, found := range subPartitionsFound {
65 if !found && strings.HasPrefix(ps.RelPathInPackage(), subPartition+"/") {
66 subPartitionsFound[subPartition] = true
67 break
68 }
69 }
70 }
71 caches = android.SortedUniquePaths(caches)
Cole Faust781eff32025-02-14 14:21:57 -080072
Justin Yund5df5182025-02-11 18:28:53 +090073 buildAconfigFlagsFiles := func(container string, dir android.OutputPath, fullInstallPath android.InstallPath) {
74 aconfigFlagsPb := android.PathForModuleOut(ctx, "aconfig", container, "aconfig_flags.pb")
75 aconfigFlagsPbBuilder := android.NewRuleBuilder(pctx, ctx)
76 cmd := aconfigFlagsPbBuilder.Command().
77 BuiltTool("aconfig").
78 Text(" dump-cache --dedup --format protobuf --out").
79 Output(aconfigFlagsPb).
80 Textf("--filter container:%s+state:ENABLED", container).
81 Textf("--filter container:%s+permission:READ_WRITE", container)
82 for _, cache := range caches {
83 cmd.FlagWithInput("--cache ", cache)
84 }
85 aconfigFlagsPbBuilder.Build(container+"_aconfig_flags_pb", "build aconfig_flags.pb")
86
87 installEtcDir := dir.Join(ctx, "etc")
88 installAconfigFlagsPath := installEtcDir.Join(ctx, "aconfig_flags.pb")
89 builder.Command().Text("mkdir -p ").Text(installEtcDir.String())
90 builder.Command().Text("cp").Input(aconfigFlagsPb).Text(installAconfigFlagsPath.String())
Cole Faust781eff32025-02-14 14:21:57 -080091 *fullInstallPaths = append(*fullInstallPaths, FullInstallPathInfo{
Justin Yund5df5182025-02-11 18:28:53 +090092 FullInstallPath: fullInstallPath.Join(ctx, "etc/aconfig_flags.pb"),
93 SourcePath: aconfigFlagsPb,
Cole Faust781eff32025-02-14 14:21:57 -080094 })
Justin Yund5df5182025-02-11 18:28:53 +090095 f.appendToEntry(ctx, installAconfigFlagsPath)
96
97 // To enable fingerprint, we need to have v2 storage files. The default version is 1.
98 storageFilesVersion := 1
99 if ctx.Config().ReleaseFingerprintAconfigPackages() {
100 storageFilesVersion = 2
101 }
102
103 installAconfigStorageDir := installEtcDir.Join(ctx, "aconfig")
104 builder.Command().Text("mkdir -p").Text(installAconfigStorageDir.String())
105
106 generatePartitionAconfigStorageFile := func(fileType, fileName string) {
107 outPath := android.PathForModuleOut(ctx, "aconfig", container, fileName)
108 installPath := installAconfigStorageDir.Join(ctx, fileName)
109 ctx.Build(pctx, android.BuildParams{
110 Rule: aconfigCreateStorage,
111 Input: aconfigFlagsPb,
112 Output: outPath,
113 Args: map[string]string{
114 "container": container,
115 "fileType": fileType,
116 "version": strconv.Itoa(storageFilesVersion),
117 },
118 })
119 builder.Command().
120 Text("cp").Input(outPath).Text(installPath.String())
121 *fullInstallPaths = append(*fullInstallPaths, FullInstallPathInfo{
122 SourcePath: outPath,
123 FullInstallPath: fullInstallPath.Join(ctx, "etc/aconfig", fileName),
124 })
125 f.appendToEntry(ctx, installPath)
126 }
127
128 if ctx.Config().ReleaseCreateAconfigStorageFile() {
129 generatePartitionAconfigStorageFile("package_map", "package.map")
130 generatePartitionAconfigStorageFile("flag_map", "flag.map")
131 generatePartitionAconfigStorageFile("flag_val", "flag.val")
132 generatePartitionAconfigStorageFile("flag_info", "flag.info")
133 }
Justin Yun16209832024-05-14 14:51:03 +0900134 }
Spandan Das3d9b69e2024-10-07 19:03:45 +0000135
Justin Yund5df5182025-02-11 18:28:53 +0900136 buildAconfigFlagsFiles(partition, dir, fullInstallPath)
137 for _, subPartition := range android.SortedKeys(subPartitionsFound) {
138 if subPartitionsFound[subPartition] {
139 buildAconfigFlagsFiles(subPartition, dir.Join(ctx, subPartition), fullInstallPath.Join(ctx, subPartition))
140 }
Spandan Das3d9b69e2024-10-07 19:03:45 +0000141 }
Justin Yun74f3f302024-05-07 14:32:14 +0900142}