1#!/bin/sh
2
3NDK_BUILDTOOLS_PATH="$(dirname $0)"
4. "$NDK_BUILDTOOLS_PATH/prebuilt-common.sh"
5. "$NDK_BUILDTOOLS_PATH/common-build-host-funcs.sh"
6
7PROGRAM_PARAMETERS=""
8PROGRAM_DESCRIPTION="\
9This program is used to build the gdb stub for Windows and replace a
10gdb executable with it. Because of the replacing nature of this, I
11check to see if there's a gdb-orig.exe there already and if so, 'undo'
12the process first by putting it back. Sample usage:
13
14$0 --gdb-executable-path=\$NDK/toolchains/arm-linux-androideabi-4.8/prebuilt/bin/arm-linux-androideabi-gdb.exe \\
15   --python-prefix-dir=\$NDK/prebuilt/windows \\
16   --mingw-w64-gcc-path=\$HOME/i686-w64-mingw32/bin/i686-w64-mingw32-gcc
17"
18
19NDK_DIR=$ANDROID_NDK_ROOT
20
21PYTHON_PREFIX_DIR=
22register_var_option "--python-prefix-dir=<path>" PYTHON_PREFIX_DIR "Python prefix directory."
23
24GDB_EXECUTABLE_PATH=
25register_var_option "--gdb-executable-path=<path>" GDB_EXECUTABLE_PATH "GDB executable file to stubify."
26
27MINGW_W64_GCC=
28register_var_option "--mingw-w64-gcc=<program>" MINGW_W64_GCC "MinGW-w64 gcc program to use."
29
30DEBUG_STUB=
31register_var_option "--debug" DEBUG_STUB "Build stub in debug mode."
32
33extract_parameters "$@"
34
35if [ -n "$DEBUG_STUB" ]; then
36    STUB_CFLAGS="-O0 -g -D_DEBUG"
37else
38    STUB_CFLAGS="-O2 -s -DNDEBUG"
39fi
40
41if [ ! -f "$GDB_EXECUTABLE_PATH" ]; then
42    panic "GDB executable $GDB_EXECUTABLE_PATH doesn't exist!"
43fi
44
45if [ ! -d "$PYTHON_PREFIX_DIR" ]; then
46    panic "Python prefix dir $PYTHON_PREFIX_DIR doesn't exist!"
47fi
48
49if [ -z "$MINGW_W64_GCC" ]; then
50    panic "Please specify an existing MinGW-w64 cross compiler with --mingw-w64-gcc=<program>"
51fi
52
53GDB_BIN_DIR=$(cd $(dirname "$GDB_EXECUTABLE_PATH"); pwd)
54PYTHON_PREFIX_DIR=$(cd "$PYTHON_PREFIX_DIR"; pwd)
55PYTHON_BIN_DIR=${PYTHON_PREFIX_DIR}/bin
56
57# Sed is used to get doubled up Windows style dir separators.
58GDB_TO_PYTHON_REL_DIR=$(relpath "$GDB_BIN_DIR" "$PYTHON_BIN_DIR" | sed -e s./.\\\\\\\\.g)
59PYTHONHOME_REL_DIR=$(relpath "$GDB_BIN_DIR" "$PYTHON_PREFIX_DIR" | sed -e s./.\\\\\\\\.g)
60dump "GDB_TO_PYTHON_REL_DIR=$GDB_TO_PYTHON_REL_DIR"
61dump "PYTHONHOME_REL_DIR=$PYTHONHOME_REL_DIR"
62
63GDB_EXECUTABLE_ORIG=$(dirname "$GDB_EXECUTABLE_PATH")/$(basename "$GDB_EXECUTABLE_PATH" ".exe")-orig.exe
64if [ -f "$GDB_EXECUTABLE_ORIG" ] ; then
65    echo "Warning : Found an existing gdb-stub called $GDB_EXECUTABLE_ORIG so will un-do a previous run of this script."
66    cp "$GDB_EXECUTABLE_ORIG" "$GDB_EXECUTABLE_PATH"
67fi
68cp "$GDB_EXECUTABLE_PATH" "$GDB_EXECUTABLE_ORIG"
69
70GDB_EXECUTABLE_ORIG_FILENAME=$(basename "$GDB_EXECUTABLE_ORIG")
71
72# Build the stub in-place of the real gdb.
73run $MINGW_W64_GCC $STUB_CFLAGS "$NDK_DIR/sources/host-tools/gdb-stub/gdb-stub.c" \
74                    -o "$GDB_EXECUTABLE_PATH" \
75                    -DGDB_TO_PYTHON_REL_DIR=\"$GDB_TO_PYTHON_REL_DIR\" \
76                    -DPYTHONHOME_REL_DIR=\"$PYTHONHOME_REL_DIR\" \
77                    -DGDB_EXECUTABLE_ORIG_FILENAME=\"$GDB_EXECUTABLE_ORIG_FILENAME\"
78
79fail_panic "Can't build gdb stub!"
80
81dump "GDB Stub done!"
82