Home | History | Annotate | Download | only in tools
      1 #!/bin/sh
      2 #
      3 # Copyright (C) 2012 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 # Rebuild the host GDB binaries from sources.
     18 #
     19 
     20 # include common function and variable definitions
     21 NDK_BUILDTOOLS_PATH="$(dirname $0)"
     22 . "$NDK_BUILDTOOLS_PATH/prebuilt-common.sh"
     23 . "$NDK_BUILDTOOLS_PATH/common-build-host-funcs.sh"
     24 
     25 PROGRAM_PARAMETERS=""
     26 PROGRAM_DESCRIPTION="\
     27 This program is used to rebuild one or more NDK gdb client programs from
     28 sources. To use it, you will need a working set of toolchain sources, like
     29 those downloaded with download-toolchain-sources.sh, then pass the
     30 corresponding directory with the --toolchain-src-dir=<path> option.
     31 
     32 By default, the script rebuilds GDB for you host system [$HOST_TAG],
     33 but you can use --systems=<tag1>,<tag2>,.. to ask binaries that can run on
     34 several distinct systems. Each <tag> value in the list can be one of the
     35 following:
     36 
     37    linux-x86
     38    linux-x86_64
     39    windows
     40    windows-x86  (equivalent to 'windows')
     41    windows-x86_64
     42    darwin-x86
     43    darwin-x86_64
     44 
     45 For example, here's how to rebuild the ARM toolchains on Linux
     46 for four different systems:
     47 
     48   $PROGNAME --toolchain-src-dir=/path/to/toolchain/src \
     49     --systems=linux-x86,linux-x86_64,windows,windows-x86_64 \
     50     --arch=arm"
     51 
     52 TOOLCHAIN_SRC_DIR=
     53 register_var_option "--toolchain-src-dir=<path>" TOOLCHAIN_SRC_DIR "Select toolchain source directory"
     54 
     55 GDB_VERSION="6.6 7.3.x"
     56 register_var_option "--gdb-version=<version>" GDB_VERSION "Select GDB version(s)."
     57 
     58 BUILD_DIR=
     59 register_var_option "--build-dir=<path>" BUILD_DIR "Build GDB into directory"
     60 
     61 PYTHON_VERSION=
     62 register_var_option "--python-version=<version>" PYTHON_VERSION "Python version."
     63 
     64 PYTHON_BUILD_DIR=
     65 register_var_option "--python-build-dir=<path>" PYTHON_BUILD_DIR "Python build directory."
     66 
     67 NDK_DIR=$ANDROID_NDK_ROOT
     68 register_var_option "--ndk-dir=<path>" NDK_DIR "Select NDK install directory."
     69 
     70 PACKAGE_DIR=
     71 register_var_option "--package-dir=<path>" PACKAGE_DIR "Package prebuilt tarballs into directory."
     72 
     73 ARCHS=$DEFAULT_ARCHS
     74 register_var_option "--arch=<list>" ARCHS "Build GDB client for these CPU architectures."
     75 
     76 bh_register_options
     77 
     78 register_jobs_option
     79 
     80 extract_parameters "$@"
     81 
     82 if [ -n "$PARAMETERS" ]; then
     83     panic "This script doesn't take parameters, only options. See --help"
     84 fi
     85 
     86 if [ -z "$TOOLCHAIN_SRC_DIR" ]; then
     87     panic "Please use --toolchain-src-dir=<path> to select toolchain source directory."
     88 fi
     89 
     90 BH_HOST_SYSTEMS=$(commas_to_spaces $BH_HOST_SYSTEMS)
     91 
     92 # Sanity check for all GDB versions
     93 for VERSION in $(commas_to_spaces $GDB_VERSION); do
     94     GDB_SRCDIR=$TOOLCHAIN_SRC_DIR/gdb/gdb-$VERSION
     95     if [ ! -d "$GDB_SRCDIR" ]; then
     96         panic "Missing source directory: $GDB_SRCDIR"
     97     fi
     98 done
     99 
    100 if [ -z "$BUILD_DIR" ] ; then
    101     BUILD_DIR=/tmp/ndk-$USER/buildgdb
    102 fi
    103 
    104 bh_setup_build_dir $BUILD_DIR
    105 
    106 # Sanity check that we have the right compilers for all hosts
    107 for SYSTEM in $BH_HOST_SYSTEMS; do
    108     bh_setup_build_for_host $SYSTEM
    109 done
    110 
    111 # Return the build install directory of a given GDB version
    112 # $1: host system tag
    113 # $2: target system tag
    114 # $3: gdb version
    115 gdb_build_install_dir ()
    116 {
    117     echo "$BH_BUILD_DIR/install/$1/gdb-$(get_toolchain_name_for_arch $(bh_tag_to_arch $2))-$3"
    118 }
    119 
    120 # $1: host system tag
    121 # $2: target system tag
    122 # $3: gdb version
    123 gdb_ndk_package_name ()
    124 {
    125     echo "gdb-$(get_toolchain_name_for_arch $(bh_tag_to_arch $2))-$3-$1"
    126 }
    127 
    128 
    129 # Same as gdb_build_install_dir, but for the final NDK installation
    130 # directory. Relative to $NDK_DIR.
    131 gdb_ndk_install_dir ()
    132 {
    133     echo "toolchains/gdb-$(get_toolchain_name_for_arch $(bh_tag_to_arch $2))-$3/prebuilt/$(install_dir_from_host_tag $1)"
    134 }
    135 
    136 python_build_install_dir ()
    137 {
    138     echo "$PYTHON_BUILD_DIR/install/prebuilt/$(install_dir_from_host_tag $1)"
    139 }
    140 
    141 # $1: host system tag
    142 build_expat ()
    143 {
    144     local ARGS
    145     local SRCDIR=$TOOLCHAIN_SRC_DIR/expat/expat-2.0.1
    146     local BUILDDIR=$BH_BUILD_DIR/build-expat-2.0.1-$1
    147     local INSTALLDIR=$BH_BUILD_DIR/install-host-$1
    148 
    149     ARGS=" --prefix=$INSTALLDIR"
    150     ARGS=$ARGS" --disable-shared --enable-static"
    151     ARGS=$ARGS" --build=$BH_BUILD_CONFIG"
    152     ARGS=$ARGS" --host=$BH_HOST_CONFIG"
    153 
    154     TEXT="$(bh_host_text) expat:"
    155 
    156     mkdir -p "$BUILDDIR" && rm -rf "$BUILDDIR"/* &&
    157     cd "$BUILDDIR" &&
    158     dump "$TEXT Building"
    159     run2 "$SRCDIR"/configure $ARGS &&
    160     run2 make -j$NUM_JOBS &&
    161     run2 make -j$NUM_JOBS install
    162 }
    163 
    164 need_build_expat ()
    165 {
    166     bh_stamps_do host-expat-$1 build_expat $1
    167 }
    168 
    169 # $1: host system tag
    170 # $2: target tag
    171 # $3: gdb version
    172 build_host_gdb ()
    173 {
    174     local SRCDIR=$TOOLCHAIN_SRC_DIR/gdb/gdb-$3
    175     local BUILDDIR=$BH_BUILD_DIR/build-gdb-$1-$2-$3
    176     local INSTALLDIR=$(gdb_build_install_dir $1 $2 $3)
    177     local ARGS TEXT
    178 
    179     if [ ! -f "$SRCDIR/configure" ]; then
    180         panic "Missing configure script in $SRCDIR"
    181     fi
    182 
    183     bh_set_target_tag $2
    184     bh_setup_host_env
    185 
    186     need_build_expat $1
    187     local EXPATPREFIX=$BH_BUILD_DIR/install-host-$1
    188 
    189     ARGS=" --prefix=$INSTALLDIR"
    190     ARGS=$ARGS" --disable-shared"
    191     ARGS=$ARGS" --build=$BH_BUILD_CONFIG"
    192     ARGS=$ARGS" --host=$BH_HOST_CONFIG"
    193     ARGS=$ARGS" --target=$(bh_tag_to_config_triplet $2)"
    194     ARGS=$ARGS" --disable-werror"
    195     ARGS=$ARGS" --disable-nls"
    196     ARGS=$ARGS" --disable-docs"
    197     ARGS=$ARGS" --with-expat"
    198     ARGS=$ARGS" --with-libexpat-prefix=$EXPATPREFIX"
    199     if [ -n "$PYTHON_VERSION" ]; then
    200         ARGS=$ARGS" --with-python=$(python_build_install_dir $BH_HOST_TAG)/bin/python-config.sh"
    201         if [ $1 = windows-x86 -o $1 = windows-x86_64 ]; then
    202             # This is necessary for the Python integration to build.
    203             CFLAGS=$CFLAGS" -D__USE_MINGW_ANSI_STDIO=1"
    204             CXXFLAGS=$CXXFLAGS" -D__USE_MINGW_ANSI_STDIO=1"
    205         fi
    206     fi
    207     TEXT="$(bh_host_text) gdb-$BH_TARGET_ARCH-$3:"
    208 
    209     mkdir -p "$BUILDDIR" && rm -rf "$BUILDDIR"/* &&
    210     cd "$BUILDDIR" &&
    211     dump "$TEXT Building"
    212     run2 "$SRCDIR"/configure $ARGS &&
    213     run2 make -j$NUM_JOBS &&
    214     run2 make -j$NUM_JOBS install
    215     fail_panic "Failed to configure/make/install gdb"
    216 }
    217 
    218 need_build_host_gdb ()
    219 {
    220     bh_stamps_do host-gdb-$1-$2-$3 build_host_gdb $1 $2 $3
    221 }
    222 
    223 # Install host GDB binaries and support files to the NDK install dir.
    224 # $1: host tag
    225 # $2: target tag
    226 # $3: gdb version
    227 install_host_gdb ()
    228 {
    229     local SRCDIR="$(gdb_build_install_dir $1 $2 $3)"
    230     local DSTDIR="$NDK_DIR/$(gdb_ndk_install_dir $1 $2 $3)"
    231     local PYDIR="$NDK_DIR/$(python_ndk_install_dir $1)"
    232 
    233     need_build_host_gdb $1 $2 $3
    234 
    235     bh_set_target_tag $2
    236 
    237     dump "$(bh_host_text) gdb-$BH_TARGET_ARCH-$3: Installing"
    238     run copy_directory "$SRCDIR/bin" "$DSTDIR/bin"
    239     if [ -d "$SRCDIR/share/gdb" ]; then
    240         run copy_directory "$SRCDIR/share/gdb" "$DSTDIR/share/gdb"
    241     fi
    242 
    243     # build the gdb stub and replace gdb with it. This is done post-install
    244     # so files are in the correct place when determining the relative path.
    245 
    246     case "$1" in
    247         windows*)
    248             dump "$TEXT Building gdb-stub"
    249             bh_setup_host_env
    250             GCC_FOR_STUB=${BH_HOST_CONFIG}-gcc
    251             GCC_FOR_STUB_TARGET=`$GCC_FOR_STUB -dumpmachine`
    252             if [ "$GCC_FOR_STUB_TARGET" = "i586-mingw32msvc" ]; then
    253                 GCC_FOR_STUB=i686-w64-mingw32-gcc
    254                 dump "Override compiler for gdb-stub: $GCC_FOR_STUB"
    255             fi
    256 
    257             # Uses $TOOLCHAIN_PATH/bin/$(bh_tag_to_config_triplet $2)-gdb.exe (1) instead of
    258             # ${DSTDIR}/bin/$(bh_tag_to_config_triplet $2)-gdb.exe (2) because
    259             # the final layout is to (1) which is a folder deeper than (2).
    260             # Sample (1):
    261             #  $NDK/gdb-arm-linux-androideabi-7.6/prebuilt/windows/bin/arm-linux-androideabi-gdb.exe
    262             # Sample (2):
    263             #  $NDK/toolchains/arm-linux-androideabi-4.7/prebuilt/windows/bin/arm-linux-androideabi-gdb.exe
    264             run $NDK_BUILDTOOLS_PATH/build-gdb-stub.sh \
    265                 --gdb-executable-path=${DSTDIR}/bin/$(bh_tag_to_config_triplet $2)-gdb.exe \
    266                 --python-prefix-dir=${PYDIR} \
    267                 --mingw-w64-gcc=${GCC_FOR_STUB}
    268             ;;
    269         *)
    270             ;;
    271     esac
    272 }
    273 
    274 need_install_host_gdb ()
    275 {
    276     bh_stamps_do install-host-gdb-$1-$2-$3 install_host_gdb $1 $2 $3
    277 }
    278 
    279 # Package host GDB binaries into a tarball
    280 # $1: host tag
    281 # $2: target tag
    282 # $3: gdb version
    283 package_host_gdb ()
    284 {
    285     local SRCDIR="$(gdb_ndk_install_dir $1 $2 $3)"
    286     local PACKAGENAME=$(gdb_ndk_package_name $1 $2 $3).tar.bz2
    287     local PACKAGE="$PACKAGE_DIR/$PACKAGENAME"
    288 
    289     need_install_host_gdb $1 $2 $3
    290 
    291     bh_set_target_tag $2
    292 
    293     dump "$(bh_host_text) $PACKAGENAME: Packaging"
    294     run pack_archive "$PACKAGE" "$NDK_DIR" "$SRCDIR"
    295 }
    296 
    297 GDB_VERSION=$(commas_to_spaces $GDB_VERSION)
    298 ARCHS=$(commas_to_spaces $ARCHS)
    299 
    300 # Let's build this
    301 for SYSTEM in $BH_HOST_SYSTEMS; do
    302     bh_setup_build_for_host $SYSTEM
    303     for ARCH in $ARCHS; do
    304         for VERSION in $GDB_VERSION; do
    305             need_install_host_gdb $SYSTEM android-$ARCH $VERSION
    306         done
    307     done
    308 done
    309 
    310 if [ "$PACKAGE_DIR" ]; then
    311     for SYSTEM in $BH_HOST_SYSTEMS; do
    312         bh_setup_build_for_host $SYSTEM
    313         for ARCH in $ARCHS; do
    314             for VERSION in $GDB_VERSION; do
    315                 bh_stamps_do package_host_gdb-$SYSTEM-$ARCH-$VERSION package_host_gdb $SYSTEM android-$ARCH $VERSION
    316             done
    317         done
    318     done
    319 fi
    320