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 49register_var_option "--binutils-version=<version>" BINUTILS_VERSION "Specify binutils version" 50 51GMP_VERSION=$DEFAULT_GMP_VERSION 52register_var_option "--gmp-version=<version>" GMP_VERSION "Specify gmp version" 53 54MPFR_VERSION=$DEFAULT_MPFR_VERSION 55register_var_option "--mpfr-version=<version>" MPFR_VERSION "Specify mpfr version" 56 57MPC_VERSION=$DEFAULT_MPC_VERSION 58register_var_option "--mpc-version=<version>" MPC_VERSION "Specify mpc version" 59 60PACKAGE_DIR= 61register_var_option "--package-dir=<path>" PACKAGE_DIR "Create archive tarball in specific directory" 62 63register_jobs_option 64register_mingw_option 65register_try64_option 66 67extract_parameters "$@" 68 69prepare_mingw_toolchain /tmp/ndk-$USER/build 70 71fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory" 72setup_default_log_file $BUILD_OUT/config.log 73 74set_parameters () 75{ 76 SRC_DIR="$1" 77 NDK_DIR="$2" 78 TOOLCHAIN="$3" 79 80 # Check source directory 81 # 82 if [ -z "$SRC_DIR" ] ; then 83 echo "ERROR: Missing source directory parameter. See --help for details." 84 exit 1 85 fi 86 87 if [ ! -d "$SRC_DIR/gcc" ] ; then 88 echo "ERROR: Source directory does not contain gcc sources: $SRC_DIR" 89 exit 1 90 fi 91 92 log "Using source directory: $SRC_DIR" 93 94 # Check NDK installation directory 95 # 96 if [ -z "$NDK_DIR" ] ; then 97 echo "ERROR: Missing NDK directory parameter. See --help for details." 98 exit 1 99 fi 100 101 if [ ! -d "$NDK_DIR" ] ; then 102 mkdir -p $NDK_DIR 103 if [ $? != 0 ] ; then 104 echo "ERROR: Could not create target NDK installation path: $NDK_DIR" 105 exit 1 106 fi 107 fi 108 109 log "Using NDK directory: $NDK_DIR" 110 111 # Check toolchain name 112 # 113 if [ -z "$TOOLCHAIN" ] ; then 114 echo "ERROR: Missing toolchain name parameter. See --help for details." 115 exit 1 116 fi 117} 118 119set_parameters $PARAMETERS 120 121prepare_target_build 122 123parse_toolchain_name $TOOLCHAIN 124 125fix_sysroot "$OPTION_SYSROOT" 126 127check_toolchain_src_dir "$SRC_DIR" 128 129if [ ! -d $SRC_DIR/gdb/gdb-$GDB_VERSION ] ; then 130 echo "ERROR: Missing gdb sources: $SRC_DIR/gdb/gdb-$GDB_VERSION" 131 echo " Use --gdb-version=<version> to specify alternative." 132 exit 1 133fi 134 135fix_option BINUTILS_VERSION "$OPTION_BINUTILS_VERSION" "binutils version" 136 137# Force MIPS to use binutils 2.21 138if [ "$ARCH" = "mips" ]; then 139 echo "However, MIPS needs to use BINUTILS 2.21" 140 BINUTILS_VERSION=2.21 141fi 142 143if [ ! -d $SRC_DIR/binutils/binutils-$BINUTILS_VERSION ] ; then 144 echo "ERROR: Missing binutils sources: $SRC_DIR/binutils/binutils-$BINUTILS_VERSION" 145 echo " Use --binutils-version=<version> to specify alternative." 146 exit 1 147fi 148 149fix_option MPFR_VERSION "$OPTION_MPFR_VERSION" "mpfr version" 150if [ ! -f $SRC_DIR/mpfr/mpfr-$MPFR_VERSION.tar.bz2 ] ; then 151 echo "ERROR: Missing mpfr sources: $SRC_DIR/mpfr/mpfr-$MPFR_VERSION.tar.bz2" 152 echo " Use --mpfr-version=<version> to specify alternative." 153 exit 1 154fi 155 156if [ "$PACKAGE_DIR" ]; then 157 mkdir -p "$PACKAGE_DIR" 158 fail_panic "Could not create package directory: $PACKAGE_DIR" 159fi 160 161set_toolchain_ndk $NDK_DIR $TOOLCHAIN 162 163dump "Using C compiler: $CC" 164dump "Using C++ compiler: $CXX" 165 166# Location where the toolchain license files are 167TOOLCHAIN_LICENSES=$ANDROID_NDK_ROOT/build/tools/toolchain-licenses 168 169# Copy the sysroot to the installation prefix. This prevents the generated 170# binaries from containing hard-coding host paths 171TOOLCHAIN_SYSROOT=$TOOLCHAIN_PATH/sysroot 172dump "Sysroot : Copying: $SYSROOT --> $TOOLCHAIN_SYSROOT" 173mkdir -p $TOOLCHAIN_SYSROOT && (cd $SYSROOT && tar ch *) | (cd $TOOLCHAIN_SYSROOT && tar x) 174if [ $? != 0 ] ; then 175 echo "Error while copying sysroot files. See $TMPLOG" 176 exit 1 177fi 178 179# For x86, we currently need to force the usage of Android-specific C runtime 180# object files to generate a few target binaries. Ideally, this should be directly 181# handled by the GCC configuration scripts, just like with ARM. 182# 183if [ "$ARCH" = "x86" ]; then 184 ABI_LDFLAGS_FOR_TARGET=" -nostartfiles $TOOLCHAIN_SYSROOT/usr/lib/crtbegin_dynamic.o $TOOLCHAIN_SYSROOT/usr/lib/crtend_android.o" 185 dump "Forcing -nostartfiles: $ABI_LDFLAGS_FOR_TARGET" 186fi 187 188# configure the toolchain 189# 190dump "Configure: $TOOLCHAIN toolchain build" 191# Old versions of the toolchain source packages placed the 192# configure script at the top-level. Newer ones place it under 193# the build directory though. Probe the file system to check 194# this. 195BUILD_SRCDIR=$SRC_DIR/build 196if [ ! -d $BUILD_SRCDIR ] ; then 197 BUILD_SRCDIR=$SRC_DIR 198fi 199rm -rf $BUILD_OUT 200OLD_ABI="${ABI}" 201export CC CXX 202export CFLAGS_FOR_TARGET="$ABI_CFLAGS_FOR_TARGET" 203export CXXFLAGS_FOR_TARGET="$ABI_CXXFLAGS_FOR_TARGET" 204export LDFLAGS_FOR_TARGET="$ABI_LDFLAGS_FOR_TARGET" 205# Needed to build a 32-bit gmp on 64-bit systems 206export ABI=$HOST_GMP_ABI 207# -Wno-error is needed because our gdb-6.6 sources use -Werror by default 208# and fail to build with recent GCC versions. 209export CFLAGS="-Wno-error" 210 211# This extra flag is used to slightly speed up the build 212EXTRA_CONFIG_FLAGS="--disable-bootstrap" 213 214# This is to disable GCC 4.6 specific features that don't compile well 215# the flags are ignored for older GCC versions. 216EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-libquadmath --disable-plugin" 217 218#export LDFLAGS="$HOST_LDFLAGS" 219mkdir -p $BUILD_OUT && cd $BUILD_OUT && run \ 220$BUILD_SRCDIR/configure --target=$ABI_CONFIGURE_TARGET \ 221 --enable-initfini-array \ 222 --host=$ABI_CONFIGURE_HOST \ 223 --build=$ABI_CONFIGURE_BUILD \ 224 --disable-nls \ 225 --prefix=$TOOLCHAIN_PATH \ 226 --with-sysroot=$TOOLCHAIN_SYSROOT \ 227 --with-binutils-version=$BINUTILS_VERSION \ 228 --with-mpfr-version=$MPFR_VERSION \ 229 --with-mpc-version=$MPC_VERSION \ 230 --with-gmp-version=$GMP_VERSION \ 231 --with-gcc-version=$GCC_VERSION \ 232 --with-gdb-version=$GDB_VERSION \ 233 $EXTRA_CONFIG_FLAGS \ 234 $ABI_CONFIGURE_EXTRA_FLAGS 235if [ $? != 0 ] ; then 236 dump "Error while trying to configure toolchain build. See $TMPLOG" 237 exit 1 238fi 239ABI="$OLD_ABI" 240# build the toolchain 241dump "Building : $TOOLCHAIN toolchain [this can take a long time]." 242cd $BUILD_OUT && 243export CC CXX && 244export ABI=$HOST_GMP_ABI && 245run make -j$NUM_JOBS 246if [ $? != 0 ] ; then 247 # Unfortunately, there is a bug in the GCC build scripts that prevent 248 # parallel mingw builds to work properly on some multi-core machines 249 # (but not all, sounds like a race condition). Detect this and restart 250 # in single-process mode! 251 if [ "$MINGW" = "yes" ] ; then 252 dump "Parallel mingw build failed - continuing in single process mode!" 253 run make -j1 254 if [ $? != 0 ] ; then 255 echo "Error while building mingw toolchain. See $TMPLOG" 256 exit 1 257 fi 258 else 259 echo "Error while building toolchain. See $TMPLOG" 260 exit 1 261 fi 262fi 263ABI="$OLD_ABI" 264 265# install the toolchain to its final location 266dump "Install : $TOOLCHAIN toolchain binaries." 267cd $BUILD_OUT && run make install 268if [ $? != 0 ] ; then 269 echo "Error while installing toolchain. See $TMPLOG" 270 exit 1 271fi 272# don't forget to copy the GPL and LGPL license files 273run cp -f $TOOLCHAIN_LICENSES/COPYING $TOOLCHAIN_LICENSES/COPYING.LIB $TOOLCHAIN_PATH 274 275# remove some unneeded files 276run rm -f $TOOLCHAIN_PATH/bin/*-gccbug 277run rm -rf $TOOLCHAIN_PATH/info 278run rm -rf $TOOLCHAIN_PATH/man 279run rm -rf $TOOLCHAIN_PATH/share 280run rm -rf $TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/*/install-tools 281run rm -rf $TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/*/plugin 282run rm -rf $TOOLCHAIN_PATH/libexec/gcc/$ABI_CONFIGURE_TARGET/*/install-tools 283run rm -rf $TOOLCHAIN_PATH/lib32/libiberty.a 284run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/libiberty.a 285run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/libiberty.a 286run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/*/libiberty.a 287 288# Remove libstdc++ for now (will add it differently later) 289# We had to build it to get libsupc++ which we keep. 290run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/libstdc++.* 291run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/libstdc++.* 292run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/include/c++ 293 294# strip binaries to reduce final package size 295run strip $TOOLCHAIN_PATH/bin/* 296run strip $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin/* 297run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1$HOST_EXE 298run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1plus$HOST_EXE 299run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/collect2$HOST_EXE 300 301# copy SOURCES file if present 302if [ -f "$SRC_DIR/SOURCES" ]; then 303 cp "$SRC_DIR/SOURCES" "$TOOLCHAIN_PATH/SOURCES" 304fi 305 306if [ "$PACKAGE_DIR" ]; then 307 ARCHIVE="$TOOLCHAIN-$HOST_TAG.tar.bz2" 308 SUBDIR=$(get_toolchain_install_subdir $TOOLCHAIN $HOST_TAG) 309 dump "Packaging $ARCHIVE" 310 pack_archive "$PACKAGE_DIR/$ARCHIVE" "$NDK_DIR" "$SUBDIR" 311fi 312 313dump "Done." 314if [ -z "$OPTION_BUILD_OUT" ] ; then 315 rm -rf $BUILD_OUT 316fi 317