# Template file for 'ghc'
pkgname=ghc
# Keep this synchronized with http://www.stackage.org/lts
version=8.10.4
revision=1
wrksrc="ghc-${version%[!0-9]}"
build_style=gnu-configure
hostmakedepends="automake docbook-xsl ghc-bin libxslt libnuma-devel
 ncurses-devel python3-Sphinx"
makedepends="libnuma-devel"
depends="perl gcc libffi-devel gmp-devel libnuma-devel"
short_desc="Glorious Haskell Compiler"
maintainer="slotThe <soliditsallgood@mailbox.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=52af871b4e08550257d720c2944ac85727d0b948407cef1bebfe7508c224910e
nocross=yes # this is conditionally unset for cross bindist
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"

# it is not possible to cross-compile an entire ghc distribution as
# some components rely on stage2 binaries being runnable on the host
# even cross-compiled bindists are incomplete, and you should only
# use them to compile proper bindists on newly bootstrapped targets
if [ -z "$build_option_bindist" ]; then
	# final package builds use system libffi as well as gmp and ncurses
	configure_args=" --with-system-libffi"
	makedepends+=" libffi-devel ncurses-devel gmp-devel"
elif [ -z "$CROSS_BUILD" ]; then
	# native bindist builds use gmp and ncurses, but builtin libffi
	makedepends+=" ncurses-devel gmp-devel"
elif [ "${XBPS_MACHINE%-*}" != "${XBPS_TARGET_MACHINE%-*}" ]; then
	# ghc's cross mode only works when the actual target arch differs
	# i.e. cross-compiling to same arch but different libc does not work
	unset nocross
	# host must equal build and target must be set when cross-compiling
	configure_args+=" --host=${XBPS_TRIPLET} --target=${XBPS_CROSS_TRIPLET}"
fi

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

if [ "$build_option_bindist" ]; then
	# 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

	# update libffi for all bindists
	rm -f libffi-tarballs/libffi-*.tar.gz
	cp ${XBPS_SRCDISTDIR}/${pkgname}-${version}/libffi-${_ffi_rev}.tar.gz \
	 libffi-tarballs

	[ "$CROSS_BUILD" ] || return 0

	# create build config for cross bindists
	local flav="quick"

	echo "BuildFlavour = ${flav}" >> mk/build.mk
	cat mk/flavours/${flav}.mk >> mk/build.mk

	# gmp segfaults when cross-built
	echo "INTEGER_LIBRARY = integer-simple" >> mk/build.mk
	# otherwise needs ncurses for target
	echo "WITH_TERMINFO = NO" >> mk/build.mk
	# cannot be cross-compiled
	echo "HADDOCK_DOCS = NO" >> mk/build.mk
	# just wastes time
	echo "BUILD_SPHINX_HTML = NO" >> mk/build.mk
	echo "BUILD_SPHINX_PDF = NO" >> mk/build.mk
	echo "BUILD_MAN = NO" >> mk/build.mk
}

pre_configure() {
	# stage0 and stage1 are built for build/host (which are the
	# same in ghc) and stage2 is built for target (when cross)
	export CONF_CC_OPTS_STAGE0=$CFLAGS_FOR_BUILD
	export CONF_CC_OPTS_STAGE1=$CFLAGS_FOR_BUILD
	export CONF_CC_OPTS_STAGE2=$CFLAGS
	export CONF_GCC_LINKER_OPTS_STAGE0=$LDFLAGS_FOR_BUILD
	export CONF_GCC_LINKER_OPTS_STAGE1=$LDFLAGS_FOR_BUILD
	export CONF_GCC_LINKER_OPTS_STAGE2=$LDFLAGS
	export CONF_CPP_OPTS_STAGE0=$CPPFLAGS_FOR_BUILD
	export CONF_CPP_OPTS_STAGE1=$CPPFLAGS_FOR_BUILD
	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

	local arch dest
	if [ "$CROSS_BUILD" ]; then
		arch=${XBPS_CROSS_TRIPLET%%-*}
	else
		arch=${XBPS_TRIPLET%%-*}
	fi
	if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
		dest=ghc-${version}-${arch}-void-linux-musl.tar.xz
	else
		dest=ghc-${version}-${arch}-void-linux.tar.xz
	fi

	if [ "$build_option_bindist" ]; then
		msg_normal "Creating binary distribution for ${XBPS_TARGET_MACHINE}...\n"
		make ${makejobs} binary-dist
		mkdir -p ${XBPS_SRCDISTDIR}/distfiles
		mv ghc-${version}-${arch}-unknown-linux.tar.xz ${dest}
		install -m 0644 ${dest} ${XBPS_SRCDISTDIR}/distfiles
		msg_normal "Installed in ${XBPS_SRCDISTDIR}/distfiles\n"
		exit 1
	fi
}

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