# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=valgrind
pkgver=3.24.0
pkgrel=0
pkgdesc="Tool to help find memory-management problems in programs"
url="https://valgrind.org/"
# armv6/riscv64 not supported upstream
arch="all !armhf !riscv64 !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
#   in the installation tree.  Either Valgrind won't work at all, or it
#   will still work if you do, but will generate less helpful error
#   messages.
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 -Ry "$pkgdir" \
			| awk '$1=="ET_DYN"{printf("%s%c",$2,0)}' \
			| xargs -0 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="
6c131ff93d1f432d8362a47285cc377cba224ebca4f18e922c6156f7736d43020ece14d8fd16913498ab00fd18c85e2042a7d5351c3901e80413c584ebb406f3  valgrind-3.24.0.tar.bz2
a6c5a33d0d5c09cc65c6c62456e213006560b20f37db0a24bf218c5bd2eb3275541494866699d15bbbc2672292122cb6f6d67176b0f37ba1dec37211c584c480  fix-tests-musl-s390x.patch
5e0f6b10081318fc5630a7500d490ff5e9b2832889158ecb40a4f310f8be6fcbd8a8744be96742341eba952a0994e407db091673a69d111fae3e132e0ea6ca72  fix-tests-musl-armv7.patch
"
