blob: 5cdf5b60547db374f14c984a5a7fb7097fef89f1 [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"
Joe Onoratofee845a2023-05-09 08:14:14 -070020 "strings"
Vinh Tran457ddef2023-08-02 13:50:26 -040021
22 "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
28
Joe Onorato981c9262023-06-21 15:16:23 -070029 // Properties for "aconfig_declarations"
Joe Onoratofee845a2023-05-09 08:14:14 -070030 properties struct {
31 // aconfig files, relative to this Android.bp file
32 Srcs []string `android:"path"`
33
Joe Onorato81b25ed2023-06-21 13:49:37 -070034 // Release config flag package
35 Package string
Joe Onoratofee845a2023-05-09 08:14:14 -070036
Joe Onorato981c9262023-06-21 15:16:23 -070037 // Values from TARGET_RELEASE / RELEASE_ACONFIG_VALUE_SETS
Joe Onoratofee845a2023-05-09 08:14:14 -070038 Values []string `blueprint:"mutated"`
39 }
40
41 intermediatePath android.WritablePath
Joe Onoratofee845a2023-05-09 08:14:14 -070042}
43
Joe Onorato981c9262023-06-21 15:16:23 -070044func DeclarationsFactory() android.Module {
45 module := &DeclarationsModule{}
Joe Onoratofee845a2023-05-09 08:14:14 -070046
47 android.InitAndroidModule(module)
48 android.InitDefaultableModule(module)
49 module.AddProperties(&module.properties)
50 // TODO: bp2build
51 //android.InitBazelModule(module)
52
53 return module
54}
55
56type implicitValuesTagType struct {
57 blueprint.BaseDependencyTag
58}
59
60var implicitValuesTag = implicitValuesTagType{}
61
Joe Onorato981c9262023-06-21 15:16:23 -070062func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Joe Onoratofee845a2023-05-09 08:14:14 -070063 // Validate Properties
64 if len(module.properties.Srcs) == 0 {
65 ctx.PropertyErrorf("srcs", "missing source files")
66 return
67 }
Joe Onorato81b25ed2023-06-21 13:49:37 -070068 if len(module.properties.Package) == 0 {
69 ctx.PropertyErrorf("package", "missing package property")
Joe Onoratofee845a2023-05-09 08:14:14 -070070 }
71
Joe Onorato981c9262023-06-21 15:16:23 -070072 // Add a dependency on the aconfig_value_sets defined in
73 // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
Joe Onorato81b25ed2023-06-21 13:49:37 -070074 // match our package.
Joe Onorato981c9262023-06-21 15:16:23 -070075 valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
Joe Onoratofee845a2023-05-09 08:14:14 -070076 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
77}
78
Joe Onorato981c9262023-06-21 15:16:23 -070079func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) {
Joe Onoratofee845a2023-05-09 08:14:14 -070080 switch tag {
Joe Onoratofee845a2023-05-09 08:14:14 -070081 case "":
82 // The default output of this module is the intermediates format, which is
83 // not installable and in a private format that no other rules can handle
84 // correctly.
85 return []android.Path{module.intermediatePath}, nil
86 default:
Joe Onorato981c9262023-06-21 15:16:23 -070087 return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag)
Joe Onoratofee845a2023-05-09 08:14:14 -070088 }
89}
90
91func joinAndPrefix(prefix string, values []string) string {
92 var sb strings.Builder
93 for _, v := range values {
94 sb.WriteString(prefix)
95 sb.WriteString(v)
96 }
97 return sb.String()
98}
99
Zhi Dou3f65a412023-08-10 21:47:40 +0000100func optionalVariable(prefix string, value string) string {
101 var sb strings.Builder
102 if value != "" {
103 sb.WriteString(prefix)
104 sb.WriteString(value)
105 }
106 return sb.String()
107}
108
Joe Onorato981c9262023-06-21 15:16:23 -0700109// Provider published by aconfig_value_set
110type declarationsProviderData struct {
Joe Onorato81b25ed2023-06-21 13:49:37 -0700111 Package string
112 IntermediatePath android.WritablePath
Joe Onorato175073c2023-06-01 14:42:59 -0700113}
114
Joe Onorato981c9262023-06-21 15:16:23 -0700115var declarationsProviderKey = blueprint.NewProvider(declarationsProviderData{})
Joe Onorato175073c2023-06-01 14:42:59 -0700116
Joe Onorato981c9262023-06-21 15:16:23 -0700117func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
118 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
Joe Onorato4551ea12023-08-19 19:02:15 -0700119 valuesFiles := make([]android.Path, 0)
Joe Onoratofee845a2023-05-09 08:14:14 -0700120 ctx.VisitDirectDeps(func(dep android.Module) {
121 if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) {
122 // Other modules get injected as dependencies too, for example the license modules
123 return
124 }
125 depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData)
Joe Onorato4551ea12023-08-19 19:02:15 -0700126 paths, ok := depData.AvailablePackages[module.properties.Package]
Joe Onoratofee845a2023-05-09 08:14:14 -0700127 if ok {
Joe Onorato4551ea12023-08-19 19:02:15 -0700128 valuesFiles = append(valuesFiles, paths...)
129 for _, path := range paths {
Joe Onoratofee845a2023-05-09 08:14:14 -0700130 module.properties.Values = append(module.properties.Values, path.String())
131 }
132 }
133 })
134
135 // Intermediate format
Joe Onorato4551ea12023-08-19 19:02:15 -0700136 declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
MÃ¥rten Kongstadc89e9242023-06-21 08:32:53 +0200137 intermediatePath := android.PathForModuleOut(ctx, "intermediate.pb")
Zhi Dou3f65a412023-08-10 21:47:40 +0000138 defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission()
Joe Onorato4551ea12023-08-19 19:02:15 -0700139 inputFiles := make([]android.Path, len(declarationFiles))
140 copy(inputFiles, declarationFiles)
141 inputFiles = append(inputFiles, valuesFiles...)
Joe Onoratofee845a2023-05-09 08:14:14 -0700142 ctx.Build(pctx, android.BuildParams{
143 Rule: aconfigRule,
Joe Onorato175073c2023-06-01 14:42:59 -0700144 Output: intermediatePath,
Joe Onorato4551ea12023-08-19 19:02:15 -0700145 Inputs: inputFiles,
Joe Onorato981c9262023-06-21 15:16:23 -0700146 Description: "aconfig_declarations",
Joe Onoratofee845a2023-05-09 08:14:14 -0700147 Args: map[string]string{
Zhi Dou3f65a412023-08-10 21:47:40 +0000148 "release_version": ctx.Config().ReleaseVersion(),
149 "package": module.properties.Package,
Joe Onorato4551ea12023-08-19 19:02:15 -0700150 "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "),
Zhi Dou3f65a412023-08-10 21:47:40 +0000151 "values": joinAndPrefix(" --values ", module.properties.Values),
152 "default-permission": optionalVariable(" --default-permission ", defaultPermission),
Joe Onoratofee845a2023-05-09 08:14:14 -0700153 },
154 })
155
Joe Onorato981c9262023-06-21 15:16:23 -0700156 ctx.SetProvider(declarationsProviderKey, declarationsProviderData{
Joe Onorato81b25ed2023-06-21 13:49:37 -0700157 Package: module.properties.Package,
158 IntermediatePath: intermediatePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700159 })
160
Joe Onoratofee845a2023-05-09 08:14:14 -0700161}