# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Eric Timmons <etimmons@alum.mit.edu>
# Contributor: Will Sinatra <wpsinatra@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
maintainer="Will Sinatra <wpsinatra@gmail.com>"
pkgname=sbcl
pkgver=2.5.4
pkgrel=0
pkgdesc="Steel Bank Common Lisp"
url="http://www.sbcl.org/"
# s390x/loongarch64: not currently supported
# riscv64/ppc64le: doesn't build
arch="all !ppc64le !s390x !riscv64 !loongarch64"
license="custom"
options="!check"
checkdepends="ed"
makedepends="
	emacs-nox
	linux-headers
	mpfr-dev
	sbcl-bootstrap
	zstd-dev
	"
subpackages="$pkgname-doc $pkgname-source:_source"
source="$pkgname-$pkgver.tar.bz2::https://prdownloads.sourceforge.net/sbcl/sbcl-$pkgver-source.tar.bz2
	ppc-musl.patch
	musl-time64.patch
	"

# This follows how other -stage0 aports work, see APKBUILD of
# sbcl-stage0 for a brief explanation of the bootstrap process.
provides="sbcl-bootstrap=$pkgver"
provider_priority=100 # highest

build() {
	#All systems can build with these features enabled
	all_arch_conf='--with-sb-core-compression --with-sb-linkable-runtime'

	#Please add additional package support by arch
	#armv7 is incapable of supporting sb:thread
	case "$CARCH" in
		armv7) conf="$all_arch_conf" ;;
		x86) conf="$all_arch_conf --fancy --with-sb-thread" ;;
		x86_64) conf="$all_arch_conf --fancy --with-sb-thread" ;;
		aarch64) conf="$all_arch_conf --fancy --with-sb-thread" ;;
		riscv64) conf="--with-sb-core-compression --fancy --with-sb-thread" ;;
		loongarch64) conf="$all_arch_conf" ;;
		ppc64le) conf="$all_arch_conf" ;;
	esac

	GNUMAKE=make ./make.sh sbcl --prefix=/usr $conf

	# Setting the sbcl source location enables more granular deep introspection tooling, like sly
	HOME="$builddir" src/runtime/sbcl --core output/sbcl.core --no-sysinit --no-userinit \
					 --eval '(sb-ext:set-sbcl-source-location "/usr/share/sbcl/source/")' \
					 --eval '(sb-ext:save-lisp-and-die "output/sbcl.core")'
}

package() {
	INSTALL_ROOT="$pkgdir/usr" \
		LIB_DIR="/usr/lib" \
		DOC_DIR="$pkgdir/usr/share/doc/$pkgname" \
		sh install.sh

	install -Dm644 COPYING \
		"$pkgdir"/usr/share/licenses/$pkgname/LICENSE.txt

	rmdir "$pkgdir"/usr/share/doc/$pkgname/html 2>/dev/null || true
	rmdir "$pkgdir"/usr/share/info 2>/dev/null || true
}

_source() {
	pkgdesc="sbcl source code"

	mkdir -p "$subpkgdir/usr/share/sbcl/source"

	# Source code files are needed for deep introspection tools, like Sly, to operate.
	cp -r "$builddir/src" "$subpkgdir/usr/share/sbcl/source/"
	cp -r "$builddir/contrib" "$subpkgdir/usr/share/sbcl/source/"
}

sha512sums="
f0e6c25c37d7c83c0ee8ae94f78438b2c0ed6491f080afec84c5cd7bc1eb65ba43982d8092b637b77d8764d6cb0de4b337c250f840184f27fe21e3944fdce31c  sbcl-2.5.4.tar.bz2
549cc9936ddbd4c19067a455dfad151bd0ce59200b2dce761de67e259e621e4174beefaa5a322886bcd731d2470d229a3e1b75a55a194c54e2feb7c0bcdffb37  ppc-musl.patch
c5b23ac36478943370156e8a499393c6923a069d8a38e4bf3b651ea91521353398cd813ef18dbcf9e76e97a24bfd85fb1d9cfb8da90951345a6f7ab0d28d65fc  musl-time64.patch
"
