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.1.x 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 "gdb-$(get_toolchain_name_for_arch $(bh_tag_to_arch $2))-$3/prebuilt/$1"
    134 }
    135 
    136 python_build_install_dir ()
    137 {
    138     echo "$PYTHON_BUILD_DIR/install/prebuilt/$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-expat-2.0.1-$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-expat-2.0.1-$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=$EXPATPREFIX"
    198     if [ "$PYTHON_VERSION" ]; then
    199         ARGS=$ARGS" --with-python=$(python_build_install_dir $BH_HOST_TAG)/bin/python-config.sh"
    200         if [ $1 = windows-x86 -o $1 = windows-x86_64 ]; then
    201             # This is necessary for the Python integration to build.
    202             CFLAGS=$CFLAGS" -D__USE_MINGW_ANSI_STDIO=1"
    203             CXXFLAGS=$CXXFLAGS" -D__USE_MINGW_ANSI_STDIO=1"
    204         fi
    205     fi
    206 
    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 }
    216 
    217 need_build_host_gdb ()
    218 {
    219     bh_stamps_do host-gdb-$1-$2-$3 build_host_gdb $1 $2 $3
    220 }
    221 
    222 # Install host GDB binaries and support files to the NDK install dir.
    223 # $1: host tag
    224 # $2: target tag
    225 # $3: gdb version
    226 install_host_gdb ()
    227 {
    228     local SRCDIR="$(gdb_build_install_dir $1 $2 $3)"
    229     local DSTDIR="$NDK_DIR/$(gdb_ndk_install_dir $1 $2 $3)"
    230 
    231     need_build_host_gdb $1 $2 $3
    232 
    233     bh_set_target_tag $2
    234 
    235     dump "$(bh_host_text) gdb-$BH_TARGET_ARCH-$3: Installing"
    236     run copy_directory "$SRCDIR/bin" "$DSTDIR/bin"
    237     if [ -d "$SRCDIR/share/gdb" ]; then
    238         run copy_directory "$SRCDIR/share/gdb" "$DSTDIR/share/gdb"
    239     fi
    240 }
    241 
    242 need_install_host_gdb ()
    243 {
    244     bh_stamps_do install-host-gdb-$1-$2-$3 install_host_gdb $1 $2 $3
    245 }
    246 
    247 # Package host GDB binaries into a tarball
    248 # $1: host tag
    249 # $2: target tag
    250 # $3: gdb version
    251 package_host_gdb ()
    252 {
    253     local SRCDIR="$(gdb_ndk_install_dir $1 $2 $3)"
    254     local PACKAGENAME=$(gdb_ndk_package_name $1 $2 $3).tar.bz2
    255     local PACKAGE="$PACKAGE_DIR/$PACKAGENAME"
    256 
    257     need_install_host_gdb $1 $2 $3
    258 
    259     bh_set_target_tag $2
    260 
    261     dump "$(bh_host_text) $PACKAGENAME: Packaging"
    262     run pack_archive "$PACKAGE" "$NDK_DIR" "$SRCDIR"
    263 }
    264 
    265 GDB_VERSION=$(commas_to_spaces $GDB_VERSION)
    266 ARCHS=$(commas_to_spaces $ARCHS)
    267 
    268 # Let's build this
    269 for SYSTEM in $BH_HOST_SYSTEMS; do
    270     bh_setup_build_for_host $SYSTEM
    271     for ARCH in $ARCHS; do
    272         for VERSION in $GDB_VERSION; do
    273             need_install_host_gdb $SYSTEM android-$ARCH $VERSION
    274         done
    275     done
    276 done
    277 
    278 if [ "$PACKAGE_DIR" ]; then
    279     for SYSTEM in $BH_HOST_SYSTEMS; do
    280         bh_setup_build_for_host $SYSTEM
    281         for ARCH in $ARCHS; do
    282             for VERSION in $GDB_VERSION; do
    283                 package_host_gdb $SYSTEM android-$ARCH $VERSION
    284             done
    285         done
    286     done
    287 fi
    288