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 "sync" 32 33 "github.com/google/blueprint" 34 "github.com/google/blueprint/proptools" 35) 36 37// Interface for override module types, e.g. override_android_app, override_apex 38type OverrideModule interface { 39 Module 40 41 getOverridingProperties() []interface{} 42 setOverridingProperties(properties []interface{}) 43 44 getOverrideModuleProperties() *OverrideModuleProperties 45 46 // Internal funcs to handle interoperability between override modules and prebuilts. 47 // i.e. cases where an overriding module, too, is overridden by a prebuilt module. 48 setOverriddenByPrebuilt(overridden bool) 49 getOverriddenByPrebuilt() bool 50} 51 52// Base module struct for override module types 53type OverrideModuleBase struct { 54 moduleProperties OverrideModuleProperties 55 56 overridingProperties []interface{} 57 58 overriddenByPrebuilt bool 59} 60 61type OverrideModuleProperties struct { 62 // Name of the base module to be overridden 63 Base *string 64 65 // TODO(jungjw): Add an optional override_name bool flag. 66} 67 68func (o *OverrideModuleBase) getOverridingProperties() []interface{} { 69 return o.overridingProperties 70} 71 72func (o *OverrideModuleBase) setOverridingProperties(properties []interface{}) { 73 o.overridingProperties = properties 74} 75 76func (o *OverrideModuleBase) getOverrideModuleProperties() *OverrideModuleProperties { 77 return &o.moduleProperties 78} 79 80func (o *OverrideModuleBase) GetOverriddenModuleName() string { 81 return proptools.String(o.moduleProperties.Base) 82} 83 84func (o *OverrideModuleBase) setOverriddenByPrebuilt(overridden bool) { 85 o.overriddenByPrebuilt = overridden 86} 87 88func (o *OverrideModuleBase) getOverriddenByPrebuilt() bool { 89 return o.overriddenByPrebuilt 90} 91 92func InitOverrideModule(m OverrideModule) { 93 m.setOverridingProperties(m.GetProperties()) 94 95 m.AddProperties(m.getOverrideModuleProperties()) 96} 97 98// Interface for overridable module types, e.g. android_app, apex 99type OverridableModule interface { 100 Module 101 moduleBase() *OverridableModuleBase 102 103 setOverridableProperties(prop []interface{}) 104 105 addOverride(o OverrideModule) 106 getOverrides() []OverrideModule 107 108 override(ctx BaseModuleContext, o OverrideModule) 109 GetOverriddenBy() string 110 111 setOverridesProperty(overridesProperties *[]string) 112 113 // Due to complications with incoming dependencies, overrides are processed after DepsMutator. 114 // So, overridable properties need to be handled in a separate, dedicated deps mutator. 115 OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) 116} 117 118type overridableModuleProperties struct { 119 OverriddenBy string `blueprint:"mutated"` 120} 121 122// Base module struct for overridable module types 123type OverridableModuleBase struct { 124 // List of OverrideModules that override this base module 125 overrides []OverrideModule 126 // Used to parallelize registerOverrideMutator executions. Note that only addOverride locks this 127 // mutex. It is because addOverride and getOverride are used in different mutators, and so are 128 // guaranteed to be not mixed. (And, getOverride only reads from overrides, and so don't require 129 // mutex locking.) 130 overridesLock sync.Mutex 131 132 overridableProperties []interface{} 133 134 // If an overridable module has a property to list other modules that itself overrides, it should 135 // set this to a pointer to the property through the InitOverridableModule function, so that 136 // override information is propagated and aggregated correctly. 137 overridesProperty *[]string 138 139 overridableModuleProperties overridableModuleProperties 140} 141 142func InitOverridableModule(m OverridableModule, overridesProperty *[]string) { 143 m.setOverridableProperties(m.(Module).GetProperties()) 144 m.setOverridesProperty(overridesProperty) 145 m.AddProperties(&m.moduleBase().overridableModuleProperties) 146} 147 148func (o *OverridableModuleBase) moduleBase() *OverridableModuleBase { 149 return o 150} 151 152func (b *OverridableModuleBase) setOverridableProperties(prop []interface{}) { 153 b.overridableProperties = prop 154} 155 156func (b *OverridableModuleBase) addOverride(o OverrideModule) { 157 b.overridesLock.Lock() 158 b.overrides = append(b.overrides, o) 159 b.overridesLock.Unlock() 160} 161 162// Should NOT be used in the same mutator as addOverride. 163func (b *OverridableModuleBase) getOverrides() []OverrideModule { 164 return b.overrides 165} 166 167func (b *OverridableModuleBase) setOverridesProperty(overridesProperty *[]string) { 168 b.overridesProperty = overridesProperty 169} 170 171// Overrides a base module with the given OverrideModule. 172func (b *OverridableModuleBase) override(ctx BaseModuleContext, o OverrideModule) { 173 for _, p := range b.overridableProperties { 174 for _, op := range o.getOverridingProperties() { 175 if proptools.TypeEqual(p, op) { 176 err := proptools.ExtendProperties(p, op, nil, proptools.OrderReplace) 177 if err != nil { 178 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { 179 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) 180 } else { 181 panic(err) 182 } 183 } 184 } 185 } 186 } 187 // Adds the base module to the overrides property, if exists, of the overriding module. See the 188 // comment on OverridableModuleBase.overridesProperty for details. 189 if b.overridesProperty != nil { 190 *b.overridesProperty = append(*b.overridesProperty, ctx.ModuleName()) 191 } 192 b.overridableModuleProperties.OverriddenBy = o.Name() 193} 194 195// GetOverriddenBy returns the name of the override module that has overridden this module. 196// For example, if an override module foo has its 'base' property set to bar, then another local variant 197// of bar is created and its properties are overriden by foo. This method returns bar when called from 198// the new local variant. It returns "" when called from the original variant of bar. 199func (b *OverridableModuleBase) GetOverriddenBy() string { 200 return b.overridableModuleProperties.OverriddenBy 201} 202 203func (b *OverridableModuleBase) OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) { 204} 205 206// Mutators for override/overridable modules. All the fun happens in these functions. It is critical 207// to keep them in this order and not put any order mutators between them. 208func RegisterOverridePostDepsMutators(ctx RegisterMutatorsContext) { 209 ctx.BottomUp("override_deps", overrideModuleDepsMutator).Parallel() 210 ctx.TopDown("register_override", registerOverrideMutator).Parallel() 211 ctx.BottomUp("perform_override", performOverrideMutator).Parallel() 212 ctx.BottomUp("overridable_deps", overridableModuleDepsMutator).Parallel() 213 ctx.BottomUp("replace_deps_on_override", replaceDepsOnOverridingModuleMutator).Parallel() 214} 215 216type overrideBaseDependencyTag struct { 217 blueprint.BaseDependencyTag 218} 219 220var overrideBaseDepTag overrideBaseDependencyTag 221 222// Adds dependency on the base module to the overriding module so that they can be visited in the 223// next phase. 224func overrideModuleDepsMutator(ctx BottomUpMutatorContext) { 225 if module, ok := ctx.Module().(OverrideModule); ok { 226 // See if there's a prebuilt module that overrides this override module with prefer flag, 227 // in which case we call SkipInstall on the corresponding variant later. 228 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(dep Module) { 229 prebuilt, ok := dep.(PrebuiltInterface) 230 if !ok { 231 panic("PrebuiltDepTag leads to a non-prebuilt module " + dep.Name()) 232 } 233 if prebuilt.Prebuilt().UsePrebuilt() { 234 module.setOverriddenByPrebuilt(true) 235 return 236 } 237 }) 238 ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base) 239 } 240} 241 242// Visits the base module added as a dependency above, checks the module type, and registers the 243// overriding module. 244func registerOverrideMutator(ctx TopDownMutatorContext) { 245 ctx.VisitDirectDepsWithTag(overrideBaseDepTag, func(base Module) { 246 if o, ok := base.(OverridableModule); ok { 247 o.addOverride(ctx.Module().(OverrideModule)) 248 } else { 249 ctx.PropertyErrorf("base", "unsupported base module type") 250 } 251 }) 252} 253 254// Now, goes through all overridable modules, finds all modules overriding them, creates a local 255// variant for each of them, and performs the actual overriding operation by calling override(). 256func performOverrideMutator(ctx BottomUpMutatorContext) { 257 if b, ok := ctx.Module().(OverridableModule); ok { 258 overrides := b.getOverrides() 259 if len(overrides) == 0 { 260 return 261 } 262 variants := make([]string, len(overrides)+1) 263 // The first variant is for the original, non-overridden, base module. 264 variants[0] = "" 265 for i, o := range overrides { 266 variants[i+1] = o.(Module).Name() 267 } 268 mods := ctx.CreateLocalVariations(variants...) 269 // Make the original variation the default one to depend on if no other override module variant 270 // is specified. 271 ctx.AliasVariation(variants[0]) 272 for i, o := range overrides { 273 mods[i+1].(OverridableModule).override(ctx, o) 274 if o.getOverriddenByPrebuilt() { 275 // The overriding module itself, too, is overridden by a prebuilt. Skip its installation. 276 mods[i+1].SkipInstall() 277 } 278 } 279 } else if o, ok := ctx.Module().(OverrideModule); ok { 280 // Create a variant of the overriding module with its own name. This matches the above local 281 // variant name rule for overridden modules, and thus allows ReplaceDependencies to match the 282 // two. 283 ctx.CreateLocalVariations(o.Name()) 284 // To allow dependencies to be added without having to know the above variation. 285 ctx.AliasVariation(o.Name()) 286 } 287} 288 289func overridableModuleDepsMutator(ctx BottomUpMutatorContext) { 290 if b, ok := ctx.Module().(OverridableModule); ok { 291 b.OverridablePropertiesDepsMutator(ctx) 292 } 293} 294 295func replaceDepsOnOverridingModuleMutator(ctx BottomUpMutatorContext) { 296 if b, ok := ctx.Module().(OverridableModule); ok { 297 if o := b.GetOverriddenBy(); o != "" { 298 // Redirect dependencies on the overriding module to this overridden module. Overriding 299 // modules are basically pseudo modules, and all build actions are associated to overridden 300 // modules. Therefore, dependencies on overriding modules need to be forwarded there as well. 301 ctx.ReplaceDependencies(o) 302 } 303 } 304} 305