envsetup.sh: Use prebuilt emulator binaries if available.

This patch changes the setpaths() function in envsetup.sh to probe
for prebuilts/android-emulator/<host>/, and prepend it to PATH if
it exists.

See https://android-review.googlesource.com/#/c/93399/
for a related patch that adds the binaries.

BUG=13747402

Change-Id: I30794ea52f6dfc58908e6271f2c0da8e2f0b68e8
diff --git a/envsetup.sh b/envsetup.sh
index 60726d3..d026466 100644
--- a/envsetup.sh
+++ b/envsetup.sh
@@ -171,6 +171,25 @@
     export ANDROID_QTOOLS=$T/development/emulator/qtools
     export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools
     export ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_QTOOLS:$ANDROID_TOOLCHAIN:$ANDROID_KERNEL_TOOLCHAIN_PATH$ANDROID_DEV_SCRIPTS:
+
+    # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
+    # to ensure that the corresponding 'emulator' binaries are used.
+    case $(uname -s) in
+        Darwin)
+            ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
+            ;;
+        Linux)
+            ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
+            ;;
+        *)
+            ANDROID_EMULATOR_PREBUILTS=
+            ;;
+    esac
+    if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
+        ANDROID_BUILD_PATHS=$ANDROID_EMULATOR_PREBUILTS:$ANDROID_BUILD_PATHS
+        export ANDROID_EMULATOR_PREBUILTS
+    fi
+
     export PATH=$ANDROID_BUILD_PATHS$PATH
 
     unset ANDROID_JAVA_TOOLCHAIN