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 gdbserver binary from
     18 #  the Android NDK's prebuilt binaries.
     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 gdbserver prebuilt binary for the Android NDK toolchain.
     28 
     29 Where <src-dir> is the location of the gdbserver sources,
     30 <ndk-dir> is the top-level NDK installation path and <toolchain>
     31 is the name of the toolchain to use (e.g. arm-eabi-4.4.0).
     32 
     33 The final binary is placed under:
     34 
     35     <ndk-dir>/toolchains <toolchain>/prebuilt/gdbserver
     36 
     37 NOTE: The --platform option is ignored if --sysroot is used."
     38 
     39 VERBOSE=no
     40 
     41 OPTION_BUILD_OUT=
     42 BUILD_OUT=`random_temp_directory`
     43 register_option "--build-out=<path>" do_build_out "Set temporary build directory" "/tmp/<random>"
     44 do_build_out () { OPTION_BUILD_OUT="$1"; }
     45 
     46 PLATFORM=android-3
     47 register_var_option "--platform=<name>"  PLATFORM "Target specific platform"
     48 
     49 SYSROOT=
     50 if [ -d $TOOLCHAIN_PATH/sysroot ] ; then
     51   SYSROOT=$TOOLCHAIN_PATH/sysroot
     52 fi
     53 register_var_option "--sysroot=<path>" SYSROOT "Specify sysroot directory directly"
     54 
     55 NOTHREADS=no
     56 register_var_option "--disable-threads" NOTHREADS "Disable threads support"
     57 
     58 JOBS=$HOST_NUM_CPUS
     59 register_var_option "-j<number>" JOBS "Use <number> build jobs in parallel"
     60 
     61 GDB_VERSION=6.6
     62 register_var_option "--gdb-version=<name>" GDB_VERSION "Use specific gdb version."
     63 
     64 extract_parameters "$@"
     65 
     66 setup_default_log_file
     67 
     68 set_parameters ()
     69 {
     70     SRC_DIR="$1"
     71     NDK_DIR="$2"
     72     TOOLCHAIN="$3"
     73 
     74     # Check source directory
     75     #
     76     if [ -z "$SRC_DIR" ] ; then
     77         echo "ERROR: Missing source directory parameter. See --help for details."
     78         exit 1
     79     fi
     80 
     81     SRC_DIR2="$SRC_DIR/gdb/gdb-$GDB_VERSION/gdb/gdbserver"
     82     if [ -d "$SRC_DIR2" ] ; then
     83         SRC_DIR="$SRC_DIR2"
     84         log "Found gdbserver source directory: $SRC_DIR"
     85     fi
     86 
     87     if [ ! -f "$SRC_DIR/gdbreplay.c" ] ; then
     88         echo "ERROR: Source directory does not contain gdbserver 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         echo "ERROR: NDK directory does not exist: $NDK_DIR"
    103         exit 1
    104     fi
    105 
    106     log "Using NDK directory: $NDK_DIR"
    107 
    108     # Check toolchain name
    109     #
    110     if [ -z "$TOOLCHAIN" ] ; then
    111         echo "ERROR: Missing toolchain name parameter. See --help for details."
    112         exit 1
    113     fi
    114 }
    115 
    116 set_parameters $PARAMETERS
    117 
    118 prepare_host_flags
    119 
    120 parse_toolchain_name
    121 check_toolchain_install $NDK_DIR
    122 
    123 # Check build directory
    124 #
    125 fix_sysroot "$SYSROOT"
    126 log "Using sysroot: $SYSROOT"
    127 
    128 if [ -n "$OPTION_BUILD_OUT" ] ; then
    129     BUILD_OUT="$OPTION_BUILD_OUT"
    130 fi
    131 log "Using build directory: $BUILD_OUT"
    132 run mkdir -p "$BUILD_OUT"
    133 
    134 # Copy the sysroot to a temporary build directory
    135 BUILD_SYSROOT="$BUILD_OUT/sysroot"
    136 run mkdir -p "$BUILD_SYSROOT"
    137 run cp -rp "$SYSROOT/*" "$BUILD_SYSROOT"
    138 
    139 # Remove libthread_db to ensure we use exactly the one we want.
    140 rm -f $BUILD_SYSROOT/usr/lib/libthread_db*
    141 rm -f $BUILD_SYSROOT/usr/include/thread_db.h
    142 
    143 if [ "$NOTHREADS" != "yes" ] ; then
    144     # We're going to rebuild libthread_db.o from its source
    145     # that is under sources/android/libthread_db and place its header
    146     # and object file into the build sysroot.
    147     LIBTHREAD_DB_DIR=$ANDROID_NDK_ROOT/sources/android/libthread_db/gdb-$GDB_VERSION
    148     if [ ! -d "$LIBTHREAD_DB_DIR" ] ; then
    149         dump "ERROR: Missing directory: $LIBTHREAD_DB_DIR"
    150         exit 1
    151     fi
    152     # Small trick, to avoid calling ar, we store the single object file
    153     # with an .a suffix. The linker will handle that seamlessly.
    154     run cp $LIBTHREAD_DB_DIR/thread_db.h $BUILD_SYSROOT/usr/include/
    155     run $TOOLCHAIN_PREFIX-gcc --sysroot=$BUILD_SYSROOT -o $BUILD_SYSROOT/usr/lib/libthread_db.a -c $LIBTHREAD_DB_DIR/libthread_db.c
    156     if [ $? != 0 ] ; then
    157         dump "ERROR: Could not compile libthread_db.c!"
    158         exit 1
    159     fi
    160 fi
    161 
    162 log "Using build sysroot: $BUILD_SYSROOT"
    163 
    164 # configure the gdbserver build now
    165 dump "Configure: $TOOLCHAIN gdbserver-$GDB_VERSION build."
    166 OLD_CC="$CC"
    167 OLD_CFLAGS="$CFLAGS"
    168 OLD_LDFLAGS="$LDFLAGS"
    169 
    170 INCLUDE_DIRS=\
    171 "-I$TOOLCHAIN_PATH/lib/gcc/$ABI_CONFIGURE_TARGET/$GCC_VERSION/include \
    172 -I$BUILD_SYSROOT/usr/include"
    173 CRTBEGIN="$BUILD_SYSROOT/usr/lib/crtbegin_static.o"
    174 CRTEND="$BUILD_SYSROOT/usr/lib/crtend_android.o"
    175 
    176 # Note: we must put a second -lc after -lgcc to resolve a cyclical
    177 #       dependency on arm-linux-androideabi, where libgcc.a contains
    178 #       a function (__div0) which depends on raise(), implemented
    179 #       in the C library.
    180 #
    181 LIBRARY_LDFLAGS="$CRTBEGIN -lc -lm -lgcc -lc $CRTEND "
    182 
    183 case "$GDB_VERSION" in
    184     6.6)
    185         CONFIGURE_FLAGS="--with-sysroot=$BUILD_SYSROOT"
    186         ;;
    187     7.1.x)
    188         # This flag is required to link libthread_db statically to our
    189         # gdbserver binary. Otherwise, the program will try to dlopen()
    190         # the threads binary, which is not possible since we build a
    191         # static executable.
    192         CONFIGURE_FLAGS="--with-libthread-db=$BUILD_SYSROOT/usr/lib/libthread_db.a"
    193         ;;
    194     *)
    195         CONFIGURE_FLAGS=""
    196 esac
    197 
    198 cd $BUILD_OUT &&
    199 export CC="$TOOLCHAIN_PREFIX-gcc --sysroot=$BUILD_SYSROOT" &&
    200 export CFLAGS="-O2 -nostdinc -nostdlib -D__ANDROID__ -DANDROID -DSTDC_HEADERS $INCLUDE_DIRS $GDBSERVER_CFLAGS"  &&
    201 export LDFLAGS="-static -Wl,-z,nocopyreloc -Wl,--no-undefined $LIBRARY_LDFLAGS" &&
    202 run $SRC_DIR/configure \
    203 --host=$GDBSERVER_HOST \
    204 $CONFIGURE_FLAGS
    205 if [ $? != 0 ] ; then
    206     dump "Could not configure gdbserver build. See $TMPLOG"
    207     exit 1
    208 fi
    209 CC="$OLD_CC"
    210 CFLAGS="$OLD_CFLAGS"
    211 LDFLAGS="$OLD_LDFLAGS"
    212 
    213 # build gdbserver
    214 dump "Building : $TOOLCHAIN gdbserver."
    215 cd $BUILD_OUT &&
    216 run make -j$JOBS
    217 if [ $? != 0 ] ; then
    218     dump "Could not build $TOOLCHAIN gdbserver. Use --verbose to see why."
    219     exit 1
    220 fi
    221 
    222 # install gdbserver
    223 #
    224 # note that we install it in the toolchain bin directory
    225 # not in $SYSROOT/usr/bin
    226 #
    227 if [ "$NOTHREADS" = "yes" ] ; then
    228     DSTFILE="gdbserver-nothreads"
    229 else
    230     DSTFILE="gdbserver"
    231 fi
    232 dump "Install  : $TOOLCHAIN $DSTFILE."
    233 DEST=`dirname $TOOLCHAIN_PATH`
    234 mkdir -p $DEST &&
    235 run $TOOLCHAIN_PREFIX-objcopy --strip-unneeded $BUILD_OUT/gdbserver $DEST/$DSTFILE
    236 if [ $? != 0 ] ; then
    237     dump "Could not install $DSTFILE. See $TMPLOG"
    238     exit 1
    239 fi
    240 
    241 log "Cleaning up."
    242 if [ -z "$OPTION_BUILD_OUT" ] ; then
    243     run rm -rf $BUILD_OUT
    244 fi
    245 
    246 dump "Done."
    247