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