blob: d29e3122d4e172d4282535e1386f26e80c1b07fc [file] [log] [blame]
Joe Onoratofee845a2023-05-09 08:14:14 -07001// Copyright 2023 Google Inc. All rights reserved.
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
Joe Onorato981c9262023-06-21 15:16:23 -070015package aconfig
Joe Onoratofee845a2023-05-09 08:14:14 -070016
17import (
Joe Onoratofee845a2023-05-09 08:14:14 -070018 "fmt"
Joe Onoratofee845a2023-05-09 08:14:14 -070019 "strings"
Vinh Tran457ddef2023-08-02 13:50:26 -040020
Yu Liu2cc802a2023-09-05 17:19:45 -070021 "android/soong/android"
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000022
Vinh Tran457ddef2023-08-02 13:50:26 -040023 "github.com/google/blueprint"
Joe Onoratofee845a2023-05-09 08:14:14 -070024)
25
Joe Onorato981c9262023-06-21 15:16:23 -070026type DeclarationsModule struct {
Joe Onoratofee845a2023-05-09 08:14:14 -070027 android.ModuleBase
28 android.DefaultableModuleBase
29
Joe Onorato981c9262023-06-21 15:16:23 -070030 // Properties for "aconfig_declarations"
Joe Onoratofee845a2023-05-09 08:14:14 -070031 properties struct {
32 // aconfig files, relative to this Android.bp file
33 Srcs []string `android:"path"`
34
Joe Onorato81b25ed2023-06-21 13:49:37 -070035 // Release config flag package
36 Package string
Joe Onoratofee845a2023-05-09 08:14:14 -070037
Joe Onorato981c9262023-06-21 15:16:23 -070038 // Values from TARGET_RELEASE / RELEASE_ACONFIG_VALUE_SETS
Joe Onoratofee845a2023-05-09 08:14:14 -070039 Values []string `blueprint:"mutated"`
Yu Liueae7b362023-11-16 17:05:47 -080040
41 // Container(system/vendor/apex) that this module belongs to
42 Container string
Zi Wang0e5d16c2024-02-08 06:19:34 +000043
44 // The flags will only be repackaged if this prop is true.
45 Exportable bool
Joe Onoratofee845a2023-05-09 08:14:14 -070046 }
47
48 intermediatePath android.WritablePath
Joe Onoratofee845a2023-05-09 08:14:14 -070049}
50
Joe Onorato981c9262023-06-21 15:16:23 -070051func DeclarationsFactory() android.Module {
52 module := &DeclarationsModule{}
Joe Onoratofee845a2023-05-09 08:14:14 -070053
54 android.InitAndroidModule(module)
55 android.InitDefaultableModule(module)
56 module.AddProperties(&module.properties)
Joe Onoratofee845a2023-05-09 08:14:14 -070057
58 return module
59}
60
61type implicitValuesTagType struct {
62 blueprint.BaseDependencyTag
63}
64
65var implicitValuesTag = implicitValuesTagType{}
66
Joe Onorato981c9262023-06-21 15:16:23 -070067func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Joe Onoratofee845a2023-05-09 08:14:14 -070068 // Validate Properties
69 if len(module.properties.Srcs) == 0 {
70 ctx.PropertyErrorf("srcs", "missing source files")
71 return
72 }
Joe Onorato81b25ed2023-06-21 13:49:37 -070073 if len(module.properties.Package) == 0 {
74 ctx.PropertyErrorf("package", "missing package property")
Joe Onoratofee845a2023-05-09 08:14:14 -070075 }
Yu Liueae7b362023-11-16 17:05:47 -080076 // TODO(b/311155208): Add mandatory check for container after all pre-existing
77 // ones are changed.
Joe Onoratofee845a2023-05-09 08:14:14 -070078
Joe Onorato981c9262023-06-21 15:16:23 -070079 // Add a dependency on the aconfig_value_sets defined in
80 // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
Joe Onorato81b25ed2023-06-21 13:49:37 -070081 // match our package.
Joe Onorato981c9262023-06-21 15:16:23 -070082 valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
Yu Liueebb2592023-10-12 20:31:27 -070083 if len(valuesFromConfig) > 0 {
84 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
Yu Liu2cc802a2023-09-05 17:19:45 -070085 }
Joe Onoratofee845a2023-05-09 08:14:14 -070086}
87
Joe Onorato981c9262023-06-21 15:16:23 -070088func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) {
Joe Onoratofee845a2023-05-09 08:14:14 -070089 switch tag {
Joe Onoratofee845a2023-05-09 08:14:14 -070090 case "":
91 // The default output of this module is the intermediates format, which is
92 // not installable and in a private format that no other rules can handle
93 // correctly.
94 return []android.Path{module.intermediatePath}, nil
95 default:
Joe Onorato981c9262023-06-21 15:16:23 -070096 return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag)
Joe Onoratofee845a2023-05-09 08:14:14 -070097 }
98}
99
100func joinAndPrefix(prefix string, values []string) string {
101 var sb strings.Builder
102 for _, v := range values {
103 sb.WriteString(prefix)
104 sb.WriteString(v)
105 }
106 return sb.String()
107}
108
Zhi Dou3f65a412023-08-10 21:47:40 +0000109func optionalVariable(prefix string, value string) string {
110 var sb strings.Builder
111 if value != "" {
112 sb.WriteString(prefix)
113 sb.WriteString(value)
114 }
115 return sb.String()
116}
117
Joe Onorato981c9262023-06-21 15:16:23 -0700118func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
119 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
Joe Onorato4551ea12023-08-19 19:02:15 -0700120 valuesFiles := make([]android.Path, 0)
Joe Onoratofee845a2023-05-09 08:14:14 -0700121 ctx.VisitDirectDeps(func(dep android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800122 if depData, ok := android.OtherModuleProvider(ctx, dep, valueSetProviderKey); ok {
123 paths, ok := depData.AvailablePackages[module.properties.Package]
124 if ok {
125 valuesFiles = append(valuesFiles, paths...)
126 for _, path := range paths {
127 module.properties.Values = append(module.properties.Values, path.String())
128 }
Joe Onoratofee845a2023-05-09 08:14:14 -0700129 }
130 }
131 })
132
133 // Intermediate format
Joe Onorato4551ea12023-08-19 19:02:15 -0700134 declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000135 intermediateCacheFilePath := android.PathForModuleOut(ctx, "intermediate.pb")
Zhi Dou3f65a412023-08-10 21:47:40 +0000136 defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission()
Joe Onorato4551ea12023-08-19 19:02:15 -0700137 inputFiles := make([]android.Path, len(declarationFiles))
138 copy(inputFiles, declarationFiles)
139 inputFiles = append(inputFiles, valuesFiles...)
Yu Liueeff2222024-03-19 23:07:34 +0000140 args := map[string]string{
141 "release_version": ctx.Config().ReleaseVersion(),
142 "package": module.properties.Package,
143 "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "),
144 "values": joinAndPrefix(" --values ", module.properties.Values),
145 "default-permission": optionalVariable(" --default-permission ", defaultPermission),
146 }
147 if len(module.properties.Container) > 0 {
148 args["container"] = "--container " + module.properties.Container
149 }
Joe Onoratofee845a2023-05-09 08:14:14 -0700150 ctx.Build(pctx, android.BuildParams{
151 Rule: aconfigRule,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000152 Output: intermediateCacheFilePath,
Joe Onorato4551ea12023-08-19 19:02:15 -0700153 Inputs: inputFiles,
Joe Onorato981c9262023-06-21 15:16:23 -0700154 Description: "aconfig_declarations",
Yu Liueeff2222024-03-19 23:07:34 +0000155 Args: args,
Joe Onoratofee845a2023-05-09 08:14:14 -0700156 })
157
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000158 intermediateDumpFilePath := android.PathForModuleOut(ctx, "intermediate.txt")
159 ctx.Build(pctx, android.BuildParams{
160 Rule: aconfigTextRule,
161 Output: intermediateDumpFilePath,
162 Inputs: android.Paths{intermediateCacheFilePath},
163 Description: "aconfig_text",
164 })
165
LaMont Jonesaa005ae2023-12-19 19:01:57 +0000166 android.SetProvider(ctx, android.AconfigDeclarationsProviderKey, android.AconfigDeclarationsProviderData{
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000167 Package: module.properties.Package,
168 Container: module.properties.Container,
Zi Wang0e5d16c2024-02-08 06:19:34 +0000169 Exportable: module.properties.Exportable,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000170 IntermediateCacheOutputPath: intermediateCacheFilePath,
171 IntermediateDumpOutputPath: intermediateDumpFilePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700172 })
173
Joe Onoratofee845a2023-05-09 08:14:14 -0700174}