# Contributor: Marian <marian.buschsieweke@posteo.net>
# Maintainer: Marian <marian.buschsieweke@posteo.net>
pkgname=afl++
pkgver=4.32c
pkgrel=0
pkgdesc="Fuzzer relying on genetic algorithms instead of brute force"
url="https://aflplus.plus/"
# x86: test failures
arch="all !x86"
license="Apache-2.0"
# afl-cmin requires stat
_llvmver=20
# llvm LTO test fails on riscv64
# https://github.com/AFLplusplus/AFLplusplus/issues/2064
case "$CARCH" in
	riscv64) options="$options !check";;
esac
depends="
	clang$_llvmver
	clang$_llvmver-rtlib
	lld$_llvmver
	llvm$_llvmver
	python3
	"
# install -T
makedepends="
	bash
	clang$_llvmver-dev
	coreutils
	gmp-dev
	grep
	llvm$_llvmver-dev
	python3-dev
	"
checkdepends="
	cmocka-dev
	"
subpackages="
	$pkgname-doc
	afl++-tools
	"
provides="
	afl=$pkgver-r$pkgrel
	afl-clang=$pkgver-r$pkgrel
	afl-gcc=$pkgver-r$pkgrel
	"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/AFLplusplus/AFLplusplus/archive/refs/tags/v$pkgver.tar.gz

	extend-64bit-detection.patch
	"
builddir="$srcdir/AFLplusplus-$pkgver"

build() {
	export PATH="$PATH:/usr/lib/llvm$_llvmver/bin"
	export NO_NYX=1
	export AFL_NO_X86=1
	export LLVM_LTO=1
	make PREFIX=/usr all
}

check() {
	# Unset our CFLAGS/CXXFLAGS for the tests since these may
	# interact in unexpected ways with afl-cc instrumentation.
	CFLAGS= CXXFLAGS= make AFL_NO_X86=1 test
}

package() {
	make AFL_NO_X86=1 PREFIX=/usr DESTDIR="$pkgdir" install

	# Test cases contain x86 binaries that cause trouble with strip on non-x86
	# architectures, so just drop the test cases here.
	rm -rf "$pkgdir"/usr/share/afl/testcases/

	# afl-cmin.bash is the same as afl-cmin, but implemented differently making
	# use of bash features. No need for the same functionality twice.
	rm "$pkgdir"/usr/bin/afl-cmin.bash
}

tools() {
	pkgdesc="AFL++ tools and utilities"
	depends="
		afl++
		coreutils
		grep
		"

	amove usr/bin/afl-cmin
	amove usr/bin/afl-plot
	amove usr/bin/afl-showmap
	amove usr/bin/afl-tmin
}

sha512sums="
9688f476129b45e842918b2e185c5fc82eb94aecd7ac27bfeecc74f5525aaf3754dbb4c442fec3902a93aad079178e1523a544f0808a4b6d21a11dc9508be0a0  afl++-4.32c.tar.gz
671c404eb44f9890be66afb3dd3cca4e9423d83e4c98e9df8ff76f75209efc7c5f3427937bdeb71cbd93b12d7d5e877b494e9a231df0a274f1e80df72ded484d  extend-64bit-detection.patch
"
