# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Dan McGee <dan@archlinux.org>
# Contributor: Allan McRae <allan@archlinux.org>

pkgname=valgrind
pkgver=3.17.0
pkgrel=1
pkgdesc='Tool to help find memory-management problems in programs'
arch=('x86_64')
license=('GPL')
url='http://valgrind.org/'
depends=('glibc' 'perl')
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)
validpgpkeys=(
  0E9FFD0C16A1856CF9C7C690BA0166E698FA6035 # Julian Seward <jseward@acm.org>
)
sha512sums=('94de78942a7059e1ab84d1c0c0b8f3efd1c2d15c70b97bc7edc8136812778adb6f8187149d53a60a8c6a7c8b40534f9be5cfed0eb3c0c314545b681f950b108f'
            'SKIP'
            'e0cec39381cefeca09ae4794cca309dfac7c8693e6315e137e64f5c33684598726d41cfbb4edf764fe985503b13ff596184ca5fc32b159d500ec092e4cf8838c')
b2sums=('96569db9bc3c25ca195a4caa64da6e96b9bff7edd82727fccccaa8307162e29f4e150386c7c9611152fc1afde609d0922aa84067957b72dff03810ee23045e9c'
        'SKIP'
        'af556fdf3c02e37892bfe9afebc954cf2f1b2fa9b75c1caacfa9f3b456ebc02bf078475f9ee30079b3af5d150d41415a947c3d04235c1ea8412cf92b959c484a')

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}

  # 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
}

# vim: ts=2 sw=2 et:
