# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           compiler_blacklist_versions 1.0

name                ghc-bootstrap
set canonicalname   ghc
version             7.6.3
revision            0
categories          lang haskell
maintainers         {cal @neverpanic} openmaintainer
license             BSD
platforms           darwin
supported_archs     i386 x86_64 ppc
universal_variant   no
installs_libs       no

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}/
distname        ${canonicalname}-${version}-src
worksrcdir      ${canonicalname}-${version}

use_bzip2       yes

distfiles       ${canonicalname}-${version}-src${extract.suffix}

# checksums       ghc-7.6.2-src.tar.bz2 \
#                 rmd160  3aa6cc4daaba1f14f28a7720d6d39cf1d9f982f2 \
#                 sha256  d5f45184abeacf7e9c6b4f63c7101a5c1d7b4fe9007901159e2287ecf38de533

checksums       ghc-7.6.3-src.tar.bz2 \
rmd160  82a673ed38b7cf9a59afeb01057625fc761a822b \
sha256  bd43823d31f6b5d0b2ca7b74151a8f98336ab0800be85f45bb591c9c26aac998 \
size    110763823

depends_build   port:ghc-ppc-bootstrap \
                port:libxslt

depends_lib     port:gmp           \
                port:ncurses       \
                port:libiconv

patchfiles      patch-configure.diff \
                patch-utils__mkdirhier__mkdirhier.sh.diff \
                patch-includes_HsFFI.h.diff \
                validate-settings.diff \
                gettime.diff

livecheck.type     none
test.run           yes
use_parallel_build yes

configure.pre_args  --prefix=${prefix}/share/ghc-bootstrap

default_variants    +gcc7

compiler.blacklist  *clang*
compiler.whitelist  macports-gcc-7

# variant gcc42 {
# 		configure.compiler gcc-4.2
# }

variant gcc7 {
		configure.compiler macports-gcc-7
}

set bootstraproot ${prefix}/share/ghc-ppc-bootstrap
configure.args  --with-ghc=${bootstraproot}/bin/ghc     \
                --with-iconv-includes=${prefix}/include \
                --with-iconv-libraries=${prefix}/lib    \
                --with-gmp-includes=${prefix}/include   \
                --with-gmp-libraries=${prefix}/lib \
                --with-gcc=${configure.cc}
                
platform darwin 8 powerpc {
configure.args-append	--with-macosx-deployment-target=10.4 \
						--with-sysroot=/Developer/SDKs/MacOSX10.4u.sdk
}
platform darwin 9 powerpc {
configure.args-append	--with-macosx-deployment-target=10.5 \
						--with-sysroot=/Developer/SDKs/MacOSX10.5.sdk
}
platform darwin 10 powerpc {
configure.args-append	--with-macosx-deployment-target=10.5 \
						--with-sysroot=/Developer/SDKs/MacOSX10.5.sdk
}

# OK so because the bootstrap binary has been prebuilt for libraries
# in /usr/lib we search these before macports stuff to prevent
# link errors, ghc _should_ actually compile itself in stage2
# using paths from the command line arguments
compiler.cpath /usr/include
compiler.library_path /usr/lib

post-destroot {
    set prefixlib ${prefix}/share/ghc-bootstrap/lib/${worksrcdir}
    set destlib   ${destroot}/${prefixlib}
    set libver    ${canonicalname}${version}

    variable libs [ list                               \
                    libHSrts-${libver}.dylib           \
                    libHSrts_debug-${libver}.dylib     \
                    libHSrts_thr-${libver}.dylib       \
                    libHSrts_thr_debug-${libver}.dylib ]

    system "install_name_tool -id ${prefixlib}/libffi.dylib ${destlib}/libffi.dylib"

    foreach lib $libs {
      regexp -line {[^[:space:]]*/libffi.*\.dylib} [exec otool -L ${destlib}/${lib}] oldlib
      system "install_name_tool -change ${oldlib} ${prefixlib}/libffi.dylib ${destlib}/$lib"
    }
}

pre-activate {
    # Legacy port deactivation hack added 2012-12-08
    if {![catch {set platform_ghc_installed [lindex [registry_active hs-platform-ghc] 0]}]} {
        # hs-platform-ghc is installed and active
        # force deactivation
        registry_deactivate_composite hs-platform-ghc "" [list ports_nodepcheck 1]
    }
    # Legacy port deactivation hack added 2013-08-11, hs-process is provided by base
    if {![catch {set hs_process_installed [lindex [registry_active hs-process] 0]}]} {
        # hs-process is installed and active
        # force deactivation
        registry_deactivate_composite hs-process "" [list ports_nodepcheck 1]
    }
    # Legacy port deactivation hack added 2013-08-14, hs-template-haskell is provided by base
    if {![catch {set hs_template_haskell_installed [lindex [registry_active hs-template-haskell] 0]}]} {
        # hs-template-haskell is installed and active
        # force deactivation
        registry_deactivate_composite hs-template-haskell "" [list ports_nodepcheck 1]
    }
}

post-activate {
    set libprefix "${prefix}/share/ghc-bootstrap/lib/${canonicalname}-${version}"

    set packageconfd "${libprefix}/package.conf.d"
    set deletefiles [list]

    # files that were not correctly unregistered due to --force missing in the
    # haskell portgroup, leaving package registrations of packages that still
    # had dependents in place

    # added 2013-12-14
    lappend deletefiles "${packageconfd}/Cabal-1.16.0.3-*.conf"
    lappend deletefiles "${packageconfd}/cereal-0.3.5.2-*.conf"
    lappend deletefiles "${packageconfd}/cpphs-1.16-*.conf"
    lappend deletefiles "${packageconfd}/entropy-0.2.2.2-*.conf"
    lappend deletefiles "${packageconfd}/highlighting-kate-0.5.3.9-*.conf"
    lappend deletefiles "${packageconfd}/language-c-0.3.2.1-*.conf"
    lappend deletefiles "${packageconfd}/nats-0.1-*.conf"
    lappend deletefiles "${packageconfd}/primitive-0.5.0.1-*.conf"
    lappend deletefiles "${packageconfd}/semigroups-0.11-*.conf"
    lappend deletefiles "${packageconfd}/semigroups-0.9.2-*.conf"
    lappend deletefiles "${packageconfd}/template-haskell-2.8.0.0-10af654f73a6befd48e3430f5bf44c21.conf"
    lappend deletefiles "${packageconfd}/uniplate-1.6.10-*.conf"

    fs-traverse file $packageconfd {
        foreach pattern $deletefiles {
            if {[string match $pattern $file]} {
                ui_debug "Removing remnant package registration file ${file}" 
                delete $file
            }
        }
    }

    catch {system "${prefix}/share/ghc-bootstrap/bin/ghc-pkg -v recache"}
}
