2019-03-01 07:35:54 +08:00
|
|
|
// Copyright (C) 2019 The Android Open Source Project
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package android
|
|
|
|
|
|
|
|
// This file contains all the foundation components for override modules and their base module
|
|
|
|
// types. Override modules are a kind of opposite of default modules in that they override certain
|
|
|
|
// properties of an existing base module whereas default modules provide base module data to be
|
|
|
|
// overridden. However, unlike default and defaultable module pairs, both override and overridable
|
|
|
|
// modules generate and output build actions, and it is up to product make vars to decide which one
|
|
|
|
// to actually build and install in the end. In other words, default modules and defaultable modules
|
|
|
|
// can be compared to abstract classes and concrete classes in C++ and Java. By the same analogy,
|
|
|
|
// both override and overridable modules act like concrete classes.
|
|
|
|
//
|
|
|
|
// There is one more crucial difference from the logic perspective. Unlike default pairs, most Soong
|
|
|
|
// actions happen in the base (overridable) module by creating a local variant for each override
|
|
|
|
// module based on it.
|
|
|
|
|
|
|
|
import (
|
2020-06-30 14:49:34 +08:00
|
|
|
"sort"
|
2019-03-01 07:35:54 +08:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Interface for override module types, e.g. override_android_app, override_apex
|
|
|
|
type OverrideModule interface {
|
|
|
|
Module
|
|
|
|
|
|
|
|
getOverridingProperties() []interface{}
|
|
|
|
setOverridingProperties(properties []interface{})
|
|
|
|
|
|
|
|
getOverrideModuleProperties() *OverrideModuleProperties
|
2020-06-23 22:58:13 +08:00
|
|
|
|
|
|
|
// Internal funcs to handle interoperability between override modules and prebuilts.
|
|
|
|
// i.e. cases where an overriding module, too, is overridden by a prebuilt module.
|
|
|
|
setOverriddenByPrebuilt(overridden bool)
|
|
|
|
getOverriddenByPrebuilt() bool
|
2019-03-01 07:35:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Base module struct for override module types
|
|
|
|
type OverrideModuleBase struct {
|
|
|
|
moduleProperties OverrideModuleProperties
|
|
|
|
|
|
|
|
overridingProperties []interface{}
|
2020-06-23 22:58:13 +08:00
|
|
|
|
|
|
|
overriddenByPrebuilt bool
|
2019-03-01 07:35:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type OverrideModuleProperties struct {
|
|
|
|
// Name of the base module to be overridden
|
|
|
|
Base *string
|
|
|
|
|
|
|
|
// TODO(jungjw): Add an optional override_name bool flag.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OverrideModuleBase) getOverridingProperties() []interface{} {
|
|
|
|
return o.overridingProperties
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OverrideModuleBase) setOverridingProperties(properties []interface{}) {
|
|
|
|
o.overridingProperties = properties
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OverrideModuleBase) getOverrideModuleProperties() *OverrideModuleProperties {
|
|
|
|
return &o.moduleProperties
|
|
|
|
}
|
|
|
|
|
2019-11-15 17:40:32 +08:00
|
|
|
func (o *OverrideModuleBase) GetOverriddenModuleName() string {
|
|
|
|
return proptools.String(o.moduleProperties.Base)
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:58:13 +08:00
|
|
|
func (o *OverrideModuleBase) setOverriddenByPrebuilt(overridden bool) {
|
|
|
|
o.overriddenByPrebuilt = overridden
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OverrideModuleBase) getOverriddenByPrebuilt() bool {
|
|
|
|
return o.overriddenByPrebuilt
|
|
|
|
}
|
|
|
|
|
2019-03-01 07:35:54 +08:00
|
|
|
func InitOverrideModule(m OverrideModule) {
|
|
|
|
m.setOverridingProperties(m.GetProperties())
|
|
|
|
|
|
|
|
m.AddProperties(m.getOverrideModuleProperties())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interface for overridable module types, e.g. android_app, apex
|
|
|
|
type OverridableModule interface {
|
2019-12-05 12:20:58 +08:00
|
|
|
Module
|
|
|
|
moduleBase() *OverridableModuleBase
|
|
|
|
|
2019-03-01 07:35:54 +08:00
|
|
|
setOverridableProperties(prop []interface{})
|
|
|
|
|
|
|
|
addOverride(o OverrideModule)
|
|
|
|
getOverrides() []OverrideModule
|
|
|
|
|
|
|
|
override(ctx BaseModuleContext, o OverrideModule)
|
2019-12-05 12:20:58 +08:00
|
|
|
GetOverriddenBy() string
|
2019-03-01 07:35:54 +08:00
|
|
|
|
|
|
|
setOverridesProperty(overridesProperties *[]string)
|
2019-05-11 06:16:29 +08:00
|
|
|
|
|
|
|
// Due to complications with incoming dependencies, overrides are processed after DepsMutator.
|
|
|
|
// So, overridable properties need to be handled in a separate, dedicated deps mutator.
|
|
|
|
OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext)
|
2019-03-01 07:35:54 +08:00
|
|
|
}
|
|
|
|
|
2019-12-05 12:20:58 +08:00
|
|
|
type overridableModuleProperties struct {
|
|
|
|
OverriddenBy string `blueprint:"mutated"`
|
|
|
|
}
|
|
|
|
|
2019-03-01 07:35:54 +08:00
|
|
|
// Base module struct for overridable module types
|
|
|
|
type OverridableModuleBase struct {
|
|
|
|
// List of OverrideModules that override this base module
|
|
|
|
overrides []OverrideModule
|
|
|
|
// Used to parallelize registerOverrideMutator executions. Note that only addOverride locks this
|
|
|
|
// mutex. It is because addOverride and getOverride are used in different mutators, and so are
|
|
|
|
// guaranteed to be not mixed. (And, getOverride only reads from overrides, and so don't require
|
|
|
|
// mutex locking.)
|
|
|
|
overridesLock sync.Mutex
|
|
|
|
|
|
|
|
overridableProperties []interface{}
|
|
|
|
|
|
|
|
// If an overridable module has a property to list other modules that itself overrides, it should
|
|
|
|
// set this to a pointer to the property through the InitOverridableModule function, so that
|
|
|
|
// override information is propagated and aggregated correctly.
|
|
|
|
overridesProperty *[]string
|
2019-05-11 06:16:29 +08:00
|
|
|
|
2020-01-11 05:26:00 +08:00
|
|
|
overridableModuleProperties overridableModuleProperties
|
2019-03-01 07:35:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func InitOverridableModule(m OverridableModule, overridesProperty *[]string) {
|
|
|
|
m.setOverridableProperties(m.(Module).GetProperties())
|
|
|
|
m.setOverridesProperty(overridesProperty)
|
2020-01-11 05:26:00 +08:00
|
|
|
m.AddProperties(&m.moduleBase().overridableModuleProperties)
|
2019-12-05 12:20:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OverridableModuleBase) moduleBase() *OverridableModuleBase {
|
|
|
|
return o
|
2019-03-01 07:35:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *OverridableModuleBase) setOverridableProperties(prop []interface{}) {
|
|
|
|
b.overridableProperties = prop
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *OverridableModuleBase) addOverride(o OverrideModule) {
|
|
|
|
b.overridesLock.Lock()
|
|
|
|
b.overrides = append(b.overrides, o)
|
|
|
|
b.overridesLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should NOT be used in the same mutator as addOverride.
|
|
|
|
func (b *OverridableModuleBase) getOverrides() []OverrideModule {
|
2020-06-30 14:49:34 +08:00
|
|
|
b.overridesLock.Lock()
|
|
|
|
sort.Slice(b.overrides, func(i, j int) bool {
|
|
|
|
return b.overrides[i].Name() < b.overrides[j].Name()
|
|
|
|
})
|
|
|
|
b.overridesLock.Unlock()
|
2019-03-01 07:35:54 +08:00
|
|
|
return b.overrides
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *OverridableModuleBase) setOverridesProperty(overridesProperty *[]string) {
|
|
|
|
b.overridesProperty = overridesProperty
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overrides a base module with the given OverrideModule.
|
|
|
|
func (b *OverridableModuleBase) override(ctx BaseModuleContext, o OverrideModule) {
|
|
|
|
for _, p := range b.overridableProperties {
|
|
|
|
for _, op := range o.getOverridingProperties() {
|
|
|
|
if proptools.TypeEqual(p, op) {
|
2019-11-15 17:40:32 +08:00
|
|
|
err := proptools.ExtendProperties(p, op, nil, proptools.OrderReplace)
|
2019-03-01 07:35:54 +08:00
|
|
|
if err != nil {
|
|
|
|
if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
|
|
|
|
ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-20 09:32:06 +08:00
|
|
|
// Adds the base module to the overrides property, if exists, of the overriding module. See the
|
|
|
|
// comment on OverridableModuleBase.overridesProperty for details.
|
|
|
|
if b.overridesProperty != nil {
|
|
|
|
*b.overridesProperty = append(*b.overridesProperty, ctx.ModuleName())
|
|
|
|
}
|
2020-01-11 05:26:00 +08:00
|
|
|
b.overridableModuleProperties.OverriddenBy = o.Name()
|
2019-05-11 06:16:29 +08:00
|
|
|
}
|
|
|
|
|
2019-12-05 12:20:58 +08:00
|
|
|
// GetOverriddenBy returns the name of the override module that has overridden this module.
|
|
|
|
// For example, if an override module foo has its 'base' property set to bar, then another local variant
|
|
|
|
// of bar is created and its properties are overriden by foo. This method returns bar when called from
|
|
|
|
// the new local variant. It returns "" when called from the original variant of bar.
|
|
|
|
func (b *OverridableModuleBase) GetOverriddenBy() string {
|
2020-01-11 05:26:00 +08:00
|
|
|
return b.overridableModuleProperties.OverriddenBy
|
2019-05-11 06:16:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *OverridableModuleBase) OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) {
|
2019-03-01 07:35:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mutators for override/overridable modules. All the fun happens in these functions. It is critical
|
|
|
|
// to keep them in this order and not put any order mutators between them.
|
2019-05-11 06:16:29 +08:00
|
|
|
func RegisterOverridePostDepsMutators(ctx RegisterMutatorsContext) {
|
2019-03-01 07:35:54 +08:00
|
|
|
ctx.BottomUp("override_deps", overrideModuleDepsMutator).Parallel()
|
|
|
|
ctx.TopDown("register_override", registerOverrideMutator).Parallel()
|
|
|
|
ctx.BottomUp("perform_override", performOverrideMutator).Parallel()
|
2019-05-11 06:16:29 +08:00
|
|
|
ctx.BottomUp("overridable_deps", overridableModuleDepsMutator).Parallel()
|
2019-06-06 23:45:58 +08:00
|
|
|
ctx.BottomUp("replace_deps_on_override", replaceDepsOnOverridingModuleMutator).Parallel()
|
2019-03-01 07:35:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type overrideBaseDependencyTag struct {
|
|
|
|
blueprint.BaseDependencyTag
|
|
|
|
}
|
|
|
|
|
|
|
|
var overrideBaseDepTag overrideBaseDependencyTag
|
|
|
|
|
|
|
|
// Adds dependency on the base module to the overriding module so that they can be visited in the
|
|
|
|
// next phase.
|
|
|
|
func overrideModuleDepsMutator(ctx BottomUpMutatorContext) {
|
|
|
|
if module, ok := ctx.Module().(OverrideModule); ok {
|
2020-06-20 18:38:21 +08:00
|
|
|
base := String(module.getOverrideModuleProperties().Base)
|
|
|
|
if !ctx.OtherModuleExists(base) {
|
|
|
|
ctx.PropertyErrorf("base", "%q is not a valid module name", base)
|
|
|
|
return
|
|
|
|
}
|
2020-06-23 22:58:13 +08:00
|
|
|
// See if there's a prebuilt module that overrides this override module with prefer flag,
|
|
|
|
// in which case we call SkipInstall on the corresponding variant later.
|
2020-06-12 01:37:51 +08:00
|
|
|
ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(dep Module) {
|
|
|
|
prebuilt, ok := dep.(PrebuiltInterface)
|
|
|
|
if !ok {
|
|
|
|
panic("PrebuiltDepTag leads to a non-prebuilt module " + dep.Name())
|
|
|
|
}
|
|
|
|
if prebuilt.Prebuilt().UsePrebuilt() {
|
2020-06-23 22:58:13 +08:00
|
|
|
module.setOverriddenByPrebuilt(true)
|
2020-06-12 01:37:51 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
2020-06-23 22:58:13 +08:00
|
|
|
ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base)
|
2019-03-01 07:35:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visits the base module added as a dependency above, checks the module type, and registers the
|
|
|
|
// overriding module.
|
|
|
|
func registerOverrideMutator(ctx TopDownMutatorContext) {
|
|
|
|
ctx.VisitDirectDepsWithTag(overrideBaseDepTag, func(base Module) {
|
|
|
|
if o, ok := base.(OverridableModule); ok {
|
|
|
|
o.addOverride(ctx.Module().(OverrideModule))
|
|
|
|
} else {
|
|
|
|
ctx.PropertyErrorf("base", "unsupported base module type")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, goes through all overridable modules, finds all modules overriding them, creates a local
|
|
|
|
// variant for each of them, and performs the actual overriding operation by calling override().
|
|
|
|
func performOverrideMutator(ctx BottomUpMutatorContext) {
|
|
|
|
if b, ok := ctx.Module().(OverridableModule); ok {
|
|
|
|
overrides := b.getOverrides()
|
|
|
|
if len(overrides) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
variants := make([]string, len(overrides)+1)
|
|
|
|
// The first variant is for the original, non-overridden, base module.
|
|
|
|
variants[0] = ""
|
|
|
|
for i, o := range overrides {
|
|
|
|
variants[i+1] = o.(Module).Name()
|
|
|
|
}
|
|
|
|
mods := ctx.CreateLocalVariations(variants...)
|
2019-06-06 23:45:58 +08:00
|
|
|
// Make the original variation the default one to depend on if no other override module variant
|
|
|
|
// is specified.
|
|
|
|
ctx.AliasVariation(variants[0])
|
2019-03-01 07:35:54 +08:00
|
|
|
for i, o := range overrides {
|
|
|
|
mods[i+1].(OverridableModule).override(ctx, o)
|
2020-06-23 22:58:13 +08:00
|
|
|
if o.getOverriddenByPrebuilt() {
|
|
|
|
// The overriding module itself, too, is overridden by a prebuilt. Skip its installation.
|
|
|
|
mods[i+1].SkipInstall()
|
|
|
|
}
|
2019-03-01 07:35:54 +08:00
|
|
|
}
|
2019-05-11 06:16:29 +08:00
|
|
|
} else if o, ok := ctx.Module().(OverrideModule); ok {
|
|
|
|
// Create a variant of the overriding module with its own name. This matches the above local
|
|
|
|
// variant name rule for overridden modules, and thus allows ReplaceDependencies to match the
|
|
|
|
// two.
|
|
|
|
ctx.CreateLocalVariations(o.Name())
|
2019-06-06 23:45:58 +08:00
|
|
|
// To allow dependencies to be added without having to know the above variation.
|
|
|
|
ctx.AliasVariation(o.Name())
|
2019-05-11 06:16:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func overridableModuleDepsMutator(ctx BottomUpMutatorContext) {
|
2019-06-06 23:45:58 +08:00
|
|
|
if b, ok := ctx.Module().(OverridableModule); ok {
|
|
|
|
b.OverridablePropertiesDepsMutator(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func replaceDepsOnOverridingModuleMutator(ctx BottomUpMutatorContext) {
|
2019-05-11 06:16:29 +08:00
|
|
|
if b, ok := ctx.Module().(OverridableModule); ok {
|
2019-12-05 12:20:58 +08:00
|
|
|
if o := b.GetOverriddenBy(); o != "" {
|
2019-05-11 06:16:29 +08:00
|
|
|
// Redirect dependencies on the overriding module to this overridden module. Overriding
|
|
|
|
// modules are basically pseudo modules, and all build actions are associated to overridden
|
|
|
|
// modules. Therefore, dependencies on overriding modules need to be forwarded there as well.
|
|
|
|
ctx.ReplaceDependencies(o)
|
|
|
|
}
|
2019-03-01 07:35:54 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-15 08:39:29 +08:00
|
|
|
|
|
|
|
// ModuleNameWithPossibleOverride returns the name of the OverrideModule that overrides the current
|
|
|
|
// variant of this OverridableModule, or ctx.ModuleName() if this module is not an OverridableModule
|
|
|
|
// or if this variant is not overridden.
|
|
|
|
func ModuleNameWithPossibleOverride(ctx ModuleContext) string {
|
|
|
|
if overridable, ok := ctx.Module().(OverridableModule); ok {
|
|
|
|
if o := overridable.GetOverriddenBy(); o != "" {
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ctx.ModuleName()
|
|
|
|
}
|