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