summaryrefslogtreecommitdiff
path: root/build-gdb.sh
blob: c8268c488c26d2b3e21b4d9e2185f374ba68e93c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/bin/sh
#
# Copyright (C) 2012 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Rebuild the host GDB binaries from sources.
#

# include common function and variable definitions
. "$NDK_BUILDTOOLS_PATH/prebuilt-common.sh"
. "$NDK_BUILDTOOLS_PATH/common-build-host-funcs.sh"

PROGRAM_PARAMETERS=""
PROGRAM_DESCRIPTION="\
This program is used to rebuild one or more NDK gdb client programs from
sources.

By default, the script rebuilds GDB for your host system [$HOST_TAG],
but you can use --systems=<tag1>,<tag2>,.. to ask binaries that can run on
several distinct systems. Each <tag> value in the list can be one of the
following:

   linux-x86
   linux-x86_64
   windows
   windows-x86  (equivalent to 'windows')
   windows-x86_64
   darwin-x86
   darwin-x86_64

For example, here's how to rebuild the ARM toolchains on Linux
for four different systems:

  $PROGNAME --toolchain-src-dir=/path/to/toolchain/src \
    --systems=linux-x86,linux-x86_64,windows,windows-x86_64 \
    --arch=arm"

TOOLCHAIN_SRC_DIR=
register_var_option "--toolchain-src-dir=<path>" TOOLCHAIN_SRC_DIR "Select toolchain source directory"

GDB_VERSION="7.9.1"
register_var_option "--gdb-version=<version>" GDB_VERSION "Select GDB version(s)."

BUILD_DIR=
register_var_option "--build-dir=<path>" BUILD_DIR "Build GDB into directory"

PYTHON_VERSION="2.7.5"
register_var_option "--python-version=<version>" PYTHON_VERSION "Python version."

PYTHON_BUILD_DIR="$TMPDIR/buildhost"
register_var_option "--python-build-dir=<path>" PYTHON_BUILD_DIR "Python build directory."

NDK_DIR=$ANDROID_NDK_ROOT
register_var_option "--ndk-dir=<path>" NDK_DIR "Select NDK install directory."

PACKAGE_DIR=
register_var_option "--package-dir=<path>" PACKAGE_DIR "Package prebuilt tarballs into directory."

ARCHS=$DEFAULT_ARCHS
register_var_option "--arch=<list>" ARCHS "Build GDB client for these CPU architectures."

bh_register_options

register_jobs_option

extract_parameters "$@"

if [ -n "$PARAMETERS" ]; then
    panic "This script doesn't take parameters, only options. See --help"
fi

if [ -z "$TOOLCHAIN_SRC_DIR" ]; then
    panic "Please use --toolchain-src-dir=<path> to select toolchain source directory."
fi

BH_HOST_SYSTEMS=$(commas_to_spaces $BH_HOST_SYSTEMS)

# Sanity check for all GDB versions
for VERSION in $(commas_to_spaces $GDB_VERSION); do
    GDB_SRCDIR=$TOOLCHAIN_SRC_DIR/gdb/gdb-$VERSION
    if [ ! -d "$GDB_SRCDIR" ]; then
        panic "Missing source directory: $GDB_SRCDIR"
    fi
done

if [ -z "$BUILD_DIR" ] ; then
    BUILD_DIR=$TMPDIR/buildgdb
fi

bh_setup_build_dir $BUILD_DIR

# Sanity check that we have the right compilers for all hosts
for SYSTEM in $BH_HOST_SYSTEMS; do
    bh_setup_build_for_host $SYSTEM
done

# Return the build install directory of a given GDB version
# $1: host system tag
# $2: target system tag
# $3: gdb version
gdb_build_install_dir ()
{
    echo "$BH_BUILD_DIR/install/$1/gdb-$(get_toolchain_name_for_arch $(bh_tag_to_arch $2))-$3"
}

# $1: host system tag
# $2: target system tag
# $3: gdb version
gdb_ndk_package_name ()
{
    echo "gdb-$(get_toolchain_name_for_arch $(bh_tag_to_arch $2))-$3-$1"
}


# Same as gdb_build_install_dir, but for the final NDK installation
# directory. Relative to $NDK_DIR.
gdb_ndk_install_dir ()
{
    echo "toolchains/gdb-$(get_toolchain_name_for_arch $(bh_tag_to_arch $2))-$3/prebuilt/$(install_dir_from_host_tag $1)"
}

python_build_install_dir ()
{
    echo "$PYTHON_BUILD_DIR/install/prebuilt/$(install_dir_from_host_tag $1)"
}

# $1: host system tag
build_expat ()
{
    local ARGS
    local SRCDIR=$TOOLCHAIN_SRC_DIR/expat/expat-2.0.1
    local BUILDDIR=$BH_BUILD_DIR/build-expat-2.0.1-$1
    local INSTALLDIR=$BH_BUILD_DIR/install-host-$1

    ARGS=" --prefix=$INSTALLDIR"
    ARGS=$ARGS" --disable-shared --enable-static"
    ARGS=$ARGS" --build=$BH_BUILD_CONFIG"
    ARGS=$ARGS" --host=$BH_HOST_CONFIG"

    TEXT="$(bh_host_text) expat:"

    mkdir -p "$BUILDDIR" && rm -rf "$BUILDDIR"/* &&
    cd "$BUILDDIR" &&
    dump "$TEXT Building"
    run "$SRCDIR"/configure $ARGS &&
    run make -j$NUM_JOBS &&
    run make -j$NUM_JOBS install
}

need_build_expat ()
{
    bh_do build_expat $1
}

# $1: host system tag
# $2: target tag
# $3: gdb version
build_host_gdb ()
{
    local SRCDIR=$TOOLCHAIN_SRC_DIR/gdb/gdb-$3
    local BUILDDIR=$BH_BUILD_DIR/build-gdb-$1-$2-$3
    local INSTALLDIR=$(gdb_build_install_dir $1 $2 $3)
    local ARGS TEXT

    if [ ! -f "$SRCDIR/configure" ]; then
        panic "Missing configure script in $SRCDIR"
    fi

    bh_set_target_tag $2
    bh_setup_host_env

    need_build_expat $1
    local EXPATPREFIX=$BH_BUILD_DIR/install-host-$1

    ARGS=" --prefix=$INSTALLDIR"
    ARGS=$ARGS" --disable-shared"
    ARGS=$ARGS" --build=$BH_BUILD_CONFIG"
    ARGS=$ARGS" --host=$BH_HOST_CONFIG"
    ARGS=$ARGS" --target=$(bh_tag_to_config_triplet $2)"
    ARGS=$ARGS" --disable-werror"
    ARGS=$ARGS" --disable-nls"
    ARGS=$ARGS" --disable-docs"
    ARGS=$ARGS" --with-expat"
    ARGS=$ARGS" --with-libexpat-prefix=$EXPATPREFIX"
    if [ -n "$PYTHON_VERSION" ]; then
        ARGS=$ARGS" --with-python=$(python_build_install_dir $BH_HOST_TAG)/bin/python-config.sh"
        if [ $1 = windows-x86 -o $1 = windows-x86_64 ]; then
            # This is necessary for the Python integration to build.
            CFLAGS=$CFLAGS" -D__USE_MINGW_ANSI_STDIO=1"
            CXXFLAGS=$CXXFLAGS" -D__USE_MINGW_ANSI_STDIO=1"
        fi
    fi
    TEXT="$(bh_host_text) gdb-$BH_TARGET_ARCH-$3:"

    mkdir -p "$BUILDDIR" && rm -rf "$BUILDDIR"/* &&
    cd "$BUILDDIR" &&
    dump "$TEXT Building"
    run "$SRCDIR"/configure $ARGS &&
    run make -j$NUM_JOBS &&
    run make -j$NUM_JOBS install
    fail_panic "Failed to configure/make/install gdb"
}

need_build_host_gdb ()
{
    bh_do build_host_gdb $1 $2 $3
}

# Install host GDB binaries and support files to the NDK install dir.
# $1: host tag
# $2: target tag
# $3: gdb version
install_host_gdb ()
{
    local SRCDIR="$(gdb_build_install_dir $1 $2 $3)"
    local DSTDIR="$NDK_DIR/$(gdb_ndk_install_dir $1 $2 $3)"
    local PYDIR="$NDK_DIR/$(python_ndk_install_dir $1)"

    need_build_host_gdb $1 $2 $3

    bh_set_target_tag $2

    dump "$(bh_host_text) gdb-$BH_TARGET_ARCH-$3: Installing"
    run copy_directory "$SRCDIR/bin" "$DSTDIR/bin"
    if [ -d "$SRCDIR/share/gdb" ]; then
        run copy_directory "$SRCDIR/share/gdb" "$DSTDIR/share/gdb"
    fi

    # build the gdb stub and replace gdb with it. This is done post-install
    # so files are in the correct place when determining the relative path.

    case "$1" in
        windows*)
            dump "$TEXT Building gdb-stub"
            bh_setup_host_env
            GCC_FOR_STUB=${BH_HOST_CONFIG}-gcc
            GCC_FOR_STUB_TARGET=`$GCC_FOR_STUB -dumpmachine`
            if [ "$GCC_FOR_STUB_TARGET" = "i586-mingw32msvc" ]; then
                GCC_FOR_STUB=i686-w64-mingw32-gcc
                dump "Override compiler for gdb-stub: $GCC_FOR_STUB"
            fi

            # Uses $TOOLCHAIN_PATH/bin/$(bh_tag_to_config_triplet $2)-gdb.exe (1) instead of
            # ${DSTDIR}/bin/$(bh_tag_to_config_triplet $2)-gdb.exe (2) because
            # the final layout is to (1) which is a folder deeper than (2).
            # Sample (1):
            #  $NDK/gdb-arm-linux-androideabi-4.8/prebuilt/windows/bin/arm-linux-androideabi-gdb.exe
            # Sample (2):
            #  $NDK/toolchains/windows/arm-linux-androideabi-4.8/prebuilt/bin/arm-linux-androideabi-gdb.exe
            run $NDK_BUILDTOOLS_PATH/build-gdb-stub.sh \
                --gdb-executable-path=${DSTDIR}/bin/$(bh_tag_to_config_triplet $2)-gdb.exe \
                --python-prefix-dir=${PYDIR} \
                --mingw-w64-gcc=${GCC_FOR_STUB}
            fail_panic "Failed to build gdb-stub"
            ;;
        *)
            ;;
    esac
}

need_install_host_gdb ()
{
    bh_do install_host_gdb $1 $2 $3
}

# Package host GDB binaries into a tarball
# $1: host tag
# $2: target tag
# $3: gdb version
package_host_gdb ()
{
    local SRCDIR="$(gdb_ndk_install_dir $1 $2 $3)"
    local PACKAGENAME=$(gdb_ndk_package_name $1 $2 $3).tar.bz2
    local PACKAGE="$PACKAGE_DIR/$PACKAGENAME"

    bh_set_target_tag $2

    dump "$(bh_host_text) $PACKAGENAME: Packaging"
    run pack_archive "$PACKAGE" "$NDK_DIR" "$SRCDIR"
}

GDB_VERSION=$(commas_to_spaces $GDB_VERSION)
ARCHS=$(commas_to_spaces $ARCHS)

# Let's build this
for SYSTEM in $BH_HOST_SYSTEMS; do
    bh_setup_build_for_host $SYSTEM
    for ARCH in $ARCHS; do
        for VERSION in $GDB_VERSION; do
            need_install_host_gdb $SYSTEM android-$ARCH $VERSION
        done
    done
done

if [ "$PACKAGE_DIR" ]; then
    for SYSTEM in $BH_HOST_SYSTEMS; do
        bh_setup_build_for_host $SYSTEM
        for ARCH in $ARCHS; do
            for VERSION in $GDB_VERSION; do
                bh_do package_host_gdb $SYSTEM android-$ARCH $VERSION
            done
        done
    done
fi