• 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.4.3)."
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
46register_var_option "--gdb-version=<version>"  GDB_VERSION "Specify gdb version"
47
48BINUTILS_VERSION=$DEFAULT_BINUTILS_VERSION
49EXPLICIT_BINUTILS_VERSION=
50register_option "--binutils-version=<version>" do_binutils_version "Specify binutils version" "$BINUTILS_VERSION"
51do_binutils_version () {
52    BINUTILS_VERSION=$1
53    EXPLICIT_BINUTILS_VERSION=true
54}
55
56GMP_VERSION=$DEFAULT_GMP_VERSION
57register_var_option "--gmp-version=<version>" GMP_VERSION "Specify gmp version"
58
59MPFR_VERSION=$DEFAULT_MPFR_VERSION
60register_var_option "--mpfr-version=<version>" MPFR_VERSION "Specify mpfr version"
61
62MPC_VERSION=$DEFAULT_MPC_VERSION
63register_var_option "--mpc-version=<version>" MPC_VERSION "Specify mpc version"
64
65PACKAGE_DIR=
66register_var_option "--package-dir=<path>" PACKAGE_DIR "Create archive tarball in specific directory"
67
68register_jobs_option
69register_mingw_option
70register_try64_option
71
72extract_parameters "$@"
73
74prepare_mingw_toolchain /tmp/ndk-$USER/build
75
76fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory"
77setup_default_log_file $BUILD_OUT/config.log
78
79set_parameters ()
80{
81    SRC_DIR="$1"
82    NDK_DIR="$2"
83    TOOLCHAIN="$3"
84
85    # Check source directory
86    #
87    if [ -z "$SRC_DIR" ] ; then
88        echo "ERROR: Missing source directory parameter. See --help for details."
89        exit 1
90    fi
91
92    if [ ! -d "$SRC_DIR/gcc" ] ; then
93        echo "ERROR: Source directory does not contain gcc sources: $SRC_DIR"
94        exit 1
95    fi
96
97    log "Using source directory: $SRC_DIR"
98
99    # Check NDK installation directory
100    #
101    if [ -z "$NDK_DIR" ] ; then
102        echo "ERROR: Missing NDK directory parameter. See --help for details."
103        exit 1
104    fi
105
106    if [ ! -d "$NDK_DIR" ] ; then
107        mkdir -p $NDK_DIR
108        if [ $? != 0 ] ; then
109            echo "ERROR: Could not create target NDK installation path: $NDK_DIR"
110            exit 1
111        fi
112    fi
113
114    log "Using NDK directory: $NDK_DIR"
115
116    # Check toolchain name
117    #
118    if [ -z "$TOOLCHAIN" ] ; then
119        echo "ERROR: Missing toolchain name parameter. See --help for details."
120        exit 1
121    fi
122}
123
124set_parameters $PARAMETERS
125
126prepare_target_build
127
128parse_toolchain_name $TOOLCHAIN
129
130fix_sysroot "$OPTION_SYSROOT"
131
132check_toolchain_src_dir "$SRC_DIR"
133
134if [ ! -d $SRC_DIR/gdb/gdb-$GDB_VERSION ] ; then
135    echo "ERROR: Missing gdb sources: $SRC_DIR/gdb/gdb-$GDB_VERSION"
136    echo "       Use --gdb-version=<version> to specify alternative."
137    exit 1
138fi
139
140if [ -z "$EXPLICIT_BINUTILS_VERSION" ]; then
141    BINUTILS_VERSION=$(get_default_binutils_version_for_gcc $TOOLCHAIN)
142    dump "Auto-config: --binutils-version=$BINUTILS_VERSION"
143fi
144
145if [ ! -d $SRC_DIR/binutils/binutils-$BINUTILS_VERSION ] ; then
146    echo "ERROR: Missing binutils sources: $SRC_DIR/binutils/binutils-$BINUTILS_VERSION"
147    echo "       Use --binutils-version=<version> to specify alternative."
148    exit 1
149fi
150
151fix_option MPFR_VERSION "$OPTION_MPFR_VERSION" "mpfr version"
152if [ ! -f $SRC_DIR/mpfr/mpfr-$MPFR_VERSION.tar.bz2 ] ; then
153    echo "ERROR: Missing mpfr sources: $SRC_DIR/mpfr/mpfr-$MPFR_VERSION.tar.bz2"
154    echo "       Use --mpfr-version=<version> to specify alternative."
155    exit 1
156fi
157
158if [ "$PACKAGE_DIR" ]; then
159    mkdir -p "$PACKAGE_DIR"
160    fail_panic "Could not create package directory: $PACKAGE_DIR"
161fi
162
163set_toolchain_ndk $NDK_DIR $TOOLCHAIN
164
165dump "Using C compiler: $CC"
166dump "Using C++ compiler: $CXX"
167
168rm -rf $BUILD_OUT
169mkdir -p $BUILD_OUT
170
171# Location where the toolchain license files are
172TOOLCHAIN_LICENSES=$ANDROID_NDK_ROOT/build/tools/toolchain-licenses
173
174# Without option "--sysroot" (and its variations), GCC will attempt to
175# search path specified by "--with-sysroot" at build time for headers/libs.
176# Path at --with-sysroot contains minimal headers and libs to boostrap
177# toolchain build, and it's not needed afterward (NOTE: NDK provides
178# sysroot at specified API level,and Android build explicit lists header/lib
179# dependencies.
180#
181# It's better to point --with-sysroot to local directory otherwise the
182# path may be found at compile-time and bad things can happen: eg.
183#  1) The path exists and contain incorrect headers/libs
184#  2) The path exists at remote server and blocks GCC for seconds
185#  3) The path exists but not accessible, which crashes GCC!
186#
187# For canadian build --with-sysroot has to be sub-directory of --prefix.
188# Put TOOLCHAIN_BUILD_PREFIX to BUILD_OUT which is in /tmp by default,
189# and TOOLCHAIN_BUILD_SYSROOT underneath.
190
191TOOLCHAIN_BUILD_PREFIX=$BUILD_OUT/prefix
192TOOLCHAIN_BUILD_SYSROOT=$TOOLCHAIN_BUILD_PREFIX/sysroot
193dump "Sysroot  : Copying: $SYSROOT --> $TOOLCHAIN_BUILD_SYSROOT"
194mkdir -p $TOOLCHAIN_BUILD_SYSROOT && (cd $SYSROOT && tar ch *) | (cd $TOOLCHAIN_BUILD_SYSROOT && tar x)
195if [ $? != 0 ] ; then
196    echo "Error while copying sysroot files. See $TMPLOG"
197    exit 1
198fi
199
200# configure the toolchain
201#
202dump "Configure: $TOOLCHAIN toolchain build"
203# Old versions of the toolchain source packages placed the
204# configure script at the top-level. Newer ones place it under
205# the build directory though. Probe the file system to check
206# this.
207BUILD_SRCDIR=$SRC_DIR/build
208if [ ! -d $BUILD_SRCDIR ] ; then
209    BUILD_SRCDIR=$SRC_DIR
210fi
211OLD_ABI="${ABI}"
212export CC CXX
213export CFLAGS_FOR_TARGET="$ABI_CFLAGS_FOR_TARGET"
214export CXXFLAGS_FOR_TARGET="$ABI_CXXFLAGS_FOR_TARGET"
215# Needed to build a 32-bit gmp on 64-bit systems
216export ABI=$HOST_GMP_ABI
217# -Wno-error is needed because our gdb-6.6 sources use -Werror by default
218# and fail to build with recent GCC versions.
219export CFLAGS="-Wno-error"
220
221# This extra flag is used to slightly speed up the build
222EXTRA_CONFIG_FLAGS="--disable-bootstrap"
223
224# This is to disable GCC 4.6 specific features that don't compile well
225# the flags are ignored for older GCC versions.
226EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-libquadmath --disable-plugin"
227
228# Enable Gold as default
229case "$TOOLCHAIN" in
230    # Note that only ARM and X86 are supported
231    x86-4.6|arm-linux-androideabi-4.6)
232        EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --enable-gold=default"
233    ;;
234esac
235
236#export LDFLAGS="$HOST_LDFLAGS"
237cd $BUILD_OUT && run \
238$BUILD_SRCDIR/configure --target=$ABI_CONFIGURE_TARGET \
239                        --enable-initfini-array \
240                        --host=$ABI_CONFIGURE_HOST \
241                        --build=$ABI_CONFIGURE_BUILD \
242                        --disable-nls \
243                        --prefix=$TOOLCHAIN_BUILD_PREFIX \
244                        --with-sysroot=$TOOLCHAIN_BUILD_SYSROOT \
245                        --with-binutils-version=$BINUTILS_VERSION \
246                        --with-mpfr-version=$MPFR_VERSION \
247                        --with-mpc-version=$MPC_VERSION \
248                        --with-gmp-version=$GMP_VERSION \
249                        --with-gcc-version=$GCC_VERSION \
250                        --with-gdb-version=$GDB_VERSION \
251                        --with-gxx-include-dir=$TOOLCHAIN_BUILD_PREFIX/include/c++/$GCC_VERSION \
252                        $EXTRA_CONFIG_FLAGS \
253                        $ABI_CONFIGURE_EXTRA_FLAGS
254if [ $? != 0 ] ; then
255    dump "Error while trying to configure toolchain build. See $TMPLOG"
256    exit 1
257fi
258ABI="$OLD_ABI"
259# build the toolchain
260dump "Building : $TOOLCHAIN toolchain [this can take a long time]."
261cd $BUILD_OUT
262export CC CXX
263export ABI=$HOST_GMP_ABI
264JOBS=$NUM_JOBS
265
266while [ -n "1" ]; do
267    run make -j$JOBS
268    if [ $? = 0 ] ; then
269        break
270    else
271        if [ "$MINGW" = "yes" ] ; then
272            # Unfortunately, there is a bug in the GCC build scripts that prevent
273            # parallel mingw builds to work properly on some multi-core machines
274            # (but not all, sounds like a race condition). Detect this and restart
275            # in less parallelism, until -j1 also fail
276            JOBS=$((JOBS/2))
277            if [ $JOBS -lt 1 ] ; then
278                echo "Error while building mingw toolchain. See $TMPLOG"
279                exit 1
280            fi
281            dump "Parallel mingw build failed - continuing in less parallelism -j$JOBS"
282        else
283            echo "Error while building toolchain. See $TMPLOG"
284            exit 1
285        fi
286    fi
287done
288
289ABI="$OLD_ABI"
290
291# install the toolchain to its final location
292dump "Install  : $TOOLCHAIN toolchain binaries."
293cd $BUILD_OUT && run make install
294if [ $? != 0 ] ; then
295    echo "Error while installing toolchain. See $TMPLOG"
296    exit 1
297fi
298
299# copy to toolchain path
300run copy_directory "$TOOLCHAIN_BUILD_PREFIX" "$TOOLCHAIN_PATH"
301
302# don't forget to copy the GPL and LGPL license files
303run cp -f $TOOLCHAIN_LICENSES/COPYING $TOOLCHAIN_LICENSES/COPYING.LIB $TOOLCHAIN_PATH
304
305# remove some unneeded files
306run rm -f $TOOLCHAIN_PATH/bin/*-gccbug
307run rm -rf $TOOLCHAIN_PATH/info
308run rm -rf $TOOLCHAIN_PATH/man
309run rm -rf $TOOLCHAIN_PATH/share
310run rm -rf $TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/*/install-tools
311run rm -rf $TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/*/plugin
312run rm -rf $TOOLCHAIN_PATH/libexec/gcc/$ABI_CONFIGURE_TARGET/*/install-tools
313run rm -rf $TOOLCHAIN_PATH/lib32/libiberty.a
314run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/libiberty.a
315run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/libiberty.a
316run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/*/libiberty.a
317
318# Remove libstdc++ for now (will add it differently later)
319# We had to build it to get libsupc++ which we keep.
320run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/libstdc++.*
321run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/libstdc++.*
322run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/include/c++
323
324# strip binaries to reduce final package size
325run strip $TOOLCHAIN_PATH/bin/*
326run strip $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin/*
327run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1$HOST_EXE
328run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1plus$HOST_EXE
329run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/collect2$HOST_EXE
330
331# copy SOURCES file if present
332if [ -f "$SRC_DIR/SOURCES" ]; then
333    cp "$SRC_DIR/SOURCES" "$TOOLCHAIN_PATH/SOURCES"
334fi
335
336if [ "$PACKAGE_DIR" ]; then
337    ARCHIVE="$TOOLCHAIN-$HOST_TAG.tar.bz2"
338    SUBDIR=$(get_toolchain_install_subdir $TOOLCHAIN $HOST_TAG)
339    dump "Packaging $ARCHIVE"
340    pack_archive "$PACKAGE_DIR/$ARCHIVE" "$NDK_DIR" "$SUBDIR"
341fi
342
343dump "Done."
344if [ -z "$OPTION_BUILD_OUT" ] ; then
345    rm -rf $BUILD_OUT
346fi
347