Improve emulator kernel rebuild script.

- Add support for building on darwin-x86
- Automatically finds the cross-toolchain (arch-dependent) if --cross=<prefix> is not used.
- Add "Auto-config: <option>" message to indicate what was automatically configured
- Select appropriate names for output files
- Print their name on the terminal in case of success

Change-Id: I908392083bf724a426d5029dc0a7dd29e045c7a4
diff --git a/distrib/build-kernel.sh b/distrib/build-kernel.sh
index 769a9a6..c7e9a8f 100755
--- a/distrib/build-kernel.sh
+++ b/distrib/build-kernel.sh
@@ -9,9 +9,29 @@
 CROSSPREFIX=arm-eabi-
 CONFIG=goldfish
 
-# Extract number of processors
-JOBS=`grep -c "processor" /proc/cpuinfo`
-JOBS=$(( $JOBS*2 ))
+# Determine the host architecture, and which default prebuilt tag we need.
+# For the toolchain auto-detection.
+#
+HOST_OS=`uname -s`
+case "$HOST_OS" in
+    Darwin)
+        HOST_OS=darwin
+        HOST_TAG=darwin-x86
+        BUILD_NUM_CPUS=$(sysctl -n hw.ncpu)
+        ;;
+    Linux)
+        # note that building  32-bit binaries on x86_64 is handled later
+        HOST_OS=linux
+        HOST_TAG=linux-x86
+        BUILD_NUM_CPUS=$(grep -c processor /proc/cpuinfo)
+        ;;
+    *)
+        echo "ERROR: Unsupported OS: $HOST_OS"
+        exit 1
+esac
+
+# Default number of parallel jobs during the build: cores * 2
+JOBS=$(( $BUILD_NUM_CPUS * 2 ))
 
 ARCH=arm
 
@@ -21,6 +41,7 @@
 OPTION_CROSS=
 OPTION_ARCH=
 OPTION_CONFIG=
+OPTION_JOBS=
 
 for opt do
     optarg=$(expr "x$opt" : 'x[^=]*=\(.*\)')
@@ -43,7 +64,7 @@
         OPTION_CONFIG=$optarg
         ;;
     -j*)
-        JOBS=$optarg
+        OPTION_JOBS=$optarg
         ;;
     *)
         echo "unknown option '$opt', use --help"
@@ -62,6 +83,7 @@
     echo "  --out=<directory>        output directory [$OUTPUT]"
     echo "  --cross=<prefix>         cross-toolchain prefix [$CROSSPREFIX]"
     echo "  --config=<name>          kernel config name [$CONFIG]"
+    echo "  -j<number>               launch <number> parallel build jobs [$JOBS]"
     echo ""
     echo "NOTE: --armv7 is equivalent to --config=goldfish_armv7. It is"
     echo "      ignored if --config=<name> is used."
@@ -70,13 +92,16 @@
 fi
 
 if [ -n "$OPTION_ARCH" ]; then
-    ARCH="$OPTION_ARCH"
+    ARCH=$OPTION_ARCH
 fi
 
 if [ -n "$OPTION_CONFIG" ]; then
-    CONFIG="$OPTION_CONFIG"
-elif [ "$OPTION_ARMV7" = "yes" ]; then
-    CONFIG=goldfish_armv7
+    CONFIG=$OPTION_CONFIG
+else
+    if [ "$OPTION_ARMV7" = "yes" ]; then
+        CONFIG=goldfish_armv7
+    fi
+    echo "Auto-config: --config=$CONFIG"
 fi
 
 # Check that we are in the kernel directory
@@ -102,10 +127,12 @@
 else
     case $ARCH in
         arm)
+            CROSSTOOLCHAIN=arm-eabi-4.4.3
             CROSSPREFIX=arm-eabi-
             ZIMAGE=zImage
             ;;
         x86)
+            CROSSTOOLCHAIN=i686-android-linux-4.4.3
             CROSSPREFIX=i686-android-linux-
             ZIMAGE=bzImage
             ;;
@@ -114,19 +141,43 @@
             exit 1
             ;;
     esac
+    echo "Auto-config: --cross=$CROSSPREFIX"
+fi
+
+# If the cross-compiler is not in the path, try to find it automatically
+CROSS_COMPILER="${CROSSPREFIX}gcc"
+CROSS_COMPILER_VERSION=$($CROSS_COMPILER --version 2>/dev/null)
+if [ $? != 0 ] ; then
+    BUILD_TOP=$ANDROID_BUILD_TOP
+    if [ -z "$BUILD_TOP" ]; then
+        # Assume this script is under external/qemu/distrib/ in the
+        # Android source tree.
+        BUILD_TOP=$(dirname $0)/../../..
+        if [ ! -d "$BUILD_TOP/prebuilt" ]; then
+            BUILD_TOP=
+        else
+            BUILD_TOP=$(cd $BUILD_TOP && pwd)
+        fi
+    fi
+    CROSSPREFIX=$BUILD_TOP/prebuilt/$HOST_TAG/toolchain/$CROSSTOOLCHAIN/bin/$CROSSPREFIX
+    if [ "$BUILD_TOP" -a -f ${CROSSPREFIX}gcc ]; then
+        echo "Auto-config: --cross=$CROSSPREFIX"
+    else
+        echo "It looks like $CROSS_COMPILER is not in your path ! Aborting."
+        exit 1
+    fi
 fi
 
 export CROSS_COMPILE="$CROSSPREFIX" ARCH SUBARCH=$ARCH
 
-# Check that the cross-compiler is in our path
-#
-CROSS_COMPILER="${CROSS_COMPILE}gcc"
-CROSS_COMPILER_VERSION=$($CROSS_COMPILER --version 2>/dev/null)
-if [ $? != 0 ] ; then
-    echo "It looks like $CROSS_COMPILER is not in your path ! Aborting."
-    exit 1
+if [ "$OPTION_JOBS" ]; then
+    JOBS=$OPTION_JOBS
+else
+    echo "Auto-config: -j$JOBS"
 fi
 
+# Do the build
+#
 rm -f include/asm &&
 make ${CONFIG}_defconfig &&    # configure the kernel
 make -j$JOBS                   # build it
@@ -136,15 +187,28 @@
     exit 1
 fi
 
-OUTPUT_KERNEL=kernel-qemu
-OUTPUT_VMLINUX=vmlinux-qemu
-if [ "$OPTION_ARMV7" = "yes" ] ; then
-    OUTPUT_KERNEL=${OUTPUT_KERNEL}-armv7
-    OUTPUT_VMLINUX=${OUTPUT_VMLINUX}-armv7
-fi
+# Note: The exact names of the output files are important for the Android build,
+#       do not change the definitions lightly.
+case $CONFIG in
+    vbox*)
+        OUTPUT_KERNEL=kernel-vbox
+        OUTPUT_VMLINUX=vmlinux-vbox
+        ;;
+    goldfish)
+        OUTPUT_KERNEL=kernel-qemu
+        OUTPUT_VMLINUX=vmlinux-qemu
+        ;;
+    goldfish_armv7)
+        OUTPUT_KERNEL=kernel-qemu-armv7
+        OUTPUT_VMLINUX=vmlinux-qemu-armv7
+        ;;
+    *)
+        OUTPUT_KERNEL=kernel-$CONFIG
+        OUTPUT_VMLINUX=vmlinux-$CONFIG
+esac
 
 cp -f arch/$ARCH/boot/$ZIMAGE $OUTPUT/$OUTPUT_KERNEL
 cp -f vmlinux $OUTPUT/$OUTPUT_VMLINUX
 
-echo "Kernel $CONFIG prebuilt image copied to $OUTPUT successfully !"
+echo "Kernel $CONFIG prebuilt images ($OUTPUT_KERNEL and $OUTPUT_VMLINUX) copied to $OUTPUT successfully !"
 exit 0