blob: 51e74d489479a0f5aae149b26b63798fa6feff4c [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()
Jaewoong Jung525443a2019-02-28 15:35:54 -0800216 ctx.BottomUp("perform_override", performOverrideMutator).Parallel()
Jooyung Han86feead2021-03-08 13:11:48 +0900217 // overridableModuleDepsMutator calls OverridablePropertiesDepsMutator so that overridable modules can
218 // add deps from overridable properties.
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700219 ctx.BottomUp("overridable_deps", overridableModuleDepsMutator).Parallel()
Jooyung Han86feead2021-03-08 13:11:48 +0900220 // Because overridableModuleDepsMutator is run after PrebuiltPostDepsMutator,
221 // prebuilt's ReplaceDependencies doesn't affect to those deps added by overridable properties.
222 // By running PrebuiltPostDepsMutator again after overridableModuleDepsMutator, deps via overridable properties
223 // can be replaced with prebuilts.
224 ctx.BottomUp("replace_deps_on_prebuilts_for_overridable_deps_again", PrebuiltPostDepsMutator).Parallel()
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700225 ctx.BottomUp("replace_deps_on_override", replaceDepsOnOverridingModuleMutator).Parallel()
Jaewoong Jung525443a2019-02-28 15:35:54 -0800226}
227
228type overrideBaseDependencyTag struct {
229 blueprint.BaseDependencyTag
230}
231
232var overrideBaseDepTag overrideBaseDependencyTag
233
234// Adds dependency on the base module to the overriding module so that they can be visited in the
235// next phase.
236func overrideModuleDepsMutator(ctx BottomUpMutatorContext) {
237 if module, ok := ctx.Module().(OverrideModule); ok {
Jooyung Han5c17ac02020-06-20 19:38:21 +0900238 base := String(module.getOverrideModuleProperties().Base)
239 if !ctx.OtherModuleExists(base) {
240 ctx.PropertyErrorf("base", "%q is not a valid module name", base)
241 return
242 }
Jaewoong Jung1e362da2020-06-23 07:58:13 -0700243 // See if there's a prebuilt module that overrides this override module with prefer flag,
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800244 // in which case we call HideFromMake on the corresponding variant later.
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700245 ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(dep Module) {
Paul Duffinf7c99f52021-04-28 10:41:21 +0100246 prebuilt := GetEmbeddedPrebuilt(dep)
247 if prebuilt == nil {
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700248 panic("PrebuiltDepTag leads to a non-prebuilt module " + dep.Name())
249 }
Paul Duffinf7c99f52021-04-28 10:41:21 +0100250 if prebuilt.UsePrebuilt() {
Jaewoong Jung1e362da2020-06-23 07:58:13 -0700251 module.setOverriddenByPrebuilt(true)
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700252 return
253 }
254 })
Colin Cross41026662021-05-06 11:59:38 -0700255 baseModule := ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base)[0]
256 if o, ok := baseModule.(OverridableModule); ok {
Jaewoong Jung525443a2019-02-28 15:35:54 -0800257 o.addOverride(ctx.Module().(OverrideModule))
Jaewoong Jung525443a2019-02-28 15:35:54 -0800258 }
Colin Cross41026662021-05-06 11:59:38 -0700259 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800260}
261
262// Now, goes through all overridable modules, finds all modules overriding them, creates a local
263// variant for each of them, and performs the actual overriding operation by calling override().
264func performOverrideMutator(ctx BottomUpMutatorContext) {
265 if b, ok := ctx.Module().(OverridableModule); ok {
266 overrides := b.getOverrides()
267 if len(overrides) == 0 {
268 return
269 }
270 variants := make([]string, len(overrides)+1)
271 // The first variant is for the original, non-overridden, base module.
272 variants[0] = ""
273 for i, o := range overrides {
274 variants[i+1] = o.(Module).Name()
275 }
276 mods := ctx.CreateLocalVariations(variants...)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700277 // Make the original variation the default one to depend on if no other override module variant
278 // is specified.
279 ctx.AliasVariation(variants[0])
Jaewoong Jung525443a2019-02-28 15:35:54 -0800280 for i, o := range overrides {
281 mods[i+1].(OverridableModule).override(ctx, o)
Jaewoong Jung1e362da2020-06-23 07:58:13 -0700282 if o.getOverriddenByPrebuilt() {
283 // The overriding module itself, too, is overridden by a prebuilt. Skip its installation.
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800284 mods[i+1].HideFromMake()
Jaewoong Jung1e362da2020-06-23 07:58:13 -0700285 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800286 }
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700287 } else if o, ok := ctx.Module().(OverrideModule); ok {
288 // Create a variant of the overriding module with its own name. This matches the above local
289 // variant name rule for overridden modules, and thus allows ReplaceDependencies to match the
290 // two.
291 ctx.CreateLocalVariations(o.Name())
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700292 // To allow dependencies to be added without having to know the above variation.
293 ctx.AliasVariation(o.Name())
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700294 }
295}
296
297func overridableModuleDepsMutator(ctx BottomUpMutatorContext) {
Daniel Norman5a3ce132021-08-26 15:44:43 -0700298 if b, ok := ctx.Module().(OverridableModule); ok && b.Enabled() {
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700299 b.OverridablePropertiesDepsMutator(ctx)
300 }
301}
302
303func replaceDepsOnOverridingModuleMutator(ctx BottomUpMutatorContext) {
304 if b, ok := ctx.Module().(OverridableModule); ok {
Jiyong Park317645e2019-12-05 13:20:58 +0900305 if o := b.GetOverriddenBy(); o != "" {
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700306 // Redirect dependencies on the overriding module to this overridden module. Overriding
307 // modules are basically pseudo modules, and all build actions are associated to overridden
308 // modules. Therefore, dependencies on overriding modules need to be forwarded there as well.
309 ctx.ReplaceDependencies(o)
310 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800311 }
312}
Colin Crosscb6143a2020-08-14 17:39:29 -0700313
314// ModuleNameWithPossibleOverride returns the name of the OverrideModule that overrides the current
315// variant of this OverridableModule, or ctx.ModuleName() if this module is not an OverridableModule
316// or if this variant is not overridden.
317func ModuleNameWithPossibleOverride(ctx ModuleContext) string {
318 if overridable, ok := ctx.Module().(OverridableModule); ok {
319 if o := overridable.GetOverriddenBy(); o != "" {
320 return o
321 }
322 }
323 return ctx.ModuleName()
324}