blob: 0278048cfb20584e54c2994a7b6058daf0c5c0b1 [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 (
18 "android/soong/android"
19 "fmt"
20 "github.com/google/blueprint"
21 "strings"
22)
23
Joe Onorato981c9262023-06-21 15:16:23 -070024type DeclarationsModule struct {
Joe Onoratofee845a2023-05-09 08:14:14 -070025 android.ModuleBase
26 android.DefaultableModuleBase
27
Joe Onorato981c9262023-06-21 15:16:23 -070028 // Properties for "aconfig_declarations"
Joe Onoratofee845a2023-05-09 08:14:14 -070029 properties struct {
30 // aconfig files, relative to this Android.bp file
31 Srcs []string `android:"path"`
32
Joe Onorato81b25ed2023-06-21 13:49:37 -070033 // Release config flag package
34 Package string
Joe Onoratofee845a2023-05-09 08:14:14 -070035
Joe Onorato981c9262023-06-21 15:16:23 -070036 // Values from TARGET_RELEASE / RELEASE_ACONFIG_VALUE_SETS
Joe Onoratofee845a2023-05-09 08:14:14 -070037 Values []string `blueprint:"mutated"`
38 }
39
40 intermediatePath android.WritablePath
Joe Onoratofee845a2023-05-09 08:14:14 -070041}
42
Joe Onorato981c9262023-06-21 15:16:23 -070043func DeclarationsFactory() android.Module {
44 module := &DeclarationsModule{}
Joe Onoratofee845a2023-05-09 08:14:14 -070045
46 android.InitAndroidModule(module)
47 android.InitDefaultableModule(module)
48 module.AddProperties(&module.properties)
49 // TODO: bp2build
50 //android.InitBazelModule(module)
51
52 return module
53}
54
55type implicitValuesTagType struct {
56 blueprint.BaseDependencyTag
57}
58
59var implicitValuesTag = implicitValuesTagType{}
60
Joe Onorato981c9262023-06-21 15:16:23 -070061func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Joe Onoratofee845a2023-05-09 08:14:14 -070062 // Validate Properties
63 if len(module.properties.Srcs) == 0 {
64 ctx.PropertyErrorf("srcs", "missing source files")
65 return
66 }
Joe Onorato81b25ed2023-06-21 13:49:37 -070067 if len(module.properties.Package) == 0 {
68 ctx.PropertyErrorf("package", "missing package property")
Joe Onoratofee845a2023-05-09 08:14:14 -070069 }
70
Joe Onorato981c9262023-06-21 15:16:23 -070071 // Add a dependency on the aconfig_value_sets defined in
72 // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
Joe Onorato81b25ed2023-06-21 13:49:37 -070073 // match our package.
Joe Onorato981c9262023-06-21 15:16:23 -070074 valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
Joe Onoratofee845a2023-05-09 08:14:14 -070075 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
76}
77
Joe Onorato981c9262023-06-21 15:16:23 -070078func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) {
Joe Onoratofee845a2023-05-09 08:14:14 -070079 switch tag {
Joe Onoratofee845a2023-05-09 08:14:14 -070080 case "":
81 // The default output of this module is the intermediates format, which is
82 // not installable and in a private format that no other rules can handle
83 // correctly.
84 return []android.Path{module.intermediatePath}, nil
85 default:
Joe Onorato981c9262023-06-21 15:16:23 -070086 return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag)
Joe Onoratofee845a2023-05-09 08:14:14 -070087 }
88}
89
90func joinAndPrefix(prefix string, values []string) string {
91 var sb strings.Builder
92 for _, v := range values {
93 sb.WriteString(prefix)
94 sb.WriteString(v)
95 }
96 return sb.String()
97}
98
Zhi Dou3f65a412023-08-10 21:47:40 +000099func optionalVariable(prefix string, value string) string {
100 var sb strings.Builder
101 if value != "" {
102 sb.WriteString(prefix)
103 sb.WriteString(value)
104 }
105 return sb.String()
106}
107
Joe Onorato981c9262023-06-21 15:16:23 -0700108// Provider published by aconfig_value_set
109type declarationsProviderData struct {
Joe Onorato81b25ed2023-06-21 13:49:37 -0700110 Package string
111 IntermediatePath android.WritablePath
Joe Onorato175073c2023-06-01 14:42:59 -0700112}
113
Joe Onorato981c9262023-06-21 15:16:23 -0700114var declarationsProviderKey = blueprint.NewProvider(declarationsProviderData{})
Joe Onorato175073c2023-06-01 14:42:59 -0700115
Joe Onorato981c9262023-06-21 15:16:23 -0700116func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
117 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
Joe Onoratofee845a2023-05-09 08:14:14 -0700118 ctx.VisitDirectDeps(func(dep android.Module) {
119 if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) {
120 // Other modules get injected as dependencies too, for example the license modules
121 return
122 }
123 depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData)
Joe Onorato81b25ed2023-06-21 13:49:37 -0700124 valuesFiles, ok := depData.AvailablePackages[module.properties.Package]
Joe Onoratofee845a2023-05-09 08:14:14 -0700125 if ok {
126 for _, path := range valuesFiles {
127 module.properties.Values = append(module.properties.Values, path.String())
128 }
129 }
130 })
131
132 // Intermediate format
133 inputFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
MÃ¥rten Kongstadc89e9242023-06-21 08:32:53 +0200134 intermediatePath := android.PathForModuleOut(ctx, "intermediate.pb")
Zhi Dou3f65a412023-08-10 21:47:40 +0000135 defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission()
Joe Onoratofee845a2023-05-09 08:14:14 -0700136 ctx.Build(pctx, android.BuildParams{
137 Rule: aconfigRule,
Joe Onorato175073c2023-06-01 14:42:59 -0700138 Output: intermediatePath,
Joe Onorato981c9262023-06-21 15:16:23 -0700139 Description: "aconfig_declarations",
Joe Onoratofee845a2023-05-09 08:14:14 -0700140 Args: map[string]string{
Zhi Dou3f65a412023-08-10 21:47:40 +0000141 "release_version": ctx.Config().ReleaseVersion(),
142 "package": module.properties.Package,
143 "declarations": android.JoinPathsWithPrefix(inputFiles, "--declarations "),
144 "values": joinAndPrefix(" --values ", module.properties.Values),
145 "default-permission": optionalVariable(" --default-permission ", defaultPermission),
Joe Onoratofee845a2023-05-09 08:14:14 -0700146 },
147 })
148
Joe Onorato981c9262023-06-21 15:16:23 -0700149 ctx.SetProvider(declarationsProviderKey, declarationsProviderData{
Joe Onorato81b25ed2023-06-21 13:49:37 -0700150 Package: module.properties.Package,
151 IntermediatePath: intermediatePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700152 })
153
Joe Onoratofee845a2023-05-09 08:14:14 -0700154}