scripts/rebuild.sh: Add --target=<name> option.

This adds a new option to the rebuild.sh script to specify the
virtual CPU target to build QEMU binaries for. Several targets
can be specified, either by calling --target=<name> multiple
times, or by using a comma-separated list, as in:

  --target=arm,arm64,x86

Valid target names are those used by the emulator, not those
used by QEMU which are slightly different:

  arm, arm64, x86, x86_64, mips, mips32

Note: The generated binaries require a custom kernel, system
      images, Android launcher, and have not been tested yet.

+ Fix SDL2 build to be more complete, otherwise the link fails
  on OS X.
diff --git a/scripts/rebuild.sh b/scripts/rebuild.sh
index 7fa4bee..3e1e33d 100755
--- a/scripts/rebuild.sh
+++ b/scripts/rebuild.sh
@@ -33,6 +33,9 @@
 
 # List of valid target systems.
 VALID_SYSTEMS="linux-x86,linux-x86_64,windows-x86,windows-x86_64,darwin-x86,darwin-x86_64"
+VALID_TARGETS="arm,arm64,x86,x86_64,mips,mips64"
+
+DEFAULT_TARGETS=arm64
 
 OPT_BUILD_DIR=
 OPT_DARWIN_SSH=
@@ -41,6 +44,7 @@
 OPT_NUM_JOBS=
 OPT_SYSTEM=
 OPT_USE_SDL1=
+OPT_TARGET=
 
 for OPT; do
     OPTARG=$(expr "x$OPT" : "x[^=]*=\\(.*\\)" || true)
@@ -69,6 +73,9 @@
         --system=*)
             OPT_SYSTEM=$OPTARG
             ;;
+        --target=*)
+            OPT_TARGET="$OPT_TARGET $OPTARG"
+            ;;
         --use-sdl1)
             OPT_USE_SDL1=true
             ;;
@@ -113,12 +120,13 @@
     --help|-?           Print this message.
     --verbose           Increase verbosity.
     --quiet             Decrease verbosity.
-    --system=<list>     List of target systems [$DEFAULT_SYSTEMS].
+    --system=<list>     List of host systems [$DEFAULT_SYSTEMS].
     --darwin-ssh=<host> Perform remote build through SSH.
     --build-dir=<path>  Use specific build directory (default is temporary).
     --no-ccache         Don't try to probe and use ccache during build.
     -j<count>           Run <count> parallel build jobs.
     --jobs=<count>      Same as -j<count>.
+    --target=<name>     Build binary for target <name> [$DEFAULT_TARGETS].
     --use-sdl1          Use SDL 1.x instead of SDL 2.x.
 
 EOF
@@ -126,7 +134,7 @@
 fi
 
 ##
-## Handle target system list
+## Handle host system list
 ##
 if [ "$OPT_SYSTEM" ]; then
     SYSTEMS=$(commas_to_spaces "$OPT_SYSTEM")
@@ -146,6 +154,26 @@
 fi
 
 ##
+## Handle target system list
+##
+if [ "$OPT_TARGET" ]; then
+    TARGETS=$(commas_to_spaces "$OPT_TARGET")
+else
+    TARGETS=$(commas_to_spaces "$DEFAULT_TARGETS")
+    log "Auto-config: --target='$TARGETS'"
+fi
+
+BAD_TARGETS=
+for TARGET in $TARGETS; do
+    if ! list_contains "$VALID_TARGETS" "$TARGET"; then
+        BAD_TARGETS="$BAD_TARGETS $TARGET"
+    fi
+done
+if [ "$BAD_TARGETS" ]; then
+    panic "Invalid target name(s): [$BAD_TARGETS], use one of: $VALID_TARGETS"
+fi
+
+##
 ## Handle remote darwin build
 ##
 DARWIN_SSH=
@@ -661,12 +689,7 @@
 
     else  # !OPT_USE_SDL1
 
-        do_autotools_package SDL2 \
-            --disable-audio \
-            --disable-joystick \
-            --disable-cdrom \
-            --disable-file \
-            --disable-threads
+        do_autotools_package SDL2
 
         SDL_CONFIG=$PREFIX/bin/sdl2-config
 
@@ -713,7 +736,34 @@
 
     . "$ENV_SH"
 
+    QEMU_TARGETS=
+    QEMU_TARGET_LIST=
+    for TARGET in $TARGETS; do
+        case $TARGET in
+            arm64)
+                QEMU_TARGET=aarch64
+                ;;
+            x86)
+                QEMU_TARGET=i386
+                ;;
+            mips)
+                QEMU_TARGET=mipsel
+                ;;
+            mips64)
+                QEMU_TARGET=mips64el
+                ;;
+            *)
+                QEMU_TARGET=$TARGET
+                ;;
+        esac
+        QEMU_TARGETS="$QEMU_TARGETS $QEMU_TARGET"
+        QEMU_TARGET_LIST="$QEMU_TARGET_LIST ${QEMU_TARGET}-softmmu"
+    done
+    QEMU_TARGET_LIST=${QEMU_TARGET_LIST##\ }
+
     dump "$CURRENT_TEXT Building qemu-android"
+    log "Qemu targets: $QEMU_TARGETS"
+
     (
         run mkdir -p "$BUILD_DIR/qemu-android"
         run rm -rf "$BUILD_DIR"/qemu-android/*
@@ -740,7 +790,7 @@
         fi
         run $QEMU_ANDROID/configure \
             $CROSS_PREFIX_FLAG \
-            --target-list=aarch64-softmmu \
+            --target-list="$QEMU_TARGET_LIST" \
             --prefix=$PREFIX \
             --extra-cflags="-I$PREFIX/include" \
             --extra-ldflags="$EXTRA_LDFLAGS" \
@@ -771,23 +821,28 @@
             # up later with -j1 to complete it.
             (run make -j$NUM_JOBS || run make -j1 || true)
 
-            if [ ! -f "aarch64-softmmu/qemu-system-aarch64$HOST_EXE_EXTENSION" ]; then
-                panic "$CURRENT_TEXT Could not build qemu-system-aarch64!!"
-            fi
+            for QEMU_TARGET in $QEMU_TARGETS; do
+                if [ ! -f "$QEMU_TARGET-softmmu/qemu-system-$QEMU_TARGET$HOST_EXE_EXTENSION" ]; then
+                    panic "$CURRENT_TEXT Could not build qemu-system-$QEMU_TARGET!!"
+                fi
+            done
 
     ) || panic "Build failed!!"
 
-    dump "$CURRENT_TEXT Copying qemu-system-aarch64 to binaries/$CURRENT_HOST"
-
     BINARY_DIR=$(program_directory)/../binaries/$CURRENT_HOST
     run mkdir -p "$BINARY_DIR" ||
     panic "Could not create final directory: $BINARY_DIR"
 
-    run cp -p \
-        "$BUILD_DIR"/qemu-android/aarch64-softmmu/qemu-system-aarch64$HOST_EXE_EXTENSION \
-        "$BINARY_DIR"/qemu-system-aarch64$HOST_EXE_EXTENSION
+    for QEMU_TARGET in $QEMU_TARGETS; do
+        QEMU_EXE=qemu-system-${QEMU_TARGET}${HOST_EXE_EXTENSION}
+        dump "$CURRENT_TEXT Copying $QEMU_EXE to binaries/$CURRENT_HOST"
 
-    run ${GNU_CONFIG_HOST_PREFIX}strip "$BINARY_DIR"/qemu-system-aarch64$HOST_EXE_EXTENSION
+        run cp -p \
+            "$BUILD_DIR"/qemu-android/$QEMU_TARGET-softmmu/$QEMU_EXE \
+            "$BINARY_DIR"/$QEMU_EXE
+
+        run ${GNU_CONFIG_HOST_PREFIX}strip "$BINARY_DIR"/$QEMU_EXE
+    done
 
     unset PKG_CONFIG PKG_CONFIG_PATH PKG_CONFIG_LIBDIR SDL_CONFIG
     unset LIBFFI_CFLAGS LIBFFI_LIBS GLIB_CFLAGS GLIB_LIBS