blob: 0a7e29491fb3abf575a8f1c3b51f5028aae2abc3 [file] [log] [blame]
Jaewoong Jung525443a2019-02-28 15:35:54 -08001// 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 (
Dan Willemsen1a8c8562020-06-29 23:49:34 -070031 "sort"
Jaewoong Jung525443a2019-02-28 15:35:54 -080032 "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
Jaewoong Jung1e362da2020-06-23 07:58:13 -070046
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 Jung525443a2019-02-28 15:35:54 -080051}
52
53// Base module struct for override module types
54type OverrideModuleBase struct {
55 moduleProperties OverrideModuleProperties
56
57 overridingProperties []interface{}
Jaewoong Jung1e362da2020-06-23 07:58:13 -070058
59 overriddenByPrebuilt bool
Jaewoong Jung525443a2019-02-28 15:35:54 -080060}
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
Jiyong Park5d790c32019-11-15 18:40:32 +090081func (o *OverrideModuleBase) GetOverriddenModuleName() string {
82 return proptools.String(o.moduleProperties.Base)
83}
84
Jaewoong Jung1e362da2020-06-23 07:58:13 -070085func (o *OverrideModuleBase) setOverriddenByPrebuilt(overridden bool) {
86 o.overriddenByPrebuilt = overridden
87}
88
89func (o *OverrideModuleBase) getOverriddenByPrebuilt() bool {
90 return o.overriddenByPrebuilt
91}
92
Jaewoong Jung525443a2019-02-28 15:35:54 -080093func 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 {
Jiyong Park317645e2019-12-05 13:20:58 +0900101 Module
102 moduleBase() *OverridableModuleBase
103
Jaewoong Jung525443a2019-02-28 15:35:54 -0800104 setOverridableProperties(prop []interface{})
105
106 addOverride(o OverrideModule)
107 getOverrides() []OverrideModule
108
109 override(ctx BaseModuleContext, o OverrideModule)
Jiyong Park317645e2019-12-05 13:20:58 +0900110 GetOverriddenBy() string
Jaewoong Jung525443a2019-02-28 15:35:54 -0800111
112 setOverridesProperty(overridesProperties *[]string)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700113
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 Jung525443a2019-02-28 15:35:54 -0800117}
118
Jiyong Park317645e2019-12-05 13:20:58 +0900119type overridableModuleProperties struct {
120 OverriddenBy string `blueprint:"mutated"`
121}
122
Jaewoong Jung525443a2019-02-28 15:35:54 -0800123// Base module struct for overridable module types
124type OverridableModuleBase struct {
Jaewoong Jung525443a2019-02-28 15:35:54 -0800125 // 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 Jungb639a6a2019-05-10 15:16:29 -0700139
Jaewoong Jungb64dd002020-01-10 13:26:00 -0800140 overridableModuleProperties overridableModuleProperties
Jaewoong Jung525443a2019-02-28 15:35:54 -0800141}
142
143func InitOverridableModule(m OverridableModule, overridesProperty *[]string) {
144 m.setOverridableProperties(m.(Module).GetProperties())
145 m.setOverridesProperty(overridesProperty)
Jaewoong Jungb64dd002020-01-10 13:26:00 -0800146 m.AddProperties(&m.moduleBase().overridableModuleProperties)
Jiyong Park317645e2019-12-05 13:20:58 +0900147}
148
149func (o *OverridableModuleBase) moduleBase() *OverridableModuleBase {
150 return o
Jaewoong Jung525443a2019-02-28 15:35:54 -0800151}
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 {
Dan Willemsen1a8c8562020-06-29 23:49:34 -0700165 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 Jung525443a2019-02-28 15:35:54 -0800170 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) {
Jiyong Park5d790c32019-11-15 18:40:32 +0900182 err := proptools.ExtendProperties(p, op, nil, proptools.OrderReplace)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800183 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 Jung7abcf8e2019-12-19 17:32:06 -0800193 // 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 Jungb64dd002020-01-10 13:26:00 -0800198 b.overridableModuleProperties.OverriddenBy = o.Name()
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700199}
200
Jiyong Park317645e2019-12-05 13:20:58 +0900201// 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 {
Jaewoong Jungb64dd002020-01-10 13:26:00 -0800206 return b.overridableModuleProperties.OverriddenBy
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700207}
208
209func (b *OverridableModuleBase) OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) {
Jaewoong Jung525443a2019-02-28 15:35:54 -0800210}
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 Jungb639a6a2019-05-10 15:16:29 -0700214func RegisterOverridePostDepsMutators(ctx RegisterMutatorsContext) {
Jaewoong Jung525443a2019-02-28 15:35:54 -0800215 ctx.BottomUp("override_deps", overrideModuleDepsMutator).Parallel()
216 ctx.TopDown("register_override", registerOverrideMutator).Parallel()
217 ctx.BottomUp("perform_override", performOverrideMutator).Parallel()
Jooyung Han86feead2021-03-08 13:11:48 +0900218 // overridableModuleDepsMutator calls OverridablePropertiesDepsMutator so that overridable modules can
219 // add deps from overridable properties.
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700220 ctx.BottomUp("overridable_deps", overridableModuleDepsMutator).Parallel()
Jooyung Han86feead2021-03-08 13:11:48 +0900221 // 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 Jung26dedd32019-06-06 08:45:58 -0700226 ctx.BottomUp("replace_deps_on_override", replaceDepsOnOverridingModuleMutator).Parallel()
Jaewoong Jung525443a2019-02-28 15:35:54 -0800227}
228
229type overrideBaseDependencyTag struct {
230 blueprint.BaseDependencyTag
231}
232
233var 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.
237func overrideModuleDepsMutator(ctx BottomUpMutatorContext) {
238 if module, ok := ctx.Module().(OverrideModule); ok {
Jooyung Han5c17ac02020-06-20 19:38:21 +0900239 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 Jung1e362da2020-06-23 07:58:13 -0700244 // See if there's a prebuilt module that overrides this override module with prefer flag,
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800245 // in which case we call HideFromMake on the corresponding variant later.
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700246 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(dep Module) {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100247 prebuilt := GetEmbeddedPrebuilt(dep)
248 if prebuilt == nil {
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700249 panic("PrebuiltDepTag leads to a non-prebuilt module " + dep.Name())
250 }
Paul Duffinf7c99f52021-04-28 10:41:21 +0100251 if prebuilt.UsePrebuilt() {
Jaewoong Jung1e362da2020-06-23 07:58:13 -0700252 module.setOverriddenByPrebuilt(true)
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700253 return
254 }
255 })
Jaewoong Jung1e362da2020-06-23 07:58:13 -0700256 ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800257 }
258}
259
260// Visits the base module added as a dependency above, checks the module type, and registers the
261// overriding module.
262func 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().
274func 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 Jung26dedd32019-06-06 08:45:58 -0700287 // 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 Jung525443a2019-02-28 15:35:54 -0800290 for i, o := range overrides {
291 mods[i+1].(OverridableModule).override(ctx, o)
Jaewoong Jung1e362da2020-06-23 07:58:13 -0700292 if o.getOverriddenByPrebuilt() {
293 // The overriding module itself, too, is overridden by a prebuilt. Skip its installation.
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800294 mods[i+1].HideFromMake()
Jaewoong Jung1e362da2020-06-23 07:58:13 -0700295 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800296 }
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700297 } 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 Jung26dedd32019-06-06 08:45:58 -0700302 // To allow dependencies to be added without having to know the above variation.
303 ctx.AliasVariation(o.Name())
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700304 }
305}
306
307func overridableModuleDepsMutator(ctx BottomUpMutatorContext) {
308 if b, ok := ctx.Module().(OverridableModule); ok {
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700309 b.OverridablePropertiesDepsMutator(ctx)
310 }
311}
312
313func replaceDepsOnOverridingModuleMutator(ctx BottomUpMutatorContext) {
314 if b, ok := ctx.Module().(OverridableModule); ok {
Jiyong Park317645e2019-12-05 13:20:58 +0900315 if o := b.GetOverriddenBy(); o != "" {
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700316 // 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 Jung525443a2019-02-28 15:35:54 -0800321 }
322}
Colin Crosscb6143a2020-08-14 17:39:29 -0700323
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.
327func 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}