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