blob: 007d5290f4b5067f2cfa346c5128548de6a7ff3c [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
Joe Onorato981c9262023-06-21 15:16:23 -070099// Provider published by aconfig_value_set
100type declarationsProviderData struct {
Joe Onorato81b25ed2023-06-21 13:49:37 -0700101 Package string
102 IntermediatePath android.WritablePath
Joe Onorato175073c2023-06-01 14:42:59 -0700103}
104
Joe Onorato981c9262023-06-21 15:16:23 -0700105var declarationsProviderKey = blueprint.NewProvider(declarationsProviderData{})
Joe Onorato175073c2023-06-01 14:42:59 -0700106
Joe Onorato981c9262023-06-21 15:16:23 -0700107func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
108 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
Joe Onoratofee845a2023-05-09 08:14:14 -0700109 ctx.VisitDirectDeps(func(dep android.Module) {
110 if !ctx.OtherModuleHasProvider(dep, valueSetProviderKey) {
111 // Other modules get injected as dependencies too, for example the license modules
112 return
113 }
114 depData := ctx.OtherModuleProvider(dep, valueSetProviderKey).(valueSetProviderData)
Joe Onorato81b25ed2023-06-21 13:49:37 -0700115 valuesFiles, ok := depData.AvailablePackages[module.properties.Package]
Joe Onoratofee845a2023-05-09 08:14:14 -0700116 if ok {
117 for _, path := range valuesFiles {
118 module.properties.Values = append(module.properties.Values, path.String())
119 }
120 }
121 })
122
123 // Intermediate format
124 inputFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
MÃ¥rten Kongstadc89e9242023-06-21 08:32:53 +0200125 intermediatePath := android.PathForModuleOut(ctx, "intermediate.pb")
Joe Onoratofee845a2023-05-09 08:14:14 -0700126 ctx.Build(pctx, android.BuildParams{
127 Rule: aconfigRule,
128 Inputs: inputFiles,
Joe Onorato175073c2023-06-01 14:42:59 -0700129 Output: intermediatePath,
Joe Onorato981c9262023-06-21 15:16:23 -0700130 Description: "aconfig_declarations",
Joe Onoratofee845a2023-05-09 08:14:14 -0700131 Args: map[string]string{
132 "release_version": ctx.Config().ReleaseVersion(),
Joe Onorato81b25ed2023-06-21 13:49:37 -0700133 "package": module.properties.Package,
Joe Onoratofee845a2023-05-09 08:14:14 -0700134 "values": joinAndPrefix(" --values ", module.properties.Values),
135 },
136 })
137
Joe Onorato981c9262023-06-21 15:16:23 -0700138 ctx.SetProvider(declarationsProviderKey, declarationsProviderData{
Joe Onorato81b25ed2023-06-21 13:49:37 -0700139 Package: module.properties.Package,
140 IntermediatePath: intermediatePath,
Joe Onoratofee845a2023-05-09 08:14:14 -0700141 })
142
Joe Onoratofee845a2023-05-09 08:14:14 -0700143}