# 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=16.3
pkgrel=1
pkgdesc="GNU Debugger (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.gnu.org/software/gdb/"
msys2_references=(
  "cpe: cpe:/a:gnu:gdb"
)
license=('spdx:GPL-3.0-or-later')
groups=($( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-toolchain" ))
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-mpfr"
         "${MINGW_PACKAGE_PREFIX}-ncurses"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-xxhash"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-pygments: for syntax highlighting")
checkdepends=('dejagnu' 'bc')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-iconv")
source=(https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.xz{,.sig}
        '0001-Workaround-performance-regression-in-info-func-var-t.patch'
        '0002-Fix-using-gnu-print.patch'
        '0003-configure-Disable-static-linking-with-standard-libs.patch'
        '0004-Python-Configure-path-fixes.patch'
        '0005-W32-Always-check-USERPROFILE-if-HOME-is-not-set.patch')
validpgpkeys=('F40ADB902B24264AA42E50BF92EDB04BFF325CF3')
sha256sums=('bcfcd095528a987917acf9fff3f1672181694926cc18d609c99d0042c00224c5'
            'SKIP'
            '428cda7dcc107c236225ae89704390b0f1e70c276b5ca6b1741988f384b21d62'
            'cf12bce0b988765ecac26a6895238c48ef9676d6fc01f986f75dcbc8bd8d3f07'
            '0eb291cd81f7392610d16a83f436a30d3384a99661c6b6ffd1bfb243d5aee6dd'
            '7ef9c6e238a4e232bc689e15e48ee2d8045542c47f0b156d9fc92a7c14e6757e'
            '39d1cb2a1be8d60c16404ad96882f10cd3ebd942d8b7af62a7416a230a50de93')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

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

  rm gdb/doc/GDBvn.texi
  rm gdb/doc/version.subst

  # https://sourceware.org/bugzilla/show_bug.cgi?id=15412
  apply_patch_with_msg \
    0001-Workaround-performance-regression-in-info-func-var-t.patch

  apply_patch_with_msg \
    0002-Fix-using-gnu-print.patch

  # https://sourceware.org/bugzilla/show_bug.cgi?id=21078
  apply_patch_with_msg \
    0003-configure-Disable-static-linking-with-standard-libs.patch

  apply_patch_with_msg \
    0004-Python-Configure-path-fixes.patch \
    0005-W32-Always-check-USERPROFILE-if-HOME-is-not-set.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-${MSYSTEM}$1
  mkdir -p ${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"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    $2 \
    --enable-64-bit-bfd \
    --disable-werror \
    --disable-win32-registry \
    --disable-rpath \
    --disable-sim \
    --with-curses \
    --with-system-gdbinit=${MINGW_PREFIX}/etc/gdbinit \
    --with-system-readline \
    --with-system-zlib \
    --with-python=${MINGW_PREFIX}/bin/python \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    --with-{expat,gmp,mpfr,lzma,zstd}=${MINGW_PREFIX} \
    --with-{libexpat,libintl,libiconv,liblzma,libxxhash}-type=shared \
    --enable-tui \
    --enable-source-highlight=no

  make
}

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

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

  # Remove unwanted files
  rm -f "${pkgdir}"${MINGW_PREFIX}/share/info/{bfd.info*,ctf-spec.info*,sframe-spec.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}=${pkgver}")
  provides=("${MINGW_PACKAGE_PREFIX}-arm-none-eabi-gdb=${pkgver}" "${MINGW_PACKAGE_PREFIX}-avr-gdb=${pkgver}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-arm-none-eabi-gdb" "${MINGW_PACKAGE_PREFIX}-avr-gdb")
  replaces=("${MINGW_PACKAGE_PREFIX}-arm-none-eabi-gdb" "${MINGW_PACKAGE_PREFIX}-avr-gdb")

  builddir="${srcdir}/build-${MSYSTEM}-multiarch"
  cd ${builddir}
  make DESTDIR=${builddir}/inst install-gdb install-gdbserver

  destdir="${pkgdir}"${MINGW_PREFIX}/bin
  bindir=${builddir}/inst${MINGW_PREFIX}/bin
  mkdir -p $destdir
  cp -p ${bindir}/gdb.exe ${destdir}/gdb-multiarch.exe
  cp -p ${bindir}/gdbserver.exe ${destdir}/gdbserver-multiarch.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;
