Add support for defaults modules

Add a cc_defaults module, which will prepend any matching properties to
modules that reference it with a "defaults" property.

Change-Id: I5908dd98d204f71b29ad95a4ab85403aa1621ca2
This commit is contained in:
Colin Cross 2015-11-02 16:43:11 -08:00
parent 6362e27848
commit cfad119eaf
5 changed files with 176 additions and 13 deletions

View File

@ -91,6 +91,7 @@ bootstrap_go_package {
srcs: [
"common/arch.go",
"common/config.go",
"common/defaults.go",
"common/defs.go",
"common/env.go",
"common/glob.go",

View File

@ -41,6 +41,7 @@ func init() {
soong.RegisterModuleType("cc_binary", CCBinaryFactory)
soong.RegisterModuleType("cc_test", CCTestFactory)
soong.RegisterModuleType("cc_benchmark", CCBenchmarkFactory)
soong.RegisterModuleType("cc_defaults", CCDefaultsFactory)
soong.RegisterModuleType("toolchain_library", ToolchainLibraryFactory)
soong.RegisterModuleType("ndk_prebuilt_library", NdkPrebuiltLibraryFactory)
@ -320,23 +321,26 @@ type CCBaseProperties struct {
Relative_install_path string
}
type CCUnusedProperties struct {
Native_coverage *bool
Required []string
Sanitize []string `android:"arch_variant"`
Sanitize_recover []string
Strip string
Tags []string
}
// CCBase contains the properties and members used by all C/C++ module types, and implements
// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
// and uses a ccModuleType interface to that struct to create the build steps.
type CCBase struct {
common.AndroidModuleBase
common.DefaultableModule
module CCModuleType
Properties CCBaseProperties
unused struct {
Native_coverage *bool
Required []string
Sanitize []string `android:"arch_variant"`
Sanitize_recover []string
Strip string
Tags []string
}
unused CCUnusedProperties
installPath string
@ -350,7 +354,9 @@ func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupport
props = append(props, &base.Properties, &base.unused)
return common.InitAndroidArchModule(module, hod, multilib, props...)
_, props = common.InitAndroidArchModule(module, hod, multilib, props...)
return common.InitDefaultableModule(module, base, props...)
}
func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
@ -1685,6 +1691,33 @@ func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
return NewCCBinary(&module.CCBinary, module, common.HostSupported)
}
//
// Defaults
//
type CCDefaults struct {
common.AndroidModuleBase
common.DefaultsModule
}
func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
}
func CCDefaultsFactory() (blueprint.Module, []interface{}) {
module := &CCDefaults{}
propertyStructs := []interface{}{
&CCBaseProperties{},
&CCLibraryProperties{},
&CCBinaryProperties{},
&CCUnusedProperties{},
}
_, propertyStructs = common.InitAndroidArchModule(module, common.HostOrDeviceSupported(0),
common.Multilib(""), propertyStructs...)
return common.InitDefaultsModule(module, module, propertyStructs...)
}
//
// Device libraries shipped with gcc
//

View File

@ -25,6 +25,9 @@ import (
)
func init() {
RegisterBottomUpMutator("defaults_deps", defaultsDepsMutator)
RegisterTopDownMutator("defaults", defaultsMutator)
RegisterBottomUpMutator("host_or_device", HostOrDeviceMutator)
RegisterBottomUpMutator("arch", ArchMutator)
}
@ -383,13 +386,11 @@ func ArchMutator(mctx AndroidBottomUpMutatorContext) {
}
}
func InitArchModule(m AndroidModule, defaultMultilib Multilib,
func InitArchModule(m AndroidModule,
propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
base := m.base()
base.commonProperties.Compile_multilib = string(defaultMultilib)
base.generalProperties = append(base.generalProperties,
propertyStructs...)

127
common/defaults.go Normal file
View File

@ -0,0 +1,127 @@
// Copyright 2015 Google Inc. All rights reserved.
//
// 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 common
import (
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
)
type defaultsProperties struct {
Defaults []string
}
type DefaultableModule struct {
defaultsProperties defaultsProperties
defaultableProperties []interface{}
}
func (d *DefaultableModule) defaults() *defaultsProperties {
return &d.defaultsProperties
}
func (d *DefaultableModule) setProperties(props []interface{}) {
d.defaultableProperties = props
}
type Defaultable interface {
defaults() *defaultsProperties
setProperties([]interface{})
applyDefaults(AndroidTopDownMutatorContext, Defaults)
}
var _ Defaultable = (*DefaultableModule)(nil)
func InitDefaultableModule(module AndroidModule, d Defaultable,
props ...interface{}) (blueprint.Module, []interface{}) {
d.setProperties(props)
props = append(props, d.defaults())
return module, props
}
type DefaultsModule struct {
defaultProperties []interface{}
}
type Defaults interface {
isDefaults() bool
setProperties([]interface{})
properties() []interface{}
}
func (d *DefaultsModule) isDefaults() bool {
return true
}
func (d *DefaultsModule) properties() []interface{} {
return d.defaultProperties
}
func (d *DefaultsModule) setProperties(props []interface{}) {
d.defaultProperties = props
}
func InitDefaultsModule(module AndroidModule, d Defaults, props ...interface{}) (blueprint.Module, []interface{}) {
d.setProperties(props)
return module, props
}
var _ Defaults = (*DefaultsModule)(nil)
func (defaultable *DefaultableModule) applyDefaults(ctx AndroidTopDownMutatorContext,
defaults Defaults) {
for _, prop := range defaultable.defaultableProperties {
for _, def := range defaults.properties() {
if proptools.TypeEqual(prop, def) {
err := proptools.PrependProperties(prop, def, nil)
if err != nil {
if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
} else {
panic(err)
}
}
}
}
}
}
func defaultsDepsMutator(ctx AndroidBottomUpMutatorContext) {
if defaultable, ok := ctx.Module().(Defaultable); ok {
ctx.AddDependency(ctx.Module(), defaultable.defaults().Defaults...)
}
}
func defaultsMutator(ctx AndroidTopDownMutatorContext) {
if defaultable, ok := ctx.Module().(Defaultable); ok {
for _, defaultsDep := range defaultable.defaults().Defaults {
ctx.VisitDirectDeps(func(m blueprint.Module) {
if ctx.OtherModuleName(m) == defaultsDep {
if defaultsModule, ok := m.(Defaults); ok {
defaultable.applyDefaults(ctx, defaultsModule)
} else {
ctx.PropertyErrorf("defaults", "module %s is not an defaults module",
ctx.OtherModuleName(m))
}
}
})
}
}
}

View File

@ -127,6 +127,7 @@ func InitAndroidArchModule(m AndroidModule, hod HostOrDeviceSupported, defaultMu
base := m.base()
base.commonProperties.HostOrDeviceSupported = hod
base.commonProperties.Compile_multilib = string(defaultMultilib)
if hod == HostAndDeviceSupported {
// Default to module to device supported, host not supported, can override in module
@ -135,7 +136,7 @@ func InitAndroidArchModule(m AndroidModule, hod HostOrDeviceSupported, defaultMu
propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
}
return InitArchModule(m, defaultMultilib, propertyStructs...)
return InitArchModule(m, propertyStructs...)
}
// A AndroidModuleBase object contains the properties that are common to all Android