# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=scudo-malloc
# tied to llvm version
pkgver=20.1.8
pkgrel=0
pkgdesc="Standalone scudo malloc from compiler-rt"
url="https://llvm.org"
arch="all"
license="Apache-2.0 WITH LLVM-exception"
provider_priority=100  # highest
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="linux-headers"
checkdepends="gtest-dev"
subpackages="$pkgname-static $pkgname-dev"
source="https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/compiler-rt-${pkgver//_/}.src.tar.xz
	0001-Add-makefile.patch
	"
builddir="$srcdir/compiler-rt-${pkgver//_/}.src/lib/scudo/standalone"

case "$CARCH" in
ppc64le)
	# tests/common_test.cpp:33: Failure
	# Expected: (OnStart) > (0UL), actual: 0 vs 0
	# https://gitlab.alpinelinux.org/alpine/aports/-/issues/16235
	options="!check"
	;;
esac

build() {
	local cflags_crc32=
	unset CXXFLAGS
	case "$CARCH" in
		aarch64)
			# this has broken emulation in aarch64 qemu (incl qemu-user),
			# which causes it to abort instantly
			CXXFLAGS="-DSCUDO_DISABLE_TBI"
			cflags_crc32="-march=armv8-a+crc"
			;;
		x86*)	cflags_crc32="-mcrc32"
			;;
	esac

	msg "building libscudo"
	make CXXFLAGS="$CXXFLAGS" CXXFLAGS_crc32="$cflags_crc32" TESTS="$(want_check && echo yes || echo no)"
}

check() {
	make test
}

package() {
	install -Dm755 libscudo.so -t "$pkgdir"/usr/lib/
	install -Dm644 libscudo.a -t "$pkgdir"/usr/lib/
	install -Dm644 include/scudo/interface.h -t "$pkgdir"/usr/include/scudo/
}

sha512sums="
905fe22cbcb777255245b8594fb6cbb496c9ad96c962e9c429335a9358c6520f2aa2964d2019a40dd5ac20b0df17569dd62f635078f92d34206700ee06123a1d  compiler-rt-20.1.8.src.tar.xz
1102a512adde2ef31aafa6011a4293d5fa361aa920f88ac20599aab84711e973315e6f6719631be11007aea869ae700e34ed57c7dc7724e8aacbc5b493632849  0001-Add-makefile.patch
"
