Merge "Fix canadian build"
diff --git a/build/tools/build-gcc.sh b/build/tools/build-gcc.sh
index 65ba81b..331c585 100755
--- a/build/tools/build-gcc.sh
+++ b/build/tools/build-gcc.sh
@@ -72,12 +72,12 @@
 register_var_option "--package-dir=<path>" PACKAGE_DIR "Create archive tarball in specific directory"
 
 register_jobs_option
-register_mingw_option
+register_canadian_option
 register_try64_option
 
 extract_parameters "$@"
 
-prepare_mingw_toolchain /tmp/ndk-$USER/build
+prepare_canadian_toolchain /tmp/ndk-$USER/build
 
 fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory"
 setup_default_log_file $BUILD_OUT/config.log
@@ -168,7 +168,7 @@
 
 set_toolchain_ndk $NDK_DIR $TOOLCHAIN
 
-if [ "$MINGW" != "yes" ] ; then
+if [ "$MINGW" != "yes" -a "$DARWIN" != "yes" ] ; then
     dump "Using C compiler: $CC"
     dump "Using C++ compiler: $CXX"
 fi
@@ -222,9 +222,17 @@
 export CXXFLAGS_FOR_TARGET="$ABI_CXXFLAGS_FOR_TARGET"
 # Needed to build a 32-bit gmp on 64-bit systems
 export ABI=$HOST_GMP_ABI
+
+# Note that the following flags only apply for "build" in canadian
 # -Wno-error is needed because our gdb-6.6 sources use -Werror by default
 # and fail to build with recent GCC versions.
-export CFLAGS=$HOST_CFLAGS" -O2 -s -Wno-error"
+CFLAGS_FOR_BUILD="-O2 -s -Wno-error"
+LDFLAGS_FOR_BUILD=
+
+CFLAGS="$CFLAGS_FOR_BUILD $HOST_CFLAGS"
+LDFLAGS="$LDFLAGS_FOR_BUILD $HOST_LDFLAGS"
+
+export CFLAGS LDFLAGS CFLAGS_FOR_BUILD LDFLAGS_FOR_BUILD
 
 # This extra flag is used to slightly speed up the build
 EXTRA_CONFIG_FLAGS="--disable-bootstrap"
@@ -232,18 +240,25 @@
 # This is to disable GCC 4.6 specific features that don't compile well
 # the flags are ignored for older GCC versions.
 EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-libquadmath"
-# Plugins are not supported well before 4.7. On 4.7 it's required to have
-# -flto working. Flag --enable-plugins (note 's') is actually for binutils,
-# this is compiler requirement to have binutils configured this way. Flag
-# --disable-plugin is for gcc.
-case "$GCC_VERSION" in
-    4.4.3|4.6)
-        EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-plugin"
-        ;;
-    *)
-        EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-plugins"
-        ;;
-esac
+if [ "$DARWIN" = "yes" ]; then
+    # Disable plugin because in canadian cross build, plugin gengtype
+    # will be incorrectly linked with build's library and fails.
+    # ToDo
+    EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-plugin"
+else
+    # Plugins are not supported well before 4.7. On 4.7 it's required to have
+    # -flto working. Flag --enable-plugins (note 's') is actually for binutils,
+    # this is compiler requirement to have binutils configured this way. Flag
+    # --disable-plugin is for gcc.
+    case "$GCC_VERSION" in
+        4.4.3|4.6)
+            EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-plugin"
+            ;;
+        *)
+            EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-plugins"
+            ;;
+    esac
+fi
 
 # Enable OpenMP
 EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-libgomp"
@@ -264,7 +279,6 @@
     ;;
 esac
 
-#export LDFLAGS="$HOST_LDFLAGS"
 cd $BUILD_OUT && run \
 $BUILD_SRCDIR/configure --target=$ABI_CONFIGURE_TARGET \
                         --enable-initfini-array \
@@ -300,17 +314,17 @@
     if [ $? = 0 ] ; then
         break
     else
-        if [ "$MINGW" = "yes" ] ; then
+        if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ] ; then
             # Unfortunately, there is a bug in the GCC build scripts that prevent
-            # parallel mingw builds to work properly on some multi-core machines
-            # (but not all, sounds like a race condition). Detect this and restart
-            # in less parallelism, until -j1 also fail
+            # parallel mingw/darwin canadian cross builds to work properly on some
+            # multi-core machines (but not all, sounds like a race condition). Detect
+            # this and restart in less parallelism, until -j1 also fail
             JOBS=$((JOBS/2))
             if [ $JOBS -lt 1 ] ; then
-                echo "Error while building mingw toolchain. See $TMPLOG"
+                echo "Error while building mingw/darwin toolchain. See $TMPLOG"
                 exit 1
             fi
-            dump "Parallel mingw build failed - continuing in less parallelism -j$JOBS"
+            dump "Parallel canadian build failed - continuing in less parallelism -j$JOBS"
         else
             echo "Error while building toolchain. See $TMPLOG"
             exit 1
@@ -331,7 +345,7 @@
 # copy to toolchain path
 run copy_directory "$TOOLCHAIN_BUILD_PREFIX" "$TOOLCHAIN_PATH"
 
-if [ "$MINGW" = "yes" ] ; then
+if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ] ; then
     # For some reasons, libraries in $ABI_CONFIGURE_TARGET (*) are not installed.
     # Hack here to copy them over.
     # (*) FYI: libgcc.a and libgcov.a not installed there in the first place
@@ -365,11 +379,12 @@
 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/include/c++
 
 # strip binaries to reduce final package size
-run strip $TOOLCHAIN_PATH/bin/*
-run strip $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin/*
-run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1$HOST_EXE
-run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1plus$HOST_EXE
-run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/collect2$HOST_EXE
+test -z "$STRIP" && STRIP=strip
+run $STRIP $TOOLCHAIN_PATH/bin/*
+run $STRIP $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin/*
+run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1$HOST_EXE
+run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1plus$HOST_EXE
+run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/collect2$HOST_EXE
 
 # copy SOURCES file if present
 if [ -f "$SRC_DIR/SOURCES" ]; then
diff --git a/build/tools/build-host-awk.sh b/build/tools/build-host-awk.sh
index b0c0957..c4d30de 100755
--- a/build/tools/build-host-awk.sh
+++ b/build/tools/build-host-awk.sh
@@ -25,7 +25,7 @@
 "Rebuild the host awk tool used by the NDK."
 
 register_try64_option
-register_mingw_option
+register_canadian_option
 register_jobs_option
 
 NDK_DIR=$ANDROID_NDK_ROOT
@@ -68,10 +68,14 @@
 
 log "Configuring the build"
 mkdir -p $BUILD_DIR && rm -rf $BUILD_DIR/*
-prepare_mingw_toolchain $BUILD_DIR
+prepare_canadian_toolchain $BUILD_DIR
 log "Building $HOST_TAG awk"
 export HOST_CC="$CC" &&
 export CFLAGS=$HOST_CFLAGS" -O2 -s" &&
+export LDFLAGS=$HOST_LDFLAGS &&
+export NATIVE_CC="gcc" &&
+export NATIVE_CFLAGS=" -O2 -s -I$BUILD_DIR -I." &&
+export NATIVE_LDFLAGS= &&
 run $GNUMAKE \
     -C "$AWK_SRCDIR" \
     -j $NUM_JOBS \
diff --git a/build/tools/build-host-make.sh b/build/tools/build-host-make.sh
index 477bb4a..533f460 100755
--- a/build/tools/build-host-make.sh
+++ b/build/tools/build-host-make.sh
@@ -28,7 +28,7 @@
 register_var_option "--ndk-dir=<path>" NDK_DIR "Install to specific NDK directory"
 
 register_try64_option
-register_mingw_option
+register_canadian_option
 register_jobs_option
 
 OUT=
@@ -74,22 +74,27 @@
 
 CONFIGURE_FLAGS="--disable-nls --disable-rpath"
 if [ "$MINGW" = "yes" ]; then
-    # Required for a proper mingw compile
+    # Required for a proper mingw cross compile
     CONFIGURE_FLAGS=$CONFIGURE_FLAGS" --host=i586-pc-mingw32"
 fi
 
+if [ "$DARWIN" = "yes" ]; then
+    # Required for a proper darwin cross compile
+    CONFIGURE_FLAGS=$CONFIGURE_FLAGS" --host=$ABI_CONFIGURE_HOST"
+fi
+
 log "Configuring the build"
 mkdir -p $BUILD_DIR && rm -rf $BUILD_DIR/*
-prepare_mingw_toolchain $BUILD_DIR
+prepare_canadian_toolchain $BUILD_DIR
 cd $BUILD_DIR &&
 CFLAGS=$HOST_CFLAGS" -O2 -s" &&
 export CC CFLAGS &&
 run $TMP_SRCDIR/configure $CONFIGURE_FLAGS
-fail_panic "Failed to configure the sed-$GNUMAKE_VERSION build!"
+fail_panic "Failed to configure the make-$GNUMAKE_VERSION build!"
 
 log "Building make"
 run $GNUMAKE -j $NUM_JOBS
-fail_panic "Failed to build the sed-$GNUMAKE_VERSION executable!"
+fail_panic "Failed to build the make-$GNUMAKE_VERSION executable!"
 
 log "Copying executable to prebuilt location"
 run mkdir -p $(dirname "$OUT") && cp $(get_host_exec_name make) $OUT
diff --git a/build/tools/build-host-prebuilts.sh b/build/tools/build-host-prebuilts.sh
index ff78b8d..f93fa3d 100755
--- a/build/tools/build-host-prebuilts.sh
+++ b/build/tools/build-host-prebuilts.sh
@@ -33,6 +33,10 @@
 # with the mingw32 cross-toolchain.
 if [ "$SYSTEMS" = "linux-x86" ]; then
     SYSTEMS=$SYSTEMS",windows"
+    # If darwin toolchain exist, build darwin too
+    if [ -f "${DARWIN_TOOLCHAIN}-gcc" ]; then
+        SYSTEMS=$SYSTEMS",darwin-x86"
+    fi
 fi
 CUSTOM_SYSTEMS=
 register_option "--systems=<names>" do_SYSTEMS "List of host systems to build for"
@@ -203,10 +207,17 @@
 
 for SYSTEM in $SYSTEMS; do
 
-    # Add --mingw flag
+    # Add --mingw/--darwin flag
     TOOLCHAIN_FLAGS=$FLAGS
-    if [ "$HOST_TAG32" = "linux-x86" -a "$SYSTEM" = "windows" ]; then
-        TOOLCHAIN_FLAGS=$TOOLCHAIN_FLAGS" --mingw"
+    if [ "$HOST_TAG32" = "linux-x86" ]; then
+        case "$SYSTEM" in
+            windows)
+                TOOLCHAIN_FLAGS=$TOOLCHAIN_FLAGS" --mingw"
+                ;;
+            darwin) 
+                TOOLCHAIN_FLAGS=$TOOLCHAIN_FLAGS" --darwin"
+                ;;
+        esac
     fi
 
     # Should we do a remote build?
diff --git a/build/tools/build-host-sed.sh b/build/tools/build-host-sed.sh
index 38c4b11..26560b6 100755
--- a/build/tools/build-host-sed.sh
+++ b/build/tools/build-host-sed.sh
@@ -25,7 +25,7 @@
 "Rebuild the host sed tool used by the NDK."
 
 register_try64_option
-register_mingw_option
+register_canadian_option
 register_jobs_option
 
 NDK_DIR=$ANDROID_NDK_ROOT
@@ -57,7 +57,7 @@
 
 log "Configuring the build"
 mkdir -p $BUILD_DIR && rm -rf $BUILD_DIR/*
-prepare_mingw_toolchain $BUILD_DIR
+prepare_canadian_toolchain $BUILD_DIR
 cd $BUILD_DIR &&
 CFLAGS=$HOST_CFLAGS" -O2 -s" &&
 export CC CFLAGS &&
diff --git a/build/tools/build-host-toolbox.sh b/build/tools/build-host-toolbox.sh
index ed719f6..76ac8e1 100755
--- a/build/tools/build-host-toolbox.sh
+++ b/build/tools/build-host-toolbox.sh
@@ -88,8 +88,8 @@
 if [ "$BUILD_WINDOWS_SOURCES" ]; then
     ORIGINAL_HOST_TAG=$HOST_TAG
     MINGW=yes
-    handle_mingw
-    prepare_mingw_toolchain $BUILD_DIR
+    handle_canadian_build
+    prepare_canadian_toolchain $BUILD_DIR
 
     SUBDIR=$(get_prebuilt_install_prefix $HOST_TAG)/bin
     DSTDIR=$NDK_DIR/$SUBDIR
diff --git a/build/tools/build-llvm.sh b/build/tools/build-llvm.sh
index 9f64a3b..2d61a5c 100755
--- a/build/tools/build-llvm.sh
+++ b/build/tools/build-llvm.sh
@@ -54,12 +54,12 @@
 register_option "--check" do_check_option "Check LLVM"
 
 register_jobs_option
-register_mingw_option
+register_canadian_option
 register_try64_option
 
 extract_parameters "$@"
 
-prepare_mingw_toolchain /tmp/ndk-$USER/build
+prepare_canadian_toolchain /tmp/ndk-$USER/build
 
 fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory"
 setup_default_log_file $BUILD_OUT/config.log
@@ -131,18 +131,36 @@
 
 set_toolchain_ndk $NDK_DIR $TOOLCHAIN
 
-dump "Using C compiler: $CC"
-dump "Using C++ compiler: $CXX"
+if [ "$MINGW" != "yes" -a "$DARWIN" != "yes" ] ; then
+    dump "Using C compiler: $CC"
+    dump "Using C++ compiler: $CXX"
+fi
 
 rm -rf $BUILD_OUT
 mkdir -p $BUILD_OUT
 
+MAKE_FLAGS=
+if [ "$VERBOSE" = "yes" ]; then
+    MAKE_FLAGS="VERBOSE=1"
+fi
+
 TOOLCHAIN_BUILD_PREFIX=$BUILD_OUT/prefix
 
-CFLAGS="$HOST_CFLAGS $CFLAGS -I$TOOLCHAIN_BUILD_PREFIX/include"
-CXXFLAGS="$CXXFLAGS -I$TOOLCHAIN_BUILD_PREFIX/include"  # polly doesn't look at CFLAGS !
-LDFLAGS="$LDFLAGS -L$TOOLCHAIN_BUILD_PREFIX/lib"
-export CC CXX CFLAGS CXXFLAGS LDFLAGS REQUIRES_RTTI=1
+# Note that the following 2 flags only apply for BUILD_CC in canadian cross build
+CFLAGS_FOR_BUILD="-O2 -I$TOOLCHAIN_BUILD_PREFIX/include"
+LDFLAGS_FOR_BUILD="-L$TOOLCHAIN_BUILD_PREFIX/lib"
+
+CFLAGS="$CFLAGS $CFLAGS_FOR_BUILD $HOST_CFLAGS"
+CXXFLAGS="$CXXFLAGS $CFLAGS_FOR_BUILD $HOST_CFLAGS"  # polly doesn't look at CFLAGS !
+LDFLAGS="$LDFLAGS $LDFLAGS_FOR_BUILD $HOST_LDFLAGS"
+export CC CXX CFLAGS CXXFLAGS LDFLAGS CFLAGS_FOR_BUILD LDFLAGS_FOR_BUILD REQUIRES_RTTI=1
+
+if [ "$DARWIN" = "yes" ]; then
+    # To stop /usr/bin/install -s calls strip on darwin binary
+    export KEEP_SYMBOLS=1
+    # Disable polly for now
+    POLLY=no
+fi
 
 EXTRA_CONFIG_FLAGS=
 rm -rf $SRC_DIR/$TOOLCHAIN/llvm/tools/polly
@@ -214,10 +232,10 @@
 # build the toolchain
 dump "Building : llvm toolchain [this can take a long time]."
 cd $LLVM_BUILD_OUT
-run make -j$NUM_JOBS
+run make -j$NUM_JOBS $MAKE_FLAGS
 fail_panic "Couldn't compile llvm toolchain"
 
-if [ "$CHECK" = "yes" -a "$MINGW" != "yes" ] ; then
+if [ "$CHECK" = "yes" -a "$MINGW" != "yes" -a "$DARWIN" != "yes" ] ; then
     # run the regression test
     dump "Running  : llvm toolchain regression test"
     cd $LLVM_BUILD_OUT
@@ -233,7 +251,7 @@
 
 # install the toolchain to its final location
 dump "Install  : llvm toolchain binaries."
-cd $LLVM_BUILD_OUT && run make install
+cd $LLVM_BUILD_OUT && run make install $MAKE_FLAGS
 fail_panic "Couldn't install llvm toolchain to $TOOLCHAIN_BUILD_PREFIX"
 
 # clean static or shared libraries
@@ -261,6 +279,12 @@
     rm -f $TOOLCHAIN_BUILD_PREFIX/bin/$i.exe
 done
 
+if [ -n "$KEEP_SYMBOLS" ]; then
+    # strip because /usr/bin/install wasn't called with -s
+    test -z "$STRIP" && STRIP=strip
+    $STRIP $TOOLCHAIN_BUILD_PREFIX/bin/*
+fi
+
 # copy to toolchain path
 run copy_directory "$TOOLCHAIN_BUILD_PREFIX" "$TOOLCHAIN_PATH"
 
diff --git a/build/tools/build-ndk-stack.sh b/build/tools/build-ndk-stack.sh
index b4c0669..e565e1a 100755
--- a/build/tools/build-ndk-stack.sh
+++ b/build/tools/build-ndk-stack.sh
@@ -44,7 +44,7 @@
 PACKAGE_DIR=
 register_var_option "--package-dir=<path>" PACKAGE_DIR "Archive binary into specific directory"
 
-register_mingw_option
+register_canadian_option
 register_try64_option
 
 extract_parameters "$@"
@@ -56,7 +56,7 @@
     BUILD_DIR=$NDK_TMPDIR/build-ndk-stack
     log "Auto-config: --build-dir=$BUILD_DIR"
 fi
-prepare_mingw_toolchain $BUILD_DIR
+prepare_canadian_toolchain $BUILD_DIR
 
 OUT=$NDK_DIR/$(get_host_exec_name ndk-stack)
 
@@ -87,6 +87,7 @@
 
 # Let's roll
 export CFLAGS=$HOST_CFLAGS" -O2 -s"
+export LDFLAGS=$HOST_LDFLAGS
 run $GNUMAKE -C $SRCDIR -f $SRCDIR/GNUMakefile \
     -B -j$NUM_JOBS \
     PROGNAME="$OUT" \
@@ -108,5 +109,8 @@
     fail_panic "Could not create package: $PACKAGE_DIR/$ARCHIVE from $OUT"
 fi
 
+log "Cleaning up"
+rm -rf $BUILD_DIR
+
 log "Done!"
 exit 0
diff --git a/build/tools/make-release.sh b/build/tools/make-release.sh
index 2ce5985..a1cdd87 100755
--- a/build/tools/make-release.sh
+++ b/build/tools/make-release.sh
@@ -65,6 +65,10 @@
     find_mingw_toolchain
     if [ -n "$MINGW_GCC" ] ; then
         HOST_SYSTEMS="$HOST_SYSTEMS,windows"
+        # If darwin toolchain exist, build darwin too
+        if [ -z "$DARWIN_SSH" -a -f "${DARWIN_TOOLCHAIN}-gcc" ]; then
+            HOST_SYSTEMS="$HOST_SYSTEMS,darwin-x86"
+        fi
     fi
 fi
 if [ -n "$DARWIN_SSH" ] ; then
diff --git a/build/tools/prebuilt-common.sh b/build/tools/prebuilt-common.sh
index 4a96a2e..6cfb888 100644
--- a/build/tools/prebuilt-common.sh
+++ b/build/tools/prebuilt-common.sh
@@ -271,12 +271,29 @@
 
 
 MINGW=no
-do_mingw_option () { MINGW=yes; }
+DARWIN=no
+do_mingw_option ()
+{
+    if [ "$DARWIN" = "yes" ]; then
+        echo "Can not have both --mingw and --darwin"
+        exit 1
+    fi
+    MINGW=yes;
+}
+do_darwin_option ()
+{
+    if [ "$MINGW" = "yes" ]; then
+        echo "Can not have both --mingw and --darwin"
+        exit 1
+    fi
+    DARWIN=yes; 
+}
 
-register_mingw_option ()
+register_canadian_option ()
 {
     if [ "$HOST_OS" = "linux" ] ; then
         register_option "--mingw" do_mingw_option "Generate windows binaries on Linux."
+        register_option "--darwin" do_darwin_option "Generate darwin binaries on Linux."
     fi
 }
 
@@ -531,50 +548,89 @@
     fi
 }
 
-# Use the check for the availability of a compatibility SDK in Darwin
+# Check for the availability of a compatibility SDK in Darwin
 # this can be used to generate binaries compatible with either Tiger or
 # Leopard.
 #
 # $1: SDK root path
-# $2: MacOS X minimum version (e.g. 10.4)
+# $2: Optional MacOS X minimum version (e.g. 10.5)
+DARWIN_MINVER=10.6
 check_darwin_sdk ()
 {
-    if [ -d "$1" ] ; then
-        HOST_CFLAGS="-isysroot $1 -mmacosx-version-min=$2 -DMAXOSX_DEPLOYEMENT_TARGET=$2"
-        HOST_LDFLAGS="-Wl,-syslibroot,$sdk -mmacosx-version-min=$2"
+    local MACSDK="$1"
+    local MINVER=$2
+
+    if [ -z "$MINVER" ] ; then
+        # expect SDK root path ended up with either MacOSX##.#.sdk or MacOSX##.#u.sdk
+        MINVER=${MACSDK##*MacOSX}
+        MINVER=${MINVER%%.sdk*}
+        if [ "$MINVER" = "10.4u" ]; then
+            MINVER=10.4
+        fi
+    fi
+    if [ -d "$MACSDK" ] ; then
+        HOST_CFLAGS=$HOST_CFLAGS" -isysroot $MACSDK -mmacosx-version-min=$MINVER -DMAXOSX_DEPLOYEMENT_TARGET=$MINVER"
+        HOST_LDFLAGS=$HOST_LDFLAGS" -Wl,-syslibroot,$MACSDK -mmacosx-version-min=$MINVER"
+        DARWIN_MINVER=$MINVER
         return 0  # success
     fi
     return 1
 }
 
-
-handle_mingw ()
+# Probe Darwin SDK in specified diectory $DARWIN_SYSROOT, or
+# /Developer/SDKs/MacOSX10.6.sdk
+#
+probe_darwin_sdk ()
 {
-    # Now handle the --mingw flag
+    if [ -n "$DARWIN_SYSROOT" ]; then
+        if check_darwin_sdk "$DARWIN_SYSROOT"; then
+            log "Use darwin sysroot $DARWIN_SYSROOT"
+        else
+            echo "darwin sysroot $DARWIN_SYSROOT is not valid"
+            exit 1
+        fi
+    elif check_darwin_sdk /Developer/SDKs/MacOSX10.6.sdk 10.6; then
+        log "Generating Snow Leopard-compatible binaries!"
+    else
+        local version=`sw_vers -productVersion`
+        log "Generating $version-compatible binaries!"
+    fi
+}
+
+handle_canadian_build ()
+{
     HOST_EXE=
-    if [ "$MINGW" = "yes" ] ; then
+    if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ] ; then
         case $HOST_TAG in
             linux-*)
                 ;;
             *)
-                echo "ERROR: Can only enable mingw on Linux platforms !"
+                echo "ERROR: Can only enable --mingw or --darwin on Linux platforms !"
                 exit 1
                 ;;
         esac
-        if [ "$TRY64" = "yes" ]; then
-            ABI_CONFIGURE_HOST=x86_64-pc-mingw32msvc
-            HOST_TAG=windows-x86_64
+        if [ "$MINGW" = "yes" ] ; then
+            # NOTE: Use x86_64-pc-mingw32msvc or i586-pc-mingw32msvc because wrappers are generated
+            #       using these names
+            if [ "$TRY64" = "yes" ]; then
+                ABI_CONFIGURE_HOST=x86_64-pc-mingw32msvc
+                HOST_TAG=windows-x86_64
+            else
+                ABI_CONFIGURE_HOST=i586-pc-mingw32msvc
+                HOST_TAG=windows
+            fi
+            HOST_OS=windows
+            HOST_EXE=.exe
         else
-            # NOTE: The canadian-cross build of Binutils 2.19 will fail if you
-            #        use i586-pc-mingw32msvc here. Binutils 2.21 will work ok
-            #        with both names.
-            #       Use i586-pc-mingw32msvc here because wrappers are generated
-            #        using this name
-            ABI_CONFIGURE_HOST=i586-pc-mingw32msvc
-            HOST_TAG=windows
+            if [ "$TRY64" = "yes" ]; then
+                ABI_CONFIGURE_HOST=x86_64-apple-darwin
+                HOST_TAG=darwin-x86_64
+            else
+                ABI_CONFIGURE_HOST=i686-apple-darwin
+                HOST_TAG=darwin-x86
+            fi
+            HOST_OS=darwin
         fi
-        HOST_OS=windows
-        HOST_EXE=.exe
     fi
 }
 
@@ -622,74 +678,114 @@
     done
 }
 
-# If --mingw option is used, check that there is a working
-# mingw32 toolchain installed.
+# Check there is a working cross-toolchain installed.
 #
-# If there is, check that it's working
+# $1: install directory for mingw/darwin wrapper toolchain
 #
-# $1: install directory for wrapper toolchain
-#
-prepare_mingw_toolchain ()
+prepare_canadian_toolchain ()
 {
-    if [ "$MINGW" != "yes" ]; then
+    if [ "$MINGW" != "yes" -a "$DARWIN" != "yes" ]; then
         return
     fi
-    find_mingw_toolchain
-    if [ -z "$MINGW_GCC" ]; then
-        echo "ERROR: Could not find in your PATH any of:"
-        for i in $BINPREFIXLST; do echo "   ${i}gcc"; done
-        echo "Please install the corresponding cross-toolchain and re-run this script"
-        echo "TIP: On Debian or Ubuntu, try: sudo apt-get install $DEBIAN_NAME"
-        exit 1
+    CROSS_GCC=
+    if [ "$MINGW" = "yes" ]; then
+        find_mingw_toolchain
+        if [ -z "$MINGW_GCC" ]; then
+            echo "ERROR: Could not find in your PATH any of:"
+            for i in $BINPREFIXLST; do echo "   ${i}gcc"; done
+            echo "Please install the corresponding cross-toolchain and re-run this script"
+            echo "TIP: On Debian or Ubuntu, try: sudo apt-get install $DEBIAN_NAME"
+            exit 1
+        fi
+        CROSS_GCC=$MINGW_GCC
+    else
+        if [ -z "$DARWIN_TOOLCHAIN" ]; then
+            echo "Please set DARWIN_TOOLCHAIN to darwin cross-toolchain"
+            exit 1
+        fi
+        if [ ! -f "${DARWIN_TOOLCHAIN}-gcc" ]; then
+            echo "darwin cross-toolchain $DARWIN_TOOLCHAIN-gcc doesn't exist"
+            exit 1
+        fi
+        if [ "$HOST_ARCH" = "x86_64" -a "$TRY64" = "yes" ]; then
+            BINPREFIX=x86_64-apple-darwin-
+            DEBIAN_NAME=darwin64
+            HOST_CFLAGS=$HOST_CFLAGS" -m64"
+        else
+            force_32bit_binaries
+            BINPREFIX=i686-apple-darwin-
+            DEBIAN_NAME=darwin32
+            HOST_CFLAGS=$HOST_CFLAGS" -m32"
+        fi
+        CROSS_GCC=${DARWIN_TOOLCHAIN}-gcc
+        probe_darwin_sdk
     fi
-    # Create a wrapper toolchain, and prepend its dir to our PATH
-    MINGW_WRAP_DIR="$1"/$DEBIAN_NAME-wrapper
-    rm -rf "$MINGW_WRAP_DIR"
 
-    DST_PREFIX=${MINGW_GCC%gcc}
+    # Create a wrapper toolchain, and prepend its dir to our PATH
+    CROSS_WRAP_DIR="$1"/$DEBIAN_NAME-wrapper
+    rm -rf "$CROSS_WRAP_DIR"
+    mkdir -p "$CROSS_WRAP_DIR"
+
+    if [ "$DARWIN" = "yes" ] ; then
+        cat > "$CROSS_WRAP_DIR/sw_vers" <<EOF
+#!/bin/sh
+# Tiny utility for the real sw_vers some Makefiles need
+case \$1 in
+    -productVersion)
+        echo $DARWIN_MINVER
+        ;;
+    *)
+        echo "ERROR: Unknown switch \$1"
+        exit 1
+esac
+EOF
+    chmod 0755 "$CROSS_WRAP_DIR/sw_vers"
+    fi
+
+    DST_PREFIX=${CROSS_GCC%gcc}
     if [ "$NDK_CCACHE" ]; then
         DST_PREFIX="$NDK_CCACHE $DST_PREFIX"
     fi
-    $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=$BINPREFIX --dst-prefix="$DST_PREFIX" "$MINGW_WRAP_DIR"
+    $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=$BINPREFIX --dst-prefix="$DST_PREFIX" "$CROSS_WRAP_DIR" \
+        --cflags="$HOST_CFLAGS" --cxxflags="$HOST_CFLAGS" --ldflags="HOST_LDFLAGS"
     # generate wrappers for BUILD toolchain
-    # this is required for mingw build to avoid tools canadian cross configuration issues
+    # this is required for mingw/darwin build to avoid tools canadian cross configuration issues
     # 32-bit BUILD toolchain
     LEGACY_TOOLCHAIN_DIR="$ANDROID_NDK_ROOT/../prebuilts/gcc/linux-x86/host/i686-linux-glibc2.7-4.6"
     $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=i386-linux-gnu- \
-            --dst-prefix="$LEGACY_TOOLCHAIN_DIR/bin/i686-linux-" "$MINGW_WRAP_DIR"
+            --dst-prefix="$LEGACY_TOOLCHAIN_DIR/bin/i686-linux-" "$CROSS_WRAP_DIR"
     $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=i386-pc-linux-gnu- \
-            --dst-prefix="$LEGACY_TOOLCHAIN_DIR/bin/i686-linux-" "$MINGW_WRAP_DIR"
+            --dst-prefix="$LEGACY_TOOLCHAIN_DIR/bin/i686-linux-" "$CROSS_WRAP_DIR"
     # 64-bit BUILD toolchain.  libbfd is still built in 32-bit.  Use gcc-sdk instead
-    # of x86_64-linux-glibc2.7-4.6 which is a 64-bit-only tol
+    # of x86_64-linux-glibc2.7-4.6 which is a 64-bit-only tool
     LEGACY_TOOLCHAIN_DIR="$ANDROID_NDK_ROOT/../prebuilts/tools/gcc-sdk"
     $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=x86_64-linux-gnu- \
-            --dst-prefix="$LEGACY_TOOLCHAIN_DIR/" "$MINGW_WRAP_DIR"
+            --dst-prefix="$LEGACY_TOOLCHAIN_DIR/" "$CROSS_WRAP_DIR"
     $NDK_BUILDTOOLS_PATH/gen-toolchain-wrapper.sh --src-prefix=x86_64-pc-linux-gnu- \
-            --dst-prefix="$LEGACY_TOOLCHAIN_DIR/" "$MINGW_WRAP_DIR"
-    fail_panic "Could not create mingw wrapper toolchain in $MINGW_WRAP_DIR"
+            --dst-prefix="$LEGACY_TOOLCHAIN_DIR/" "$CROSS_WRAP_DIR"
+    fail_panic "Could not create $DEBIAN_NAME wrapper toolchain in $CROSS_WRAP_DIR"
 
-    export PATH=$MINGW_WRAP_DIR:$PATH
-    dump "Using mingw wrapper: $MINGW_WRAP_DIR/${BINPREFIX}gcc"
+    export PATH=$CROSS_WRAP_DIR:$PATH
+    dump "Using $DEBIAN_NAME wrapper: $CROSS_WRAP_DIR/${BINPREFIX}gcc"
 }
 
 handle_host ()
 {
-    # For now, we only support building 32-bit binaries anyway
     if [ "$TRY64" != "yes" ]; then
         force_32bit_binaries  # to modify HOST_TAG and others
         HOST_BITS=32
     fi
-    handle_mingw
+    handle_canadian_build
 }
 
 setup_ccache ()
 {
     # Support for ccache compilation
-    # We can't use this here when building Windows binaries on Linux with
+    # We can't use this here when building Windows/darwin binaries on Linux with
     # binutils 2.21, because defining CC/CXX in the environment makes the
     # configure script fail later
     #
-    if [ "$NDK_CCACHE" -a "$MINGW" != "yes" ]; then
+    if [ "$NDK_CCACHE" -a "$MINGW" != "yes" -a "$DARWIN" != "yes" ]; then
         NDK_CCACHE_CC=$CC
         NDK_CCACHE_CXX=$CXX
         # Unfortunately, we can just do CC="$NDK_CCACHE $CC" because some
@@ -707,19 +803,22 @@
 
 prepare_common_build ()
 {
-    if [ "$MINGW" = "yes" ]; then
+    if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ]; then
         if [ "$TRY64" = "yes" ]; then
-            log "Generating 64-bit Windows binaries"
             HOST_BITS=64
         else
-            log "Generating 32-bit Windows binaries"
             HOST_BITS=32
         fi
-        # Do *not* set CC and CXX when building the Windows binaries
+        if [ "$MINGW" = "yes" ]; then
+            log "Generating $HOST_BITS-bit Windows binaries"
+        else
+            log "Generating $HOST_BITS-bit Darwin binaries"
+        fi
+        # Do *not* set CC and CXX when building the Windows/Darwin binaries in canadian build.
         # Otherwise, the GCC configure/build script will mess that Canadian cross
         # build in weird ways. Instead we rely on the toolchain detected or generated
-        # previously in prepare_mingw_toolchain.
-        unset CC CXX STRIP
+        # previously in prepare_canadian_toolchain.
+        unset CC CXX
         return
     fi
 
@@ -728,9 +827,7 @@
     # binaries.
     #
     # We only do this if the CC variable is not defined to a given value
-    # and the --mingw or --try-64 options are not used.
-    #
-    if [ -z "$CC" -a "$MINGW" != "yes" ]; then
+    if [ -z "$CC" ]; then
         LEGACY_TOOLCHAIN_DIR=
         if [ "$HOST_OS" = "linux" ]; then
             LEGACY_TOOLCHAIN_DIR="$ANDROID_NDK_ROOT/../prebuilts/tools/gcc-sdk"
@@ -746,18 +843,12 @@
         fi
     fi
 
-    # Force generation of 32-bit binaries on 64-bit systems
     CC=${CC:-gcc}
     CXX=${CXX:-g++}
     STRIP=${STRIP:-strip}
     case $HOST_TAG in
         darwin-*)
-            if check_darwin_sdk /Developer/SDKs/MacOSX10.6.sdk 10.6; then
-                log "Generating Snow Leopard-compatible binaries!"
-            else
-                local version=`sw_vers -productVersion`
-                log "Generating $version-compatible binaries!"
-            fi
+            probe_darwin_sdk
             ;;
     esac
 
@@ -808,17 +899,18 @@
 {
     prepare_common_build
 
-    # Now deal with mingw
-    if [ "$MINGW" = "yes" ]; then
-        handle_mingw
+    # Now deal with mingw or darwin
+    if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ]; then
+        handle_canadian_build
         CC=$ABI_CONFIGURE_HOST-gcc
         CXX=$ABI_CONFIGURE_HOST-g++
+        CPP=$ABI_CONFIGURE_HOST-cpp
         LD=$ABI_CONFIGURE_HOST-ld
         AR=$ABI_CONFIGURE_HOST-ar
         AS=$ABI_CONFIGURE_HOST-as
         RANLIB=$ABI_CONFIGURE_HOST-ranlib
         STRIP=$ABI_CONFIGURE_HOST-strip
-        export CC CXX LD AR AS RANLIB STRIP
+        export CC CXX CPP LD AR AS RANLIB STRIP
     fi
 
     setup_ccache
@@ -856,13 +948,16 @@
     prepare_common_build
     HOST_GMP_ABI=$HOST_BITS
 
-    # Now handle the --mingw flag
-    if [ "$MINGW" = "yes" ] ; then
-        handle_mingw
-        # It turns out that we need to undefine this to be able to
-        # perform a canadian-cross build with mingw. Otherwise, the
-        # GMP configure scripts will not be called with the right options
-        HOST_GMP_ABI=
+    # Now handle the --mingw/--darwin flag
+    if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ] ; then
+        handle_canadian_build
+        STRIP=$ABI_CONFIGURE_HOST-strip
+        if [ "$MINGW" = "yes" ] ; then
+            # It turns out that we need to undefine this to be able to
+            # perform a canadian-cross build with mingw. Otherwise, the
+            # GMP configure scripts will not be called with the right options
+            HOST_GMP_ABI=
+        fi
     fi
 
     setup_ccache
@@ -956,7 +1051,7 @@
 }
 
 # Return the host "tag" used to identify prebuilt host binaries.
-# NOTE: Handles the case where '$MINGW = true'
+# NOTE: Handles the case where '$MINGW = true' or '$DARWIN = true'
 # For now, valid values are: linux-x86, darwin-x86 and windows
 get_prebuilt_host_tag ()
 {
@@ -968,6 +1063,9 @@
             RET=windows-x86_64
         fi
     fi
+    if [ "$DARWIN" = "yes" ]; then
+        RET=darwin-x86_64  # let the following handles 32-bit case
+    fi
     case $RET in
         linux-x86_64)
             if [ "$TRY64" = "no" ]; then
@@ -1144,7 +1242,7 @@
 
 # Return the relative install prefix for prebuilt host
 # executables (relative to the NDK top directory).
-# NOTE: This deals with MINGW==yes appropriately
+# NOTE: This deals with MINGW==yes or DARWIN==yes appropriately
 #
 # $1: optional, system name
 # Out: relative path to prebuilt install prefix
@@ -1156,7 +1254,7 @@
 
 # Return the relative path of an installed prebuilt host
 # executable
-# NOTE: This deals with MINGW==yes appropriately.
+# NOTE: This deals with MINGW==yes or DARWIN==yes appropriately.
 #
 # $1: executable name
 # $2: optional, host system name
diff --git a/build/tools/rebuild-all-prebuilt.sh b/build/tools/rebuild-all-prebuilt.sh
index 36a1019..2013c4f 100755
--- a/build/tools/rebuild-all-prebuilt.sh
+++ b/build/tools/rebuild-all-prebuilt.sh
@@ -33,6 +33,10 @@
 SYSTEMS=$HOST_TAG32
 if [ "$HOST_TAG32" = "linux-x86" ]; then
     SYSTEMS=$SYSTEMS",windows"
+    # If darwin toolchain exist, build darwin too
+    if [ -f "${DARWIN_TOOLCHAIN}-gcc" ]; then
+        SYSTEMS=$SYSTEMS",darwin-x86"
+    fi
 fi
 CUSTOM_SYSTEMS=
 register_option "--systems=<list>" do_SYSTEMS "Specify host systems"
diff --git a/sources/host-tools/make-3.81/configure b/sources/host-tools/make-3.81/configure
index a743fd1..ba92244 100755
--- a/sources/host-tools/make-3.81/configure
+++ b/sources/host-tools/make-3.81/configure
@@ -2318,7 +2318,7 @@
 ## -------------------- ##
 
 ac_ext=c
-ac_cpp='$CPP $CPPFLAGS'
+ac_cpp='$CPP $CFLAGS $CPPFLAGS'
 ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
 ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
 ac_compiler_gnu=$ac_cv_c_compiler_gnu
@@ -2791,7 +2791,7 @@
 
 # Checks for programs.
 ac_ext=c
-ac_cpp='$CPP $CPPFLAGS'
+ac_cpp='$CPP $CFLAGS $CPPFLAGS'
 ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
 ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
 ac_compiler_gnu=$ac_cv_c_compiler_gnu
@@ -3576,7 +3576,7 @@
 fi
 
 ac_ext=c
-ac_cpp='$CPP $CPPFLAGS'
+ac_cpp='$CPP $CFLAGS $CPPFLAGS'
 ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
 ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
 ac_compiler_gnu=$ac_cv_c_compiler_gnu
@@ -3849,7 +3849,7 @@
 fi
 
 ac_ext=c
-ac_cpp='$CPP $CPPFLAGS'
+ac_cpp='$CPP $CFLAGS $CPPFLAGS'
 ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
 ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
 ac_compiler_gnu=$ac_cv_c_compiler_gnu
@@ -3980,7 +3980,7 @@
 fi
 
 ac_ext=c
-ac_cpp='$CPP $CPPFLAGS'
+ac_cpp='$CPP $CFLAGS $CPPFLAGS'
 ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
 ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
 ac_compiler_gnu=$ac_cv_c_compiler_gnu
diff --git a/sources/host-tools/nawk-20071023/GNUmakefile b/sources/host-tools/nawk-20071023/GNUmakefile
index 631227f..cf1d634 100644
--- a/sources/host-tools/nawk-20071023/GNUmakefile
+++ b/sources/host-tools/nawk-20071023/GNUmakefile
@@ -5,10 +5,18 @@
 BUILD_DIR ?= /tmp/ndk-$(USER)/build-awk
 
 
-CC      := $(HOST_CC)
-CFLAGS  := -O2 -I$(BUILD_DIR) -I.
-LDFLAGS := -Wl,-s
-BISON   := bison
+CC       := $(HOST_CC)
+CFLAGS   := $(CFLAGS) -O2 -I$(BUILD_DIR) -I.
+LDFLAGS  := $(LDFLAGS) -Wl,-s
+BISON    := bison
+
+# NATIVE_CC is to build maketab which needs to run in order
+# to generate proctab.c from ytab.h.  Set it to host native
+# cc if host can run windows binary (MINGW=yes) or darwin binary
+# (DARWIN=yes)
+NATIVE_CC       ?= $(CC)
+NATIVE_CFLAGS   ?= $(CFLAGS)
+NATIVE_LDFLAGS  ?= $(LDFLAGS)
 
 MINGW := $(strip $(WIN32))
 TRY64 := $(strip $(TRY64))
@@ -37,8 +45,6 @@
 CC      := i586-pc-mingw32msvc-gcc
 endif
 EXE     := .exe
-else
-WINE    :=
 endif
 
 PROGRAM := $(BUILD_DIR)/ndk-awk$(EXE)
@@ -74,8 +80,8 @@
 
 MAKETAB := $(BUILD_DIR)/maketab$(EXE)
 $(MAKETAB): maketab.c $(BUILD_DIR)/ytab.h
-	@echo "Executable: $(notdir $@)"
-	$(hide)$(CC) $(CFLAGS) $(LDFLAGS) maketab.c -o $@
+	@echo "Native Executable: $(notdir $@)"
+	$(hide)$(NATIVE_CC) $(NATIVE_CFLAGS) $(NATIVE_LDFLAGS) maketab.c -o $@
 
 PROCTAB_C := $(BUILD_DIR)/proctab.c
 PROCTAB_O := $(PROCTAB_C:%.c=%.o)
@@ -84,7 +90,7 @@
 
 $(PROCTAB_C): $(MAKETAB) $(YTAB_H)
 	@echo "Gen: $(notdir $@)"
-	$(hide)$(WINE) $(MAKETAB) $(YTAB_H) > $@
+	$(hide)$(MAKETAB) $(YTAB_H) > $@
 
 
 $(PROGRAM): $(OBJECTS)
diff --git a/sources/host-tools/sed-4.2.1/configure b/sources/host-tools/sed-4.2.1/configure
index 378a934..6dd4972 100755
--- a/sources/host-tools/sed-4.2.1/configure
+++ b/sources/host-tools/sed-4.2.1/configure
Binary files differ