# Template file for 'ghc'
pkgname=ghc
# Keep this synchronized with http://www.stackage.org/lts
version=8.8.3
revision=1
wrksrc="ghc-${version%[!0-9]}"
build_style=gnu-configure
configure_args="--with-system-libffi"
hostmakedepends="automake docbook-xsl ghc-bin libxslt python3-Sphinx"
makedepends="gmp-devel libffi-devel libnuma-devel ncurses-devel"
depends="perl gcc libffi-devel gmp-devel libnuma-devel"
short_desc="Glorious Haskell Compiler"
maintainer="Leah Neukirchen <leah@vuxu.org>"
license="BSD-3-Clause"
homepage="http://www.haskell.org/ghc/"
distfiles="http://www.haskell.org/ghc/dist/${version%[!0-9]}/${pkgname}-${version%[!0-9]}-src.tar.xz"
checksum=e0dcc0aaf3e234c5978f29e6df62947e97720ab404ec0158343df211c5480f89
patch_args="-Np1"
nocross=yes  # ask leah2 before wasting time trying to do that
nodebug=yes  # Work around assembler error "Fatal error: duplicate .debug_line sections"
_bindir="/usr/lib/${pkgname}-${version}/bin"
nopie_files="
 ${_bindir}/ghc
 ${_bindir}/ghc-iserv
 ${_bindir}/ghc-iserv-dyn
 ${_bindir}/ghc-iserv-prof
 ${_bindir}/ghc-pkg
 ${_bindir}/haddock
 ${_bindir}/hp2ps
 ${_bindir}/hpc
 ${_bindir}/hsc2hs
 ${_bindir}/runghc
 ${_bindir}/unlit
"

build_options="bindist"
desc_option_bindist="Create a binary distribution"

# Recent safe to use tarball
# GHC's bundled tarball is from 2017, buggy on some archs
_ffi_rev=4d6d2866ae43e55325e8ee96561221804602cd7a

if [ "$build_option_bindist" ]; then
	# Strip --with-system-libffi from configuration
	configure_args=${configure_args/--with-system-libffi/}
	# Required to make the binary distribution tarball
	hostmakedepends+=" tar xz libtool libltdl-devel"
	# Fetch a safe libffi tarball
	distfiles+=" https://github.com/libffi/libffi/archive/${_ffi_rev}.tar.gz>libffi-${_ffi_rev}.tar.gz"
	checksum+=" f7a17fa61261942e93450ec52272b5f09d8f42cfffd147867587f969ee01b739"
	skip_extraction+=" libffi-${_ffi_rev}.tar.gz"
fi

post_extract() {
	[ "$build_option_bindist" ] || return 0
	rm -f libffi-tarballs/libffi-*.tar.gz
	cp ${XBPS_SRCDISTDIR}/${pkgname}-${version}/libffi-${_ffi_rev}.tar.gz \
	 libffi-tarballs
}

pre_configure() {
	export CONF_CC_OPTS_STAGE0=$CFLAGS_FOR_BUILD
	export CONF_CC_OPTS_STAGE1=$CFLAGS
	export CONF_CC_OPTS_STAGE2=$CFLAGS
	export CONF_GCC_LINKER_OPTS_STAGE0=$LDFLAGS_FOR_BUILD
	export CONF_GCC_LINKER_OPTS_STAGE1=$LDFLAGS
	export CONF_GCC_LINKER_OPTS_STAGE2=$LDFLAGS
	export CONF_CPP_OPTS_STAGE0=$CPPFLAGS_FOR_BUILD
	export CONF_CPP_OPTS_STAGE1=$CPPFLAGS
	export CONF_CPP_OPTS_STAGE2=$CPPFLAGS

	autoreconf -fi
}

post_install() {
	sed -i 's#/usr/lib/ccache/bin/##g' ${DESTDIR}/usr/lib/ghc-${version%[!0-9]}/settings
	vlicense LICENSE

	if [ "$build_option_bindist" ]; then
		msg_normal "Creating binary distribution for ${XBPS_TARGET_MACHINE}...\n"
		make ${makejobs} binary-dist
		dest="${XBPS_SRCDISTDIR}"/distfiles/ghc-${version}-void-linux-${XBPS_TARGET_LIBC}.tar.xz
		install -d -m 0644 ghc-${version}-*-unknown-linux.tar.xz "${dest}"
		msg_normal "Installed in ${dest}\n"
	fi
}

ghc-doc_package() {
	archs=noarch
	short_desc+=" -- documentation"
	pkg_install() {
		vmove usr/share/doc
	}
}