blob: 45f7be070f95a4889270479768be80fecf718864 [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 (
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
47// Base module struct for override module types
48type OverrideModuleBase struct {
49 moduleProperties OverrideModuleProperties
50
51 overridingProperties []interface{}
52}
53
54type OverrideModuleProperties struct {
55 // Name of the base module to be overridden
56 Base *string
57
58 // TODO(jungjw): Add an optional override_name bool flag.
59}
60
61func (o *OverrideModuleBase) getOverridingProperties() []interface{} {
62 return o.overridingProperties
63}
64
65func (o *OverrideModuleBase) setOverridingProperties(properties []interface{}) {
66 o.overridingProperties = properties
67}
68
69func (o *OverrideModuleBase) getOverrideModuleProperties() *OverrideModuleProperties {
70 return &o.moduleProperties
71}
72
Jiyong Park5d790c32019-11-15 18:40:32 +090073func (o *OverrideModuleBase) GetOverriddenModuleName() string {
74 return proptools.String(o.moduleProperties.Base)
75}
76
Jaewoong Jung525443a2019-02-28 15:35:54 -080077func InitOverrideModule(m OverrideModule) {
78 m.setOverridingProperties(m.GetProperties())
79
80 m.AddProperties(m.getOverrideModuleProperties())
81}
82
83// Interface for overridable module types, e.g. android_app, apex
84type OverridableModule interface {
Jiyong Park317645e2019-12-05 13:20:58 +090085 Module
86 moduleBase() *OverridableModuleBase
87
Jaewoong Jung525443a2019-02-28 15:35:54 -080088 setOverridableProperties(prop []interface{})
89
90 addOverride(o OverrideModule)
91 getOverrides() []OverrideModule
92
93 override(ctx BaseModuleContext, o OverrideModule)
Jiyong Park317645e2019-12-05 13:20:58 +090094 GetOverriddenBy() string
Jaewoong Jung525443a2019-02-28 15:35:54 -080095
96 setOverridesProperty(overridesProperties *[]string)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070097
98 // Due to complications with incoming dependencies, overrides are processed after DepsMutator.
99 // So, overridable properties need to be handled in a separate, dedicated deps mutator.
100 OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800101}
102
Jiyong Park317645e2019-12-05 13:20:58 +0900103type overridableModuleProperties struct {
104 OverriddenBy string `blueprint:"mutated"`
105}
106
Jaewoong Jung525443a2019-02-28 15:35:54 -0800107// Base module struct for overridable module types
108type OverridableModuleBase struct {
Jaewoong Jung525443a2019-02-28 15:35:54 -0800109 // List of OverrideModules that override this base module
110 overrides []OverrideModule
111 // Used to parallelize registerOverrideMutator executions. Note that only addOverride locks this
112 // mutex. It is because addOverride and getOverride are used in different mutators, and so are
113 // guaranteed to be not mixed. (And, getOverride only reads from overrides, and so don't require
114 // mutex locking.)
115 overridesLock sync.Mutex
116
117 overridableProperties []interface{}
118
119 // If an overridable module has a property to list other modules that itself overrides, it should
120 // set this to a pointer to the property through the InitOverridableModule function, so that
121 // override information is propagated and aggregated correctly.
122 overridesProperty *[]string
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700123
Jiyong Park317645e2019-12-05 13:20:58 +0900124 properties overridableModuleProperties
Jaewoong Jung525443a2019-02-28 15:35:54 -0800125}
126
127func InitOverridableModule(m OverridableModule, overridesProperty *[]string) {
128 m.setOverridableProperties(m.(Module).GetProperties())
129 m.setOverridesProperty(overridesProperty)
Jiyong Park317645e2019-12-05 13:20:58 +0900130 m.AddProperties(&m.moduleBase().properties)
131}
132
133func (o *OverridableModuleBase) moduleBase() *OverridableModuleBase {
134 return o
Jaewoong Jung525443a2019-02-28 15:35:54 -0800135}
136
137func (b *OverridableModuleBase) setOverridableProperties(prop []interface{}) {
138 b.overridableProperties = prop
139}
140
141func (b *OverridableModuleBase) addOverride(o OverrideModule) {
142 b.overridesLock.Lock()
143 b.overrides = append(b.overrides, o)
144 b.overridesLock.Unlock()
145}
146
147// Should NOT be used in the same mutator as addOverride.
148func (b *OverridableModuleBase) getOverrides() []OverrideModule {
149 return b.overrides
150}
151
152func (b *OverridableModuleBase) setOverridesProperty(overridesProperty *[]string) {
153 b.overridesProperty = overridesProperty
154}
155
156// Overrides a base module with the given OverrideModule.
157func (b *OverridableModuleBase) override(ctx BaseModuleContext, o OverrideModule) {
Jaewoong Junga641ee92019-03-27 11:17:14 -0700158 // Adds the base module to the overrides property, if exists, of the overriding module. See the
159 // comment on OverridableModuleBase.overridesProperty for details.
160 if b.overridesProperty != nil {
Jaewoong Jung8985d522019-06-19 11:22:25 -0700161 *b.overridesProperty = append(*b.overridesProperty, ctx.ModuleName())
Jaewoong Junga641ee92019-03-27 11:17:14 -0700162 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800163 for _, p := range b.overridableProperties {
164 for _, op := range o.getOverridingProperties() {
165 if proptools.TypeEqual(p, op) {
Jiyong Park5d790c32019-11-15 18:40:32 +0900166 err := proptools.ExtendProperties(p, op, nil, proptools.OrderReplace)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800167 if err != nil {
168 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
169 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
170 } else {
171 panic(err)
172 }
173 }
174 }
175 }
176 }
Jiyong Park317645e2019-12-05 13:20:58 +0900177 b.properties.OverriddenBy = o.Name()
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700178}
179
Jiyong Park317645e2019-12-05 13:20:58 +0900180// GetOverriddenBy returns the name of the override module that has overridden this module.
181// For example, if an override module foo has its 'base' property set to bar, then another local variant
182// of bar is created and its properties are overriden by foo. This method returns bar when called from
183// the new local variant. It returns "" when called from the original variant of bar.
184func (b *OverridableModuleBase) GetOverriddenBy() string {
185 return b.properties.OverriddenBy
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700186}
187
188func (b *OverridableModuleBase) OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) {
Jaewoong Jung525443a2019-02-28 15:35:54 -0800189}
190
191// Mutators for override/overridable modules. All the fun happens in these functions. It is critical
192// to keep them in this order and not put any order mutators between them.
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700193func RegisterOverridePostDepsMutators(ctx RegisterMutatorsContext) {
Jaewoong Jung525443a2019-02-28 15:35:54 -0800194 ctx.BottomUp("override_deps", overrideModuleDepsMutator).Parallel()
195 ctx.TopDown("register_override", registerOverrideMutator).Parallel()
196 ctx.BottomUp("perform_override", performOverrideMutator).Parallel()
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700197 ctx.BottomUp("overridable_deps", overridableModuleDepsMutator).Parallel()
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700198 ctx.BottomUp("replace_deps_on_override", replaceDepsOnOverridingModuleMutator).Parallel()
Jaewoong Jung525443a2019-02-28 15:35:54 -0800199}
200
201type overrideBaseDependencyTag struct {
202 blueprint.BaseDependencyTag
203}
204
205var overrideBaseDepTag overrideBaseDependencyTag
206
207// Adds dependency on the base module to the overriding module so that they can be visited in the
208// next phase.
209func overrideModuleDepsMutator(ctx BottomUpMutatorContext) {
210 if module, ok := ctx.Module().(OverrideModule); ok {
211 ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base)
212 }
213}
214
215// Visits the base module added as a dependency above, checks the module type, and registers the
216// overriding module.
217func registerOverrideMutator(ctx TopDownMutatorContext) {
218 ctx.VisitDirectDepsWithTag(overrideBaseDepTag, func(base Module) {
219 if o, ok := base.(OverridableModule); ok {
220 o.addOverride(ctx.Module().(OverrideModule))
221 } else {
222 ctx.PropertyErrorf("base", "unsupported base module type")
223 }
224 })
225}
226
227// Now, goes through all overridable modules, finds all modules overriding them, creates a local
228// variant for each of them, and performs the actual overriding operation by calling override().
229func performOverrideMutator(ctx BottomUpMutatorContext) {
230 if b, ok := ctx.Module().(OverridableModule); ok {
231 overrides := b.getOverrides()
232 if len(overrides) == 0 {
233 return
234 }
235 variants := make([]string, len(overrides)+1)
236 // The first variant is for the original, non-overridden, base module.
237 variants[0] = ""
238 for i, o := range overrides {
239 variants[i+1] = o.(Module).Name()
240 }
241 mods := ctx.CreateLocalVariations(variants...)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700242 // Make the original variation the default one to depend on if no other override module variant
243 // is specified.
244 ctx.AliasVariation(variants[0])
Jaewoong Jung525443a2019-02-28 15:35:54 -0800245 for i, o := range overrides {
246 mods[i+1].(OverridableModule).override(ctx, o)
247 }
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700248 } else if o, ok := ctx.Module().(OverrideModule); ok {
249 // Create a variant of the overriding module with its own name. This matches the above local
250 // variant name rule for overridden modules, and thus allows ReplaceDependencies to match the
251 // two.
252 ctx.CreateLocalVariations(o.Name())
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700253 // To allow dependencies to be added without having to know the above variation.
254 ctx.AliasVariation(o.Name())
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700255 }
256}
257
258func overridableModuleDepsMutator(ctx BottomUpMutatorContext) {
259 if b, ok := ctx.Module().(OverridableModule); ok {
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700260 b.OverridablePropertiesDepsMutator(ctx)
261 }
262}
263
264func replaceDepsOnOverridingModuleMutator(ctx BottomUpMutatorContext) {
265 if b, ok := ctx.Module().(OverridableModule); ok {
Jiyong Park317645e2019-12-05 13:20:58 +0900266 if o := b.GetOverriddenBy(); o != "" {
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700267 // Redirect dependencies on the overriding module to this overridden module. Overriding
268 // modules are basically pseudo modules, and all build actions are associated to overridden
269 // modules. Therefore, dependencies on overriding modules need to be forwarded there as well.
270 ctx.ReplaceDependencies(o)
271 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800272 }
273}