| 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 | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 28 | ctx.RegisterSingletonModuleType("all_apex_contributions", allApexContributionsFactory) | 
| Spandan Das | 0d53dd2 | 2023-10-24 18:55:12 +0000 | [diff] [blame] | 29 | } | 
|  | 30 |  | 
|  | 31 | type apexContributions struct { | 
|  | 32 | ModuleBase | 
|  | 33 | properties contributionProps | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | type contributionProps struct { | 
|  | 37 | // Name of the mainline module | 
|  | 38 | Api_domain *string | 
|  | 39 | // A list of module names that should be used when this contribution | 
|  | 40 | // is selected via product_config | 
|  | 41 | // The name should be explicit (foo or prebuilt_foo) | 
|  | 42 | Contents []string | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | func (m *apexContributions) ApiDomain() string { | 
|  | 46 | return proptools.String(m.properties.Api_domain) | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | func (m *apexContributions) Contents() []string { | 
|  | 50 | return m.properties.Contents | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | // apex_contributions contains a list of module names (source or | 
|  | 54 | // prebuilt) belonging to the mainline module | 
|  | 55 | // An apex can have multiple apex_contributions modules | 
|  | 56 | // with different combinations of source or prebuilts, but only one can be | 
|  | 57 | // selected via product_config. | 
|  | 58 | func apexContributionsFactory() Module { | 
|  | 59 | module := &apexContributions{} | 
|  | 60 | module.AddProperties(&module.properties) | 
|  | 61 | InitAndroidModule(module) | 
|  | 62 | return module | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | // This module type does not have any build actions. | 
|  | 66 | // It provides metadata that is used in post-deps mutator phase for source vs | 
|  | 67 | // prebuilts selection. | 
|  | 68 | func (m *apexContributions) GenerateAndroidBuildActions(ctx ModuleContext) { | 
|  | 69 | } | 
| Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 70 |  | 
|  | 71 | // A container for apex_contributions. | 
|  | 72 | // Based on product_config, it will create a dependency on the selected | 
|  | 73 | // apex_contributions per mainline module | 
|  | 74 | type allApexContributions struct { | 
|  | 75 | SingletonModuleBase | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | func allApexContributionsFactory() SingletonModule { | 
|  | 79 | module := &allApexContributions{} | 
|  | 80 | InitAndroidModule(module) | 
|  | 81 | return module | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | type apexContributionsDepTag struct { | 
|  | 85 | blueprint.BaseDependencyTag | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | var ( | 
|  | 89 | acDepTag = apexContributionsDepTag{} | 
|  | 90 | ) | 
|  | 91 |  | 
|  | 92 | // Creates a dep to each selected apex_contributions | 
|  | 93 | func (a *allApexContributions) DepsMutator(ctx BottomUpMutatorContext) { | 
|  | 94 | ctx.AddDependency(ctx.Module(), acDepTag, ctx.Config().AllApexContributions()...) | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | // Set PrebuiltSelectionInfoProvider in post deps phase | 
|  | 98 | func (a *allApexContributions) SetPrebuiltSelectionInfoProvider(ctx BaseModuleContext) { | 
|  | 99 | addContentsToProvider := func(p *PrebuiltSelectionInfoMap, m *apexContributions) { | 
|  | 100 | for _, content := range m.Contents() { | 
| Spandan Das | 2daded4 | 2023-11-17 18:58:48 +0000 | [diff] [blame] | 101 | if !ctx.OtherModuleExists(content) && !ctx.Config().AllowMissingDependencies() { | 
| Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 102 | ctx.ModuleErrorf("%s listed in apex_contributions %s does not exist\n", content, m.Name()) | 
|  | 103 | } | 
|  | 104 | pi := &PrebuiltSelectionInfo{ | 
|  | 105 | baseModuleName:     RemoveOptionalPrebuiltPrefix(content), | 
|  | 106 | selectedModuleName: content, | 
|  | 107 | metadataModuleName: m.Name(), | 
|  | 108 | apiDomain:          m.ApiDomain(), | 
|  | 109 | } | 
|  | 110 | p.Add(ctx, pi) | 
|  | 111 | } | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | if ctx.Config().Bp2buildMode() { // Skip bp2build | 
|  | 115 | return | 
|  | 116 | } | 
|  | 117 | p := PrebuiltSelectionInfoMap{} | 
|  | 118 | ctx.VisitDirectDepsWithTag(acDepTag, func(child Module) { | 
|  | 119 | if m, ok := child.(*apexContributions); ok { | 
|  | 120 | addContentsToProvider(&p, m) | 
|  | 121 | } else { | 
|  | 122 | ctx.ModuleErrorf("%s is not an apex_contributions module\n", child.Name()) | 
|  | 123 | } | 
|  | 124 | }) | 
|  | 125 | ctx.SetProvider(PrebuiltSelectionInfoProvider, p) | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | // A provider containing metadata about whether source or prebuilt should be used | 
|  | 129 | // This provider will be used in prebuilt_select mutator to redirect deps | 
|  | 130 | var PrebuiltSelectionInfoProvider = blueprint.NewMutatorProvider(PrebuiltSelectionInfoMap{}, "prebuilt_select") | 
|  | 131 |  | 
|  | 132 | // Map of baseModuleName to the selected source or prebuilt | 
|  | 133 | type PrebuiltSelectionInfoMap map[string]PrebuiltSelectionInfo | 
|  | 134 |  | 
|  | 135 | // Add a new entry to the map with some validations | 
|  | 136 | func (pm *PrebuiltSelectionInfoMap) Add(ctx BaseModuleContext, p *PrebuiltSelectionInfo) { | 
|  | 137 | if p == nil { | 
|  | 138 | return | 
|  | 139 | } | 
|  | 140 | // Do not allow dups. If the base module (without the prebuilt_) has been added before, raise an exception. | 
|  | 141 | if old, exists := (*pm)[p.baseModuleName]; exists { | 
|  | 142 | ctx.ModuleErrorf("Cannot use Soong module: %s from apex_contributions: %s because it has been added previously as: %s from apex_contributions: %s\n", | 
|  | 143 | p.selectedModuleName, p.metadataModuleName, old.selectedModuleName, old.metadataModuleName, | 
|  | 144 | ) | 
|  | 145 | } | 
|  | 146 | (*pm)[p.baseModuleName] = *p | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | type PrebuiltSelectionInfo struct { | 
|  | 150 | // e.g. libc | 
|  | 151 | baseModuleName string | 
|  | 152 | // e.g. (libc|prebuilt_libc) | 
|  | 153 | selectedModuleName string | 
|  | 154 | // Name of the apex_contributions module | 
|  | 155 | metadataModuleName string | 
|  | 156 | // e.g. com.android.runtime | 
|  | 157 | apiDomain string | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | // Returns true if `name` is explicitly requested using one of the selected | 
|  | 161 | // apex_contributions metadata modules. | 
|  | 162 | func (p *PrebuiltSelectionInfoMap) IsSelected(baseModuleName, name string) bool { | 
|  | 163 | if i, exists := (*p)[baseModuleName]; exists { | 
|  | 164 | return i.selectedModuleName == name | 
|  | 165 | } else { | 
|  | 166 | return false | 
|  | 167 | } | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | // This module type does not have any build actions. | 
|  | 171 | func (a *allApexContributions) GenerateAndroidBuildActions(ctx ModuleContext) { | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | func (a *allApexContributions) GenerateSingletonBuildActions(ctx SingletonContext) { | 
|  | 175 | } |