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 24 PROGRAM_PARAMETERS="<src-dir> <ndk-dir> <toolchain>" 25 26 PROGRAM_DESCRIPTION=\ 27 "Rebuild the gcc toolchain prebuilt binaries for the Android NDK. 28 29 Where <src-dir> is the location of toolchain sources, <ndk-dir> is 30 the top-level NDK installation path and <toolchain> is the name of 31 the toolchain to use (e.g. arm-linux-androideabi-4.4.3)." 32 33 RELEASE=`date +%Y%m%d` 34 BUILD_OUT=/tmp/ndk-$USER/build/toolchain 35 OPTION_BUILD_OUT= 36 register_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 39 PLATFORM=$DEFAULT_PLATFORM 40 register_var_option "--platform=<name>" PLATFORM "Specify platform name" 41 42 OPTION_SYSROOT= 43 register_var_option "--sysroot=<path>" OPTION_SYSROOT "Specify sysroot directory directly" 44 45 GDB_VERSION=$DEFAULT_GDB_VERSION 46 register_var_option "--gdb-version=<version>" GDB_VERSION "Specify gdb version" 47 48 BINUTILS_VERSION=$DEFAULT_BINUTILS_VERSION 49 register_var_option "--binutils-version=<version>" BINUTILS_VERSION "Specify binutils version" 50 51 GMP_VERSION=$DEFAULT_GMP_VERSION 52 register_var_option "--gmp-version=<version>" GMP_VERSION "Specify gmp version" 53 54 MPFR_VERSION=$DEFAULT_MPFR_VERSION 55 register_var_option "--mpfr-version=<version>" MPFR_VERSION "Specify mpfr version" 56 57 MPC_VERSION=$DEFAULT_MPC_VERSION 58 register_var_option "--mpc-version=<version>" MPC_VERSION "Specify mpc version" 59 60 PACKAGE_DIR= 61 register_var_option "--package-dir=<path>" PACKAGE_DIR "Create archive tarball in specific directory" 62 63 register_jobs_option 64 register_mingw_option 65 register_try64_option 66 67 extract_parameters "$@" 68 69 prepare_mingw_toolchain /tmp/ndk-$USER/build 70 71 fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory" 72 setup_default_log_file $BUILD_OUT/config.log 73 74 set_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 119 set_parameters $PARAMETERS 120 121 prepare_target_build 122 123 parse_toolchain_name $TOOLCHAIN 124 125 fix_sysroot "$OPTION_SYSROOT" 126 127 check_toolchain_src_dir "$SRC_DIR" 128 129 if [ ! -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 133 fi 134 135 fix_option BINUTILS_VERSION "$OPTION_BINUTILS_VERSION" "binutils version" 136 137 # Force MIPS to use binutils 2.21 138 if [ "$ARCH" = "mips" ]; then 139 echo "However, MIPS needs to use BINUTILS 2.21" 140 BINUTILS_VERSION=2.21 141 fi 142 143 if [ ! -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 147 fi 148 149 fix_option MPFR_VERSION "$OPTION_MPFR_VERSION" "mpfr version" 150 if [ ! -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 154 fi 155 156 if [ "$PACKAGE_DIR" ]; then 157 mkdir -p "$PACKAGE_DIR" 158 fail_panic "Could not create package directory: $PACKAGE_DIR" 159 fi 160 161 set_toolchain_ndk $NDK_DIR $TOOLCHAIN 162 163 dump "Using C compiler: $CC" 164 dump "Using C++ compiler: $CXX" 165 166 # Location where the toolchain license files are 167 TOOLCHAIN_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 171 TOOLCHAIN_SYSROOT=$TOOLCHAIN_PATH/sysroot 172 dump "Sysroot : Copying: $SYSROOT --> $TOOLCHAIN_SYSROOT" 173 mkdir -p $TOOLCHAIN_SYSROOT && (cd $SYSROOT && tar ch *) | (cd $TOOLCHAIN_SYSROOT && tar x) 174 if [ $? != 0 ] ; then 175 echo "Error while copying sysroot files. See $TMPLOG" 176 exit 1 177 fi 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 # 183 if [ "$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" 186 fi 187 188 # configure the toolchain 189 # 190 dump "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. 195 BUILD_SRCDIR=$SRC_DIR/build 196 if [ ! -d $BUILD_SRCDIR ] ; then 197 BUILD_SRCDIR=$SRC_DIR 198 fi 199 rm -rf $BUILD_OUT 200 OLD_ABI="${ABI}" 201 export CC CXX 202 export CFLAGS_FOR_TARGET="$ABI_CFLAGS_FOR_TARGET" 203 export CXXFLAGS_FOR_TARGET="$ABI_CXXFLAGS_FOR_TARGET" 204 export LDFLAGS_FOR_TARGET="$ABI_LDFLAGS_FOR_TARGET" 205 # Needed to build a 32-bit gmp on 64-bit systems 206 export 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. 209 export CFLAGS="-Wno-error" 210 211 # This extra flag is used to slightly speed up the build 212 EXTRA_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. 216 EXTRA_CONFIG_FLAGS=$EXTRA_CONFIG_FLAGS" --disable-libquadmath --disable-plugin" 217 218 #export LDFLAGS="$HOST_LDFLAGS" 219 mkdir -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 235 if [ $? != 0 ] ; then 236 dump "Error while trying to configure toolchain build. See $TMPLOG" 237 exit 1 238 fi 239 ABI="$OLD_ABI" 240 # build the toolchain 241 dump "Building : $TOOLCHAIN toolchain [this can take a long time]." 242 cd $BUILD_OUT && 243 export CC CXX && 244 export ABI=$HOST_GMP_ABI && 245 run make -j$NUM_JOBS 246 if [ $? != 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 262 fi 263 ABI="$OLD_ABI" 264 265 # install the toolchain to its final location 266 dump "Install : $TOOLCHAIN toolchain binaries." 267 cd $BUILD_OUT && run make install 268 if [ $? != 0 ] ; then 269 echo "Error while installing toolchain. See $TMPLOG" 270 exit 1 271 fi 272 # don't forget to copy the GPL and LGPL license files 273 run cp -f $TOOLCHAIN_LICENSES/COPYING $TOOLCHAIN_LICENSES/COPYING.LIB $TOOLCHAIN_PATH 274 275 # remove some unneeded files 276 run rm -f $TOOLCHAIN_PATH/bin/*-gccbug 277 run rm -rf $TOOLCHAIN_PATH/info 278 run rm -rf $TOOLCHAIN_PATH/man 279 run rm -rf $TOOLCHAIN_PATH/share 280 run rm -rf $TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/*/install-tools 281 run rm -rf $TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/*/plugin 282 run rm -rf $TOOLCHAIN_PATH/libexec/gcc/$ABI_CONFIGURE_TARGET/*/install-tools 283 run rm -rf $TOOLCHAIN_PATH/lib32/libiberty.a 284 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/libiberty.a 285 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/libiberty.a 286 run 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. 290 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/libstdc++.* 291 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/libstdc++.* 292 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/include/c++ 293 294 # strip binaries to reduce final package size 295 run strip $TOOLCHAIN_PATH/bin/* 296 run strip $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin/* 297 run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1$HOST_EXE 298 run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1plus$HOST_EXE 299 run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/collect2$HOST_EXE 300 301 # copy SOURCES file if present 302 if [ -f "$SRC_DIR/SOURCES" ]; then 303 cp "$SRC_DIR/SOURCES" "$TOOLCHAIN_PATH/SOURCES" 304 fi 305 306 if [ "$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" 311 fi 312 313 dump "Done." 314 if [ -z "$OPTION_BUILD_OUT" ] ; then 315 rm -rf $BUILD_OUT 316 fi 317