summaryrefslogtreecommitdiff
path: root/scripts/build/debug/300-gdb.sh
blob: 15e0684d7ab54b116d1ac00aabe3cad37b2b8dbf (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
# Build script for the gdb debug facility

do_debug_gdb_get()
{
    CT_Fetch GDB
}

do_debug_gdb_extract()
{
    CT_ExtractPatch GDB

    # Workaround for bad versions, where the configure
    # script for gdbserver is not executable...
    # Bah, GNU folks strike again... :-(
    chmod a+x "${CT_SRC_DIR}/gdb/gdb/gdbserver/configure"
}

do_debug_gdb_build()
{
    if [ "${CT_GDB_CROSS}" = "y" ]; then
        local gcc_version p _p
        local -a cross_extra_config

        CT_DoStep INFO "Installing cross-gdb"
        CT_mkdir_pushd "${CT_BUILD_DIR}/build-gdb-cross"

        cross_extra_config=( "${CT_GDB_CROSS_EXTRA_CONFIG_ARRAY[@]}" )
        if [ "${CT_GDB_CROSS_PYTHON}" = "y" ]; then
            if [ -z "${CT_GDB_CROSS_PYTHON_BINARY}" ]; then
                if [ "${CT_CANADIAN}" = "y" -o "${CT_CROSS_NATIVE}" = "y" ]; then
                    CT_Abort "For canadian build, Python wrapper runnable on the build machine must be provided. Set CT_GDB_CROSS_PYTHON_BINARY."
                elif [ "${CT_CONFIGURE_has_python}" = "y" ]; then
                    cross_extra_config+=("--with-python=${python}")
                else
                    CT_Abort "Python support requested in GDB, but Python not found. Set CT_GDB_CROSS_PYTHON_BINARY."
                fi
            else
                cross_extra_config+=("--with-python=${CT_GDB_CROSS_PYTHON_BINARY}")
            fi
        else
            cross_extra_config+=("--with-python=no")
        fi

        if [ "${CT_GDB_CROSS_SIM}" = "y" ]; then
            cross_extra_config+=("--enable-sim")
        else
            cross_extra_config+=("--disable-sim")
        fi

        if ${CT_HOST}-gcc --version 2>&1 | grep clang; then
            # clang detects the line from gettext's _ macro as format string
            # not being a string literal and produces a lot of warnings - which
            # ct-ng's logger faithfully relays to user if this happens in the
            # error() function. Suppress them.
            cross_extra_config+=("--enable-build-warnings=,-Wno-format-nonliteral,-Wno-format-security")
        fi

        do_gdb_backend \
            buildtype=cross \
            host="${CT_HOST}" \
            cflags="${CT_CFLAGS_FOR_HOST}" \
            ldflags="${CT_LDFLAGS_FOR_HOST}" \
            prefix="${CT_PREFIX_DIR}" \
            static="${CT_GDB_CROSS_STATIC}" \
            --with-sysroot="${CT_SYSROOT_DIR}"          \
            "${cross_extra_config[@]}"

        if [ "${CT_BUILD_MANUALS}" = "y" ]; then
            CT_DoLog EXTRA "Building and installing the cross-GDB manuals"
            CT_DoExecLog ALL make ${JOBSFLAGS} pdf html
            CT_DoExecLog ALL make install-{pdf,html}-gdb
        fi

        if [ "${CT_GDB_INSTALL_GDBINIT}" = "y" ]; then
            CT_DoLog EXTRA "Installing '.gdbinit' template"
            # See in scripts/build/internals.sh for why we do this
            # TBD GCC 3.x and older not supported
            if [ -f "${CT_SRC_DIR}/gcc/gcc/BASE-VER" ]; then
                gcc_version=$(cat "${CT_SRC_DIR}/gcc/gcc/BASE-VER")
            else
                gcc_version=$(sed -r -e '/version_string/!d; s/^.+= "([^"]+)".*$/\1/;'   \
                                   "${CT_SRC_DIR}/gcc/gcc/version.c"   \
                             )
            fi
            sed -r                                                  \
                   -e "s:@@PREFIX@@:${CT_PREFIX_DIR}:;"             \
                   -e "s:@@VERSION@@:${gcc_version}:;"              \
                   "${CT_LIB_DIR}/scripts/build/debug/gdbinit.in"   \
                   >"${CT_PREFIX_DIR}/share/gdb/gdbinit"
        fi # Install gdbinit sample

        CT_Popd
        CT_EndStep
    fi

    if [ "${CT_GDB_NATIVE}" = "y" -o "${CT_GDB_GDBSERVER}" = "y" ]; then
        local -a native_extra_config
        local subdir

        CT_DoStep INFO "Installing native gdb"
        CT_mkdir_pushd "${CT_BUILD_DIR}/build-gdb-native"

        native_extra_config+=("--program-prefix=")

        # GDB on Mingw depends on PDcurses, not ncurses
        if [ "${CT_MINGW32}" != "y" ]; then
            native_extra_config+=("--with-curses")
        fi

        # Build a native gdbserver if needed. If building only
        # gdbserver, configure in the subdirectory.
        # Newer versions enable it automatically for a native target by default.
        if [ "${CT_GDB_GDBSERVER}" != "y" ]; then
            native_extra_config+=("--disable-gdbserver")
        else
            native_extra_config+=("--enable-gdbserver")
            if [ "${CT_GDB_NATIVE_BUILD_IPA_LIB}" = "y" ]; then
                gdbserver_extra_config+=("--enable-inprocess-agent")
            else
                gdbserver_extra_config+=("--disable-inprocess-agent")
            fi
            if [ "${CT_GDB_NATIVE}" != "y" ]; then
                subdir=gdb/gdbserver/
            fi
        fi

        export ac_cv_func_strncmp_works=yes

        # TBD do we need all these? Eg why do we disable TUI if we build curses for target?
        native_extra_config+=(
            --without-uiout
            --disable-tui
            --disable-gdbtk
            --without-x
            --disable-sim
            --without-included-gettext
            --without-develop
            --sysconfdir=/etc
            --localstatedir=/var
        )

        do_gdb_backend \
            buildtype=native \
            subdir=${subdir} \
            host="${CT_TARGET}" \
            cflags="${CT_ALL_TARGET_CFLAGS}" \
            ldflags="${CT_ALL_TARGET_LDFLAGS}" \
            static="${CT_GDB_NATIVE_STATIC}" \
            static_libstdc="${CT_GDB_NATIVE_STATIC_LIBSTDC}" \
            prefix=/usr \
            destdir="${CT_DEBUGROOT_DIR}" \
            "${native_extra_config[@]}"

        unset ac_cv_func_strncmp_works

        CT_Popd
        CT_EndStep # native gdb build
    fi
}

do_gdb_backend()
{
    local host prefix destdir cflags ldflags static buildtype subdir
    local -a extra_config

    for arg in "$@"; do
        case "$arg" in
            --*)
                extra_config+=("${arg}")
                ;;
            *)
                eval "${arg// /\\ }"
                ;;
        esac
    done

    # Starting with glibc 2.25, it now provides a <proc_service.h> header. The
    # problem is that GDB releases prior to 7.12 used to implement one of the
    # interfaces, ps_get_thread_are with a const qualifier on one of the arguments.
    # Therefore, such older versions cannot be compiled against a newer glibc.
    # If we detect such a combination, mitigate by creating a local proc_service.h
    # with a prototype adjusted for GDB requirements.
    if [ -r "${CT_HEADERS_DIR}/proc_service.h" -a "${CT_GDB_CONST_GET_THREAD_AREA}" = "y" ]; then
        CT_DoLog DEBUG "Fixing up the prototype in <proc_service.h>"
        CT_DoExecLog ALL mkdir -p gdb/gdbserver
        CT_DoExecLog ALL cp "${CT_HEADERS_DIR}/proc_service.h" gdb/proc_service.h
        CT_DoExecLog ALL sed -i \
            "s/\(ps_get_thread_area *(\).*\(struct ps_prochandle\)/\1const \2/" \
            gdb/proc_service.h
        CT_DoExecLog ALL cp gdb/proc_service.h gdb/gdbserver/proc_service.h
    fi

    if [ "${CT_GDB_HAS_PKGVERSION_BUGURL}" = "y" ]; then
        [ -n "${CT_PKGVERSION}" ] && extra_config+=("--with-pkgversion=${CT_PKGVERSION}")
        [ -n "${CT_TOOLCHAIN_BUGURL}" ] && extra_config+=("--with-bugurl=${CT_TOOLCHAIN_BUGURL}")
    fi

    # Disable binutils options when building from the binutils-gdb repo.
    extra_config+=("--disable-binutils")
    extra_config+=("--disable-ld")
    extra_config+=("--disable-gas")

    if [ "${CT_GDB_HAS_DISABLE_CXX_BUILD}" = "y" ]; then
        extra_config+=("--disable-build-with-cxx")
    fi

    case "${CT_THREADS}" in
        none)   extra_config+=("--disable-threads");;
        *)      extra_config+=("--enable-threads");;
    esac

    if [ "${CT_TOOLCHAIN_ENABLE_NLS}" != "y" ]; then
        extra_config+=("--disable-nls")
    fi

    # Target libexpat resides in sysroot and does not have
    # any dependencies, so just passing '-lexpat' to gcc is enough.
    #
    # By default gdb configure looks for expat in '$prefix/lib'
    # directory. In our case '$prefix/lib' resolves to '/usr/lib'
    # where libexpat for build platform lives, which is
    # unacceptable for cross-compiling.
    #
    # To prevent this '--without-libexpat-prefix' flag must be passed.
    # Thus configure falls back to '-lexpat', which is exactly what we want.
    #
    # NOTE: DO NOT USE --with-libexpat-prefix (until GDB configure is smarter)!!!
    # It conflicts with a static build: GDB's configure script will find the shared
    # version of expat and will attempt to link that, despite the -static flag.
    # The link will fail, and configure will abort with "expat missing or unusable"
    # message.
    extra_config+=("--with-expat")
    extra_config+=("--without-libexpat-prefix")

    if [ "${static}" = "y" ]; then
        cflags+=" -static"
        ldflags+=" -static"
    fi
    if [ "${static_libstdc}" = "y" ]; then
        ldflags+=" -static-libstdc++"
    fi


    # Fix up whitespace. Some older GDB releases (e.g. 6.8a) get confused if there
    # are multiple consecutive spaces: sub-configure scripts replace them with a
    # single space and then complain that $CC value changed from that in
    # the master directory.
    cflags=`echo ${cflags}`
    ldflags=`echo ${ldflags}`

    CT_DoLog EXTRA "Configuring ${buildtype} gdb"
    CT_DoLog DEBUG "Extra config passed: '${extra_config[*]}'"

    # Run configure/make in the matching subdirectory so that any fixups
    # prepared in a given subdirectory apply.
    if [ -n "${subdir}" ]; then
        CT_mkdir_pushd "${subdir}"
    fi

    # TBD: is passing CPP/CC/CXX/LD needed? GCC should be determining this automatically from the triplets
    CT_DoExecLog CFG                                \
    CPP="${host}-cpp"                               \
    CC="${host}-gcc"                                \
    CXX="${host}-g++"                               \
    LD="${host}-ld"                                 \
    CFLAGS="${cflags}"                              \
    CXXFLAGS="${cflags}"                            \
    LDFLAGS="${ldflags}"                            \
    ${CONFIG_SHELL}                                 \
    "${CT_SRC_DIR}/gdb/${subdir}configure"          \
        --build=${CT_BUILD}                         \
        --host=${host}                              \
        --target=${CT_TARGET}                       \
        --prefix="${prefix}"                        \
        --with-build-sysroot="${CT_SYSROOT_DIR}"    \
        --includedir="${CT_HEADERS_DIR}"            \
        --disable-werror                            \
        "${extra_config[@]}"                        \

    CT_DoLog EXTRA "Building ${buildtype} gdb"
    CT_DoExecLog ALL make ${JOBSFLAGS}

    CT_DoLog EXTRA "Installing ${buildtype} gdb"
    CT_DoExecLog ALL make install ${destdir:+DESTDIR="${destdir}"}

    if [ -n "${subdir}" ]; then
        CT_Popd
    fi
}