Take LLVM from the platform prebuilts.

We won't be able to build the Windows LLVM until we get one in the
platform, but that doesn't build right now anyway.

As a side effect, stop building compiler-rt. We get this for free as
part of the prebuilt LLVM.

When building with LLVM, use the platform LLVM.

Note that having ANDROID_BUILD_TOP set is now a requirement. This can
be set manually, by `lunch`, or will be handled by `checkbuild.sh`,
but must be set.

Change-Id: Ieceace0e7b2091421eabb363f427f935ddd9436a
diff --git a/README.md b/README.md
index c45282e..8b7ee0a 100644
--- a/README.md
+++ b/README.md
@@ -58,8 +58,6 @@
       user-specified `APP_ABI` and `APP_PLATFORM`.
 * `sources/cxx-stl/$STL/$ABI/` contains the headers and libraries for the various
   C++ STLs.
-* `sources/android/compiler-rt/libs/` contains compiler-rt libraries for use
-  with Clang.
 * `prebuilt/android-$ARCH/gdbserver/` contains gdbserver.
 
 ### Others
diff --git a/build/tools/build-analyzer.sh b/build/tools/build-analyzer.sh
index 05e9871..88a541d 100755
--- a/build/tools/build-analyzer.sh
+++ b/build/tools/build-analyzer.sh
@@ -21,14 +21,12 @@
 # include common function and variable definitions
 . `dirname $0`/prebuilt-common.sh
 
-PROGRAM_PARAMETERS="<src-dir> <ndk-dir> <toolchain>"
+PROGRAM_PARAMETERS="<ndk-dir>"
 
 PROGRAM_DESCRIPTION=\
 "Copy clang static code analyzer for the Android NDK.
 
-Where <src-dir> is the location of toolchain sources, <ndk-dir> is
-the top-level NDK installation path and <toolchain> is the name of
-the toolchain to use (e.g. llvm-3.1)."
+Where <ndk-dir> is the top-level NDK installation path."
 
 RELEASE=`date +%Y%m%d`
 
@@ -39,37 +37,27 @@
 
 set_parameters ()
 {
-    SRC_DIR="$1"
-    NDK_DIR="$2"
-    TOOLCHAIN="$3"
+    NDK_DIR="$1"
+    CLANG_DIR=$ANDROID_BUILD_TOP/external/clang
 
-    # Check source directory
-    #
-    if [ -z "$SRC_DIR" ] ; then
-        echo "ERROR: Missing source directory parameter. See --help for details."
-        exit 1
-    fi
-
-    SCAN_BUILD_SRC_DIR=$SRC_DIR/$TOOLCHAIN/clang/tools/scan-build
+    SCAN_BUILD_SRC_DIR=$CLANG_DIR/tools/scan-build
     if [ ! -d "$SCAN_BUILD_SRC_DIR" ] ; then
         echo "ERROR: Source directory does not contain scan-build: $SCAN_BUILD_SRC_DIR"
         exit 1
     fi
 
-    SCAN_VIEW_SRC_DIR=$SRC_DIR/$TOOLCHAIN/clang/tools/scan-view
+    SCAN_VIEW_SRC_DIR=$CLANG_DIR/tools/scan-view
     if [ ! -d "$SCAN_VIEW_SRC_DIR" ] ; then
         echo "ERROR: Source directory does not contain scan-view: $SCAN_VIEW_SRC_DIR"
         exit 1
     fi
 
-    LICENSE_FILE=$SRC_DIR/$TOOLCHAIN/clang/LICENSE.TXT
+    LICENSE_FILE=$CLANG_DIR/LICENSE.TXT
     if [ ! -f "$LICENSE_FILE" ] ; then
         echo "ERROR: Source directory does not contain clang license file: $LICENSE_FILE"
         exit 1
     fi
 
-    log "Using source directory: $SRC_DIR"
-
     # Check NDK installation directory
     #
     if [ -z "$NDK_DIR" ] ; then
@@ -83,13 +71,6 @@
     fi
 
     log "Using NDK directory: $NDK_DIR"
-
-    # Check toolchain name
-    #
-    if [ -z "$TOOLCHAIN" ] ; then
-        echo "ERROR: Missing toolchain name parameter. See --help for details."
-        exit 1
-    fi
 }
 
 set_parameters $PARAMETERS
diff --git a/build/tools/build-compiler-rt.sh b/build/tools/build-compiler-rt.sh
deleted file mode 100755
index 0af7451..0000000
--- a/build/tools/build-compiler-rt.sh
+++ /dev/null
@@ -1,300 +0,0 @@
-#!/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.
-
-# include common function and variable definitions
-. `dirname $0`/prebuilt-common.sh
-. `dirname $0`/builder-funcs.sh
-
-PROGRAM_PARAMETERS=""
-
-PROGRAM_DESCRIPTION=\
-"Rebuild compiler-rt for the Android NDK.
-
-This requires a temporary NDK installation containing
-toolchain binaries for all target architectures.
-
-By default, this will try with the current NDK directory, unless
-you use the --ndk-dir=<path> option.
-
-The output will be placed in appropriate sub-directories of
-<ndk>/$COMPILER_RT_SUBDIR, but you can override this with the --out-dir=<path>
-option.
-"
-
-PACKAGE_DIR=
-register_var_option "--package-dir=<path>" PACKAGE_DIR "Put prebuilt tarballs into <path>."
-
-NDK_DIR=
-register_var_option "--ndk-dir=<path>" NDK_DIR "Specify NDK root path for the build."
-
-SRC_DIR=
-register_var_option "--src-dir=<path>" SRC_DIR "Specify compiler-rt source dir."
-
-BUILD_DIR=
-OPTION_BUILD_DIR=
-register_var_option "--build-dir=<path>" OPTION_BUILD_DIR "Specify temporary build dir."
-
-OUT_DIR=
-register_var_option "--out-dir=<path>" OUT_DIR "Specify output directory directly."
-
-ABIS="$PREBUILT_ABIS"
-register_var_option "--abis=<list>" ABIS "Specify list of target ABIs."
-
-NO_MAKEFILE=
-register_var_option "--no-makefile" NO_MAKEFILE "Do not use makefile to speed-up build"
-
-GCC_VERSION=
-register_var_option "--gcc-version=<ver>" GCC_VERSION "Specify GCC version"
-
-LLVM_VERSION=
-register_var_option "--llvm-version=<ver>" LLVM_VERSION "Specify LLVM version"
-
-register_jobs_option
-
-extract_parameters "$@"
-
-ABIS=$(commas_to_spaces $ABIS)
-
-# Handle NDK_DIR
-if [ -z "$NDK_DIR" ] ; then
-    NDK_DIR=$ANDROID_NDK_ROOT
-    log "Auto-config: --ndk-dir=$NDK_DIR"
-else
-    if [ ! -d "$NDK_DIR" ] ; then
-        echo "ERROR: NDK directory does not exists: $NDK_DIR"
-        exit 1
-    fi
-fi
-
-if [ -z "$OPTION_BUILD_DIR" ]; then
-    BUILD_DIR=$NDK_TMPDIR/build-compiler-rt
-else
-    BUILD_DIR=$OPTION_BUILD_DIR
-fi
-rm -rf "$BUILD_DIR"
-mkdir -p "$BUILD_DIR"
-fail_panic "Could not create build directory: $BUILD_DIR"
-
-if [ -z "$SRC_DIR" -o ! -d "$SRC_DIR" ]; then
-    dump "Could not found compiler-rt source directory: $SRC_DIR"
-    dump "Use --src-dir=<dir> to specify source directory."
-    exit 1
-fi
-
-# Compiler flags we want to use
-COMPILER_RT_CFLAGS="-fPIC -O2 -DANDROID -D__ANDROID__ -ffunction-sections"
-COMPILER_RT_CFLAGS=$COMPILER_RT_CFLAGS" -I$SRC_DIR/include -I$SRC_DIR/lib"
-COMPILER_RT_LDFLAGS="-nostdlib"
-
-# List of sources to compile
-COMPILER_RT_GENERIC_SOURCES=$(cd $SRC_DIR && ls lib/builtins/*.c)
-
-# filter out the sources we don't need
-UNUSED_SOURCES="lib/builtins/apple_versioning.c lib/builtins/gcc_personality_v0.c"
-COMPILER_RT_GENERIC_SOURCES=$(filter_out "$UNUSED_SOURCES" "$COMPILER_RT_GENERIC_SOURCES")
-
-# ARM specific
-COMPILER_RT_ARM_SOURCES="
-lib/builtins/arm/aeabi_dcmp.S \
-lib/builtins/arm/aeabi_div0.c \
-lib/builtins/arm/aeabi_fcmp.S \
-lib/builtins/arm/aeabi_idivmod.S \
-lib/builtins/arm/aeabi_ldivmod.S \
-lib/builtins/arm/aeabi_memcmp.S \
-lib/builtins/arm/aeabi_memcpy.S \
-lib/builtins/arm/aeabi_memmove.S \
-lib/builtins/arm/aeabi_memset.S \
-lib/builtins/arm/aeabi_uidivmod.S \
-lib/builtins/arm/aeabi_uldivmod.S \
-lib/builtins/arm/comparesf2.S
-lib/builtins/arm/divmodsi4.S
-lib/builtins/arm/divsi3.S
-lib/builtins/arm/modsi3.S
-lib/builtins/arm/udivmodsi4.S
-lib/builtins/arm/udivsi3.S
-lib/builtins/arm/umodsi3.S"
-
-# X86 specific
-COMPILER_RT_X86_SOURCES="
-lib/builtins/i386/ashldi3.S \
-lib/builtins/i386/ashrdi3.S \
-lib/builtins/i386/divdi3.S \
-lib/builtins/i386/floatdidf.S \
-lib/builtins/i386/floatdisf.S \
-lib/builtins/i386/floatdixf.S \
-lib/builtins/i386/floatundidf.S \
-lib/builtins/i386/floatundisf.S \
-lib/builtins/i386/floatundixf.S \
-lib/builtins/i386/lshrdi3.S \
-lib/builtins/i386/moddi3.S \
-lib/builtins/i386/muldi3.S \
-lib/builtins/i386/udivdi3.S \
-lib/builtins/i386/umoddi3.S"
-
-# Mips specific
-COMPILER_RT_MIPS_SOURCES=
-
-# X86_64 specific
-COMPILER_RT_X86_64_SOURCES="
-lib/builtins/x86_64/floatdidf.c \
-lib/builtins/x86_64/floatdisf.c \
-lib/builtins/x86_64/floatdixf.c \
-lib/builtins/x86_64/floatundidf.S \
-lib/builtins/x86_64/floatundisf.S \
-lib/builtins/x86_64/floatundixf.S
-"
-
-# If the --no-makefile flag is not used, we're going to put all build
-# commands in a temporary Makefile that we will be able to invoke with
-# -j$NUM_JOBS to build stuff in parallel.
-#
-if [ -z "$NO_MAKEFILE" ]; then
-    MAKEFILE=$BUILD_DIR/Makefile
-else
-    MAKEFILE=
-fi
-
-# prepare_compiler_rt_source_for_abi
-# $1: ABI
-prepare_compiler_rt_source_for_abi ()
-{
-    local ABI=$1
-    local ARCH_SOURCES GENERIC_SOURCES FOUND
-
-    if [ $ABI = "armeabi" -o $ABI = "armeabi-v7a" -o $ABI = "armeabi-v7a-hard" ]; then
-        ARCH_SOURCES="$COMPILER_RT_ARM_SOURCES"
-    elif [ $ABI = "x86" ]; then
-        ARCH_SOURCES="$COMPILER_RT_X86_SOURCES"
-    elif [ $ABI = "mips" -o $ABI = "mips32r6" ]; then
-        ARCH_SOURCES="$COMPILER_RT_MIPS_SOURCES"
-    elif [ $ABI = "x86_64" ]; then
-        ARCH_SOURCES="$COMPILER_RT_X86_64_SOURCES"
-    fi
-
-    for SOURCE in $COMPILER_RT_GENERIC_SOURCES; do
-        FILENAME=`basename $SOURCE`
-        FILENAME=$"${FILENAME/\.c/}"
-        # if we have lib/$ABI/*.S, skip lib/*.c
-        FOUND=$(echo $ARCH_SOURCES | grep $FILENAME)
-        if [ -z "$FOUND" ]; then
-            GENERIC_SOURCES="$GENERIC_SOURCES $SOURCE"
-        fi
-    done
-
-    echo "$ARCH_SOURCES $GENERIC_SOURCES"
-}
-
-# build_compiler_rt_libs_for_abi
-# $1: ABI
-# $2: build directory
-# $3: build type: "static" or "shared"
-# $4: (optional) installation directory
-build_compiler_rt_libs_for_abi ()
-{
-    local ARCH BINPREFIX
-    local ABI=$1
-    local BUILDDIR="$2"
-    local TYPE="$3"
-    local DSTDIR="$4"
-    local GCCVER
-
-    mkdir -p "$BUILDDIR"
-
-    # If the output directory is not specified, use default location
-    if [ -z "$DSTDIR" ]; then
-        DSTDIR=$NDK_DIR/$COMPILER_RT_SUBDIR/libs/$ABI
-    fi
-
-    mkdir -p "$DSTDIR"
-
-    if [ -n "$GCC_VERSION" ]; then
-        GCCVER=$GCC_VERSION
-    else
-        ARCH=$(convert_abi_to_arch $ABI)
-        GCCVER=$(get_default_gcc_version_for_arch $ARCH)
-        if [ "$LLVM_VERSION" \> "3.4" ]; then
-            # Turn on integrated-as for clang >= 3.5 otherwise file like
-            # can't be compiled
-            COMPILER_RT_CFLAGS="$COMPILER_RT_CFLAGS -fintegrated-as"
-        fi
-    fi
-
-    if [ -z "$PLATFORM" ]; then
-        PLATFORM="android-$FIRST_API64_LEVEL"
-    fi
-
-    builder_begin_android $ABI "$BUILDDIR" "$GCCVER" "$LLVM_VERSION" "$MAKEFILE" "$PLATFORM"
-    builder_set_srcdir "$SRC_DIR"
-    builder_set_dstdir "$DSTDIR"
-
-    builder_cflags "$COMPILER_RT_CFLAGS"
-
-    if [ $ABI = "armeabi" -o $ABI = "armeabi-v7a" -o $ABI = "armeabi-v7a-hard" ]; then
-        builder_cflags "-D__ARM_EABI__"
-        if [ $ABI = "armeabi-v7a-hard" ]; then
-            builder_cflags "-mhard-float -D_NDK_MATH_NO_SOFTFP=1"
-        fi
-    fi
-
-    builder_ldflags "$COMPILER_RT_LDFLAGS"
-    if [ $ABI = "armeabi-v7a-hard" ]; then
-        builder_cflags "-Wl,--no-warn-mismatch -lm_hard"
-    fi
-
-    builder_sources $(prepare_compiler_rt_source_for_abi $ABI)
-
-    if [ "$TYPE" = "static" ]; then
-        log "Building $DSTDIR/libcompiler_rt_static.a"
-        builder_static_library libcompiler_rt_static
-    else
-        log "Building $DSTDIR/libcompiler_rt_shared.so"
-        builder_ldflags "-lc"
-        if [ $ABI != "armeabi-v7a-hard" ]; then
-            builder_ldflags "-lm"
-        fi
-        builder_nostdlib_shared_library libcompiler_rt_shared
-    fi
-    builder_end
-}
-
-for ABI in $ABIS; do
-    build_compiler_rt_libs_for_abi $ABI "$BUILD_DIR/$ABI/shared" "shared" "$OUT_DIR"
-    build_compiler_rt_libs_for_abi $ABI "$BUILD_DIR/$ABI/static" "static" "$OUT_DIR"
-done
-
-# If needed, package files into tarballs
-if [ -n "$PACKAGE_DIR" ] ; then
-    for ABI in $ABIS; do
-        FILES=""
-        for LIB in libcompiler_rt_static.a libcompiler_rt_shared.so; do
-            FILES="$FILES $COMPILER_RT_SUBDIR/libs/$ABI/$LIB"
-        done
-        PACKAGE="$PACKAGE_DIR/compiler-rt-libs-$ABI.tar.bz2"
-        log "Packaging: $PACKAGE"
-        pack_archive "$PACKAGE" "$NDK_DIR" "$FILES"
-        fail_panic "Could not package $ABI compiler-rt binaries!"
-        dump "Packaging: $PACKAGE"
-    done
-fi
-
-if [ -z "$OPTION_BUILD_DIR" ]; then
-    log "Cleaning up..."
-    rm -rf $BUILD_DIR
-else
-    log "Don't forget to cleanup: $BUILD_DIR"
-fi
-
-log "Done!"
diff --git a/build/tools/build-host-prebuilts.sh b/build/tools/build-host-prebuilts.sh
index f1f0bbf..760998a 100755
--- a/build/tools/build-host-prebuilts.sh
+++ b/build/tools/build-host-prebuilts.sh
@@ -338,17 +338,20 @@
         done
     done
 
-    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 $CHECK_FLAG -j$BUILD_NUM_CPUS
-        fail_panic "Could not build llvm for $SYSNAME"
-    done
+    echo "Packaging $SYSNAME LLVM"
+    PACKAGE_ARG=
+    if [ -n "$PACKAGE_DIR" ]; then
+        PACKAGE_ARG="--package-dir $PACKAGE_DIR"
+    fi
+    run $BUILDTOOLS/build-llvm.py --host $SYSTEM $PACKAGE_ARG
+    fail_panic "Could not build llvm for $SYSNAME"
 
-    # We're done for this system
+    # We're done for this system.
 done
 
-# Build tools common to all system
-run $BUILDTOOLS/build-analyzer.sh "$SRC_DIR" "$NDK_DIR" "llvm-$DEFAULT_LLVM_VERSION" --package-dir="$PACKAGE_DIR"
+# Build tools common to all systems.
+ANDROID_BUILD_TOP=$($BUILDTOOLS/../../realpath $BUILDTOOLS/../../..)
+run $BUILDTOOLS/build-analyzer.sh "$NDK_DIR" --package-dir="$PACKAGE_DIR"
 
 if [ "$PACKAGE_DIR" ]; then
     echo "Done, please look at $PACKAGE_DIR"
diff --git a/build/tools/build-llvm.py b/build/tools/build-llvm.py
new file mode 100755
index 0000000..fe426d5
--- /dev/null
+++ b/build/tools/build-llvm.py
@@ -0,0 +1,88 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2015 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.
+#
+"""Packages the platform's LLVM for the NDK."""
+import argparse
+import datetime
+import os
+import tarfile
+import sys
+
+
+def android_top():
+    top = os.getenv('ANDROID_BUILD_TOP', '')
+    if not top:
+        sys.exit('ANDROID_BUILD_TOP not set. Cannot continue.\n'
+                 'Please set ANDROID_BUILD_TOP to point to the root of an '
+                 'Android tree.')
+    return top
+
+
+def get_default_package_dir():
+    tmp_dir = '/tmp'
+    if 'TMPDIR' in os.environ:
+        tmp_dir = os.getenv('TMPDIR')
+    datestamp = datetime.date.today().isoformat().replace('-', '')
+    return os.path.join(tmp_dir, 'prebuilt-{}'.format(datestamp))
+
+
+def get_llvm_prebuilt_path(host, version):
+    rel_prebuilt_path = 'prebuilts/clang/{}/host/{}'.format(
+        host, version)
+    prebuilt_path = os.path.join(android_top(), rel_prebuilt_path)
+    if not os.path.isdir(prebuilt_path):
+        sys.exit('Could not find prebuilt LLVM at {}'.format(prebuilt_path))
+    return prebuilt_path
+
+
+class ArgParser(argparse.ArgumentParser):
+    def __init__(self):
+        super(ArgParser, self).__init__(
+            description="Package the platform's LLVM for the NDK.")
+
+        # TODO(srhines): Make a prebuilt LLVM for Windows.
+        self.add_argument(
+            '--host', choices=('darwin-x86', 'linux-x86'), required=True,
+            help='Host tag in the prebuilt path (e.g. linux-x86)')
+        self.add_argument(
+            '--package-dir', help='Directory to place the packaged LLVM.',
+            default=get_default_package_dir())
+
+
+def main():
+    LLVM_VERSION = '3.6'
+
+    args = ArgParser().parse_args()
+    host = args.host
+    package_dir = args.package_dir
+
+    prebuilt_path = get_llvm_prebuilt_path(host, LLVM_VERSION)
+
+    package_name = 'llvm-{}-{}.tar.bz2'.format(LLVM_VERSION, host)
+    package_path = os.path.join(package_dir, package_name)
+    with tarfile.TarFile(package_path, 'w') as tarball:
+        arcname = 'toolchains/llvm-{}/prebuilt/{}'.format(LLVM_VERSION, host)
+
+        def package_filter(tarinfo):
+            if os.path.basename(tarinfo.name) == '.git':
+                return None
+            if os.path.splitext(tarinfo.name)[1] == '.sh':
+                return None
+            return tarinfo
+        tarball.add(prebuilt_path, arcname, filter=package_filter)
+
+if __name__ == '__main__':
+    main()
diff --git a/build/tools/build-llvm.sh b/build/tools/build-llvm.sh
deleted file mode 100755
index 23effe2..0000000
--- a/build/tools/build-llvm.sh
+++ /dev/null
@@ -1,521 +0,0 @@
-#!/bin/sh
-#
-# Copyright (C) 2012 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 shell script is used to rebuild the llvm and clang binaries
-#  for the Android NDK.
-#
-
-# include common function and variable definitions
-. `dirname $0`/prebuilt-common.sh
-
-PROGRAM_PARAMETERS="<src-dir> <ndk-dir> <toolchain>"
-
-PROGRAM_DESCRIPTION=\
-"Rebuild the LLVM prebuilt binaries for the Android NDK.
-
-Where <src-dir> is the location of toolchain sources, <ndk-dir> is
-the top-level NDK installation path and <toolchain> is the name of
-the toolchain to use (e.g. llvm-3.6)."
-
-RELEASE=`date +%Y%m%d`
-BUILD_OUT=$TMPDIR/build/toolchain
-OPTION_BUILD_OUT=
-register_var_option "--build-out=<path>" OPTION_BUILD_OUT "Set temporary build directory"
-
-# Note: platform API level 9 or higher is needed for proper C++ support
-register_var_option "--platform=<name>"  PLATFORM "Specify platform name"
-
-GMP_VERSION=$DEFAULT_GMP_VERSION
-register_var_option "--gmp-version=<version>" GMP_VERSION "Specify gmp version"
-
-PACKAGE_DIR=
-register_var_option "--package-dir=<path>" PACKAGE_DIR "Create archive tarball in specific directory"
-
-POLLY=no
-do_polly_option () { POLLY=yes; }
-register_option "--with-polly" do_polly_option "Enable Polyhedral optimizations for LLVM"
-
-CHECK=no
-do_check_option () { CHECK=yes; }
-register_option "--check" do_check_option "Check LLVM"
-
-USE_PYTHON=no
-do_use_python_option () { USE_PYTHON=yes; }
-register_option "--use-python" do_use_python_option "Use python bc2native instead of integrated one"
-
-register_jobs_option
-register_canadian_option
-register_try64_option
-
-extract_parameters "$@"
-
-prepare_canadian_toolchain $TMPDIR/build
-
-fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory"
-setup_default_log_file $BUILD_OUT/config.log
-
-set_parameters ()
-{
-    SRC_DIR="$1"
-    NDK_DIR="$2"
-    TOOLCHAIN="$3"
-
-    # Check source directory
-    #
-    if [ -z "$SRC_DIR" ] ; then
-        echo "ERROR: Missing source directory parameter. See --help for details."
-        exit 1
-    fi
-
-    if [ ! -d "$SRC_DIR/$TOOLCHAIN/llvm" ] ; then
-        echo "ERROR: Source directory does not contain llvm sources: $SRC_DIR/$TOOLCHAIN/llvm"
-        exit 1
-    fi
-
-    if [ -e "$SRC_DIR/$TOOLCHAIN/llvm/tools/polly" -a ! -h "$SRC_DIR/$TOOLCHAIN/llvm/tools/polly" ] ; then
-        echo "ERROR: polly, if exist, needs to be a symbolic link: $SRC_DIR/$TOOLCHAIN/llvm/tools/polly"
-        exit 1
-    fi
-
-    GMP_SOURCE=$SRC_DIR/gmp/gmp-$GMP_VERSION.tar.bz2
-    if [ ! -f "$GMP_SOURCE" ] ; then
-        echo "ERROR: Source directory does not contain gmp: $GMP_SOURCE"
-        exit 1
-    fi
-    SRC_DIR=`cd $SRC_DIR; pwd`
-    log "Using source directory: $SRC_DIR"
-
-    # Check NDK installation directory
-    #
-    if [ -z "$NDK_DIR" ] ; then
-        echo "ERROR: Missing NDK directory parameter. See --help for details."
-        exit 1
-    fi
-
-    if [ ! -d "$NDK_DIR" ] ; then
-        mkdir -p $NDK_DIR
-        if [ $? != 0 ] ; then
-            echo "ERROR: Could not create target NDK installation path: $NDK_DIR"
-            exit 1
-        fi
-    fi
-    NDK_DIR=`cd $NDK_DIR; pwd`
-    log "Using NDK directory: $NDK_DIR"
-
-    # Check toolchain name
-    #
-    if [ -z "$TOOLCHAIN" ] ; then
-        echo "ERROR: Missing toolchain name parameter. See --help for details."
-        exit 1
-    fi
-}
-
-set_parameters $PARAMETERS
-
-if [ -z "$PLATFORM" ]; then
-   PLATFORM="android-"$(get_default_api_level_for_arch $ARCH)
-fi
-
-prepare_target_build
-
-if [ "$PACKAGE_DIR" ]; then
-    mkdir -p "$PACKAGE_DIR"
-    fail_panic "Could not create package directory: $PACKAGE_DIR"
-fi
-
-set_toolchain_ndk $NDK_DIR $TOOLCHAIN
-
-if [ "$MINGW" != "yes" -a "$DARWIN" != "yes" ] ; then
-    dump "Using C compiler: $CC"
-    dump "Using C++ compiler: $CXX"
-fi
-
-if [ "$MINGW" = "yes" -a "$TRY64" != "yes" ]; then
-    # Clang3.5+ needs gcc4.7+ to build, and some of
-    # cross toolchain "i586-*" we search for in find_mingw_toolchain()
-    # can no longer build.  One solution is to provide DEBIAN_NAME=mingw32
-    # BINPREFIX=i686-pc-mingw32msvc- MINGW_GCC=/path/to/i686-w64-mingw32,
-    # but ABI_CONFIGURE_HOST is still hard-coded to i586-pc-mingw32msvc.
-    # Fixup ABI_CONFIGURE_HOST in this case.
-    if [ "$ABI_CONFIGURE_HOST" = "i586-pc-mingw32msvc" ]; then
-        MINGW_GCC_BASENAME=`basename $MINGW_GCC`
-        if [ "$MINGW_GCC_BASENAME" = "${MINGW_GCC_BASENAME%%i585*}" ]; then
-            ABI_CONFIGURE_HOST=${MINGW_GCC_BASENAME%-gcc}
-	    STRIP=$ABI_CONFIGURE_HOST-strip
-        fi
-    fi
-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
-
-ARCH=$HOST_ARCH
-
-# Disable futimens@GLIBC_2.6 not available in system on server with very old libc.so
-CFLAGS_FOR_BUILD="-O2 -I$TOOLCHAIN_BUILD_PREFIX/include -DDISABLE_FUTIMENS"
-LDFLAGS_FOR_BUILD="-L$TOOLCHAIN_BUILD_PREFIX/lib"
-
-# Statically link stdc++ to eliminate dependency on outdated libctdc++.so in old 32-bit
-# linux system, and libgcc_s_sjlj-1.dll and libstdc++-6.dll on windows
-LLVM_VERSION="`echo $TOOLCHAIN | tr '-' '\n' | tail -n 1`"
-if [ "$MINGW" = "yes" -o "$HOST_TAG" = "linux-x86" -o "$LLVM_VERSION" \> "3.4" ]; then
-    LDFLAGS_FOR_BUILD=$LDFLAGS_FOR_BUILD" -static-libstdc++"
-    if [ "$CC" = "${CC%%clang*}" ]; then
-        LDFLAGS_FOR_BUILD=$LDFLAGS_FOR_BUILD" -static-libgcc"
-    fi
-fi
-
-# Static link to avoid dependencies on libwinpthread-1.dll in mingw
-if [ "$MINGW" = "yes" ]; then
-    LDFLAGS_FOR_BUILD=$LDFLAGS_FOR_BUILD" -static"
-fi
-
-# Starting from llvm-3.7 lib/Support/Signals.cpp needs set_abort_behavior() doesn't exist in
-# Windows until -lmsvcr90
-if [ "$MINGW" = "yes" -a "$LLVM_VERSION" \> "3.5" ]; then
-    XXX_MAKE_FLAGS="$MAKE_FLAGS LIBS=-lmsvcr90"
-fi
-
-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 ARCH
-
-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
-
-if [ "$POLLY" = "yes" -a ! -d "$SRC_DIR/$TOOLCHAIN/polly" ] ; then
-    dump "Disable polly because $SRC_DIR/$TOOLCHAIN/polly doesn't exist"
-    POLLY=no
-fi
-
-EXTRA_CONFIG_FLAGS=
-rm -rf $SRC_DIR/$TOOLCHAIN/llvm/tools/polly
-if [ "$POLLY" = "yes" ]; then
-    # crate symbolic link
-    ln -s ../../polly $SRC_DIR/$TOOLCHAIN/llvm/tools
-
-    # build polly dependencies
-    unpack_archive "$GMP_SOURCE" "$BUILD_OUT"
-    fail_panic "Couldn't unpack $SRC_DIR/gmp/gmp-$GMP_VERSION to $BUILD_OUT"
-
-    GMP_BUILD_OUT=$BUILD_OUT/gmp-$GMP_VERSION
-    cd $GMP_BUILD_OUT
-    fail_panic "Couldn't cd into gmp build path: $GMP_BUILD_OUT"
-
-    OLD_ABI="${ABI}"
-    export ABI=$HOST_GMP_ABI  # needed to build 32-bit on 64-bit host
-    run ./configure \
-        --prefix=$TOOLCHAIN_BUILD_PREFIX \
-        --host=$ABI_CONFIGURE_HOST \
-        --build=$ABI_CONFIGURE_BUILD \
-        --disable-shared \
-        --disable-nls \
-        --enable-cxx
-    fail_panic "Couldn't configure gmp"
-    run make -j$NUM_JOBS
-    fail_panic "Couldn't compile gmp"
-    run make install
-    fail_panic "Couldn't install gmp to $TOOLCHAIN_BUILD_PREFIX"
-    ABI="$OLD_ABI"
-
-    CLOOG_BUILD_OUT=$BUILD_OUT/cloog
-    mkdir -p $CLOOG_BUILD_OUT && cd $CLOOG_BUILD_OUT
-    fail_panic "Couldn't create cloog build path: $CLOOG_BUILD_OUT"
-
-    run $SRC_DIR/$TOOLCHAIN/llvm/tools/polly/utils/cloog_src/configure \
-        --prefix=$TOOLCHAIN_BUILD_PREFIX \
-        --host=$ABI_CONFIGURE_HOST \
-        --build=$ABI_CONFIGURE_BUILD \
-        --with-gmp-prefix=$TOOLCHAIN_BUILD_PREFIX \
-        --disable-shared \
-        --disable-nls
-    fail_panic "Couldn't configure cloog"
-    run make -j$NUM_JOBS
-    fail_panic "Couldn't compile cloog"
-    run make install
-    fail_panic "Couldn't install cloog to $TOOLCHAIN_BUILD_PREFIX"
-
-    EXTRA_CONFIG_FLAGS="--with-cloog=$TOOLCHAIN_BUILD_PREFIX --with-isl=$TOOLCHAIN_BUILD_PREFIX"
-
-    # Allow text relocs when linking LLVMPolly.dylib against statically linked libgmp.a
-    if [ "$HOST_TAG32" = "darwin-x86" -o "$DARWIN" = "yes" ]; then   # -a "$HOST_ARCH" = "x86"
-        LDFLAGS="$LDFLAGS -read_only_relocs suppress"
-        export LDFLAGS
-    fi
-fi # POLLY = yes
-
-# configure the toolchain
-dump "Configure: $TOOLCHAIN toolchain build"
-LLVM_BUILD_OUT=$BUILD_OUT/llvm
-mkdir -p $LLVM_BUILD_OUT && cd $LLVM_BUILD_OUT
-fail_panic "Couldn't cd into llvm build path: $LLVM_BUILD_OUT"
-
-# Only start using integrated bc2native source >= 3.3 by default
-LLVM_VERSION_MAJOR=`echo $LLVM_VERSION | tr '.' '\n' | head -n 1`
-LLVM_VERSION_MINOR=`echo $LLVM_VERSION | tr '.' '\n' | tail -n 1`
-if [ $LLVM_VERSION_MAJOR -lt 3 ]; then
-    USE_PYTHON=yes
-elif [ $LLVM_VERSION_MAJOR -eq 3 ] && [ $LLVM_VERSION_MINOR -lt 3 ]; then
-    USE_PYTHON=yes
-fi
-
-if [ "$USE_PYTHON" != "yes" ]; then
-    # Refresh intermediate source
-    rm -f $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/*.cpp
-    rm -f $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/*.c
-    rm -f $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/*.h
-    run cp -a $NDK_DIR/tests/abcc/jni/*.cpp $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native
-    run cp -a $NDK_DIR/tests/abcc/jni/Abcc.h $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native
-    run cp -a $NDK_DIR/tests/abcc/jni/host/*.cpp $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native
-    run cp -a $NDK_DIR/tests/abcc/jni/host/*.h $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native
-    run cp -a $NDK_DIR/tests/abcc/jni/llvm_${LLVM_VERSION_MAJOR}${LLVM_VERSION_MINOR}.h $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/llvm_version.h
-    run cp -a $NDK_DIR/tests/abcc/jni/mman-win32/mman.[ch] $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native
-    export LLVM_TOOLS_FILTER="PARALLEL_DIRS:=\$\$(PARALLEL_DIRS:%=% ndk-bc2native)"
-fi
-
-BINUTILS_VERSION=$(get_default_binutils_version_for_llvm $TOOLCHAIN)
-
-run $SRC_DIR/$TOOLCHAIN/llvm/configure \
-    --prefix=$TOOLCHAIN_BUILD_PREFIX \
-    --host=$ABI_CONFIGURE_HOST \
-    --build=$ABI_CONFIGURE_BUILD \
-    --with-bug-report-url=$DEFAULT_ISSUE_TRACKER_URL \
-    --enable-targets=arm,mips,x86,aarch64 \
-    --enable-optimized \
-    --with-binutils-include=$SRC_DIR/binutils/binutils-$BINUTILS_VERSION/include \
-    $EXTRA_CONFIG_FLAGS
-fail_panic "Couldn't configure llvm toolchain"
-
-# build llvm/clang
-dump "Building : llvm toolchain [this can take a long time]."
-cd $LLVM_BUILD_OUT
-run make -j$NUM_JOBS $MAKE_FLAGS
-fail_panic "Couldn't compile llvm toolchain"
-
-if [ "$CHECK" = "yes" -a "$MINGW" != "yes" -a "$DARWIN" != "yes" ] ; then
-    # run the regression test
-    dump "Running  : llvm toolchain regression test"
-    cd $LLVM_BUILD_OUT
-    run make check-all
-    fail_warning "Couldn't pass all llvm regression test"  # change to fail_panic later
-    if [ "$POLLY" = "yes" ]; then
-        dump "Running  : polly toolchain regression test"
-        cd $LLVM_BUILD_OUT
-        run make polly-test -C tools/polly/test
-        fail_warning "Couldn't pass all polly regression test"  # change to fail_panic later
-    fi
-fi
-
-# install the toolchain to its final location
-dump "Install  : llvm toolchain binaries"
-cd $LLVM_BUILD_OUT && run make install $MAKE_FLAGS
-fail_panic "Couldn't install llvm toolchain to $TOOLCHAIN_BUILD_PREFIX"
-
-# copy arm_neon_x86.h from GCC
-GCC_SRC_DIR=$SRC_DIR/gcc/gcc-$DEFAULT_GCC32_VERSION
-cp -a $GCC_SRC_DIR/gcc/config/i386/arm_neon.h $TOOLCHAIN_BUILD_PREFIX/lib/clang/$LLVM_VERSION/include/arm_neon_x86.h
-
-# Since r156448, llvm installs a separate llvm-config-host when cross-compiling. Use llvm-config-host if this
-# exists otherwise llvm-config.
-# Note, llvm-config-host should've really been called llvm-config-build and the following changes fix this by
-# doing this rename and also making a proper llvm-config-host;
-# https://android-review.googlesource.com/#/c/64261/
-# https://android-review.googlesource.com/#/c/64263/
-# .. with these fixes in place, Wine is not needed for Windows cross
-# To my mind, llvm-config-host is a misnomer and it should be llvm-config-build.
-LLVM_CONFIG=$TOOLCHAIN_BUILD_PREFIX/bin/llvm-config
-if [ -f $TOOLCHAIN_BUILD_PREFIX/bin/llvm-config-host ] ; then
-    LLVM_CONFIG=$TOOLCHAIN_BUILD_PREFIX/bin/llvm-config-host
-fi
-
-# remove redundant bits
-rm -rf $TOOLCHAIN_BUILD_PREFIX/docs
-rm -rf $TOOLCHAIN_BUILD_PREFIX/include
-rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/*.a
-rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/*.la
-rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/pkgconfig
-rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/lib[cp]*.so
-rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/lib[cp]*.dylib
-rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/B*.so
-rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/B*.dylib
-rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/LLVMH*.so
-rm -rf $TOOLCHAIN_BUILD_PREFIX/lib/LLVMH*.dylib
-rm -rf $TOOLCHAIN_BUILD_PREFIX/share
-
-UNUSED_LLVM_EXECUTABLES="
-bugpoint c-index-test clang-check clang-format clang-tblgen lli llvm-bcanalyzer
-llvm-config llvm-config-host llvm-cov llvm-diff llvm-dsymutil llvm-dwarfdump llvm-extract llvm-ld
-llvm-mc llvm-nm llvm-mcmarkup llvm-objdump llvm-prof llvm-ranlib llvm-readobj llvm-rtdyld
-llvm-size llvm-stress llvm-stub llvm-symbolizer llvm-tblgen llvm-vtabledump macho-dump cloog
-llvm-vtabledump lli-child-target not count FileCheck llvm-profdata obj2yaml yaml2obj verify-uselistorder"
-
-for i in $UNUSED_LLVM_EXECUTABLES; do
-    rm -f $TOOLCHAIN_BUILD_PREFIX/bin/$i
-    rm -f $TOOLCHAIN_BUILD_PREFIX/bin/$i.exe
-done
-
-test -z "$STRIP" && STRIP=strip
-find $TOOLCHAIN_BUILD_PREFIX/bin -maxdepth 1 -type f -exec $STRIP {} \;
-# Note that MacOSX strip generate the follow error on .dylib:
-# "symbols referenced by indirect symbol table entries that can't be stripped "
-find $TOOLCHAIN_BUILD_PREFIX/lib -maxdepth 1 -type f \( -name "*.dll" -o -name "*.so" \) -exec $STRIP {} \;
-
-# For now, le64-tools is just like le32 ones
-if [ -f "$TOOLCHAIN_BUILD_PREFIX/bin/ndk-link${HOST_EXE}" ]; then
-    run ln -s ndk-link${HOST_EXE} $TOOLCHAIN_BUILD_PREFIX/bin/le32-none-ndk-link${HOST_EXE}
-    run ln -s ndk-link${HOST_EXE} $TOOLCHAIN_BUILD_PREFIX/bin/le64-none-ndk-link${HOST_EXE}
-fi
-if [ -f "$TOOLCHAIN_BUILD_PREFIX/bin/ndk-strip${HOST_EXE}" ]; then
-    run ln -s ndk-strip${HOST_EXE} $TOOLCHAIN_BUILD_PREFIX/bin/le32-none-ndk-strip${HOST_EXE}
-    run ln -s ndk-strip${HOST_EXE} $TOOLCHAIN_BUILD_PREFIX/bin/le64-none-ndk-strip${HOST_EXE}
-fi
-if [ -f "$TOOLCHAIN_BUILD_PREFIX/bin/ndk-translate${HOST_EXE}" ]; then
-    run ln -s ndk-translate${HOST_EXE} $TOOLCHAIN_BUILD_PREFIX/bin/le32-none-ndk-translate${HOST_EXE}
-    run ln -s ndk-translate${HOST_EXE} $TOOLCHAIN_BUILD_PREFIX/bin/le64-none-ndk-translate${HOST_EXE}
-fi
-
-# install script
-if [ "$USE_PYTHON" != "yes" ]; then
-    # Remove those intermediate cpp
-    rm -f $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/*.cpp
-    rm -f $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/*.c
-    rm -f $SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/*.h
-else
-    cp -p "$SRC_DIR/$TOOLCHAIN/llvm/tools/ndk-bc2native/ndk-bc2native.py" "$TOOLCHAIN_BUILD_PREFIX/bin/"
-fi
-
-# copy to toolchain path
-run copy_directory "$TOOLCHAIN_BUILD_PREFIX" "$TOOLCHAIN_PATH"
-
-# create analyzer/++ scripts
-ABIS=$PREBUILT_ABIS
-# temp hack before 64-bit ABIs are part of PREBUILT_ABIS
-if [ "$ABIS" != "${ABIS%%64*}" ]; then
-    ABIS="$PREBUILT_ABIS arm64-v8a x86_64 mips64"
-fi
-ABIS=$ABIS
-for ABI in $ABIS; do
-    ANALYZER_PATH="$TOOLCHAIN_PATH/bin/$ABI"
-    ANALYZER="$ANALYZER_PATH/analyzer"
-    mkdir -p "$ANALYZER_PATH"
-    case "$ABI" in
-      armeabi)
-          LLVM_TARGET=armv5te-none-linux-androideabi
-          ;;
-      armeabi-v7a|armeabi-v7a-hard)
-          LLVM_TARGET=armv7-none-linux-androideabi
-          ;;
-      arm64-v8a)
-          LLVM_TARGET=aarch64-none-linux-android
-          ;;
-      x86)
-          LLVM_TARGET=i686-none-linux-android
-          ;;
-      x86_64)
-          LLVM_TARGET=x86_64-none-linux-android
-          ;;
-      mips|mips32r6)
-          LLVM_TARGET=mipsel-none-linux-android
-          ;;
-      mips64)
-          LLVM_TARGET=mips64el-none-linux-android
-          ;;
-      *)
-        dump "ERROR: Unsupported NDK ABI: $ABI"
-        exit 1
-    esac
-
-    cat > "${ANALYZER}" <<EOF
-if [ "\$1" != "-cc1" ]; then
-    \`dirname \$0\`/../clang -target $LLVM_TARGET "\$@"
-else
-    # target/triple already spelled out.
-    \`dirname \$0\`/../clang "\$@"
-fi
-EOF
-    cat > "${ANALYZER}++" <<EOF
-if [ "\$1" != "-cc1" ]; then
-    \`dirname \$0\`/../clang++ -target $LLVM_TARGET "\$@"
-else
-    # target/triple already spelled out.
-    \`dirname \$0\`/../clang++ "\$@"
-fi
-EOF
-    chmod 0755 "${ANALYZER}" "${ANALYZER}++"
-
-    if [ -n "$HOST_EXE" ] ; then
-        cat > "${ANALYZER}.cmd" <<EOF
-@echo off
-if "%1" == "-cc1" goto :L
-%~dp0\\..\\clang${HOST_EXE} -target $LLVM_TARGET %*
-if ERRORLEVEL 1 exit /b 1
-goto :done
-:L
-rem target/triple already spelled out.
-%~dp0\\..\\clang${HOST_EXE} %*
-if ERRORLEVEL 1 exit /b 1
-:done
-EOF
-        cat > "${ANALYZER}++.cmd" <<EOF
-@echo off
-if "%1" == "-cc1" goto :L
-%~dp0\\..\\clang++${HOST_EXE} -target $LLVM_TARGET %*
-if ERRORLEVEL 1 exit /b 1
-goto :done
-:L
-rem target/triple already spelled out.
-%~dp0\\..\\clang++${HOST_EXE} %*
-if ERRORLEVEL 1 exit /b 1
-:done
-EOF
-        chmod 0755 "${ANALYZER}.cmd" "${ANALYZER}++.cmd"
-    fi
-done
-
-# copy SOURCES file if present
-if [ -f "$SRC_DIR/SOURCES" ]; then
-    cp "$SRC_DIR/SOURCES" "$TOOLCHAIN_PATH/SOURCES"
-fi
-
-# check GLIBC/GLBICXX symbols
-if [ "$HOST_OS" = "linux" ]; then
-    SUBDIR=$(get_toolchain_install_subdir $TOOLCHAIN $HOST_TAG)
-    $ANDROID_NDK_ROOT/build/tools/check-glibc.sh $NDK_DIR/$SUBDIR
-fi
-
-if [ "$PACKAGE_DIR" ]; then
-    ARCHIVE="$TOOLCHAIN-$HOST_TAG.tar.bz2"
-    SUBDIR=$(get_toolchain_install_subdir $TOOLCHAIN $HOST_TAG)
-    dump "Packaging $ARCHIVE"
-    pack_archive "$PACKAGE_DIR/$ARCHIVE" "$NDK_DIR" "$SUBDIR"
-fi
-
-dump "Done."
-if [ -z "$OPTION_BUILD_OUT" ] ; then
-    rm -rf $BUILD_OUT
-fi
diff --git a/build/tools/build-target-prebuilts.sh b/build/tools/build-target-prebuilts.sh
index a07cc5f..88db794 100755
--- a/build/tools/build-target-prebuilts.sh
+++ b/build/tools/build-target-prebuilts.sh
@@ -148,14 +148,6 @@
 ABIS=$(convert_archs_to_abis $ARCHS)
 UNKNOWN_ABIS=$(convert_archs_to_abis $UNKNOWN_ARCH)
 
-if [ ! -z "$LLVM_VERSION" ]; then
-   dump "Building $ABIS compiler-rt binaries..."
-   run $BUILDTOOLS/build-compiler-rt.sh --abis="$ABIS" $FLAGS \
-       --src-dir="$SRC_DIR/llvm-$LLVM_VERSION/llvm/projects/compiler-rt" \
-       $BUILD_TOOLCHAIN
-   fail_panic "Could not build compiler-rt!"
-fi
-
 dump "Building $ABIS gabi++ binaries..."
 run $BUILDTOOLS/build-cxx-stl.sh --stl=gabi++ --abis="$ABIS" $FLAGS --with-debug-info $BUILD_TOOLCHAIN
 fail_panic "Could not build gabi++ with debug info!"
diff --git a/build/tools/builder-funcs.sh b/build/tools/builder-funcs.sh
index 0ef54a0..54339c7 100644
--- a/build/tools/builder-funcs.sh
+++ b/build/tools/builder-funcs.sh
@@ -135,8 +135,8 @@
 builder_set_binprefix_llvm ()
 {
     _BUILD_BINPREFIX=$1
-    _BUILD_CC=${1}clang
-    _BUILD_CXX=${1}clang++
+    _BUILD_CC=${1}/clang
+    _BUILD_CXX=${1}/clang++
     _BUILD_AR=${2}ar
 }
 
@@ -554,7 +554,7 @@
     if [ -n "$LLVM_VERSION" ]; then
         GCC_TOOLCHAIN=`dirname $BINPREFIX`
         GCC_TOOLCHAIN=`dirname $GCC_TOOLCHAIN`
-        BINPREFIX=$NDK_DIR/$(get_llvm_toolchain_binprefix $LLVM_VERSION $TAG)
+        BINPREFIX=$(get_llvm_toolchain_binprefix $LLVM_VERSION $TAG)
     fi
 
     if [ -z "$PLATFORM" ]; then
diff --git a/build/tools/dev-defaults.sh b/build/tools/dev-defaults.sh
index b2d5396..c32824a 100644
--- a/build/tools/dev-defaults.sh
+++ b/build/tools/dev-defaults.sh
@@ -36,9 +36,6 @@
 # Location of the gccunwind sources, relative to the NDK root directory
 GCCUNWIND_SUBDIR=sources/android/gccunwind
 
-# Location of the compiler-rt sources, relative to the NDK root directory
-COMPILER_RT_SUBDIR=sources/android/compiler-rt
-
 # Location of the support sources for libc++, relative to the NDK root directory
 SUPPORT_SUBDIR=sources/android/support
 
diff --git a/build/tools/ndk-common.sh b/build/tools/ndk-common.sh
index 492aa6c..2a32a8c 100644
--- a/build/tools/ndk-common.sh
+++ b/build/tools/ndk-common.sh
@@ -21,7 +21,14 @@
 PROGNAME=`basename $0`
 
 if [ -z "$TMPDIR" ]; then
-  export TMPDIR=/tmp/ndk-$USER
+    export TMPDIR=/tmp/ndk-$USER
+fi
+
+if [ -z "$ANDROID_BUILD_TOP" ]; then
+    >&2 echo 'ANDROID_BUILD_TOP not set. Cannot continue.'
+    >&2 echo 'Please set ANDROID_BUILD_TOP to point to the root of an Android' \
+             'tree.'
+    exit 1
 fi
 
 # Find the Android NDK root, assuming we are invoked from a script
diff --git a/build/tools/package-release.sh b/build/tools/package-release.sh
index 880267f..ff89f4d 100755
--- a/build/tools/package-release.sh
+++ b/build/tools/package-release.sh
@@ -398,7 +398,6 @@
             unpack_prebuilt gnu-libstdc++-libs-$VERSION-$ABI-g "$REFERENCE"
         done
         unpack_prebuilt libportable-libs-$ABI "$REFERENCE"
-        unpack_prebuilt compiler-rt-libs-$ABI "$REFERENCE"
         unpack_prebuilt libgccunwind-libs-$ABI "$REFERENCE"
     done
 fi
@@ -485,15 +484,6 @@
             echo "WARNING: Could not find libportable source tree!"
         fi
 
-        if [ -d "$DSTDIR/$COMPILER_RT_SUBDIR" ]; then
-            COMPILER_RT_ABIS=$PREBUILT_ABIS
-            for COMPILER_RT_ABI in $COMPILER_RT_ABIS; do
-                copy_prebuilt "$COMPILER_RT_SUBDIR/libs/$COMPILER_RT_ABI" "$COMPILER_RT_SUBDIR/libs"
-            done
-        else
-            echo "WARNING: Could not find compiler-rt source tree!"
-        fi
-
         if [ -d "$DSTDIR/$GCCUNWIND_SUBDIR" ]; then
             GCCUNWIND_ABIS=$PREBUILT_ABIS
             for GCCUNWIND_ABI in $GCCUNWIND_ABIS; do
diff --git a/build/tools/prebuilt-common.sh b/build/tools/prebuilt-common.sh
index 852e63a..b9d1733 100644
--- a/build/tools/prebuilt-common.sh
+++ b/build/tools/prebuilt-common.sh
@@ -1417,9 +1417,7 @@
 {
     local NAME DIR BINPREFIX
     local SYSTEM=${2:-$(get_prebuilt_host_tag)}
-    NAME=llvm-$1
-    DIR=$(get_toolchain_install . $NAME $SYSTEM)
-    BINPREFIX=${DIR#./}/bin/
+    BINPREFIX=$ANDROID_BUILD_TOP/prebuilts/clang/$SYSTEM/host/$1/bin
     echo "$BINPREFIX"
 }
 
diff --git a/checkbuild.sh b/checkbuild.sh
index 1ba9c14..b69e274 100755
--- a/checkbuild.sh
+++ b/checkbuild.sh
@@ -2,6 +2,9 @@
 set -e
 
 cd "$( dirname "${BASH_SOURCE[0]}" )"
+if [ -z "$ANDROID_BUILD_TOP" ]; then
+  export ANDROID_BUILD_TOP=$(./realpath ..)
+fi
 
 PACKAGE_DIR_ARG=
 if [ -n "$DIST_DIR" ]; then
diff --git a/sources/android/compiler-rt/Android.mk b/sources/android/compiler-rt/Android.mk
deleted file mode 100644
index 6929816..0000000
--- a/sources/android/compiler-rt/Android.mk
+++ /dev/null
@@ -1,12 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-
-include $(CLEAR_VARS)
-LOCAL_MODULE:= compiler_rt_shared
-LOCAL_SRC_FILES:= libs/$(TARGET_ARCH_ABI)/lib$(LOCAL_MODULE)$(TARGET_SONAME_EXTENSION)
-include $(PREBUILT_SHARED_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_MODULE:= compiler_rt_static
-LOCAL_SRC_FILES:= libs/$(TARGET_ARCH_ABI)/lib$(LOCAL_MODULE)$(TARGET_LIB_EXTENSION)
-include $(PREBUILT_STATIC_LIBRARY)
-