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" |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame] | 20 | "slices" |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 21 | ) |
| 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. |
| 30 | func AllAconfigDeclarationsFactory() android.Singleton { |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame] | 31 | return &allAconfigDeclarationsSingleton{releaseMap: make(map[string]allAconfigReleaseDeclarationsSingleton)} |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 32 | } |
| 33 | |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame] | 34 | type allAconfigReleaseDeclarationsSingleton struct { |
Mårten Kongstad | 2a1adcc | 2024-02-20 12:43:21 +0100 | [diff] [blame] | 35 | intermediateBinaryProtoPath android.OutputPath |
| 36 | intermediateTextProtoPath android.OutputPath |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 37 | } |
| 38 | |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame] | 39 | type allAconfigDeclarationsSingleton struct { |
| 40 | releaseMap map[string]allAconfigReleaseDeclarationsSingleton |
| 41 | } |
| 42 | |
| 43 | func (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 Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 52 | func (this *allAconfigDeclarationsSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame] | 53 | 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 Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 72 | } |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 73 | |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame] | 74 | if numOffendingPkg > 0 { |
| 75 | panic(fmt.Errorf("Only one aconfig_declarations allowed for each package.")) |
Dennis Shen | 4e7773d | 2024-01-05 19:06:50 +0000 | [diff] [blame] | 76 | } |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame] | 77 | |
| 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 Shen | 4e7773d | 2024-01-05 19:06:50 +0000 | [diff] [blame] | 106 | } |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | func (this *allAconfigDeclarationsSingleton) MakeVars(ctx android.MakeVarsContext) { |
LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame] | 110 | 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 Kongstad | c613532 | 2024-02-23 09:22:56 +0100 | [diff] [blame] | 116 | } |
Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 117 | } |