# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=gdb
pkgver=10.2
pkgrel=3
_gcc_ver=10.2.0
pkgdesc="GNU Debugger (MSYS2 version)"
arch=('i686' 'x86_64')
license=('GPL3')
url="https://www.gnu.org/software/gdb/"
groups=('base-devel')
depends=("libiconv" "zlib" "expat" "python" "libexpat" "libreadline" "mpfr" "xxhash")
#checkdepends=('dejagnu' 'bc')
makedepends=("libiconv-devel" "zlib-devel" "ncurses-devel" "liblzma-devel" "mpfr-devel" "libexpat-devel" "libreadline-devel" "python-devel")
options=('staticlibs' '!distcc' '!ccache')
source=("https://ftp.gnu.org/gnu/gdb/gdb-${pkgver}.tar.xz"{,.sig}
        'gdbinit'
        0001-Teach-gdb-how-to-unwind-cygwin-_sigbe-and-sigdelayed.patch
        0002-7.8-windows-nat-cygwin.patch
        0003-Better-handling-for-realpath-failures-in-windows_mak.patch
        0004-7.8-symtab-cygwin.patch
        0005-msysize.patch)
validpgpkeys=('F40ADB902B24264AA42E50BF92EDB04BFF325CF3')
sha256sums=('aaa1223d534c9b700a8bec952d9748ee1977513f178727e1bee520ee000b4f29'
            'SKIP'
            '2bbe7eddb1828c394d0ff99777058df79b1a596172603bb0e30d983fc1ea8785'
            'bebb9adc23867939e4fb04639ef0da4c63aa09faad221f20c1d5fc810317f198'
            'cbb6162bb222161906f1425e76f630b724bdfe807d11748803e887e2f6ec3b13'
            'cdf8e29386848652069754d95089c6fd3f93cad939a8eed6e6a9875550eac3e0'
            'caa97f691e22e10c0b57251f31ca79a17a25bb77a351dbb80ec47a95fa577d49'
            '10193948948d6dd8334dff410e058d90dbaea8af66d74a2a9d058270a2a336ae')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}

  patch -p1 -i ${srcdir}/0001-Teach-gdb-how-to-unwind-cygwin-_sigbe-and-sigdelayed.patch
  patch -p1 -i ${srcdir}/0002-7.8-windows-nat-cygwin.patch
  patch -p1 -i ${srcdir}/0003-Better-handling-for-realpath-failures-in-windows_mak.patch
  patch -p1 -i ${srcdir}/0004-7.8-symtab-cygwin.patch
  patch -p1 -i ${srcdir}/0005-msysize.patch

  # hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure
}

build() {
  [[ -d "${srcdir}/build-${CHOST}" ]] && rm -rf "${srcdir}/build-${CHOST}"
  mkdir -p "${srcdir}/build-${CHOST}" && pushd "${srcdir}/build-${CHOST}"

  ../${pkgname}-${pkgver}/configure \
    --build=${CHOST} \
    --host=${CHOST} \
    --prefix=/usr \
    --enable-64-bit-bfd \
    --disable-werror \
    --disable-staticlib \
    --with-system-gdbinit=/etc/gdbinit \
    --with-python=/usr/bin/python3 \
    --without-tcl \
    --without-tk \
    --without-guile \
    --with-expat \
    --with-lzma \
    --with-mpfr \
    --without-libexpat-prefix \
    --with-system-readline \
    --with-libiconv-prefix=/usr \
    --with-system-zlib \
    --enable-tui
  make
}

check() {
  cd ${srcdir}/build-${CHOST}
  make check
}

package() {
  cd ${srcdir}/build-${CHOST}
  make DESTDIR=${pkgdir} install

  # install "custom" system gdbinit
  install -D -m644 ${srcdir}/gdbinit ${pkgdir}/etc/gdbinit
  sed -i 's|%GCC_NAME%|gcc-'${_gcc_ver}'|g' ${pkgdir}/etc/gdbinit

  # these are shipped by binutils
  rm -fr ${pkgdir}/usr/{include,lib}/ ${pkgdir}/usr/share/locale/
  rm -f ${pkgdir}/usr/share/info/{bfd,configure,standards}.info
}
