SUMMARY="The Glasgow Haskell Compiler"
DESCRIPTION="
The Glorious Glasgow Haskell Compilation system (GHC) is a compiler for Haskell.

Haskell is \"the\" standard lazy functional programming language.
"
HOMEPAGE="https://www.haskell.org/ghc/"
COPYRIGHT="The Glasgow Haskell Team"
LICENSE="BSD (3-clause)
	GNU LGPL v2.1"
REVISION="1"
SOURCE_URI="https://www.haskell.org/ghc/dist/$portVersion/ghc-$portVersion-src.tar.xz"
SOURCE_DIR="ghc-$portVersion"
CHECKSUM_SHA256="bb8ec3634aa132d09faa270bbd604b82dfa61f04855655af6f9d14a9eedc05fc"
PATCHES="ghc-$portVersion.patchset"

ARCHITECTURES="!x86"
SECONDARY_ARCHITECTURES="x86"

GLOBAL_WRITABLE_FILES="
	settings/ghc directory keep-old
	"

PROVIDES="
	ghc78$secondaryArchSuffix = $portVersion
	cmd:ghc = $portVersion compat >= 8.2
	cmd:ghc_$portVersion
	cmd:ghc_pkg
	cmd:ghc_pkg_$portVersion
	cmd:ghci
	cmd:ghci_$portVersion
	cmd:haddock
	cmd:haddock_ghc_$portVersion
	cmd:hp2ps
	cmd:hpc
	cmd:hsc2hs
	cmd:runghc
	cmd:runghc_$portVersion
	cmd:runhaskell
	"
REQUIRES="
	haiku$secondaryArchSuffix >= $haikuVersion
	lib:libiconv$secondaryArchSuffix
	lib:libncursesw$secondaryArchSuffix >= 6
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel >= $haikuVersion
	haiku_devel >= $haikuVersion
	devel:libgmp$secondaryArchSuffix
	devel:libiconv$secondaryArchSuffix
	devel:libncursesw$secondaryArchSuffix >= 6
	devel:libz$secondaryArchSuffix
	docbook_xml_dtd
	docbook_xsl_stylesheets
	"
BUILD_PREREQUIRES="
	cmd:autoconf
	cmd:awk
	cmd:find
	cmd:gcc$secondaryArchSuffix
	cmd:ghc_7.10.3
	cmd:ghc_pkg_7.10.3
	cmd:git
	cmd:grep
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:patch
	cmd:perl
	cmd:sed
	cmd:sort
	cmd:tar
	cmd:xsltproc
	"

BUILD()
{
	# 2. Build GHC using the bootstrap compiler; we create a "dyn" only install
	echo "Building GHC proper..."

	cat > mk/build.mk <<-EOF
		V = 0
		GhcLibWays = v dyn
		SRC_HC_OPTS     = -O -H64m
		GhcStage1HcOpts = -O -fasm
		GhcStage2HcOpts = -O2 -fasm
		GhcHcOpts       = -Rghc-timing
		GhcLibHcOpts    = -O2

		DYNAMIC_GHC_PROGRAMS = YES
		DYNAMIC_TOO = YES

		NoFibWays =
		STRIP_CMD = :
EOF

	# Workaround for haikuporter bug #207
	mkdir -p /system/settings/ghc
	cp -r /system/lib/x86/ghc-*/package.conf.d /system/settings/ghc/

	export HOME=/boot/home

	autoconf

	# haikuporter's buildspec confuses GHC, so we omit it, and we can't
	# have a gcc2 GHC, so just install directly into $prefix/bin
	runConfigure --omit-buildspec --omit-dirs binDir configure --bindir=$prefix/bin GHC=/bin/ghc-7.10.3 CC=/bin/gcc-x86
	make $jobArgs
}

INSTALL()
{
	export HOME=/boot/home

	export GHC_PACKAGE_PATH=$settingsDir/ghc/package.conf.d
	ghc-pkg init $GHC_PACKAGE_PATH
	unset GHC_PACKAGE_PATH
	# 1. Install our new GHC
	echo "Installing GHC!"
	make install

	# 2. Move package.conf.d to settings
	sed -i -e '/^PKGCONF=/c\
	PKGCONF='"$settingsDir"'/ghc/package.conf.d' $prefix/bin/ghc-pkg-$portVersion
	unset GHC_PACKAGE_PATH

	# move package.conf.d files to the right place...
	mv $libDir/ghc-$portVersion/package.conf.d/*.conf $settingsDir/ghc/package.conf.d/
	$prefix/bin/ghc-pkg recache
}
