Integrate ld.mcld into host prebuilt

See comment in script deploy-host-mcld.sh for the reason why
separate script is needed

Change-Id: I4cf74aec9cda4bf014783f43cd057199c5598a81
diff --git a/build/tools/build-host-prebuilts.sh b/build/tools/build-host-prebuilts.sh
index 2d05a3e..e546ae0 100755
--- a/build/tools/build-host-prebuilts.sh
+++ b/build/tools/build-host-prebuilts.sh
@@ -242,7 +242,7 @@
     if [ "$TRY64" = "yes" ]; then
         case $SYSTEM in
             darwin-x86|linux-x86)
-                SYSNAME=${SYSTEM%%x86}x86-64
+                SYSNAME=${SYSTEM%%x86}x86_64
                 ;;
             windows)
                 SYSNAME=windows-x86_64
@@ -288,7 +288,7 @@
         for TOOLCHAIN_NAME in $TOOLCHAIN_NAMES; do
             echo "Building $SYSNAME toolchain for $ARCH architecture: $TOOLCHAIN_NAME"
             run $BUILDTOOLS/build-gcc.sh "$SRC_DIR" "$NDK_DIR" $TOOLCHAIN_NAME $TOOLCHAIN_FLAGS
-            fail_panic "Could not build $TOOLCHAIN_NAME-$SYSTEM!"
+            fail_panic "Could not build $TOOLCHAIN_NAME-$SYSNAME!"
         done
     done
 
@@ -300,9 +300,13 @@
     for LLVM_VERSION in $LLVM_VERSION_LIST; do
         echo "Building $SYSNAME clang/llvm-$LLVM_VERSION"
         run $BUILDTOOLS/build-llvm.sh "$SRC_DIR" "$NDK_DIR" "llvm-$LLVM_VERSION" $TOOLCHAIN_FLAGS $POLLY_FLAGS $CHECK_FLAG
-        fail_panic "Could not build llvm for $SYSTEM"
+        fail_panic "Could not build llvm for $SYSNAME"
     done
 
+    # Deploy ld.mcld
+    $PROGDIR/deploy-host-mcld.sh --package-dir=$PACKAGE_DIR --systems=$SYSNAME
+    fail_panic "Could not deploy ld.mcld for $SYSNAME"
+
     # We're done for this system
 done
 
diff --git a/build/tools/deploy-host-mcld.sh b/build/tools/deploy-host-mcld.sh
new file mode 100755
index 0000000..2ba8356
--- /dev/null
+++ b/build/tools/deploy-host-mcld.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+#
+# Copyright (C) 2013 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.
+#
+# Rebuild the host GCC toolchain binaries from sources.
+#
+# NOTE: this script does not rebuild gdb, see build-host-gdb.sh for this.
+#
+
+# include common function and variable definitions
+. `dirname $0`/prebuilt-common.sh
+
+PROGRAM_PARAMETERS=""
+PROGRAM_DESCRIPTION="\
+This program is used to deploy mclinker (ld.mcld) to GCC directories.
+Although ld.mcld depends on lots of LLVM modules and is built in
+build-llvm.sh to reduce long LLVM compilation time, it can be used as
+a drop-in replacement for ld.bfd and ld.gold in GCC.
+
+Running after completion of both build-llvm.sh and build-[host-]gcc.sh,
+this script copy toolchains/llvm-$DEFAULT_LLVM_VERSION/prebuilt/$SYSTEM/bin/ld.mcld[.exe]
+to be sibling of ld in all GCC directories with same HOST_OS and bitness,
+ie. {linux, darwin, windows} x {64, 32}
+
+If --systems isn't specified, this script discovers all ld.mcld[.exe] in
+toolchains/llvm-$DEFAULT_LLVM_VERSION
+
+Note that one copy of ld.mcld serves all GCC {4.7, 4.6, 4.4.3} x {arm, x86, mips}.
+GCC passes -m flag for ld.mcld to figure out the right target.
+"
+
+PACKAGE_DIR=
+register_var_option "--package-dir=<path>" PACKAGE_DIR "Create archive tarball in specific directory"
+
+SYSTEMS=
+register_var_option "--systems=<list>" SYSTEMS "List of host systems to deply ld.mcld for"
+
+extract_parameters "$@"
+
+if [ "$PACKAGE_DIR" ]; then
+    mkdir -p "$PACKAGE_DIR"
+    fail_panic "Could not create package directory: $PACKAGE_DIR"
+fi
+
+if [ -z "$SYSTEMS" ]; then
+    # find all ld.mcld
+    ALL_MCLDS=`find toolchains/llvm-$DEFAULT_LLVM_VERSION -name "ld.mcld*"`
+
+    for MCLD in $ALL_MCLDS; do
+        # compute SYSTEM of this ld.mcld
+        SYSTEM=${MCLD%%/bin/*}
+        SYSTEM=${SYSTEM##*prebuilt/}
+        SYSTEMS=$SYSTEMS" $SYSTEM"
+    done
+fi
+
+for SYSTEM in $SYSTEMS; do
+    HOST_EXE=
+    if [ "$SYSTEM" != "${SYSTEM%%windows*}" ] ; then
+        HOST_EXE=.exe
+    fi
+
+    MCLD=toolchains/llvm-$DEFAULT_LLVM_VERSION/prebuilt/$SYSTEM/bin/ld.mcld$HOST_EXE
+    test -f "$MCLD" || fail_panic "Could not find $MCLD"
+
+    # find all GNU ld with the same SYSTEM
+    ALL_LDS=`find . \( -name "*-ld" -o -name "ld" -o -name "*-ld.exe" -o -name "ld.exe" \) | grep $SYSTEM/`
+
+    ALL_LD_MCLDS=
+    for LD in $ALL_LDS; do
+        LD_NOEXE=${LD%%.exe}
+        LD_MCLD=${LD_NOEXE}.mcld$HOST_EXE
+        run rm -f "$LD_MCLD"
+        run cp -a "$MCLD" "$LD_MCLD"
+        ALL_LD_MCLDS=$ALL_LD_MCLDS" $LD_MCLD"
+    done
+
+    # package
+    if [ "$PACKAGE_DIR" ]; then
+        ARCHIVE="ld.mcld-$SYSTEM.tar.bz2"
+        #echo $ARCHIVE
+        echo  "Packaging $ARCHIVE"
+        pack_archive "$PACKAGE_DIR/$ARCHIVE" "$ANDROID_NDK_ROOT" $ALL_LD_MCLDS
+    fi
+done
+
+dump "Done."
diff --git a/build/tools/package-release.sh b/build/tools/package-release.sh
index 332d70e..0479ad8 100755
--- a/build/tools/package-release.sh
+++ b/build/tools/package-release.sh
@@ -482,6 +482,9 @@
             unpack_prebuilt llvm-$LLVM_VERSION-$SYSTEM "$DSTDIR" "$DSTDIR64"
         done
 
+        # Unpack ld.mcld
+        unpack_prebuilt ld.mcld-$SYSTEM "$DSTDIR" "$DSTDIR64"
+
         # Unpack prebuilt ndk-stack and other host tools
         unpack_prebuilt ndk-stack-$SYSTEM "$DSTDIR" "$DSTDIR64" "yes"
         unpack_prebuilt ndk-make-$SYSTEM "$DSTDIR" "$DSTDIR64"