package-release.sh: Add --darwin-ssh=<host> option

It is now possible to package Darwin binaries from Linux,
using SSH to connect to a remote host where the build will
happen. To do so, use --darwin-ssh=<host>.

Alternatively, one can define ANDROID_EMULATOR_DARWIN_SSH
in its environment to get the same effect by default.

+ android-configure.sh: Always try to use the host ccache,
  if available, if USE_CCACHE is not set to 0. Useful to
  speed up remote builds.

+ android-configure.sh/android-rebuild.sh will strip all
  generated binaries, unless --debug is used.

+ Properly cleanup temporary files created by android-configure.sh
  to avoid polluting /tmp with android-<pid>-test* files.

Change-Id: If4147d35e55ca393463e2f1084841f02bac1fd63
diff --git a/android-configure.sh b/android-configure.sh
index 12ed942..1f2c4d1 100755
--- a/android-configure.sh
+++ b/android-configure.sh
@@ -218,7 +218,7 @@
             ;;
     esac
 else
-    if [ -n "$USE_CCACHE" ]; then
+    if [ "$USE_CCACHE" != 0 ]; then
         CCACHE=$(which ccache 2>/dev/null)
     fi
     GLES_PROBE_OS=$TARGET_OS
@@ -448,6 +448,7 @@
             eval $1=no
             log "AudioProbe : $2 seems to be UNUSABLE on this system !!"
         fi
+        clean_temp
     fi
 }
 
@@ -472,7 +473,7 @@
 ####
 
 # because the previous version could be read-only
-rm -f $TMPC
+clean_temp
 
 # check host endianess
 #
@@ -517,6 +518,18 @@
         ;;
 esac
 
+# Strip executables and shared libraries unless --debug is used.
+if [ "$OPTION_DEBUG" != "yes" ]; then
+    case $HOST_OS in
+        darwin)
+            LDFLAGS="$LDFLAGS -Wl,-S"
+            ;;
+        *)
+            LDFLAGS="$LDFLAGS -Wl,--strip-all"
+            ;;
+    esac
+fi
+
 create_config_mk
 echo "" >> $config_mk
 echo "HOST_PREBUILT_TAG := $TARGET_OS" >> $config_mk
@@ -693,4 +706,6 @@
 python scripts/qapi-commands.py --output-dir=$AUTOGENERATED_DIR -m < qapi-schema.json
 log "Generate   : $AUTOGENERATED_DIR"
 
+clean_temp
+
 echo "Ready to go. Type 'make' to build emulator"
diff --git a/android/build/common.sh b/android/build/common.sh
index 230fa8a..5808570 100644
--- a/android/build/common.sh
+++ b/android/build/common.sh
@@ -298,6 +298,7 @@
         AR=ar
     fi
     log "AR         : archiver ($AR)"
+    clean_temp
 }
 
 # try to compile the current source file in $TMPC into an object
@@ -401,6 +402,7 @@
     CFLAGS=$OLD_CFLAGS
     LDFLAGS=$OLD_LDFLAGS
     eval $1=$result_cl
+    clean_temp
 }
 
 # check that a given C header file exists on the host system
@@ -422,6 +424,7 @@
     eval $1=$result_ch
     #eval result=$`echo $1`
     #log  "Host       : $1=$result_ch"
+    clean_temp
 }
 
 # run the test program that is in $TMPC and set its exit status
@@ -441,6 +444,7 @@
     LDFLAGS="$OLD_LDFLAGS"
     eval $1=$run_exec_result
     log "Host       : $1=$run_exec_result"
+    clean_temp
 }
 
 ## Android build system auto-detection
diff --git a/distrib/package-release.sh b/distrib/package-release.sh
index 6c0e6e7..ac289a3 100755
--- a/distrib/package-release.sh
+++ b/distrib/package-release.sh
@@ -57,6 +57,25 @@
     esac
 }
 
+# Same as 'run', but slightly more quiet:
+#  0 or 1 -> Don't display anything
+#  2      -> Diplay the command, its output and error.
+run2 () {
+    case $VERBOSE in
+        0|1)
+            "$@" >/dev/null 2>&1
+            ;;
+        2)
+            echo "COMMAND: $@"
+            "$@" >/dev/null 2>&1
+            ;;
+        *)
+            echo "COMMAND: $@"
+            "$@"
+            ;;
+    esac
+}
+
 # $1: Source directory.
 # $2: Destination directory.
 # $3: List of files to copy, relative to $1 (if empty, all files will be copied).
@@ -92,10 +111,49 @@
   printf "%s" "$@" | tr ',' ' '
 }
 
+# Rebuild Darwin binaries remotely through SSH
+# $1: Host name.
+# $2: Source package file.
+build_darwin_binaries_on () {
+  local HOST PKG_FILE PKG_FILE_BASENAME DST_DIR TARFLAGS
+  HOST=$1
+  PKG_FILE=$2
+
+  # The package file is ....../something-darwin.tar.bz2
+  # And should unpack to a single directory named 'something/'
+  # so extract the prefix from the package name.
+  PKG_FILE_BASENAME=$(basename "$PKG_FILE")
+  PKG_FILE_PREFIX=${PKG_FILE_BASENAME%%-sources.tar.bz2}
+  if [ "$PKG_FILE_PREFIX" = "$PKG_FILE_BASENAME" ]; then
+    # Sanity check.
+    panic "Can't get package prefix from $PKG_FILE_BASENAME"
+  fi
+
+  # Where to do the work on the remote host.
+  DST_DIR=/tmp/android-emulator-build
+
+  if [ "$VERBOSE" -ge 3 ]; then
+    TARFLAGS="v"
+  fi
+  dump "Copying sources to Darwin host: $HOST"
+  run ssh $HOST "mkdir -p $DST_DIR && rm -rf $DST_DIR/$PKG_FILE_BASENAME"
+  cat "$PKG_FILE" | ssh $HOST "cd $DST_DIR && tar x${TARGFLAGS}f -"
+
+  dump "Rebuilding Darwin binaries remotely."
+  run ssh $HOST "bash -l -c \"cd $DST_DIR/$PKG_FILE_PREFIX/qemu && ./android-rebuild.sh $REBUILD_FLAGS\"" ||
+        panic "Can't rebuild binaries on Darwin, use --verbose to see why!"
+
+  dump "Retrieving Darwin binaries from: $HOST"
+  rm -rf objs/*
+  run scp $HOST:$DST_DIR/$PKG_FILE_PREFIX/qemu/objs/emulator* objs/
+  run ssh $HOST rm -rf $DST_DIR/$PKG_FILE_PREFIX
+}
+
 # Defaults.
 DEFAULT_REVISION=$(date +%Y%m%d)
 DEFAULT_PKG_PREFIX=android-emulator
 DEFAULT_PKG_DIR=/tmp
+DEFAULT_DARWIN_SSH=$ANDROID_EMULATOR_DARWIN_SSH
 
 case $(uname -s) in
     Linux)
@@ -112,6 +170,7 @@
 
 # Command-line parsing.
 DO_HELP=
+OPT_DARWIN_SSH=
 OPT_PKG_DIR=
 OPT_PKG_PREFIX=
 OPT_REVISION=
@@ -123,6 +182,9 @@
         --help|-?)
             DO_HELP=true
             ;;
+        --darwin-ssh=*)
+            OPT_DARWIN_SSH=${OPT##--darwin-ssh=}
+            ;;
         --package-dir=*)
             OPT_PKG_DIR=${OPT##--package-dir=}
             ;;
@@ -171,12 +233,18 @@
 
 Use --sources option to also generate a source tarball.
 
+Use --darwin-ssh=<host> to build perform a remote build of the Darwin
+binaries on a remote host through ssh. Note that this forces --sources
+as well. You can also define ANDROID_EMULATOR_DARWIN_SSH in your
+environment to setup a default value for this option.
+
 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].
 
 EOF
     exit 0
@@ -211,6 +279,39 @@
     log "Auto-config: --system=$SYSTEMS"
 fi
 
+if [ -z "$OPT_DARWIN_SSH" ]; then
+  DARWIN_SSH=$DEFAULT_DARWIN_SSH
+  if [ "$DARWIN_SSH" ]; then
+    log "Auto-config: --darwin-ssh=$DARWIN_SSH  (from environment)."
+  fi
+else
+  DARWIN_SSH=$OPT_DARWIN_SSH
+fi
+
+if [ "$DARWIN_SSH" ]; then
+    if [ -z "$OPT_SOURCES" ]; then
+        OPT_SOURCES=true
+        log "Auto-config: --sources  (remote Darwin build)."
+    fi
+    SYSTEMS="$SYSTEMS darwin"
+fi
+
+case $VERBOSE in
+  0|1)
+    REBUILD_FLAGS=""
+    ;;
+  2)
+    REBUILD_FLAGS="--verbose"
+    ;;
+  *)
+    REBUILD_FLAGS="--verbose --verbose"
+    ;;
+esac
+
+# Remove duplicates.
+SYSTEMS=$(echo "$SYSTEMS" | tr ' ' '\n' | sort -u | tr '\n' ' ')
+log "Building for the following systems: $SYSTEMS"
+
 # Default build directory.
 TEMP_BUILD_DIR=/tmp/$USER-qemu-package-binaries
 
@@ -251,56 +352,7 @@
 fi
 log "Found GoogleTest directory: $GTEST_DIR"
 
-for SYSTEM in $SYSTEMS; do
-    PKG_NAME="$PKG_REVISION-$SYSTEM"
-    dump "[$PKG_NAME] Rebuilding binaries from sources."
-    run cd $QEMU_DIR
-    case $SYSTEM in
-        $HOST_SYSTEM)
-            run ./android-rebuild.sh || panic "Use ./android-rebuild.sh to see why."
-            ;;
-        windows)
-            if [ "$HOST_SYSTEM" != "linux" ]; then
-                panic "Windows binaries can only be rebuilt on Linux!"
-            fi
-            run ./android-rebuild.sh --mingw || panic "Use ./android-rebuild.sh --mingw to see why."
-            ;;
-        *)
-            panic "Can't rebuild $SYSTEM binaries on $HOST_SYSTEM for now!"
-            ;;
-    esac
-
-    dump "[$PKG_NAME] Copying emulator binaries."
-    TEMP_PKG_DIR=$TEMP_BUILD_DIR/$SYSTEM/$PKG_PREFIX-$PKG_REVISION
-    run mkdir -p "$TEMP_PKG_DIR"/tools
-
-    run cp -p objs/emulator* "$TEMP_PKG_DIR"/tools
-    if [ -d "objs/lib" ]; then
-        dump "[$PKG_NAME] Copying GLES emulation libraries."
-        run mkdir -p "$TEMP_PKG_DIR"/tools/lib
-        run cp -p objs/lib/* "$TEMP_PKG_DIR"/tools/lib/
-    fi
-
-    dump "[$PKG_NAME] Creating README file."
-    cat > $TEMP_PKG_DIR/README <<EOF
-This directory contains Android emulator binaries. You can use them directly
-by defining ANDROID_SDK_ROOT in your environment, then call tools/emulator
-with the usual set of options.
-
-To install them directly into your SDK, copy them with:
-
-    cp -r tools/* \$ANDROID_SDK_ROOT/tools/
-EOF
-
-    dump "[$PKG_NAME] Copying license files."
-    mkdir -p "$TEMP_PKG_DIR"/licenses/
-    cp COPYING COPYING.LIB "$TEMP_PKG_DIR"/licenses/
-
-    dump "[$PKG_NAME] Creating tarball."
-    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
-
+SOURCES_PKG_FILE=
 if [ "$OPT_SOURCES" ]; then
     BUILD_DIR=$TEMP_BUILD_DIR/sources/$PKG_PREFIX-$PKG_REVISION
     PKG_NAME="$PKG_REVISION-sources"
@@ -336,9 +388,71 @@
     chmod +x "$BUILD_DIR"/rebuild.sh
 
     PKG_FILE=$PKG_DIR/$PKG_PREFIX-$PKG_REVISION-sources.tar.bz2
+    SOURCES_PKG_FILE=$PKG_FILE
     dump "[$PKG_NAME] Creating tarball..."
     (run cd "$BUILD_DIR"/.. && run tar cjf "$PKG_FILE" $PKG_PREFIX-$PKG_REVISION)
 fi
 
+for SYSTEM in $SYSTEMS; do
+    PKG_NAME="$PKG_REVISION-$SYSTEM"
+    dump "[$PKG_NAME] Rebuilding binaries from sources."
+    run cd $QEMU_DIR
+    case $SYSTEM in
+        $HOST_SYSTEM)
+            run ./android-rebuild.sh $REBUILD_FLAGS || panic "Use ./android-rebuild.sh to see why."
+            ;;
+        darwin)
+            if [ -z "$DARWIN_SSH" ]; then
+                # You can only rebuild Darwin binaries on non-Darwin systems
+                # by using --darwin-ssh=<host>.
+                panic "You can only rebuild Darwin binaries with --darwin-ssh"
+            fi
+            if [ -z "$SOURCES_PKG_FILE" ]; then
+                panic "You must use --sources to build Darwin binaries through ssh"
+            fi
+            build_darwin_binaries_on "$DARWIN_SSH" "$SOURCES_PKG_FILE"
+            ;;
+        windows)
+            if [ "$HOST_SYSTEM" != "linux" ]; then
+                panic "Windows binaries can only be rebuilt on Linux!"
+            fi
+            run ./android-rebuild.sh --mingw $REBUILD_FLAGS || panic "Use ./android-rebuild.sh --mingw to see why."
+            ;;
+        *)
+            panic "Can't rebuild $SYSTEM binaries on $HOST_SYSTEM for now!"
+            ;;
+    esac
+
+    dump "[$PKG_NAME] Copying emulator binaries."
+    TEMP_PKG_DIR=$TEMP_BUILD_DIR/$SYSTEM/$PKG_PREFIX-$PKG_REVISION
+    run mkdir -p "$TEMP_PKG_DIR"/tools
+
+    run cp -p objs/emulator* "$TEMP_PKG_DIR"/tools
+    if [ -d "objs/lib" ]; then
+        dump "[$PKG_NAME] Copying GLES emulation libraries."
+        run mkdir -p "$TEMP_PKG_DIR"/tools/lib
+        run2 cp -p objs/lib/* "$TEMP_PKG_DIR"/tools/lib/
+    fi
+
+    dump "[$PKG_NAME] Creating README file."
+    cat > $TEMP_PKG_DIR/README <<EOF
+This directory contains Android emulator binaries. You can use them directly
+by defining ANDROID_SDK_ROOT in your environment, then call tools/emulator
+with the usual set of options.
+
+To install them directly into your SDK, copy them with:
+
+    cp -r tools/* \$ANDROID_SDK_ROOT/tools/
+EOF
+
+    dump "[$PKG_NAME] Copying license files."
+    mkdir -p "$TEMP_PKG_DIR"/licenses/
+    cp COPYING COPYING.LIB "$TEMP_PKG_DIR"/licenses/
+
+    dump "[$PKG_NAME] Creating tarball."
+    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
+
 dump "Done. See $PKG_DIR"
-ls -l "$PKG_DIR"/$PKG_PREFIX-$PKG_REVISION*
+ls -lh "$PKG_DIR"/$PKG_PREFIX-$PKG_REVISION*