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