Merge "Allow path to KVM to be overridden by environment." into idea133
diff --git a/distrib/package-release.sh b/distrib/package-release.sh
index ac62573..a766937 100755
--- a/distrib/package-release.sh
+++ b/distrib/package-release.sh
@@ -151,6 +151,24 @@
   run ssh $HOST rm -rf $DST_DIR/$PKG_FILE_PREFIX
 }
 
+# Extract the git commit SHA1 of a given directory, and put its value
+# in a destination variable. If the target directory is not the root
+# of a git checkout, abort.
+# $1: Destination variable name.
+# $2: Git directory.
+# Example:   extract_commit_description GTEST_DESC "$GTEST_DIR"
+extract_git_commit_description () {
+    local VARNAME GIT_DIR SHA1
+    VARNAME=$1
+    GIT_DIR=$2
+    # Extract the commit description, then escape (') characters in it.
+    SHA1=$(cd $GIT_DIR && git log --oneline -1 .) || \
+        panic "Not a Git directory: $GIT_DIR"
+
+    SHA1=$(printf "%s" "$SHA1" | sed -e s/\'/\\\'/g)
+    eval $VARNAME=\"$SHA1\"
+}
+
 # Defaults.
 DEFAULT_REVISION=$(date +%Y%m%d)
 DEFAULT_PKG_PREFIX=android-emulator
@@ -172,6 +190,7 @@
 
 # Command-line parsing.
 DO_HELP=
+OPT_COPY_PREBUILTS=
 OPT_DARWIN_SSH=
 OPT_PKG_DIR=
 OPT_PKG_PREFIX=
@@ -184,6 +203,9 @@
         --help|-?)
             DO_HELP=true
             ;;
+        --copy-prebuilts=*)
+            OPT_COPY_PREBUILTS=${OPT##--copy-prebuilts=}
+            ;;
         --darwin-ssh=*)
             OPT_DARWIN_SSH=${OPT##--darwin-ssh=}
             ;;
@@ -240,13 +262,20 @@
 as well. You can also define ANDROID_EMULATOR_DARWIN_SSH in your
 environment to setup a default value for this option.
 
+Use --copy-prebuilts=<path> to specify the path of an AOSP workspace/checkout,
+and to copy 64-bit prebuilt binaries to <path>/prebuilts/android-emulator/
+for both Linux and Darwin platforms. This option requires the use of
+--darwin-ssh=<host> or ANDROID_EMULATOR_DARWIN_SSH to build the Darwin
+binaries.
+
 Valid options (defaults are inside brackets):
     --help | -?           Print this message.
     --package-dir=<path>  Change package output directory [$DEFAULT_PKG_DIR].
     --revision=<name>     Change revision [$DEFAULT_REVISION].
     --sources             Also create sources package.
     --system=<list>       Specify host system list [$DEFAULT_SYSTEMS].
-    --darwin-ssh=<host>   Specify remote Darwin host [$DEFAULT_DARWIN_SSH].
+    --copy-prebuilts=<path>  Copy 64-bit Linux and Darwin binaries to
+                             <path>/prebuilts/android-emulator/
 
 EOF
     exit 0
@@ -298,6 +327,18 @@
     SYSTEMS="$SYSTEMS darwin"
 fi
 
+if [ "$OPT_COPY_PREBUILTS" ]; then
+    if [ -z "$DARWIN_SSH" ]; then
+        panic "The --copy-prebuilts=<dir> option requires --darwin-ssh=<host>."
+    fi
+    TARGET_AOSP=$OPT_COPY_PREBUILTS
+    if [ ! -f "$TARGET_AOSP/build/envsetup.sh" ]; then
+        panic "Not an AOSP checkout / workspace: $TARGET_AOSP"
+    fi
+    TARGET_PREBUILTS_DIR=$TARGET_AOSP/prebuilts/android-emulator
+    mkdir -p "$TARGET_PREBUILTS_DIR"
+fi
+
 case $VERBOSE in
   0|1)
     REBUILD_FLAGS=""
@@ -348,17 +389,20 @@
     exit 1
 fi
 
+extract_git_commit_description QEMU_GIT_COMMIT "$QEMU_DIR"
 GTEST_DIR=$(dirname $QEMU_DIR)/gtest
 if [ ! -d "$GTEST_DIR" ]; then
   panic "Cannot find GoogleTest source directory: $GTEST_DIR"
 fi
 log "Found GoogleTest directory: $GTEST_DIR"
+extract_git_commit_description GTEST_GIT_COMMIT "$GTEST_DIR"
 
 EMUGL_DIR=$QEMU_DIR/../../sdk/emulator/opengl
 if [ ! -d "$EMUGL_DIR" ]; then
   panic "Cannot find GPU emulation source directory: $EMUGL_DIR"
 fi
 log "Found GPU emulation directory: $EMUGL_DIR"
+extract_git_commit_description EMUGL_GIT_COMMIT "$EMUGL_DIR"
 
 SOURCES_PKG_FILE=
 if [ "$OPT_SOURCES" ]; then
@@ -464,6 +508,46 @@
     PKG_FILE=$PKG_DIR/$PKG_PREFIX-$PKG_REVISION-$SYSTEM.tar.bz2
     (run cd "$TEMP_BUILD_DIR"/$SYSTEM && run tar cf $PKG_FILE $PKG_PREFIX-$PKG_REVISION)
 done
+if [ "$OPT_COPY_PREBUILTS" ]; then
+    for SYSTEM in linux darwin; do
+        SRC_DIR="$TEMP_BUILD_DIR"/$SYSTEM/$PKG_PREFIX-$PKG_REVISION
+        DST_DIR=$TARGET_PREBUILTS_DIR/$SYSTEM-x86_64
+        dump "[$SYSTEM-x86_64] Copying emulator binaries into $DST_DIR"
+        run mkdir -p "$DST_DIR" || panic "Could not create directory: $DST_DIR"
+        case $SYSTEM in
+            linux) DLLEXT=.so;;
+            darwin) DLLEXT=.dylib;;
+            *) panic "Unsupported prebuilt system: $SYSTEM";;
+        esac
+        FILES="emulator"
+        for ARCH in arm x86 mips; do
+            FILES="$FILES emulator64-$ARCH"
+        done
+        for LIB in OpenglRender EGL_translator GLES_CM_translator GLES_V2_translator; do
+            FILES="$FILES lib/lib64$LIB$DLLEXT"
+        done
+        (run cd "$SRC_DIR/tools" && tar cf - $FILES) | (cd $DST_DIR && tar xf -) ||
+                panic "Could not copy binaries to $DST_DIR"
+    done
+    cat > $TARGET_PREBUILTS_DIR/README <<EOF
+This directory contains prebuilt emulator binaries that were generated by
+running the following command on a 64-bit Linux machine:
+
+  external/qemu/distrib/package-release.sh \\
+      --darwin-ssh=<host> \\
+      --copy-prebuilts=<path>
+
+Where <host> is the host name of a Darwin machine, and <path> is the root
+path of this AOSP repo workspace.
+
+Below is the list of specific commits for each input directory used:
+
+external/gtest       $GTEST_GIT_COMMIT
+external/qemu        $QEMU_GIT_COMMIT
+sdk/emulator/opengl  $EMUGL_GIT_COMMIT
+
+EOF
+fi
 
 dump "Done. See $PKG_DIR"
 ls -lh "$PKG_DIR"/$PKG_PREFIX-$PKG_REVISION*