Home | History | Annotate | Download | only in tools
      1 #!/bin/sh
      2 #
      3 # Copyright (C) 2010 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #      http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 #
     17 #  This shell script is used to rebuild the gcc and toolchain binaries
     18 #  for the Android NDK.
     19 #
     20 
     21 # include common function and variable definitions
     22 . `dirname $0`/prebuilt-common.sh
     23 
     24 PROGRAM_PARAMETERS="<src-dir> <ndk-dir> <toolchain>"
     25 
     26 PROGRAM_DESCRIPTION=\
     27 "Rebuild the gcc toolchain prebuilt binaries for the Android NDK.
     28 
     29 Where <src-dir> is the location of toolchain sources, <ndk-dir> is
     30 the top-level NDK installation path and <toolchain> is the name of
     31 the toolchain to use (e.g. arm-linux-androideabi-4.6)."
     32 
     33 RELEASE=`date +%Y%m%d`
     34 BUILD_OUT=/tmp/ndk-$USER/build/toolchain
     35 OPTION_BUILD_OUT=
     36 register_var_option "--build-out=<path>" OPTION_BUILD_OUT "Set temporary build directory"
     37 
     38 # Note: platform API level 9 or higher is needed for proper C++ support
     39 register_var_option "--platform=<name>"  PLATFORM "Specify platform name"
     40 
     41 OPTION_SYSROOT=
     42 register_var_option "--sysroot=<path>"   OPTION_SYSROOT   "Specify sysroot directory directly"
     43 
     44 GDB_VERSION=$DEFAULT_GDB_VERSION
     45 EXPLICIT_GDB_VERSION=
     46 register_option "--gdb-version=<version>"  do_gdb_version "Specify gdb version" "$GDB_VERSION"
     47 do_gdb_version () {
     48     GDB_VERSION=$1
     49     EXPLICIT_GDB_VERSION=true
     50 }
     51 
     52 BINUTILS_VERSION=$DEFAULT_BINUTILS_VERSION
     53 EXPLICIT_BINUTILS_VERSION=
     54 register_option "--binutils-version=<version>" do_binutils_version "Specify binutils version" "$BINUTILS_VERSION"
     55 do_binutils_version () {
     56     BINUTILS_VERSION=$1
     57     EXPLICIT_BINUTILS_VERSION=true
     58 }
     59 
     60 GMP_VERSION=$DEFAULT_GMP_VERSION
     61 register_var_option "--gmp-version=<version>" GMP_VERSION "Specify gmp version"
     62 
     63 MPFR_VERSION=$DEFAULT_MPFR_VERSION
     64 register_var_option "--mpfr-version=<version>" MPFR_VERSION "Specify mpfr version"
     65 
     66 MPC_VERSION=$DEFAULT_MPC_VERSION
     67 register_var_option "--mpc-version=<version>" MPC_VERSION "Specify mpc version"
     68 
     69 CLOOG_VERSION=$DEFAULT_CLOOG_VERSION
     70 register_var_option "--cloog-version=<version>" CLOOG_VERSION "Specify cloog version"
     71 
     72 ISL_VERSION=$DEFAULT_ISL_VERSION
     73 register_var_option "--isl-version=<version>" ISL_VERSION "Specify ISL version"
     74 
     75 PPL_VERSION=$DEFAULT_PPL_VERSION
     76 register_var_option "--ppl-version=<version>" PPL_VERSION "Specify ppl version"
     77 
     78 WITH_PYTHON=
     79 register_var_option "--with-python=<path/to/python-config.sh>" WITH_PYTHON "Specify python config script, or prebuilt"
     80 
     81 PACKAGE_DIR=
     82 register_var_option "--package-dir=<path>" PACKAGE_DIR "Create archive tarball in specific directory"
     83 
     84 register_jobs_option
     85 register_canadian_option
     86 register_try64_option
     87 
     88 extract_parameters "$@"
     89 
     90 prepare_canadian_toolchain /tmp/ndk-$USER/build
     91 
     92 fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory"
     93 setup_default_log_file $BUILD_OUT/config.log
     94 
     95 set_parameters ()
     96 {
     97     SRC_DIR="$1"
     98     NDK_DIR="$2"
     99     TOOLCHAIN="$3"
    100 
    101     # Check source directory
    102     #
    103     if [ -z "$SRC_DIR" ] ; then
    104         echo "ERROR: Missing source directory parameter. See --help for details."
    105         exit 1
    106     fi
    107 
    108     if [ ! -d "$SRC_DIR/gcc" ] ; then
    109         echo "ERROR: Source directory does not contain gcc sources: $SRC_DIR"
    110         exit 1
    111     fi
    112     SRC_DIR=`cd $SRC_DIR; pwd`
    113     log "Using source directory: $SRC_DIR"
    114 
    115     # Check NDK installation directory
    116     #
    117     if [ -z "$NDK_DIR" ] ; then
    118         echo "ERROR: Missing NDK directory parameter. See --help for details."
    119         exit 1
    120     fi
    121 
    122     if [ ! -d "$NDK_DIR" ] ; then
    123         mkdir -p $NDK_DIR
    124         if [ $? != 0 ] ; then
    125             echo "ERROR: Could not create target NDK installation path: $NDK_DIR"
    126             exit 1
    127         fi
    128     fi
    129     NDK_DIR=`cd $NDK_DIR; pwd`
    130     log "Using NDK directory: $NDK_DIR"
    131 
    132     # Check toolchain name
    133     #
    134     if [ -z "$TOOLCHAIN" ] ; then
    135         echo "ERROR: Missing toolchain name parameter. See --help for details."
    136         exit 1
    137     fi
    138 }
    139 
    140 set_parameters $PARAMETERS
    141 
    142 # Disable x86_64 build for toolchains older than 4.7
    143 case "$TOOLCHAIN" in
    144   x86_64-4.4.3|x86_64-4.6)
    145     echo "ERROR: x86_64 toolchain is enabled in 4.7+. Please try to build newer version."
    146     exit 1
    147     ;;
    148 esac
    149 
    150 prepare_target_build
    151 
    152 parse_toolchain_name $TOOLCHAIN
    153 
    154 if [ -z "$PLATFORM" ]; then
    155    PLATFORM="android-"$(get_default_api_level_for_arch $ARCH)
    156 fi
    157 
    158 fix_sysroot "$OPTION_SYSROOT"
    159 
    160 check_toolchain_src_dir "$SRC_DIR"
    161 
    162 if [ ! -d $SRC_DIR/gdb/gdb-$GDB_VERSION ] ; then
    163     echo "ERROR: Missing gdb sources: $SRC_DIR/gdb/gdb-$GDB_VERSION"
    164     echo "       Use --gdb-version=<version> to specify alternative."
    165     exit 1
    166 fi
    167 
    168 if [ -z "$EXPLICIT_BINUTILS_VERSION" ]; then
    169     BINUTILS_VERSION=$(get_default_binutils_version_for_gcc $TOOLCHAIN)
    170     dump "Auto-config: --binutils-version=$BINUTILS_VERSION"
    171 fi
    172 
    173 if [ ! -d $SRC_DIR/binutils/binutils-$BINUTILS_VERSION ] ; then
    174     echo "ERROR: Missing binutils sources: $SRC_DIR/binutils/binutils-$BINUTILS_VERSION"
    175     echo "       Use --binutils-version=<version> to specify alternative."
    176     exit 1
    177 fi
    178 
    179 if [ -z "$EXPLICIT_GDB_VERSION" ]; then
    180     GDB_VERSION=$(get_default_gdb_version_for_gcc $TOOLCHAIN)
    181     dump "Auto-config: --gdb-version=$GDB_VERSION"
    182 fi
    183 
    184 if [ ! -d $SRC_DIR/gdb/gdb-$GDB_VERSION ] ; then
    185     echo "ERROR: Missing gdb sources: $SRC_DIR/gdb/gdb-$GDB_VERSION"
    186     echo "       Use --gdb-version=<version> to specify alternative."
    187     exit 1
    188 fi
    189 
    190 if [ ! -z "$WITH_PYTHON" ] ; then
    191     if [ "$WITH_PYTHON" = "prebuilt" ] ; then
    192         WITH_PYTHON_SCRIPT="$ANDROID_NDK_ROOT/prebuilt/$HOST_TAG/bin/python-config.sh"
    193     fi
    194     if [ ! -f "$WITH_PYTHON_SCRIPT" ] ; then
    195         echo "ERROR: --with-python ($WITH_PYTHON_SCRIPT)"
    196         echo "       Does not exist!"
    197         exit 1
    198     else
    199         WITH_PYTHON="--with-python=$WITH_PYTHON_SCRIPT"
    200     fi
    201 fi
    202 
    203 fix_option MPFR_VERSION "$OPTION_MPFR_VERSION" "mpfr version"
    204 if [ ! -f $SRC_DIR/mpfr/mpfr-$MPFR_VERSION.tar.bz2 ] ; then
    205     echo "ERROR: Missing mpfr sources: $SRC_DIR/mpfr/mpfr-$MPFR_VERSION.tar.bz2"
    206     echo "       Use --mpfr-version=<version> to specify alternative."
    207     exit 1
    208 fi
    209 
    210 if [ "$PACKAGE_DIR" ]; then
    211     mkdir -p "$PACKAGE_DIR"
    212     fail_panic "Could not create package directory: $PACKAGE_DIR"
    213 fi
    214 
    215 set_toolchain_ndk $NDK_DIR $TOOLCHAIN
    216 
    217 if [ "$MINGW" != "yes" -a "$DARWIN" != "yes" ] ; then
    218     dump "Using C compiler: $CC"
    219     dump "Using C++ compiler: $CXX"
    220 fi
    221 
    222 rm -rf $BUILD_OUT
    223 mkdir -p $BUILD_OUT
    224 
    225 # Location where the toolchain license files are
    226 TOOLCHAIN_LICENSES=$ANDROID_NDK_ROOT/build/tools/toolchain-licenses
    227 
    228 # Without option "--sysroot" (and its variations), GCC will attempt to
    229 # search path specified by "--with-sysroot" at build time for headers/libs.
    230 # Path at --with-sysroot contains minimal headers and libs to boostrap
    231 # toolchain build, and it's not needed afterward (NOTE: NDK provides
    232 # sysroot at specified API level,and Android build explicit lists header/lib
    233 # dependencies.
    234 #
    235 # It's better to point --with-sysroot to local directory otherwise the
    236 # path may be found at compile-time and bad things can happen: eg.
    237 #  1) The path exists and contain incorrect headers/libs
    238 #  2) The path exists at remote server and blocks GCC for seconds
    239 #  3) The path exists but not accessible, which crashes GCC!
    240 #
    241 # For canadian build --with-sysroot has to be sub-directory of --prefix.
    242 # Put TOOLCHAIN_BUILD_PREFIX to BUILD_OUT which is in /tmp by default,
    243 # and TOOLCHAIN_BUILD_SYSROOT underneath.
    244 
    245 TOOLCHAIN_BUILD_PREFIX=$BUILD_OUT/prefix
    246 TOOLCHAIN_BUILD_SYSROOT=$TOOLCHAIN_BUILD_PREFIX/sysroot
    247 dump "Sysroot  : Copying: $SYSROOT --> $TOOLCHAIN_BUILD_SYSROOT"
    248 mkdir -p $TOOLCHAIN_BUILD_SYSROOT && (cd $SYSROOT && tar chf - *) | (cd $TOOLCHAIN_BUILD_SYSROOT && tar xf -)
    249 if [ $? != 0 ] ; then
    250     echo "Error while copying sysroot files. See $TMPLOG"
    251     exit 1
    252 fi
    253 
    254 # configure the toolchain
    255 #
    256 dump "Configure: $TOOLCHAIN toolchain build"
    257 # Old versions of the toolchain source packages placed the
    258 # configure script at the top-level. Newer ones place it under
    259 # the build directory though. Probe the file system to check
    260 # this.
    261 BUILD_SRCDIR=$SRC_DIR/build
    262 if [ ! -d $BUILD_SRCDIR ] ; then
    263     BUILD_SRCDIR=$SRC_DIR
    264 fi
    265 OLD_ABI="${ABI}"
    266 export CC CXX
    267 export CFLAGS_FOR_TARGET="$ABI_CFLAGS_FOR_TARGET"
    268 export CXXFLAGS_FOR_TARGET="$ABI_CXXFLAGS_FOR_TARGET"
    269 # Needed to build a 32-bit gmp on 64-bit systems
    270 export ABI=$HOST_GMP_ABI
    271 
    272 # Note that the following flags only apply for "build" in canadian
    273 # -Wno-error is needed because our gdb-6.6 sources use -Werror by default
    274 # and fail to build with recent GCC versions.
    275 CFLAGS_FOR_BUILD="-O2 -s -Wno-error"
    276 LDFLAGS_FOR_BUILD=
    277 
    278 if [ "$MINGW" = "yes" ] ; then
    279     CFLAGS_FOR_BUILD=$CFLAGS_FOR_BUILD" -D__USE_MINGW_ANSI_STDIO=1"
    280 fi
    281 
    282 CFLAGS="$CFLAGS_FOR_BUILD $HOST_CFLAGS"
    283 LDFLAGS="$LDFLAGS_FOR_BUILD $HOST_LDFLAGS"
    284 
    285 export CFLAGS LDFLAGS CFLAGS_FOR_BUILD LDFLAGS_FOR_BUILD
    286 
    287 # This extra flag is used to slightly speed up the build
    288 EXTRA_CONFIG_FLAGS="--disable-bootstrap"
    289 
    290 # This is to disable GCC 4.6 specific features that don't compile well
    291 # the flags are ignored for older GCC versions.
    292 EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-libquadmath"
    293 if [ "$DARWIN" = "yes" ]; then
    294     # Disable plugin because in canadian cross build, plugin gengtype
    295     # will be incorrectly linked with build's library and fails.
    296     # ToDo
    297     EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-plugin"
    298 else
    299     # Plugins are not supported well before 4.7. On 4.7 it's required to have
    300     # -flto working. Flag --enable-plugins (note 's') is actually for binutils,
    301     # this is compiler requirement to have binutils configured this way. Flag
    302     # --disable-plugin is for gcc.
    303     case "$GCC_VERSION" in
    304         4.4.3)
    305             EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-plugin"
    306             ;;
    307         *)
    308             EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-plugins"
    309             ;;
    310     esac
    311 fi
    312 
    313 # Enable OpenMP
    314 case "$TOOLCHAIN" in
    315     *) EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-libgomp" ;;
    316 esac
    317 
    318 # Disable libcilkrts which needs C++ for now, because libstdlibc++ in NDK is built separately...
    319 case "$TOOLCHAIN" in
    320     x86*-4.9) EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-libcilkrts"
    321 esac
    322 
    323 # Disable libsanitizer (which depends on libstdc++ built separately) for now
    324 EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-libsanitizer"
    325 
    326 # Enable Gold as default
    327 case "$TOOLCHAIN" in
    328     # Note that only ARM and X86 >= GCC 4.6 are supported
    329     mips*)
    330     ;;
    331     *-4.4.3)
    332     ;;
    333     *)
    334         EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-gold=default"
    335     ;;
    336 esac
    337 
    338 # Enable Graphite
    339 case "$TOOLCHAIN" in
    340     *-4.4.3) ;;
    341     *-4.6|*-4.7)
    342         EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-graphite=yes --with-cloog-version=$CLOOG_VERSION --with-ppl-version=$PPL_VERSION"
    343     ;;
    344     *)
    345         EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-graphite=yes --with-cloog-version=$CLOOG_VERSION --with-isl-version=$ISL_VERSION"
    346     ;;
    347 esac
    348 
    349 # Enable linker option -eh-frame-hdr also for static executable
    350 EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-eh-frame-hdr-for-static"
    351 
    352 MAY_FAIL_DUE_TO_RACE_CONDITION=
    353 if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ]; then
    354    MAY_FAIL_DUE_TO_RACE_CONDITION=yes
    355 fi
    356 
    357 # hack to use different set of sources
    358 CONFIGURE_GCC_VERSION=$GCC_VERSION
    359 case "$TOOLCHAIN" in
    360   *4.9l)
    361     CONFIGURE_GCC_VERSION=4.9l
    362     ;;
    363   *4.8l)
    364     CONFIGURE_GCC_VERSION=4.8l
    365     ;;
    366 esac
    367 
    368 cd $BUILD_OUT && run \
    369 $BUILD_SRCDIR/configure --target=$ABI_CONFIGURE_TARGET \
    370                         --enable-initfini-array \
    371                         --host=$ABI_CONFIGURE_HOST \
    372                         --build=$ABI_CONFIGURE_BUILD \
    373                         --disable-nls \
    374                         --prefix=$TOOLCHAIN_BUILD_PREFIX \
    375                         --with-sysroot=$TOOLCHAIN_BUILD_SYSROOT \
    376                         --with-binutils-version=$BINUTILS_VERSION \
    377                         --with-mpfr-version=$MPFR_VERSION \
    378                         --with-mpc-version=$MPC_VERSION \
    379                         --with-gmp-version=$GMP_VERSION \
    380                         --with-gcc-version=$CONFIGURE_GCC_VERSION \
    381                         --with-gdb-version=$GDB_VERSION \
    382                         $WITH_PYTHON \
    383                         --with-gxx-include-dir=$TOOLCHAIN_BUILD_PREFIX/include/c++/$GCC_VERSION \
    384                         --with-bugurl=$DEFAULT_ISSUE_TRACKER_URL \
    385                         $EXTRA_CONFIG_FLAGS \
    386                         $ABI_CONFIGURE_EXTRA_FLAGS
    387 if [ $? != 0 ] ; then
    388     dump "Error while trying to configure toolchain build. See $TMPLOG"
    389     exit 1
    390 fi
    391 
    392 ABI="$OLD_ABI"
    393 # build the toolchain
    394 dump "Building : $TOOLCHAIN toolchain [this can take a long time]."
    395 cd $BUILD_OUT
    396 export CC CXX
    397 export ABI=$HOST_GMP_ABI
    398 export NUM_JOBS
    399 
    400 while [ -n "1" ]; do
    401     run make -j$NUM_JOBS
    402     if [ $? = 0 ] ; then
    403         break
    404     else
    405         if [ "$MAY_FAIL_DUE_TO_RACE_CONDITION" = "yes" ] ; then
    406             # Unfortunately, there is a bug in the GCC build scripts that prevent
    407             # parallel mingw/darwin canadian cross builds to work properly on some
    408             # multi-core machines (but not all, sounds like a race condition). Detect
    409             # this and restart in less parallelism, until -j1 also fail
    410             NUM_JOBS=$((NUM_JOBS/2))
    411             export NUM_JOBS
    412             if [ $NUM_JOBS -lt 1 ] ; then
    413                 echo "Error while building mingw/darwin toolchain. See $TMPLOG"
    414                 exit 1
    415             fi
    416             dump "Parallel canadian build failed - continuing in less parallelism -j$NUM_JOBS"
    417         else
    418             echo "Error while building toolchain. See $TMPLOG"
    419             exit 1
    420         fi
    421     fi
    422 done
    423 
    424 ABI="$OLD_ABI"
    425 
    426 # install the toolchain to its final location.
    427 dump "Install  : $TOOLCHAIN toolchain binaries."
    428 cd $BUILD_OUT && run make install
    429 if [ $? != 0 ] ; then
    430     # try "-j1", eg.  for aarch64-linux-android-4.8 with libatomic may fail to install due to race condition (missing prefix/lib/../lib64/./libiberty.an)
    431     NUM_JOBS=1
    432     export NUM_JOBS
    433     run make install -j$NUM_JOBS
    434     if [ $? != 0 ] ; then
    435         echo "Error while installing toolchain. See $TMPLOG"
    436         exit 1
    437     fi
    438 fi
    439 
    440 unwind_library_for_abi ()
    441 {
    442     local ABI="$1"
    443     local BASE_DIR OBJS UNWIND_OBJS
    444 
    445     case $ABI in
    446     armeabi)
    447     BASE_DIR="$BUILD_OUT/gcc-$CONFIGURE_GCC_VERSION/$ABI_CONFIGURE_TARGET/libgcc/"
    448     OBJS="unwind-arm.o \
    449           libunwind.o \
    450           pr-support.o \
    451           unwind-c.o"
    452     ;;
    453     armeabi-v7a)
    454     BASE_DIR="$BUILD_OUT/gcc-$CONFIGURE_GCC_VERSION/$ABI_CONFIGURE_TARGET/armv7-a/libgcc/"
    455     OBJS="unwind-arm.o \
    456           libunwind.o \
    457           pr-support.o \
    458           unwind-c.o"
    459     ;;
    460     armeabi-v7a-hard)
    461     BASE_DIR="$BUILD_OUT/gcc-$CONFIGURE_GCC_VERSION/$ABI_CONFIGURE_TARGET/armv7-a/hard/libgcc/"
    462     OBJS="unwind-arm.o \
    463           libunwind.o \
    464           pr-support.o \
    465           unwind-c.o"
    466     ;;
    467     x86|mips)
    468     BASE_DIR="$BUILD_OUT/gcc-$CONFIGURE_GCC_VERSION/$ABI_CONFIGURE_TARGET/libgcc/"
    469     if [ "$GCC_VERSION" = "4.6" -o "$GCC_VERSION" = "4.4.3" ]; then
    470        OBJS="unwind-c.o \
    471           unwind-dw2-fde-glibc.o \
    472           unwind-dw2.o"
    473     else
    474        OBJS="unwind-c.o \
    475           unwind-dw2-fde-dip.o \
    476           unwind-dw2.o"
    477     fi
    478     ;;
    479     arm64-v8a|x86_64|mips64)
    480     BASE_DIR="$BUILD_OUT/gcc-$CONFIGURE_GCC_VERSION/$ABI_CONFIGURE_TARGET/libgcc/"
    481     OBJS="unwind-c.o \
    482        unwind-dw2-fde-dip.o \
    483        unwind-dw2.o"
    484     ;;
    485     esac
    486 
    487     for OBJ in $OBJS; do
    488         UNWIND_OBJS=$UNWIND_OBJS" $BASE_DIR/$OBJ"
    489     done
    490     echo $UNWIND_OBJS
    491 }
    492 
    493 # Create libgccunwind.a for app linking
    494 # $1: arch name
    495 # $2: NDK_DIR
    496 create_unwind_library ()
    497 {
    498     local ARCH="$1"
    499     local NDK_DIR="$2"
    500     local ABIS="$(commas_to_spaces $(convert_archs_to_abis $ARCH))"
    501     local ABI UNWIND_OBJS UNWIND_LIB
    502     for ABI in $ABIS; do
    503         UNWIND_OBJS=$(unwind_library_for_abi $ABI)
    504         UNWIND_LIB_DIR="$NDK_DIR/$GCCUNWIND_SUBDIR/libs/$ABI/"
    505         run mkdir -p $UNWIND_LIB_DIR
    506         run ar crsD $UNWIND_LIB_DIR/libgccunwind.a $UNWIND_OBJS
    507     done
    508 }
    509 
    510 # Only create libgccunwind.a when building default version of gcc
    511 DEFAULT_GCC_VERSION=$(get_default_gcc_version_for_arch $ARCH)
    512 if [ "$HOST_OS" = "linux" -a "$GCC_VERSION" = "$DEFAULT_GCC_VERSION" ]; then
    513     run create_unwind_library $ARCH $NDK_DIR
    514 fi
    515 
    516 # copy to toolchain path
    517 run copy_directory "$TOOLCHAIN_BUILD_PREFIX" "$TOOLCHAIN_PATH"
    518 
    519 if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ] ; then
    520     # For some reasons, libraries in $ABI_CONFIGURE_TARGET (*) are not installed.
    521     # Hack here to copy them over.
    522     # (*) FYI: libgcc.a and libgcov.a not installed there in the first place
    523     INSTALL_TARGET_LIB_PATH="$BUILD_OUT/host-$ABI_CONFIGURE_BUILD/install/$ABI_CONFIGURE_TARGET/lib"
    524     TOOLCHAIN_TARGET_LIB_PATH="$TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib"
    525     (cd "$INSTALL_TARGET_LIB_PATH" &&
    526         find . \( -name "*.a" -o -name "*.la" -o -name "*.spec" \) -exec install -D "{}" "$TOOLCHAIN_TARGET_LIB_PATH/{}" \;)
    527 fi
    528 
    529 # build the gdb stub and replace gdb with it. This is done post-install
    530 # so files are in the correct place when determining the relative path.
    531 if [ -n "$WITH_PYTHON" -a "$MINGW" = "yes" ] ; then
    532     WITH_PYTHON_PREFIX=$(dirname $(dirname "$WITH_PYTHON_SCRIPT"))
    533     dump "Building : $TOOLCHAIN GDB stub. "$TOOLCHAIN_PATH/bin/${ABI_CONFIGURE_TARGET}-gdb.exe", "$WITH_PYTHON_PREFIX", $ABI_CONFIGURE_HOST-gcc"
    534     GCC_FOR_STUB=$ABI_CONFIGURE_HOST-gcc
    535     if [ "$TRY64" != "yes" ]; then
    536         # The i586-mingw32msvc-gcc is missing CreateJobObject, SetInformationJobObject, and
    537         # AssignProcessToJobObject needed for gdb-stub.c.  Hack to use i686-w64-mingw32-gcc.  ToDo:
    538         GCC_FOR_STUB_TARGET=`$GCC_FOR_STUB -dumpmachine`
    539         if [ "$GCC_FOR_STUB_TARGET" = "i586-mingw32msvc" ]; then
    540             GCC_FOR_STUB=i686-w64-mingw32-gcc
    541             dump "Override compiler for gdb-stub: $GCC_FOR_STUB"
    542 	fi
    543     fi
    544     run $NDK_DIR/build/tools/build-gdb-stub.sh --gdb-executable-path="$TOOLCHAIN_PATH/bin/${ABI_CONFIGURE_TARGET}-gdb.exe" \
    545                                                --python-prefix-dir=${WITH_PYTHON_PREFIX} \
    546                                                --mingw-w64-gcc=$GCC_FOR_STUB
    547     fail_panic "Could not build gdb-stub"
    548 fi
    549 
    550 # don't forget to copy the GPL and LGPL license files
    551 run cp -f $TOOLCHAIN_LICENSES/COPYING $TOOLCHAIN_LICENSES/COPYING.LIB $TOOLCHAIN_PATH
    552 
    553 # remove some unneeded files
    554 run rm -f $TOOLCHAIN_PATH/bin/*-gccbug
    555 run rm -f $TOOLCHAIN_PATH/bin/*gdbtui$HOST_EXE
    556 run rm -f $TOOLCHAIN_PATH/bin/*-run$HOST_EXE
    557 run rm -rf $TOOLCHAIN_PATH/info
    558 run rm -rf $TOOLCHAIN_PATH/man
    559 run rm -rf $TOOLCHAIN_PATH/share/info
    560 run rm -rf $TOOLCHAIN_PATH/share/man
    561 run rm -rf $TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/*/install-tools
    562 run rm -rf $TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/*/plugin
    563 run rm -rf $TOOLCHAIN_PATH/libexec/gcc/$ABI_CONFIGURE_TARGET/*/install-tools
    564 run rm -rf $TOOLCHAIN_PATH/lib/libiberty.a
    565 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/libiberty.a
    566 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/libiberty.a
    567 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/*/libiberty.a
    568 find $TOOLCHAIN_PATH -name "*.la" -exec rm -f {} \;
    569 # Remove host install in cross compilation
    570 if [ "$ABI_CONFIGURE_HOST" != "$ABI_CONFIGURE_TARGET" ]; then
    571     run rm -rf "$TOOLCHAIN_PATH/$ABI_CONFIGURE_HOST"
    572 fi
    573 # remove sysroot
    574 run rm -rf "$TOOLCHAIN_PATH/sysroot"
    575 
    576 # Remove libstdc++ for now (will add it differently later)
    577 # We had to build it to get libsupc++ which we keep.
    578 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/libstdc++.*
    579 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/libstdc++.*
    580 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/include/c++
    581 
    582 # strip binaries to reduce final package size
    583 test -z "$STRIP" && STRIP=strip
    584 # because libpython is statically linked to GDB, it introduces symbols
    585 # that are only used by Python modules that must not be stripped. This
    586 # is not true of Windows which dynamically links to Python.
    587 if [ "$MINGW" = "yes" ] ; then
    588     run $STRIP $TOOLCHAIN_PATH/bin/*
    589 else
    590     find $TOOLCHAIN_PATH/bin -type f -not -name "*gdb" \
    591         | while read EXECUTABLE; do run $STRIP "$EXECUTABLE"; done
    592 fi
    593 run $STRIP $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin/*
    594 run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1$HOST_EXE
    595 run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1plus$HOST_EXE
    596 run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/collect2$HOST_EXE
    597 run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/lto*$HOST_EXE
    598 
    599 # Some of the files should really be links to save space.
    600 # This is mostly to reduce the size of the Windows zip archives,
    601 # since:
    602 #  - The toolchain install script actually use hard-links
    603 #  - Tar automatically detects hard links and will only store a
    604 #    single copy of each file anyway.
    605 
    606 # $1: Source file (will be changed to a link)
    607 # $2: Destination (relative to source).
    608 do_relink () {
    609     log "Relink: $1 --> $2"
    610     local BASENAME DIRNAME
    611     DIRNAME=$(dirname "$1")
    612     BASENAME=$(basename "$1")
    613     ( cd "$DIRNAME" && rm -f "$BASENAME" && ln -s "$2" "$BASENAME" )
    614     fail_panic "Can't relink $1 to $2"
    615 }
    616 
    617 # <config>/bin/<name> should point to ../../<config>-<name>
    618 LINK_FILES=$(cd $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin && ls * 2>/dev/null)
    619 for LINK_FILE in $LINK_FILES; do
    620   do_relink $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin/$LINK_FILE ../../bin/$ABI_CONFIGURE_TARGET-$LINK_FILE
    621 done
    622 
    623 # $1: Source file prefix (e.g. 'c++')
    624 # $2: Destination file prefix (e.g. 'g++')
    625 # $3: Alternative file prefix if $2 doesn't exist (eg. ld.bfd)
    626 do_relink_bin () {
    627     local DST_FILE=$2
    628     if [ ! -f "$TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET-$DST_FILE$HOST_EXE" ]; then
    629         DST_FILE=$3
    630     fi
    631     if [ ! -f "$TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET-$DST_FILE$HOST_EXE" ]; then
    632         echo "ERROR: Can't relink $1 to $DST_FILE because $DST_FILE doesn't exist"
    633         exit 1
    634     fi
    635     do_relink \
    636         $TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET-$1$HOST_EXE \
    637         $ABI_CONFIGURE_TARGET-$DST_FILE$HOST_EXE
    638 }
    639 
    640 do_relink_bin c++ g++
    641 do_relink_bin gcc-$GCC_VERSION gcc
    642 # symlink ld to either ld.gold or ld.bfd
    643 do_relink_bin ld ld.gold ld.bfd
    644 
    645 # copy SOURCES file if present
    646 if [ -f "$SRC_DIR/SOURCES" ]; then
    647     cp "$SRC_DIR/SOURCES" "$TOOLCHAIN_PATH/SOURCES"
    648 fi
    649 
    650 if [ "$PACKAGE_DIR" ]; then
    651     ARCHIVE="$TOOLCHAIN-$HOST_TAG.tar.bz2"
    652     SUBDIR=$(get_toolchain_install_subdir $TOOLCHAIN $HOST_TAG)
    653     dump "Packaging $ARCHIVE"
    654   # exlude ld.mcld
    655     EXCLUSIONS=
    656     if [ -f $SUBDIR/bin/$ABI_CONFIGURE_TARGET-ld.mcld${HOST_EXE} ] ; then
    657         EXCLUSIONS=$EXCLUSIONS" --exclude=$SUBDIR/bin/$ABI_CONFIGURE_TARGET-ld.mcld${HOST_EXE}"
    658     fi
    659     if [ -f $SUBDIR/$ABI_CONFIGURE_TARGET/bin/ld.mcld${HOST_EXE} ] ; then
    660         EXCLUSIONS=$EXCLUSIONS" --exclude=$SUBDIR/$ABI_CONFIGURE_TARGET/bin/ld.mcld${HOST_EXE}"
    661     fi
    662     pack_archive "$PACKAGE_DIR/$ARCHIVE" "$NDK_DIR" "$SUBDIR" $EXCLUSIONS
    663     # package libgccunwind.a
    664     if [ "$HOST_OS" = "linux" -a "$GCC_VERSION" = "$DEFAULT_GCC_VERSION" ]; then
    665         ABIS=$(commas_to_spaces $(convert_archs_to_abis $ARCH))
    666         for ABI in $ABIS; do
    667             FILES="$GCCUNWIND_SUBDIR/libs/$ABI/libgccunwind.a"
    668             PACKAGE="$PACKAGE_DIR/libgccunwind-libs-$ABI.tar.bz2"
    669             log "Packaging: $PACKAGE"
    670             pack_archive "$PACKAGE" "$NDK_DIR" "$FILES"
    671             fail_panic "Could not package $ABI libgccunwind binaries!"
    672             dump "Packaging: $PACKAGE"
    673         done
    674     fi
    675 fi
    676 
    677 dump "Done."
    678 if [ -z "$OPTION_BUILD_OUT" ] ; then
    679     rm -rf $BUILD_OUT
    680 fi
    681