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 "fmt" 32 "sort" 33 "sync" 34 35 "github.com/google/blueprint" 36 "github.com/google/blueprint/proptools" 37) 38 39// Interface for override module types, e.g. override_android_app, override_apex 40type OverrideModule interface { 41 Module 42 43 getOverridingProperties() []interface{} 44 setOverridingProperties(properties []interface{}) 45 46 getOverrideModuleProperties() *OverrideModuleProperties 47 48 // Internal funcs to handle interoperability between override modules and prebuilts. 49 // i.e. cases where an overriding module, too, is overridden by a prebuilt module. 50 setOverriddenByPrebuilt(prebuilt Module) 51 getOverriddenByPrebuilt() Module 52 53 // Directory containing the Blueprint definition of the overriding module 54 setModuleDir(string) 55 ModuleDir() string 56} 57 58// Base module struct for override module types 59type OverrideModuleBase struct { 60 moduleProperties OverrideModuleProperties 61 62 overridingProperties []interface{} 63 64 overriddenByPrebuilt Module 65 66 moduleDir string 67} 68 69type OverrideModuleProperties struct { 70 // Name of the base module to be overridden 71 Base *string 72 73 // TODO(jungjw): Add an optional override_name bool flag. 74} 75 76func (o *OverrideModuleBase) setModuleDir(d string) { 77 o.moduleDir = d 78} 79 80func (o *OverrideModuleBase) ModuleDir() string { 81 return o.moduleDir 82} 83 84func (o *OverrideModuleBase) getOverridingProperties() []interface{} { 85 return o.overridingProperties 86} 87 88func (o *OverrideModuleBase) setOverridingProperties(properties []interface{}) { 89 o.overridingProperties = properties 90} 91 92func (o *OverrideModuleBase) getOverrideModuleProperties() *OverrideModuleProperties { 93 return &o.moduleProperties 94} 95 96func (o *OverrideModuleBase) GetOverriddenModuleName() string { 97 return proptools.String(o.moduleProperties.Base) 98} 99 100func (o *OverrideModuleBase) setOverriddenByPrebuilt(prebuilt Module) { 101 o.overriddenByPrebuilt = prebuilt 102} 103 104func (o *OverrideModuleBase) getOverriddenByPrebuilt() Module { 105 return o.overriddenByPrebuilt 106} 107 108func InitOverrideModule(m OverrideModule) { 109 m.setOverridingProperties(m.GetProperties()) 110 111 m.AddProperties(m.getOverrideModuleProperties()) 112} 113 114// Interface for overridable module types, e.g. android_app, apex 115type OverridableModule interface { 116 Module 117 moduleBase() *OverridableModuleBase 118 119 setOverridableProperties(prop []interface{}) 120 121 addOverride(o OverrideModule) 122 getOverrides() []OverrideModule 123 124 override(ctx BaseModuleContext, bm OverridableModule, o OverrideModule) 125 GetOverriddenBy() string 126 GetOverriddenByModuleDir() string 127 128 setOverridesProperty(overridesProperties *[]string) 129 130 // Due to complications with incoming dependencies, overrides are processed after DepsMutator. 131 // So, overridable properties need to be handled in a separate, dedicated deps mutator. 132 OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) 133} 134 135type overridableModuleProperties struct { 136 OverriddenBy string `blueprint:"mutated"` 137 OverriddenByModuleDir string `blueprint:"mutated"` 138} 139 140// Base module struct for overridable module types 141type OverridableModuleBase struct { 142 // List of OverrideModules that override this base module 143 overrides []OverrideModule 144 // Used to parallelize registerOverrideMutator executions. Note that only addOverride locks this 145 // mutex. It is because addOverride and getOverride are used in different mutators, and so are 146 // guaranteed to be not mixed. (And, getOverride only reads from overrides, and so don't require 147 // mutex locking.) 148 overridesLock sync.Mutex 149 150 overridableProperties []interface{} 151 152 // If an overridable module has a property to list other modules that itself overrides, it should 153 // set this to a pointer to the property through the InitOverridableModule function, so that 154 // override information is propagated and aggregated correctly. 155 overridesProperty *[]string 156 157 overridableModuleProperties overridableModuleProperties 158} 159 160func InitOverridableModule(m OverridableModule, overridesProperty *[]string) { 161 m.setOverridableProperties(m.(Module).GetProperties()) 162 m.setOverridesProperty(overridesProperty) 163 m.AddProperties(&m.moduleBase().overridableModuleProperties) 164} 165 166func (o *OverridableModuleBase) moduleBase() *OverridableModuleBase { 167 return o 168} 169 170func (b *OverridableModuleBase) setOverridableProperties(prop []interface{}) { 171 b.overridableProperties = prop 172} 173 174func (b *OverridableModuleBase) addOverride(o OverrideModule) { 175 b.overridesLock.Lock() 176 b.overrides = append(b.overrides, o) 177 b.overridesLock.Unlock() 178} 179 180// Should NOT be used in the same mutator as addOverride. 181func (b *OverridableModuleBase) getOverrides() []OverrideModule { 182 b.overridesLock.Lock() 183 sort.Slice(b.overrides, func(i, j int) bool { 184 return b.overrides[i].Name() < b.overrides[j].Name() 185 }) 186 b.overridesLock.Unlock() 187 return b.overrides 188} 189 190func (b *OverridableModuleBase) setOverridesProperty(overridesProperty *[]string) { 191 b.overridesProperty = overridesProperty 192} 193 194// Overrides a base module with the given OverrideModule. 195func (b *OverridableModuleBase) override(ctx BaseModuleContext, bm OverridableModule, o OverrideModule) { 196 for _, p := range b.overridableProperties { 197 for _, op := range o.getOverridingProperties() { 198 if proptools.TypeEqual(p, op) { 199 err := proptools.ExtendProperties(p, op, nil, proptools.OrderReplace) 200 if err != nil { 201 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { 202 ctx.OtherModulePropertyErrorf(bm, propertyErr.Property, "%s", propertyErr.Err.Error()) 203 } else { 204 panic(err) 205 } 206 } 207 } 208 } 209 } 210 // Adds the base module to the overrides property, if exists, of the overriding module. See the 211 // comment on OverridableModuleBase.overridesProperty for details. 212 if b.overridesProperty != nil { 213 *b.overridesProperty = append(*b.overridesProperty, ctx.OtherModuleName(bm)) 214 } 215 b.overridableModuleProperties.OverriddenBy = o.Name() 216 b.overridableModuleProperties.OverriddenByModuleDir = o.ModuleDir() 217} 218 219// GetOverriddenBy returns the name of the override module that has overridden this module. 220// For example, if an override module foo has its 'base' property set to bar, then another local variant 221// of bar is created and its properties are overriden by foo. This method returns bar when called from 222// the new local variant. It returns "" when called from the original variant of bar. 223func (b *OverridableModuleBase) GetOverriddenBy() string { 224 return b.overridableModuleProperties.OverriddenBy 225} 226 227func (b *OverridableModuleBase) GetOverriddenByModuleDir() string { 228 return b.overridableModuleProperties.OverriddenByModuleDir 229} 230 231func (b *OverridableModuleBase) OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) { 232} 233 234// Mutators for override/overridable modules. All the fun happens in these functions. It is critical 235// to keep them in this order and not put any order mutators between them. 236func RegisterOverridePostDepsMutators(ctx RegisterMutatorsContext) { 237 ctx.BottomUp("override_deps", overrideModuleDepsMutator).Parallel() 238 ctx.Transition("override", &overrideTransitionMutator{}) 239 // overridableModuleDepsMutator calls OverridablePropertiesDepsMutator so that overridable modules can 240 // add deps from overridable properties. 241 ctx.BottomUp("overridable_deps", overridableModuleDepsMutator).Parallel() 242 // Because overridableModuleDepsMutator is run after PrebuiltPostDepsMutator, 243 // prebuilt's ReplaceDependencies doesn't affect to those deps added by overridable properties. 244 // By running PrebuiltPostDepsMutator again after overridableModuleDepsMutator, deps via overridable properties 245 // can be replaced with prebuilts. 246 ctx.BottomUp("replace_deps_on_prebuilts_for_overridable_deps_again", PrebuiltPostDepsMutator).Parallel() 247 ctx.BottomUp("replace_deps_on_override", replaceDepsOnOverridingModuleMutator).Parallel() 248} 249 250type overrideBaseDependencyTag struct { 251 blueprint.BaseDependencyTag 252} 253 254var overrideBaseDepTag overrideBaseDependencyTag 255 256// Override module should always override the source module. 257// Overrides are implemented as a variant of the overridden module, and the build actions are created in the 258// module context of the overridden module. 259// If we replace override module with the prebuilt of the overridden module, `GenerateAndroidBuildActions` for 260// the override module will have a very different meaning. 261func (tag overrideBaseDependencyTag) ReplaceSourceWithPrebuilt() bool { 262 return false 263} 264 265// Adds dependency on the base module to the overriding module so that they can be visited in the 266// next phase. 267func overrideModuleDepsMutator(ctx BottomUpMutatorContext) { 268 if module, ok := ctx.Module().(OverrideModule); ok { 269 base := String(module.getOverrideModuleProperties().Base) 270 if !ctx.OtherModuleExists(base) { 271 ctx.PropertyErrorf("base", "%q is not a valid module name", base) 272 return 273 } 274 baseModule := ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base)[0] 275 if o, ok := baseModule.(OverridableModule); ok { 276 overrideModule := ctx.Module().(OverrideModule) 277 overrideModule.setModuleDir(ctx.ModuleDir()) 278 o.addOverride(overrideModule) 279 } 280 } 281} 282 283// Now, goes through all overridable modules, finds all modules overriding them, creates a local 284// variant for each of them, and performs the actual overriding operation by calling override(). 285type overrideTransitionMutator struct{} 286 287func (overrideTransitionMutator) Split(ctx BaseModuleContext) []string { 288 if b, ok := ctx.Module().(OverridableModule); ok { 289 overrides := b.getOverrides() 290 if len(overrides) == 0 { 291 return []string{""} 292 } 293 variants := make([]string, len(overrides)+1) 294 // The first variant is for the original, non-overridden, base module. 295 variants[0] = "" 296 for i, o := range overrides { 297 variants[i+1] = o.(Module).Name() 298 } 299 return variants 300 } else if o, ok := ctx.Module().(OverrideModule); ok { 301 // Create a variant of the overriding module with its own name. This matches the above local 302 // variant name rule for overridden modules, and thus allows ReplaceDependencies to match the 303 // two. 304 return []string{o.Name()} 305 } 306 307 return []string{""} 308} 309 310func (overrideTransitionMutator) OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string { 311 if o, ok := ctx.Module().(OverrideModule); ok { 312 if ctx.DepTag() == overrideBaseDepTag { 313 return o.Name() 314 } 315 } 316 317 // Variations are always local and shouldn't affect the variant used for dependencies 318 return "" 319} 320 321func (overrideTransitionMutator) IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string { 322 if _, ok := ctx.Module().(OverridableModule); ok { 323 return incomingVariation 324 } else if o, ok := ctx.Module().(OverrideModule); ok { 325 // To allow dependencies to be added without having to know the variation. 326 return o.Name() 327 } 328 329 return "" 330} 331 332func (overrideTransitionMutator) Mutate(ctx BottomUpMutatorContext, variation string) { 333 if o, ok := ctx.Module().(OverrideModule); ok { 334 overridableDeps := ctx.GetDirectDepsWithTag(overrideBaseDepTag) 335 if len(overridableDeps) > 1 { 336 panic(fmt.Errorf("expected a single dependency with overrideBaseDepTag, found %q", overridableDeps)) 337 } else if len(overridableDeps) == 1 { 338 b := overridableDeps[0].(OverridableModule) 339 b.override(ctx, b, o) 340 341 checkPrebuiltReplacesOverride(ctx, b) 342 } 343 } 344} 345 346func checkPrebuiltReplacesOverride(ctx BottomUpMutatorContext, b OverridableModule) { 347 // See if there's a prebuilt module that overrides this override module with prefer flag, 348 // in which case we call HideFromMake on the corresponding variant later. 349 prebuiltDeps := ctx.GetDirectDepsWithTag(PrebuiltDepTag) 350 for _, prebuiltDep := range prebuiltDeps { 351 prebuilt := GetEmbeddedPrebuilt(prebuiltDep) 352 if prebuilt == nil { 353 panic("PrebuiltDepTag leads to a non-prebuilt module " + prebuiltDep.Name()) 354 } 355 if prebuilt.UsePrebuilt() { 356 // The overriding module itself, too, is overridden by a prebuilt. 357 // Perform the same check for replacement 358 checkInvariantsForSourceAndPrebuilt(ctx, b, prebuiltDep) 359 // Copy the flag and hide it in make 360 b.ReplacedByPrebuilt() 361 } 362 } 363} 364 365func overridableModuleDepsMutator(ctx BottomUpMutatorContext) { 366 if b, ok := ctx.Module().(OverridableModule); ok && b.Enabled(ctx) { 367 b.OverridablePropertiesDepsMutator(ctx) 368 } 369} 370 371func replaceDepsOnOverridingModuleMutator(ctx BottomUpMutatorContext) { 372 if b, ok := ctx.Module().(OverridableModule); ok { 373 if o := b.GetOverriddenBy(); o != "" { 374 // Redirect dependencies on the overriding module to this overridden module. Overriding 375 // modules are basically pseudo modules, and all build actions are associated to overridden 376 // modules. Therefore, dependencies on overriding modules need to be forwarded there as well. 377 ctx.ReplaceDependencies(o) 378 } 379 } 380} 381