Copy tools needed by host gtests to testcases directory.

Atest requires that everything is in the testcases directory.
The files are not used yet (this is left to follow-up CLs).

Bug: 147819342
Test: check the generated general-tests.zip ("m general-tests")
Change-Id: Ied22a736e81a7cff03641d18bacd829b86a6ef0b
diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk
index dc5015f..21199da 100644
--- a/build/Android.gtest.mk
+++ b/build/Android.gtest.mk
@@ -17,6 +17,52 @@
 # Build rules are excluded from Mac, since we can not run ART tests there in the first place.
 ifneq ($(HOST_OS),darwin)
 
+###################################################################################################
+# Create module in testcases to hold all common data and tools needed for ART host tests.
+
+# ART binary tools and libraries (automatic list of all art_cc_binary/art_cc_library modules).
+my_files := $(ART_TESTCASES_CONTENT)
+
+# Manually add system libraries that we need to run the host ART tools.
+my_files += \
+  $(foreach lib, \
+    libbacktrace libbase libc++ libicu_jni liblog libsigchain libunwindstack libziparchive, \
+    $(call intermediates-dir-for,SHARED_LIBRARIES,$(lib),HOST)/$(lib).so:lib64/$(lib).so) \
+  $(foreach lib, \
+    libcrypto libz, \
+    $(call intermediates-dir-for,SHARED_LIBRARIES,$(lib),HOST)/$(lib).so:lib64/$(lib)-host.so)
+
+# Add apex directories for art, conscrypt and i18n.
+my_files += $(foreach infix,_ _VDEX_,$(foreach suffix,$(HOST_ARCH) $(HOST_2ND_ARCH), \
+  $(DEXPREOPT_IMAGE$(infix)BUILT_INSTALLED_art_host_$(suffix))))
+my_files += \
+  $(foreach jar,$(CORE_IMG_JARS),\
+    $(HOST_OUT_JAVA_LIBRARIES)/$(jar)-hostdex.jar:apex/com.android.art/javalib/$(jar).jar) \
+  $(HOST_OUT_JAVA_LIBRARIES)/conscrypt-hostdex.jar:apex/com.android.conscrypt/javalib/conscrypt.jar\
+  $(HOST_OUT_JAVA_LIBRARIES)/core-icu4j-hostdex.jar:apex/com.android.i18n/javalib/core-icu4j.jar
+
+# Create dummy module that will copy all the data files into testcases directory.
+# For now, this copies everything to "out/host/linux-x86/" subdirectory, since it
+# is hard-coded in many places. TODO: Refactor tests to remove the need for this.
+include $(CLEAR_VARS)
+LOCAL_IS_HOST_MODULE := true
+LOCAL_MODULE := art_common
+LOCAL_MODULE_TAGS := tests
+LOCAL_MODULE_CLASS := NATIVE_TESTS
+LOCAL_MODULE_SUFFIX := .txt
+LOCAL_COMPATIBILITY_SUITE := general-tests
+LOCAL_COMPATIBILITY_SUPPORT_FILES := \
+	$(foreach f,$(my_files),$(call word-colon,1,$f):out/host/linux-x86/$(call word-colon,2,$f))
+include $(BUILD_SYSTEM)/base_rules.mk
+
+$(LOCAL_BUILT_MODULE):
+	@mkdir -p $(dir $@)
+	echo "This directory contains common data and tools needed for ART host tests" > $@
+
+my_files :=
+include $(CLEAR_VARS)
+###################################################################################################
+
 # The path for which all the dex files are relative, not actually the current directory.
 LOCAL_PATH := art/test
 
diff --git a/build/art.go b/build/art.go
index 5a09be0..6b684c2 100644
--- a/build/art.go
+++ b/build/art.go
@@ -17,6 +17,7 @@
 import (
 	"fmt"
 	"log"
+	"strings"
 	"sync"
 
 	"github.com/google/blueprint/proptools"
@@ -274,6 +275,32 @@
 	testMap[name] = tests
 }
 
+var testcasesContentKey = android.NewOnceKey("artTestcasesContent")
+
+func testcasesContent(config android.Config) map[string]string {
+	return config.Once(testcasesContentKey, func() interface{} {
+		return make(map[string]string)
+	}).(map[string]string)
+}
+
+// Binaries and libraries also need to be copied in the testcases directory for
+// running tests on host.  This method adds module to the list of needed files.
+// The 'key' is the file in testcases and 'value' is the path to copy it from.
+// The actual copy will be done in make since soong does not do installations.
+func addTestcasesFile(ctx android.InstallHookContext) {
+	testcasesContent := testcasesContent(ctx.Config())
+
+	artTestMutex.Lock()
+	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
+	}
+}
+
 var artTestMutex sync.Mutex
 
 func init() {
@@ -392,6 +419,7 @@
 
 	installCodegenCustomizer(module, staticAndSharedLibrary)
 
+	android.AddInstallHook(module, addTestcasesFile)
 	return module
 }
 
@@ -408,6 +436,7 @@
 
 	android.AddLoadHook(module, customLinker)
 	android.AddLoadHook(module, prefer32Bit)
+	android.AddInstallHook(module, addTestcasesFile)
 	return module
 }
 
diff --git a/build/makevars.go b/build/makevars.go
index 1faa0f6..6e66966 100644
--- a/build/makevars.go
+++ b/build/makevars.go
@@ -44,4 +44,12 @@
 	for _, name := range testNames {
 		ctx.Strict("ART_TEST_LIST_"+name, strings.Join(testMap[name], " "))
 	}
+
+	// Create list of copy commands to install the content of the testcases directory.
+	testcasesContent := testcasesContent(ctx.Config())
+	copy_cmds := []string{}
+	for _, key := range android.SortedStringKeys(testcasesContent) {
+		copy_cmds = append(copy_cmds, testcasesContent[key]+":"+key)
+	}
+	ctx.Strict("ART_TESTCASES_CONTENT", strings.Join(copy_cmds, " "))
 }
diff --git a/libdexfile/Android.bp b/libdexfile/Android.bp
index 5a8cdb3..9fb6c8d 100644
--- a/libdexfile/Android.bp
+++ b/libdexfile/Android.bp
@@ -320,7 +320,7 @@
     ],
 }
 
-cc_library {
+art_cc_library {
     name: "libdexfile_external",
     defaults: [
         "art_defaults",
@@ -341,7 +341,7 @@
     ],
 }
 
-cc_library {
+art_cc_library {
     name: "libdexfiled_external",
     defaults: [
         "art_debug_defaults",
@@ -380,7 +380,7 @@
 // time dependency on dex file logic. It is therefore safe to use from binaries
 // compiled without dex file support, given they won't encounter any dex file
 // stack frames.
-cc_library {
+art_cc_library {
     name: "libdexfile_support",
     visibility: ["//visibility:public"],
     host_supported: true,
diff --git a/libnativebridge/Android.bp b/libnativebridge/Android.bp
index e05771a..c0f4791 100644
--- a/libnativebridge/Android.bp
+++ b/libnativebridge/Android.bp
@@ -27,7 +27,7 @@
     ],
 }
 
-cc_library {
+art_cc_library {
     name: "libnativebridge",
     defaults: ["libnativebridge-defaults"],
     visibility: [
diff --git a/libnativeloader/Android.bp b/libnativeloader/Android.bp
index 645cf64..5f29b4d 100644
--- a/libnativeloader/Android.bp
+++ b/libnativeloader/Android.bp
@@ -10,7 +10,7 @@
     export_header_lib_headers: ["libnativeloader-headers"],
 }
 
-cc_library {
+art_cc_library {
     name: "libnativeloader",
     defaults: ["libnativeloader-defaults"],
     visibility: [