blob: fa4c3ad4221e178b8d4ee60c4e42ce8d058bd276 [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 "strings"
Vinh Tran457ddef2023-08-02 13:50:26 -040019
Yu Liu2cc802a2023-09-05 17:19:45 -070020 "android/soong/android"
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000021
Vinh Tran457ddef2023-08-02 13:50:26 -040022 "github.com/google/blueprint"
Joe Onoratofee845a2023-05-09 08:14:14 -070023)
24
Joe Onorato981c9262023-06-21 15:16:23 -070025type DeclarationsModule struct {
Joe Onoratofee845a2023-05-09 08:14:14 -070026 android.ModuleBase
27 android.DefaultableModuleBase
Yu Liufa297642024-06-11 00:13:02 +000028 blueprint.IncrementalModule
Joe Onoratofee845a2023-05-09 08:14:14 -070029
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 }
Joe Onoratofee845a2023-05-09 08:14:14 -070047}
48
Joe Onorato981c9262023-06-21 15:16:23 -070049func DeclarationsFactory() android.Module {
50 module := &DeclarationsModule{}
Joe Onoratofee845a2023-05-09 08:14:14 -070051
52 android.InitAndroidModule(module)
53 android.InitDefaultableModule(module)
54 module.AddProperties(&module.properties)
Joe Onoratofee845a2023-05-09 08:14:14 -070055
56 return module
57}
58
59type implicitValuesTagType struct {
60 blueprint.BaseDependencyTag
61}
62
63var implicitValuesTag = implicitValuesTagType{}
64
Joe Onorato981c9262023-06-21 15:16:23 -070065func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Joe Onoratofee845a2023-05-09 08:14:14 -070066 // Validate Properties
67 if len(module.properties.Srcs) == 0 {
68 ctx.PropertyErrorf("srcs", "missing source files")
69 return
70 }
Joe Onorato81b25ed2023-06-21 13:49:37 -070071 if len(module.properties.Package) == 0 {
72 ctx.PropertyErrorf("package", "missing package property")
Joe Onoratofee845a2023-05-09 08:14:14 -070073 }
Yu Liu315a53c2024-04-24 16:41:57 +000074 if len(module.properties.Container) == 0 {
75 ctx.PropertyErrorf("container", "missing container property")
76 }
Joe Onoratofee845a2023-05-09 08:14:14 -070077
Joe Onorato981c9262023-06-21 15:16:23 -070078 // Add a dependency on the aconfig_value_sets defined in
79 // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
Joe Onorato81b25ed2023-06-21 13:49:37 -070080 // match our package.
Joe Onorato981c9262023-06-21 15:16:23 -070081 valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
Yu Liueebb2592023-10-12 20:31:27 -070082 if len(valuesFromConfig) > 0 {
83 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
Yu Liu2cc802a2023-09-05 17:19:45 -070084 }
Joe Onoratofee845a2023-05-09 08:14:14 -070085}
86
Joe Onoratofee845a2023-05-09 08:14:14 -070087func joinAndPrefix(prefix string, values []string) string {
88 var sb strings.Builder
89 for _, v := range values {
90 sb.WriteString(prefix)
91 sb.WriteString(v)
92 }
93 return sb.String()
94}
95
Zhi Dou3f65a412023-08-10 21:47:40 +000096func optionalVariable(prefix string, value string) string {
97 var sb strings.Builder
98 if value != "" {
99 sb.WriteString(prefix)
100 sb.WriteString(value)
101 }
102 return sb.String()
103}
104
Joe Onorato981c9262023-06-21 15:16:23 -0700105func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
106 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
Joe Onorato4551ea12023-08-19 19:02:15 -0700107 valuesFiles := make([]android.Path, 0)
Joe Onoratofee845a2023-05-09 08:14:14 -0700108 ctx.VisitDirectDeps(func(dep android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800109 if depData, ok := android.OtherModuleProvider(ctx, dep, valueSetProviderKey); ok {
110 paths, ok := depData.AvailablePackages[module.properties.Package]
111 if ok {
112 valuesFiles = append(valuesFiles, paths...)
113 for _, path := range paths {
114 module.properties.Values = append(module.properties.Values, path.String())
115 }
Joe Onoratofee845a2023-05-09 08:14:14 -0700116 }
117 }
118 })
119
120 // Intermediate format
Joe Onorato4551ea12023-08-19 19:02:15 -0700121 declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000122 intermediateCacheFilePath := android.PathForModuleOut(ctx, "intermediate.pb")
Zhi Dou3f65a412023-08-10 21:47:40 +0000123 defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission()
Joe Onorato4551ea12023-08-19 19:02:15 -0700124 inputFiles := make([]android.Path, len(declarationFiles))
125 copy(inputFiles, declarationFiles)
126 inputFiles = append(inputFiles, valuesFiles...)
Yu Liueeff2222024-03-19 23:07:34 +0000127 args := map[string]string{
128 "release_version": ctx.Config().ReleaseVersion(),
129 "package": module.properties.Package,
130 "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "),
131 "values": joinAndPrefix(" --values ", module.properties.Values),
132 "default-permission": optionalVariable(" --default-permission ", defaultPermission),
133 }
134 if len(module.properties.Container) > 0 {
135 args["container"] = "--container " + module.properties.Container
136 }
Joe Onoratofee845a2023-05-09 08:14:14 -0700137 ctx.Build(pctx, android.BuildParams{
138 Rule: aconfigRule,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000139 Output: intermediateCacheFilePath,
Joe Onorato4551ea12023-08-19 19:02:15 -0700140 Inputs: inputFiles,
Joe Onorato981c9262023-06-21 15:16:23 -0700141 Description: "aconfig_declarations",
Yu Liueeff2222024-03-19 23:07:34 +0000142 Args: args,
Joe Onoratofee845a2023-05-09 08:14:14 -0700143 })
144
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000145 intermediateDumpFilePath := android.PathForModuleOut(ctx, "intermediate.txt")
146 ctx.Build(pctx, android.BuildParams{
147 Rule: aconfigTextRule,
148 Output: intermediateDumpFilePath,
149 Inputs: android.Paths{intermediateCacheFilePath},
150 Description: "aconfig_text",
151 })
152
LaMont Jonesaa005ae2023-12-19 19:01:57 +0000153 android.SetProvider(ctx, android.AconfigDeclarationsProviderKey, android.AconfigDeclarationsProviderData{
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000154 Package: module.properties.Package,
155 Container: module.properties.Container,
Zi Wang0e5d16c2024-02-08 06:19:34 +0000156 Exportable: module.properties.Exportable,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000157 IntermediateCacheOutputPath: intermediateCacheFilePath,
158 IntermediateDumpOutputPath: intermediateDumpFilePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700159 })
Joe Onoratofee845a2023-05-09 08:14:14 -0700160}
Yu Liufa297642024-06-11 00:13:02 +0000161
162func (module *DeclarationsModule) BuildActionProviderKeys() []blueprint.AnyProviderKey {
163 return []blueprint.AnyProviderKey{android.AconfigDeclarationsProviderKey}
164}
165
166func (module *DeclarationsModule) PackageContextPath() string {
167 return pkgPath
168}
169
170func (module *DeclarationsModule) CachedRules() []blueprint.Rule {
171 return []blueprint.Rule{aconfigRule, aconfigTextRule}
172}
173
174var _ blueprint.Incremental = &DeclarationsModule{}