Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1 | // Copyright (C) 2019 The Android Open Source Project |
| 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 | // This file contains all the foundation components for override modules and their base module |
| 18 | // types. Override modules are a kind of opposite of default modules in that they override certain |
| 19 | // properties of an existing base module whereas default modules provide base module data to be |
| 20 | // overridden. However, unlike default and defaultable module pairs, both override and overridable |
| 21 | // modules generate and output build actions, and it is up to product make vars to decide which one |
| 22 | // to actually build and install in the end. In other words, default modules and defaultable modules |
| 23 | // can be compared to abstract classes and concrete classes in C++ and Java. By the same analogy, |
| 24 | // both override and overridable modules act like concrete classes. |
| 25 | // |
| 26 | // There is one more crucial difference from the logic perspective. Unlike default pairs, most Soong |
| 27 | // actions happen in the base (overridable) module by creating a local variant for each override |
| 28 | // module based on it. |
| 29 | |
| 30 | import ( |
Dan Willemsen | 1a8c856 | 2020-06-29 23:49:34 -0700 | [diff] [blame] | 31 | "sort" |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 32 | "sync" |
| 33 | |
| 34 | "github.com/google/blueprint" |
| 35 | "github.com/google/blueprint/proptools" |
| 36 | ) |
| 37 | |
| 38 | // Interface for override module types, e.g. override_android_app, override_apex |
| 39 | type OverrideModule interface { |
| 40 | Module |
| 41 | |
| 42 | getOverridingProperties() []interface{} |
| 43 | setOverridingProperties(properties []interface{}) |
| 44 | |
| 45 | getOverrideModuleProperties() *OverrideModuleProperties |
Jaewoong Jung | 1e362da | 2020-06-23 07:58:13 -0700 | [diff] [blame] | 46 | |
| 47 | // Internal funcs to handle interoperability between override modules and prebuilts. |
| 48 | // i.e. cases where an overriding module, too, is overridden by a prebuilt module. |
| 49 | setOverriddenByPrebuilt(overridden bool) |
| 50 | getOverriddenByPrebuilt() bool |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // Base module struct for override module types |
| 54 | type OverrideModuleBase struct { |
| 55 | moduleProperties OverrideModuleProperties |
| 56 | |
| 57 | overridingProperties []interface{} |
Jaewoong Jung | 1e362da | 2020-06-23 07:58:13 -0700 | [diff] [blame] | 58 | |
| 59 | overriddenByPrebuilt bool |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | type OverrideModuleProperties struct { |
| 63 | // Name of the base module to be overridden |
| 64 | Base *string |
| 65 | |
| 66 | // TODO(jungjw): Add an optional override_name bool flag. |
| 67 | } |
| 68 | |
| 69 | func (o *OverrideModuleBase) getOverridingProperties() []interface{} { |
| 70 | return o.overridingProperties |
| 71 | } |
| 72 | |
| 73 | func (o *OverrideModuleBase) setOverridingProperties(properties []interface{}) { |
| 74 | o.overridingProperties = properties |
| 75 | } |
| 76 | |
| 77 | func (o *OverrideModuleBase) getOverrideModuleProperties() *OverrideModuleProperties { |
| 78 | return &o.moduleProperties |
| 79 | } |
| 80 | |
Jiyong Park | 5d790c3 | 2019-11-15 18:40:32 +0900 | [diff] [blame] | 81 | func (o *OverrideModuleBase) GetOverriddenModuleName() string { |
| 82 | return proptools.String(o.moduleProperties.Base) |
| 83 | } |
| 84 | |
Jaewoong Jung | 1e362da | 2020-06-23 07:58:13 -0700 | [diff] [blame] | 85 | func (o *OverrideModuleBase) setOverriddenByPrebuilt(overridden bool) { |
| 86 | o.overriddenByPrebuilt = overridden |
| 87 | } |
| 88 | |
| 89 | func (o *OverrideModuleBase) getOverriddenByPrebuilt() bool { |
| 90 | return o.overriddenByPrebuilt |
| 91 | } |
| 92 | |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 93 | func InitOverrideModule(m OverrideModule) { |
| 94 | m.setOverridingProperties(m.GetProperties()) |
| 95 | |
| 96 | m.AddProperties(m.getOverrideModuleProperties()) |
| 97 | } |
| 98 | |
| 99 | // Interface for overridable module types, e.g. android_app, apex |
| 100 | type OverridableModule interface { |
Jiyong Park | 317645e | 2019-12-05 13:20:58 +0900 | [diff] [blame] | 101 | Module |
| 102 | moduleBase() *OverridableModuleBase |
| 103 | |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 104 | setOverridableProperties(prop []interface{}) |
| 105 | |
| 106 | addOverride(o OverrideModule) |
| 107 | getOverrides() []OverrideModule |
| 108 | |
| 109 | override(ctx BaseModuleContext, o OverrideModule) |
Jiyong Park | 317645e | 2019-12-05 13:20:58 +0900 | [diff] [blame] | 110 | GetOverriddenBy() string |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 111 | |
| 112 | setOverridesProperty(overridesProperties *[]string) |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 113 | |
| 114 | // Due to complications with incoming dependencies, overrides are processed after DepsMutator. |
| 115 | // So, overridable properties need to be handled in a separate, dedicated deps mutator. |
| 116 | OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Jiyong Park | 317645e | 2019-12-05 13:20:58 +0900 | [diff] [blame] | 119 | type overridableModuleProperties struct { |
| 120 | OverriddenBy string `blueprint:"mutated"` |
| 121 | } |
| 122 | |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 123 | // Base module struct for overridable module types |
| 124 | type OverridableModuleBase struct { |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 125 | // List of OverrideModules that override this base module |
| 126 | overrides []OverrideModule |
| 127 | // Used to parallelize registerOverrideMutator executions. Note that only addOverride locks this |
| 128 | // mutex. It is because addOverride and getOverride are used in different mutators, and so are |
| 129 | // guaranteed to be not mixed. (And, getOverride only reads from overrides, and so don't require |
| 130 | // mutex locking.) |
| 131 | overridesLock sync.Mutex |
| 132 | |
| 133 | overridableProperties []interface{} |
| 134 | |
| 135 | // If an overridable module has a property to list other modules that itself overrides, it should |
| 136 | // set this to a pointer to the property through the InitOverridableModule function, so that |
| 137 | // override information is propagated and aggregated correctly. |
| 138 | overridesProperty *[]string |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 139 | |
Jaewoong Jung | b64dd00 | 2020-01-10 13:26:00 -0800 | [diff] [blame] | 140 | overridableModuleProperties overridableModuleProperties |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | func InitOverridableModule(m OverridableModule, overridesProperty *[]string) { |
| 144 | m.setOverridableProperties(m.(Module).GetProperties()) |
| 145 | m.setOverridesProperty(overridesProperty) |
Jaewoong Jung | b64dd00 | 2020-01-10 13:26:00 -0800 | [diff] [blame] | 146 | m.AddProperties(&m.moduleBase().overridableModuleProperties) |
Jiyong Park | 317645e | 2019-12-05 13:20:58 +0900 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | func (o *OverridableModuleBase) moduleBase() *OverridableModuleBase { |
| 150 | return o |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | func (b *OverridableModuleBase) setOverridableProperties(prop []interface{}) { |
| 154 | b.overridableProperties = prop |
| 155 | } |
| 156 | |
| 157 | func (b *OverridableModuleBase) addOverride(o OverrideModule) { |
| 158 | b.overridesLock.Lock() |
| 159 | b.overrides = append(b.overrides, o) |
| 160 | b.overridesLock.Unlock() |
| 161 | } |
| 162 | |
| 163 | // Should NOT be used in the same mutator as addOverride. |
| 164 | func (b *OverridableModuleBase) getOverrides() []OverrideModule { |
Dan Willemsen | 1a8c856 | 2020-06-29 23:49:34 -0700 | [diff] [blame] | 165 | b.overridesLock.Lock() |
| 166 | sort.Slice(b.overrides, func(i, j int) bool { |
| 167 | return b.overrides[i].Name() < b.overrides[j].Name() |
| 168 | }) |
| 169 | b.overridesLock.Unlock() |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 170 | return b.overrides |
| 171 | } |
| 172 | |
| 173 | func (b *OverridableModuleBase) setOverridesProperty(overridesProperty *[]string) { |
| 174 | b.overridesProperty = overridesProperty |
| 175 | } |
| 176 | |
| 177 | // Overrides a base module with the given OverrideModule. |
| 178 | func (b *OverridableModuleBase) override(ctx BaseModuleContext, o OverrideModule) { |
| 179 | for _, p := range b.overridableProperties { |
| 180 | for _, op := range o.getOverridingProperties() { |
| 181 | if proptools.TypeEqual(p, op) { |
Jiyong Park | 5d790c3 | 2019-11-15 18:40:32 +0900 | [diff] [blame] | 182 | err := proptools.ExtendProperties(p, op, nil, proptools.OrderReplace) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 183 | if err != nil { |
| 184 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 185 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 186 | } else { |
| 187 | panic(err) |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
Jaewoong Jung | 7abcf8e | 2019-12-19 17:32:06 -0800 | [diff] [blame] | 193 | // Adds the base module to the overrides property, if exists, of the overriding module. See the |
| 194 | // comment on OverridableModuleBase.overridesProperty for details. |
| 195 | if b.overridesProperty != nil { |
| 196 | *b.overridesProperty = append(*b.overridesProperty, ctx.ModuleName()) |
| 197 | } |
Jaewoong Jung | b64dd00 | 2020-01-10 13:26:00 -0800 | [diff] [blame] | 198 | b.overridableModuleProperties.OverriddenBy = o.Name() |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jiyong Park | 317645e | 2019-12-05 13:20:58 +0900 | [diff] [blame] | 201 | // GetOverriddenBy returns the name of the override module that has overridden this module. |
| 202 | // For example, if an override module foo has its 'base' property set to bar, then another local variant |
| 203 | // of bar is created and its properties are overriden by foo. This method returns bar when called from |
| 204 | // the new local variant. It returns "" when called from the original variant of bar. |
| 205 | func (b *OverridableModuleBase) GetOverriddenBy() string { |
Jaewoong Jung | b64dd00 | 2020-01-10 13:26:00 -0800 | [diff] [blame] | 206 | return b.overridableModuleProperties.OverriddenBy |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | func (b *OverridableModuleBase) OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) { |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // Mutators for override/overridable modules. All the fun happens in these functions. It is critical |
| 213 | // to keep them in this order and not put any order mutators between them. |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 214 | func RegisterOverridePostDepsMutators(ctx RegisterMutatorsContext) { |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 215 | ctx.BottomUp("override_deps", overrideModuleDepsMutator).Parallel() |
| 216 | ctx.TopDown("register_override", registerOverrideMutator).Parallel() |
| 217 | ctx.BottomUp("perform_override", performOverrideMutator).Parallel() |
Jooyung Han | 86feead | 2021-03-08 13:11:48 +0900 | [diff] [blame] | 218 | // overridableModuleDepsMutator calls OverridablePropertiesDepsMutator so that overridable modules can |
| 219 | // add deps from overridable properties. |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 220 | ctx.BottomUp("overridable_deps", overridableModuleDepsMutator).Parallel() |
Jooyung Han | 86feead | 2021-03-08 13:11:48 +0900 | [diff] [blame] | 221 | // Because overridableModuleDepsMutator is run after PrebuiltPostDepsMutator, |
| 222 | // prebuilt's ReplaceDependencies doesn't affect to those deps added by overridable properties. |
| 223 | // By running PrebuiltPostDepsMutator again after overridableModuleDepsMutator, deps via overridable properties |
| 224 | // can be replaced with prebuilts. |
| 225 | ctx.BottomUp("replace_deps_on_prebuilts_for_overridable_deps_again", PrebuiltPostDepsMutator).Parallel() |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 226 | ctx.BottomUp("replace_deps_on_override", replaceDepsOnOverridingModuleMutator).Parallel() |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | type overrideBaseDependencyTag struct { |
| 230 | blueprint.BaseDependencyTag |
| 231 | } |
| 232 | |
| 233 | var overrideBaseDepTag overrideBaseDependencyTag |
| 234 | |
| 235 | // Adds dependency on the base module to the overriding module so that they can be visited in the |
| 236 | // next phase. |
| 237 | func overrideModuleDepsMutator(ctx BottomUpMutatorContext) { |
| 238 | if module, ok := ctx.Module().(OverrideModule); ok { |
Jooyung Han | 5c17ac0 | 2020-06-20 19:38:21 +0900 | [diff] [blame] | 239 | base := String(module.getOverrideModuleProperties().Base) |
| 240 | if !ctx.OtherModuleExists(base) { |
| 241 | ctx.PropertyErrorf("base", "%q is not a valid module name", base) |
| 242 | return |
| 243 | } |
Jaewoong Jung | 1e362da | 2020-06-23 07:58:13 -0700 | [diff] [blame] | 244 | // See if there's a prebuilt module that overrides this override module with prefer flag, |
Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 245 | // in which case we call HideFromMake on the corresponding variant later. |
Jaewoong Jung | fb25a64 | 2020-06-11 10:37:51 -0700 | [diff] [blame] | 246 | ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(dep Module) { |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame^] | 247 | prebuilt := GetEmbeddedPrebuilt(dep) |
| 248 | if prebuilt == nil { |
Jaewoong Jung | fb25a64 | 2020-06-11 10:37:51 -0700 | [diff] [blame] | 249 | panic("PrebuiltDepTag leads to a non-prebuilt module " + dep.Name()) |
| 250 | } |
Paul Duffin | f7c99f5 | 2021-04-28 10:41:21 +0100 | [diff] [blame^] | 251 | if prebuilt.UsePrebuilt() { |
Jaewoong Jung | 1e362da | 2020-06-23 07:58:13 -0700 | [diff] [blame] | 252 | module.setOverriddenByPrebuilt(true) |
Jaewoong Jung | fb25a64 | 2020-06-11 10:37:51 -0700 | [diff] [blame] | 253 | return |
| 254 | } |
| 255 | }) |
Jaewoong Jung | 1e362da | 2020-06-23 07:58:13 -0700 | [diff] [blame] | 256 | ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | // Visits the base module added as a dependency above, checks the module type, and registers the |
| 261 | // overriding module. |
| 262 | func registerOverrideMutator(ctx TopDownMutatorContext) { |
| 263 | ctx.VisitDirectDepsWithTag(overrideBaseDepTag, func(base Module) { |
| 264 | if o, ok := base.(OverridableModule); ok { |
| 265 | o.addOverride(ctx.Module().(OverrideModule)) |
| 266 | } else { |
| 267 | ctx.PropertyErrorf("base", "unsupported base module type") |
| 268 | } |
| 269 | }) |
| 270 | } |
| 271 | |
| 272 | // Now, goes through all overridable modules, finds all modules overriding them, creates a local |
| 273 | // variant for each of them, and performs the actual overriding operation by calling override(). |
| 274 | func performOverrideMutator(ctx BottomUpMutatorContext) { |
| 275 | if b, ok := ctx.Module().(OverridableModule); ok { |
| 276 | overrides := b.getOverrides() |
| 277 | if len(overrides) == 0 { |
| 278 | return |
| 279 | } |
| 280 | variants := make([]string, len(overrides)+1) |
| 281 | // The first variant is for the original, non-overridden, base module. |
| 282 | variants[0] = "" |
| 283 | for i, o := range overrides { |
| 284 | variants[i+1] = o.(Module).Name() |
| 285 | } |
| 286 | mods := ctx.CreateLocalVariations(variants...) |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 287 | // Make the original variation the default one to depend on if no other override module variant |
| 288 | // is specified. |
| 289 | ctx.AliasVariation(variants[0]) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 290 | for i, o := range overrides { |
| 291 | mods[i+1].(OverridableModule).override(ctx, o) |
Jaewoong Jung | 1e362da | 2020-06-23 07:58:13 -0700 | [diff] [blame] | 292 | if o.getOverriddenByPrebuilt() { |
| 293 | // The overriding module itself, too, is overridden by a prebuilt. Skip its installation. |
Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 294 | mods[i+1].HideFromMake() |
Jaewoong Jung | 1e362da | 2020-06-23 07:58:13 -0700 | [diff] [blame] | 295 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 296 | } |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 297 | } else if o, ok := ctx.Module().(OverrideModule); ok { |
| 298 | // Create a variant of the overriding module with its own name. This matches the above local |
| 299 | // variant name rule for overridden modules, and thus allows ReplaceDependencies to match the |
| 300 | // two. |
| 301 | ctx.CreateLocalVariations(o.Name()) |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 302 | // To allow dependencies to be added without having to know the above variation. |
| 303 | ctx.AliasVariation(o.Name()) |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | func overridableModuleDepsMutator(ctx BottomUpMutatorContext) { |
| 308 | if b, ok := ctx.Module().(OverridableModule); ok { |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 309 | b.OverridablePropertiesDepsMutator(ctx) |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | func replaceDepsOnOverridingModuleMutator(ctx BottomUpMutatorContext) { |
| 314 | if b, ok := ctx.Module().(OverridableModule); ok { |
Jiyong Park | 317645e | 2019-12-05 13:20:58 +0900 | [diff] [blame] | 315 | if o := b.GetOverriddenBy(); o != "" { |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 316 | // Redirect dependencies on the overriding module to this overridden module. Overriding |
| 317 | // modules are basically pseudo modules, and all build actions are associated to overridden |
| 318 | // modules. Therefore, dependencies on overriding modules need to be forwarded there as well. |
| 319 | ctx.ReplaceDependencies(o) |
| 320 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 321 | } |
| 322 | } |
Colin Cross | cb6143a | 2020-08-14 17:39:29 -0700 | [diff] [blame] | 323 | |
| 324 | // ModuleNameWithPossibleOverride returns the name of the OverrideModule that overrides the current |
| 325 | // variant of this OverridableModule, or ctx.ModuleName() if this module is not an OverridableModule |
| 326 | // or if this variant is not overridden. |
| 327 | func ModuleNameWithPossibleOverride(ctx ModuleContext) string { |
| 328 | if overridable, ok := ctx.Module().(OverridableModule); ok { |
| 329 | if o := overridable.GetOverriddenBy(); o != "" { |
| 330 | return o |
| 331 | } |
| 332 | } |
| 333 | return ctx.ModuleName() |
| 334 | } |