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="14"
SOURCE_URI_1="https://www.haskell.org/ghc/dist/7.8.3/ghc-7.8.3-src.tar.bz2"
CHECKSUM_SHA256_1="2358826f8424bf571dcc313bd882422fe108a340d6e37db4339ff6d5d6ac3f37"
SOURCE_URI_2="https://pulkomandy.tk/drop/ghc-7.8.3-i386-unknown-haiku.tar.gz"
CHECKSUM_SHA256_2="23b1a7a13b7ec07b7768d35e8ce9334131e57b7ab652b0ad3aa0faa559630d15"
PATCHES="ghc-$portVersion.patchset"

ARCHITECTURES="x86"
SECONDARY_ARCHITECTURES="x86"

GLOBAL_WRITABLE_FILES="
	settings/ghc directory keep-old
	"

PROVIDES="
	ghc$secondaryArchSuffix = $portVersion
	cmd:ghc
	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:libiconv$secondaryArchSuffix
	devel:libncursesw$secondaryArchSuffix >= 6
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:autoconf
	cmd:awk
	cmd:find
	cmd:gcc$secondaryArchSuffix
	cmd:grep
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:perl
	cmd:sed
	cmd:sort
	cmd:tar
	lib:libncursesw$secondaryArchSuffix >= 5
	"

BUILD()
{
	echo "Installing bootstrap GHC..."
	pwd
	# 1. Extract, configure, and install the bindist bootstrap GHC
	ln -sf /sources-2/ ghc-bootstrap
	pushd ghc-bootstrap
	pushd ghc-7.8.3
	configure --prefix=/tmp/bootstrap
	make install
	popd
	popd

	# 2. Build GHC using the bootstrap compiler; we create a "dyn" only install
	echo "Building GHC proper..."
	if [ ! -e flag.configured ] ; then

	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

	# 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 --with-ghc=/tmp/bootstrap/bin/ghc --with-gcc=/bin/gcc-x86
		touch ../flag.configured
	else
		echo "Skipping configure..."
	fi
	make $jobArgs
}

INSTALL()
{
	export GHC_PACKAGE_PATH=$settingsDir/ghc/package.conf.d
	/tmp/bootstrap/bin/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
}
