# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=capstone
pkgver=5.0.6
pkgrel=0
pkgdesc="The Ultimate Disassembler"
url="https://www.capstone-engine.org"
arch="all"
license="BSD-3-Clause-Clear"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="
	bash
	cython
	py3-gpep517
	py3-setuptools
	python3-dev
	"
subpackages="
	$pkgname-static
	$pkgname-dev
	py3-$pkgname-pyc
	py3-${pkgname}:_py3
	cstool
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/capstone-engine/capstone/archive/$pkgver.tar.gz
	fix-python-binding-test-patch.patch
	py-path-fix.patch
	"

build() {
	export CFLAGS="${CFLAGS//-Os/-O2} -flto=auto -ffat-lto-objects"
	make

	cd bindings/python
	gpep517 build-wheel --wheel-dir .dist --output-fd 3 3>&1 >&2
}

check() {
	make check

	# The `make check` doesn't run the Python binding tests.
	cd bindings/python
	PYTHONPATH="$(pwd)" make check
}

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

	cd bindings/python
	python3 -m installer -d "$pkgdir" .dist/*.whl

	rm -rf "$pkgdir"/usr/lib/python3*/site-packages/capstone/lib
	rm -rf "$pkgdir"/usr/lib/python3*/site-packages/capstone/include
}

_py3() {
	pkgdesc="$pkgdesc (for python3)"
	depends="py3-setuptools capstone"

	amove usr/lib/python*
}

cstool() {
	pkgdesc="cstool for capstone disassembler"

	amove usr/bin/cstool
}

sha512sums="
d4ed08a2ab4ed8ede51a163e98542129d6441889cf6936ac9e3f8027fb2dfcbb04a7aacba14c2a007e788790bb3939c173b47db3d95f5dd9eafce2f30ff493e1  capstone-5.0.6.tar.gz
481629f39c901f4a53e0d1ea27573a87cfa200d48e5acebef6e87d2d26879403ffc3fd38ab9a63d556087d10320c204844f647bb0000627cc103c71d70f81bd1  fix-python-binding-test-patch.patch
93a28193a86ac2e7cd7d4081b4dc962c70d8898d2ed4d22b0760f2f4966f12204e8f6919f199a630b4fe4676df98d593d9adc79870f61e6343c93bdf88fe46c3  py-path-fix.patch
"
