# -*- coding: utf-8; mode: tcl; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4
# $Id: Portfile 89810 2012-02-11 08:55:39Z and.damore@macports.org $

PortSystem	1.0

name		ghc
set canonicalname	ghc
version		6.10.4
revision	5
categories	lang haskell
maintainers	gwright
license		BSD
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.

homepage	http://haskell.org/${canonicalname}
master_sites	${homepage}/dist/${version}/

use_bzip2	yes

distfiles	${canonicalname}-${version}-src.tar.bz2			\
		${canonicalname}-${version}-src-extralibs.tar.bz2	\
		testsuite-${version}.tar.bz2

checksums       ${canonicalname}-${version}-src.tar.bz2 \
                    md5     167687fa582ef6702aaac24e139ec982 \
                    sha1    0566858b409066d98da70de5adb9a7030d0df5dc \
                    rmd160  5f83baf2549f1f4bea140c6a80728c2a76f60ab6 \
                ${canonicalname}-${version}-src-extralibs.tar.bz2 \
                    md5     37ce285617d7cebabc3cf6805bdbca25 \
                    sha1    b08b26070f2965122662d035c602536ae9aafbb8 \
                    rmd160  78e6dba88a253095c79f94c5356918402810bd01 \
                testsuite-${version}.tar.bz2 \
                    md5     1973d7c1a24afbc58cebc7c574ebb56f \
                    sha1    cfb327686398fba2c12a5cd1fdad56faa4fbf3c6 \
                    rmd160  8952dda913a6363f60d3c4ad2d18ec6abcf33425

# ghc does not support a universal build:
universal_variant	no

patchfiles	patch-configure.ac.diff			\
		patch-aclocal.m4.diff			\
		patch-compiler-ghc.cabal.in.diff	\
		patch-compiler-Makefile.diff		\
		patch-compiler-Makefile.local.diff	\
		patch-ghc-Makefile.diff			\
		patch-libraries-Makefile.diff		\
		patch-libraries-cabal-bin.hs.diff	\
		patch-libraries-terminfo-configure.ac.diff	\
		patch-mk-cabal.mk.diff			\
		patch-mk-target.mk.diff			\
		patch-utils-genapply-Makefile.diff	\
		patch-utils-genprimopcode-Makefile.diff

depends_build	port:autoconf263
depends_lib	port:gmp	\
		port:ncurses	\
		port:libiconv

variant maintainer_debug requires cabal_configure_debug description {Verbose logging and save all temporary files} { }

variant cabal_configure_debug description {Add verbose debugging to cabal configure commands} {

		patchfiles-delete	patch-compiler-Makefile.diff	\
					patch-ghc-Makefile.diff		\
					patch-libraries-Makefile.diff	\
					patch-mk-cabal.mk.diff

		if { ${configure.build_arch} == "i386" && ${os.platform} == "darwin" && ${os.major} == 10 } {
			patchfiles-append	patch-snowleopard-i386-compiler-Makefile-debug.diff	\
						patch-snowleopard-i386-ghc-Makefile-debug.diff		\
						patch-snowleopard-i386-libraries-Makefile-debug.diff	\
						patch-snowleopard-i386-mk-cabal.mk-debug.diff
		} else {
			patchfiles-append	patch-compiler-Makefile-debug.diff	\
						patch-ghc-Makefile-debug.diff		\
						patch-libraries-Makefile-debug.diff	\
						patch-mk-cabal.mk-debug.diff
		}
}

# 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".
#
# Not only that, but since correctly handling whitespace is critical,
# only perl 5.8 is known to work reliably.
#
depends_lib-append	port:perl5.8
configure.env-append	PerlCmd=${prefix}/bin/perl5.8

worksrcdir	${canonicalname}-${version}

platform darwin 8 powerpc {
		global ghc_bootversion
		set ghc_bootversion	6.10.1

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

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

                checksums-append	${canonicalname}-${ghc_bootversion}-darwin-ppc-tiger-bootstrap.tar.bz2 \
					md5     511f4bec375b51aaa82de999529916ae		\
                      			sha1    cb6974a680168246c6e13a92818b39c559d7a2b4	\
                      			rmd160  be60cfffc0ed223fb39ce2fe40ad501486564f0e

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

		pre-fetch {
			set minimum_xcodeversion 2.5
        		set current_xcodeversion [exec defaults read /Developer/Applications/Xcode.app/Contents/Info CFBundleShortVersionString]
        		if {[vercmp ${current_xcodeversion} ${minimum_xcodeversion}] < 0} {
            			ui_msg "On Mac OS X ${macosx_version}, ${name} ${version} requires Xcode ${minimum_xcodeversion} but you have Xcode ${current_xcodeversion}."
            			return -code error "incompatible Xcode version"
        		}
		}
}


platform darwin 8 i386 {
		global ghc_bootversion
		set ghc_bootversion	6.6

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

		distfiles-append	${canonicalname}-${ghc_bootversion}-darwin-i386-tiger-bootstrap-rev1.tar.bz2:bootstrap

		checksums-append	${canonicalname}-${ghc_bootversion}-darwin-i386-tiger-bootstrap-rev1.tar.bz2 \
					md5 f01663cecefd50b5f1e1f524f49cd6df

		extract.only		${canonicalname}-${version}-src.tar.bz2 \
					${canonicalname}-${version}-src-extralibs.tar.bz2 \
					${canonicalname}-${ghc_bootversion}-darwin-i386-tiger-bootstrap-rev1.tar.bz2

		pre-fetch {
			set minimum_xcodeversion 2.5
        		set current_xcodeversion [exec defaults read /Developer/Applications/Xcode.app/Contents/Info CFBundleShortVersionString]
        		if {[vercmp ${current_xcodeversion} ${minimum_xcodeversion}] < 0} {
            			ui_msg "On Mac OS X ${macosx_version}, ${name} ${version} requires Xcode ${minimum_xcodeversion} but you have Xcode ${current_xcodeversion}."
            			return -code error "incompatible Xcode version"
        		}
		}
}


platform darwin 9 powerpc {
		global ghc_bootversion
		set ghc_bootversion	6.10.1

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

		distfiles-append	${canonicalname}-${ghc_bootversion}-darwin-ppc-leopard-bootstrap.tar.bz2:bootstrap

                checksums-append	${canonicalname}-${ghc_bootversion}-darwin-ppc-leopard-bootstrap.tar.bz2 \
					md5     511f4bec375b51aaa82de999529916ae	\
                        		sha1    cb6974a680168246c6e13a92818b39c559d7a2b4\
                        		rmd160  be60cfffc0ed223fb39ce2fe40ad501486564f0e

		extract.only		${canonicalname}-${version}-src.tar.bz2 \
					${canonicalname}-${version}-src-extralibs.tar.bz2 \
					${canonicalname}-${ghc_bootversion}-darwin-ppc-leopard-bootstrap.tar.bz2

		pre-fetch {
			set minimum_xcodeversion 3.1.2
        		set current_xcodeversion [exec defaults read /Developer/Applications/Xcode.app/Contents/Info CFBundleShortVersionString]
        		if {[vercmp ${current_xcodeversion} ${minimum_xcodeversion}] < 0} {
            			ui_msg "On Mac OS X ${macosx_version}, ${name} ${version} requires Xcode ${minimum_xcodeversion} but you have Xcode ${current_xcodeversion}."
            			return -code error "incompatible Xcode version"
        		}
		}
}


platform darwin 9 i386 {
		global ghc_bootversion
		set ghc_bootversion	6.8.2

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

		distfiles-append	${canonicalname}-${ghc_bootversion}-darwin-i386-leopard-bootstrap.tar.bz2:bootstrap

		checksums-append	${canonicalname}-${ghc_bootversion}-darwin-i386-leopard-bootstrap.tar.bz2 \
					md5 f263cedef8bcbaa7e9014b3e06923583

		extract.only		${canonicalname}-${version}-src.tar.bz2 \
					${canonicalname}-${version}-src-extralibs.tar.bz2 \
					${canonicalname}-${ghc_bootversion}-darwin-i386-leopard-bootstrap.tar.bz2

		pre-fetch {
			set minimum_xcodeversion 3.1
        		set current_xcodeversion [exec defaults read /Developer/Applications/Xcode.app/Contents/Info CFBundleShortVersionString]
        		if {[vercmp ${current_xcodeversion} ${minimum_xcodeversion}] < 0} {
            			ui_msg "On Mac OS X ${macosx_version}, ${name} ${version} requires Xcode ${minimum_xcodeversion} but you have Xcode ${current_xcodeversion}."
            			return -code error "incompatible Xcode version"
        		}
		}
}


platform darwin 10 {
		global ghc_bootversion
		set ghc_bootversion	6.10.4

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

		if { ${configure.build_arch} == "x86_64" } {
			distfiles-append	${canonicalname}-${ghc_bootversion}-darwin-x86_64-snowleopard-macports-bootstrap.tar.bz2:bootstrap

			checksums-append	${canonicalname}-${ghc_bootversion}-darwin-x86_64-snowleopard-macports-bootstrap.tar.bz2 \
						md5     70c8743dc4d606904426146e0e6d7cdf \
						sha1    bd6830318c522527b2c35a84724832d983054416 \
						rmd160  bc67c499417e009428fe6bf90b86fda4f1ec3e9e

			extract.only		${canonicalname}-${version}-src.tar.bz2 \
						${canonicalname}-${version}-src-extralibs.tar.bz2 \
						${canonicalname}-${ghc_bootversion}-darwin-x86_64-snowleopard-macports-bootstrap.tar.bz2

		} elseif { ${configure.build_arch} == "i386" } {
			distfiles-append	${canonicalname}-${ghc_bootversion}-darwin-i386-snowleopard-bootstrap.tar.bz2:bootstrap

			checksums-append	${canonicalname}-${ghc_bootversion}-darwin-i386-snowleopard-bootstrap.tar.bz2 \
						md5     fba9493ef1a1cb3f54274ab59f007725 \
						sha1    cd30029d0c32b8e97ef31c81f0a9613586628c0f \
						rmd160  d32bc2006ff9d56da1dfecce0eb8be780e81b8b9

			patchfiles-delete	patch-compiler-Makefile.local.diff		\
						patch-libraries-cabal-bin.hs.diff		\
						patch-mk-target.mk.diff

			patchfiles-append	patch-snowleopard-i386-includes-Makefile.diff           	\
						patch-snowleopard-i386-compiler-Makefile.local.diff             \
						patch-snowleopard-i386-compiler-main-DriverPipeline.hs.diff     \
						patch-snowleopard-i386-ghc-ghc.wrapper.diff                     \
						patch-snowleopard-i386-libraries-cabal-bin.hs.diff              \
						patch-snowleopard-i386-libraries-haskeline-haskeline.cabal.diff \
						patch-snowleopard-i386-libffi-Makefile.diff                     \
						patch-snowleopard-i386-mk-target.mk.diff                        \
						patch-snowleopard-i386-utils-genapply-Makefile.diff             \
						patch-snowleopard-i386-utils-genprimopcode-Makefile.diff        \
						patch-snowleopard-i386-utils-hsc2hs-hsc2hs.wrapper.diff

			if { !([variant_isset maintainer_debug] || [variant_isset cabal_configure_debug]) } {
				patchfiles-delete	patch-compiler-Makefile.diff	\
							patch-ghc-Makefile.diff		\
							patch-libraries-Makefile.diff	\
							patch-mk-cabal.mk.diff

				patchfiles-append	patch-snowleopard-i386-compiler-Makefile.diff	\
							patch-snowleopard-i386-ghc-Makefile.diff	\
							patch-snowleopard-i386-libraries-Makefile.diff	\
							patch-snowleopard-i386-mk-cabal.mk.diff
			}

			extract.only		${canonicalname}-${version}-src.tar.bz2 \
						${canonicalname}-${version}-src-extralibs.tar.bz2 \
						${canonicalname}-${ghc_bootversion}-darwin-i386-snowleopard-bootstrap.tar.bz2

		} else {
			# belt and braces approach:
			return -code error "Unrecognized build architecture on Snow Leopard"
		}

		patchfiles-append	patch-snowleopard-compiler-cmm-CLabel.hs.diff		\
					patch-snowleopard-compiler-main-DynFlags.hs.diff	\
					patch-snowleopard-ghc-ghc-bin.cabal.in.diff		\
					patch-snowleopard-rts-Linker.c.diff			\
					patch-snowleopard-rts-Makefile.diff			\
					patch-snowleopard-rts-package.conf.in.diff

}

platform darwin 11 {
		global ghc_bootversion
		set ghc_bootversion	6.10.4

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

		if { ${configure.build_arch} == "x86_64" } {
			distfiles-append	${canonicalname}-${ghc_bootversion}-darwin-x86_64-snowleopard-macports-bootstrap.tar.bz2:bootstrap

			checksums-append	${canonicalname}-${ghc_bootversion}-darwin-x86_64-snowleopard-macports-bootstrap.tar.bz2 \
						md5     70c8743dc4d606904426146e0e6d7cdf \
						sha1    bd6830318c522527b2c35a84724832d983054416 \
						rmd160  bc67c499417e009428fe6bf90b86fda4f1ec3e9e

			extract.only		${canonicalname}-${version}-src.tar.bz2 \
						${canonicalname}-${version}-src-extralibs.tar.bz2 \
						${canonicalname}-${ghc_bootversion}-darwin-x86_64-snowleopard-macports-bootstrap.tar.bz2

		} elseif { ${configure.build_arch} == "i386" } {
			distfiles-append	${canonicalname}-${ghc_bootversion}-darwin-i386-snowleopard-bootstrap.tar.bz2:bootstrap

			checksums-append	${canonicalname}-${ghc_bootversion}-darwin-i386-snowleopard-bootstrap.tar.bz2 \
						md5     fba9493ef1a1cb3f54274ab59f007725 \
						sha1    cd30029d0c32b8e97ef31c81f0a9613586628c0f \
						rmd160  d32bc2006ff9d56da1dfecce0eb8be780e81b8b9

			patchfiles-delete	patch-compiler-Makefile.local.diff		\
						patch-libraries-cabal-bin.hs.diff		\
						patch-mk-target.mk.diff

			patchfiles-append	patch-snowleopard-i386-includes-Makefile.diff           	\
						patch-snowleopard-i386-compiler-Makefile.local.diff             \
						patch-snowleopard-i386-compiler-main-DriverPipeline.hs.diff     \
						patch-snowleopard-i386-ghc-ghc.wrapper.diff                     \
						patch-snowleopard-i386-libraries-cabal-bin.hs.diff              \
						patch-snowleopard-i386-libraries-haskeline-haskeline.cabal.diff \
						patch-snowleopard-i386-libffi-Makefile.diff                     \
						patch-snowleopard-i386-mk-target.mk.diff                        \
						patch-snowleopard-i386-utils-genapply-Makefile.diff             \
						patch-snowleopard-i386-utils-genprimopcode-Makefile.diff        \
						patch-snowleopard-i386-utils-hsc2hs-hsc2hs.wrapper.diff

			if { !([variant_isset maintainer_debug] || [variant_isset cabal_configure_debug]) } {
				patchfiles-delete	patch-compiler-Makefile.diff	\
							patch-ghc-Makefile.diff		\
							patch-libraries-Makefile.diff	\
							patch-mk-cabal.mk.diff

				patchfiles-append	patch-snowleopard-i386-compiler-Makefile.diff	\
							patch-snowleopard-i386-ghc-Makefile.diff	\
							patch-snowleopard-i386-libraries-Makefile.diff	\
							patch-snowleopard-i386-mk-cabal.mk.diff
			}

			extract.only		${canonicalname}-${version}-src.tar.bz2 \
						${canonicalname}-${version}-src-extralibs.tar.bz2 \
						${canonicalname}-${ghc_bootversion}-darwin-i386-snowleopard-bootstrap.tar.bz2

		} else {
			# belt and braces approach:
			return -code error "Unrecognized build architecture on Snow Leopard"
		}

		patchfiles-append	patch-snowleopard-compiler-cmm-CLabel.hs.diff		\
					patch-snowleopard-compiler-main-DynFlags.hs.diff	\
					patch-snowleopard-ghc-ghc-bin.cabal.in.diff		\
					patch-snowleopard-rts-Linker.c.diff			\
					patch-snowleopard-rts-Makefile.diff			\
					patch-snowleopard-rts-package.conf.in.diff

}


if {${configure.compiler} == "llvm-gcc-4.2"} {
    configure.compiler gcc-4.2
} elseif {${configure.compiler} == "clang"} {
    depends_build-append port:apple-gcc42
    configure.compiler apple-gcc-4.2
    # base (as of 2.0.3) doesn't set cxx for apple-gcc-4.2
    configure.cxx ${prefix}/bin/g++-apple-4.2
}


post-extract {
     system "tar jxf ${distpath}/testsuite-${version}.tar.bz2 -C ${worksrcpath}" 
}


post-patch	{
		reinplace "s|XXPATH_TO_GCCXX|${configure.cc}|g" ${worksrcpath}/ghc/Makefile
		reinplace "s|XXPATH_TO_GCCXX|${configure.cc}|g" ${worksrcpath}/compiler/Makefile
		reinplace "s|XXPATH_TO_GCCXX|${configure.cc}|g" ${worksrcpath}/compiler/Makefile.local
		reinplace "s|XXPATH_TO_GCCXX|${configure.cc}|g" ${worksrcpath}/libraries/Makefile
		reinplace "s|XXPATH_TO_GCCXX|${configure.cc}|g" ${worksrcpath}/libraries/cabal-bin.hs
		reinplace "s|XXPATH_TO_GCCXX|${configure.cc}|g" ${worksrcpath}/aclocal.m4
		reinplace "s|XXPATH_TO_GCCXX|${configure.cc}|g" ${worksrcpath}/mk/cabal.mk
		reinplace "s|XXPATH_TO_GCCXX|${configure.cc}|g" ${worksrcpath}/mk/target.mk
		reinplace "s|XXPATH_TO_GCCXX|${configure.cc}|g" ${worksrcpath}/utils/genapply/Makefile
		reinplace "s|XXPATH_TO_GCCXX|${configure.cc}|g" ${worksrcpath}/utils/genprimopcode/Makefile

		reinplace "s|XXPATH_TO_HSC2HSXX|${workpath}/ghc-bootstrap/bin/hsc2hs|" ${worksrcpath}/libraries/Makefile

		reinplace "s|XXMACPORTS_PREFIXXX|${prefix}|" ${worksrcpath}/compiler/ghc.cabal.in

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

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

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

		if {"/opt/local" != ${prefix}} {
			reinplace "s|-L/opt/local|-L${prefix}|" ${workpath}/${canonicalname}-bootstrap/bin/ghc
			reinplace "s|-I/opt/local|-I${prefix}|" ${workpath}/${canonicalname}-bootstrap/bin/ghc
			reinplace "s|/opt/local|${prefix}|g" \
				${workpath}/${canonicalname}-bootstrap/lib/ghc-${ghc_bootversion}/ghc-asm \
				${workpath}/${canonicalname}-bootstrap/lib/ghc-${ghc_bootversion}/ghc-split \
		}
}


pre-configure	{
		system "${prefix}/bin/autoreconf263 ${worksrcpath}"

		set cfg [open "${workpath}/ghc-bootstrap/bin/ghc-pkg" w]
		puts $cfg "#!/bin/sh"
		if {${ghc_bootversion} eq "6.10.4"} {
			puts $cfg "GHCPKGBIN=${workpath}/ghc-bootstrap/lib/ghc-${ghc_bootversion}/ghc-pkg"
		} else {
			puts $cfg "GHCPKGBIN=${workpath}/ghc-bootstrap/lib/ghc-${ghc_bootversion}/ghc-pkg.bin"
		}
		puts $cfg "PKGCONF=${workpath}/ghc-bootstrap/lib/ghc-${ghc_bootversion}/package.conf"
		puts $cfg "exec \$GHCPKGBIN --global-conf \$PKGCONF \${1+\"\$@\"}"
		close $cfg

		system "chmod +x ${workpath}/ghc-bootstrap/bin/ghc-pkg"

		set cfg [open "${workpath}/ghc-bootstrap/bin/hsc2hs" w]
		puts $cfg "#!/bin/sh"
		if {${ghc_bootversion} eq "6.10.4"} {
			puts $cfg "HSC2HSBIN=${workpath}/ghc-bootstrap/lib/ghc-${ghc_bootversion}/hsc2hs"
		} else {
			puts $cfg "HSC2HSBIN=${workpath}/ghc-bootstrap/lib/ghc-${ghc_bootversion}/hsc2hs-bin"
		}
		if {${ghc_bootversion} eq "6.10.1" || ${ghc_bootversion} eq "6.10.4"} {
			puts $cfg "tflag=\"--template=${workpath}/ghc-bootstrap/lib/ghc-${ghc_bootversion}/hsc2hs-0.67/template-hsc.h\""
		} else {
			puts $cfg "tflag=\"--template=${workpath}/ghc-bootstrap/lib/ghc-${ghc_bootversion}/template-hsc.h\""
		}
		puts $cfg "for arg do"
		puts $cfg "    case \"\$arg\" in"
		puts $cfg "        -c*)	         HSC2HS_EXTRA=;;"
		puts $cfg "        --cc=*)       HSC2HS_EXTRA=;;"
		puts $cfg "	   -t*)	         tflag=;;"
		puts $cfg "        --template=*) tflag=;;"
		puts $cfg "        --)           break;;"
		puts $cfg "    esac"
		puts $cfg "done"
		if { ${configure.build_arch} == "i386" && ${os.platform} == "darwin" && ${os.major} == 10 } {
			puts $cfg "exec \$HSC2HSBIN --cflag=\"-m32\" --lflag=\"-m32\" \$tflag \$HSC2HS_EXTRA -I${prefix}/include --lflag=\"-L${prefix}/lib\" \${1+\"\$@\"}"
		} else {
			puts $cfg "exec \$HSC2HSBIN \$tflag \$HSC2HS_EXTRA -I${prefix}/include --lflag=\"-L${prefix}/lib\" \${1+\"\$@\"}"
		}
		close $cfg

		system "chmod +x ${workpath}/ghc-bootstrap/bin/hsc2hs"

		set cfg [open "${worksrcpath}/mk/build.mk" w]
	  	puts $cfg "#"
	  	puts $cfg "# Local configuration overrides for MacPorts"
	  	puts $cfg "#"
	  	puts $cfg "SRC_HC_OPTS += -L${worksrcpath}/libffi"
	  	puts $cfg "SRC_HC_OPTS += -I${prefix}/include -L${prefix}/lib"

		if {[variant_isset maintainer_debug]} {
			puts $cfg "SRC_HC_OPTS += -v -keep-tmp-files"
		}

	  	puts $cfg "EXTRA_HSC2HS_OPTS += -I${prefix}/include --lflag=\"-L${prefix}/lib\""
	  	puts $cfg "EXTRA_LD_OPTS += -L${worksrcpath}/libffi"
	  	puts $cfg "EXTRA_LD_OPTS += -L${prefix}/lib"
		puts $cfg "EXTRA_CABAL_CONFIGURE_FLAGS += --extra-include-dirs=${prefix}/include"
		puts $cfg "EXTRA_CABAL_CONFIGURE_FLAGS += --extra-lib-dirs=${prefix}/lib"

		if {[variant_isset cabal_configure_debug]} then {
			puts $cfg "EXTRA_CABAL_CONFIGURE_FLAGS += --verbose=3"
		}

		if { ${configure.build_arch} == "i386" && ${os.platform} == "darwin" && ${os.major} == 10 } {
			puts $cfg "SRC_HC_OPTS += -optP-m32 -optc-m32 -opta-m32 -optl-m32"
			puts $cfg "GHC_CC_OPTS += -optP-m32 -optc-m32 -opta-m32 -optl-m32"

			puts $cfg "EXTRA_HSC2HC_OPTS += -optc-m32 -opta-m32 -optl-m32"

			puts $cfg "EXTRA_CABAL_CONFIGURE_FLAGS += --ghc-option=-optP-m32"
			puts $cfg "EXTRA_CABAL_CONFIGURE_FLAGS += --ghc-option=-optc-m32"
			puts $cfg "EXTRA_CABAL_CONFIGURE_FLAGS += --ghc-option=-opta-m32"
			puts $cfg "EXTRA_CABAL_CONFIGURE_FLAGS += --ghc-option=-optl-m32"
		}

	  	close $cfg
}

#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.					\
#									\
#		The odd "ac_cv_lib_elf_nlist" environment variable	\
#		is required to avoid including the wrong nlist.h	\
#		header if libelf is installed on the system.

configure.ldflags-append	"-L/usr/lib"
configure.cflags-append		"-I${prefix}/include"
configure.env-append	DYLD_FALLBACK_LIBRARY_PATH=${prefix}/lib	\
			ac_cv_lib_elf_nlist=no

configure.pre_args	--prefix=${destroot}${prefix}
configure.args  --datadir=${destroot}${prefix}/share/${canonicalname}-${version}	\
		--with-gmp-includes=${prefix}/include					\
		--with-gmp-libraries=${prefix}/lib					\
		--with-ghc='${workpath}/${canonicalname}-bootstrap/bin/ghc' 		\
		--with-gcc=${configure.cc}

# The configure script needs to be told the platform explicitly if we're on x86_64:
if { ${configure.build_arch} == "x86_64" } {
	configure.args-append	--build=x86_64-apple-darwin	\
				--host=x86_64-apple-darwin	\
				--target=x86_64-apple-darwin
}

build.env DYLD_FALLBACK_LIBRARY_PATH=${prefix}/lib

destroot.destdir

# Note that we catch the results of the tests, since usually at least
# one of the tests unexpectedly fails.

set testlog ${canonicalname}-${version}-tests.log

test {
     set testpath    ${worksrcpath}/testsuite-${version}
     catch {system "cd ${testpath} && make stage=2 2>&1 | tee ${workpath}/${testlog}"}
}


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

	reinplace "s|\\\$@\"\}|\\\$@\"\} -L${prefix}/lib -I${prefix}/include |" ${destroot}${prefix}/bin/ghc
	reinplace s|${destroot}${prefix}|${prefix}|g ${destroot}${prefix}/lib/${canonicalname}-${version}/package.conf

	file rename ${destroot}${prefix}/lib/${canonicalname}-${version}/libffi.a ${destroot}${prefix}/lib/${canonicalname}-${version}/libHsLocalFfi.a
	reinplace s|"ffi"|"HsLocalFfi"| ${destroot}${prefix}/lib/${canonicalname}-${version}/package.conf

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

	if {[file exists ${workpath}/${testlog}]} {
		set testsavepath ${prefix}/share/${canonicalname}-${version}/tests
		file mkdir  ${destroot}${testsavepath}
		file rename ${workpath}/${testlog} ${destroot}${testsavepath}/${testlog}
		ui_msg "Test results are saved in ${testsavepath}"
	}
}

livecheck.type	regex
livecheck.url	${homepage}
livecheck.regex	"GHC (\[0-9\\.\]+) Released"

