# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=valgrind
pkgver=3.25.1
pkgrel=1
pkgdesc="Tool to help find memory-management problems in programs"
url="https://valgrind.org/"
# armv6 not supported upstream
arch="all !armhf !loongarch64"
license="GPL-2.0-or-later"
# it seems like busybox sed works but the configure script requires GNU sed
makedepends="sed perl linux-headers"
# from README_PACKAGERS:
#   Don't strip the debug info off lib/valgrind/$platform/vgpreload*.so
#   [and] libexec/valgrind/* in the installation tree.
options="!strip"
subpackages="$pkgname-scripts $pkgname-dev $pkgname-doc"
source="https://sourceware.org/pub/valgrind/valgrind-$pkgver.tar.bz2
	fix-tests-musl-s390x.patch
	fix-tests-musl-armv7.patch
	"

build() {
	export CFLAGS="${CFLAGS/-fno-plt} -fno-stack-protector -no-pie -U_FORTIFY_SOURCE"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--without-mpicc
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install

	# we have options=!strip above so we strip the /usr/bin/* manually
	if [ -z "$DEBUG" ]; then
		scanelf -BRy "$pkgdir"/usr/bin | awk '{print $2}' | xargs strip
	fi
}

scripts() {
	pkgdesc="$pkgdesc (perl+python cachegrind/callgrind script tools)"
	depends="$pkgname=$pkgver-r$pkgrel python3 perl"

	amove \
		usr/bin/ms_print \
		usr/bin/cg_merge \
		usr/bin/cg_annotate \
		usr/bin/cg_diff \
		usr/bin/callgrind_control \
		usr/bin/callgrind_annotate
}

sha512sums="
6a474d58730274c57d6ca012ffd3226d393d6507cd57bf46c3b26c87a0cba84e3c8cfc84c8c76e50041861e98baba7b9613490faef4212d082845581d08af351  valgrind-3.25.1.tar.bz2
a6c5a33d0d5c09cc65c6c62456e213006560b20f37db0a24bf218c5bd2eb3275541494866699d15bbbc2672292122cb6f6d67176b0f37ba1dec37211c584c480  fix-tests-musl-s390x.patch
5e0f6b10081318fc5630a7500d490ff5e9b2832889158ecb40a4f310f8be6fcbd8a8744be96742341eba952a0994e407db091673a69d111fae3e132e0ea6ca72  fix-tests-musl-armv7.patch
"
