Merge "Make more scripts supporting the unknown archs."
diff --git a/build/tools/build-host-prebuilts.sh b/build/tools/build-host-prebuilts.sh
index 7ae94b7..c6d0b57 100755
--- a/build/tools/build-host-prebuilts.sh
+++ b/build/tools/build-host-prebuilts.sh
@@ -42,7 +42,8 @@
 register_option "--systems=<names>" do_SYSTEMS "List of host systems to build for"
 do_SYSTEMS () { CUSTOM_SYSTEMS=true; SYSTEMS=$1; }
 
-ARCHS=$DEFAULT_ARCHS
+ARCHS=$(find_ndk_unknown_archs)
+ARCHS="$DEFAULT_ARCHS $ARCHS"
 register_var_option "--arch=<list>" ARCHS "List of target archs to build for"
 
 PACKAGE_DIR=
@@ -108,6 +109,13 @@
 
 SYSTEMS=$(commas_to_spaces $SYSTEMS)
 ARCHS=$(commas_to_spaces $ARCHS)
+
+# Detect unknown arch
+UNKNOWN_ARCH=$(filter_out "$DEFAULT_ARCHS" "$ARCHS")
+if [ ! -z "$UNKNOWN_ARCH" ]; then
+    ARCHS=$(filter_out "$UNKNOWN_ARCH" "$ARCHS")
+fi
+
 LLVM_VERSION_LIST=$(commas_to_spaces $LLVM_VERSION_LIST)
 
 if [ "$DARWIN_SSH" -a -z "$CUSTOM_SYSTEMS" ]; then
@@ -178,7 +186,7 @@
     fail_panic "Could not copy toolchain!"
 
     # Time to run the show :-)
-    for ARCH in $(commas_to_spaces $ARCHS); do
+    for ARCH in $(commas_to_spaces $ARCHS $UNKNOWN_ARCH); do
         dump "Running remote $ARCH toolchain build..."
         SYSROOT=$TMPREMOTE/ndk/platforms/android-$(get_default_api_level_for_arch $ARCH)/arch-$ARCH
         run ssh $REMOTE_HOST "$TMPREMOTE/ndk/build/tools/build-host-prebuilts.sh $TMPREMOTE/toolchain --package-dir=$TMPREMOTE/packages --arch=$ARCH --ndk-dir=$TMPREMOTE/ndk --no-gen-platforms"
diff --git a/build/tools/dev-platform-expand-all.sh b/build/tools/dev-platform-expand-all.sh
index 48e3866..5fd7928 100755
--- a/build/tools/dev-platform-expand-all.sh
+++ b/build/tools/dev-platform-expand-all.sh
@@ -31,7 +31,8 @@
 register_var_option "--dst-dir=<path>" DSTDIR "Destination directory"
 
 # Default architecture, note we can have several ones here
-ARCHS="arm,x86,mips"
+ARCHS=$(find_ndk_unknown_archs)
+ARCHS="$DEFAULT_ARCHS $ARCHS"
 register_var_option "--arch=<name>" ARCHS "List of target architectures"
 
 PROGRAM_PARAMETERS=""
diff --git a/build/tools/dev-platform-expand.sh b/build/tools/dev-platform-expand.sh
index 9eda0a8..df0894d 100755
--- a/build/tools/dev-platform-expand.sh
+++ b/build/tools/dev-platform-expand.sh
@@ -63,7 +63,8 @@
 register_var_option "--dst-dir=<path>" DSTDIR "Destination directory"
 
 # Default architecture, note we can have several ones here
-ARCHS="arm,x86,mips"
+ARCHS=$(find_ndk_unknown_archs)
+ARCHS="$DEFAULT_ARCHS $ARCHS"
 register_var_option "--arch=<name>" ARCHS "List of target architectures"
 
 PROGRAM_PARAMETERS=""
diff --git a/build/tools/dev-rebuild-ndk.sh b/build/tools/dev-rebuild-ndk.sh
index 3bf72c1..1c2ea91 100755
--- a/build/tools/dev-rebuild-ndk.sh
+++ b/build/tools/dev-rebuild-ndk.sh
@@ -150,7 +150,8 @@
 fi
 
 
-ARCHS=$DEFAULT_ARCHS
+ARCHS=$(find_ndk_unknown_archs)
+ARCHS="$DEFAULT_ARCHS $ARCHS"
 
 # Build the platform
 echo
diff --git a/build/tools/gen-platforms.sh b/build/tools/gen-platforms.sh
index b3fa96a..6bfa00e 100755
--- a/build/tools/gen-platforms.sh
+++ b/build/tools/gen-platforms.sh
@@ -54,7 +54,8 @@
 SRCDIR="../development/ndk"
 DSTDIR="$ANDROID_NDK_ROOT"
 
-ARCHS="$DEFAULT_ARCHS"
+ARCHS=$(find_ndk_unknown_archs)
+ARCHS="$DEFAULT_ARCHS $ARCHS"
 PLATFORMS=`extract_platforms_from "$SRCDIR"`
 NDK_DIR=$ANDROID_NDK_ROOT
 
diff --git a/build/tools/prebuilt-common.sh b/build/tools/prebuilt-common.sh
index d87147d..a87b0c8 100644
--- a/build/tools/prebuilt-common.sh
+++ b/build/tools/prebuilt-common.sh
@@ -1138,11 +1138,11 @@
     fi
 }
 
-# Find all archs from $NDK_DIR/platforms/android-*
-# Return: the list of found arch names
+# Find all archs from $DEV_DIR/platforms or $NDK_DIR/platforms
+# Return: the list of found arch name
 find_ndk_archs ()
 {
-    local NDK_ROOT_DIR
+    local NDK_ROOT_DIR DEVDIR
     local RESULT FOUND_ARCHS
 
     if [ ! -z "$NDK_DIR" ]; then
@@ -1151,15 +1151,31 @@
         NDK_ROOT_DIR=$ANDROID_NDK_ROOT
     fi
 
-    if [ ! -d $NDK_ROOT_DIR/platforms ]; then
-        echo "ERROR: Cannot find directory '$NDK_ROOT_DIR/platforms'!"
-        exit 1
+    DEVDIR="$ANDROID_NDK_ROOT/../development/ndk"
+
+    # Check development directory first
+    if [ -d $DEVDIR/platforms ]; then
+        RESULT=$(ls $DEVDIR/platforms/android-* | grep "arch-")
+        for arch in $RESULT; do
+            arch=$(basename $arch | sed -e 's/^arch-//')
+            FOUND_ARCHS="$FOUND_ARCHS $arch"
+        done
     fi
-    RESULT=$(ls $NDK_ROOT_DIR/platforms/android-* | grep "arch-")
-    for arch in $RESULT; do
-        arch=$(basename $arch | sed -e 's/^arch-//')
-        FOUND_ARCHS="$FOUND_ARCHS $arch"
-    done
+
+    # Check ndk directory
+    if [ -z "$FOUND_ARCHS" ] && [ -d $NDK_ROOT_DIR/platforms ]; then
+        RESULT=$(ls $NDK_ROOT_DIR/platforms/android-* | grep "arch-")
+        for arch in $RESULT; do
+            arch=$(basename $arch | sed -e 's/^arch-//')
+            FOUND_ARCHS="$FOUND_ARCHS $arch"
+        done
+    fi
+
+    # If we cannot find any arch, set to default archs
+    if [ -z "$FOUND_ARCHS" ]; then
+        FOUND_ARCHS=$DEFAULT_ARCHS
+    fi
+
     echo "$(sort_uniq $FOUND_ARCHS)"
 }
 
diff --git a/build/tools/rebuild-all-prebuilt.sh b/build/tools/rebuild-all-prebuilt.sh
index fbc8252..1db0d39 100755
--- a/build/tools/rebuild-all-prebuilt.sh
+++ b/build/tools/rebuild-all-prebuilt.sh
@@ -27,7 +27,8 @@
 BUILD_DIR=/tmp/ndk-$USER/build
 register_var_option "--build-dir=<path>" BUILD_DIR "Specify temporary build directory"
 
-ARCHS=$DEFAULT_ARCHS
+ARCHS=$(find_ndk_unknown_archs)
+ARCHS="$DEFAULT_ARCHS $ARCHS"
 register_var_option "--arch=<arch>" ARCHS "Specify target architectures"
 
 SYSTEMS=$HOST_TAG32