merge from open-source master

Change-Id: I7ef151320d924b2b258163d8aabf5eeebffecd39
diff --git a/ndk/build/host-setup.sh b/ndk/build/host-setup.sh
index a29cf84..a677b8f 100755
--- a/ndk/build/host-setup.sh
+++ b/ndk/build/host-setup.sh
@@ -122,6 +122,23 @@
     log2 "    Check $result"
 }
 
+bad_toolchain()
+{
+    tc=$1	# Toolchain
+    ht=$2	# Host Tag
+    echo ""
+    echo "ERROR: Toolchain compiler not found"
+    echo "It seems you do not have the correct $tc toolchain binaries."
+    echo "This may be the result of incorrect unzipping of the NDK archive."
+    echo "Please go to the official Android NDK web site and download the"
+    echo "appropriate NDK package for your platform ($ht)."
+    echo "See http://developer.android.com/sdk/index.html"
+    echo ""
+    echo "ABORTING."
+    echo ""
+}
+
+
 OPTION_HELP=no
 OPTION_NO_MAKE_CHECK=no
 OPTION_NO_AWK_CHECK=no
@@ -216,10 +233,15 @@
 add_config "HOST_TAG      := $HOST_TAG"
 add_config "HOST_AWK      := $AWK"
 
-## Check that the toolchains we need are installed
+## Determine which toolchains are installed and verify their integrity.
 ## Otherwise, instruct the user to download them from the web site
 
-TOOLCHAINS=arm-eabi-4.2.1
+TOOLCHAINS=`ls build/prebuilt/$HOST_TAG`
+
+if [ -z "$TOOLCHAINS" ]; then
+    bad_toolchain NONE_FOUND $HOST_TAG
+    exit 1
+fi
 
 for tc in $TOOLCHAINS; do
     echo "Toolchain  : Checking for $tc prebuilt binaries"
@@ -228,16 +250,7 @@
     COMPILER_PATTERN=$ANDROID_NDK_ROOT/$PREBUILT_BIN/*-gcc$HOST_EXE
     COMPILERS=`ls $COMPILER_PATTERN 2> /dev/null`
     if [ -z $COMPILERS ] ; then
-        echo ""
-        echo "ERROR: Toolchain compiler not found"
-        echo "It seems you do not have the correct $tc toolchain binaries."
-        echo "This may be the result of incorrect unzipping of the NDK archive."
-        echo "Please go to the official Android NDK web site and download the"
-        echo "appropriate NDK package for your platform ($HOST_TAG)."
-        echo "See http://developer.android.com/sdk/index.html"
-        echo ""
-        echo "ABORTING."
-        echo ""
+        bad_toolchain $tc $HOST_TAG
         exit 1
     fi
 done
diff --git a/ndk/build/toolchains/x86-4.2.1/config.mk b/ndk/build/toolchains/x86-4.2.1/config.mk
new file mode 100644
index 0000000..ea11e02
--- /dev/null
+++ b/ndk/build/toolchains/x86-4.2.1/config.mk
@@ -0,0 +1,19 @@
+# Copyright (C) 2009 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# config file for the x86-4.2.1 toolchain for the Android NDK
+# the real meat is in the setup.mk file adjacent to this one
+#
+TOOLCHAIN_ABIS := x86
diff --git a/ndk/build/toolchains/x86-4.2.1/setup.mk b/ndk/build/toolchains/x86-4.2.1/setup.mk
new file mode 100644
index 0000000..2579148
--- /dev/null
+++ b/ndk/build/toolchains/x86-4.2.1/setup.mk
@@ -0,0 +1,128 @@
+# Copyright (C) 2009 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# this file is used to prepare the NDK to build with the x86-4.2.1
+# toolchain any number of source files
+#
+# its purpose is to define (or re-define) templates used to build
+# various sources into target object files, libraries or executables.
+#
+# Note that this file may end up being parsed several times in future
+# revisions of the NDK.
+#
+
+TOOLCHAIN_NAME   := x86-4.2.1
+TOOLCHAIN_PREFIX := $(HOST_PREBUILT)/$(TOOLCHAIN_NAME)/bin/i686-android-linux-gnu-
+
+TARGET_CFLAGS.common := \
+    -I$(SYSROOT)/usr/include \
+    -msoft-float -fpic \
+    -ffunction-sections \
+    -funwind-tables \
+    -fstack-protector \
+    -fno-short-enums
+
+TARGET_x86_release_CFLAGS :=  -O2 \
+                              -fomit-frame-pointer \
+                              -fstrict-aliasing    \
+                              -funswitch-loops     \
+                              -finline-limit=300
+
+# When building for debug, compile everything as x86.
+TARGET_x86_debug_CFLAGS := $(TARGET_x86_release_CFLAGS) \
+                           -fno-omit-frame-pointer \
+                           -fno-strict-aliasing
+
+# This function will be called to determine the target CFLAGS used to build
+# a C or Assembler source file, based on its tags.
+#
+TARGET-process-src-files-tags = \
+$(eval __debug_sources := $(call get-src-files-with-tag,debug)) \
+$(eval __release_sources := $(call get-sr-files-without-tag,debug)) \
+$(call set-src-files-target-cflags, $(__debug_sources), $(TARGET_x86_debug_CFLAGS)) \
+$(call set-src-files-target-cflags, $(__release_sources),$(TARGET_x86_release_CFLAGS)) \
+$(call set-src-files-text,$(LOCAL_SRC_FILES),x86$(space)$(space)) \
+
+TARGET_CC       := $(TOOLCHAIN_PREFIX)gcc
+TARGET_CFLAGS   := $(TARGET_CFLAGS.common)
+
+
+TARGET_CXX      := $(TOOLCHAIN_PREFIX)g++
+TARGET_CXXFLAGS := $(TARGET_CFLAGS.common) -fno-exceptions -fno-rtti
+
+TARGET_LD      := $(TOOLCHAIN_PREFIX)ld
+TARGET_LDFLAGS :=
+
+TARGET_AR      := $(TOOLCHAIN_PREFIX)ar
+TARGET_ARFLAGS := crs
+
+TARGET_LIBGCC := $(shell $(TARGET_CC) -print-libgcc-file-name)
+TARGET_LDLIBS := -Wl,-rpath-link=$(SYSROOT)/usr/lib
+
+# These flags are used to ensure that a binary doesn't reference undefined
+# flags.
+TARGET_NO_UNDEFINED_LDFLAGS := -Wl,--no-undefined
+
+# The ABI-specific sub-directory that the SDK tools recognize for
+# this toolchain's generated binaries
+TARGET_ABI_SUBDIR := x86
+
+# NOTE: Ensure that TARGET_LIBGCC is placed after all private objects
+#       and static libraries, but before any other library in the link
+#       command line when generating shared libraries and executables.
+#
+#       This ensures that all libgcc.a functions required by the target
+#       will be included into it, instead of relying on what's available
+#       on other libraries like libc.so, which may change between system
+#       releases due to toolchain or library changes.
+#
+define cmd-build-shared-library
+$(TARGET_CC) \
+    -nostdlib -Wl,-soname,$(notdir $@) \
+    -Wl,-shared,-Bsymbolic \
+    $(PRIVATE_OBJECTS) \
+    -Wl,--whole-archive \
+    $(PRIVATE_WHOLE_STATIC_LIBRARIES) \
+    -Wl,--no-whole-archive \
+    $(PRIVATE_STATIC_LIBRARIES) \
+    $(TARGET_LIBGCC) \
+    $(PRIVATE_SHARED_LIBRARIES) \
+    $(PRIVATE_LDFLAGS) \
+    $(PRIVATE_LDLIBS) \
+    -o $@
+endef
+
+define cmd-build-executable
+$(TARGET_CC) \
+    -nostdlib -Bdynamic \
+    -Wl,-dynamic-linker,/system/bin/linker \
+    -Wl,--gc-sections \
+    -Wl,-z,nocopyreloc \
+    $(TARGET_CRTBEGIN_DYNAMIC_O) \
+    $(PRIVATE_OBJECTS) \
+    $(PRIVATE_STATIC_LIBRARIES) \
+    $(TARGET_LIBGCC) \
+    $(PRIVATE_SHARED_LIBRARIES) \
+    $(PRIVATE_LDFLAGS) \
+    $(PRIVATE_LDLIBS) \
+    $(TARGET_CRTEND_O) \
+    -o $@
+endef
+
+define cmd-build-static-library
+$(TARGET_AR) $(TARGET_ARFLAGS) $@ $(PRIVATE_OBJECTS)
+endef
+
+cmd-strip = $(TOOLCHAIN_PREFIX)strip --strip-debug $1
diff --git a/ndk/build/tools/build-ndk-sysroot.sh b/ndk/build/tools/build-ndk-sysroot.sh
index c9f3285..4b3bb41 100755
--- a/ndk/build/tools/build-ndk-sysroot.sh
+++ b/ndk/build/tools/build-ndk-sysroot.sh
@@ -36,13 +36,12 @@
 # ABI is the target ABI name for the NDK
 ABI=arm
 
-# ARCH is the target ABI name in the Android sources
-ARCH=arm
-
 OPTION_HELP=no
 OPTION_BUILD_OUT=
 OPTION_PLATFORM=
 OPTION_PACKAGE=no
+OPTION_ABI=
+
 for opt do
   optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
   case "$opt" in
@@ -64,6 +63,9 @@
   --package)
     OPTION_PACKAGE=yes
     ;;
+  --abi=*)
+    OPTION_ABI=$optarg
+    ;;
   *)
     echo "unknown option '$opt', use --help"
     exit 1
@@ -80,6 +82,7 @@
     echo "  --help             print this message"
     echo "  --verbose          enable verbose messages"
     echo "  --platform=<name>  generate sysroot for platform <name> (default is $PLATFORM)"
+    echo "  --abi=<name>       generate sysroot for abi <name> (default is $ABI)"
     echo "  --build-out=<path> set Android build out directory"
     echo "  --package          generate sysroot package tarball"
     echo ""
@@ -90,6 +93,20 @@
     PLATFORM=$OPTION_PLATFORM
 fi
 
+if [ -n "$OPTION_ABI" ] ; then
+    ABI=$OPTION_ABI
+fi
+
+case "$ABI" in
+arm*)
+    ARCH=arm
+    ;;
+*)
+    ARCH=$ABI
+    ;;
+esac
+
+
 # Get the root of the NDK from the current program location
 NDK_ROOT=`dirname $0`
 NDK_ROOT=`dirname $NDK_ROOT`
@@ -258,14 +275,18 @@
 
 # for libm, just copy math.h and fenv.h
 common_header $BIONIC_ROOT/libm/include math.h
-arch_header   $BIONIC_ROOT/libm/include $ARCH/fenv.h
+if [ "$ARCH" = "x86" ]; then
+    arch_header   $BIONIC_ROOT/libm/include i387/fenv.h
+else
+    arch_header   $BIONIC_ROOT/libm/include $ARCH/fenv.h
+fi
 
 # our tiny C++ standard library
 common_headers $BIONIC_ROOT/libstdc++/include
 
 # C library kernel headers
 common_headers $LIBC_ROOT/kernel/common
-arch_headers   $LIBC_ROOT/kernel/arch-arm
+arch_headers   $LIBC_ROOT/kernel/arch-$ARCH
 
 # C library headers
 common_headers $LIBC_ROOT/include
@@ -275,6 +296,6 @@
 if [ $OPTION_PACKAGE = yes ] ; then
     DATE=`date +%Y%m%d`
     PKGFILE=/tmp/android-ndk-sysroot-$DATE.tar.bz2
-    tar cjf $PKGFILE build/platforms/$PLATFORM
+    tar cjf $PKGFILE build/platforms/$PLATFORM/arch-$ARCH
     echo "Packaged in $PKGFILE"
 fi
diff --git a/ndk/build/tools/build-toolchain.sh b/ndk/build/tools/build-toolchain.sh
index 80bb4e9..f025c3a 100755
--- a/ndk/build/tools/build-toolchain.sh
+++ b/ndk/build/tools/build-toolchain.sh
@@ -23,6 +23,32 @@
 # include common function and variable definitions
 . `dirname $0`/../core/ndk-common.sh
 
+print_help() {
+    echo "Rebuild the prebuilt binaries for the Android NDK toolchain."
+    echo "This script will automatically download the sources from the"
+    echo "Internet, unless you use the --package=<file> option to specify"
+    echo "the exact source package to use."
+    echo ""
+    echo "See build/tools/download-toolchain-sources.sh for a tool that"
+    echo "can create a compatible source package from the current"
+    echo "git repositories."
+    echo ""
+    echo "options (defaults are within brackets):"
+    echo ""
+    echo "  --help                   print this message"
+    echo "  --gcc-version=<version>  select GCC version [$GCC_VERSION]"
+    echo "  --gdb-version=<version>  select GDB version [$GDB_VERSION]"
+    echo "  --package=<file>         specify download source package"
+    echo "  --platform=<name>        generate toolchain from platform <name> [$PLATFORM]"
+    echo "  --build-toolchains=<tc>   Only build these toolchains (specifiy multiple times)"
+    echo "  --abi=<name>             generate toolchain from abi <name> [$ABI]"
+    echo "  --release=<name>         specify prebuilt release name [$RELEASE]"
+    echo "  --build-out=<path>       set temporary build out directory [/tmp/<random>]"
+    echo "  --force-download         force a download and unpacking of the toolchain sources"
+    echo "  --force-build            force a rebuild of the sources"
+    echo ""
+}
+
 # number of jobs to run in parallel when running make
 JOBS=$HOST_NUM_CPUS
 
@@ -37,19 +63,20 @@
 OPTION_HELP=no
 OPTION_PLATFORM=
 OPTION_FORCE_32=no
-OPTION_REBUILD=no
+OPTION_FORCE_BUILD=no
 OPTION_GCC_VERSION=
 OPTION_GDB_VERSION=
 OPTION_PACKAGE=
 OPTION_RELEASE=
 OPTION_BUILD_OUT=
+OPTION_BUILD_TOOLCHAINS=
 
 VERBOSE=no
 for opt do
     optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
     case "$opt" in
     --help|-h|-\?) OPTION_HELP=yes
-    ;;
+        ;;
     --verbose)
         if [ "$VERBOSE" = "yes" ] ; then
             VERBOSE2=yes
@@ -85,6 +112,9 @@
     --force-build)
         OPTION_FORCE_BUILD=yes
         ;;
+    --build-toolchains=*)
+        OPTION_BUILD_TOOLCHAINS="$OPTION_BUILD_TOOLCHAINS $optarg"
+        ;;
     --verbose)
         VERBOSE=yes
         ;;
@@ -95,28 +125,7 @@
 done
 
 if [ $OPTION_HELP = "yes" ] ; then
-    echo "Rebuild the prebuilt binaries for the Android NDK toolchain."
-    echo "This script will automatically download the sources from the"
-    echo "Internet, unless you use the --package=<file> option to specify"
-    echo "the exact source package to use."
-    echo ""
-    echo "See build/tools/download-toolchain-sources.sh for a tool that"
-    echo "can create a compatible source package from the current"
-    echo "git repositories."
-    echo ""
-    echo "options (defaults are within brackets):"
-    echo ""
-    echo "  --help                   print this message"
-    echo "  --gcc-version=<version>  select GCC version [$GCC_VERSION]"
-    echo "  --gdb-version=<version>  select GDB version [$GDB_VERSION]"
-    echo "  --package=<file>         specify download source package"
-    echo "  --platform=<name>        generate toolchain from platform <name> [$PLATFORM]"
-    echo "  --abi=<name>             generate toolchain from abi <name> [$ABI]"
-    echo "  --release=<name>         specify prebuilt release name [$RELEASE]"
-    echo "  --build-out=<path>       set temporary build out directory [/tmp/<random>]"
-    echo "  --force-download         force a download and unpacking of the toolchain sources"
-    echo "  --force-build            force a rebuild of the sources"
-    echo ""
+    print_help
     exit 0
 fi
 
@@ -129,6 +138,27 @@
         ;;
 esac
 
+case "$ABI" in
+arm )
+    ARCH="arm"
+    ABI_INSTALL_NAME="arm-eabi"
+    ABI_TOOLCHAIN_PREFIX="arm-eabi"
+    ABI_CONFIGURE_HOST="arm-eabi-linux"
+    ;;
+x86 )
+    ARCH="x86"
+    ABI_INSTALL_NAME="x86"
+    ABI_TOOLCHAIN_PREFIX="i686-android-linux-gnu"
+    ABI_CONFIGURE_HOST="i686-linux"
+    ;;
+* )
+    echo "Invalid ABI specified ($ABI). Expected [arm | x86]"
+    echo
+    print_help
+    exit 1
+    ;;
+esac
+
 TMPLOG=/tmp/android-toolchain-build-$$.log
 rm -rf $TMPLOG
 
@@ -157,6 +187,7 @@
     }
 fi
 
+
 if [ -n "$OPTION_GCC_VERSION" ] ; then
     GCC_VERSION="$OPTION_GCC_VERSION"
     log "Using gcc version $GCC_VERSION"
@@ -203,7 +234,7 @@
 
 # where the sysroot is located
 ANDROID_TOOLCHAIN_SRC=$OUT/src
-ANDROID_SYSROOT=$ANDROID_NDK_ROOT/build/platforms/$PLATFORM/arch-$ABI
+ANDROID_SYSROOT=$ANDROID_NDK_ROOT/build/platforms/$PLATFORM/arch-$ARCH
 
 # Let's check that we have a working md5sum here
 A_MD5=`echo "A" | md5sum | cut -d' ' -f1`
@@ -389,14 +420,13 @@
 }
 
 # Unpack a given package in a target location
-# $1: package name
-# $2: target directory
+# $1: package name      (e.g. toolchain)
+# $2: target directory  (e.g. /tmp/foo)
 #
 unpack_package ()
 {
-    WORKSPACE=$ANDROID_NDK_ARCHIVE/$1
-    SRCDIR=$2
     SRCPKG=`var_value PKG_$1`
+    SRCDIR=$2
     if ! timestamp_check $1 unpack; then
         echo "Unpack  : $1 sources"
         echo "          from $SRCPKG"
@@ -413,6 +443,45 @@
             exit 1
         fi
         timestamp_set   $1 unpack
+        timestamp_force $1 patch
+    fi
+}
+
+# Patch a given package at a target location
+# $1: package name      (e.g. toolchain)
+# $2: target directory  (e.g. /tmp/foo)
+# $3: patch directory   (e.g. build/tools/toolchain-patches)
+#
+# The rationale here is that anything named like $3/<subpath>/<foo>.patch
+# will be applied with "patch -p1" under $2/<subpath>
+#
+# Patches are listed and applied in alphanumerical order of their names
+# as returned by 'find'. Consider using numbered prefixes like the patch
+# files generated by "git format-patch" are named.
+#
+patch_package ()
+{
+    SRCPKG=`var_value PKG_$1`
+    SRCDIR=$2
+    if ! timestamp_check $1 patch; then
+        PATCH_FILES=`(cd $3 && find . -name "*.patch") 2> /dev/null`
+        if [ -z "$PATCH_FILES" ] ; then
+            echo "Patch   : none provided"
+            return
+        fi
+        for PATCH in $PATCH_FILES; do
+            echo "Patch   : $1 sources"
+            echo "          from $PATCH"
+            echo "          into $SRCDIR"
+            PATCHDIR=`dirname $PATCH`
+            PATCHNAME=`basename $PATCH`
+            cd $SRCDIR/$PATCHDIR && patch -p1 < $3/$PATCH
+            if [ $? != 0 ] ; then
+                echo "Patch failure !! Please check toolchain package !"
+                exit 1
+            fi
+        done
+        timestamp_set   $1 patch
         timestamp_force $1 configure
     fi
 }
@@ -423,7 +492,7 @@
     timestamp_force toolchain verify
 fi
 
-if [ $OPTION_FORCE_BUILD ] ; then
+if [ $OPTION_FORCE_BUILD = "yes" ] ; then
     rm -rf $ANDROID_TOOLCHAIN_BUILD
     timestamp_clear toolchain
     timestamp_clear gdbserver
@@ -443,6 +512,7 @@
 fi
 
 unpack_package toolchain $ANDROID_TOOLCHAIN_SRC
+patch_package  toolchain $ANDROID_TOOLCHAIN_SRC $ANDROID_NDK_ROOT/build/tools/toolchain-patches
 
 # remove all info files from the unpacked toolchain sources
 # they create countless little problems during the build
@@ -479,7 +549,7 @@
         if [ ! -d $BUILD_SRCDIR ] ; then
             BUILD_SRCDIR=$TOOLCHAIN_SRC
         fi
-        OLD_ABI="$ABI"
+        OLD_ABI="${ABI}"
         OLD_CFLAGS="$CFLAGS"
         OLD_LDFLAGS="$LDFLAGS"
         mkdir -p $TOOLCHAIN_BUILD &&
@@ -487,7 +557,7 @@
         export ABI="32" &&  # needed to build a 32-bit gmp
         export CFLAGS="$HOST_CFLAGS" &&
         export LDFLAGS="$HOST_LDFLAGS" && run \
-        $BUILD_SRCDIR/configure --target=arm-eabi \
+        $BUILD_SRCDIR/configure --target=$ABI_TOOLCHAIN_PREFIX \
                                 --disable-nls \
                                 --prefix=$TOOLCHAIN_PREFIX \
                                 --with-sysroot=$ANDROID_SYSROOT \
@@ -543,7 +613,7 @@
         rm -rf $TOOLCHAIN_PREFIX/man $TOOLCHAIN_PREFIX/info
         # strip binaries to reduce final package size
         strip $TOOLCHAIN_PREFIX/bin/*
-        strip $TOOLCHAIN_PREFIX/arm-eabi/bin/*
+        strip $TOOLCHAIN_PREFIX/$ABI_TOOLCHAIN_PREFIX/bin/*
         strip $TOOLCHAIN_PREFIX/libexec/gcc/*/*/cc1
         strip $TOOLCHAIN_PREFIX/libexec/gcc/*/*/cc1plus
         strip $TOOLCHAIN_PREFIX/libexec/gcc/*/*/collect2
@@ -565,12 +635,19 @@
         OLD_CC="$CC"
         OLD_CFLAGS="$CFLAGS"
         OLD_LDFLAGS="$LDFLAGS"
+
+        INCLUDE_DIRS="-I$TOOLCHAIN_PREFIX/lib/gcc/$ABI_TOOLCHAIN_PREFIX/$GCC_VERSION/include \
+                      -I$ANDROID_SYSROOT/usr/include"
+        CRTBEGIN="$ANDROID_SYSROOT/usr/lib/crtbegin_static.o"
+        CRTEND="$ANDROID_SYSROOT/usr/lib/crtend_android.o"
+        LIBRARY_LDFLAGS="$CRTBEGIN -L$ANDROID_SYSROOT/usr/lib -lc -L$ANDROID_SYSROOT/usr/lib -lm -lgcc $CRTEND "
+
         cd $GDBSERVER_BUILD &&
-        export CC="$TOOLCHAIN_PREFIX/bin/arm-eabi-gcc" &&
-        export CFLAGS="-g -O2 -static -mandroid"  &&
-        export LDFLAGS= &&
+        export CC="$TOOLCHAIN_PREFIX/bin/$ABI_TOOLCHAIN_PREFIX-gcc" &&
+        export CFLAGS="-O2 -nostdinc -nostdlib -D__ANDROID__ -DANDROID -DSTDC_HEADERS $INCLUDE_DIRS"  &&
+        export LDFLAGS="-static -Wl,-z,nocopyreloc -Wl,--no-undefined $LIBRARY_LDFLAGS" &&
         run $GDB_SRCDIR/gdb/gdbserver/configure \
-        --host=arm-eabi-linux \
+        --host=${ABI_CONFIGURE_HOST} \
         --with-sysroot=$ANDROID_SYSROOT
         if [ $? != 0 ] ; then
             echo "Could not configure gdbserver build. See $TMPLOG"
@@ -605,7 +682,7 @@
         echo "Install  : $TOOLCHAIN_NAME gdbserver."
         DEST=$TOOLCHAIN_PREFIX/bin
         mkdir -p $DEST &&
-        $TOOLCHAIN_PREFIX/bin/arm-eabi-strip $GDBSERVER_BUILD/gdbserver &&
+        $TOOLCHAIN_PREFIX/bin/${ABI_TOOLCHAIN_PREFIX}-strip $GDBSERVER_BUILD/gdbserver &&
         run cp -f $GDBSERVER_BUILD/gdbserver $DEST/gdbserver
         if [ $? != 0 ] ; then
             echo "Could not install gdbserver. See $TMPLOG"
@@ -621,26 +698,35 @@
 # The old source tarball only contained gcc 4.2.1, the new
 # ones contain multiple toolchains
 #
-if [ -d $ANDROID_TOOLCHAIN_SRC/gcc-4.2.1 ] ; then
-    # An old toolchain source package
-    ANDROID_TOOLCHAIN_LIST=arm-eabi-4.2.1
+if [ "$OPTION_BUILD_TOOLCHAINS" ]; then
+    ANDROID_TOOLCHAIN_LIST="$OPTION_BUILD_TOOLCHAINS"
 else
-    ANDROID_TOOLCHAIN_LIST="arm-eabi-4.2.1 arm-eabi-4.4.0"
+    ANDROID_TOOLCHAIN_LIST=`ls -1 $ANDROID_TOOLCHAIN_SRC/gcc | grep gcc- | sed "s/gcc-/${ABI_INSTALL_NAME}-/"`
 fi
 
 for _toolchain in $ANDROID_TOOLCHAIN_LIST; do
-    if timestamp_check toolchain build; then
+    if [ $OPTION_FORCE_BUILD = "yes" ] ; then
         timestamp_force ${_toolchain} configure
         timestamp_force ${_toolchain}-gdbserver configure
     fi
-    # Gcc 4.2.1 needs binutils 2.17
-    if [ ${_toolchain} = arm-eabi-4.2.1 ] ; then
+    case "${_toolchain}" in
+    x86-4.2.1 )
+        GCC_VERSION=4.2.1
+        BINUTILS_VERSION=2.19
+        ;;
+    arm*-4.2.1 )
         GCC_VERSION=4.2.1
         BINUTILS_VERSION=2.17
-    else
+        ;;
+    x86-4.4.0 )
         GCC_VERSION=4.4.0
         BINUTILS_VERSION=2.19
-    fi
+        ;;
+    arm*-4.4.0 )
+        GCC_VERSION=4.4.0
+        BINUTILS_VERSION=2.19
+        ;;
+    esac
     build_toolchain ${_toolchain}
 done
 
@@ -650,7 +736,7 @@
     echo "Cleanup  : Removing unuseful stuff"
     rm -rf $OUT/build/prebuilt/$HOST_TAG/*/share
     find $OUT/build/prebuilt/$HOST_TAG -name "libiberty.a" | xargs rm -f
-    find $OUT/build/prebuilt/$HOST_TAG -name "libarm-elf-linux-sim.a" | xargs rm -f
+    find $OUT/build/prebuilt/$HOST_TAG -name "lib${ABI}-elf-linux-sim.a" | xargs rm -f
     echo "Package  : $HOST_ARCH toolchain binaries"
     echo "           into $TOOLCHAIN_TARBALL"
     cd $ANDROID_NDK_ROOT &&
diff --git a/ndk/build/tools/download-toolchain-sources.sh b/ndk/build/tools/download-toolchain-sources.sh
index 5d8332d..ffbc065 100755
--- a/ndk/build/tools/download-toolchain-sources.sh
+++ b/ndk/build/tools/download-toolchain-sources.sh
@@ -31,7 +31,7 @@
 RELEASE=`date +%Y%m%d`
 
 # the default branch to use
-BRANCH=eclair
+BRANCH=master
 
 GITCMD=git
 
@@ -161,10 +161,12 @@
     fi
     log "checking out $BRANCH branch of $1.git"
     cd $1
-    run git checkout -b $BRANCH origin/$BRANCH
-    if [ $? != 0 ] ; then
-        echo "Could not checkout $1 ?"
-        exit 1
+    if [ "$BRANCH" != "master" ] ; then
+        run git checkout -b $BRANCH origin/$BRANCH
+        if [ $? != 0 ] ; then
+            echo "Could not checkout $1 ?"
+            exit 1
+        fi
     fi
     # get rid of .git directory, we won't need it.
     cd ..
diff --git a/ndk/build/tools/make-release.sh b/ndk/build/tools/make-release.sh
index 7ddb63d..a74cfb3 100755
--- a/ndk/build/tools/make-release.sh
+++ b/ndk/build/tools/make-release.sh
@@ -171,6 +171,7 @@
     rm -rf $NDK_ROOT_DIR/out
     rm -rf $NDK_ROOT_DIR/apps/*/project/libs/armeabi
     rm -rf $NDK_ROOT_DIR/apps/*/project/libs/armeabi-v7a
+    rm -rf $NDK_ROOT_DIR/apps/*/project/libs/x86
     # Get all files under the NDK root
     GIT_FILES=`cd $NDK_ROOT_DIR && find .`
     GIT_FILES=`echo $GIT_FILES | sed -e "s!\./!!g"`
diff --git a/ndk/docs/CHANGES.TXT b/ndk/docs/CHANGES.TXT
index 51088d7..9701b8c 100644
--- a/ndk/docs/CHANGES.TXT
+++ b/ndk/docs/CHANGES.TXT
@@ -1,6 +1,21 @@
 Android NDK ChangeLog:
 
 -------------------------------------------------------------------------------
+android-ndk-r4
+
+IMPORTANT BUG FIXES:
+
+IMPORTANT CHANGES:
+
+OTHER:
+
+- build-toolchain.sh: Allow ad-hoc patching of toolchain sources when rebuilding
+  them. This is primarily to ease development. All you need to do is put a patch
+  under build/tools/toolchain-patches/<foo>/<name>.patch, and it will be applied
+  with 'patch -p1' into the <foo> directory of the unpacked toolchain sources
+  before the configure step. 
+
+-------------------------------------------------------------------------------
 android-ndk-r3
 
 IMPORTANT BUG FIXES: