Add new AndroidModuleContext helper functions

Add Host(), Device(), and Debug() to AndroidModuleContext to allow
build rule generators to easily determine build options.

Change-Id: Ib93a462cb45189399063f641b3a8df175db0592e
diff --git a/cc/cc.go b/cc/cc.go
index 30319a8..bd81dd7 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -367,9 +367,6 @@
 // Create a ccFlags struct that collects the compile flags from global values,
 // per-target values, module type values, and per-module Blueprints properties
 func (c *ccBase) flags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
-
-	arch := ctx.Arch()
-
 	flags := CCFlags{
 		CFlags:     c.properties.Cflags,
 		CppFlags:   c.properties.Cppflags,
@@ -389,7 +386,7 @@
 	// TODO: debug
 	flags.CFlags = append(flags.CFlags, c.properties.Release.Cflags...)
 
-	if arch.HostOrDevice.Host() {
+	if ctx.Host() {
 		// TODO: allow per-module clang disable for host
 		flags.Clang = true
 	}
@@ -404,7 +401,7 @@
 
 		flags.CFlags = append(flags.CFlags, "${clangExtraCflags}")
 		flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
-		if arch.HostOrDevice.Device() {
+		if ctx.Device() {
 			flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}")
 		}
 
@@ -415,7 +412,7 @@
 		flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
 		flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
 
-		if arch.HostOrDevice.Host() {
+		if ctx.Host() {
 			gccToolchain := "--gcc-toolchain=" + toolchain.GccRoot()
 			sysroot := "--sysroot=" + filepath.Join(toolchain.GccRoot(), "sysroot")
 
@@ -441,7 +438,7 @@
 			flags.IncludeDirs = append(flags.IncludeDirs, "${SrcDir}/libnativehelper/include/nativehelper")
 		}
 
-		if arch.HostOrDevice.Device() && !c.properties.Allow_undefined_symbols {
+		if ctx.Device() && !c.properties.Allow_undefined_symbols {
 			flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
 		}
 
@@ -453,7 +450,7 @@
 				instructionSetFlags,
 				toolchain.ClangCflags(),
 				"${commonClangGlobalCflags}",
-				fmt.Sprintf("${%sClangGlobalCflags}", arch.HostOrDevice),
+				fmt.Sprintf("${%sClangGlobalCflags}", ctx.Arch().HostOrDevice),
 			}
 		} else {
 			flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
@@ -463,15 +460,15 @@
 				instructionSetFlags,
 				toolchain.Cflags(),
 				"${commonGlobalCflags}",
-				fmt.Sprintf("${%sGlobalCflags}", arch.HostOrDevice),
+				fmt.Sprintf("${%sGlobalCflags}", ctx.Arch().HostOrDevice),
 			}
 		}
 
-		if arch.HostOrDevice.Host() {
+		if ctx.Host() {
 			flags.LdFlags = append(flags.LdFlags, c.properties.Host_ldlibs...)
 		}
 
-		if arch.HostOrDevice.Device() {
+		if ctx.Device() {
 			if c.properties.Rtti {
 				flags.CppFlags = append(flags.CppFlags, "-frtti")
 			} else {
@@ -507,7 +504,6 @@
 // Modify ccFlags structs with STL library info
 func (c *ccBase) addStlFlags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
 	if !c.properties.No_default_compiler_flags {
-		arch := ctx.Arch()
 		stl := "libc++" // TODO: mingw needs libstdc++
 		if c.properties.Stl != "" {
 			stl = c.properties.Stl
@@ -522,7 +518,7 @@
 		case "libc++", "libc++_static":
 			flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
 			flags.IncludeDirs = append(flags.IncludeDirs, "${SrcDir}/external/libcxx/include")
-			if arch.HostOrDevice.Host() {
+			if ctx.Host() {
 				flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
 				flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
 				flags.LdLibs = append(flags.LdLibs, "-lc", "-lm", "-lpthread")
@@ -533,7 +529,7 @@
 				flags.ExtraSharedLibs = append(flags.ExtraSharedLibs, "libc++")
 			}
 		case "stlport", "stlport_static":
-			if arch.HostOrDevice.Device() {
+			if ctx.Device() {
 				flags.IncludeDirs = append(flags.IncludeDirs,
 					"${SrcDir}/external/stlport/stlport",
 					"${SrcDir}/bionic/libstdc++/include",
@@ -550,12 +546,12 @@
 			// Using bionic's basic libstdc++. Not actually an STL. Only around until the
 			// tree is in good enough shape to not need it.
 			// Host builds will use GNU libstdc++.
-			if arch.HostOrDevice.Device() {
+			if ctx.Device() {
 				flags.IncludeDirs = append(flags.IncludeDirs, "${SrcDir}/bionic/libstdc++/include")
 				flags.ExtraSharedLibs = append(flags.ExtraSharedLibs, "libstdc++")
 			}
 		case "none":
-			if arch.HostOrDevice.Host() {
+			if ctx.Host() {
 				flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
 				flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
 				flags.LdLibs = append(flags.LdLibs, "-lc", "-lm")
@@ -701,12 +697,11 @@
 
 const defaultSystemSharedLibraries = "__default__"
 
-func (c *ccDynamic) systemSharedLibs() []string {
-
+func (c *ccDynamic) systemSharedLibs(ctx common.AndroidBaseContext) []string {
 	if len(c.properties.System_shared_libs) == 1 &&
 		c.properties.System_shared_libs[0] == defaultSystemSharedLibraries {
 
-		if c.HostOrDevice().Host() {
+		if ctx.Host() {
 			return []string{}
 		} else {
 			return []string{"libc", "libm"}
@@ -725,8 +720,8 @@
 func (c *ccDynamic) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
 	deps := c.ccBase.AndroidDynamicDependencies(ctx)
 
-	if c.HostOrDevice().Device() {
-		ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.systemSharedLibs()...)
+	if ctx.Device() {
+		ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.systemSharedLibs(ctx)...)
 		ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}},
 			"libcompiler_rt-extras",
 			"libgcov",
@@ -752,7 +747,7 @@
 
 	deps, flags := c.ccBase.collectDeps(ctx, flags)
 
-	systemSharedLibs := c.systemSharedLibs()
+	systemSharedLibs := c.systemSharedLibs(ctx)
 	sharedLibNames := make([]string, 0, len(c.properties.Shared_libs)+len(systemSharedLibs)+
 		len(flags.ExtraSharedLibs))
 	sharedLibNames = append(sharedLibNames, c.properties.Shared_libs...)
@@ -761,7 +756,7 @@
 	_, deps.SharedLibs, newIncludeDirs = c.collectDepsFromList(ctx, sharedLibNames)
 	deps.IncludeDirs = append(deps.IncludeDirs, newIncludeDirs...)
 
-	if ctx.Arch().HostOrDevice.Device() {
+	if ctx.Device() {
 		var staticLibs []string
 		staticLibNames := []string{"libcompiler_rt-extras"}
 		_, staticLibs, newIncludeDirs = c.collectDepsFromList(ctx, staticLibNames)
@@ -868,7 +863,7 @@
 func (c *CCLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
 	if c.LibraryProperties.IsShared {
 		deps := c.ccDynamic.AndroidDynamicDependencies(ctx)
-		if c.HostOrDevice().Device() {
+		if ctx.Device() {
 			deps = append(deps, "crtbegin_so", "crtend_so")
 		}
 		return deps
@@ -923,10 +918,10 @@
 		libName := ctx.ModuleName()
 		// GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
 		sharedFlag := "-Wl,-shared"
-		if c.properties.Clang || ctx.Arch().HostOrDevice.Host() {
+		if c.properties.Clang || ctx.Host() {
 			sharedFlag = "-shared"
 		}
-		if ctx.Arch().HostOrDevice.Device() {
+		if ctx.Device() {
 			flags.LdFlags = append(flags.LdFlags, "-nostdlib")
 		}
 
@@ -1135,7 +1130,7 @@
 
 func (c *CCBinary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
 	deps := c.ccDynamic.AndroidDynamicDependencies(ctx)
-	if c.HostOrDevice().Device() {
+	if ctx.Device() {
 		if c.BinaryProperties.Static_executable {
 			deps = append(deps, "crtbegin_static", "crtend_android")
 		} else {
@@ -1161,7 +1156,7 @@
 func (c *CCBinary) ModuleTypeFlags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
 	flags.CFlags = append(flags.CFlags, "-fpie")
 
-	if ctx.Arch().HostOrDevice.Device() {
+	if ctx.Device() {
 		linker := "/system/bin/linker"
 		if flags.Toolchain.Is64Bit() {
 			linker = "/system/bin/linker64"
@@ -1217,7 +1212,7 @@
 	deps, flags := c.CCBinary.collectDeps(ctx, flags)
 
 	flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
-	if c.HostOrDevice().Host() {
+	if ctx.Host() {
 		flags.CFlags = append(flags.CFlags, "-O0", "-g")
 		flags.LdLibs = append(flags.LdLibs, "-lpthread")
 	}
@@ -1239,7 +1234,7 @@
 }
 
 func (c *ccTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
-	if c.HostOrDevice().Device() {
+	if ctx.Device() {
 		ctx.InstallFile("../data/nativetest/"+ctx.ModuleName(), c.out)
 	} else {
 		c.CCBinary.installModule(ctx, flags)
diff --git a/common/arch.go b/common/arch.go
index 8cc3b10..a43aa72 100644
--- a/common/arch.go
+++ b/common/arch.go
@@ -15,12 +15,13 @@
 package common
 
 import (
-	"github.com/google/blueprint"
-	"github.com/google/blueprint/proptools"
 	"fmt"
 	"reflect"
 	"runtime"
 	"strings"
+
+	"github.com/google/blueprint"
+	"github.com/google/blueprint/proptools"
 )
 
 var (
diff --git a/common/module.go b/common/module.go
index 6b2dfb4..4b1200d 100644
--- a/common/module.go
+++ b/common/module.go
@@ -15,8 +15,9 @@
 package common
 
 import (
-	"github.com/google/blueprint"
 	"path/filepath"
+
+	"github.com/google/blueprint"
 )
 
 var (
@@ -28,10 +29,22 @@
 	HostExecutable      = "host_executable"
 )
 
+type androidBaseContext interface {
+	Arch() Arch
+	Host() bool
+	Device() bool
+	Debug() bool
+}
+
+type AndroidBaseContext interface {
+	blueprint.BaseModuleContext
+	androidBaseContext
+}
+
 type AndroidModuleContext interface {
 	blueprint.ModuleContext
+	androidBaseContext
 
-	Arch() Arch
 	InstallFile(installPath, srcPath string)
 	CheckbuildFile(srcPath string)
 }
@@ -52,6 +65,7 @@
 
 type AndroidDynamicDependerModuleContext interface {
 	blueprint.DynamicDependerModuleContext
+	androidBaseContext
 }
 
 type commonProperties struct {
@@ -277,7 +291,9 @@
 func (a *AndroidModuleBase) DynamicDependencies(ctx blueprint.DynamicDependerModuleContext) []string {
 	actx := &androidDynamicDependerContext{
 		DynamicDependerModuleContext: ctx,
-		module: a,
+		androidBaseContextImpl: androidBaseContextImpl{
+			arch: a.commonProperties.CompileArch,
+		},
 	}
 
 	if dynamic, ok := a.module.(AndroidDynamicDepender); ok {
@@ -290,9 +306,11 @@
 func (a *AndroidModuleBase) GenerateBuildActions(ctx blueprint.ModuleContext) {
 	androidCtx := &androidModuleContext{
 		ModuleContext: ctx,
-		installDeps:   a.computeInstallDeps(ctx),
-		installFiles:  a.installFiles,
-		arch:          a.commonProperties.CompileArch,
+		androidBaseContextImpl: androidBaseContextImpl{
+			arch: a.commonProperties.CompileArch,
+		},
+		installDeps:  a.computeInstallDeps(ctx),
+		installFiles: a.installFiles,
 	}
 
 	if a.commonProperties.Disabled {
@@ -313,9 +331,14 @@
 	a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
 }
 
+type androidBaseContextImpl struct {
+	arch  Arch
+	debug bool
+}
+
 type androidModuleContext struct {
 	blueprint.ModuleContext
-	arch            Arch
+	androidBaseContextImpl
 	installDeps     []string
 	installFiles    []string
 	checkbuildFiles []string
@@ -326,10 +349,22 @@
 	a.ModuleContext.Build(pctx, params)
 }
 
-func (a *androidModuleContext) Arch() Arch {
+func (a *androidBaseContextImpl) Arch() Arch {
 	return a.arch
 }
 
+func (a *androidBaseContextImpl) Host() bool {
+	return a.arch.HostOrDevice.Host()
+}
+
+func (a *androidBaseContextImpl) Device() bool {
+	return a.arch.HostOrDevice.Device()
+}
+
+func (a *androidBaseContextImpl) Debug() bool {
+	return a.debug
+}
+
 func (a *androidModuleContext) InstallFile(installPath, srcPath string) {
 	var fullInstallPath string
 	if a.arch.HostOrDevice.Device() {
@@ -357,7 +392,7 @@
 
 type androidDynamicDependerContext struct {
 	blueprint.DynamicDependerModuleContext
-	module *AndroidModuleBase
+	androidBaseContextImpl
 }
 
 type fileInstaller interface {