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-eabi-4.4.0)." 32 33 RELEASE=`date +%Y%m%d` 34 BUILD_OUT=`random_temp_directory` 35 OPTION_BUILD_OUT= 36 register_var_option "--build-out=<path>" OPTION_BUILD_OUT "Set temporary build directory" "/tmp/<random>" 37 38 # Note: platform API level 9 or higher is needed for proper C++ support 39 PLATFORM=android-9 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=6.6 46 register_var_option "--gdb-version=<version>" GDB_VERSION "Specify gdb version" 47 48 BINUTILS_VERSION=2.19 49 register_var_option "--binutils-version=<version>" BINUTILS_VERSION "Specify binutils version" 50 51 JOBS=$BUILD_NUM_CPUS 52 register_var_option "-j<number>" JOBS "Use <number> parallel build jobs" 53 54 COPY_LIBSTDCXX=no 55 register_var_option "--copy-libstdcxx" COPY_LIBSTDCXX "Copy libstdc++ to <ndk-dir>/$GNUSTL_SUBDIR" 56 57 KEEP_LIBSTDCXX=no 58 register_var_option "--keep-libstdcxx" KEEP_LIBSTDCXX "Experimental: keep libstdc++ inside toolchain" 59 60 register_mingw_option 61 62 extract_parameters "$@" 63 64 setup_default_log_file 65 66 set_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 111 set_parameters $PARAMETERS 112 113 prepare_host_flags 114 115 parse_toolchain_name 116 117 fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory" 118 fix_sysroot "$OPTION_SYSROOT" 119 120 if [ ! -d $SRC_DIR/gdb/gdb-$GDB_VERSION ] ; then 121 echo "ERROR: Missing gdb sources: $SRC_DIR/gdb/gdb-$GDB_VERSION" 122 echo " Use --gdb-version=<version> to specify alternative." 123 exit 1 124 fi 125 126 fix_option BINUTILS_VERSION "$OPTION_BINUTILS_VERSION" "binutils version" 127 if [ ! -d $SRC_DIR/binutils/binutils-$BINUTILS_VERSION ] ; then 128 echo "ERROR: Missing binutils sources: $SRC_DIR/binutils/binutils-$BINUTILS_VERSION" 129 echo " Use --binutils-version=<version> to specify alternative." 130 exit 1 131 fi 132 133 set_toolchain_ndk $NDK_DIR 134 135 dump "Using C compiler: $CC" 136 dump "Using C++ compiler: $CXX" 137 138 # Location where the toolchain license files are 139 TOOLCHAIN_LICENSES=$ANDROID_NDK_ROOT/build/tools/toolchain-licenses 140 141 # Copy the sysroot to the installation prefix. This prevents the generated 142 # binaries from containing hard-coding host paths 143 TOOLCHAIN_SYSROOT=$TOOLCHAIN_PATH/sysroot 144 dump "Sysroot : Copying: $SYSROOT --> $TOOLCHAIN_SYSROOT" 145 mkdir -p $TOOLCHAIN_SYSROOT && (cd $SYSROOT && tar ch *) | (cd $TOOLCHAIN_SYSROOT && tar x) 146 if [ $? != 0 ] ; then 147 echo "Error while copying sysroot files. See $TMPLOG" 148 exit 1 149 fi 150 151 # configure the toolchain 152 # 153 dump "Configure: $TOOLCHAIN toolchain build" 154 # Old versions of the toolchain source packages placed the 155 # configure script at the top-level. Newer ones place it under 156 # the build directory though. Probe the file system to check 157 # this. 158 BUILD_SRCDIR=$SRC_DIR/build 159 if [ ! -d $BUILD_SRCDIR ] ; then 160 BUILD_SRCDIR=$SRC_DIR 161 fi 162 rm -rf $BUILD_OUT 163 OLD_ABI="${ABI}" 164 export CC CXX 165 export CFLAGS_FOR_TARGET="$ABI_CFLAGS_FOR_TARGET" 166 export CXXFLAGS_FOR_TARGET="$ABI_CXXFLAGS_FOR_TARGET" 167 # Needed to build a 32-bit gmp on 64-bit systems 168 export ABI=$HOST_GMP_ABI 169 # -Wno-error is needed because our gdb-6.6 sources use -Werror by default 170 # and fail to build with recent GCC versions. 171 export CFLAGS="-Wno-error" 172 #export LDFLAGS="$HOST_LDFLAGS" 173 mkdir -p $BUILD_OUT && cd $BUILD_OUT && run \ 174 $BUILD_SRCDIR/configure --target=$ABI_CONFIGURE_TARGET \ 175 --host=$ABI_CONFIGURE_HOST \ 176 --build=$ABI_CONFIGURE_BUILD \ 177 --disable-nls \ 178 --prefix=$TOOLCHAIN_PATH \ 179 --with-sysroot=$TOOLCHAIN_SYSROOT \ 180 --with-binutils-version=$BINUTILS_VERSION \ 181 --with-gcc-version=$GCC_VERSION \ 182 --with-gdb-version=$GDB_VERSION \ 183 $ABI_CONFIGURE_EXTRA_FLAGS 184 if [ $? != 0 ] ; then 185 dump "Error while trying to configure toolchain build. See $TMPLOG" 186 exit 1 187 fi 188 ABI="$OLD_ABI" 189 # build the toolchain 190 dump "Building : $TOOLCHAIN toolchain [this can take a long time]." 191 cd $BUILD_OUT && 192 export CC CXX && 193 export ABI=$HOST_GMP_ABI && 194 run make -j$JOBS 195 if [ $? != 0 ] ; then 196 # Unfortunately, there is a bug in the GCC build scripts that prevent 197 # parallel mingw builds to work properly on some multi-core machines 198 # (but not all, sounds like a race condition). Detect this and restart 199 # in single-process mode! 200 if [ "$MINGW" = "yes" ] ; then 201 dump "Parallel mingw build failed - continuing in single process mode!" 202 run make -j1 203 if [ $? != 0 ] ; then 204 echo "Error while building mingw toolchain. See $TMPLOG" 205 exit 1 206 fi 207 else 208 echo "Error while building toolchain. See $TMPLOG" 209 exit 1 210 fi 211 fi 212 ABI="$OLD_ABI" 213 214 # install the toolchain to its final location 215 dump "Install : $TOOLCHAIN toolchain binaries." 216 cd $BUILD_OUT && run make install 217 if [ $? != 0 ] ; then 218 echo "Error while installing toolchain. See $TMPLOG" 219 exit 1 220 fi 221 # don't forget to copy the GPL and LGPL license files 222 run cp -f $TOOLCHAIN_LICENSES/COPYING $TOOLCHAIN_LICENSES/COPYING.LIB $TOOLCHAIN_PATH 223 224 # remove some unneeded files 225 run rm -f $TOOLCHAIN_PATH/bin/*-gccbug 226 run rm -rf $TOOLCHAIN_PATH/info 227 run rm -rf $TOOLCHAIN_PATH/man 228 run rm -rf $TOOLCHAIN_PATH/share 229 run rm -rf $TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/*/install-tools 230 run rm -rf $TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/*/plugin 231 run rm -rf $TOOLCHAIN_PATH/libexec/gcc/$ABI_CONFIGURE_TARGET/*/install-tools 232 run rm -rf $TOOLCHAIN_PATH/lib32/libiberty.a 233 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/libiberty.a 234 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/libiberty.a 235 236 # Copy libstdc++ headers and libraries if needed 237 if [ "$COPY_LIBSTDCXX" = "yes" ] ; then 238 dump "Copying libstdc++ prebuild binaries." 239 $ANDROID_NDK_ROOT/build/tools/copy-libstdcxx.sh "$TOOLCHAIN_PATH" "$NDK_DIR" --toolchain=$TOOLCHAIN 240 fi 241 242 # Remove libstdc++ for now (will add it differently later) 243 # We had to build it to get libsupc++ which we keep. 244 if [ "$KEEP_LIBSTDCXX" = "no" ] ; then 245 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/libstdc++.* 246 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/lib/*/libstdc++.* 247 run rm -rf $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/include/c++ 248 fi 249 250 # strip binaries to reduce final package size 251 run strip $TOOLCHAIN_PATH/bin/* 252 run strip $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin/* 253 run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1$HOST_EXE 254 run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1plus$HOST_EXE 255 run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/collect2$HOST_EXE 256 257 dump "Done." 258 if [ -z "$OPTION_BUILD_OUT" ] ; then 259 rm -rf $BUILD_OUT 260 fi 261