Support a chroot-based environment in ART Buildbot's target harness.

When ART_TEST_CHROOT is defined, instead of having the ART Buildbot
install ART into a local path (set with ART_TEST_ANDROID_ROOT) and
hijack the linker (using CUSTOM_TARGET_LINKER), prepare and use a
chroot environment (set with ART_TEST_CHROOT).

In this scenario, ART and its dependencies can be built and used
normally (in particular, when building from the master-art Android
manifest) and still be executed as a standalone runtime outside of
the device's /system path, for testing purposes.

Test: Have the ART Buildbot build and run ART on device with chroot.
Bug: 34729697
Bug: 68125496
Change-Id: I08f1acd0d2813584f703fedb84e69df954cbdbda
diff --git a/tools/cleanup-buildbot-device.sh b/tools/cleanup-buildbot-device.sh
new file mode 100755
index 0000000..53072ae
--- /dev/null
+++ b/tools/cleanup-buildbot-device.sh
@@ -0,0 +1,64 @@
+#!/bin/bash
+#
+# Copyright (C) 2017 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+green='\033[0;32m'
+nc='\033[0m'
+
+# Setup as root, as device cleanup requires it.
+adb root
+adb wait-for-device
+
+if [[ -n "$ART_TEST_CHROOT" ]]; then
+  # Check that ART_TEST_CHROOT is correctly defined.
+  if [[ "x$ART_TEST_CHROOT" != x/* ]]; then
+    echo "$ART_TEST_CHROOT is not an absolute path"
+    exit 1
+  fi
+
+  echo -e "${green}Clean up /system in chroot${nc}"
+  # Remove all files under /system except the potential property_contexts file.
+  #
+  # The current ART Buildbot set-up runs the "setup device" step
+  # (performed by script tools/setup-buildbot-device.sh) before the
+  # "device cleanup" step (implemented by this script). As
+  # property_contexts file aliases are created during the former step,
+  # we need this exception to prevent the property_contexts file under
+  # /system in the chroot from being removed by the latter step.
+  #
+  # TODO: Reorder ART Buildbot steps so that "device cleanup" happens
+  # before "setup device" and remove this special case.
+  #
+  # TODO: Also consider adding a "tear down device" step on the ART
+  # Buildbot (at the very end of a build) undoing (some of) the work
+  # done in the "device setup" step.
+  adb shell find "$ART_TEST_CHROOT/system" \
+    ! -path "$ART_TEST_CHROOT/system/etc/selinux/plat_property_contexts" \
+    ! -type d \
+    -exec rm -f \{\} +
+
+  echo -e "${green}Clean up some subdirs in /data in chroot${nc}"
+  adb shell rm -rf \
+    "$ART_TEST_CHROOT/data/local/tmp/*" \
+    "$ART_TEST_CHROOT/data/art-test" \
+    "$ART_TEST_CHROOT/data/nativetest" \
+    "$ART_TEST_CHROOT/data/nativetest64" \
+    "$ART_TEST_CHROOT/data/run-test" \
+    "$ART_TEST_CHROOT/data/dalvik-cache/*" \
+    "$ART_TEST_CHROOT/data/misc/trace/*"
+else
+  adb shell rm -rf \
+    /data/local/tmp /data/art-test /data/nativetest /data/nativetest64 '/data/misc/trace/*'
+fi