Refactor factories
Change module factories from returning a blueprint.Module and a list
of property structs to returning an android.Module, which holds the
list of property structs.
Test: build.ninja identical except for Factory: comment lines
Change-Id: Ica1d823f009db812c518f271a386fbff39c9766f
diff --git a/android/arch.go b/android/arch.go
index effd5a6..67ce30e 100644
--- a/android/arch.go
+++ b/android/arch.go
@@ -20,7 +20,6 @@
"runtime"
"strings"
- "github.com/google/blueprint"
"github.com/google/blueprint/proptools"
)
@@ -491,13 +490,11 @@
var archPropTypeMap OncePer
-func InitArchModule(m Module,
- propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
+func InitArchModule(m Module) {
base := m.base()
- base.generalProperties = append(base.generalProperties,
- propertyStructs...)
+ base.generalProperties = m.GetProperties()
for _, properties := range base.generalProperties {
propertiesValue := reflect.ValueOf(properties)
@@ -524,17 +521,13 @@
}
}
- var allProperties []interface{}
- allProperties = append(allProperties, base.generalProperties...)
for _, asp := range base.archProperties {
if asp != nil {
- allProperties = append(allProperties, asp)
+ m.AddProperties(asp)
}
}
- base.customizableProperties = allProperties
-
- return m, allProperties
+ base.customizableProperties = m.GetProperties()
}
var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
diff --git a/android/defaults.go b/android/defaults.go
index df1409e..0776405 100644
--- a/android/defaults.go
+++ b/android/defaults.go
@@ -50,14 +50,11 @@
var _ Defaultable = (*DefaultableModule)(nil)
-func InitDefaultableModule(module Module, d Defaultable,
- props ...interface{}) (blueprint.Module, []interface{}) {
+func InitDefaultableModule(module Module, d Defaultable) {
- d.setProperties(props)
+ d.setProperties(module.GetProperties())
- props = append(props, d.defaults())
-
- return module, props
+ module.AddProperties(d.defaults())
}
type DefaultsModule struct {
@@ -79,21 +76,18 @@
return d.defaultableProperties
}
-func InitDefaultsModule(module Module, d Defaults, props ...interface{}) (blueprint.Module, []interface{}) {
- props = append(props,
+func InitDefaultsModule(module Module, d Defaults) {
+ module.AddProperties(
&hostAndDeviceProperties{},
&commonProperties{},
&variableProperties{})
- _, props = InitArchModule(module, props...)
+ InitArchModule(module)
+ InitDefaultableModule(module, d)
- _, props = InitDefaultableModule(module, d, props...)
-
- props = append(props, &module.base().nameProperties)
+ module.AddProperties(&module.base().nameProperties)
module.base().module = module
-
- return module, props
}
var _ Defaults = (*DefaultsModule)(nil)
diff --git a/android/module.go b/android/module.go
index 8f8f34b..18810fb 100644
--- a/android/module.go
+++ b/android/module.go
@@ -108,6 +108,9 @@
InstallInData() bool
InstallInSanitizerDir() bool
SkipInstall()
+
+ AddProperties(props ...interface{})
+ GetProperties() []interface{}
}
type nameProperties struct {
@@ -194,24 +197,18 @@
NeitherHostNorDeviceSupported
)
-func InitAndroidModule(m Module,
- propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
-
+func InitAndroidModule(m Module) {
base := m.base()
base.module = m
- propertyStructs = append(propertyStructs,
+ m.AddProperties(
&base.nameProperties,
&base.commonProperties,
&base.variableProperties)
-
- return m, propertyStructs
}
-func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib,
- propertyStructs ...interface{}) (blueprint.Module, []interface{}) {
-
- _, propertyStructs = InitAndroidModule(m, propertyStructs...)
+func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
+ InitAndroidModule(m)
base := m.base()
base.commonProperties.HostOrDeviceSupported = hod
@@ -225,10 +222,10 @@
base.hostAndDeviceProperties.Device_supported = boolPtr(true)
fallthrough
case HostAndDeviceDefault:
- propertyStructs = append(propertyStructs, &base.hostAndDeviceProperties)
+ m.AddProperties(&base.hostAndDeviceProperties)
}
- return InitArchModule(m, propertyStructs...)
+ InitArchModule(m)
}
// A ModuleBase object contains the properties that are common to all Android
@@ -250,7 +247,6 @@
//
// import (
// "android/soong/android"
-// "github.com/google/blueprint"
// )
//
// type myModule struct {
@@ -260,9 +256,11 @@
// }
// }
//
-// func NewMyModule() (blueprint.Module, []interface{}) {
+// func NewMyModule() android.Module) {
// m := &myModule{}
-// return android.InitAndroidModule(m, &m.properties)
+// m.AddProperties(&m.properties)
+// android.InitAndroidModule(m)
+// return m
// }
//
// func (m *myModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
@@ -274,6 +272,7 @@
type ModuleBase struct {
// Putting the curiously recurring thing pointing to the thing that contains
// the thing pattern to good use.
+ // TODO: remove this
module Module
nameProperties nameProperties
@@ -295,6 +294,16 @@
blueprintDir string
hooks hooks
+
+ registerProps []interface{}
+}
+
+func (a *ModuleBase) AddProperties(props ...interface{}) {
+ a.registerProps = append(a.registerProps, props...)
+}
+
+func (a *ModuleBase) GetProperties() []interface{} {
+ return a.registerProps
}
// Name returns the name of the module. It may be overridden by individual module types, for
diff --git a/android/prebuilt_test.go b/android/prebuilt_test.go
index d09518b..5fa2032 100644
--- a/android/prebuilt_test.go
+++ b/android/prebuilt_test.go
@@ -123,8 +123,8 @@
for _, test := range prebuiltsTests {
t.Run(test.name, func(t *testing.T) {
ctx := NewContext()
- ctx.RegisterModuleType("prebuilt", newPrebuiltModule)
- ctx.RegisterModuleType("source", newSourceModule)
+ ctx.RegisterModuleType("prebuilt", ModuleFactoryAdaptor(newPrebuiltModule))
+ ctx.RegisterModuleType("source", ModuleFactoryAdaptor(newSourceModule))
ctx.MockFileSystem(map[string][]byte{
"Blueprints": []byte(`
source {
@@ -183,9 +183,11 @@
prebuilt Prebuilt
}
-func newPrebuiltModule() (blueprint.Module, []interface{}) {
+func newPrebuiltModule() Module {
m := &prebuiltModule{}
- return InitAndroidModule(m, &m.prebuilt.Properties)
+ m.AddProperties(&m.prebuilt.Properties)
+ InitAndroidModule(m)
+ return m
}
func (p *prebuiltModule) Name() string {
@@ -210,9 +212,11 @@
dependsOnSourceModule, dependsOnPrebuiltModule bool
}
-func newSourceModule() (blueprint.Module, []interface{}) {
+func newSourceModule() Module {
m := &sourceModule{}
- return InitAndroidModule(m, &m.properties)
+ m.AddProperties(&m.properties)
+ InitAndroidModule(m)
+ return m
}
func (s *sourceModule) DepsMutator(ctx BottomUpMutatorContext) {
diff --git a/android/register.go b/android/register.go
index 9396664..76a1cc9 100644
--- a/android/register.go
+++ b/android/register.go
@@ -41,8 +41,19 @@
var mutators []*mutator
-func RegisterModuleType(name string, factory blueprint.ModuleFactory) {
- moduleTypes = append(moduleTypes, moduleType{name, factory})
+type ModuleFactory func() Module
+
+// ModuleFactoryAdapter Wraps a ModuleFactory into a blueprint.ModuleFactory by converting an Module
+// into a blueprint.Module and a list of property structs
+func ModuleFactoryAdaptor(factory ModuleFactory) blueprint.ModuleFactory {
+ return func() (blueprint.Module, []interface{}) {
+ module := factory()
+ return module, module.GetProperties()
+ }
+}
+
+func RegisterModuleType(name string, factory ModuleFactory) {
+ moduleTypes = append(moduleTypes, moduleType{name, ModuleFactoryAdaptor(factory)})
}
func RegisterSingletonType(name string, factory blueprint.SingletonFactory) {