blob: 897f8925a9e8adda07656b56449c120176397725 [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"
22 "android/soong/bazel"
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
Yu Liu2cc802a2023-09-05 17:19:45 -070029 android.BazelModuleBase
Joe Onoratofee845a2023-05-09 08:14:14 -070030
Joe Onorato981c9262023-06-21 15:16:23 -070031 // Properties for "aconfig_declarations"
Joe Onoratofee845a2023-05-09 08:14:14 -070032 properties struct {
33 // aconfig files, relative to this Android.bp file
34 Srcs []string `android:"path"`
35
Joe Onorato81b25ed2023-06-21 13:49:37 -070036 // Release config flag package
37 Package string
Joe Onoratofee845a2023-05-09 08:14:14 -070038
Joe Onorato981c9262023-06-21 15:16:23 -070039 // Values from TARGET_RELEASE / RELEASE_ACONFIG_VALUE_SETS
Joe Onoratofee845a2023-05-09 08:14:14 -070040 Values []string `blueprint:"mutated"`
41 }
42
43 intermediatePath android.WritablePath
Joe Onoratofee845a2023-05-09 08:14:14 -070044}
45
Joe Onorato981c9262023-06-21 15:16:23 -070046func DeclarationsFactory() android.Module {
47 module := &DeclarationsModule{}
Joe Onoratofee845a2023-05-09 08:14:14 -070048
49 android.InitAndroidModule(module)
50 android.InitDefaultableModule(module)
51 module.AddProperties(&module.properties)
Yu Liu2cc802a2023-09-05 17:19:45 -070052 android.InitBazelModule(module)
Joe Onoratofee845a2023-05-09 08:14:14 -070053
54 return module
55}
56
57type implicitValuesTagType struct {
58 blueprint.BaseDependencyTag
59}
60
61var implicitValuesTag = implicitValuesTagType{}
62
Joe Onorato981c9262023-06-21 15:16:23 -070063func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Joe Onoratofee845a2023-05-09 08:14:14 -070064 // Validate Properties
65 if len(module.properties.Srcs) == 0 {
66 ctx.PropertyErrorf("srcs", "missing source files")
67 return
68 }
Joe Onorato81b25ed2023-06-21 13:49:37 -070069 if len(module.properties.Package) == 0 {
70 ctx.PropertyErrorf("package", "missing package property")
Joe Onoratofee845a2023-05-09 08:14:14 -070071 }
72
Joe Onorato981c9262023-06-21 15:16:23 -070073 // Add a dependency on the aconfig_value_sets defined in
74 // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
Joe Onorato81b25ed2023-06-21 13:49:37 -070075 // match our package.
Joe Onorato981c9262023-06-21 15:16:23 -070076 valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
Yu Liueebb2592023-10-12 20:31:27 -070077 if len(valuesFromConfig) > 0 {
78 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
Yu Liu2cc802a2023-09-05 17:19:45 -070079 }
Joe Onoratofee845a2023-05-09 08:14:14 -070080}
81
Joe Onorato981c9262023-06-21 15:16:23 -070082func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) {
Joe Onoratofee845a2023-05-09 08:14:14 -070083 switch tag {
Joe Onoratofee845a2023-05-09 08:14:14 -070084 case "":
85 // The default output of this module is the intermediates format, which is
86 // not installable and in a private format that no other rules can handle
87 // correctly.
88 return []android.Path{module.intermediatePath}, nil
89 default:
Joe Onorato981c9262023-06-21 15:16:23 -070090 return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag)
Joe Onoratofee845a2023-05-09 08:14:14 -070091 }
92}
93
94func joinAndPrefix(prefix string, values []string) string {
95 var sb strings.Builder
96 for _, v := range values {
97 sb.WriteString(prefix)
98 sb.WriteString(v)
99 }
100 return sb.String()
101}
102
Zhi Dou3f65a412023-08-10 21:47:40 +0000103func optionalVariable(prefix string, value string) string {
104 var sb strings.Builder
105 if value != "" {
106 sb.WriteString(prefix)
107 sb.WriteString(value)
108 }
109 return sb.String()
110}
111
Joe Onorato981c9262023-06-21 15:16:23 -0700112// Provider published by aconfig_value_set
113type declarationsProviderData struct {
Joe Onorato81b25ed2023-06-21 13:49:37 -0700114 Package string
115 IntermediatePath android.WritablePath
Joe Onorato175073c2023-06-01 14:42:59 -0700116}
117
Joe Onorato981c9262023-06-21 15:16:23 -0700118var declarationsProviderKey = blueprint.NewProvider(declarationsProviderData{})
Joe Onorato175073c2023-06-01 14:42:59 -0700119
Joe Onorato981c9262023-06-21 15:16:23 -0700120func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
121 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
Joe Onorato4551ea12023-08-19 19:02:15 -0700122 valuesFiles := make([]android.Path, 0)
Joe Onoratofee845a2023-05-09 08:14:14 -0700123 ctx.VisitDirectDeps(func(dep android.Module) {
124 if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) {
125 // Other modules get injected as dependencies too, for example the license modules
126 return
127 }
128 depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData)
Joe Onorato4551ea12023-08-19 19:02:15 -0700129 paths, ok := depData.AvailablePackages[module.properties.Package]
Joe Onoratofee845a2023-05-09 08:14:14 -0700130 if ok {
Joe Onorato4551ea12023-08-19 19:02:15 -0700131 valuesFiles = append(valuesFiles, paths...)
132 for _, path := range paths {
Joe Onoratofee845a2023-05-09 08:14:14 -0700133 module.properties.Values = append(module.properties.Values, path.String())
134 }
135 }
136 })
137
138 // Intermediate format
Joe Onorato4551ea12023-08-19 19:02:15 -0700139 declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
MÃ¥rten Kongstadc89e9242023-06-21 08:32:53 +0200140 intermediatePath := android.PathForModuleOut(ctx, "intermediate.pb")
Zhi Dou3f65a412023-08-10 21:47:40 +0000141 defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission()
Joe Onorato4551ea12023-08-19 19:02:15 -0700142 inputFiles := make([]android.Path, len(declarationFiles))
143 copy(inputFiles, declarationFiles)
144 inputFiles = append(inputFiles, valuesFiles...)
Joe Onoratofee845a2023-05-09 08:14:14 -0700145 ctx.Build(pctx, android.BuildParams{
146 Rule: aconfigRule,
Joe Onorato175073c2023-06-01 14:42:59 -0700147 Output: intermediatePath,
Joe Onorato4551ea12023-08-19 19:02:15 -0700148 Inputs: inputFiles,
Joe Onorato981c9262023-06-21 15:16:23 -0700149 Description: "aconfig_declarations",
Joe Onoratofee845a2023-05-09 08:14:14 -0700150 Args: map[string]string{
Zhi Dou3f65a412023-08-10 21:47:40 +0000151 "release_version": ctx.Config().ReleaseVersion(),
152 "package": module.properties.Package,
Joe Onorato4551ea12023-08-19 19:02:15 -0700153 "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "),
Zhi Dou3f65a412023-08-10 21:47:40 +0000154 "values": joinAndPrefix(" --values ", module.properties.Values),
155 "default-permission": optionalVariable(" --default-permission ", defaultPermission),
Joe Onoratofee845a2023-05-09 08:14:14 -0700156 },
157 })
158
Joe Onorato981c9262023-06-21 15:16:23 -0700159 ctx.SetProvider(declarationsProviderKey, declarationsProviderData{
Joe Onorato81b25ed2023-06-21 13:49:37 -0700160 Package: module.properties.Package,
161 IntermediatePath: intermediatePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700162 })
163
Joe Onoratofee845a2023-05-09 08:14:14 -0700164}
Yu Liu2cc802a2023-09-05 17:19:45 -0700165
166type bazelAconfigDeclarationsAttributes struct {
167 Srcs bazel.LabelListAttribute
168 Package string
169}
170
Chris Parsons637458d2023-09-19 20:09:00 +0000171func (module *DeclarationsModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Yu Liu2cc802a2023-09-05 17:19:45 -0700172 if ctx.ModuleType() != "aconfig_declarations" {
173 return
174 }
175 srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, module.properties.Srcs))
176
177 attrs := bazelAconfigDeclarationsAttributes{
178 Srcs: srcs,
179 Package: module.properties.Package,
180 }
181 props := bazel.BazelTargetModuleProperties{
182 Rule_class: "aconfig_declarations",
183 Bzl_load_location: "//build/bazel/rules/aconfig:aconfig_declarations.bzl",
184 }
185
186 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs)
187}