Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package aconfig |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Dennis Shen | 4e7773d | 2024-01-05 19:06:50 +0000 | [diff] [blame] | 19 | "fmt" |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 20 | ) |
| 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. |
| 29 | func AllAconfigDeclarationsFactory() android.Singleton { |
| 30 | return &allAconfigDeclarationsSingleton{} |
| 31 | } |
| 32 | |
| 33 | type allAconfigDeclarationsSingleton struct { |
Mårten Kongstad | 2a1adcc | 2024-02-20 12:43:21 +0100 | [diff] [blame^] | 34 | intermediateBinaryProtoPath android.OutputPath |
| 35 | intermediateTextProtoPath android.OutputPath |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | func (this *allAconfigDeclarationsSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 39 | // Find all of the aconfig_declarations modules |
Dennis Shen | 4e7773d | 2024-01-05 19:06:50 +0000 | [diff] [blame] | 40 | var packages = make(map[string]int) |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 41 | var cacheFiles android.Paths |
| 42 | ctx.VisitAllModules(func(module android.Module) { |
LaMont Jones | aa005ae | 2023-12-19 19:01:57 +0000 | [diff] [blame] | 43 | decl, ok := android.SingletonModuleProvider(ctx, module, android.AconfigDeclarationsProviderKey) |
Colin Cross | 5a37718 | 2023-12-14 14:46:23 -0800 | [diff] [blame] | 44 | if !ok { |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 45 | return |
| 46 | } |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 47 | cacheFiles = append(cacheFiles, decl.IntermediateCacheOutputPath) |
Dennis Shen | 4e7773d | 2024-01-05 19:06:50 +0000 | [diff] [blame] | 48 | packages[decl.Package]++ |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 49 | }) |
| 50 | |
Dennis Shen | 4e7773d | 2024-01-05 19:06:50 +0000 | [diff] [blame] | 51 | 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 Kongstad | 2a1adcc | 2024-02-20 12:43:21 +0100 | [diff] [blame^] | 63 | // Generate build action for aconfig (binary proto output) |
| 64 | this.intermediateBinaryProtoPath = android.PathForIntermediates(ctx, "all_aconfig_declarations.pb") |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 65 | ctx.Build(pctx, android.BuildParams{ |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 66 | Rule: AllDeclarationsRule, |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 67 | Inputs: cacheFiles, |
Mårten Kongstad | 2a1adcc | 2024-02-20 12:43:21 +0100 | [diff] [blame^] | 68 | Output: this.intermediateBinaryProtoPath, |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 69 | Description: "all_aconfig_declarations", |
| 70 | Args: map[string]string{ |
| 71 | "cache_files": android.JoinPathsWithPrefix(cacheFiles, "--cache "), |
| 72 | }, |
| 73 | }) |
Mårten Kongstad | 2a1adcc | 2024-02-20 12:43:21 +0100 | [diff] [blame^] | 74 | 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 Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | func (this *allAconfigDeclarationsSingleton) MakeVars(ctx android.MakeVarsContext) { |
Mårten Kongstad | 2a1adcc | 2024-02-20 12:43:21 +0100 | [diff] [blame^] | 91 | ctx.DistForGoal("droid", this.intermediateBinaryProtoPath) |
| 92 | ctx.DistForGoalWithFilename("sdk", this.intermediateBinaryProtoPath, "flags.pb") |
| 93 | ctx.DistForGoalWithFilename("sdk", this.intermediateTextProtoPath, "flags.textproto") |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 94 | } |