Spandan Das | 0d53dd2 | 2023-10-24 18:55:12 +0000 | [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 android |
| 16 | |
| 17 | import ( |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 18 | "github.com/google/blueprint" |
Spandan Das | 0d53dd2 | 2023-10-24 18:55:12 +0000 | [diff] [blame] | 19 | "github.com/google/blueprint/proptools" |
| 20 | ) |
| 21 | |
| 22 | func init() { |
| 23 | RegisterApexContributionsBuildComponents(InitRegistrationContext) |
| 24 | } |
| 25 | |
| 26 | func RegisterApexContributionsBuildComponents(ctx RegistrationContext) { |
| 27 | ctx.RegisterModuleType("apex_contributions", apexContributionsFactory) |
Spandan Das | 471d068 | 2024-03-06 14:47:08 +0000 | [diff] [blame] | 28 | ctx.RegisterModuleType("apex_contributions_defaults", apexContributionsDefaultsFactory) |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 29 | ctx.RegisterSingletonModuleType("all_apex_contributions", allApexContributionsFactory) |
Spandan Das | 0d53dd2 | 2023-10-24 18:55:12 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | type apexContributions struct { |
| 33 | ModuleBase |
Spandan Das | 471d068 | 2024-03-06 14:47:08 +0000 | [diff] [blame] | 34 | DefaultableModuleBase |
Spandan Das | 0d53dd2 | 2023-10-24 18:55:12 +0000 | [diff] [blame] | 35 | properties contributionProps |
| 36 | } |
| 37 | |
| 38 | type contributionProps struct { |
| 39 | // Name of the mainline module |
| 40 | Api_domain *string |
| 41 | // A list of module names that should be used when this contribution |
| 42 | // is selected via product_config |
| 43 | // The name should be explicit (foo or prebuilt_foo) |
| 44 | Contents []string |
| 45 | } |
| 46 | |
| 47 | func (m *apexContributions) ApiDomain() string { |
| 48 | return proptools.String(m.properties.Api_domain) |
| 49 | } |
| 50 | |
| 51 | func (m *apexContributions) Contents() []string { |
| 52 | return m.properties.Contents |
| 53 | } |
| 54 | |
| 55 | // apex_contributions contains a list of module names (source or |
| 56 | // prebuilt) belonging to the mainline module |
| 57 | // An apex can have multiple apex_contributions modules |
| 58 | // with different combinations of source or prebuilts, but only one can be |
| 59 | // selected via product_config. |
| 60 | func apexContributionsFactory() Module { |
| 61 | module := &apexContributions{} |
| 62 | module.AddProperties(&module.properties) |
| 63 | InitAndroidModule(module) |
Spandan Das | 471d068 | 2024-03-06 14:47:08 +0000 | [diff] [blame] | 64 | InitDefaultableModule(module) |
Spandan Das | 0d53dd2 | 2023-10-24 18:55:12 +0000 | [diff] [blame] | 65 | return module |
| 66 | } |
| 67 | |
| 68 | // This module type does not have any build actions. |
| 69 | // It provides metadata that is used in post-deps mutator phase for source vs |
| 70 | // prebuilts selection. |
| 71 | func (m *apexContributions) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 72 | } |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 73 | |
Spandan Das | 471d068 | 2024-03-06 14:47:08 +0000 | [diff] [blame] | 74 | type apexContributionsDefaults struct { |
| 75 | ModuleBase |
| 76 | DefaultsModuleBase |
| 77 | } |
| 78 | |
| 79 | func apexContributionsDefaultsFactory() Module { |
| 80 | module := &apexContributionsDefaults{} |
| 81 | module.AddProperties(&contributionProps{}) |
| 82 | InitDefaultsModule(module) |
| 83 | return module |
| 84 | } |
| 85 | |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 86 | // A container for apex_contributions. |
| 87 | // Based on product_config, it will create a dependency on the selected |
| 88 | // apex_contributions per mainline module |
| 89 | type allApexContributions struct { |
| 90 | SingletonModuleBase |
| 91 | } |
| 92 | |
| 93 | func allApexContributionsFactory() SingletonModule { |
| 94 | module := &allApexContributions{} |
| 95 | InitAndroidModule(module) |
| 96 | return module |
| 97 | } |
| 98 | |
| 99 | type apexContributionsDepTag struct { |
| 100 | blueprint.BaseDependencyTag |
| 101 | } |
| 102 | |
| 103 | var ( |
Jihoon Kang | a3a0546 | 2024-04-05 00:36:44 +0000 | [diff] [blame] | 104 | AcDepTag = apexContributionsDepTag{} |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 105 | ) |
| 106 | |
| 107 | // Creates a dep to each selected apex_contributions |
| 108 | func (a *allApexContributions) DepsMutator(ctx BottomUpMutatorContext) { |
Spandan Das | a866713 | 2024-05-30 16:46:44 +0000 | [diff] [blame] | 109 | // Skip apex_contributions if BuildApexContributionContents is true |
| 110 | // This product config var allows some products in the same family to use mainline modules from source |
| 111 | // (e.g. shiba and shiba_fullmte) |
| 112 | // Eventually these product variants will have their own release config maps. |
| 113 | if !proptools.Bool(ctx.Config().BuildIgnoreApexContributionContents()) { |
| 114 | ctx.AddDependency(ctx.Module(), AcDepTag, ctx.Config().AllApexContributions()...) |
| 115 | } |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Set PrebuiltSelectionInfoProvider in post deps phase |
| 119 | func (a *allApexContributions) SetPrebuiltSelectionInfoProvider(ctx BaseModuleContext) { |
| 120 | addContentsToProvider := func(p *PrebuiltSelectionInfoMap, m *apexContributions) { |
| 121 | for _, content := range m.Contents() { |
Spandan Das | 2daded4 | 2023-11-17 18:58:48 +0000 | [diff] [blame] | 122 | if !ctx.OtherModuleExists(content) && !ctx.Config().AllowMissingDependencies() { |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 123 | ctx.ModuleErrorf("%s listed in apex_contributions %s does not exist\n", content, m.Name()) |
| 124 | } |
| 125 | pi := &PrebuiltSelectionInfo{ |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 126 | selectedModuleName: content, |
| 127 | metadataModuleName: m.Name(), |
| 128 | apiDomain: m.ApiDomain(), |
| 129 | } |
| 130 | p.Add(ctx, pi) |
| 131 | } |
| 132 | } |
| 133 | |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 134 | p := PrebuiltSelectionInfoMap{} |
Spandan Das | a866713 | 2024-05-30 16:46:44 +0000 | [diff] [blame] | 135 | ctx.VisitDirectDepsWithTag(AcDepTag, func(child Module) { |
| 136 | if m, ok := child.(*apexContributions); ok { |
| 137 | addContentsToProvider(&p, m) |
| 138 | } else { |
| 139 | ctx.ModuleErrorf("%s is not an apex_contributions module\n", child.Name()) |
| 140 | } |
| 141 | }) |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 142 | SetProvider(ctx, PrebuiltSelectionInfoProvider, p) |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // A provider containing metadata about whether source or prebuilt should be used |
| 146 | // This provider will be used in prebuilt_select mutator to redirect deps |
Colin Cross | bc7d76c | 2023-12-12 16:39:03 -0800 | [diff] [blame] | 147 | var PrebuiltSelectionInfoProvider = blueprint.NewMutatorProvider[PrebuiltSelectionInfoMap]("prebuilt_select") |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 148 | |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 149 | // Map of selected module names to a metadata object |
| 150 | // The metadata contains information about the api_domain of the selected module |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 151 | type PrebuiltSelectionInfoMap map[string]PrebuiltSelectionInfo |
| 152 | |
| 153 | // Add a new entry to the map with some validations |
| 154 | func (pm *PrebuiltSelectionInfoMap) Add(ctx BaseModuleContext, p *PrebuiltSelectionInfo) { |
| 155 | if p == nil { |
| 156 | return |
| 157 | } |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 158 | (*pm)[p.selectedModuleName] = *p |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | type PrebuiltSelectionInfo struct { |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 162 | // e.g. (libc|prebuilt_libc) |
| 163 | selectedModuleName string |
| 164 | // Name of the apex_contributions module |
| 165 | metadataModuleName string |
| 166 | // e.g. com.android.runtime |
| 167 | apiDomain string |
| 168 | } |
| 169 | |
| 170 | // Returns true if `name` is explicitly requested using one of the selected |
| 171 | // apex_contributions metadata modules. |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 172 | func (p *PrebuiltSelectionInfoMap) IsSelected(name string) bool { |
| 173 | _, exists := (*p)[name] |
| 174 | return exists |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Spandan Das | da739a3 | 2023-12-13 00:06:32 +0000 | [diff] [blame] | 177 | // Return the list of soong modules selected for this api domain |
| 178 | // In the case of apexes, it is the canonical name of the apex on device (/apex/<apex_name>) |
| 179 | func (p *PrebuiltSelectionInfoMap) GetSelectedModulesForApiDomain(apiDomain string) []string { |
| 180 | selected := []string{} |
| 181 | for _, entry := range *p { |
| 182 | if entry.apiDomain == apiDomain { |
| 183 | selected = append(selected, entry.selectedModuleName) |
| 184 | } |
| 185 | } |
| 186 | return selected |
| 187 | } |
| 188 | |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 189 | // This module type does not have any build actions. |
| 190 | func (a *allApexContributions) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 191 | } |
| 192 | |
| 193 | func (a *allApexContributions) GenerateSingletonBuildActions(ctx SingletonContext) { |
| 194 | } |