Merge "Support plugin in platforms."
diff --git a/build/tools/build-stlport.sh b/build/tools/build-stlport.sh
index 908943a..f6dce8f 100755
--- a/build/tools/build-stlport.sh
+++ b/build/tools/build-stlport.sh
@@ -163,6 +163,12 @@
     local DSTDIR="$4"
     local DEFAULT_CFLAGS DEFAULT_CXXLAGS
     local SRC OBJ OBJECTS CFLAGS CXXFLAGS
+    local UNKNOWN_ARCH=$(find_ndk_unknown_archs | grep $ABI)
+
+    # Don't build gabi++ for unknown archs
+    if [ ! -z "$UNKNOWN_ARCH" ]; then
+        GABIXX_SOURCES=
+    fi
 
     mkdir -p "$BUILDDIR"
 
diff --git a/build/tools/builder-funcs.sh b/build/tools/builder-funcs.sh
index 6204088..b70868d 100644
--- a/build/tools/builder-funcs.sh
+++ b/build/tools/builder-funcs.sh
@@ -446,7 +446,8 @@
 # $4: Optional Makefile name
 builder_begin_android ()
 {
-    local ARCH ABI PLATFORM BUILDDIR DSTDIR SYSROOT CFLAGS
+    local ABI BUILDDIR LLVM_VERSION MAKEFILE
+    local ARCH UNKNOWN_ARCH PLATFORM SYSROOT FLAGS
     local CRTBEGIN_SO_O CRTEND_SO_O CRTBEGIN_EXE_SO CRTEND_SO_O
     local BINPREFIX GCC_TOOLCHAIN LLVM_TRIPLE
     if [ -z "$NDK_DIR" ]; then
@@ -455,14 +456,22 @@
         panic "Missing directory: $NDK_DIR/platforms"
     fi
     ABI=$1
+    BUILDDIR=$2
+    LLVM_VERSION=$3
+    MAKEFILE=$4
     ARCH=$(convert_abi_to_arch $ABI)
+    UNKNOWN_ARCH=$(find_ndk_unknown_archs | grep $ARCH)
     PLATFORM=${2##android-}
     SYSROOT=$NDK_DIR/platforms/android-$PLATFORM/arch-$ARCH
 
-    if [ -z "$3" ]; then
+    if [ ! -z "$UNKNOWN_ARCH" ]; then
+        LLVM_VERSION=$DEFAULT_LLVM_VERSION
+    fi
+
+    if [ -z "$LLVM_VERSION" ]; then
         BINPREFIX=$NDK_DIR/$(get_default_toolchain_binprefix_for_arch $ARCH)
     else
-        BINPREFIX=$NDK_DIR/$(get_llvm_toolchain_binprefix $3)
+        BINPREFIX=$NDK_DIR/$(get_llvm_toolchain_binprefix $LLVM_VERSION)
         GCC_TOOLCHAIN=`dirname $NDK_DIR/$(get_default_toolchain_binprefix_for_arch $ARCH)`
         GCC_TOOLCHAIN=`dirname $GCC_TOOLCHAIN`
     fi
@@ -481,9 +490,9 @@
         CRTEND_SO_O=$CRTEND_EXE_O
     fi
 
-    builder_begin "$2" "$4"
+    builder_begin "$BUILDDIR" "$MAKEFILE"
     builder_set_prefix "$ABI "
-    if [ -z "$3" ]; then
+    if [ -z "$LLVM_VERSION" ]; then
         builder_set_binprefix "$BINPREFIX"
     else
         builder_set_binprefix_llvm "$BINPREFIX"
@@ -500,9 +509,22 @@
             mips)
                 LLVM_TRIPLE=mipsel-none-linux-android
                 ;;
+            *)
+                LLVM_TRIPLE=le32-none-ndk
+                GCC_TOOLCHAIN=
+                CRTBEGIN_SO_O=
+                CRTEND_SO_O=
+                CRTBEGIN_EXE_O=
+                CRTEND_EXE_O=
+                FLAGS=-emit-llvm
+                ;;
         esac
-        builder_cflags "-target $LLVM_TRIPLE -gcc-toolchain $GCC_TOOLCHAIN"
-        builder_ldflags "-target $LLVM_TRIPLE -gcc-toolchain $GCC_TOOLCHAIN"
+        builder_cflags "-target $LLVM_TRIPLE $FLAGS"
+        builder_ldflags "-target $LLVM_TRIPLE $FLAGS"
+        if [ ! -z $GCC_TOOLCHAIN ]; then
+            builder_cflags "-gcc-toolchain $GCC_TOOLCHAIN"
+            builder_ldflags "-gcc-toolchain $GCC_TOOLCHAIN"
+        fi
     fi
 
     builder_cflags "--sysroot=$SYSROOT"
diff --git a/build/tools/prebuilt-common.sh b/build/tools/prebuilt-common.sh
index d6b6433..a83b92a 100644
--- a/build/tools/prebuilt-common.sh
+++ b/build/tools/prebuilt-common.sh
@@ -127,6 +127,19 @@
     fi
 }
 
+# Returns all words in text that do not match any of the pattern
+# $1: pattern
+# $2: text
+filter_out ()
+{
+    local PATTERN="$1"
+    local TEXT="$2"
+    for pat in $PATTERN; do
+        TEXT=$(echo $TEXT | sed -e 's/'$pat' //g' -e 's/'$pat'$//g')
+    done
+    echo $TEXT
+}
+
 # Assign a value to a variable
 # $1: Variable name
 # $2: Value
@@ -1091,13 +1104,40 @@
     fi
 }
 
+# Find all archs from $NDK_DIR/platforms/android-*
+# Return: the list of found arch names
+find_ndk_archs ()
+{
+    local RESULT FOUND_ARCHS
+    if [ ! -d $NDK_DIR/platforms ]; then
+        echo "ERROR: Cannot find directory '$NDK_DIR/platforms'!"
+        exit 1
+    fi
+    RESULT=$(ls $NDK_DIR/platforms/android-* | grep "arch-")
+    for arch in $RESULT; do
+        arch=$(basename $arch | sed -e 's/^arch-//')
+        FOUND_ARCHS="$FOUND_ARCHS $arch"
+    done
+    echo "$(sort_uniq $FOUND_ARCHS)"
+}
+
+# Find unknown archs from $NDK_DIR/platforms
+# Return: arch names not in ndk default archs
+find_ndk_unknown_archs()
+{
+  local FOUND_ARCHS=$(find_ndk_archs)
+  echo "$(filter_out "$DEFAULT_ARCHS" "$FOUND_ARCHS")"
+}
+
 # Convert an ABI name into an Architecture name
 # $1: ABI name
 # Result: Arch name
 convert_abi_to_arch ()
 {
     local RET
-    case $1 in
+    local ABI=$1
+    local FOUND_ARCH
+    case $ABI in
         armeabi|armeabi-v7a)
             RET=arm
             ;;
@@ -1108,8 +1148,13 @@
             RET=mips
             ;;
         *)
-            2> echo "ERROR: Unsupported ABI name: $1, use one of: armeabi, armeabi-v7a or x86 or mips"
-            exit 1
+            FOUND_ARCH=$(echo $(find_ndk_unknown_archs) | grep $ABI)
+            if [ ! -z $FOUND_ARCH ]; then
+                RET=$ABI
+            else
+                >&2 echo "ERROR: Unsupported ABI name: $ABI, use one of: armeabi, armeabi-v7a or x86 or mips"
+                exit 1
+            fi
             ;;
     esac
     echo "$RET"
@@ -1122,7 +1167,9 @@
 convert_arch_to_abi ()
 {
     local RET
-    case $1 in
+    local ARCH=$1
+    local FOUND_ARCH
+    case $ARCH in
         arm)
             RET=armeabi,armeabi-v7a
             ;;
@@ -1133,8 +1180,13 @@
             RET=mips
             ;;
         *)
-            >&2 echo "ERROR: Unsupported ARCH name: $1, use one of: arm, x86, mips"
-            exit 1
+            FOUND_ARCH=$(echo $(find_ndk_unknown_archs) | grep $ARCH)
+            if [ ! -z $FOUND_ARCH ]; then
+                RET=$ARCH
+            else
+                >&2 echo "ERROR: Unsupported ARCH name: $ARCH, use one of: arm, x86, mips"
+                exit 1
+            fi
             ;;
     esac
     echo "$RET"