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.8)."
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
39register_var_option "--platform=<name>"  PLATFORM "Specify platform name"
40
41OPTION_SYSROOT=
42register_var_option "--sysroot=<path>"   OPTION_SYSROOT   "Specify sysroot directory directly"
43
44GDB_VERSION=$DEFAULT_GDB_VERSION
45EXPLICIT_GDB_VERSION=
46register_option "--gdb-version=<version>"  do_gdb_version "Specify gdb version" "$GDB_VERSION"
47do_gdb_version () {
48    GDB_VERSION=$1
49    EXPLICIT_GDB_VERSION=true
50}
51
52BINUTILS_VERSION=$DEFAULT_BINUTILS_VERSION
53EXPLICIT_BINUTILS_VERSION=
54register_option "--binutils-version=<version>" do_binutils_version "Specify binutils version" "$BINUTILS_VERSION"
55do_binutils_version () {
56    BINUTILS_VERSION=$1
57    EXPLICIT_BINUTILS_VERSION=true
58}
59
60GMP_VERSION=$DEFAULT_GMP_VERSION
61register_var_option "--gmp-version=<version>" GMP_VERSION "Specify gmp version"
62
63MPFR_VERSION=$DEFAULT_MPFR_VERSION
64register_var_option "--mpfr-version=<version>" MPFR_VERSION "Specify mpfr version"
65
66MPC_VERSION=$DEFAULT_MPC_VERSION
67register_var_option "--mpc-version=<version>" MPC_VERSION "Specify mpc version"
68
69CLOOG_VERSION=$DEFAULT_CLOOG_VERSION
70register_var_option "--cloog-version=<version>" CLOOG_VERSION "Specify cloog version"
71
72ISL_VERSION=$DEFAULT_ISL_VERSION
73register_var_option "--isl-version=<version>" ISL_VERSION "Specify ISL version"
74
75PPL_VERSION=$DEFAULT_PPL_VERSION
76register_var_option "--ppl-version=<version>" PPL_VERSION "Specify ppl version"
77
78WITH_PYTHON=
79register_var_option "--with-python=<path/to/python-config.sh>" WITH_PYTHON "Specify python config script, or prebuilt"
80
81PACKAGE_DIR=
82register_var_option "--package-dir=<path>" PACKAGE_DIR "Create archive tarball in specific directory"
83
84register_jobs_option
85register_canadian_option
86register_try64_option
87
88extract_parameters "$@"
89
90prepare_canadian_toolchain /tmp/ndk-$USER/build
91
92fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory"
93setup_default_log_file $BUILD_OUT/config.log
94
95set_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
140set_parameters $PARAMETERS
141
142# Disable x86_64 build for toolchains older than 4.7
143case "$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    ;;
148esac
149
150prepare_target_build
151
152parse_toolchain_name $TOOLCHAIN
153
154if [ -z "$PLATFORM" ]; then
155   PLATFORM="android-"$(get_default_api_level_for_arch $ARCH)
156fi
157
158fix_sysroot "$OPTION_SYSROOT"
159
160check_toolchain_src_dir "$SRC_DIR"
161
162if [ ! -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
166fi
167
168if [ -z "$EXPLICIT_BINUTILS_VERSION" ]; then
169    BINUTILS_VERSION=$(get_default_binutils_version_for_gcc $TOOLCHAIN)
170    dump "Auto-config: --binutils-version=$BINUTILS_VERSION"
171fi
172
173if [ ! -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
177fi
178
179if [ -z "$EXPLICIT_GDB_VERSION" ]; then
180    GDB_VERSION=$(get_default_gdb_version_for_gcc $TOOLCHAIN)
181    dump "Auto-config: --gdb-version=$GDB_VERSION"
182fi
183
184if [ ! -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
188fi
189
190if [ ! -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
201fi
202
203fix_option MPFR_VERSION "$OPTION_MPFR_VERSION" "mpfr version"
204if [ ! -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
208fi
209
210if [ "$PACKAGE_DIR" ]; then
211    mkdir -p "$PACKAGE_DIR"
212    fail_panic "Could not create package directory: $PACKAGE_DIR"
213fi
214
215set_toolchain_ndk $NDK_DIR $TOOLCHAIN
216
217if [ "$MINGW" != "yes" -a "$DARWIN" != "yes" ] ; then
218    dump "Using C compiler: $CC"
219    dump "Using C++ compiler: $CXX"
220fi
221
222rm -rf $BUILD_OUT
223mkdir -p $BUILD_OUT
224
225# Location where the toolchain license files are
226TOOLCHAIN_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
245TOOLCHAIN_BUILD_PREFIX=$BUILD_OUT/prefix
246TOOLCHAIN_BUILD_SYSROOT=$TOOLCHAIN_BUILD_PREFIX/sysroot
247dump "Sysroot  : Copying: $SYSROOT --> $TOOLCHAIN_BUILD_SYSROOT"
248mkdir -p $TOOLCHAIN_BUILD_SYSROOT && (cd $SYSROOT && tar chf - *) | (cd $TOOLCHAIN_BUILD_SYSROOT && tar xf -)
249if [ $? != 0 ] ; then
250    echo "Error while copying sysroot files. See $TMPLOG"
251    exit 1
252fi
253
254# configure the toolchain
255#
256dump "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.
261BUILD_SRCDIR=$SRC_DIR/build
262if [ ! -d $BUILD_SRCDIR ] ; then
263    BUILD_SRCDIR=$SRC_DIR
264fi
265OLD_ABI="${ABI}"
266export CC CXX
267export CFLAGS_FOR_TARGET="$ABI_CFLAGS_FOR_TARGET"
268export CXXFLAGS_FOR_TARGET="$ABI_CXXFLAGS_FOR_TARGET"
269# Needed to build a 32-bit gmp on 64-bit systems
270export 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.
275CFLAGS_FOR_BUILD="-O2 -s -Wno-error"
276LDFLAGS_FOR_BUILD=
277
278if [ "$MINGW" = "yes" ] ; then
279    CFLAGS_FOR_BUILD=$CFLAGS_FOR_BUILD" -D__USE_MINGW_ANSI_STDIO=1"
280fi
281
282CFLAGS="$CFLAGS_FOR_BUILD $HOST_CFLAGS"
283LDFLAGS="$LDFLAGS_FOR_BUILD $HOST_LDFLAGS"
284
285export CFLAGS LDFLAGS CFLAGS_FOR_BUILD LDFLAGS_FOR_BUILD
286
287# This extra flag is used to slightly speed up the build
288EXTRA_CONFIG_FLAGS="--disable-bootstrap"
289
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# Enable indirect functions in the compilers that support it (4.6 and above)
316case "$TOOLCHAIN" in
317    *-4.4.3) ;;
318    *) EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-gnu-indirect-function" ;;
319esac
320
321# Disable libcilkrts which needs C++ for now, because libstdlibc++ in NDK is built separately...
322case "$TOOLCHAIN" in
323    x86*-4.9) EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-libcilkrts"
324esac
325
326# Disable libsanitizer (which depends on libstdc++ built separately) for now
327EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-libsanitizer"
328
329# Enable Gold
330case "$TOOLCHAIN" in
331    # Note that only ARM/X86 >= GCC 4.6 and AARCH64 >= GCC 4.9 are supported
332    mips*)
333    ;;
334    *-4.4.3)
335    ;;
336    aarch64*)
337        # Enable ld.gold but ld.bfd remain the default
338        EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-gold --enable-ld=default --enable-threads"
339    ;;
340    *)
341        # Enable ld.gold as default
342        EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-gold=default --enable-threads"
343    ;;
344esac
345
346# Enable Graphite
347case "$TOOLCHAIN" in
348    *-4.4.3) ;;
349    *-4.6|*-4.7)
350        EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-graphite=yes --with-cloog-version=$CLOOG_VERSION --with-ppl-version=$PPL_VERSION"
351    ;;
352    *)
353        EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-graphite=yes --with-cloog-version=$CLOOG_VERSION --with-isl-version=$ISL_VERSION"
354    ;;
355esac
356
357# Enable linker option -eh-frame-hdr also for static executable
358EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-eh-frame-hdr-for-static"
359
360# Enable aarch64 workaround for Cortex-A53 Erratum number 835769
361case "$TOOLCHAIN" in
362    aarch64*-4.9) EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-fix-cortex-a53-835769"
363esac
364
365MAY_FAIL_DUE_TO_RACE_CONDITION=
366if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ]; then
367   MAY_FAIL_DUE_TO_RACE_CONDITION=yes
368fi
369
370# hack to use different set of sources
371CONFIGURE_GCC_VERSION=$GCC_VERSION
372case "$TOOLCHAIN" in
373  *4.9l)
374    CONFIGURE_GCC_VERSION=4.9l
375    ;;
376  *4.8l)
377    CONFIGURE_GCC_VERSION=4.8l
378    ;;
379esac
380
381cd $BUILD_OUT && run \
382$BUILD_SRCDIR/configure --target=$ABI_CONFIGURE_TARGET \
383                        --enable-initfini-array \
384                        --host=$ABI_CONFIGURE_HOST \
385                        --build=$ABI_CONFIGURE_BUILD \
386                        --disable-nls \
387                        --prefix=$TOOLCHAIN_BUILD_PREFIX \
388                        --with-sysroot=$TOOLCHAIN_BUILD_SYSROOT \
389                        --with-binutils-version=$BINUTILS_VERSION \
390                        --with-mpfr-version=$MPFR_VERSION \
391                        --with-mpc-version=$MPC_VERSION \
392                        --with-gmp-version=$GMP_VERSION \
393                        --with-gcc-version=$CONFIGURE_GCC_VERSION \
394                        --with-gdb-version=$GDB_VERSION \
395                        $WITH_PYTHON \
396                        --with-gxx-include-dir=$TOOLCHAIN_BUILD_PREFIX/include/c++/$GCC_VERSION \
397                        --with-bugurl=$DEFAULT_ISSUE_TRACKER_URL \
398                        $EXTRA_CONFIG_FLAGS \
399                        $ABI_CONFIGURE_EXTRA_FLAGS
400if [ $? != 0 ] ; then
401    dump "Error while trying to configure toolchain build. See $TMPLOG"
402    exit 1
403fi
404
405ABI="$OLD_ABI"
406# build the toolchain
407dump "Building : $TOOLCHAIN toolchain [this can take a long time]."
408cd $BUILD_OUT
409export CC CXX
410export ABI=$HOST_GMP_ABI
411export NUM_JOBS
412
413while [ -n "1" ]; do
414    run make -j$NUM_JOBS
415    if [ $? = 0 ] ; then
416        break
417    else
418        if [ "$MAY_FAIL_DUE_TO_RACE_CONDITION" = "yes" ] ; then
419            # Unfortunately, there is a bug in the GCC build scripts that prevent
420            # parallel mingw/darwin canadian cross builds to work properly on some
421            # multi-core machines (but not all, sounds like a race condition). Detect
422            # this and restart in less parallelism, until -j1 also fail
423            NUM_JOBS=$((NUM_JOBS/2))
424            export NUM_JOBS
425            if [ $NUM_JOBS -lt 1 ] ; then
426                echo "Error while building mingw/darwin toolchain. See $TMPLOG"
427                exit 1
428            fi
429            dump "Parallel canadian build failed - continuing in less parallelism -j$NUM_JOBS"
430        else
431            echo "Error while building toolchain. See $TMPLOG"
432            exit 1
433        fi
434    fi
435done
436
437ABI="$OLD_ABI"
438
439# install the toolchain to its final location.
440dump "Install  : $TOOLCHAIN toolchain binaries."
441cd $BUILD_OUT && run make install
442if [ $? != 0 ] ; then
443    # 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)
444    NUM_JOBS=1
445    export NUM_JOBS
446    run make install -j$NUM_JOBS
447    if [ $? != 0 ] ; then
448        echo "Error while installing toolchain. See $TMPLOG"
449        exit 1
450    fi
451fi
452
453unwind_library_for_abi ()
454{
455    local ABI="$1"
456    local BASE_DIR OBJS UNWIND_OBJS
457
458    case $ABI in
459    armeabi)
460    BASE_DIR="$BUILD_OUT/gcc-$CONFIGURE_GCC_VERSION/$ABI_CONFIGURE_TARGET/libgcc/"
461    OBJS="unwind-arm.o \
462          libunwind.o \
463          pr-support.o \
464          unwind-c.o"
465    ;;
466    armeabi-v7a)
467    BASE_DIR="$BUILD_OUT/gcc-$CONFIGURE_GCC_VERSION/$ABI_CONFIGURE_TARGET/armv7-a/libgcc/"
468    OBJS="unwind-arm.o \
469          libunwind.o \
470          pr-support.o \
471          unwind-c.o"
472    ;;
473    armeabi-v7a-hard)
474    BASE_DIR="$BUILD_OUT/gcc-$CONFIGURE_GCC_VERSION/$ABI_CONFIGURE_TARGET/armv7-a/hard/libgcc/"
475    OBJS="unwind-arm.o \
476          libunwind.o \
477          pr-support.o \
478          unwind-c.o"
479    ;;
480    x86|mips|mips32r6)
481    BASE_DIR="$BUILD_OUT/gcc-$CONFIGURE_GCC_VERSION/$ABI_CONFIGURE_TARGET/libgcc/"
482    if [ "$GCC_VERSION" = "4.6" -o "$GCC_VERSION" = "4.4.3" ]; then
483       OBJS="unwind-c.o \
484          unwind-dw2-fde-glibc.o \
485          unwind-dw2.o"
486    else
487       OBJS="unwind-c.o \
488          unwind-dw2-fde-dip.o \
489          unwind-dw2.o"
490    fi
491    ;;
492    arm64-v8a|x86_64|mips64)
493    BASE_DIR="$BUILD_OUT/gcc-$CONFIGURE_GCC_VERSION/$ABI_CONFIGURE_TARGET/libgcc/"
494    OBJS="unwind-c.o \
495       unwind-dw2-fde-dip.o \
496       unwind-dw2.o"
497    ;;
498    esac
499
500    for OBJ in $OBJS; do
501        UNWIND_OBJS=$UNWIND_OBJS" $BASE_DIR/$OBJ"
502    done
503    echo $UNWIND_OBJS
504}
505
506# Create libgccunwind.a for app linking
507# $1: arch name
508# $2: NDK_DIR
509create_unwind_library ()
510{
511    local ARCH="$1"
512    local NDK_DIR="$2"
513    local ABIS="$(commas_to_spaces $(convert_archs_to_abis $ARCH))"
514    local ABI UNWIND_OBJS UNWIND_LIB
515    for ABI in $ABIS; do
516        UNWIND_OBJS=$(unwind_library_for_abi $ABI)
517        UNWIND_LIB_DIR="$NDK_DIR/$GCCUNWIND_SUBDIR/libs/$ABI/"
518        run mkdir -p $UNWIND_LIB_DIR
519        run ar crsD $UNWIND_LIB_DIR/libgccunwind.a $UNWIND_OBJS
520    done
521}
522
523# Only create libgccunwind.a when building default version of gcc
524DEFAULT_GCC_VERSION=$(get_default_gcc_version_for_arch $ARCH)
525if [ "$HOST_OS" = "linux" -a "$GCC_VERSION" = "$DEFAULT_GCC_VERSION" ]; then
526    run create_unwind_library $ARCH $NDK_DIR
527fi
528
529# copy to toolchain path
530run copy_directory "$TOOLCHAIN_BUILD_PREFIX" "$TOOLCHAIN_PATH"
531
532if [ "$MINGW" = "yes" -o "$DARWIN" = "yes" ] ; then
533    # For some reasons, libraries in $ABI_CONFIGURE_TARGET (*) are not installed.
534    # Hack here to copy them over.
535    # (*) FYI: libgcc.a and libgcov.a not installed there in the first place
536    INSTALL_TARGET_LIB_PATH="$BUILD_OUT/host-$ABI_CONFIGURE_BUILD/install/$ABI_CONFIGURE_TARGET/lib"
537    TOOLCHAIN_TARGET_LIB_PATH="$TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib"
538    (cd "$INSTALL_TARGET_LIB_PATH" &&
539        find . \( -name "*.a" -o -name "*.la" -o -name "*.spec" \) -exec install -D "{}" "$TOOLCHAIN_TARGET_LIB_PATH/{}" \;)
540fi
541
542# build the gdb stub and replace gdb with it. This is done post-install
543# so files are in the correct place when determining the relative path.
544if [ -n "$WITH_PYTHON" -a "$MINGW" = "yes" ] ; then
545    WITH_PYTHON_PREFIX=$(dirname $(dirname "$WITH_PYTHON_SCRIPT"))
546    dump "Building : $TOOLCHAIN GDB stub. "$TOOLCHAIN_PATH/bin/${ABI_CONFIGURE_TARGET}-gdb.exe", "$WITH_PYTHON_PREFIX", $ABI_CONFIGURE_HOST-gcc"
547    GCC_FOR_STUB=$ABI_CONFIGURE_HOST-gcc
548    if [ "$TRY64" != "yes" ]; then
549        # The i586-mingw32msvc-gcc is missing CreateJobObject, SetInformationJobObject, and
550        # AssignProcessToJobObject needed for gdb-stub.c.  Hack to use i686-w64-mingw32-gcc.  ToDo:
551        GCC_FOR_STUB_TARGET=`$GCC_FOR_STUB -dumpmachine`
552        if [ "$GCC_FOR_STUB_TARGET" = "i586-mingw32msvc" ]; then
553            GCC_FOR_STUB=i686-w64-mingw32-gcc
554            dump "Override compiler for gdb-stub: $GCC_FOR_STUB"
555	fi
556    fi
557    run $NDK_DIR/build/tools/build-gdb-stub.sh --gdb-executable-path="$TOOLCHAIN_PATH/bin/${ABI_CONFIGURE_TARGET}-gdb.exe" \
558                                               --python-prefix-dir=${WITH_PYTHON_PREFIX} \
559                                               --mingw-w64-gcc=$GCC_FOR_STUB
560    fail_panic "Could not build gdb-stub"
561fi
562
563# don't forget to copy the GPL and LGPL license files
564run cp -f $TOOLCHAIN_LICENSES/COPYING* $TOOLCHAIN_PATH
565
566# remove some unneeded files
567run rm -f $TOOLCHAIN_PATH/bin/*-gccbug
568run rm -f $TOOLCHAIN_PATH/bin/*gdbtui$HOST_EXE
569run rm -f $TOOLCHAIN_PATH/bin/*-run$HOST_EXE
570run rm -rf $TOOLCHAIN_PATH/info
571run rm -rf $TOOLCHAIN_PATH/man
572run rm -rf $TOOLCHAIN_PATH/share/info
573run rm -rf $TOOLCHAIN_PATH/share/man
574run rm -rf $TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/*/install-tools
575run rm -rf $TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/*/plugin
576run rm -rf $TOOLCHAIN_PATH/libexec/gcc/$ABI_CONFIGURE_TARGET/*/install-tools
577run rm -rf $TOOLCHAIN_PATH/lib/libiberty.a
578run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/libiberty.a
579run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/libiberty.a
580run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/*/libiberty.a
581find $TOOLCHAIN_PATH -name "*.la" -exec rm -f {} \;
582# Remove host install in cross compilation
583if [ "$ABI_CONFIGURE_HOST" != "$ABI_CONFIGURE_TARGET" ]; then
584    run rm -rf "$TOOLCHAIN_PATH/$ABI_CONFIGURE_HOST"
585fi
586# remove sysroot
587run rm -rf "$TOOLCHAIN_PATH/sysroot"
588
589# Remove libstdc++ for now (will add it differently later)
590# We had to build it to get libsupc++ which we keep.
591run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/libstdc++.*
592run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/libstdc++.*
593run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/include/c++
594
595# strip binaries to reduce final package size
596test -z "$STRIP" && STRIP=strip
597# because libpython is statically linked to GDB, it introduces symbols
598# that are only used by Python modules that must not be stripped. This
599# is not true of Windows which dynamically links to Python.
600if [ "$MINGW" = "yes" ] ; then
601    run $STRIP $TOOLCHAIN_PATH/bin/*
602else
603    find $TOOLCHAIN_PATH/bin -type f -not -name "*gdb" \
604        | while read EXECUTABLE; do run $STRIP "$EXECUTABLE"; done
605fi
606run $STRIP $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin/*
607run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1$HOST_EXE
608run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1plus$HOST_EXE
609run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/collect2$HOST_EXE
610run $STRIP $TOOLCHAIN_PATH/libexec/gcc/*/*/lto*$HOST_EXE
611
612# Some of the files should really be links to save space.
613# This is mostly to reduce the size of the Windows zip archives,
614# since:
615#  - The toolchain install script actually use hard-links
616#  - Tar automatically detects hard links and will only store a
617#    single copy of each file anyway.
618
619# $1: Source file (will be changed to a link)
620# $2: Destination (relative to source).
621do_relink () {
622    log "Relink: $1 --> $2"
623    local BASENAME DIRNAME
624    DIRNAME=$(dirname "$1")
625    BASENAME=$(basename "$1")
626    ( cd "$DIRNAME" && rm -f "$BASENAME" && ln -s "$2" "$BASENAME" )
627    fail_panic "Can't relink $1 to $2"
628}
629
630# <config>/bin/<name> should point to ../../<config>-<name>
631LINK_FILES=$(cd $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin && ls * 2>/dev/null)
632for LINK_FILE in $LINK_FILES; do
633  do_relink $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin/$LINK_FILE ../../bin/$ABI_CONFIGURE_TARGET-$LINK_FILE
634done
635
636# $1: Source file prefix (e.g. 'c++')
637# $2: Destination file prefix (e.g. 'g++')
638# $3: Alternative file prefix if $2 doesn't exist (eg. ld.bfd)
639do_relink_bin () {
640    local DST_FILE=$2
641    if [ ! -f "$TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET-$DST_FILE$HOST_EXE" ]; then
642        DST_FILE=$3
643    fi
644    if [ ! -f "$TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET-$DST_FILE$HOST_EXE" ]; then
645        echo "ERROR: Can't relink $1 to $DST_FILE because $DST_FILE doesn't exist"
646        exit 1
647    fi
648    do_relink \
649        $TOOLCHAIN_PATH/bin/$ABI_CONFIGURE_TARGET-$1$HOST_EXE \
650        $ABI_CONFIGURE_TARGET-$DST_FILE$HOST_EXE
651}
652
653do_relink_bin c++ g++
654do_relink_bin gcc-$GCC_VERSION gcc
655# symlink ld to either ld.gold or ld.bfd
656case "$TOOLCHAIN" in
657    aarch64*)
658    # Don't make ld.gold as default for now because it's new
659    do_relink_bin ld ld.bfd ld.gold
660    ;;
661    *)
662    do_relink_bin ld ld.gold ld.bfd
663    ;;
664esac
665
666# copy SOURCES file if present
667if [ -f "$SRC_DIR/SOURCES" ]; then
668    cp "$SRC_DIR/SOURCES" "$TOOLCHAIN_PATH/SOURCES"
669fi
670
671if [ "$PACKAGE_DIR" ]; then
672    ARCHIVE="$TOOLCHAIN-$HOST_TAG.tar.bz2"
673    SUBDIR=$(get_toolchain_install_subdir $TOOLCHAIN $HOST_TAG)
674    dump "Packaging $ARCHIVE"
675  # exlude ld.mcld
676    EXCLUSIONS=
677    if [ -f $SUBDIR/bin/$ABI_CONFIGURE_TARGET-ld.mcld${HOST_EXE} ] ; then
678        EXCLUSIONS=$EXCLUSIONS" --exclude=$SUBDIR/bin/$ABI_CONFIGURE_TARGET-ld.mcld${HOST_EXE}"
679    fi
680    if [ -f $SUBDIR/$ABI_CONFIGURE_TARGET/bin/ld.mcld${HOST_EXE} ] ; then
681        EXCLUSIONS=$EXCLUSIONS" --exclude=$SUBDIR/$ABI_CONFIGURE_TARGET/bin/ld.mcld${HOST_EXE}"
682    fi
683    pack_archive "$PACKAGE_DIR/$ARCHIVE" "$NDK_DIR" "$SUBDIR" $EXCLUSIONS
684    # package libgccunwind.a
685    if [ "$HOST_OS" = "linux" -a "$GCC_VERSION" = "$DEFAULT_GCC_VERSION" ]; then
686        ABIS=$(commas_to_spaces $(convert_archs_to_abis $ARCH))
687        for ABI in $ABIS; do
688            FILES="$GCCUNWIND_SUBDIR/libs/$ABI/libgccunwind.a"
689            PACKAGE="$PACKAGE_DIR/libgccunwind-libs-$ABI.tar.bz2"
690            log "Packaging: $PACKAGE"
691            pack_archive "$PACKAGE" "$NDK_DIR" "$FILES"
692            fail_panic "Could not package $ABI libgccunwind binaries!"
693            dump "Packaging: $PACKAGE"
694        done
695    fi
696fi
697
698dump "Done."
699if [ -z "$OPTION_BUILD_OUT" ] ; then
700    rm -rf $BUILD_OUT
701fi
702