• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	"sort"
32	"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
46
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
51}
52
53// Base module struct for override module types
54type OverrideModuleBase struct {
55	moduleProperties OverrideModuleProperties
56
57	overridingProperties []interface{}
58
59	overriddenByPrebuilt bool
60}
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
81func (o *OverrideModuleBase) GetOverriddenModuleName() string {
82	return proptools.String(o.moduleProperties.Base)
83}
84
85func (o *OverrideModuleBase) setOverriddenByPrebuilt(overridden bool) {
86	o.overriddenByPrebuilt = overridden
87}
88
89func (o *OverrideModuleBase) getOverriddenByPrebuilt() bool {
90	return o.overriddenByPrebuilt
91}
92
93func 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 {
101	Module
102	moduleBase() *OverridableModuleBase
103
104	setOverridableProperties(prop []interface{})
105
106	addOverride(o OverrideModule)
107	getOverrides() []OverrideModule
108
109	override(ctx BaseModuleContext, o OverrideModule)
110	GetOverriddenBy() string
111
112	setOverridesProperty(overridesProperties *[]string)
113
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)
117}
118
119type overridableModuleProperties struct {
120	OverriddenBy string `blueprint:"mutated"`
121}
122
123// Base module struct for overridable module types
124type OverridableModuleBase struct {
125	// 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
139
140	overridableModuleProperties overridableModuleProperties
141}
142
143func InitOverridableModule(m OverridableModule, overridesProperty *[]string) {
144	m.setOverridableProperties(m.(Module).GetProperties())
145	m.setOverridesProperty(overridesProperty)
146	m.AddProperties(&m.moduleBase().overridableModuleProperties)
147}
148
149func (o *OverridableModuleBase) moduleBase() *OverridableModuleBase {
150	return o
151}
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 {
165	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()
170	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) {
182				err := proptools.ExtendProperties(p, op, nil, proptools.OrderReplace)
183				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	}
193	// 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	}
198	b.overridableModuleProperties.OverriddenBy = o.Name()
199}
200
201// 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 {
206	return b.overridableModuleProperties.OverriddenBy
207}
208
209func (b *OverridableModuleBase) OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) {
210}
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.
214func RegisterOverridePostDepsMutators(ctx RegisterMutatorsContext) {
215	ctx.BottomUp("override_deps", overrideModuleDepsMutator).Parallel()
216	ctx.TopDown("register_override", registerOverrideMutator).Parallel()
217	ctx.BottomUp("perform_override", performOverrideMutator).Parallel()
218	// overridableModuleDepsMutator calls OverridablePropertiesDepsMutator so that overridable modules can
219	// add deps from overridable properties.
220	ctx.BottomUp("overridable_deps", overridableModuleDepsMutator).Parallel()
221	// 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()
226	ctx.BottomUp("replace_deps_on_override", replaceDepsOnOverridingModuleMutator).Parallel()
227}
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 {
239		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		}
244		// See if there's a prebuilt module that overrides this override module with prefer flag,
245		// in which case we call HideFromMake on the corresponding variant later.
246		ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(dep Module) {
247			prebuilt := GetEmbeddedPrebuilt(dep)
248			if prebuilt == nil {
249				panic("PrebuiltDepTag leads to a non-prebuilt module " + dep.Name())
250			}
251			if prebuilt.UsePrebuilt() {
252				module.setOverriddenByPrebuilt(true)
253				return
254			}
255		})
256		ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base)
257	}
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...)
287		// Make the original variation the default one to depend on if no other override module variant
288		// is specified.
289		ctx.AliasVariation(variants[0])
290		for i, o := range overrides {
291			mods[i+1].(OverridableModule).override(ctx, o)
292			if o.getOverriddenByPrebuilt() {
293				// The overriding module itself, too, is overridden by a prebuilt. Skip its installation.
294				mods[i+1].HideFromMake()
295			}
296		}
297	} 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())
302		// To allow dependencies to be added without having to know the above variation.
303		ctx.AliasVariation(o.Name())
304	}
305}
306
307func overridableModuleDepsMutator(ctx BottomUpMutatorContext) {
308	if b, ok := ctx.Module().(OverridableModule); ok {
309		b.OverridablePropertiesDepsMutator(ctx)
310	}
311}
312
313func replaceDepsOnOverridingModuleMutator(ctx BottomUpMutatorContext) {
314	if b, ok := ctx.Module().(OverridableModule); ok {
315		if o := b.GetOverriddenBy(); o != "" {
316			// 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		}
321	}
322}
323
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}
335