Home | History | Annotate | Download | only in tools
      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 PLATFORM=android-3
     39 register_var_option "--platform=<name>"  PLATFORM "Specify platform name"
     40 
     41 OPTION_SYSROOT=
     42 register_var_option "--sysroot=<path>"   OPTION_SYSROOT   "Specify sysroot directory directly"
     43 
     44 GDB_VERSION=6.6
     45 register_var_option "--gdb-version=<version>"  GDB_VERSION "Specify gdb version"
     46 
     47 BINUTILS_VERSION=2.19
     48 register_var_option "--binutils-version=<version>" BINUTILS_VERSION "Specify binutils version"
     49 
     50 JOBS=$BUILD_NUM_CPUS
     51 register_var_option "-j<number>" JOBS "Use <number> parallel build jobs"
     52 
     53 register_mingw_option
     54 
     55 extract_parameters $@
     56 
     57 setup_default_log_file
     58 
     59 set_parameters ()
     60 {
     61     SRC_DIR="$1"
     62     NDK_DIR="$2"
     63     TOOLCHAIN="$3"
     64 
     65     # Check source directory
     66     #
     67     if [ -z "$SRC_DIR" ] ; then
     68         echo "ERROR: Missing source directory parameter. See --help for details."
     69         exit 1
     70     fi
     71 
     72     if [ ! -d "$SRC_DIR/gcc" ] ; then
     73         echo "ERROR: Source directory does not contain gcc sources: $SRC_DIR"
     74         exit 1
     75     fi
     76 
     77     log "Using source directory: $SRC_DIR"
     78 
     79     # Check NDK installation directory
     80     #
     81     if [ -z "$NDK_DIR" ] ; then
     82         echo "ERROR: Missing NDK directory parameter. See --help for details."
     83         exit 1
     84     fi
     85 
     86     if [ ! -d "$NDK_DIR" ] ; then
     87         mkdir -p $NDK_DIR
     88         if [ $? != 0 ] ; then
     89             echo "ERROR: Could not create target NDK installation path: $NDK_DIR"
     90             exit 1
     91         fi
     92     fi
     93 
     94     log "Using NDK directory: $NDK_DIR"
     95 
     96     # Check toolchain name
     97     #
     98     if [ -z "$TOOLCHAIN" ] ; then
     99         echo "ERROR: Missing toolchain name parameter. See --help for details."
    100         exit 1
    101     fi
    102 }
    103 
    104 set_parameters $PARAMETERS
    105 
    106 prepare_host_flags
    107 
    108 parse_toolchain_name
    109 
    110 fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory"
    111 fix_sysroot "$OPTION_SYSROOT"
    112 
    113 if [ ! -d $SRC_DIR/gdb/gdb-$GDB_VERSION ] ; then
    114     echo "ERROR: Missing gdb sources: $SRC_DIR/gdb/gdb-$GDB_VERSION"
    115     echo "       Use --gdb-version=<version> to specify alternative."
    116     exit 1
    117 fi
    118 
    119 fix_option BINUTILS_VERSION "$OPTION_BINUTILS_VERSION" "binutils version"
    120 if [ ! -d $SRC_DIR/binutils/binutils-$BINUTILS_VERSION ] ; then
    121     echo "ERROR: Missing binutils sources: $SRC_DIR/binutils/binutils-$BINUTILS_VERSION"
    122     echo "       Use --binutils-version=<version> to specify alternative."
    123     exit 1
    124 fi
    125 
    126 set_toolchain_ndk $NDK_DIR
    127 
    128 dump "Using C compiler: $CC"
    129 dump "Using C++ compiler: $CXX"
    130 
    131 # Location where the toolchain license files are
    132 TOOLCHAIN_LICENSES=$ANDROID_NDK_ROOT/build/tools/toolchain-licenses
    133 
    134 # Copy the sysroot to the installation prefix. This prevents the generated
    135 # binaries from containing hard-coding host paths
    136 TOOLCHAIN_SYSROOT=$TOOLCHAIN_PATH/sysroot
    137 dump "Sysroot  : Copying: $SYSROOT --> $TOOLCHAIN_SYSROOT"
    138 mkdir -p $TOOLCHAIN_SYSROOT && (cd $SYSROOT && tar c *) | (cd $TOOLCHAIN_SYSROOT && tar x)
    139 if [ $? != 0 ] ; then
    140     echo "Error while copying sysroot files. See $TMPLOG"
    141     exit 1
    142 fi
    143 
    144 # configure the toolchain
    145 #
    146 dump "Configure: $TOOLCHAIN toolchain build"
    147 # Old versions of the toolchain source packages placed the
    148 # configure script at the top-level. Newer ones place it under
    149 # the build directory though. Probe the file system to check
    150 # this.
    151 BUILD_SRCDIR=$SRC_DIR/build
    152 if [ ! -d $BUILD_SRCDIR ] ; then
    153     BUILD_SRCDIR=$SRC_DIR
    154 fi
    155 rm -rf $BUILD_OUT
    156 OLD_ABI="${ABI}"
    157 export CC CXX
    158 mkdir -p $BUILD_OUT &&
    159 cd $BUILD_OUT &&
    160 export CC CXX &&
    161 export ABI=$HOST_GMP_ABI &&  # needed to build a 32-bit gmp
    162 export CFLAGS="-Wno-error" && # needed because gdb-6.6 uses -Werror by default and fails to build with recent GCC versions
    163 run \
    164 $BUILD_SRCDIR/configure --target=$ABI_CONFIGURE_TARGET \
    165                         --host=$ABI_CONFIGURE_HOST \
    166                         --build=$ABI_CONFIGURE_BUILD \
    167                         --disable-nls \
    168                         --prefix=$TOOLCHAIN_PATH \
    169                         --with-sysroot=$TOOLCHAIN_SYSROOT \
    170                         --with-binutils-version=$BINUTILS_VERSION \
    171                         --with-gcc-version=$GCC_VERSION \
    172                         --with-gdb-version=$GDB_VERSION \
    173                         $ABI_CONFIGURE_EXTRA_FLAGS
    174 if [ $? != 0 ] ; then
    175     dump "Error while trying to configure toolchain build. See $TMPLOG"
    176     exit 1
    177 fi
    178 ABI="$OLD_ABI"
    179 # build the toolchain
    180 dump "Building : $TOOLCHAIN toolchain [this can take a long time]."
    181 cd $BUILD_OUT &&
    182 export CC CXX &&
    183 export ABI=$HOST_GMP_ABI &&
    184 run make -j$JOBS
    185 if [ $? != 0 ] ; then
    186     echo "Error while building toolchain. See $TMPLOG"
    187     exit 1
    188 fi
    189 ABI="$OLD_ABI"
    190 
    191 # install the toolchain to its final location
    192 dump "Install  : $TOOLCHAIN toolchain binaries."
    193 cd $BUILD_OUT && run make install
    194 if [ $? != 0 ] ; then
    195     echo "Error while installing toolchain. See $TMPLOG"
    196     exit 1
    197 fi
    198 # don't forget to copy the GPL and LGPL license files
    199 run cp -f $TOOLCHAIN_LICENSES/COPYING $TOOLCHAIN_LICENSES/COPYING.LIB $TOOLCHAIN_PATH
    200 # remove some unneeded files
    201 run rm -f $TOOLCHAIN_PATH/bin/*-gccbug
    202 run rm -rf $TOOLCHAIN_PATH/man $TOOLCHAIN_PATH/info
    203 # strip binaries to reduce final package size
    204 run strip $TOOLCHAIN_PATH/bin/*
    205 run strip $TOOLCHAIN_PATH/$ABI_CONFIGURE_TARGET/bin/*
    206 run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1
    207 run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1plus
    208 run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/collect2
    209 
    210 dump "Done."
    211 if [ -n "$OPTION_BUILD_OUT" ] ; then
    212     rm -rf $BUILD_OUT
    213 fi
    214