Add binary prebuilt tools needed for host gtests

Bug: 147817558
Test: check testcases directory after "m general-tests"
Change-Id: If33dfdc6b3ad4792918383bf1481369735007113
diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk
index 083bb9e..8e7e7c9 100644
--- a/build/Android.gtest.mk
+++ b/build/Android.gtest.mk
@@ -51,7 +51,7 @@
 LOCAL_MODULE_CLASS := NATIVE_TESTS
 LOCAL_MODULE_SUFFIX := .txt
 LOCAL_COMPATIBILITY_SUITE := general-tests
-LOCAL_COMPATIBILITY_SUPPORT_FILES := \
+LOCAL_COMPATIBILITY_SUPPORT_FILES := $(ART_TESTCASES_PREBUILT_CONTENT) \
 	$(foreach f,$(my_files),$(call word-colon,1,$f):out/host/linux-x86/$(call word-colon,2,$f))
 include $(BUILD_SYSTEM)/base_rules.mk
 
diff --git a/build/art.go b/build/art.go
index 8deea23..af2a5de 100644
--- a/build/art.go
+++ b/build/art.go
@@ -291,10 +291,11 @@
 	defer artTestMutex.Unlock()
 
 	if ctx.Os().Class == android.Host {
-		path := ctx.Path().ToMakePath().String()
-		parts := strings.Split(path, "/")
-		// Keep last two parts of the path (e.g. bin/dex2oat).
-		testcasesContent[strings.Join(parts[len(parts)-2:], "/")] = path
+		src := ctx.SrcPath().String()
+		path := strings.Split(ctx.Path().ToMakePath().String(), "/")
+		// Keep last two parts of the install path (e.g. bin/dex2oat).
+		dst := strings.Join(path[len(path)-2:], "/")
+		testcasesContent[dst] = src
 	}
 }
 
diff --git a/build/makevars.go b/build/makevars.go
index 6e66966..22ef205 100644
--- a/build/makevars.go
+++ b/build/makevars.go
@@ -15,18 +15,22 @@
 package art
 
 import (
+	"path/filepath"
 	"sort"
 	"strings"
 
 	"android/soong/android"
+	"android/soong/cc/config"
 )
 
 var (
-	pctx = android.NewPackageContext("android/soong/art")
+	pctx                  = android.NewPackageContext("android/soong/art")
+	prebuiltToolsForTests = []string{"as", "addr2line", "objdump"}
 )
 
 func init() {
 	android.RegisterMakeVarsProvider(pctx, makeVarsProvider)
+	pctx.Import("android/soong/cc/config")
 }
 
 func makeVarsProvider(ctx android.MakeVarsContext) {
@@ -52,4 +56,25 @@
 		copy_cmds = append(copy_cmds, testcasesContent[key]+":"+key)
 	}
 	ctx.Strict("ART_TESTCASES_CONTENT", strings.Join(copy_cmds, " "))
+
+	// Add prebuilt tools.
+	copy_cmds = []string{}
+	for _, cmd := range prebuiltToolsForTests {
+		target := ctx.Config().Targets[android.BuildOs][0]
+		toolchain := config.FindToolchain(target.Os, target.Arch)
+		gccRoot, gccTriple := toolchain.GccRoot(), toolchain.GccTriple()
+		eval := func(path ...string) string {
+			result, err := ctx.Eval(filepath.Join(path...))
+			if err != nil {
+				panic(err)
+			}
+			return result
+		}
+		src := eval(gccRoot, "bin", gccTriple+"-"+cmd)
+		// Different tests use different paths, so we need to copy to two locations.
+		// TODO: Unify the test code so that this is no longer necessary.
+		copy_cmds = append(copy_cmds, src+":"+eval(gccRoot, "bin", gccTriple+"-"+cmd))
+		copy_cmds = append(copy_cmds, src+":"+eval(gccRoot, gccTriple, "bin", cmd))
+	}
+	ctx.Strict("ART_TESTCASES_PREBUILT_CONTENT", strings.Join(copy_cmds, " "))
 }