blob: 90ddf5049e981c2b078a3c4f2cfe4ede715f8ce1 [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
Jaewoong Jung1e362da2020-06-23 07:58:13 -070045
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
Jaewoong Jung525443a2019-02-28 15:35:54 -080050}
51
52// Base module struct for override module types
53type OverrideModuleBase struct {
54 moduleProperties OverrideModuleProperties
55
56 overridingProperties []interface{}
Jaewoong Jung1e362da2020-06-23 07:58:13 -070057
58 overriddenByPrebuilt bool
Jaewoong Jung525443a2019-02-28 15:35:54 -080059}
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
Jiyong Park5d790c32019-11-15 18:40:32 +090080func (o *OverrideModuleBase) GetOverriddenModuleName() string {
81 return proptools.String(o.moduleProperties.Base)
82}
83
Jaewoong Jung1e362da2020-06-23 07:58:13 -070084func (o *OverrideModuleBase) setOverriddenByPrebuilt(overridden bool) {
85 o.overriddenByPrebuilt = overridden
86}
87
88func (o *OverrideModuleBase) getOverriddenByPrebuilt() bool {
89 return o.overriddenByPrebuilt
90}
91
Jaewoong Jung525443a2019-02-28 15:35:54 -080092func 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 {
Jiyong Park317645e2019-12-05 13:20:58 +0900100 Module
101 moduleBase() *OverridableModuleBase
102
Jaewoong Jung525443a2019-02-28 15:35:54 -0800103 setOverridableProperties(prop []interface{})
104
105 addOverride(o OverrideModule)
106 getOverrides() []OverrideModule
107
108 override(ctx BaseModuleContext, o OverrideModule)
Jiyong Park317645e2019-12-05 13:20:58 +0900109 GetOverriddenBy() string
Jaewoong Jung525443a2019-02-28 15:35:54 -0800110
111 setOverridesProperty(overridesProperties *[]string)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700112
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)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800116}
117
Jiyong Park317645e2019-12-05 13:20:58 +0900118type overridableModuleProperties struct {
119 OverriddenBy string `blueprint:"mutated"`
120}
121
Jaewoong Jung525443a2019-02-28 15:35:54 -0800122// Base module struct for overridable module types
123type OverridableModuleBase struct {
Jaewoong Jung525443a2019-02-28 15:35:54 -0800124 // 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
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700138
Jaewoong Jungb64dd002020-01-10 13:26:00 -0800139 overridableModuleProperties overridableModuleProperties
Jaewoong Jung525443a2019-02-28 15:35:54 -0800140}
141
142func InitOverridableModule(m OverridableModule, overridesProperty *[]string) {
143 m.setOverridableProperties(m.(Module).GetProperties())
144 m.setOverridesProperty(overridesProperty)
Jaewoong Jungb64dd002020-01-10 13:26:00 -0800145 m.AddProperties(&m.moduleBase().overridableModuleProperties)
Jiyong Park317645e2019-12-05 13:20:58 +0900146}
147
148func (o *OverridableModuleBase) moduleBase() *OverridableModuleBase {
149 return o
Jaewoong Jung525443a2019-02-28 15:35:54 -0800150}
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) {
Jiyong Park5d790c32019-11-15 18:40:32 +0900176 err := proptools.ExtendProperties(p, op, nil, proptools.OrderReplace)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800177 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 }
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -0800187 // 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 }
Jaewoong Jungb64dd002020-01-10 13:26:00 -0800192 b.overridableModuleProperties.OverriddenBy = o.Name()
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700193}
194
Jiyong Park317645e2019-12-05 13:20:58 +0900195// 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 {
Jaewoong Jungb64dd002020-01-10 13:26:00 -0800200 return b.overridableModuleProperties.OverriddenBy
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700201}
202
203func (b *OverridableModuleBase) OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) {
Jaewoong Jung525443a2019-02-28 15:35:54 -0800204}
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.
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700208func RegisterOverridePostDepsMutators(ctx RegisterMutatorsContext) {
Jaewoong Jung525443a2019-02-28 15:35:54 -0800209 ctx.BottomUp("override_deps", overrideModuleDepsMutator).Parallel()
210 ctx.TopDown("register_override", registerOverrideMutator).Parallel()
211 ctx.BottomUp("perform_override", performOverrideMutator).Parallel()
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700212 ctx.BottomUp("overridable_deps", overridableModuleDepsMutator).Parallel()
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700213 ctx.BottomUp("replace_deps_on_override", replaceDepsOnOverridingModuleMutator).Parallel()
Jaewoong Jung525443a2019-02-28 15:35:54 -0800214}
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 {
Jaewoong Jung1e362da2020-06-23 07:58:13 -0700226 // 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.
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700228 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() {
Jaewoong Jung1e362da2020-06-23 07:58:13 -0700234 module.setOverriddenByPrebuilt(true)
Jaewoong Jungfb25a642020-06-11 10:37:51 -0700235 return
236 }
237 })
Jaewoong Jung1e362da2020-06-23 07:58:13 -0700238 ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base)
Jaewoong Jung525443a2019-02-28 15:35:54 -0800239 }
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...)
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700269 // Make the original variation the default one to depend on if no other override module variant
270 // is specified.
271 ctx.AliasVariation(variants[0])
Jaewoong Jung525443a2019-02-28 15:35:54 -0800272 for i, o := range overrides {
273 mods[i+1].(OverridableModule).override(ctx, o)
Jaewoong Jung1e362da2020-06-23 07:58:13 -0700274 if o.getOverriddenByPrebuilt() {
275 // The overriding module itself, too, is overridden by a prebuilt. Skip its installation.
276 mods[i+1].SkipInstall()
277 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800278 }
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700279 } 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())
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700284 // To allow dependencies to be added without having to know the above variation.
285 ctx.AliasVariation(o.Name())
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700286 }
287}
288
289func overridableModuleDepsMutator(ctx BottomUpMutatorContext) {
290 if b, ok := ctx.Module().(OverridableModule); ok {
Jaewoong Jung26dedd32019-06-06 08:45:58 -0700291 b.OverridablePropertiesDepsMutator(ctx)
292 }
293}
294
295func replaceDepsOnOverridingModuleMutator(ctx BottomUpMutatorContext) {
296 if b, ok := ctx.Module().(OverridableModule); ok {
Jiyong Park317645e2019-12-05 13:20:58 +0900297 if o := b.GetOverriddenBy(); o != "" {
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700298 // 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 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800303 }
304}