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.8).
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=$TMPDIR/build/gdbserver
43register_option "--build-out=<path>" do_build_out "Set temporary build directory"
44do_build_out () { OPTION_BUILD_OUT="$1"; }
45
46SYSROOT=
47register_var_option "--sysroot=<path>" SYSROOT "Specify sysroot directory directly"
48
49NOTHREADS=no
50register_var_option "--disable-threads" NOTHREADS "Disable threads support"
51
52GDB_VERSION=
53register_var_option "--gdb-version=<name>" GDB_VERSION "Use specific gdb version."
54
55PACKAGE_DIR=
56register_var_option "--package-dir=<path>" PACKAGE_DIR "Archive binary into specific directory"
57
58register_jobs_option
59
60register_try64_option
61
62extract_parameters "$@"
63
64setup_default_log_file
65
66set_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
121set_parameters $PARAMETERS
122
123if [ "$PACKAGE_DIR" ]; then
124    mkdir -p "$PACKAGE_DIR"
125    fail_panic "Could not create package directory: $PACKAGE_DIR"
126fi
127
128prepare_target_build
129
130parse_toolchain_name $TOOLCHAIN
131check_toolchain_install $ANDROID_BUILD_TOP/prebuilts/ndk/current $TOOLCHAIN
132
133PLATFORM="android-$LATEST_API_LEVEL"
134
135# Check build directory
136#
137fix_sysroot "$SYSROOT"
138log "Using sysroot: $SYSROOT"
139
140if [ -n "$OPTION_BUILD_OUT" ] ; then
141    BUILD_OUT="$OPTION_BUILD_OUT"
142fi
143log "Using build directory: $BUILD_OUT"
144run rm -rf "$BUILD_OUT"
145run mkdir -p "$BUILD_OUT"
146
147# Copy the sysroot to a temporary build directory
148BUILD_SYSROOT="$BUILD_OUT/sysroot"
149run mkdir -p "$BUILD_SYSROOT"
150run cp -RHL "$SYSROOT"/* "$BUILD_SYSROOT"
151
152LIBDIR=$(get_default_libdir_for_arch $ARCH)
153
154# Remove libthread_db to ensure we use exactly the one we want.
155rm -f $BUILD_SYSROOT/usr/$LIBDIR/libthread_db*
156rm -f $BUILD_SYSROOT/usr/include/thread_db.h
157
158if [ "$NOTHREADS" != "yes" ] ; then
159    # We're going to rebuild libthread_db.o from its source
160    # that is under sources/android/libthread_db and place its header
161    # and object file into the build sysroot.
162    LIBTHREAD_DB_DIR=$ANDROID_NDK_ROOT/sources/android/libthread_db
163    if [ ! -d "$LIBTHREAD_DB_DIR" ] ; then
164        dump "ERROR: Missing directory: $LIBTHREAD_DB_DIR"
165        exit 1
166    fi
167
168    run cp $LIBTHREAD_DB_DIR/thread_db.h $BUILD_SYSROOT/usr/include/
169    run $TOOLCHAIN_PREFIX-gcc --sysroot=$BUILD_SYSROOT -o $BUILD_SYSROOT/usr/$LIBDIR/libthread_db.o -c $LIBTHREAD_DB_DIR/libthread_db.c
170    run $TOOLCHAIN_PREFIX-ar -rD $BUILD_SYSROOT/usr/$LIBDIR/libthread_db.a $BUILD_SYSROOT/usr/$LIBDIR/libthread_db.o
171    if [ $? != 0 ] ; then
172        dump "ERROR: Could not compile libthread_db.c!"
173        exit 1
174    fi
175fi
176
177log "Using build sysroot: $BUILD_SYSROOT"
178
179# configure the gdbserver build now
180dump "Configure: $TOOLCHAIN gdbserver-$GDBVER build with $PLATFORM"
181
182# This flag is required to link libthread_db statically to our
183# gdbserver binary. Otherwise, the program will try to dlopen()
184# the threads binary, which is not possible since we build a
185# static executable.
186CONFIGURE_FLAGS="--with-libthread-db=$BUILD_SYSROOT/usr/$LIBDIR/libthread_db.a"
187# Disable libinproctrace.so which needs crtbegin_so.o and crtbend_so.o instead of
188# CRTBEGIN/END above.  Clean it up and re-enable it in the future.
189CONFIGURE_FLAGS=$CONFIGURE_FLAGS" --disable-inprocess-agent"
190# gdb 7.7 builds with -Werror by default, but they redefine constants such as
191# HWCAP_VFPv3 in a way that's compatible with glibc's headers but not our
192# kernel uapi headers. We should send a patch upstream to add the missing
193# #ifndefs, but for now just build gdbserver without -Werror.
194CONFIGURE_FLAGS=$CONFIGURE_FLAGS" --enable-werror=no"
195
196cd $BUILD_OUT &&
197export CC="$TOOLCHAIN_PREFIX-gcc --sysroot=$BUILD_SYSROOT" &&
198export AR="$TOOLCHAIN_PREFIX-ar" &&
199export RANLIB="$TOOLCHAIN_PREFIX-ranlib" &&
200export CFLAGS="-O2 $GDBSERVER_CFLAGS"  &&
201export LDFLAGS="-static -Wl,-z,nocopyreloc -Wl,--no-undefined" &&
202run $SRC_DIR/configure \
203--host=$GDBSERVER_HOST \
204$CONFIGURE_FLAGS
205if [ $? != 0 ] ; then
206    dump "Could not configure gdbserver build. See $TMPLOG"
207    exit 1
208fi
209
210# build gdbserver
211dump "Building : $TOOLCHAIN gdbserver."
212cd $BUILD_OUT &&
213run make -j$NUM_JOBS
214if [ $? != 0 ] ; then
215    dump "Could not build $TOOLCHAIN gdbserver. Use --verbose to see why."
216    exit 1
217fi
218
219# install gdbserver
220#
221# note that we install it in the toolchain bin directory
222# not in $SYSROOT/usr/bin
223#
224if [ "$NOTHREADS" = "yes" ] ; then
225    DSTFILE="gdbserver-nothreads"
226else
227    DSTFILE="gdbserver"
228fi
229dump "Install  : $TOOLCHAIN $DSTFILE."
230INSTALL_DIR=`mktemp -d $TMPDIR/gdbserver.XXXXXX`
231GDBSERVER_SUBDIR="gdbserver-$ARCH"
232DEST=$INSTALL_DIR/$GDBSERVER_SUBDIR
233mkdir -p $DEST &&
234run $TOOLCHAIN_PREFIX-objcopy --strip-unneeded $BUILD_OUT/gdbserver $DEST/$DSTFILE
235if [ $? != 0 ] ; then
236    dump "Could not install $DSTFILE. See $TMPLOG"
237    exit 1
238fi
239
240if [ "$PACKAGE_DIR" ]; then
241    make_repo_prop "$INSTALL_DIR/$GDBSERVER_SUBDIR"
242    cp "$SRC_DIR/../../COPYING" "$DEST/NOTICE"
243    fail_panic "Could not copy license file!"
244
245    ARCHIVE=gdbserver-$ARCH.zip
246    dump "Packaging: $ARCHIVE"
247    pack_archive "$PACKAGE_DIR/$ARCHIVE" "$INSTALL_DIR" "$GDBSERVER_SUBDIR"
248fi
249
250log "Cleaning up."
251if [ -z "$OPTION_BUILD_OUT" ] ; then
252    run rm -rf $BUILD_OUT
253fi
254
255dump "Done."
256