blob: 71a64dddc21e1e1352b62ff323b79300ab5748a3 [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 Liu315a53c2024-04-24 16:41:57 +000076 if len(module.properties.Container) == 0 {
77 ctx.PropertyErrorf("container", "missing container property")
78 }
Joe Onoratofee845a2023-05-09 08:14:14 -070079
Joe Onorato981c9262023-06-21 15:16:23 -070080 // Add a dependency on the aconfig_value_sets defined in
81 // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
Joe Onorato81b25ed2023-06-21 13:49:37 -070082 // match our package.
Joe Onorato981c9262023-06-21 15:16:23 -070083 valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
Yu Liueebb2592023-10-12 20:31:27 -070084 if len(valuesFromConfig) > 0 {
85 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
Yu Liu2cc802a2023-09-05 17:19:45 -070086 }
Joe Onoratofee845a2023-05-09 08:14:14 -070087}
88
Joe Onorato981c9262023-06-21 15:16:23 -070089func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) {
Joe Onoratofee845a2023-05-09 08:14:14 -070090 switch tag {
Joe Onoratofee845a2023-05-09 08:14:14 -070091 case "":
92 // The default output of this module is the intermediates format, which is
93 // not installable and in a private format that no other rules can handle
94 // correctly.
95 return []android.Path{module.intermediatePath}, nil
96 default:
Joe Onorato981c9262023-06-21 15:16:23 -070097 return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag)
Joe Onoratofee845a2023-05-09 08:14:14 -070098 }
99}
100
101func joinAndPrefix(prefix string, values []string) string {
102 var sb strings.Builder
103 for _, v := range values {
104 sb.WriteString(prefix)
105 sb.WriteString(v)
106 }
107 return sb.String()
108}
109
Zhi Dou3f65a412023-08-10 21:47:40 +0000110func optionalVariable(prefix string, value string) string {
111 var sb strings.Builder
112 if value != "" {
113 sb.WriteString(prefix)
114 sb.WriteString(value)
115 }
116 return sb.String()
117}
118
Joe Onorato981c9262023-06-21 15:16:23 -0700119func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
120 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
Joe Onorato4551ea12023-08-19 19:02:15 -0700121 valuesFiles := make([]android.Path, 0)
Joe Onoratofee845a2023-05-09 08:14:14 -0700122 ctx.VisitDirectDeps(func(dep android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800123 if depData, ok := android.OtherModuleProvider(ctx, dep, valueSetProviderKey); ok {
124 paths, ok := depData.AvailablePackages[module.properties.Package]
125 if ok {
126 valuesFiles = append(valuesFiles, paths...)
127 for _, path := range paths {
128 module.properties.Values = append(module.properties.Values, path.String())
129 }
Joe Onoratofee845a2023-05-09 08:14:14 -0700130 }
131 }
132 })
133
134 // Intermediate format
Joe Onorato4551ea12023-08-19 19:02:15 -0700135 declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000136 intermediateCacheFilePath := android.PathForModuleOut(ctx, "intermediate.pb")
Zhi Dou3f65a412023-08-10 21:47:40 +0000137 defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission()
Joe Onorato4551ea12023-08-19 19:02:15 -0700138 inputFiles := make([]android.Path, len(declarationFiles))
139 copy(inputFiles, declarationFiles)
140 inputFiles = append(inputFiles, valuesFiles...)
Yu Liueeff2222024-03-19 23:07:34 +0000141 args := map[string]string{
142 "release_version": ctx.Config().ReleaseVersion(),
143 "package": module.properties.Package,
144 "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "),
145 "values": joinAndPrefix(" --values ", module.properties.Values),
146 "default-permission": optionalVariable(" --default-permission ", defaultPermission),
147 }
148 if len(module.properties.Container) > 0 {
149 args["container"] = "--container " + module.properties.Container
150 }
Joe Onoratofee845a2023-05-09 08:14:14 -0700151 ctx.Build(pctx, android.BuildParams{
152 Rule: aconfigRule,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000153 Output: intermediateCacheFilePath,
Joe Onorato4551ea12023-08-19 19:02:15 -0700154 Inputs: inputFiles,
Joe Onorato981c9262023-06-21 15:16:23 -0700155 Description: "aconfig_declarations",
Yu Liueeff2222024-03-19 23:07:34 +0000156 Args: args,
Joe Onoratofee845a2023-05-09 08:14:14 -0700157 })
158
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000159 intermediateDumpFilePath := android.PathForModuleOut(ctx, "intermediate.txt")
160 ctx.Build(pctx, android.BuildParams{
161 Rule: aconfigTextRule,
162 Output: intermediateDumpFilePath,
163 Inputs: android.Paths{intermediateCacheFilePath},
164 Description: "aconfig_text",
165 })
166
LaMont Jonesaa005ae2023-12-19 19:01:57 +0000167 android.SetProvider(ctx, android.AconfigDeclarationsProviderKey, android.AconfigDeclarationsProviderData{
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000168 Package: module.properties.Package,
169 Container: module.properties.Container,
Zi Wang0e5d16c2024-02-08 06:19:34 +0000170 Exportable: module.properties.Exportable,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000171 IntermediateCacheOutputPath: intermediateCacheFilePath,
172 IntermediateDumpOutputPath: intermediateDumpFilePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700173 })
174
Joe Onoratofee845a2023-05-09 08:14:14 -0700175}