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