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-linux-androideabi-4.6).
     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=/tmp/ndk-$USER/build/gdbserver
     43 register_option "--build-out=<path>" do_build_out "Set temporary build directory"
     44 do_build_out () { OPTION_BUILD_OUT="$1"; }
     45 
     46 register_var_option "--platform=<name>"  PLATFORM "Target specific platform"
     47 
     48 SYSROOT=
     49 register_var_option "--sysroot=<path>" SYSROOT "Specify sysroot directory directly"
     50 
     51 NOTHREADS=no
     52 register_var_option "--disable-threads" NOTHREADS "Disable threads support"
     53 
     54 GDB_VERSION=
     55 register_var_option "--gdb-version=<name>" GDB_VERSION "Use specific gdb version."
     56 
     57 PACKAGE_DIR=
     58 register_var_option "--package-dir=<path>" PACKAGE_DIR "Archive binary into specific directory"
     59 
     60 register_jobs_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     GDBVER=
     72 
     73     # Check source directory
     74     #
     75     if [ -z "$SRC_DIR" ] ; then
     76         echo "ERROR: Missing source directory parameter. See --help for details."
     77         exit 1
     78     fi
     79 
     80     if [ -n "$GDB_VERSION" ]; then
     81         GDBVER=$GDB_VERSION
     82     else
     83         GDBVER=$(get_default_gdbserver_version_for_gcc $TOOLCHAIN)
     84     fi
     85 
     86     SRC_DIR2="$SRC_DIR/gdb/gdb-$GDBVER/gdb/gdbserver"
     87     if [ -d "$SRC_DIR2" ] ; then
     88         SRC_DIR="$SRC_DIR2"
     89         log "Found gdbserver source directory: $SRC_DIR"
     90     fi
     91 
     92     if [ ! -f "$SRC_DIR/gdbreplay.c" ] ; then
     93         echo "ERROR: Source directory does not contain gdbserver sources: $SRC_DIR"
     94         exit 1
     95     fi
     96 
     97     log "Using source directory: $SRC_DIR"
     98 
     99     # Check NDK installation directory
    100     #
    101     if [ -z "$NDK_DIR" ] ; then
    102         echo "ERROR: Missing NDK directory parameter. See --help for details."
    103         exit 1
    104     fi
    105 
    106     if [ ! -d "$NDK_DIR" ] ; then
    107         echo "ERROR: NDK directory does not exist: $NDK_DIR"
    108         exit 1
    109     fi
    110 
    111     log "Using NDK directory: $NDK_DIR"
    112 
    113     # Check toolchain name
    114     #
    115     if [ -z "$TOOLCHAIN" ] ; then
    116         echo "ERROR: Missing toolchain name parameter. See --help for details."
    117         exit 1
    118     fi
    119 }
    120 
    121 set_parameters $PARAMETERS
    122 
    123 if [ "$PACKAGE_DIR" ]; then
    124     mkdir -p "$PACKAGE_DIR"
    125     fail_panic "Could not create package directory: $PACKAGE_DIR"
    126 fi
    127 
    128 prepare_target_build
    129 
    130 parse_toolchain_name $TOOLCHAIN
    131 check_toolchain_install $NDK_DIR $TOOLCHAIN
    132 
    133 if [ -z "$PLATFORM" ]; then
    134    PLATFORM="android-"$(get_default_api_level_for_arch $ARCH)
    135 fi
    136 
    137 # Check build directory
    138 #
    139 fix_sysroot "$SYSROOT"
    140 log "Using sysroot: $SYSROOT"
    141 
    142 if [ -n "$OPTION_BUILD_OUT" ] ; then
    143     BUILD_OUT="$OPTION_BUILD_OUT"
    144 fi
    145 log "Using build directory: $BUILD_OUT"
    146 run rm -rf "$BUILD_OUT"
    147 run mkdir -p "$BUILD_OUT"
    148 
    149 # Copy the sysroot to a temporary build directory
    150 BUILD_SYSROOT="$BUILD_OUT/sysroot"
    151 run mkdir -p "$BUILD_SYSROOT"
    152 run cp -RHL "$SYSROOT"/* "$BUILD_SYSROOT"
    153 
    154 LIBDIR=$(get_default_libdir_for_arch $ARCH)
    155 
    156 # Remove libthread_db to ensure we use exactly the one we want.
    157 rm -f $BUILD_SYSROOT/usr/$LIBDIR/libthread_db*
    158 rm -f $BUILD_SYSROOT/usr/include/thread_db.h
    159 
    160 if [ "$NOTHREADS" != "yes" ] ; then
    161     # We're going to rebuild libthread_db.o from its source
    162     # that is under sources/android/libthread_db and place its header
    163     # and object file into the build sysroot.
    164     LIBTHREAD_DB_DIR=$ANDROID_NDK_ROOT/sources/android/libthread_db/gdb-$GDBVER
    165     if [ ! -d "$LIBTHREAD_DB_DIR" ] ; then
    166         dump "ERROR: Missing directory: $LIBTHREAD_DB_DIR"
    167         exit 1
    168     fi
    169     # Small trick, to avoid calling ar, we store the single object file
    170     # with an .a suffix. The linker will handle that seamlessly.
    171     run cp $LIBTHREAD_DB_DIR/thread_db.h $BUILD_SYSROOT/usr/include/
    172     run $TOOLCHAIN_PREFIX-gcc --sysroot=$BUILD_SYSROOT -o $BUILD_SYSROOT/usr/$LIBDIR/libthread_db.o -c $LIBTHREAD_DB_DIR/libthread_db.c
    173     run $TOOLCHAIN_PREFIX-ar -rD $BUILD_SYSROOT/usr/$LIBDIR/libthread_db.a $BUILD_SYSROOT/usr/$LIBDIR/libthread_db.o
    174     if [ $? != 0 ] ; then
    175         dump "ERROR: Could not compile libthread_db.c!"
    176         exit 1
    177     fi
    178 fi
    179 
    180 log "Using build sysroot: $BUILD_SYSROOT"
    181 
    182 # configure the gdbserver build now
    183 dump "Configure: $TOOLCHAIN gdbserver-$GDBVER build."
    184 
    185 case "$GDBVER" in
    186     6.6)
    187         CONFIGURE_FLAGS="--with-sysroot=$BUILD_SYSROOT"
    188         ;;
    189     7.3.x)
    190         # This flag is required to link libthread_db statically to our
    191         # gdbserver binary. Otherwise, the program will try to dlopen()
    192         # the threads binary, which is not possible since we build a
    193         # static executable.
    194         CONFIGURE_FLAGS="--with-libthread-db=$BUILD_SYSROOT/usr/$LIBDIR/libthread_db.a"
    195         # Disable libinproctrace.so which needs crtbegin_so.o and crtbend_so.o instead of
    196         # CRTBEGIN/END above.  Clean it up and re-enable it in the future.
    197         CONFIGURE_FLAGS=$CONFIGURE_FLAGS" --disable-inprocess-agent"
    198         ;;
    199     7.6)
    200         CONFIGURE_FLAGS="--with-libthread-db=$BUILD_SYSROOT/usr/$LIBDIR/libthread_db.a"
    201         CONFIGURE_FLAGS=$CONFIGURE_FLAGS" --disable-inprocess-agent"
    202         ;;
    203     *)
    204         CONFIGURE_FLAGS=""
    205 esac
    206 
    207 cd $BUILD_OUT &&
    208 export CC="$TOOLCHAIN_PREFIX-gcc --sysroot=$BUILD_SYSROOT" &&
    209 export CFLAGS="-O2 $GDBSERVER_CFLAGS"  &&
    210 export LDFLAGS="-static -Wl,-z,nocopyreloc -Wl,--no-undefined" &&
    211 run $SRC_DIR/configure \
    212 --host=$GDBSERVER_HOST \
    213 $CONFIGURE_FLAGS
    214 if [ $? != 0 ] ; then
    215     dump "Could not configure gdbserver build. See $TMPLOG"
    216     exit 1
    217 fi
    218 
    219 # build gdbserver
    220 dump "Building : $TOOLCHAIN gdbserver."
    221 cd $BUILD_OUT &&
    222 run make -j$NUM_JOBS
    223 if [ $? != 0 ] ; then
    224     dump "Could not build $TOOLCHAIN gdbserver. Use --verbose to see why."
    225     exit 1
    226 fi
    227 
    228 # install gdbserver
    229 #
    230 # note that we install it in the toolchain bin directory
    231 # not in $SYSROOT/usr/bin
    232 #
    233 if [ "$NOTHREADS" = "yes" ] ; then
    234     DSTFILE="gdbserver-nothreads"
    235 else
    236     DSTFILE="gdbserver"
    237 fi
    238 dump "Install  : $TOOLCHAIN $DSTFILE."
    239 DEST=$ANDROID_NDK_ROOT/prebuilt/android-$ARCH/gdbserver
    240 mkdir -p $DEST &&
    241 run $TOOLCHAIN_PREFIX-objcopy --strip-unneeded $BUILD_OUT/gdbserver $DEST/$DSTFILE
    242 if [ $? != 0 ] ; then
    243     dump "Could not install $DSTFILE. See $TMPLOG"
    244     exit 1
    245 fi
    246 
    247 if [ "$PACKAGE_DIR" ]; then
    248     ARCHIVE=$ARCH-gdbserver.tar.bz2
    249     dump "Packaging: $ARCHIVE"
    250     pack_archive "$PACKAGE_DIR/$ARCHIVE" "$ANDROID_NDK_ROOT" "prebuilt/android-$ARCH/gdbserver/$DSTFILE"
    251 fi
    252 
    253 log "Cleaning up."
    254 if [ -z "$OPTION_BUILD_OUT" ] ; then
    255     run rm -rf $BUILD_OUT
    256 fi
    257 
    258 dump "Done."
    259