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 15package 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 30import ( 31 "sort" 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 39type OverrideModule interface { 40 Module 41 42 getOverridingProperties() []interface{} 43 setOverridingProperties(properties []interface{}) 44 45 getOverrideModuleProperties() *OverrideModuleProperties 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 51} 52 53// Base module struct for override module types 54type OverrideModuleBase struct { 55 moduleProperties OverrideModuleProperties 56 57 overridingProperties []interface{} 58 59 overriddenByPrebuilt bool 60} 61 62type 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 69func (o *OverrideModuleBase) getOverridingProperties() []interface{} { 70 return o.overridingProperties 71} 72 73func (o *OverrideModuleBase) setOverridingProperties(properties []interface{}) { 74 o.overridingProperties = properties 75} 76 77func (o *OverrideModuleBase) getOverrideModuleProperties() *OverrideModuleProperties { 78 return &o.moduleProperties 79} 80 81func (o *OverrideModuleBase) GetOverriddenModuleName() string { 82 return proptools.String(o.moduleProperties.Base) 83} 84 85func (o *OverrideModuleBase) setOverriddenByPrebuilt(overridden bool) { 86 o.overriddenByPrebuilt = overridden 87} 88 89func (o *OverrideModuleBase) getOverriddenByPrebuilt() bool { 90 return o.overriddenByPrebuilt 91} 92 93func 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 100type OverridableModule interface { 101 Module 102 moduleBase() *OverridableModuleBase 103 104 setOverridableProperties(prop []interface{}) 105 106 addOverride(o OverrideModule) 107 getOverrides() []OverrideModule 108 109 override(ctx BaseModuleContext, o OverrideModule) 110 GetOverriddenBy() string 111 112 setOverridesProperty(overridesProperties *[]string) 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) 117} 118 119type overridableModuleProperties struct { 120 OverriddenBy string `blueprint:"mutated"` 121} 122 123// Base module struct for overridable module types 124type OverridableModuleBase struct { 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 139 140 overridableModuleProperties overridableModuleProperties 141} 142 143func InitOverridableModule(m OverridableModule, overridesProperty *[]string) { 144 m.setOverridableProperties(m.(Module).GetProperties()) 145 m.setOverridesProperty(overridesProperty) 146 m.AddProperties(&m.moduleBase().overridableModuleProperties) 147} 148 149func (o *OverridableModuleBase) moduleBase() *OverridableModuleBase { 150 return o 151} 152 153func (b *OverridableModuleBase) setOverridableProperties(prop []interface{}) { 154 b.overridableProperties = prop 155} 156 157func (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. 164func (b *OverridableModuleBase) getOverrides() []OverrideModule { 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() 170 return b.overrides 171} 172 173func (b *OverridableModuleBase) setOverridesProperty(overridesProperty *[]string) { 174 b.overridesProperty = overridesProperty 175} 176 177// Overrides a base module with the given OverrideModule. 178func (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) { 182 err := proptools.ExtendProperties(p, op, nil, proptools.OrderReplace) 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 } 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 } 198 b.overridableModuleProperties.OverriddenBy = o.Name() 199} 200 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. 205func (b *OverridableModuleBase) GetOverriddenBy() string { 206 return b.overridableModuleProperties.OverriddenBy 207} 208 209func (b *OverridableModuleBase) OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) { 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. 214func RegisterOverridePostDepsMutators(ctx RegisterMutatorsContext) { 215 ctx.BottomUp("override_deps", overrideModuleDepsMutator).Parallel() 216 ctx.TopDown("register_override", registerOverrideMutator).Parallel() 217 ctx.BottomUp("perform_override", performOverrideMutator).Parallel() 218 ctx.BottomUp("overridable_deps", overridableModuleDepsMutator).Parallel() 219 ctx.BottomUp("replace_deps_on_override", replaceDepsOnOverridingModuleMutator).Parallel() 220} 221 222type overrideBaseDependencyTag struct { 223 blueprint.BaseDependencyTag 224} 225 226var overrideBaseDepTag overrideBaseDependencyTag 227 228// Adds dependency on the base module to the overriding module so that they can be visited in the 229// next phase. 230func overrideModuleDepsMutator(ctx BottomUpMutatorContext) { 231 if module, ok := ctx.Module().(OverrideModule); ok { 232 base := String(module.getOverrideModuleProperties().Base) 233 if !ctx.OtherModuleExists(base) { 234 ctx.PropertyErrorf("base", "%q is not a valid module name", base) 235 return 236 } 237 // See if there's a prebuilt module that overrides this override module with prefer flag, 238 // in which case we call SkipInstall on the corresponding variant later. 239 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(dep Module) { 240 prebuilt, ok := dep.(PrebuiltInterface) 241 if !ok { 242 panic("PrebuiltDepTag leads to a non-prebuilt module " + dep.Name()) 243 } 244 if prebuilt.Prebuilt().UsePrebuilt() { 245 module.setOverriddenByPrebuilt(true) 246 return 247 } 248 }) 249 ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base) 250 } 251} 252 253// Visits the base module added as a dependency above, checks the module type, and registers the 254// overriding module. 255func registerOverrideMutator(ctx TopDownMutatorContext) { 256 ctx.VisitDirectDepsWithTag(overrideBaseDepTag, func(base Module) { 257 if o, ok := base.(OverridableModule); ok { 258 o.addOverride(ctx.Module().(OverrideModule)) 259 } else { 260 ctx.PropertyErrorf("base", "unsupported base module type") 261 } 262 }) 263} 264 265// Now, goes through all overridable modules, finds all modules overriding them, creates a local 266// variant for each of them, and performs the actual overriding operation by calling override(). 267func performOverrideMutator(ctx BottomUpMutatorContext) { 268 if b, ok := ctx.Module().(OverridableModule); ok { 269 overrides := b.getOverrides() 270 if len(overrides) == 0 { 271 return 272 } 273 variants := make([]string, len(overrides)+1) 274 // The first variant is for the original, non-overridden, base module. 275 variants[0] = "" 276 for i, o := range overrides { 277 variants[i+1] = o.(Module).Name() 278 } 279 mods := ctx.CreateLocalVariations(variants...) 280 // Make the original variation the default one to depend on if no other override module variant 281 // is specified. 282 ctx.AliasVariation(variants[0]) 283 for i, o := range overrides { 284 mods[i+1].(OverridableModule).override(ctx, o) 285 if o.getOverriddenByPrebuilt() { 286 // The overriding module itself, too, is overridden by a prebuilt. Skip its installation. 287 mods[i+1].SkipInstall() 288 } 289 } 290 } else if o, ok := ctx.Module().(OverrideModule); ok { 291 // Create a variant of the overriding module with its own name. This matches the above local 292 // variant name rule for overridden modules, and thus allows ReplaceDependencies to match the 293 // two. 294 ctx.CreateLocalVariations(o.Name()) 295 // To allow dependencies to be added without having to know the above variation. 296 ctx.AliasVariation(o.Name()) 297 } 298} 299 300func overridableModuleDepsMutator(ctx BottomUpMutatorContext) { 301 if b, ok := ctx.Module().(OverridableModule); ok { 302 b.OverridablePropertiesDepsMutator(ctx) 303 } 304} 305 306func replaceDepsOnOverridingModuleMutator(ctx BottomUpMutatorContext) { 307 if b, ok := ctx.Module().(OverridableModule); ok { 308 if o := b.GetOverriddenBy(); o != "" { 309 // Redirect dependencies on the overriding module to this overridden module. Overriding 310 // modules are basically pseudo modules, and all build actions are associated to overridden 311 // modules. Therefore, dependencies on overriding modules need to be forwarded there as well. 312 ctx.ReplaceDependencies(o) 313 } 314 } 315} 316