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
diff --git a/Android.bp b/Android.bp
index abae680..c887247 100644
--- a/Android.bp
+++ b/Android.bp
@@ -91,6 +91,7 @@
     srcs: [
         "common/arch.go",
         "common/config.go",
+        "common/defaults.go",
         "common/defs.go",
         "common/env.go",
         "common/glob.go",
diff --git a/cc/cc.go b/cc/cc.go
index b23ab43..992982d 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -41,6 +41,7 @@
 	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 @@
 	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 @@
 
 	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) {
@@ -1686,6 +1692,33 @@
 }
 
 //
+// 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
 //
 
diff --git a/common/arch.go b/common/arch.go
index f0f4409..e524dd5 100644
--- a/common/arch.go
+++ b/common/arch.go
@@ -25,6 +25,9 @@
 )
 
 func init() {
+	RegisterBottomUpMutator("defaults_deps", defaultsDepsMutator)
+	RegisterTopDownMutator("defaults", defaultsMutator)
+
 	RegisterBottomUpMutator("host_or_device", HostOrDeviceMutator)
 	RegisterBottomUpMutator("arch", ArchMutator)
 }
@@ -383,13 +386,11 @@
 	}
 }
 
-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...)
 
diff --git a/common/defaults.go b/common/defaults.go
new file mode 100644
index 0000000..693d510
--- /dev/null
+++ b/common/defaults.go
@@ -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))
+					}
+				}
+			})
+		}
+	}
+}
diff --git a/common/module.go b/common/module.go
index 1860ecb..69da7c6 100644
--- a/common/module.go
+++ b/common/module.go
@@ -127,6 +127,7 @@
 
 	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 @@
 		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