• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
24PROGRAM_PARAMETERS="<src-dir> <ndk-dir> <toolchain>"
25
26PROGRAM_DESCRIPTION=\
27"Rebuild the gcc toolchain prebuilt binaries for the Android NDK.
28
29Where <src-dir> is the location of toolchain sources, <ndk-dir> is
30the top-level NDK installation path and <toolchain> is the name of
31the toolchain to use (e.g. arm-linux-androideabi-4.6)."
32
33RELEASE=`date +%Y%m%d`
34BUILD_OUT=/tmp/ndk-$USER/build/toolchain
35OPTION_BUILD_OUT=
36register_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
39PLATFORM=$DEFAULT_PLATFORM
40register_var_option "--platform=<name>"  PLATFORM "Specify platform name"
41
42OPTION_SYSROOT=
43register_var_option "--sysroot=<path>"   OPTION_SYSROOT   "Specify sysroot directory directly"
44
45GDB_VERSION=$DEFAULT_GDB_VERSION
46EXPLICIT_GDB_VERSION=
47register_option "--gdb-version=<version>"  do_gdb_version "Specify gdb version" "$GDB_VERSION"
48do_gdb_version () {
49    GDB_VERSION=$1
50    EXPLICIT_GDB_VERSION=true
51}
52
53BINUTILS_VERSION=$DEFAULT_BINUTILS_VERSION
54EXPLICIT_BINUTILS_VERSION=
55register_option "--binutils-version=<version>" do_binutils_version "Specify binutils version" "$BINUTILS_VERSION"
56do_binutils_version () {
57    BINUTILS_VERSION=$1
58    EXPLICIT_BINUTILS_VERSION=true
59}
60
61GMP_VERSION=$DEFAULT_GMP_VERSION
62register_var_option "--gmp-version=<version>" GMP_VERSION "Specify gmp version"
63
64MPFR_VERSION=$DEFAULT_MPFR_VERSION
65register_var_option "--mpfr-version=<version>" MPFR_VERSION "Specify mpfr version"
66
67MPC_VERSION=$DEFAULT_MPC_VERSION
68register_var_option "--mpc-version=<version>" MPC_VERSION "Specify mpc version"
69
70CLOOG_VERSION=$DEFAULT_CLOOG_VERSION
71register_var_option "--cloog-version=<version>" CLOOG_VERSION "Specify cloog version"
72
73ISL_VERSION=$DEFAULT_ISL_VERSION
74register_var_option "--isl-version=<version>" ISL_VERSION "Specify ISL version"
75
76PPL_VERSION=$DEFAULT_PPL_VERSION
77register_var_option "--ppl-version=<version>" PPL_VERSION "Specify ppl version"
78
79WITH_PYTHON=
80register_var_option "--with-python=<path/to/python-config.sh>" WITH_PYTHON "Specify python config script, or prebuilt"
81
82PACKAGE_DIR=
83register_var_option "--package-dir=<path>" PACKAGE_DIR "Create archive tarball in specific directory"
84
85register_jobs_option
86register_canadian_option
87register_try64_option
88
89extract_parameters "$@"
90
91prepare_canadian_toolchain /tmp/ndk-$USER/build
92
93fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory"
94setup_default_log_file $BUILD_OUT/config.log
95
96set_parameters ()
97{
98    SRC_DIR="$1"
99    NDK_DIR="$2"
100    TOOLCHAIN="$3"
101
102    # Check source directory
103    #
104    if [ -z "$SRC_DIR" ] ; then
105        echo "ERROR: Missing source directory parameter. See --help for details."
106        exit 1
107    fi
108
109    if [ ! -d "$SRC_DIR/gcc" ] ; then
110        echo "ERROR: Source directory does not contain gcc sources: $SRC_DIR"
111        exit 1
112    fi
113    SRC_DIR=`cd $SRC_DIR; pwd`
114    log "Using source directory: $SRC_DIR"
115
116    # Check NDK installation directory
117    #
118    if [ -z "$NDK_DIR" ] ; then
119        echo "ERROR: Missing NDK directory parameter. See --help for details."
120        exit 1
121    fi
122
123    if [ ! -d "$NDK_DIR" ] ; then
124        mkdir -p $NDK_DIR
125        if [ $? != 0 ] ; then
126            echo "ERROR: Could not create target NDK installation path: $NDK_DIR"
127            exit 1
128        fi
129    fi
130    NDK_DIR=`cd $NDK_DIR; pwd`
131    log "Using NDK directory: $NDK_DIR"
132
133    # Check toolchain name
134    #
135    if [ -z "$TOOLCHAIN" ] ; then
136        echo "ERROR: Missing toolchain name parameter. See --help for details."
137        exit 1
138    fi
139}
140
141set_parameters $PARAMETERS
142
143# Disable x86_64 build for toolchains older than 4.7
144case "$TOOLCHAIN" in
145  x86_64-4.4.3|x86_64-4.6)
146    echo "ERROR: x86_64 toolchain is enabled in 4.7+. Please try to build newer version."
147    exit 1
148    ;;
149esac
150
151prepare_target_build
152
153parse_toolchain_name $TOOLCHAIN
154
155fix_sysroot "$OPTION_SYSROOT"
156
157check_toolchain_src_dir "$SRC_DIR"
158
159if [ ! -d $SRC_DIR/gdb/gdb-$GDB_VERSION ] ; then
160    echo "ERROR: Missing gdb sources: $SRC_DIR/gdb/gdb-$GDB_VERSION"
161    echo "       Use --gdb-version=<version> to specify alternative."
162    exit 1
163fi
164
165if [ -z "$EXPLICIT_BINUTILS_VERSION" ]; then
166    BINUTILS_VERSION=$(get_default_binutils_version_for_gcc $TOOLCHAIN)
167    dump "Auto-config: --binutils-version=$BINUTILS_VERSION"
168fi
169
170if [ ! -d $SRC_DIR/binutils/binutils-$BINUTILS_VERSION ] ; then
171    echo "ERROR: Missing binutils sources: $SRC_DIR/binutils/binutils-$BINUTILS_VERSION"
172    echo "       Use --binutils-version=<version> to specify alternative."
173    exit 1
174fi
175
176if [ -z "$EXPLICIT_GDB_VERSION" ]; then
177    GDB_VERSION=$(get_default_gdb_version_for_gcc $TOOLCHAIN)
178    dump "Auto-config: --gdb-version=$GDB_VERSION"
179fi
180
181if [ ! -d $SRC_DIR/gdb/gdb-$GDB_VERSION ] ; then
182    echo "ERROR: Missing gdb sources: $SRC_DIR/gdb/gdb-$GDB_VERSION"
183    echo "       Use --gdb-version=<version> to specify alternative."
184    exit 1
185fi
186
187if [ ! -z "$WITH_PYTHON" ] ; then
188    if [ "$WITH_PYTHON" = "prebuilt" ] ; then
189        WITH_PYTHON_SCRIPT="$ANDROID_NDK_ROOT/prebuilt/$HOST_TAG/bin/python-config.sh"
190    fi
191    if [ ! -f "$WITH_PYTHON_SCRIPT" ] ; then
192        echo "ERROR: --with-python ($WITH_PYTHON_SCRIPT)"
193        echo "       Does not exist!"
194        exit 1
195    else
196        WITH_PYTHON="--with-python=$WITH_PYTHON_SCRIPT"
197    fi
198fi
199
200fix_option MPFR_VERSION "$OPTION_MPFR_VERSION" "mpfr version"
201if [ ! -f $SRC_DIR/mpfr/mpfr-$MPFR_VERSION.tar.bz2 ] ; then
202    echo "ERROR: Missing mpfr sources: $SRC_DIR/mpfr/mpfr-$MPFR_VERSION.tar.bz2"
203    echo "       Use --mpfr-version=<version> to specify alternative."
204    exit 1
205fi
206
207if [ "$PACKAGE_DIR" ]; then
208    mkdir -p "$PACKAGE_DIR"
209    fail_panic "Could not create package directory: $PACKAGE_DIR"
210fi
211
212set_toolchain_ndk $NDK_DIR $TOOLCHAIN
213
214if [ "$MINGW" != "yes" -a "$DARWIN" != "yes" ] ; then
215    dump "Using C compiler: $CC"
216    dump "Using C++ compiler: $CXX"
217fi
218
219rm -rf $BUILD_OUT
220mkdir -p $BUILD_OUT
221
222# Location where the toolchain license files are
223TOOLCHAIN_LICENSES=$ANDROID_NDK_ROOT/build/tools/toolchain-licenses
224
225# Without option "--sysroot" (and its variations), GCC will attempt to
226# search path specified by "--with-sysroot" at build time for headers/libs.
227# Path at --with-sysroot contains minimal headers and libs to boostrap
228# toolchain build, and it's not needed afterward (NOTE: NDK provides
229# sysroot at specified API level,and Android build explicit lists header/lib
230# dependencies.
231#
232# It's better to point --with-sysroot to local directory otherwise the
233# path may be found at compile-time and bad things can happen: eg.
234#  1) The path exists and contain incorrect headers/libs
235#  2) The path exists at remote server and blocks GCC for seconds
236#  3) The path exists but not accessible, which crashes GCC!
237#
238# For canadian build --with-sysroot has to be sub-directory of --prefix.
239# Put TOOLCHAIN_BUILD_PREFIX to BUILD_OUT which is in /tmp by default,
240# and TOOLCHAIN_BUILD_SYSROOT underneath.
241
242TOOLCHAIN_BUILD_PREFIX=$BUILD_OUT/prefix
243TOOLCHAIN_BUILD_SYSROOT=$TOOLCHAIN_BUILD_PREFIX/sysroot
244dump "Sysroot  : Copying: $SYSROOT --> $TOOLCHAIN_BUILD_SYSROOT"
245mkdir -p $TOOLCHAIN_BUILD_SYSROOT && (cd $SYSROOT && tar ch *) | (cd $TOOLCHAIN_BUILD_SYSROOT && tar x)
246if [ $? != 0 ] ; then
247    echo "Error while copying sysroot files. See $TMPLOG"
248    exit 1
249fi
250
251# configure the toolchain
252#
253dump "Configure: $TOOLCHAIN toolchain build"
254# Old versions of the toolchain source packages placed the
255# configure script at the top-level. Newer ones place it under
256# the build directory though. Probe the file system to check
257# this.
258BUILD_SRCDIR=$SRC_DIR/build
259if [ ! -d $BUILD_SRCDIR ] ; then
260    BUILD_SRCDIR=$SRC_DIR
261fi
262OLD_ABI="${ABI}"
263export CC CXX
264export CFLAGS_FOR_TARGET="$ABI_CFLAGS_FOR_TARGET"
265export CXXFLAGS_FOR_TARGET="$ABI_CXXFLAGS_FOR_TARGET"
266# Needed to build a 32-bit gmp on 64-bit systems
267export ABI=$HOST_GMP_ABI
268
269# Note that the following flags only apply for "build" in canadian
270# -Wno-error is needed because our gdb-6.6 sources use -Werror by default
271# and fail to build with recent GCC versions.
272CFLAGS_FOR_BUILD="-O2 -s -Wno-error"
273LDFLAGS_FOR_BUILD=
274
275if [ "$MINGW" = "yes" ] ; then
276    CFLAGS_FOR_BUILD=$CFLAGS_FOR_BUILD" -D__USE_MINGW_ANSI_STDIO=1"
277fi
278
279CFLAGS="$CFLAGS_FOR_BUILD $HOST_CFLAGS"
280LDFLAGS="$LDFLAGS_FOR_BUILD $HOST_LDFLAGS"
281
282export CFLAGS LDFLAGS CFLAGS_FOR_BUILD LDFLAGS_FOR_BUILD
283
284# This extra flag is used to slightly speed up the build
285EXTRA_CONFIG_FLAGS="--disable-bootstrap"
286
287# This is to disable GCC 4.6 specific features that don't compile well
288# the flags are ignored for older GCC versions.
289EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-libquadmath"
290if [ "$DARWIN" = "yes" ]; then
291    # Disable plugin because in canadian cross build, plugin gengtype
292    # will be incorrectly linked with build's library and fails.
293    # ToDo
294    EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-plugin"
295else
296    # Plugins are not supported well before 4.7. On 4.7 it's required to have
297    # -flto working. Flag --enable-plugins (note 's') is actually for binutils,
298    # this is compiler requirement to have binutils configured this way. Flag
299    # --disable-plugin is for gcc.
300    case "$GCC_VERSION" in
301        4.4.3)
302            EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-plugin"
303            ;;
304        *)
305            EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-plugins"
306            ;;
307    esac
308fi
309
310# Enable OpenMP
311case "$TOOLCHAIN" in
312    *) EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-libgomp" ;;
313esac
314
315# Disable libsanitizer (which depends on libstdc++ built separately) for now
316EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-libsanitizer"
317
318# Enable Gold as default
319case "$TOOLCHAIN" in
320    # Note that only ARM and X86 >= GCC 4.6 are supported
321    mips*)
322    ;;
323    *-4.4.3)
324    ;;
325    *)
326        EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-gold=default"
327    ;;
328esac
329
330# Enable Graphite
331case "$TOOLCHAIN" in
332    *-4.4.3) ;;
333    *-4.6|*-4.7)
334        EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-graphite=yes --with-cloog-version=$CLOOG_VERSION --with-ppl-version=$PPL_VERSION"
335    ;;
336    *)
337        EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-graphite=yes --with-cloog-version=$CLOOG_VERSION --with-isl-version=$ISL_VERSION"
338    ;;
339esac
340
341# Enable linker option -eh-frame-hdr also for static executable
342EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-eh-frame-hdr-for-static"
343
344cd $BUILD_OUT && run \
345$BUILD_SRCDIR/configure --target=$ABI_CONFIGURE_TARGET \
346                        --enable-initfini-array \
347                        --host=$ABI_CONFIGURE_HOST \
348                        --build=$ABI_CONFIGURE_BUILD \
349                        --disable-nls \
350                        --prefix=$TOOLCHAIN_BUILD_PREFIX \
351                        --with-sysroot=$TOOLCHAIN_BUILD_SYSROOT \
352                        --with-binutils-version=$BINUTILS_VERSION \
353                        --with-mpfr-version=$MPFR_VERSION \
354                        --with-mpc-version=$MPC_VERSION \
355                        --with-gmp-version=$GMP_VERSION \
356                        --with-gcc-version=$GCC_VERSION \
357                        --with-gdb-version=$GDB_VERSION \
358                        $WITH_PYTHON \
359                        --with-gxx-include-dir=$TOOLCHAIN_BUILD_PREFIX/include/c++/$GCC_VERSION \
360                        --with-bugurl=$DEFAULT_ISSUE_TRACKER_URL \
361                        $EXTRA_CONFIG_FLAGS \
362                        $ABI_CONFIGURE_EXTRA_FLAGS
363if [ $? != 0 ] ; then
364    dump "Error while trying to configure toolchain build. See $TMPLOG"
365    exit 1
366fi
367
368ABI="$OLD_ABI"
369# build the toolchain
370dump "Building : $TOOLCHAIN toolchain [this can take a long time]."
371cd $BUILD_OUT
372export CC CXX
373export ABI=$HOST_GMP_ABI
374JOBS=$NUM_JOBS
375
376while [ -n "1" ]; do
377    run make -j$JOBS
378    if [ $? = 0 ] ; then
379        break
380    else
381        if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ] ; then
382            # Unfortunately, there is a bug in the GCC build scripts that prevent
383            # parallel mingw/darwin canadian cross builds to work properly on some
384            # multi-core machines (but not all, sounds like a race condition). Detect
385            # this and restart in less parallelism, until -j1 also fail
386            JOBS=$((JOBS/2))
387            if [ $JOBS -lt 1 ] ; then
388                echo "Error while building mingw/darwin toolchain. See $TMPLOG"
389                exit 1
390            fi
391            dump "Parallel canadian build failed - continuing in less parallelism -j$JOBS"
392        else
393            echo "Error while building toolchain. See $TMPLOG"
394            exit 1
395        fi
396    fi
397done
398
399ABI="$OLD_ABI"
400
401# install the toolchain to its final location
402dump "Install  : $TOOLCHAIN toolchain binaries."
403cd $BUILD_OUT && run make install
404if [ $? != 0 ] ; then
405    echo "Error while installing toolchain. See $TMPLOG"
406    exit 1
407fi
408
409# copy to toolchain path
410run copy_directory "$TOOLCHAIN_BUILD_PREFIX" "$TOOLCHAIN_PATH"
411
412if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ] ; then
413    # For some reasons, libraries in $ABI_CONFIGURE_TARGET (*) are not installed.
414    # Hack here to copy them over.
415    # (*) FYI: libgcc.a and libgcov.a not installed there in the first place
416    INSTALL_TARGET_LIB_PATH="$BUILD_OUT/host-$ABI_CONFIGURE_BUILD/install/$ABI_CONFIGURE_TARGET/lib"
417    TOOLCHAIN_TARGET_LIB_PATH="$TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib"
418    (cd "$INSTALL_TARGET_LIB_PATH" &&
419        find . \( -name "*.a" -o -name "*.la" -o -name "*.spec" \) -exec install -D "{}" "$TOOLCHAIN_TARGET_LIB_PATH/{}" \;)
420fi
421
422# build the gdb stub and replace gdb with it. This is done post-install
423# so files are in the correct place when determining the relative path.
424if [ "$MINGW" = "yes" ] ; then
425    WITH_PYTHON_PREFIX=$(dirname $(dirname "$WITH_PYTHON_SCRIPT"))
426    dump "Building : $TOOLCHAIN GDB stub. "$TOOLCHAIN_PATH/bin/${ABI_CONFIGURE_TARGET}-gdb.exe", "$WITH_PYTHON_PREFIX", $ABI_CONFIGURE_HOST-gcc"
427    GCC_FOR_STUB=$ABI_CONFIGURE_HOST-gcc
428    if [ "$TRY64" != "yes" ]; then
429        # The i586-mingw32msvc-gcc is missing CreateJobObject, SetInformationJobObject, and
430        # AssignProcessToJobObject needed for gdb-stub.c.  Hack to use i686-w64-mingw32-gcc.  ToDo:
431        GCC_FOR_STUB_TARGET=`$GCC_FOR_STUB -dumpmachine`
432        if [ "$GCC_FOR_STUB_TARGET" = "i586-mingw32msvc" ]; then
433            GCC_FOR_STUB=i686-w64-mingw32-gcc
434            dump "Override compiler for gdb-stub: $GCC_FOR_STUB"
435	fi
436    fi
437    run $NDK_DIR/build/tools/build-gdb-stub.sh --gdb-executable-path="$TOOLCHAIN_PATH/bin/${ABI_CONFIGURE_TARGET}-gdb.exe" \
438                                               --python-prefix-dir=${WITH_PYTHON_PREFIX} \
439                                               --mingw-w64-gcc=$GCC_FOR_STUB
440fi
441
442# don't forget to copy the GPL and LGPL license files
443run cp -f $TOOLCHAIN_LICENSES/COPYING $TOOLCHAIN_LICENSES/COPYING.LIB $TOOLCHAIN_PATH
444
445# remove some unneeded files
446run rm -f $TOOLCHAIN_PATH/bin/*-gccbug
447run rm -f $TOOLCHAIN_PATH/bin/*gdbtui$HOST_EXE
448run rm -f $TOOLCHAIN_PATH/bin/*-run$HOST_EXE
449run rm -rf $TOOLCHAIN_PATH/info
450run rm -rf $TOOLCHAIN_PATH/man
451run rm -rf $TOOLCHAIN_PATH/share/info
452run rm -rf $TOOLCHAIN_PATH/share/man
453run rm -rf $TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/*/install-tools
454run rm -rf $TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/*/plugin
455run rm -rf $TOOLCHAIN_PATH/libexec/gcc/$ABI_CONFIGURE_TARGET/*/install-tools
456run rm -rf $TOOLCHAIN_PATH/lib/libiberty.a
457run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/libiberty.a
458run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/libiberty.a
459run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/*/libiberty.a
460find $TOOLCHAIN_PATH -name "*.la" -exec rm -f {} \;
461
462# Remove libstdc++ for now (will add it differently later)
463# We had to build it to get libsupc++ which we keep.
464run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/libstdc++.*
465run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/libstdc++.*
466run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/include/c++
467
468# strip binaries to reduce final package size
469test -z "$STRIP" && STRIP=strip
470run $STRIP $TOOLCHAIN_PATH/bin/*
471run $STRIP $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin/*
472run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1$HOST_EXE
473run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1plus$HOST_EXE
474run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/collect2$HOST_EXE
475run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/lto*$HOST_EXE
476
477# Some of the files should really be links to save space.
478# This is mostly to reduce the size of the Windows zip archives,
479# since:
480#  - The toolchain install script actually use hard-links
481#  - Tar automatically detects hard links and will only store a
482#    single copy of each file anyway.
483
484# $1: Source file (will be changed to a link)
485# $2: Destination (relative to source).
486do_relink () {
487    log "Relink: $1 --> $2"
488    local BASENAME DIRNAME
489    DIRNAME=$(dirname "$1")
490    BASENAME=$(basename "$1")
491    ( cd "$DIRNAME" && rm -f "$BASENAME" && ln -s "$2" "$BASENAME" )
492    fail_panic "Can't relink $1 to $2"
493}
494
495# <config>/bin/<name> should point to ../../<config>-<name>
496LINK_FILES=$(cd $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin && ls * 2>/dev/null)
497for LINK_FILE in $LINK_FILES; do
498  do_relink $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin/$LINK_FILE ../../bin/$ABI_CONFIGURE_TARGET-$LINK_FILE
499done
500
501# $1: Source file prefix (e.g. 'c++')
502# $2: Destination file prefix (e.g. 'g++')
503# $3: Alternative file prefix if $2 doesn't exist (eg. ld.bfd)
504do_relink_bin () {
505    local DST_FILE=$2
506    if [ ! -f "$TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET-$DST_FILE$HOST_EXE" ]; then
507        DST_FILE=$3
508    fi
509    if [ ! -f "$TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET-$DST_FILE$HOST_EXE" ]; then
510        echo "ERROR: Can't relink $1 to $DST_FILE because $DST_FILE doesn't exist"
511        exit 1
512    fi
513    do_relink \
514        $TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET-$1$HOST_EXE \
515        $ABI_CONFIGURE_TARGET-$DST_FILE$HOST_EXE
516}
517
518do_relink_bin c++ g++
519do_relink_bin gcc-$GCC_VERSION gcc
520# symlink ld to either ld.gold or ld.bfd
521do_relink_bin ld ld.gold ld.bfd
522
523# copy SOURCES file if present
524if [ -f "$SRC_DIR/SOURCES" ]; then
525    cp "$SRC_DIR/SOURCES" "$TOOLCHAIN_PATH/SOURCES"
526fi
527
528if [ "$PACKAGE_DIR" ]; then
529    ARCHIVE="$TOOLCHAIN-$HOST_TAG.tar.bz2"
530    SUBDIR=$(get_toolchain_install_subdir $TOOLCHAIN $HOST_TAG)
531    dump "Packaging $ARCHIVE"
532  # exlude ld.mcld
533    EXCLUSIONS=
534    if [ -f $SUBDIR/bin/$ABI_CONFIGURE_TARGET-ld.mcld${HOST_EXE} ] ; then
535        EXCLUSIONS=$EXCLUSIONS" --exclude=$SUBDIR/bin/$ABI_CONFIGURE_TARGET-ld.mcld${HOST_EXE}"
536    fi
537    if [ -f $SUBDIR/$ABI_CONFIGURE_TARGET/bin/ld.mcld${HOST_EXE} ] ; then
538        EXCLUSIONS=$EXCLUSIONS" --exclude=$SUBDIR/$ABI_CONFIGURE_TARGET/bin/ld.mcld${HOST_EXE}"
539    fi
540    pack_archive "$PACKAGE_DIR/$ARCHIVE" "$NDK_DIR" "$SUBDIR" $EXCLUSIONS
541fi
542
543dump "Done."
544if [ -z "$OPTION_BUILD_OUT" ] ; then
545    rm -rf $BUILD_OUT
546fi
547