blob: 3d9663c1d340267ed87410d7fca0b3aeaf26e5b6 [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"
Joe Onorato2f99c472023-06-21 18:10:28 -070020)
21
22// A singleton module that collects all of the aconfig flags declared in the
23// tree into a single combined file for export to the external flag setting
24// server (inside Google it's Gantry).
25//
26// Note that this is ALL aconfig_declarations modules present in the tree, not just
27// ones that are relevant to the product currently being built, so that that infra
28// doesn't need to pull from multiple builds and merge them.
29func AllAconfigDeclarationsFactory() android.Singleton {
30 return &allAconfigDeclarationsSingleton{}
31}
32
33type allAconfigDeclarationsSingleton struct {
Mårten Kongstad2a1adcc2024-02-20 12:43:21 +010034 intermediateBinaryProtoPath android.OutputPath
35 intermediateTextProtoPath android.OutputPath
Joe Onorato2f99c472023-06-21 18:10:28 -070036}
37
38func (this *allAconfigDeclarationsSingleton) GenerateBuildActions(ctx android.SingletonContext) {
39 // Find all of the aconfig_declarations modules
Dennis Shen4e7773d2024-01-05 19:06:50 +000040 var packages = make(map[string]int)
Joe Onorato2f99c472023-06-21 18:10:28 -070041 var cacheFiles android.Paths
42 ctx.VisitAllModules(func(module android.Module) {
LaMont Jonesaa005ae2023-12-19 19:01:57 +000043 decl, ok := android.SingletonModuleProvider(ctx, module, android.AconfigDeclarationsProviderKey)
Colin Cross5a377182023-12-14 14:46:23 -080044 if !ok {
Joe Onorato2f99c472023-06-21 18:10:28 -070045 return
46 }
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000047 cacheFiles = append(cacheFiles, decl.IntermediateCacheOutputPath)
Dennis Shen4e7773d2024-01-05 19:06:50 +000048 packages[decl.Package]++
Joe Onorato2f99c472023-06-21 18:10:28 -070049 })
50
Dennis Shen4e7773d2024-01-05 19:06:50 +000051 var numOffendingPkg = 0
52 for pkg, cnt := range packages {
53 if cnt > 1 {
54 fmt.Printf("%d aconfig_declarations found for package %s\n", cnt, pkg)
55 numOffendingPkg++
56 }
57 }
58
59 if numOffendingPkg > 0 {
60 panic(fmt.Errorf("Only one aconfig_declarations allowed for each package."))
61 }
62
Mårten Kongstad2a1adcc2024-02-20 12:43:21 +010063 // Generate build action for aconfig (binary proto output)
64 this.intermediateBinaryProtoPath = android.PathForIntermediates(ctx, "all_aconfig_declarations.pb")
Joe Onorato2f99c472023-06-21 18:10:28 -070065 ctx.Build(pctx, android.BuildParams{
Yu Liueae7b362023-11-16 17:05:47 -080066 Rule: AllDeclarationsRule,
Joe Onorato2f99c472023-06-21 18:10:28 -070067 Inputs: cacheFiles,
Mårten Kongstad2a1adcc2024-02-20 12:43:21 +010068 Output: this.intermediateBinaryProtoPath,
Joe Onorato2f99c472023-06-21 18:10:28 -070069 Description: "all_aconfig_declarations",
70 Args: map[string]string{
71 "cache_files": android.JoinPathsWithPrefix(cacheFiles, "--cache "),
72 },
73 })
Mårten Kongstad2a1adcc2024-02-20 12:43:21 +010074 ctx.Phony("all_aconfig_declarations", this.intermediateBinaryProtoPath)
75
76 // Generate build action for aconfig (text proto output)
77 this.intermediateTextProtoPath = android.PathForIntermediates(ctx, "all_aconfig_declarations.textproto")
78 ctx.Build(pctx, android.BuildParams{
79 Rule: AllDeclarationsRuleTextProto,
80 Inputs: cacheFiles,
81 Output: this.intermediateTextProtoPath,
82 Description: "all_aconfig_declarations_textproto",
83 Args: map[string]string{
84 "cache_files": android.JoinPathsWithPrefix(cacheFiles, "--cache "),
85 },
86 })
87 ctx.Phony("all_aconfig_declarations_textproto", this.intermediateTextProtoPath)
Joe Onorato2f99c472023-06-21 18:10:28 -070088}
89
90func (this *allAconfigDeclarationsSingleton) MakeVars(ctx android.MakeVarsContext) {
Mårten Kongstad2a1adcc2024-02-20 12:43:21 +010091 ctx.DistForGoal("droid", this.intermediateBinaryProtoPath)
92 ctx.DistForGoalWithFilename("sdk", this.intermediateBinaryProtoPath, "flags.pb")
93 ctx.DistForGoalWithFilename("sdk", this.intermediateTextProtoPath, "flags.textproto")
Joe Onorato2f99c472023-06-21 18:10:28 -070094}