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 JOBS=$HOST_NUM_CPUS
     34 RELEASE=`date +%Y%m%d`
     35 BUILD_OUT=`random_temp_directory`
     36 PLATFORM=android-3
     37 GDB_VERSION=6.6
     38 BINUTILS_VERSION=2.19
     39 
     40 OPTION_BUILD_OUT=
     41 OPTION_PLATFORM=
     42 OPTION_SYSROOT=
     43 OPTION_BINUTILS_VERSION=
     44 
     45 register_option "--build-out=<path>" do_build_out "Set temporary build directory" "/tmp/<random>"
     46 register_option "--sysroot=<path>"   do_sysroot   "Specify sysroot directory directly"
     47 register_option "--platform=<name>"  do_platform  "Specify platform name" "$PLATFORM"
     48 register_option "--gdb-version=<version>"  do_gdb_version  "Specify gdb version" "$GDB_VERSION"
     49 register_option "--binutils-version=<version>" do_binutils_version "Specify binutils version" "$BINUTILS_VERSION"
     50 register_option "-j<number>" do_jobs "Use <number> parallel build jobs" "$JOBS"
     51 
     52 do_build_out ()
     53 {
     54     OPTION_BUILD_OUT=$1
     55 }
     56 
     57 do_platform ()
     58 {
     59     OPTION_PLATFORM=$1
     60 }
     61 
     62 do_sysroot ()
     63 {
     64     OPTION_SYSROOT=$1
     65 }
     66 
     67 do_gdb_version ()
     68 {
     69     OPTION_GDB_VERSION=$1
     70 }
     71 
     72 do_binutils_version ()
     73 {
     74     OPTION_BINUTILS_VERSION=$1
     75 }
     76 
     77 do_jobs ()
     78 {
     79     JOBS=$1
     80 }
     81 
     82 extract_parameters $@
     83 
     84 set_parameters ()
     85 {
     86     SRC_DIR="$1"
     87     NDK_DIR="$2"
     88     TOOLCHAIN="$3"
     89 
     90     # Check source directory
     91     #
     92     if [ -z "$SRC_DIR" ] ; then
     93         echo "ERROR: Missing source directory parameter. See --help for details."
     94         exit 1
     95     fi
     96 
     97     if [ ! -d "$SRC_DIR/gcc" ] ; then
     98         echo "ERROR: Source directory does not contain gcc sources: $SRC_DIR"
     99         exit 1
    100     fi
    101 
    102     log "Using source directory: $SRC_DIR"
    103 
    104     # Check NDK installation directory
    105     #
    106     if [ -z "$NDK_DIR" ] ; then
    107         echo "ERROR: Missing NDK directory parameter. See --help for details."
    108         exit 1
    109     fi
    110 
    111     if [ ! -d "$NDK_DIR" ] ; then
    112         mkdir -p $NDK_DIR
    113         if [ $? != 0 ] ; then
    114             echo "ERROR: Could not create target NDK installation path: $NDK_DIR"
    115             exit 1
    116         fi
    117     fi
    118 
    119     log "Using NDK directory: $NDK_DIR"
    120 
    121     # Check toolchain name
    122     #
    123     if [ -z "$TOOLCHAIN" ] ; then
    124         echo "ERROR: Missing toolchain name parameter. See --help for details."
    125         exit 1
    126     fi
    127 }
    128 
    129 set_parameters $PARAMETERS
    130 
    131 prepare_host_flags
    132 
    133 parse_toolchain_name
    134 
    135 fix_option PLATFORM "$OPTION_PLATFORM" "platform"
    136 fix_option BUILD_OUT "$OPTION_BUILD_OUT" "build directory"
    137 fix_sysroot "$OPTION_SYSROOT"
    138 
    139 fix_option GDB_VERSION "$OPTION_GDB_VERSION" "gdb version"
    140 if [ ! -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
    144 fi
    145 
    146 fix_option BINUTILS_VERSION "$OPTION_BINUTILS_VERSION" "binutils version"
    147 if [ ! -d $SRC_DIR/binutils/binutils-$BINUTILS_VERSION ] ; then
    148     echo "ERROR: Missing binutils sources: $SRC_DIR/binutils/binutils-$BINUTILS_VERSION"
    149     echo "       Use --binutils-version=<version> to specify alternative."
    150     exit 1
    151 fi
    152 
    153 set_toolchain_install $NDK_DIR/build/prebuilt/$HOST_TAG/$TOOLCHAIN
    154 
    155 # Location where the toolchain license files are
    156 TOOLCHAIN_LICENSES=$ANDROID_NDK_ROOT/build/tools/toolchain-licenses
    157 
    158 # configure the toolchain
    159 #
    160 dump "Configure: $TOOLCHAIN toolchain build"
    161 # Old versions of the toolchain source packages placed the
    162 # configure script at the top-level. Newer ones place it under
    163 # the build directory though. Probe the file system to check
    164 # this.
    165 BUILD_SRCDIR=$SRC_DIR/build
    166 if [ ! -d $BUILD_SRCDIR ] ; then
    167     BUILD_SRCDIR=$SRC_DIR
    168 fi
    169 OLD_ABI="${ABI}"
    170 OLD_CFLAGS="$CFLAGS"
    171 OLD_LDFLAGS="$LDFLAGS"
    172 mkdir -p $BUILD_OUT &&
    173 cd $BUILD_OUT &&
    174 export ABI="32" &&  # needed to build a 32-bit gmp
    175 export CFLAGS="$HOST_CFLAGS" &&
    176 export LDFLAGS="$HOST_LDFLAGS" && run \
    177 $BUILD_SRCDIR/configure --target=$ABI_TOOLCHAIN_PREFIX \
    178                         --disable-nls \
    179                         --prefix=$TOOLCHAIN_PATH \
    180                         --with-sysroot=$SYSROOT \
    181                         --with-binutils-version=$BINUTILS_VERSION \
    182                         --with-gcc-version=$GCC_VERSION \
    183                         --with-gdb-version=$GDB_VERSION
    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 CFLAGS="$OLD_CFLAGS"
    190 LDFLAGS="$OLD_LDFLAGS"
    191 
    192 # build the toolchain
    193 dump "Building : $TOOLCHAIN toolchain [this can take a long time]."
    194 OLD_CFLAGS="$CFLAGS"
    195 OLD_LDFLAGS="$LDFLAGS"
    196 OLD_ABI="$ABI"
    197 cd $BUILD_OUT &&
    198 export CFLAGS="$HOST_CFLAGS" &&
    199 export LDFLAGS="$HOST_LDFLAGS" &&
    200 export ABI="32" &&
    201 run make -j$JOBS
    202 if [ $? != 0 ] ; then
    203     echo "Error while building toolchain. See $TMPLOG"
    204     exit 1
    205 fi
    206 CFLAGS="$OLD_CFLAGS"
    207 LDFLAGS="$OLD_LDFLAGS"
    208 ABI="$OLD_ABI"
    209 
    210 # install the toolchain to its final location
    211 dump "Install  : $TOOLCHAIN toolchain binaries."
    212 cd $BUILD_OUT && run make install
    213 if [ $? != 0 ] ; then
    214     echo "Error while installing toolchain. See $TMPLOG"
    215     exit 1
    216 fi
    217 # don't forget to copy the GPL and LGPL license files
    218 run cp -f $TOOLCHAIN_LICENSES/COPYING $TOOLCHAIN_LICENSES/COPYING.LIB $TOOLCHAIN_PATH
    219 # remove some unneeded files
    220 run rm -f $TOOLCHAIN_PATH/bin/*-gccbug
    221 run rm -rf $TOOLCHAIN_PATH/man $TOOLCHAIN_PATH/info
    222 # strip binaries to reduce final package size
    223 run strip $TOOLCHAIN_PATH/bin/*
    224 run strip $TOOLCHAIN_PATH/$ABI_TOOLCHAIN_PATH/bin/*
    225 run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1
    226 run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/cc1plus
    227 run strip $TOOLCHAIN_PATH/libexec/gcc/*/*/collect2
    228 
    229 dump "Done."
    230 if [ -n "$OPTION_BUILD_OUT" ] ; then
    231     rm -rf $BUILD_OUT
    232 fi
    233