# Contributor: Will Sinatra <wpsinatra@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=chez-scheme
# XXX: Upgrades require rebuilding community/idris2 & community/idris2-stage0
pkgver=10.0.0
_realver=${pkgver%%[a-z]}
pkgrel=1
pkgdesc="Cisco R6RS Scheme Compiler"
url="https://github.com/cisco/ChezScheme"
arch="all"
license="Apache-2.0"
makedepends="util-linux-dev ncurses-dev libx11-dev zlib-dev lz4-dev"
subpackages="$pkgname-doc"
source="https://github.com/cisco/ChezScheme/releases/download/v$pkgver/csv$pkgver.tar.gz
	check-expected::https://raw.githubusercontent.com/cisco/ChezScheme/v$pkgver/.github/workflows/summarytest-some-fast

	0001-add-configuration-for-s390x-and-related-to-c-version.patch
	0002-configure-improve-inference-of-compiler-flags-for-pb.patch
	disable-iconv-tests.patch"
builddir="$srcdir"/csv$pkgver

# upstream only seems to run the test suite on x86_64 and aarch64.
# Therefore, the expected check file from above only works on these.
case "$CARCH" in
x86_64|aarch64) ;;
*) options="!check" ;;
esac

case "$CARCH" in
	x86)     _chez_host=ti3le    ;;
	x86_64)  _chez_host=ta6le    ;;
	arm*)    _chez_host=tarm32le ;;
	aarch64) _chez_host=tarm64le ;;
	riscv64) _chez_host=trv64le  ;;
	ppc64le) _chez_host=tpb64l   ;;
	s390x)   _chez_host=tpb64b   ;;
esac

prepare() {
	default_prepare

	# Ensure we don't use vendored zlib/lz4 versions.
	rm -r zlib lz4
}

build() {
	./configure --os=tXle \
		--installprefix=/usr \
		--installman=/usr/share/man \
		--nogzip-man-pages \
		--temproot=$pkgdir \
		--machine=$_chez_host \
		--installschemename="chez" \
		--installscriptname="chez-script" \
		ZLIB="$(pkg-config --libs zlib)" LZ4="$(pkg-config --libs liblz4)"
	make
}

check() {
	# Only run a subset of the test suite, otherwise the test
	# take several hours to complete on some architectures.
	make test-some-fast

	# The `make test` command does not exit with a non-zero exit
	# status when test fail (see BUILDING). For this reason, we
	# compare the summary file here. This is also the approach
	# taken by ChezScheme for their GitHub CI configuration.
	diff -q "$srcdir"/check-expected "$_chez_host"/mats/summary
}

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

	# Remove pre-compiled example programs (reduces package size).
	rm -f "$pkgdir"/usr/lib/csv$_realver/examples/*.so

	# Move examples to -doc subpackage.
	mkdir -p "$pkgdir"/usr/share/doc/$pkgname/
	mv "$pkgdir"/usr/lib/csv$_realver/examples \
		"$pkgdir"/usr/share/doc/$pkgname/
}

sha512sums="
094298744bd69033366305330d660299023c5bc5776df86f28af119f9f4cddc463366fc8f955084463137b6186843fcd34026d2714b932baa4841d6522a8dd92  csv10.0.0.tar.gz
cdf845795cf06506b7af21d68325cb4d72044b6179bf2f827c658a3668657da353fe6006a576db52bc1bd737adc9e8971aa08b8ffa81a28b4e7a3df8dadc32b0  check-expected
5ae1ed10d0640f39fce71fad9dfc7d0bec67876a5c3b93f903118de0e6535ff97091d6ad523b4886755733ac165aa150f277d064eb22c7cd27577e4c362e5d3b  0001-add-configuration-for-s390x-and-related-to-c-version.patch
8979061ed307a08d8be20083e30d861da7ef60278c1de93c080bfee7326c7a32824c3ad33c40f857f673cac4bc05a9e249b6f838a37c59c7cd95dce08c7e6fd7  0002-configure-improve-inference-of-compiler-flags-for-pb.patch
c1177eedfd562593f2be664c4f0643b7cbe7d4ff0d9e994acbf226739f26f76d32f3b8f3024067e44536eb0e1edcff9a2daa3b61e05dfcd0e04b7430a3097503  disable-iconv-tests.patch
"
