Simplify arch target handling

Soong's multi-architecture building has grown complex, with the
combination of HostOrDevice+HostType+Arch necessary to determine how to
build a variant of a module, and three separate mutators to split each
into its variations.

Combine HostOrDevice+HostType into Os, which will be Linux, Darwin,
Windows, or Android.  Store Os+Arch as a single Target.

Change-Id: I92f2e2dac53617d595a35cc285d2bd348baa0fbd
diff --git a/cc/arm64_device.go b/cc/arm64_device.go
index 4e41287..8e7c57b 100644
--- a/cc/arm64_device.go
+++ b/cc/arm64_device.go
@@ -208,5 +208,5 @@
 }
 
 func init() {
-	registerDeviceToolchainFactory(android.Arm64, arm64ToolchainFactory)
+	registerToolchainFactory(android.Android, android.Arm64, arm64ToolchainFactory)
 }
diff --git a/cc/arm_device.go b/cc/arm_device.go
index 766eea6..1624cfc 100644
--- a/cc/arm_device.go
+++ b/cc/arm_device.go
@@ -392,5 +392,5 @@
 }
 
 func init() {
-	registerDeviceToolchainFactory(android.Arm, armToolchainFactory)
+	registerToolchainFactory(android.Android, android.Arm, armToolchainFactory)
 }
diff --git a/cc/cc.go b/cc/cc.go
index bac3e88..3cc0d0d 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -118,7 +118,7 @@
 )
 
 func init() {
-	if android.CurrentHostType() == android.Linux {
+	if android.BuildOs == android.Linux {
 		commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
 	}
 
@@ -729,11 +729,10 @@
 func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
 	if c.cachedToolchain == nil {
 		arch := ctx.Arch()
-		hod := ctx.HostOrDevice()
-		ht := ctx.HostType()
-		factory := toolchainFactories[hod][ht][arch.ArchType]
+		os := ctx.Os()
+		factory := toolchainFactories[os][arch.ArchType]
 		if factory == nil {
-			ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
+			ctx.ModuleErrorf("Toolchain not found for %s arch %q", os.String(), arch.String())
 			return nil
 		}
 		c.cachedToolchain = factory(arch)
@@ -905,8 +904,13 @@
 			return
 		}
 
-		if a.HostOrDevice() != ctx.HostOrDevice() {
-			ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(), name)
+		if a.Target().Os != ctx.Os() {
+			ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
+			return
+		}
+
+		if a.Target().Arch.ArchType != ctx.Arch().ArchType {
+			ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
 			return
 		}
 
@@ -1081,6 +1085,11 @@
 		flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
 	}
 
+	hod := "host"
+	if ctx.Os().Class == android.Device {
+		hod = "device"
+	}
+
 	if !ctx.noDefaultCompilerFlags() {
 		flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
 
@@ -1090,7 +1099,7 @@
 			flags.GlobalFlags = append(flags.GlobalFlags,
 				toolchain.ClangCflags(),
 				"${commonClangGlobalCflags}",
-				fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
+				fmt.Sprintf("${%sClangGlobalCflags}", hod))
 
 			flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
 		} else {
@@ -1098,7 +1107,7 @@
 			flags.GlobalFlags = append(flags.GlobalFlags,
 				toolchain.Cflags(),
 				"${commonGlobalCflags}",
-				fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
+				fmt.Sprintf("${%sGlobalCflags}", hod))
 		}
 
 		if Bool(ctx.AConfig().ProductVariables.Brillo) {
@@ -1410,7 +1419,7 @@
 	// MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
 	// all code is position independent, and then those warnings get promoted to
 	// errors.
-	if ctx.HostType() != android.Windows {
+	if ctx.Os() != android.Windows {
 		flags.CFlags = append(flags.CFlags, "-fPIC")
 	}
 
@@ -1863,7 +1872,7 @@
 
 	static := Bool(binary.Properties.Static_executable)
 	if ctx.Host() {
-		if ctx.HostType() == android.Linux {
+		if ctx.Os() == android.Linux {
 			if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
 				static = true
 			}
@@ -1883,7 +1892,7 @@
 
 	if ctx.Host() && !binary.staticBinary() {
 		flags.LdFlags = append(flags.LdFlags, "-pie")
-		if ctx.HostType() == android.Windows {
+		if ctx.Os() == android.Windows {
 			flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
 		}
 	}
@@ -1891,7 +1900,7 @@
 	// MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
 	// all code is position independent, and then those warnings get promoted to
 	// errors.
-	if ctx.HostType() != android.Windows {
+	if ctx.Os() != android.Windows {
 		flags.CFlags = append(flags.CFlags, "-fpie")
 	}
 
@@ -1945,7 +1954,7 @@
 	fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
 	outputFile := android.PathForModuleOut(ctx, fileName)
 	ret := outputFile
-	if ctx.HostOrDevice().Host() {
+	if ctx.Os().Class == android.Host {
 		binary.hostToolPath = android.OptionalPathForPath(outputFile)
 	}
 
@@ -2052,7 +2061,7 @@
 	if ctx.Host() {
 		flags.CFlags = append(flags.CFlags, "-O0", "-g")
 
-		switch ctx.HostType() {
+		switch ctx.Os() {
 		case android.Windows:
 			flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
 		case android.Linux:
diff --git a/cc/makevars.go b/cc/makevars.go
index 863e3dc..f107eff 100644
--- a/cc/makevars.go
+++ b/cc/makevars.go
@@ -41,49 +41,54 @@
 	ctx.Strict("GLOBAL_CPPFLAGS_NO_OVERRIDE", "")
 	ctx.Strict("GLOBAL_CLANG_CPPFLAGS_NO_OVERRIDE", "")
 
-	hostType := android.CurrentHostType()
-	arches := ctx.Config().HostArches[hostType]
-	makeVarsToolchain(ctx, "", android.Host, hostType, arches[0])
-	if len(arches) > 1 {
-		makeVarsToolchain(ctx, "2ND_", android.Host, hostType, arches[1])
+	hostTargets := ctx.Config().Targets[android.Host]
+	makeVarsToolchain(ctx, "", hostTargets[0])
+	if len(hostTargets) > 1 {
+		makeVarsToolchain(ctx, "2ND_", hostTargets[1])
 	}
 
-	if winArches, ok := ctx.Config().HostArches[android.Windows]; ok {
-		makeVarsToolchain(ctx, "", android.Host, android.Windows, winArches[0])
-		if len(winArches) > 1 {
-			makeVarsToolchain(ctx, "2ND_", android.Host, android.Windows, winArches[1])
+	crossTargets := ctx.Config().Targets[android.HostCross]
+	if len(crossTargets) > 0 {
+		makeVarsToolchain(ctx, "", crossTargets[0])
+		if len(crossTargets) > 1 {
+			makeVarsToolchain(ctx, "2ND_", crossTargets[1])
 		}
 	}
 
-	arches = ctx.Config().DeviceArches
-	makeVarsToolchain(ctx, "", android.Device, android.NoHostType, arches[0])
-	if len(arches) > 1 {
-		makeVarsToolchain(ctx, "2ND_", android.Device, android.NoHostType, arches[1])
+	deviceTargets := ctx.Config().Targets[android.Device]
+	makeVarsToolchain(ctx, "", deviceTargets[0])
+	if len(deviceTargets) > 1 {
+		makeVarsToolchain(ctx, "2ND_", deviceTargets[1])
 	}
 }
 
 func makeVarsToolchain(ctx android.MakeVarsContext, secondPrefix string,
-	hod android.HostOrDevice, ht android.HostType, arch android.Arch) {
+	target android.Target) {
 	var typePrefix string
-	if hod.Host() {
-		if ht == android.Windows {
-			typePrefix = "HOST_CROSS_"
-		} else {
-			typePrefix = "HOST_"
-		}
-	} else {
+	switch target.Os.Class {
+	case android.Host:
+		typePrefix = "HOST_"
+	case android.HostCross:
+		typePrefix = "HOST_CROSS_"
+	case android.Device:
 		typePrefix = "TARGET_"
 	}
 	makePrefix := secondPrefix + typePrefix
 
-	toolchain := toolchainFactories[hod][ht][arch.ArchType](arch)
+	toolchain := toolchainFactories[target.Os][target.Arch.ArchType](target.Arch)
 
 	var productExtraCflags string
 	var productExtraLdflags string
-	if hod.Device() && Bool(ctx.Config().ProductVariables.Brillo) {
+
+	hod := "host"
+	if target.Os.Class == android.Device {
+		hod = "device"
+	}
+
+	if target.Os.Class == android.Device && Bool(ctx.Config().ProductVariables.Brillo) {
 		productExtraCflags += "-D__BRILLO__"
 	}
-	if hod.Host() && Bool(ctx.Config().ProductVariables.HostStaticBinaries) {
+	if target.Os.Class == android.Host && Bool(ctx.Config().ProductVariables.HostStaticBinaries) {
 		productExtraLdflags += "-static"
 	}
 
@@ -108,23 +113,29 @@
 	ctx.Strict(makePrefix+"SYSTEMCPP_LDFLAGS", toolchain.SystemCppLdflags())
 
 	includeFlags, err := ctx.Eval(toolchain.IncludeFlags())
-	if err != nil { panic(err) }
+	if err != nil {
+		panic(err)
+	}
 	ctx.StrictRaw(makePrefix+"C_INCLUDES", strings.Replace(includeFlags, "-isystem ", "", -1))
 
-	if arch.ArchType == android.Arm {
+	if target.Arch.ArchType == android.Arm {
 		flags, err := toolchain.InstructionSetFlags("arm")
-		if err != nil { panic(err) }
+		if err != nil {
+			panic(err)
+		}
 		ctx.Strict(makePrefix+"arm_CFLAGS", flags)
 
 		flags, err = toolchain.InstructionSetFlags("thumb")
-		if err != nil { panic(err) }
+		if err != nil {
+			panic(err)
+		}
 		ctx.Strict(makePrefix+"thumb_CFLAGS", flags)
 	}
 
 	if toolchain.ClangSupported() {
 		clangPrefix := secondPrefix + "CLANG_" + typePrefix
 		clangExtras := "-target " + toolchain.ClangTriple()
-		if ht != android.Darwin {
+		if target.Os != android.Darwin {
 			clangExtras += " -B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
 		}
 
@@ -148,19 +159,19 @@
 			clangExtras,
 		}, " "))
 
-		if hod.Device() {
-			ctx.Strict(secondPrefix + "ADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(toolchain.AddressSanitizerRuntimeLibrary(), ".so"))
+		if target.Os.Class == android.Device {
+			ctx.Strict(secondPrefix+"ADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(toolchain.AddressSanitizerRuntimeLibrary(), ".so"))
 		}
 
 		// This is used by external/gentoo/...
-		ctx.Strict("CLANG_CONFIG_" + arch.ArchType.Name + "_" + typePrefix + "TRIPLE",
+		ctx.Strict("CLANG_CONFIG_"+target.Arch.ArchType.Name+"_"+typePrefix+"TRIPLE",
 			toolchain.ClangTriple())
 	}
 
 	ctx.Strict(makePrefix+"CC", gccCmd(toolchain, "gcc"))
 	ctx.Strict(makePrefix+"CXX", gccCmd(toolchain, "g++"))
 
-	if ht == android.Darwin {
+	if target.Os == android.Darwin {
 		ctx.Strict(makePrefix+"AR", "${macArPath}")
 	} else {
 		ctx.Strict(makePrefix+"AR", gccCmd(toolchain, "ar"))
@@ -168,11 +179,11 @@
 		ctx.Strict(makePrefix+"NM", gccCmd(toolchain, "nm"))
 	}
 
-	if ht == android.Windows {
+	if target.Os == android.Windows {
 		ctx.Strict(makePrefix+"OBJDUMP", gccCmd(toolchain, "objdump"))
 	}
 
-	if hod.Device() {
+	if target.Os.Class == android.Device {
 		ctx.Strict(makePrefix+"OBJCOPY", gccCmd(toolchain, "objcopy"))
 		ctx.Strict(makePrefix+"LD", gccCmd(toolchain, "ld"))
 		ctx.Strict(makePrefix+"STRIP", gccCmd(toolchain, "strip"))
diff --git a/cc/mips64_device.go b/cc/mips64_device.go
index 04e31a9..474f284 100644
--- a/cc/mips64_device.go
+++ b/cc/mips64_device.go
@@ -200,5 +200,5 @@
 }
 
 func init() {
-	registerDeviceToolchainFactory(android.Mips64, mips64ToolchainFactory)
+	registerToolchainFactory(android.Android, android.Mips64, mips64ToolchainFactory)
 }
diff --git a/cc/mips_device.go b/cc/mips_device.go
index 0dbbbd7..f9dc5c6 100644
--- a/cc/mips_device.go
+++ b/cc/mips_device.go
@@ -248,5 +248,5 @@
 }
 
 func init() {
-	registerDeviceToolchainFactory(android.Mips, mipsToolchainFactory)
+	registerToolchainFactory(android.Android, android.Mips, mipsToolchainFactory)
 }
diff --git a/cc/stl.go b/cc/stl.go
index cdc887e..c5dbae3 100644
--- a/cc/stl.go
+++ b/cc/stl.go
@@ -52,7 +52,7 @@
 				ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", stl.Properties.Stl)
 				return ""
 			}
-		} else if ctx.HostType() == android.Windows {
+		} else if ctx.Os() == android.Windows {
 			switch stl.Properties.Stl {
 			case "libc++", "libc++_static", "libstdc++", "":
 				// libc++ is not supported on mingw
@@ -60,7 +60,7 @@
 			case "none":
 				return ""
 			default:
-				ctx.ModuleErrorf("stl: %q is not a supported STL", stl.Properties.Stl)
+				ctx.ModuleErrorf("stl: %q is not a supported STL for windows", stl.Properties.Stl)
 				return ""
 			}
 		} else {
@@ -133,9 +133,9 @@
 			flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
 			flags.LdFlags = append(flags.LdFlags, "-lpthread", "-lm")
 			if ctx.staticBinary() {
-				flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
+				flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...)
 			} else {
-				flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
+				flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...)
 			}
 		} else {
 			if ctx.Arch().ArchType == android.Arm {
@@ -167,9 +167,9 @@
 			flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
 			flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
 			if ctx.staticBinary() {
-				flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
+				flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.Os()]...)
 			} else {
-				flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
+				flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.Os()]...)
 			}
 		}
 	default:
@@ -179,10 +179,10 @@
 	return flags
 }
 
-var hostDynamicGccLibs, hostStaticGccLibs map[android.HostType][]string
+var hostDynamicGccLibs, hostStaticGccLibs map[android.OsType][]string
 
 func init() {
-	hostDynamicGccLibs = map[android.HostType][]string{
+	hostDynamicGccLibs = map[android.OsType][]string{
 		android.Linux:  []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
 		android.Darwin: []string{"-lc", "-lSystem"},
 		android.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
@@ -190,7 +190,7 @@
 			"-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
 			"-lmsvcrt"},
 	}
-	hostStaticGccLibs = map[android.HostType][]string{
+	hostStaticGccLibs = map[android.OsType][]string{
 		android.Linux:   []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
 		android.Darwin:  []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
 		android.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
diff --git a/cc/toolchain.go b/cc/toolchain.go
index efb7953..87657ef 100644
--- a/cc/toolchain.go
+++ b/cc/toolchain.go
@@ -22,23 +22,13 @@
 
 type toolchainFactory func(arch android.Arch) Toolchain
 
-var toolchainFactories = map[android.HostOrDevice]map[android.HostType]map[android.ArchType]toolchainFactory{
-	android.Host: map[android.HostType]map[android.ArchType]toolchainFactory{
-		android.Linux:   make(map[android.ArchType]toolchainFactory),
-		android.Darwin:  make(map[android.ArchType]toolchainFactory),
-		android.Windows: make(map[android.ArchType]toolchainFactory),
-	},
-	android.Device: map[android.HostType]map[android.ArchType]toolchainFactory{
-		android.NoHostType: make(map[android.ArchType]toolchainFactory),
-	},
-}
+var toolchainFactories = make(map[android.OsType]map[android.ArchType]toolchainFactory)
 
-func registerDeviceToolchainFactory(arch android.ArchType, factory toolchainFactory) {
-	toolchainFactories[android.Device][android.NoHostType][arch] = factory
-}
-
-func registerHostToolchainFactory(ht android.HostType, arch android.ArchType, factory toolchainFactory) {
-	toolchainFactories[android.Host][ht][arch] = factory
+func registerToolchainFactory(os android.OsType, arch android.ArchType, factory toolchainFactory) {
+	if toolchainFactories[os] == nil {
+		toolchainFactories[os] = make(map[android.ArchType]toolchainFactory)
+	}
+	toolchainFactories[os][arch] = factory
 }
 
 type Toolchain interface {
diff --git a/cc/x86_64_device.go b/cc/x86_64_device.go
index 9a0d663..986dc86 100644
--- a/cc/x86_64_device.go
+++ b/cc/x86_64_device.go
@@ -263,5 +263,5 @@
 }
 
 func init() {
-	registerDeviceToolchainFactory(android.X86_64, x86_64ToolchainFactory)
+	registerToolchainFactory(android.Android, android.X86_64, x86_64ToolchainFactory)
 }
diff --git a/cc/x86_darwin_host.go b/cc/x86_darwin_host.go
index f3cf1c9..987b1dd 100644
--- a/cc/x86_darwin_host.go
+++ b/cc/x86_darwin_host.go
@@ -280,6 +280,6 @@
 }
 
 func init() {
-	registerHostToolchainFactory(android.Darwin, android.X86, darwinX86ToolchainFactory)
-	registerHostToolchainFactory(android.Darwin, android.X86_64, darwinX8664ToolchainFactory)
+	registerToolchainFactory(android.Darwin, android.X86, darwinX86ToolchainFactory)
+	registerToolchainFactory(android.Darwin, android.X86_64, darwinX8664ToolchainFactory)
 }
diff --git a/cc/x86_device.go b/cc/x86_device.go
index e47c6fb..f16e68b 100644
--- a/cc/x86_device.go
+++ b/cc/x86_device.go
@@ -286,5 +286,5 @@
 }
 
 func init() {
-	registerDeviceToolchainFactory(android.X86, x86ToolchainFactory)
+	registerToolchainFactory(android.Android, android.X86, x86ToolchainFactory)
 }
diff --git a/cc/x86_linux_host.go b/cc/x86_linux_host.go
index a75de90..b2d8462 100644
--- a/cc/x86_linux_host.go
+++ b/cc/x86_linux_host.go
@@ -254,6 +254,6 @@
 }
 
 func init() {
-	registerHostToolchainFactory(android.Linux, android.X86, linuxX86ToolchainFactory)
-	registerHostToolchainFactory(android.Linux, android.X86_64, linuxX8664ToolchainFactory)
+	registerToolchainFactory(android.Linux, android.X86, linuxX86ToolchainFactory)
+	registerToolchainFactory(android.Linux, android.X86_64, linuxX8664ToolchainFactory)
 }
diff --git a/cc/x86_windows_host.go b/cc/x86_windows_host.go
index 9f5cdc4..cc0ef66 100644
--- a/cc/x86_windows_host.go
+++ b/cc/x86_windows_host.go
@@ -199,6 +199,6 @@
 }
 
 func init() {
-	registerHostToolchainFactory(android.Windows, android.X86, windowsX86ToolchainFactory)
-	registerHostToolchainFactory(android.Windows, android.X86_64, windowsX8664ToolchainFactory)
+	registerToolchainFactory(android.Windows, android.X86, windowsX86ToolchainFactory)
+	registerToolchainFactory(android.Windows, android.X86_64, windowsX8664ToolchainFactory)
 }