Supported python build in host side.

The base module handles all the common functionalites, such as version
compatibilty check, version variations split, source file format check,
source/data file duplicate check.

The library/binary module focuses on how to generate binary build actions,
such as setting up stub script, zipping, filling in __init__.py in
runfiles dir tree.

Bug: b/31676493
Test: go test under python package

Change-Id: I06608369f350f7195873d459e1c8d1bdb811e77e
diff --git a/android/androidmk.go b/android/androidmk.go
index af6608f..b81cc2a 100644
--- a/android/androidmk.go
+++ b/android/androidmk.go
@@ -217,13 +217,22 @@
 	host := false
 	switch amod.Os().Class {
 	case Host:
-		fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
+		// Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
+		if archStr != "common" {
+			fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
+		}
 		host = true
 	case HostCross:
-		fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
+		// Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
+		if archStr != "common" {
+			fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
+		}
 		host = true
 	case Device:
-		fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
+		// Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
+		if archStr != "common" {
+			fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
+		}
 
 		if len(amod.commonProperties.Logtags) > 0 {
 			fmt.Fprintln(w, "LOCAL_LOGTAGS_FILES := ", strings.Join(amod.commonProperties.Logtags, " "))
diff --git a/android/arch.go b/android/arch.go
index e21a070..39477ad 100644
--- a/android/arch.go
+++ b/android/arch.go
@@ -189,7 +189,8 @@
 }()
 
 var (
-	osTypeList []OsType
+	osTypeList      []OsType
+	commonTargetMap = make(map[string]Target)
 
 	NoOsType    OsType
 	Linux       = NewOsType("linux", Host, false)
@@ -236,6 +237,13 @@
 		DefaultDisabled: defDisabled,
 	}
 	osTypeList = append(osTypeList, os)
+
+	if _, found := commonTargetMap[name]; found {
+		panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
+	} else {
+		commonTargetMap[name] = Target{Os: os, Arch: Arch{ArchType: Common}}
+	}
+
 	return os
 }
 
@@ -249,15 +257,6 @@
 	return NoOsType
 }
 
-var (
-	commonTarget = Target{
-		Os: Android,
-		Arch: Arch{
-			ArchType: Common,
-		},
-	}
-)
-
 type Target struct {
 	Os   OsType
 	Arch Arch
@@ -989,6 +988,20 @@
 	return ret
 }
 
+func getCommonTargets(targets []Target) []Target {
+	var ret []Target
+	set := make(map[string]bool)
+
+	for _, t := range targets {
+		if _, found := set[t.Os.String()]; !found {
+			set[t.Os.String()] = true
+			ret = append(ret, commonTargetMap[t.Os.String()])
+		}
+	}
+
+	return ret
+}
+
 // Use the module multilib setting to select one or more targets from a target list
 func decodeMultilib(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
 	buildTargets := []Target{}
@@ -1001,7 +1014,7 @@
 	}
 	switch multilib {
 	case "common":
-		buildTargets = append(buildTargets, commonTarget)
+		buildTargets = append(buildTargets, getCommonTargets(targets)...)
 	case "both":
 		if prefer32 {
 			buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
diff --git a/android/mutator.go b/android/mutator.go
index 940b0ff..bb49487 100644
--- a/android/mutator.go
+++ b/android/mutator.go
@@ -81,8 +81,18 @@
 }
 
 func RegisterTestMutators(ctx *blueprint.Context) {
-	mutators := registerMutatorsContext{}
+	mutators := &registerMutatorsContext{}
+
+	register := func(funcs []RegisterMutatorFunc) {
+		for _, f := range funcs {
+			f(mutators)
+		}
+	}
+
+	register(testPreDeps)
 	mutators.BottomUp("deps", depsMutator).Parallel()
+	register(testPostDeps)
+
 	registerMutatorsToContext(ctx, mutators.mutators)
 }
 
@@ -97,7 +107,7 @@
 
 type RegisterMutatorFunc func(RegisterMutatorsContext)
 
-var preArch, preDeps, postDeps []RegisterMutatorFunc
+var preArch, preDeps, postDeps, testPreDeps, testPostDeps []RegisterMutatorFunc
 
 func PreArchMutators(f RegisterMutatorFunc) {
 	preArch = append(preArch, f)
@@ -111,6 +121,14 @@
 	postDeps = append(postDeps, f)
 }
 
+func TestPreDepsMutators(f RegisterMutatorFunc) {
+	testPreDeps = append(testPreDeps, f)
+}
+
+func TeststPostDepsMutators(f RegisterMutatorFunc) {
+	testPostDeps = append(testPostDeps, f)
+}
+
 type AndroidTopDownMutator func(TopDownMutatorContext)
 
 type TopDownMutatorContext interface {