#!/usr/bin/bash
# JOBoRun		: Jwm OpenBox Obarun RUNit
# Maintainer	: Joe Bo Run <joborun@disroot.org>
# PkgSource		: url="https://gittea.disroot.org/joborun-pkg/jobextra/$pkgname"
# Website		: https://pozol.eu
#-----------------------------------------| DESCRIPTION |---------------------------------------

# README
# this package requires glibc-debug to run the check() function
# as -debug packages are not available in [core], it has to be applied manually
# whenever the tests shall run
#
# For a fresh build:
# $repo-x86_64-build -- -I ../../glibc/trunk/glibc-debug*.pkg.tar.zst
# $repo-x86_64-build
# ${repo}pkg

pkgname=valgrind
pkgver=3.20.0
pkgrel=01
pkgdesc='Tool to help find memory-management problems in programs'
arch=('x86_64')
url='https://valgrind.org/'
depends=('glibc' 'perl' 'debuginfod')
makedepends=('gdb' 'lib32-glibc' 'lib32-gcc-libs' 'docbook-xml'
             'docbook-xsl' 'docbook-sgml')
checkdepends=('procps-ng')
optdepends=('lib32-glibc: 32-bit ABI support')
provides=('valgrind-multilib')
replaces=('valgrind-multilib')
options=('!emptydirs' '!strip')
source=(https://sourceware.org/pub/valgrind/valgrind-${pkgver}.tar.bz2{,.asc}
        valgrind-3.7.0-respect-flags.patch)
options=(!lto) # https://bugs.kde.org/show_bug.cgi?id=338252

prepare() {
  cd valgrind-${pkgver}
  patch -Np1 < ../valgrind-3.7.0-respect-flags.patch
  sed -i 's|sgml/docbook/xsl-stylesheets|xml/docbook/xsl-stylesheets-1.79.2-nons|' docs/Makefile.am

  autoreconf -ifv
}

build() {
  # valgrind does not like some of our flags
  CPPFLAGS=${CPPFLAGS/-D_FORTIFY_SOURCE=2/}
  CFLAGS=${CFLAGS/-fno-plt/}
  CXXFLAGS=${CXXFLAGS/-fno-plt/}

  cd valgrind-${pkgver}
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --libexecdir=/usr/lib \
    --mandir=/usr/share/man
  make
  make -C docs man-pages
}

#check() {
#  cd valgrind-${pkgver}
#  # only run if glibc-debug is supplied manually
#  if ! pacman -Q glibc-debug; then echo -e "\033[1;31mcheck() not run, supply glibc-debug if unintended!\033[0m"; return 0; fi

  # Make sure a basic binary runs. There should be no errors.
#  ./vg-in-place --error-exitcode=1 /bin/true

  # Make sure no extra FLAGS leak through, the testsuite
  # sets all flags necessary. See also configure above.
#  make check CPPFLAGS= CFLAGS= CXXFLAGS= LDFLAGS=

  # XXX: run full regtest but only report issues some tests fail duo
  # current toolchain and expectations, take a manual look if its fine
  #echo "===============TESTING==================="
  #make regtest || true

  # Make sure test failures show up in build.log
  # Gather up the diffs (at most the first 20 lines for each one)
  #local f max_lines=20 diff_files=()
  #mapfile -d '' diff_files < <(find . -name '*.diff' -print0 | sort -z)
  #if (( ${#diff_files[@]} == 0 )); then
    #echo "Congratulations, all tests passed!"
  #else
    #warning "Some tests failed!"
    #for f in "${diff_files[@]}"; do
        #echo "================================================="
        #echo "${f}"
        #echo "================================================="
        #if (( $(wc -l < "${f}") < ${max_lines} )); then
          #cat "${f}"
        #else
          #head -n ${max_lines} "${f}"
          #echo "<truncated beyond ${max_lines} lines>"
        #fi
    #done | tee diffs
  #fi
  #echo "===============END TESTING==============="
#}

package() {
  cd valgrind-${pkgver}
  make DESTDIR="${pkgdir}" install

  install -Dm644 docs/*.1 -t "$pkgdir/usr/share/man/man1"

  if check_option 'debug' n; then
    find "${pkgdir}/usr/bin" -type f -executable -exec strip $STRIP_BINARIES {} + || :
  fi
}

#---- license gpg-key sha256sums ----

license=('GPL')

validpgpkeys=(
  0E9FFD0C16A1856CF9C7C690BA0166E698FA6035 # Julian Seward <jseward@acm.org>
  EC3CFE88F6CA0788774F5C1D1AA44BE649DE760A # Mark Wielaard <mjw@gnu.org>
)

sha256sums=(8536c031dbe078d342f121fa881a9ecd205cb5a78e639005ad570011bdb9f3c6  # valgrind-3.20.0.tar.bz2
	6685053c20f117c89bc588bdbdcccfb20dba0f2a3de855745eea45079cc533de  # valgrind-3.20.0.tar.bz2.asc
	4e6be3a1799c17a6e843ab1966a3a68ac0ffa83d4b2230ce1b607518c42a31a2) # valgrind-3.7.0-respect-flags.patch

##  b660f7db33e3dfabf5fecc91202f95405bc48d7dcfc143a8786cad01dd83b0dc  valgrind-3.20.0-01-x86_64.pkg.tar.lz

