# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=gdb
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-multiarch")
pkgver=10.2
pkgrel=1
pkgdesc="GNU Debugger (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.gnu.org/software/gdb/"
license=('GPL')
groups=($( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-toolchain" ))
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libssp"
         "${MINGW_PACKAGE_PREFIX}-ncurses"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-xxhash"
         "${MINGW_PACKAGE_PREFIX}-zlib")
checkdepends=('dejagnu' 'bc')
makedepends=("${MINGW_PACKAGE_PREFIX}-iconv"
             "${MINGW_PACKAGE_PREFIX}-xz")
options=('staticlibs' '!distcc' '!ccache')
source=(https://ftp.gnu.org/gnu/gdb/${_realname}-${pkgver}.tar.xz{,.sig}
        'gdb-perfomance.patch'
        'gdb-fix-using-gnu-print.patch'
        'gdb-7.12-dynamic-libs.patch'
        'python-configure-path-fixes.patch'
        'gdb-fix-tui-with-pdcurses.patch'
        'gdb-lib-order.patch'
        'gdb-home-is-userprofile.patch')
validpgpkeys=('F40ADB902B24264AA42E50BF92EDB04BFF325CF3')
sha256sums=('aaa1223d534c9b700a8bec952d9748ee1977513f178727e1bee520ee000b4f29'
            'SKIP'
            'a0a2d974e80b656646c87cc669fda798182a35b001aab933567c41b880530aa0'
            '59856cfcf0beed270d8b444e2fa7fabb413ee0fb9f3a763f63ad82d65515f838'
            '9de9c30bbb8cd2c6f60414b5a34cdf1fae27238293fb6007c628a8b821f88e0b'
            '6378e1a96e3bedc2a160f0f6780cb973ce6139017f2786e7ab97f8bcd9824d27'
            '6d9b91ca2823207c77b587d6e16a56ca3db1a08bda3d7d09292a25ffe03fafe0'
            'ac38fca803d1626c858a0ebc046f9670547c36543eace767c73bc7b59313ea51'
            '4c8f889202a1fa3ef684c96063ce7c0e949274a11998c7f95b45bb7032f15b4a')

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

  # https://sourceware.org/bugzilla/show_bug.cgi?id=15412
  patch -p1 -i ${srcdir}/gdb-perfomance.patch

  patch -p1 -i ${srcdir}/gdb-fix-using-gnu-print.patch

  # https://sourceware.org/bugzilla/show_bug.cgi?id=21078
  patch -p1 -i ${srcdir}/gdb-7.12-dynamic-libs.patch

  patch -p1 -i ${srcdir}/python-configure-path-fixes.patch

  patch -p1 -i ${srcdir}/gdb-fix-tui-with-pdcurses.patch
  patch -p1 -i ${srcdir}/gdb-lib-order.patch
  patch -p1 -i ${srcdir}/gdb-home-is-userprofile.patch

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

do_build() {
  builddir=${srcdir}/build-${MINGW_CHOST}$1
  [[ -d ${builddir} ]] && rm -rf ${builddir}
  mkdir ${builddir} && cd ${builddir}

  if [ "${CARCH}" != "x86_64" ]; then
    LDFLAGS+=" -Wl,--large-address-aware"
  fi

  if check_option "debug" "y"; then
    CFLAGS+=" -O0"
    CXXFLAGS+=" -O0"
  fi


  CPPFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"
  CFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"
  CXXFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"
  LDFLAGS+=" -fstack-protector"

  ../${_realname}-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    $2 \
    --enable-64-bit-bfd \
    --disable-werror \
    --disable-win32-registry \
    --disable-rpath \
    --with-curses \
    --with-system-gdbinit=${MINGW_PREFIX}/etc/gdbinit \
    --with-system-readline \
    --with-python=${MINGW_PREFIX}/bin/python \
    --with-expat \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    --with-zlib \
    --with-lzma \
    --enable-tui \
    --enable-source-highlight=no

  make
}

build() {
  do_build
  do_build -multiarch --enable-targets=all
}

package_gdb() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  # Remove unwanted files
  rm -rf ${pkgdir}${MINGW_PREFIX}/share/{man,info}

  rm -f ${pkgdir}${MINGW_PREFIX}/include/*.h
  rm -f ${pkgdir}${MINGW_PREFIX}/lib/*.a
}

package_gdb-multiarch() {
  pkgdesc="GNU Debugger (supports all targets)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  destdir=${pkgdir}${MINGW_PREFIX}/bin
  mkdir -p $destdir
  strip -o ${destdir}/gdb-multiarch.exe ${srcdir}/build-${MINGW_CHOST}-multiarch/gdb/gdb.exe
  strip -o ${destdir}/gdbserver-multiarch.exe ${srcdir}/build-${MINGW_CHOST}-multiarch/gdbserver/gdbserver.exe
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
