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