android-rebuild.sh: Add --out-dir=<path> option.

This patch adds an option to android-rebuild.sh and
android-configure.sh which allows one to specify a specific
output directory for all objects / binaries, instead of using
the default of 'objs' under the current source directory.

Tested for both regular and mingw builds.

Change-Id: I8c5ddfac8fb9899a2ff59a43e6cb5b9d8c45cef1
diff --git a/android-configure.sh b/android-configure.sh
index 64f2f96..bb09011 100755
--- a/android-configure.sh
+++ b/android-configure.sh
@@ -20,6 +20,7 @@
 OPTION_DEBUG=no
 OPTION_IGNORE_AUDIO=no
 OPTION_NO_PREBUILTS=no
+OPTION_OUT_DIR=
 OPTION_HELP=no
 OPTION_STATIC=no
 OPTION_MINGW=no
@@ -57,6 +58,8 @@
   ;;
   --no-strip) OPTION_NO_STRIP=yes
   ;;
+  --out-dir=*) OPTION_OUT_DIR=$optarg
+  ;;
   --ignore-audio) OPTION_IGNORE_AUDIO=yes
   ;;
   --no-prebuilts) OPTION_NO_PREBUILTS=yes
@@ -92,6 +95,7 @@
     echo "  --debug                  enable debug (-O0 -g) build"
     echo "  --ignore-audio           ignore audio messages (may build sound-less emulator)"
     echo "  --no-prebuilts           do not use prebuilt libraries and compiler"
+    echo "  --out-dir=<path>         use specific output directory [objs/]"
     echo "  --mingw                  build Windows executable on Linux"
     echo "  --static                 build a completely static executable"
     echo "  --verbose                verbose configuration"
@@ -169,6 +173,14 @@
     enable_cygwin
 fi
 
+if [ "$OPTION_OUT_DIR" ]; then
+    OUT_DIR="$OPTION_OUT_DIR"
+    mkdir -p "$OUT_DIR" || panic "Could not create output directory: $OUT_DIR"
+else
+    OUT_DIR=objs
+    log "Auto-config: --out-dir=objs"
+fi
+
 # Are we running in the Android build system ?
 check_android_build
 
@@ -262,10 +274,10 @@
     if [ ! -d "$PCBIOS_DIR" ]; then
         log "PC Bios    : Could not find prebuilts directory."
     else
-        mkdir -p objs/lib/pc-bios
+        mkdir -p $OUT_DIR/lib/pc-bios
         for BIOS_FILE in bios.bin vgabios-cirrus.bin; do
             log "PC Bios    : Copying $BIOS_FILE"
-            cp -f $PCBIOS_DIR/$BIOS_FILE objs/lib/pc-bios/$BIOS_FILE
+            cp -f $PCBIOS_DIR/$BIOS_FILE $OUT_DIR/lib/pc-bios/$BIOS_FILE
         done
     fi
 fi
@@ -458,7 +470,7 @@
 # create the objs directory that is going to contain all generated files
 # including the configuration ones
 #
-mkdir -p objs
+mkdir -p $OUT_DIR
 
 ###
 ###  Compiler probe
@@ -546,7 +558,7 @@
     esac
 fi
 
-create_config_mk
+create_config_mk "$OUT_DIR"
 echo "" >> $config_mk
 echo "HOST_PREBUILT_TAG := $TARGET_OS" >> $config_mk
 echo "HOST_EXEEXT       := $TARGET_EXEEXT" >> $config_mk
@@ -607,7 +619,7 @@
 
 # Build the config-host.h file
 #
-config_h=objs/config-host.h
+config_h=$OUT_DIR/config-host.h
 cat > $config_h <<EOF
 /* This file was autogenerated by '$PROGNAME' */
 
diff --git a/android-rebuild.sh b/android-rebuild.sh
index 875afb0..a4d09de 100755
--- a/android-rebuild.sh
+++ b/android-rebuild.sh
@@ -13,6 +13,8 @@
 VERBOSE=0
 
 MINGW=
+OUT_DIR=objs
+
 for OPT; do
     case $OPT in
         --mingw)
@@ -21,6 +23,9 @@
         --verbose)
             VERBOSE=$(( $VERBOSE + 1 ))
             ;;
+        --out-dir=*)
+            OUT_DIR=${OPT##--out-dir=}
+            ;;
         --help|-?)
             VERBOSE=2
             ;;
@@ -59,11 +64,11 @@
 cd `dirname $0`
 rm -rf objs
 echo "Configuring build."
-run ./android-configure.sh "$@" ||
+run ./android-configure.sh --out-dir=$OUT_DIR "$@" ||
     panic "Configuration error, please run ./android-configure.sh to see why."
 
 echo "Building sources."
-run make -j$HOST_NUM_CPUS ||
+run make -j$HOST_NUM_CPUS OBJS_DIR="$OUT_DIR" ||
     panic "Could not build sources, please run 'make' to see why."
 
 RUN_64BIT_TESTS=true
@@ -80,14 +85,14 @@
 FAILURES=""
 for UNIT_TEST in emulator_unittests emugl_common_host_unittests; do
   echo "   - $UNIT_TEST"
-  run $TEST_SHELL objs/$UNIT_TEST$EXE_SUFFIX || FAILURES="$FAILURES $UNIT_TEST"
+  run $TEST_SHELL $OUT_DIR/$UNIT_TEST$EXE_SUFFIX || FAILURES="$FAILURES $UNIT_TEST"
 done
 
 if [ "$RUN_64BIT_TESTS" ]; then
     echo "Running 64-bit unit test suite."
     for UNIT_TEST in emulator64_unittests emugl64_common_host_unittests; do
         echo "   - $UNIT_TEST"
-        run $TEST_SHELL objs/$UNIT_TEST$EXE_SUFFIX || FAILURES="$FAILURES $UNIT_TEST"
+        run $TEST_SHELL $OUT_DIR/$UNIT_TEST$EXE_SUFFIX || FAILURES="$FAILURES $UNIT_TEST"
     done
 fi
 
diff --git a/android/build/common.sh b/android/build/common.sh
index 76c907f..965a462 100644
--- a/android/build/common.sh
+++ b/android/build/common.sh
@@ -548,11 +548,13 @@
 ## Build configuration file support
 ## you must define $config_mk before calling this function
 ##
+## $1: Optional output directory.
 create_config_mk ()
 {
     # create the directory if needed
     local  config_dir
-    config_mk=${config_mk:-objs/config.make}
+    local out_dir=${1:-objs}
+    config_mk=${config_mk:-$out_dir/config.make}
     config_dir=`dirname $config_mk`
     mkdir -p $config_dir 2> $TMPL
     if [ $? != 0 ] ; then
@@ -576,6 +578,7 @@
     echo "HOST_CC     := $CC" >> $config_mk
     echo "HOST_LD     := $LD" >> $config_mk
     echo "HOST_AR     := $AR" >> $config_mk
+    echo "OBJS_DIR    := $out_dir" >> $config_mk
 }
 
 add_android_config_mk ()