blob: 0437c266f33d7eff680d14b0f4dc255d0593e95a [file] [log] [blame]
Joe Onorato2f99c472023-06-21 18:10:28 -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
15package aconfig
16
17import (
18 "android/soong/android"
Dennis Shen4e7773d2024-01-05 19:06:50 +000019 "fmt"
LaMont Jones21d04d92024-06-11 11:28:54 -070020 "slices"
Joe Onorato2f99c472023-06-21 18:10:28 -070021)
22
23// A singleton module that collects all of the aconfig flags declared in the
24// tree into a single combined file for export to the external flag setting
25// server (inside Google it's Gantry).
26//
27// Note that this is ALL aconfig_declarations modules present in the tree, not just
28// ones that are relevant to the product currently being built, so that that infra
29// doesn't need to pull from multiple builds and merge them.
30func AllAconfigDeclarationsFactory() android.Singleton {
LaMont Jones21d04d92024-06-11 11:28:54 -070031 return &allAconfigDeclarationsSingleton{releaseMap: make(map[string]allAconfigReleaseDeclarationsSingleton)}
Joe Onorato2f99c472023-06-21 18:10:28 -070032}
33
LaMont Jones21d04d92024-06-11 11:28:54 -070034type allAconfigReleaseDeclarationsSingleton struct {
Mårten Kongstad2a1adcc2024-02-20 12:43:21 +010035 intermediateBinaryProtoPath android.OutputPath
36 intermediateTextProtoPath android.OutputPath
Joe Onorato2f99c472023-06-21 18:10:28 -070037}
38
LaMont Jones21d04d92024-06-11 11:28:54 -070039type allAconfigDeclarationsSingleton struct {
40 releaseMap map[string]allAconfigReleaseDeclarationsSingleton
41}
42
43func (this *allAconfigDeclarationsSingleton) sortedConfigNames() []string {
44 var names []string
45 for k := range this.releaseMap {
46 names = append(names, k)
47 }
48 slices.Sort(names)
49 return names
50}
51
Joe Onorato2f99c472023-06-21 18:10:28 -070052func (this *allAconfigDeclarationsSingleton) GenerateBuildActions(ctx android.SingletonContext) {
LaMont Jones21d04d92024-06-11 11:28:54 -070053 for _, rcName := range append([]string{""}, ctx.Config().ReleaseAconfigExtraReleaseConfigs()...) {
54 // Find all of the aconfig_declarations modules
55 var packages = make(map[string]int)
56 var cacheFiles android.Paths
57 ctx.VisitAllModules(func(module android.Module) {
58 decl, ok := android.SingletonModuleProvider(ctx, module, android.AconfigReleaseDeclarationsProviderKey)
59 if !ok {
60 return
61 }
62 cacheFiles = append(cacheFiles, decl[rcName].IntermediateCacheOutputPath)
63 packages[decl[rcName].Package]++
64 })
65
66 var numOffendingPkg = 0
67 for pkg, cnt := range packages {
68 if cnt > 1 {
69 fmt.Printf("%d aconfig_declarations found for package %s\n", cnt, pkg)
70 numOffendingPkg++
71 }
Joe Onorato2f99c472023-06-21 18:10:28 -070072 }
Joe Onorato2f99c472023-06-21 18:10:28 -070073
LaMont Jones21d04d92024-06-11 11:28:54 -070074 if numOffendingPkg > 0 {
75 panic(fmt.Errorf("Only one aconfig_declarations allowed for each package."))
Dennis Shen4e7773d2024-01-05 19:06:50 +000076 }
LaMont Jones21d04d92024-06-11 11:28:54 -070077
78 // Generate build action for aconfig (binary proto output)
79 paths := allAconfigReleaseDeclarationsSingleton{
80 intermediateBinaryProtoPath: android.PathForIntermediates(ctx, assembleFileName(rcName, "all_aconfig_declarations.pb")),
81 intermediateTextProtoPath: android.PathForIntermediates(ctx, assembleFileName(rcName, "all_aconfig_declarations.textproto")),
82 }
83 this.releaseMap[rcName] = paths
84 ctx.Build(pctx, android.BuildParams{
85 Rule: AllDeclarationsRule,
86 Inputs: cacheFiles,
87 Output: this.releaseMap[rcName].intermediateBinaryProtoPath,
88 Description: "all_aconfig_declarations",
89 Args: map[string]string{
90 "cache_files": android.JoinPathsWithPrefix(cacheFiles, "--cache "),
91 },
92 })
93 ctx.Phony("all_aconfig_declarations", this.releaseMap[rcName].intermediateBinaryProtoPath)
94
95 // Generate build action for aconfig (text proto output)
96 ctx.Build(pctx, android.BuildParams{
97 Rule: AllDeclarationsRuleTextProto,
98 Inputs: cacheFiles,
99 Output: this.releaseMap[rcName].intermediateTextProtoPath,
100 Description: "all_aconfig_declarations_textproto",
101 Args: map[string]string{
102 "cache_files": android.JoinPathsWithPrefix(cacheFiles, "--cache "),
103 },
104 })
105 ctx.Phony("all_aconfig_declarations_textproto", this.releaseMap[rcName].intermediateTextProtoPath)
Dennis Shen4e7773d2024-01-05 19:06:50 +0000106 }
Joe Onorato2f99c472023-06-21 18:10:28 -0700107}
108
109func (this *allAconfigDeclarationsSingleton) MakeVars(ctx android.MakeVarsContext) {
LaMont Jones21d04d92024-06-11 11:28:54 -0700110 for _, rcName := range this.sortedConfigNames() {
111 ctx.DistForGoal("droid", this.releaseMap[rcName].intermediateBinaryProtoPath)
112 for _, goal := range []string{"docs", "droid", "sdk"} {
113 ctx.DistForGoalWithFilename(goal, this.releaseMap[rcName].intermediateBinaryProtoPath, assembleFileName(rcName, "flags.pb"))
114 ctx.DistForGoalWithFilename(goal, this.releaseMap[rcName].intermediateTextProtoPath, assembleFileName(rcName, "flags.textproto"))
115 }
Mårten Kongstadc6135322024-02-23 09:22:56 +0100116 }
Joe Onorato2f99c472023-06-21 18:10:28 -0700117}