package-release.sh: Refine --copy-prebuilts

This patch refines the behaviour of the --copy-prebuilts=<path>
option, by adding the generation of a README file containing the
exact rebuild instructions, and input directory git commit SHA1s.

Change-Id: Ibcc620e4df54c8fc8198955c5f04393e086ef172
diff --git a/distrib/package-release.sh b/distrib/package-release.sh
index dffc203..b47e334 100755
--- a/distrib/package-release.sh
+++ b/distrib/package-release.sh
@@ -151,6 +151,23 @@
   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" | tr "'" "\\'")
+    eval $VARNAME=\'$SHA1\'
+}
 # Defaults.
 DEFAULT_REVISION=$(date +%Y%m%d)
 DEFAULT_PKG_PREFIX=android-emulator
@@ -371,17 +388,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
@@ -492,7 +512,7 @@
     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 Linux binaries into $DST_DIR"
+        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;;
@@ -509,6 +529,24 @@
         (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"