# Maintainer: Mitch Tishmack <mitch.tishmack@gmail.com>
pkgname=ghc
pkgver=8.6.5
_llvmver=9
pkgrel=3
# Normal non rc candidate
_urlprefix="$pkgver"
_pkgprefix="$pkgname-$pkgver"
pkgdesc="The Glasgow Haskell Compiler"
# Next 5 variables only needed for release candidate testing
#pkgrcver=8.2.2
#pkgrc=rc3
#pkgdate=20171108
#urlprefix="$pkgrcver-$pkgrc"
#pkgprefix="ghc-$pkgver.$pkgdate"
url="https://haskell.org/"
arch="x86_64"
# Note ghc's license is basically BSD-3. If you'd like to know more visit:
# * https://www.haskell.org/ghc/license
# * https://ghc.haskell.org/trac/ghc/wiki/Licensing
license="BSD-3-Clause"
# Note that ghc is sensitive to the version of llvm used.
#
# Ref: https://ghc.haskell.org/trac/ghc/wiki/Status/GHC-8.0.1
#      https://ghc.haskell.org/trac/ghc/wiki/ImprovedLLVMBackend
depends="gmp-dev perl gcc>=6.2.1 llvm$_llvmver libffi-dev"
# ghc is self-hosted, so we need ghc to build ghc. ghc-bootstrap is provided by
# this aport (see below).
makedepends_build="$pkgname-bootstrap
	autoconf cpio binutils binutils-gold paxmark libffi-dev ncurses-dev xz coreutils grep"
makedepends_host="linux-headers musl-dev zlib-dev gmp-dev binutils-dev libffi-dev ncurses-dev cpio" # need GNU cpio
makedepends="$makedepends_build $makedepends_host"
checkdepends="python3"
# XXX: ghc-bootstrap is a hack to allow this abuild to depend on itself.
# Adding "ghc" to makedepends would not work, because abuild implicitly removes
# $pkgname and $subpackages from the abuild's dependencies.
provides="$pkgname-bootstrap=$pkgver-r$pkgrel"
subpackages="$pkgname-doc $pkgname-dev"
install="$pkgname.post-install"
options="!strip !check"  # we strip it manually in build()
source="https://downloads.haskell.org/~ghc/$_urlprefix/$_pkgprefix-src.tar.xz
	https://downloads.haskell.org/~ghc/$_urlprefix/$_pkgprefix-testsuite.tar.xz
	0005-buildpath-abi-stability.patch
	fix-testsuite.patch
        0001-testsuite-unset-MAKEFLAGS-when-calling-python.patch
        0001-testsuite-Fix-T8602-on-musl.patch
        0001-testsuite-Add-Alpine-Linux-x64-stat-too-good-allocat.patch
        0001-testsuite-Ensure-that-ffi005-output-order-is-predict.patch
        0001-testsuite-Ensure-T5423-flushes-C-output-buffer.patch
        0001-testsuite-Skip-broken-tests-on-Alpine.patch
        0002-testsuite-Increase-tolerance-for-failed-stat-tests-o.patch
        0001-testsuite-T12600-avoid-broken-pipe-on-Alpine.patch
	"

# We only need the bootstrap patch when bootstrapping a new arches ghc.
if [ "$CBUILD" != "$CTARGET" ]; then
	source="$source 0000-bootstrap.patch"
fi

builddir="$srcdir/$_pkgprefix"

prepare() {
	default_prepare

	cp mk/build.mk.sample mk/build.mk

	cat >> mk/build.mk <<-EOF
		BuildFlavour         = perf-llvm
		INTEGER_LIBRARY      = integer-gmp
		BeConservative       = YES
		GhcStage3HcOpts     += -O3
		SplitSections        = YES
	EOF

	if [ "$CBUILD" != "$CTARGET" ]; then
		# cross-build
		cat >> mk/build.mk <<-EOF
			BuildFlavour         = perf-cross
			HADDOCK_DOCS         = NO
			BUILD_SPHINX_HTML    = NO
			BUILD_SPHINX_PS      = NO
			BUILD_SPHINX_PDF     = NO
		EOF
	fi

	# Due to patches to the configure script
	autoreconf
}

build() {
	cd "$builddir"

	local ffi_inc=$(pkg-config libffi --cflags-only-I); ffi_inc="${ffi_inc%% }"
	local ffi_lib=$(pkg-config libffi --libs-only-L); ffi_lib="${ffi_lib%% }"

	GHCLD=${CROSS_COMPILE}ld.gold

	# NOTE: ghc build system requires host == build, and it ends up
	# compiling the cross-compiler (stage1) and cross-compiling with
	# that the native compiler (stage2)
	./configure \
		--build=$CBUILD \
		--host=$CBUILD \
		--target=$CTARGET \
		--prefix=/usr \
		--with-system-libffi \
		${ffi_inc:+--with-ffi-includes="${ffi_inc#-I}"} \
		${ffi_lib:+--with-ffi-libraries="${ffi_lib#-L}"} \
		--with-ar=${CROSS_COMPILE}ar \
		--with-nm=${CROSS_COMPILE}nm \
		--with-ranlib=${CROSS_COMPILE}ranlib \
		--with-objdump=${CROSS_COMPILE}objdump \
		--disable-ld-override \
		CONF_CPP_OPTS_STAGE0=" $ffi_inc $ffi_lib " \
		CONF_CC_OPTS_STAGE0=" $ffi_inc $ffi_lib " \
		LD=$GHCLD

	# Switch llvm-targets from unknown-linux-gnueabihf->alpine-linux
	# so we can match the llvm vendor string alpine uses
	sed -i -e 's/unknown-linux-gnueabihf/alpine-linux/g' llvm-targets
	sed -i -e 's/unknown-linux-gnueabi/alpine-linux/g' llvm-targets
	sed -i -e 's/unknown-linux-gnu/alpine-linux/g' llvm-targets

	make
}

check() {
	cd "$builddir/testsuite"
	make fast THREADS=$JOBS
}

doc() {
	default_doc
	install -Dm644 "$builddir/LICENSE" \
		"$subpkgdir/usr/share/licenses/$subpkgname/LICENSE"
}

package() {
	local ghclib="usr/lib/ghc-$pkgver"
	local newpath path target

	cd "$builddir"
	make -j1 DESTDIR="$pkgdir" install

	cd "$pkgdir"

	# Fixup install tree if needed.
	if [ -d usr/lib/$CTARGET-ghc-$pkgver ]; then
		# different location
		ghclib="usr/lib/$CTARGET-ghc-$pkgver"

		# Rename binaries and fix links.
		local path; for path in usr/bin/"$CTARGET"-*; do
			newpath="${path//$CTARGET-/}"

			if [ -h "$path" ]; then
				target="$(readlink $path)"
				ln -sf "${target//$CTARGET-/}" "$newpath"
				rm "$path"
			else
				mv "$path" "$newpath"
			fi
		done

		# Remove triplet prefix from settings -- the intention is
		# that the native compiler will use native gcc/ld on the target.
		sed -i "s|$CTARGET-||g" usr/lib/$CTARGET-ghc-$pkgver/settings
	fi

	# Can't do a full strip on archives.
	find . -type f \( -name "*.so" -o -name "*.a" \) \
		-exec ${CROSS_COMPILE}strip --strip-unneeded {} \;
	find $ghclib/bin -type f -exec ${CROSS_COMPILE}strip {} \;

	paxmark -m \
		$ghclib/bin/ghc \
		$ghclib/bin/ghc-iserv \
		$ghclib/bin/ghc-iserv-dyn \
		$ghclib/bin/ghc-iserv-prof
}

# Like debian, we split apart the profiled archives/etc...
# This drastically reduces the install size of the ghc pkg.
dev() {
	pkgdesc="$pkgdesc (development files)"
	depends="$pkgname=$pkgver-r$pkgrel"

	cd "$pkgdir"

	install -dm755 "$subpkgdir"

	local pfiles=$(find . \( -type f -o -type l \) \( -name "*.p_*" -o -name "lib*_p.a" \))
	echo "$pfiles" | cpio -pamVd "$subpkgdir"
	echo "$pfiles" | xargs rm -fr
}
sha512sums="c08a7480200cb99e1ffbe4ce7669f552b1054054966f7e7efcbc5f98af8032e1249fa391c4fc4c7d62cc8e0be5d17fa05845177f3cea3dbcf86e6c92d40fc0f9  ghc-8.6.5-src.tar.xz
980f3934bb18876cc80b80a0eb64a9ca1dfaa7f4d8b0761f5d733a6583053e38b231445388b992b26ec1ad5f569b6385dbe60f96381794f5577964979c253e16  ghc-8.6.5-testsuite.tar.xz
e1c2cef06d307eda4b35521204e95eb54ace5dbcd22de659e95356f884b4424d6304365e4ab45c5116192cba4c095e2e91114bc7cb73d7c7173a7035287d0854  0005-buildpath-abi-stability.patch
6be1706f430883ff71bcbf18c2d17add6f54b6c4eaf902f519bb7c553acc8922f7eb718eda0a5ec8a108c91eb66da1471b45c4a03b71f2019b6dc16b27c3b8bb  fix-testsuite.patch
ac2ad793b4d33ea890e236820af4cd10996b47d0f4249c825b31450145cb8195c2be62f2d7942cc3bf564f768dca187379196b251f4b37ed326ed2a0a5f59e8a  0001-testsuite-unset-MAKEFLAGS-when-calling-python.patch
3e955b3e025e7dc6521c8545107c747506cdf40aef55210cbc0b0a7adc8a1ac1b5206f2766dd13dbb114c7fb943e439864774422928f7a4ad10484a5e80ac09c  0001-testsuite-Fix-T8602-on-musl.patch
226b15077c5fb44af8e9076236982c494e18bdc9425c6dd3d9b16d4d72062ce567144479ee8c4b052b9b931af6ae304f6918705e1b66ac9bf699df61ffce4ada  0001-testsuite-Add-Alpine-Linux-x64-stat-too-good-allocat.patch
cc552b3d01b8d1a31d7a609dc5c82dc7d602a14f427a2594abfdd5c1d6e92d6cf47aafd9cc46e84e2c678e040039b98c673f3dc88efa809f221e75110850f60a  0001-testsuite-Ensure-that-ffi005-output-order-is-predict.patch
6be53550fee8e59b63355a1f4ce6ef0a5fdc545008a7808a50cb1d0c1b5219e4b2c215f21ae0e2f953486e88dc7c5f1d193b01190553405e620fa3ac9dd31a61  0001-testsuite-Ensure-T5423-flushes-C-output-buffer.patch
328433df26351acfe67c61c14a365d6b942c7d9d51bfbe099e9f6e26f147a78f3ce5be350df2e896bd179c27b80e0b2a98e0fb8a7590920edca866ed7dc22a39  0001-testsuite-Skip-broken-tests-on-Alpine.patch
23934d13e5d9daf33338105e525d8daee0b0c1a5614f2042690c26efd46a0ab672041d80658bf06e7fcdb97255c471950af708d5bf8c96dbe9571eed0bef60c5  0002-testsuite-Increase-tolerance-for-failed-stat-tests-o.patch
b71bb7945c6cae3ea324ff1de9576b883f0e63169fbe50d781acc0f4fb694387c43bf7aa1bd1cca236613ff202dd503c4577dc06368614752affdc54a5c62101  0001-testsuite-T12600-avoid-broken-pipe-on-Alpine.patch"
