Reorder location of toolchains files.

build/toolchains/<name>/*                    --> toolchains/<name>/*
build/prebuilt/<system>/<name>/lib/gdbserver --> toolchains/<name>/prebuilt/gdbserver
build/prebuilt/<system>/<name>/              --> toolchains/<name>/prebuilt/<system>/

The main idea is to group all toolchain-related files under the same directory.

Change-Id: Idb41de49b5d81b6881ac4e0eea24506ff4a07f03
diff --git a/.gitignore b/.gitignore
index 37540a6..776f44a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,4 +6,4 @@
 build.xml
 samples/
 platforms/
-build/prebuilt/
+toolchains/*/prebuilt/
diff --git a/build/core/init.mk b/build/core/init.mk
index 627f9b8..8ee16f8 100644
--- a/build/core/init.mk
+++ b/build/core/init.mk
@@ -200,10 +200,6 @@
 # Include common definitions
 include $(BUILD_SYSTEM)/definitions.mk
 
-# Location where all prebuilt binaries for a given host architectures
-# will be stored.
-HOST_PREBUILT := $(NDK_ROOT)/build/prebuilt/$(HOST_TAG)
-
 # ====================================================================
 #
 # Read all toolchain-specific configuration files.
@@ -227,7 +223,7 @@
 NDK_ALL_TOOLCHAINS :=
 NDK_ALL_ABIS       :=
 
-TOOLCHAIN_CONFIGS := $(wildcard $(NDK_ROOT)/build/toolchains/*/config.mk)
+TOOLCHAIN_CONFIGS := $(wildcard $(NDK_ROOT)/toolchains/*/config.mk)
 $(foreach _config_mk,$(TOOLCHAIN_CONFIGS),\
   $(eval include $(BUILD_SYSTEM)/add-toolchain.mk)\
 )
diff --git a/build/core/ndk-common.sh b/build/core/ndk-common.sh
index d54feb0..15fa986 100644
--- a/build/core/ndk-common.sh
+++ b/build/core/ndk-common.sh
@@ -63,20 +63,31 @@
 VERBOSE=${VERBOSE-yes}
 VERBOSE2=${VERBOSE2-no}
 
+
+# If NDK_LOGFILE is defined in the environment, use this as the log file
 TMPLOG=
+if [ -n "$NDK_LOGFILE" ] ; then
+    mkdir -p `dirname "$NDK_LOGFILE"` && touch "$NDK_LOGFILE"
+    TMPLOG="$NDK_LOGFILE"
+fi
 
 # Setup a log file where all log() and log2() output will be sent
 #
 # $1: log file path  (optional)
 #
-setup_log_file ()
+setup_default_log_file ()
 {
-    if [ -n "$1" ] ; then
-        TMPLOG="$1"
-    else
-        TMPLOG=/tmp/ndk-log-$$.log
+    if [ -n "$NDK_LOGFILE" ] ; then
+        return
     fi
-    rm -f $TMPLOG && touch $TMPLOG
+    if [ -n "$1" ] ; then
+        NDK_LOGFILE="$1"
+    else
+        NDK_LOGFILE=/tmp/ndk-log-$$.txt
+    fi
+    export NDK_LOGFILE
+    TMPLOG="$NDK_LOGFILE"
+    rm -rf "$TMPLOG" && mkdir -p `dirname "$TMPLOG"` && touch "$TMPLOG"
     echo "To follow build in another terminal, please use: tail -F $TMPLOG"
 }
 
@@ -93,7 +104,7 @@
     if [ "$VERBOSE" = "yes" ] ; then
         echo "$@"
     else
-        if [ "$TMPLOG" ] ; then
+        if [ -n "$TMPLOG" ] ; then
             echo "$@" >> $TMPLOG
         fi
     fi
@@ -253,7 +264,7 @@
 
 # If BUILD_NUM_CPUS is not already defined in your environment,
 # define it as the double of HOST_NUM_CPUS. This is used to
-# run Make commends in parralles, as in 'make -j$BUILD_NUM_CPUS'
+# run Make commands in parralles, as in 'make -j$BUILD_NUM_CPUS'
 #
 if [ -z "$BUILD_NUM_CPUS" ] ; then
     BUILD_NUM_CPUS=`expr $HOST_NUM_CPUS \* 2`
@@ -514,7 +525,7 @@
     # Is this HTTP, HTTPS or FTP ?
     if pattern_match "^(http|https|ftp):.*" "$1"; then
         if [ -n "$CMD_WGET" ] ; then
-            run $CMD_WGET -O $2 $1 
+            run $CMD_WGET -O $2 $1
         elif [ -n "$CMD_CURL" ] ; then
             run $CMD_CURL -o $2 $1
         else
diff --git a/build/core/setup-toolchain.mk b/build/core/setup-toolchain.mk
index c7808b6..5ff6b38 100644
--- a/build/core/setup-toolchain.mk
+++ b/build/core/setup-toolchain.mk
@@ -73,20 +73,25 @@
 # the setup file.
 TOOLCHAIN_NAME   := $(TARGET_TOOLCHAIN)
 
+# Define the root path of the toolchain in the NDK tree.
+TOOLCHAIN_ROOT   := $(NDK_ROOT)/toolchains/$(TOOLCHAIN_NAME)
+
+# Define the root path where toolchain prebuilts are stored
+TOOLCHAIN_PREBUILT_ROOT := $(TOOLCHAIN_ROOT)/prebuilt/$(HOST_TAG)
+
 # Do the same for TOOLCHAIN_PREFIX. Note that we must chop the version
 # number from the toolchain name, e.g. arm-eabi-4.4.0 -> path/bin/arm-eabi-
 # to do that, we split at dashes, remove the last element, then merge the
 # result. Finally, add the complete path prefix.
 #
 TOOLCHAIN_PREFIX := $(call merge,-,$(call chop,$(call split,-,$(TOOLCHAIN_NAME))))-
-TOOLCHAIN_PREFIX := $(HOST_PREBUILT)/$(TOOLCHAIN_NAME)/bin/$(TOOLCHAIN_PREFIX)
+TOOLCHAIN_PREFIX := $(TOOLCHAIN_PREBUILT_ROOT)/bin/$(TOOLCHAIN_PREFIX)
 
 # now call the toolchain-specific setup script
 include $(NDK_TOOLCHAIN.$(TARGET_TOOLCHAIN).setup)
 
-# We expect the gdbserver binary for this toolchain to be located at the same
-# place than the target C compiler.
-TARGET_GDBSERVER := $(dir $(TARGET_CC))/gdbserver
+# We expect the gdbserver binary for this toolchain to be located at its root.
+TARGET_GDBSERVER := $(TOOLCHAIN_ROOT)/prebuilt/gdbserver
 
 # compute NDK_APP_DST_DIR as the destination directory for the generated files
 NDK_APP_DST_DIR := $(NDK_APP_PROJECT_PATH)/libs/$(TARGET_ARCH_ABI)
diff --git a/build/toolchains/archive/toolchain/sources.txt b/build/toolchains/archive/toolchain/sources.txt
deleted file mode 100644
index c59d51a..0000000
--- a/build/toolchains/archive/toolchain/sources.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-android-ndk-toolchain-20090323.tar.bz2
-d56abac4df36271ae0c961d21d0847db
-http://android.git.kernel.org/pub/android-ndk-toolchain-20090323.tar.bz2
diff --git a/build/tools/build-gcc.sh b/build/tools/build-gcc.sh
index 2767db9..847fea9 100755
--- a/build/tools/build-gcc.sh
+++ b/build/tools/build-gcc.sh
@@ -30,12 +30,13 @@
 the top-level NDK installation path and <toolchain> is the name of
 the toolchain to use (e.g. arm-eabi-4.4.0)."
 
-JOBS=$HOST_NUM_CPUS
+JOBS=$BUILD_NUM_CPUS
 RELEASE=`date +%Y%m%d`
 BUILD_OUT=`random_temp_directory`
 PLATFORM=android-3
 GDB_VERSION=6.6
 BINUTILS_VERSION=2.19
+LOG=
 
 OPTION_BUILD_OUT=
 OPTION_PLATFORM=
@@ -83,6 +84,8 @@
 
 extract_parameters $@
 
+setup_default_log_file
+
 set_parameters ()
 {
     SRC_DIR="$1"
@@ -134,10 +137,6 @@
 
 parse_toolchain_name
 
-if [ "$MINGW" = "yes" ] ; then
-    enable_linux_mingw
-fi
-
 fix_option PLATFORM "$OPTION_PLATFORM" "platform"
 fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory"
 fix_sysroot "$OPTION_SYSROOT"
@@ -156,7 +155,7 @@
     exit 1
 fi
 
-set_toolchain_install $NDK_DIR/build/prebuilt/$HOST_TAG/$TOOLCHAIN
+set_toolchain_ndk $NDK_DIR
 
 dump "Using C compiler: $CC"
 dump "Using C++ compiler: $CXX"
@@ -166,7 +165,7 @@
 
 # Copy the sysroot to the installation prefix. This prevents the generated
 # binaries from containing hard-coding host paths
-TOOLCHAIN_SYSROOT=$TOOLCHAIN_PATH/libc
+TOOLCHAIN_SYSROOT=$TOOLCHAIN_PATH/sysroot
 dump "Sysroot  : Copying: $SYSROOT --> $TOOLCHAIN_SYSROOT"
 mkdir -p $TOOLCHAIN_SYSROOT && (cd $SYSROOT && tar c *) | (cd $TOOLCHAIN_SYSROOT && tar x)
 if [ $? != 0 ] ; then
diff --git a/build/tools/build-gdbserver.sh b/build/tools/build-gdbserver.sh
index f5f9b0a..94f2094 100755
--- a/build/tools/build-gdbserver.sh
+++ b/build/tools/build-gdbserver.sh
@@ -43,8 +43,8 @@
 OPTION_SYSROOT=
 
 SYSROOT=
-if [ -d $TOOLCHAIN_PATH/libc ] ; then
-  SYSROOT=$TOOLCHAIN_PATH/libc
+if [ -d $TOOLCHAIN_PATH/sysroot ] ; then
+  SYSROOT=$TOOLCHAIN_PATH/sysroot
 fi
 
 register_option "--build-out=<path>" do_build_out "Set temporary build directory" "/tmp/random"
@@ -59,6 +59,8 @@
 
 extract_parameters $@
 
+setup_default_log_file
+
 set_parameters ()
 {
     SRC_DIR="$1"
@@ -164,9 +166,9 @@
 # not in $SYSROOT/usr/bin
 #
 dump "Install  : $TOOLCHAIN gdbserver."
-DEST=$TOOLCHAIN_PATH/bin
+DEST=`dirname $TOOLCHAIN_PATH`
 mkdir -p $DEST &&
-run $TOOLCHAIN_PREFIX-objcopy --strip-unneeded $BUILD_OUT/gdbserver $TOOLCHAIN_PATH/bin/gdbserver
+run $TOOLCHAIN_PREFIX-objcopy --strip-unneeded $BUILD_OUT/gdbserver $DEST/gdbserver
 if [ $? != 0 ] ; then
     dump "Could not install gdbserver. See $TMPLOG"
     exit 1
diff --git a/build/tools/build-platforms.sh b/build/tools/build-platforms.sh
index d864b5a..9b7fe37 100755
--- a/build/tools/build-platforms.sh
+++ b/build/tools/build-platforms.sh
@@ -53,6 +53,7 @@
 OPTION_PLATFORMS=
 OPTION_SRCDIR=
 OPTION_DSTDIR=
+OPTION_NO_SAMPLES=no
 
 VERBOSE=no
 VERBOSE2=no
@@ -81,6 +82,9 @@
   --abi=*)
     OPTION_ABI=$optarg
     ;;
+  --no-samples)
+    OPTION_NO_SAMPLES=yes
+    ;;
   *)
     echo "unknown option '$opt', use --help"
     exit 1
@@ -93,12 +97,13 @@
     echo ""
     echo "options:"
     echo ""
-    echo "  --help             print this message"
-    echo "  --verbose          enable verbose messages"
-    echo "  --src-dir=<path>   source directory for development platform files [$SRCDIR]"
-    echo "  --dst-dir=<path>   destination directory [$DSTDIR]"
-    echo "  --platform=<list>  space-separated list of API levels [$PLATFORMS]"
-    echo "  --abi=<list>       space-separated list of ABIs [$ABIS]"
+    echo "  --help             Print this message"
+    echo "  --verbose          Enable verbose messages"
+    echo "  --src-dir=<path>   Source directory for development platform files [$SRCDIR]"
+    echo "  --dst-dir=<path>   Destination directory [$DSTDIR]"
+    echo "  --platform=<list>  Space-separated list of API levels [$PLATFORMS]"
+    echo "  --abi=<list>       Space-separated list of ABIs [$ABIS]"
+    echo "  --no-samples       Ignore samples"
     echo ""
     exit 0
 fi
@@ -238,20 +243,22 @@
     PREV_PLATFORM_DST=$PLATFORM_DST
 done
 
-# Copy platform samples and generic samples into your destination
-#
-# $SRC/samples/ --> $DST/samples/
-# $SRC/android-$PLATFORM/samples/ --> $DST/samples
-#
-copy_directory  samples samples samples
+if [ "$OPTION_NO_SAMPLES" = no ] ; then
+    # Copy platform samples and generic samples into your destination
+    #
+    # $SRC/samples/ --> $DST/samples/
+    # $SRC/android-$PLATFORM/samples/ --> $DST/samples
+    #
+    copy_directory  samples samples samples
 
-for PLATFORM in $PLATFORMS; do
-    # $SRC/platform-$PLATFORM/samples --> $DST/samples
-    copy_directory platforms/android-$PLATFORM/samples samples samples
-done
+    for PLATFORM in $PLATFORMS; do
+        # $SRC/platform-$PLATFORM/samples --> $DST/samples
+        copy_directory platforms/android-$PLATFORM/samples samples samples
+    done
 
-# Cleanup generated files in samples
-rm -rf "$DSTDIR/samples/*/obj"
-rm -rf "$DSTDIR/samples/*/libs"
+    # Cleanup generated files in samples
+    rm -rf "$DSTDIR/samples/*/obj"
+    rm -rf "$DSTDIR/samples/*/libs"
+fi
 
 log "Done !"
diff --git a/build/tools/dev-cleanup.sh b/build/tools/dev-cleanup.sh
index a10064d..27901a4 100755
--- a/build/tools/dev-cleanup.sh
+++ b/build/tools/dev-cleanup.sh
@@ -32,11 +32,9 @@
     exit 1
 fi
 
-rm -rf $DIR/build/platforms
 rm -rf $DIR/platforms
+rm -rf $DIR/toolchains/*/prebuilt
 rm -rf $DIR/samples
-rm -rf $DIR/apps
-rm -rf $DIR/out
 
 DIR=`dirname $ANDROID_NDK_ROOT`/development/ndk
 if [ ! -d $DIR ] ; then
diff --git a/build/tools/prebuilt-common.sh b/build/tools/prebuilt-common.sh
index fba40d1..adcec1d 100644
--- a/build/tools/prebuilt-common.sh
+++ b/build/tools/prebuilt-common.sh
@@ -495,25 +495,43 @@
 
 }
 
-set_toolchain_install ()
+# Return the host/build specific path for prebuilt toolchain binaries
+# relative to $1.
+#
+# $1: target root NDK directory
+#
+get_toolchain_install ()
 {
-    TOOLCHAIN_PATH=$1
+    echo "$1/toolchains/$TOOLCHAIN/prebuilt/$HOST_TAG"
+}
+
+
+# Set the toolchain target NDK location.
+# this sets TOOLCHAIN_PATH and TOOLCHAIN_PREFIX
+# $1: target NDK path
+set_toolchain_ndk ()
+{
+    TOOLCHAIN_PATH=`get_toolchain_install $1`
     log "Using toolchain path: $TOOLCHAIN_PATH"
 
     TOOLCHAIN_PREFIX=$TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET
     log "Using toolchain prefix: $TOOLCHAIN_PREFIX"
 }
 
+# Check that a toolchain is properly installed at a target NDK location
+#
+# $1: target root NDK directory
+#
 check_toolchain_install ()
 {
-    TOOLCHAIN_PATH=$1/build/prebuilt/$HOST_TAG/$TOOLCHAIN
+    TOOLCHAIN_PATH=`get_toolchain_install $1`
     if [ ! -d "$TOOLCHAIN_PATH" ] ; then
         echo "ERROR: Toolchain '$TOOLCHAIN' not installed in '$NDK_DIR'!"
         echo "       Ensure that the toolchain has been installed there before."
         exit 1
     fi
 
-    set_toolchain_install $TOOLCHAIN_PATH
+    set_toolchain_ndk $1
 }
 
 
diff --git a/build/tools/rebuild-all-prebuilt.sh b/build/tools/rebuild-all-prebuilt.sh
index a49c941..cc86a12 100755
--- a/build/tools/rebuild-all-prebuilt.sh
+++ b/build/tools/rebuild-all-prebuilt.sh
@@ -65,21 +65,25 @@
 register_option "--git-http" do_git_http "Download sources with http."
 do_git_http() { OPTION_GIT_HTTP=yes; }
 
+register_mingw_option
+
 extract_parameters $@
 
-if [ "$OPTION_PACKAGE" = yes -a -z "$OPTION_NDK_DIR" ] ; then
-    NDK_DIR=/tmp/ndk-prebuilt-$$
+fix_option NDK_DIR "$OPTION_NDK_DIR" "target NDK directory"
+fix_option BUILD_DIR "$OPTION_BUILD_DIR" "build directory"
+fix_option GDB_VERSION "$OPTION_GDB_VERSION" "gdb version"
+
+if [ "$OPTION_PACKAGE" = "yes" -a -z "$OPTION_NDK_DIR" ] ; then
+    NDK_DIR=/tmp/ndk-toolchain/ndk-prebuilt-$$
 fi
 
+setup_default_log_file "$NDK_DIR/log.txt"
+
 if [ -n "$PARAMETERS" ]; then
     dump "ERROR: Too many parameters. See --help for proper usage."
     exit 1
 fi
 
-fix_option NDK_DIR "$OPTION_NDK_DIR" "target NDK directory"
-fix_option BUILD_DIR "$OPTION_BUILD_DIR" "build directory"
-fix_option GDB_VERSION "$OPTION_GDB_VERSION" "gdb version"
-
 mkdir -p $BUILD_DIR
 if [ $? != 0 ] ; then
     dump "ERROR: Could not create build directory: $BUILD_DIR"
@@ -105,6 +109,9 @@
 if [ $VERBOSE = yes ] ; then
     FLAGS="--verbose"
 fi
+if [ "$MINGW" = yes ] ; then
+    FLAGS="$FLAGS --mingw"
+fi
 
 if [ -z "$OPTION_TOOLCHAIN_SRC_DIR" ] ; then
     if [ -n "$OPTION_TOOLCHAIN_SRC_PKG" ] ; then
@@ -155,6 +162,32 @@
 
 fi # ! $TOOLCHAIN_SRC_DIR
 
+# Needed to set HOST_TAG to windows if --mingw is used.
+prepare_host_flags
+
+RELEASE=`date +%Y%m%d`
+PREBUILT_ROOT=/tmp/ndk-prebuilt/prebuilt-$RELEASE
+mkdir -p $PREBUILT_ROOT
+
+# Package a directory in a .tar.bz2 archive
+#
+# $1: textual description
+# $2: final package name (without .tar.bz2 suffix)
+# $3: relative root path from $NDK_DIR
+#
+package_it ()
+{
+    if [ "$OPTION_PACKAGE" = "yes" ] ; then
+        dump "Packaging $1 ($2.tar.bz2) ..."
+        PREBUILT_PACKAGE=$PREBUILT_ROOT/"$2".tar.bz2
+        (cd $NDK_DIR && tar cjf $PREBUILT_PACKAGE "$3")
+        if [ $? != 0 ] ; then
+            dump "ERROR: Could not package $1!"
+            exit 1
+        fi
+    fi
+}
+
 # Build the toolchain from sources
 build_toolchain ()
 {
@@ -164,16 +197,22 @@
         dump "ERROR: Could not build $1 toolchain!"
         exit 1
     fi
+    package_it "$1 toolchain" "$1-$HOST_TAG" "toolchains/$1/prebuilt/$HOST_TAG"
 }
 
 build_gdbserver ()
 {
+    if [ "$MINGW" = yes ] ; then
+        dump "Skipping gdbserver build (--mingw option being used)."
+        return
+    fi
     dump "Build $1 gdbserver..."
     $PROGDIR/build-gdbserver.sh $FLAGS --build-out=$BUILD_DIR/gdbserver-$1 $SRC_DIR/gdb/gdb-$GDB_VERSION/gdb/gdbserver $NDK_DIR $1
     if [ $? != 0 ] ; then
         dump "ERROR: Could not build $1 toolchain!"
         exit 1
     fi
+    package_it "$1 gdbserver" "$1-gdbserver" "toolchains/$1/prebuilt/gdbserver"
 }
 
 build_toolchain arm-eabi-4.4.0
@@ -196,16 +235,4 @@
 #    exit 1
 #fi
 
-if [ "$OPTION_PACKAGE" = yes ] ; then
-    RELEASE=`date +%Y%m%d`
-    dump "Packaging prebuilt binaries..."
-    PREBUILT_PACKAGE=/tmp/android-ndk-prebuilt-$RELEASE-$HOST_TAG.tar.bz2
-    cd $NDK_DIR && tar cjf $PREBUILT_PACKAGE *
-    if [ $? != 0 ] ; then
-        dump "ERROR: Could not package prebuilt binaries!"
-        exit 1
-    fi
-    dump "See: $PREBUILT_PACKAGE"
-fi
-
 dump "Done!"
diff --git a/docs/CHANGES.TXT b/docs/CHANGES.TXT
index 1866445..72ec567 100644
--- a/docs/CHANGES.TXT
+++ b/docs/CHANGES.TXT
@@ -110,6 +110,9 @@
 
 - Platforms files are now under $NDK/platforms instead of $NDK/build/platforms
 
+- Toolchain files are now under $NDK/toolchains instead of
+  $NDK/build/toolchains and $NDK/build/prebuilt.
+
 -------------------------------------------------------------------------------
 android-ndk-r4b
 
diff --git a/build/toolchains/arm-eabi-4.4.0/config.mk b/toolchains/arm-eabi-4.4.0/config.mk
similarity index 100%
rename from build/toolchains/arm-eabi-4.4.0/config.mk
rename to toolchains/arm-eabi-4.4.0/config.mk
diff --git a/build/toolchains/arm-eabi-4.4.0/setup.mk b/toolchains/arm-eabi-4.4.0/setup.mk
similarity index 100%
rename from build/toolchains/arm-eabi-4.4.0/setup.mk
rename to toolchains/arm-eabi-4.4.0/setup.mk
diff --git a/build/toolchains/arm-linux-androideabi-4.4.3/config.mk b/toolchains/arm-linux-androideabi-4.4.3/config.mk
similarity index 100%
rename from build/toolchains/arm-linux-androideabi-4.4.3/config.mk
rename to toolchains/arm-linux-androideabi-4.4.3/config.mk
diff --git a/build/toolchains/arm-linux-androideabi-4.4.3/setup.mk b/toolchains/arm-linux-androideabi-4.4.3/setup.mk
similarity index 100%
rename from build/toolchains/arm-linux-androideabi-4.4.3/setup.mk
rename to toolchains/arm-linux-androideabi-4.4.3/setup.mk
diff --git a/build/toolchains/x86-4.2.1/config.mk b/toolchains/x86-4.2.1/config.mk
similarity index 100%
rename from build/toolchains/x86-4.2.1/config.mk
rename to toolchains/x86-4.2.1/config.mk
diff --git a/build/toolchains/x86-4.2.1/setup.mk b/toolchains/x86-4.2.1/setup.mk
similarity index 97%
rename from build/toolchains/x86-4.2.1/setup.mk
rename to toolchains/x86-4.2.1/setup.mk
index 5be83fb..814a94f 100644
--- a/build/toolchains/x86-4.2.1/setup.mk
+++ b/toolchains/x86-4.2.1/setup.mk
@@ -23,7 +23,7 @@
 # revisions of the NDK.
 #
 
-TOOLCHAIN_PREFIX := $(HOST_PREBUILT)/$(TOOLCHAIN_NAME)/bin/i686-android-linux-gnu-
+TOOLCHAIN_PREFIX := $(TOOLCHAIN_PREBUILT_ROOT)/bin/i686-android-linux-gnu-
 
 TARGET_CFLAGS.common := \
     -msoft-float -fpic \