# $Id: Portfile,v 1.54 2006/05/30 10:54:28 gwright Exp $

PortSystem 1.0
name		ghc
version		6.4.2
revision	2
categories	lang
maintainers	gwright@opendarwin.org
platforms	darwin
description	The Glorious Glasgow Haskell Compilation System
long_description	\
		The Glasgow Haskell Compiler is a robust,		\
		fully-featured, optimising compiler and interactive	\
		environment for Haskell 98, GHC compiles Haskell to	\
		either native code or C.  It implements numerous	\
		experimental language extensions to Haskell 98,		\
		for example: concurrency, a foreign language interface,	\
		multi-parameter type classes, scoped type variables,	\
		existential and universal quantification, unboxed	\
		types, exceptions, weak pointers, and so on.		\
		GHC comes with a generational garbage collector,	\
		and a space and time profiler. 

#
# The compiler build requires happy version 1.15.  
#
set happy_version	1.15

homepage	http://www.haskell.org/ghc/
master_sites	${homepage}/dist/${version}/:source	\
	        http://www.haskell.org/happy/dist/${happy_version}:happy

use_bzip2	yes

distfiles	${name}-${version}-src.tar.bz2:source		\
		happy-${happy_version}-src.tar.gz:happy

checksums	${name}-${version}-src.tar.bz2      md5 a394bf14e94c3bca5507d568fcc03375 \
		happy-${happy_version}-src.tar.gz   md5 02ceb122b904fa4a4290e6ea1072d59e

#
# After it extracting the happy parser generator, copy it into the
# main ghc tree. The configure argument --enable-src-tree-happy
# directs the ghc build to make happy from the freshly downloaded
# source before building the compiler.
#
post-extract	{
		cd ${workpath}
		system "gzip ${extract.pre_args} ${distpath}/happy-${happy_version}-src.tar.gz ${extract.post_args}"

		file copy ${workpath}/happy-${happy_version}/happy ${worksrcpath}/happy
	}

patchfiles	patch-configure.ac	\
		patch-ghc-compiler-Makefile

depends_lib	port:readline \
		port:gmp

# Yes, it some situations (-fvia-C) we really need perl
# to _run_ ghc, since the mangler (an assembly to assembly transformation)
# is a perl script. That's why it called the "Evil Mangler".
#
depends_run	port:perl5.8

platform darwin i386 {
		ui_msg "GHC is not supported on OS X i386 yet"
		exit 1
	}

platform darwin 6 {
		ui_msg "GHC is not supported on Jaguar (OS X 10.2.x)"
		exit 1
	}


platform darwin 7 {
		global	ghc_bootversion
		set ghc_bootversion	6.4

		master_sites-append	${homepage}/dist/${ghc_bootversion}/MacOSX/:bootstrap

		distfiles-append	${name}-${ghc_bootversion}-darwin-bootstrap-v01.tar.bz2:bootstrap

		checksums-append	${name}-${ghc_bootversion}-darwin-bootstrap-v01.tar.bz2 md5 3a0a79ed656cb619dd69a24cab32ba97

		extract.only		${name}-${version}-src.tar.bz2 \
					${name}-${ghc_bootversion}-darwin-bootstrap-v01.tar.bz2

# The following is required because ghc triggers a long-standing bug
# in make 3.79 and earlier. (3.79 is standard on Panther.) The bug causes
# make to fall into an infinite loop. (Not required on Tiger.)

		depends_build		port:gmake

# The bootstrap compiler requires version 5.0 of readline. Ugh.

		depends_lib-append	port:readline-5

		pre-configure	{
			cd ${worksrcpath}
			system "autoreconf"

			set cfg [open "${worksrcpath}/mk/build.mk" w]
		  	puts $cfg "#"
		  	puts $cfg "# Local configuration overrides for DarwinPorts"
		  	puts $cfg "#"
		  	puts $cfg "ReadlineIncludePath=${prefix}/include"
		  	puts $cfg "SRC_CC_OPTS += -I${prefix}/include"
		  	puts $cfg "SRC_HC_OPTS += -I${prefix}/include -I/usr/include -L${prefix}/lib -L/usr/lib"
		  	puts $cfg "EXTRA_HSC2HS_OPTS += -I${prefix}/include"
		  	puts $cfg "EXTRA_LD_OPTS += -L${prefix}/lib"
		  	puts $cfg "EXTRA_LD_OPTS += -L/usr/lib"
		  	close $cfg

			reinplace "s|/opt/local|${workpath}/ghc-bootstrap|g" ${workpath}/ghc-bootstrap/lib/ghc-${ghc_bootversion}/package.conf

			reinplace "s|GHCBIN=\"/opt/local|GHCBIN=\"${workpath}/${name}-bootstrap|" ${workpath}/${name}-bootstrap/bin/ghc

			reinplace "s|TOPDIROPT=\"-B/opt/local|TOPDIROPT=\"-B${workpath}/${name}-bootstrap|" ${workpath}/${name}-bootstrap/bin/ghc

			reinplace "s|-L/opt/local|-L${prefix}|" ${workpath}/${name}-bootstrap/bin/ghc

			reinplace "s|-I/opt/local|-I${prefix}|" ${workpath}/${name}-bootstrap/bin/ghc

		  	reinplace "s|/opt/local|${prefix}|" ${workpath}/${name}-bootstrap/lib/ghc-${ghc_bootversion}/ghc-asm

		  	reinplace "s|/opt/local|${prefix}|" ${workpath}/${name}-bootstrap/lib/ghc-${ghc_bootversion}/ghc-split
		}

		configure.args-append --with-ghc='${workpath}/${name}-bootstrap/bin/ghc'
		build.cmd	gmake
		build.target
	}


platform darwin 8 {
		global ghc_bootversion
		set ghc_bootversion	6.4

		master_sites-append	${homepage}/dist/${ghc_bootversion}/MacOSX/:bootstrap

		distfiles-append	${name}-${ghc_bootversion}-darwin-bootstrap-tiger.tar.bz2:bootstrap

		checksums-append	${name}-${ghc_bootversion}-darwin-bootstrap-tiger.tar.bz2 md5 1665720d4d35cf89b9efa7865d0228f6

		extract.only		${name}-${version}-src.tar.bz2 \
					${name}-${ghc_bootversion}-darwin-bootstrap-tiger.tar.bz2

		pre-configure	{
			cd ${worksrcpath}
			system "autoreconf"

			set cfg [open "${worksrcpath}/mk/build.mk" w]
		  	puts $cfg "#"
		  	puts $cfg "# Local configuration overrides for DarwinPorts"
		  	puts $cfg "#"
		  	puts $cfg "ReadlineIncludePath=${prefix}/include"
		  	puts $cfg "SRC_CC_OPTS += -I${prefix}/include"
		  	puts $cfg "SRC_HC_OPTS += -I${prefix}/include -I/usr/include -L${prefix}/lib -L/usr/lib"
		  	puts $cfg "EXTRA_HSC2HS_OPTS += -I${prefix}/include"
		  	puts $cfg "EXTRA_LD_OPTS += -L${prefix}/lib"
		  	puts $cfg "EXTRA_LD_OPTS += -L/usr/lib"
		  	close $cfg

			reinplace "s|/opt/local|${workpath}/ghc-bootstrap|g" ${workpath}/ghc-bootstrap/lib/ghc-${ghc_bootversion}/package.conf

			reinplace "s|GHCBIN=\"/opt/local|GHCBIN=\"${workpath}/${name}-bootstrap|" ${workpath}/${name}-bootstrap/bin/ghc

			reinplace "s|TOPDIROPT=\"-B/opt/local|TOPDIROPT=\"-B${workpath}/${name}-bootstrap|" ${workpath}/${name}-bootstrap/bin/ghc

			reinplace "s|-L/opt/local|-L${prefix}|" ${workpath}/${name}-bootstrap/bin/ghc

			reinplace "s|-I/opt/local|-I${prefix}|" ${workpath}/${name}-bootstrap/bin/ghc

		  	reinplace "s|/opt/local|${prefix}|" ${workpath}/${name}-bootstrap/lib/ghc-${ghc_bootversion}/ghc-asm

		  	reinplace "s|/opt/local|${prefix}|" ${workpath}/${name}-bootstrap/lib/ghc-${ghc_bootversion}/ghc-split
		}

		configure.args-append --with-ghc='${workpath}/${name}-bootstrap/bin/ghc'
	}

#bugs		GHC does not support DESTDIR. Instead, we install	\
#		everything into ${destroot}/${prefix}, and then fix up	\
#		the five scripts which actually have the installation	\
#		path hard coded.

configure.env	LDFLAGS="-L/usr/lib -L${prefix}/lib"	\
		CPPFLAGS="-I${prefix}/include"		\
		CFLAGS="-I${prefix}/include"		\
		DYLD_FALLBACK_LIBRARY_PATH=${prefix}/lib

configure.args	--prefix=${destroot}/${prefix}			\
		--mandir=${destroot}/${prefix}/share/man/	\
		--enable-src-tree-happy				\
		--disable-openal				\
		--disable-alut

variant noopengl	{ configure.args-append --disable-hopengl }

build	{
	system "DYLD_FALLBACK_LIBRARY_PATH=${prefix}/lib ${build.cmd} ${build.target}"
}



# Note the middle "/" in the ${destroot}/${prefix} pattern. It is required,
# because the actual text we are trying to match has a "//" between the
# destroot and the prefix pieces.

post-destroot	{
	reinplace s|${destroot}/${prefix}|${prefix}|g ${destroot}/${prefix}/bin/ghc
	reinplace s|${destroot}/${prefix}|${prefix}|g ${destroot}/${prefix}/bin/ghci
	reinplace s|${destroot}/${prefix}|${prefix}|g ${destroot}/${prefix}/bin/ghc-pkg
	reinplace s|${destroot}/${prefix}|${prefix}|g ${destroot}/${prefix}/bin/ghcprof
	reinplace s|${destroot}/${prefix}|${prefix}|g ${destroot}/${prefix}/bin/hsc2hs

	reinplace "s|\\\$@\"\}|\\\$@\"\} -L${prefix}/lib -I${prefix}/include |" ${destroot}/${prefix}/bin/ghc

	reinplace s|${destroot}/${prefix}|${prefix}|g ${destroot}${prefix}/lib/${name}-${version}/package.conf

	cd ${destroot}${prefix}/lib/ghc-${version}
        system "ranlib *.a"

	file delete ${destroot}${prefix}/bin/happy
	file delete ${destroot}${prefix}/bin/happy-${happy_version}
	file delete -force ${destroot}${prefix}/lib/happy-${happy_version}
}
