blob: dac0ae36dde1ad0cc6ca5dc895680075ab5f9b25 [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
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"`
Yu Liueae7b362023-11-16 17:05:47 -080039
40 // Container(system/vendor/apex) that this module belongs to
41 Container string
Zi Wang0e5d16c2024-02-08 06:19:34 +000042
43 // The flags will only be repackaged if this prop is true.
44 Exportable bool
Joe Onoratofee845a2023-05-09 08:14:14 -070045 }
Joe Onoratofee845a2023-05-09 08:14:14 -070046}
47
Joe Onorato981c9262023-06-21 15:16:23 -070048func DeclarationsFactory() android.Module {
49 module := &DeclarationsModule{}
Joe Onoratofee845a2023-05-09 08:14:14 -070050
51 android.InitAndroidModule(module)
52 android.InitDefaultableModule(module)
53 module.AddProperties(&module.properties)
Joe Onoratofee845a2023-05-09 08:14:14 -070054
55 return module
56}
57
58type implicitValuesTagType struct {
59 blueprint.BaseDependencyTag
60}
61
62var implicitValuesTag = implicitValuesTagType{}
63
Joe Onorato981c9262023-06-21 15:16:23 -070064func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Joe Onoratofee845a2023-05-09 08:14:14 -070065 // Validate Properties
66 if len(module.properties.Srcs) == 0 {
67 ctx.PropertyErrorf("srcs", "missing source files")
68 return
69 }
Joe Onorato81b25ed2023-06-21 13:49:37 -070070 if len(module.properties.Package) == 0 {
71 ctx.PropertyErrorf("package", "missing package property")
Joe Onoratofee845a2023-05-09 08:14:14 -070072 }
Yu Liu315a53c2024-04-24 16:41:57 +000073 if len(module.properties.Container) == 0 {
74 ctx.PropertyErrorf("container", "missing container property")
75 }
Joe Onoratofee845a2023-05-09 08:14:14 -070076
Joe Onorato981c9262023-06-21 15:16:23 -070077 // Add a dependency on the aconfig_value_sets defined in
78 // RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
Joe Onorato81b25ed2023-06-21 13:49:37 -070079 // match our package.
Joe Onorato981c9262023-06-21 15:16:23 -070080 valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
Yu Liueebb2592023-10-12 20:31:27 -070081 if len(valuesFromConfig) > 0 {
82 ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
Yu Liu2cc802a2023-09-05 17:19:45 -070083 }
Joe Onoratofee845a2023-05-09 08:14:14 -070084}
85
Joe Onoratofee845a2023-05-09 08:14:14 -070086func joinAndPrefix(prefix string, values []string) string {
87 var sb strings.Builder
88 for _, v := range values {
89 sb.WriteString(prefix)
90 sb.WriteString(v)
91 }
92 return sb.String()
93}
94
Zhi Dou3f65a412023-08-10 21:47:40 +000095func optionalVariable(prefix string, value string) string {
96 var sb strings.Builder
97 if value != "" {
98 sb.WriteString(prefix)
99 sb.WriteString(value)
100 }
101 return sb.String()
102}
103
Joe Onorato981c9262023-06-21 15:16:23 -0700104func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
105 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
Joe Onorato4551ea12023-08-19 19:02:15 -0700106 valuesFiles := make([]android.Path, 0)
Joe Onoratofee845a2023-05-09 08:14:14 -0700107 ctx.VisitDirectDeps(func(dep android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800108 if depData, ok := android.OtherModuleProvider(ctx, dep, valueSetProviderKey); ok {
109 paths, ok := depData.AvailablePackages[module.properties.Package]
110 if ok {
111 valuesFiles = append(valuesFiles, paths...)
112 for _, path := range paths {
113 module.properties.Values = append(module.properties.Values, path.String())
114 }
Joe Onoratofee845a2023-05-09 08:14:14 -0700115 }
116 }
117 })
118
119 // Intermediate format
Joe Onorato4551ea12023-08-19 19:02:15 -0700120 declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000121 intermediateCacheFilePath := android.PathForModuleOut(ctx, "intermediate.pb")
Zhi Dou3f65a412023-08-10 21:47:40 +0000122 defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission()
Joe Onorato4551ea12023-08-19 19:02:15 -0700123 inputFiles := make([]android.Path, len(declarationFiles))
124 copy(inputFiles, declarationFiles)
125 inputFiles = append(inputFiles, valuesFiles...)
Yu Liueeff2222024-03-19 23:07:34 +0000126 args := map[string]string{
127 "release_version": ctx.Config().ReleaseVersion(),
128 "package": module.properties.Package,
129 "declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "),
130 "values": joinAndPrefix(" --values ", module.properties.Values),
131 "default-permission": optionalVariable(" --default-permission ", defaultPermission),
132 }
133 if len(module.properties.Container) > 0 {
134 args["container"] = "--container " + module.properties.Container
135 }
Joe Onoratofee845a2023-05-09 08:14:14 -0700136 ctx.Build(pctx, android.BuildParams{
137 Rule: aconfigRule,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000138 Output: intermediateCacheFilePath,
Joe Onorato4551ea12023-08-19 19:02:15 -0700139 Inputs: inputFiles,
Joe Onorato981c9262023-06-21 15:16:23 -0700140 Description: "aconfig_declarations",
Yu Liueeff2222024-03-19 23:07:34 +0000141 Args: args,
Joe Onoratofee845a2023-05-09 08:14:14 -0700142 })
143
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000144 intermediateDumpFilePath := android.PathForModuleOut(ctx, "intermediate.txt")
145 ctx.Build(pctx, android.BuildParams{
146 Rule: aconfigTextRule,
147 Output: intermediateDumpFilePath,
148 Inputs: android.Paths{intermediateCacheFilePath},
149 Description: "aconfig_text",
150 })
151
LaMont Jonesaa005ae2023-12-19 19:01:57 +0000152 android.SetProvider(ctx, android.AconfigDeclarationsProviderKey, android.AconfigDeclarationsProviderData{
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000153 Package: module.properties.Package,
154 Container: module.properties.Container,
Zi Wang0e5d16c2024-02-08 06:19:34 +0000155 Exportable: module.properties.Exportable,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000156 IntermediateCacheOutputPath: intermediateCacheFilePath,
157 IntermediateDumpOutputPath: intermediateDumpFilePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700158 })
Joe Onoratofee845a2023-05-09 08:14:14 -0700159}