# -*- 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
# $Id: Portfile 114724 2013-12-14 12:30:59Z cal@macports.org $

PortSystem          1.0
PortGroup           active_variants 1.1

name                ghc
set canonicalname   ghc
# Do not update GHC separate from Haskell Platform.
# When updating GHC, make sure to revbump all Haskell ports.
version             7.6.3
revision            4
categories          lang haskell
maintainers         gmail.com:kitchen.andy cal openmaintainer
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}/
distname        ${canonicalname}-${version}-src
worksrcdir      ${canonicalname}-${version}

use_bzip2       yes

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

checksums       ghc-7.6.3-src.tar.bz2 \
                rmd160  82a673ed38b7cf9a59afeb01057625fc761a822b \
                sha256  bd43823d31f6b5d0b2ca7b74151a8f98336ab0800be85f45bb591c9c26aac998 \
                ghc-7.6.3-testsuite.tar.bz2 \
                rmd160  e1d7536b1b0799a6c069eb89be41ceb56692b68d \
                sha256  870d8f50a1b569b0f93d0c7c6b80225280841988f1d779aafe847bcf02d76256

depends_build   port:ghc-bootstrap \
                port:libxslt

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

patchfiles      patch-configure-disable-docbook-ps-and-pdf.diff \
                patch-utils__mkdirhier__mkdirhier.sh.diff \
                patch-includes_HsFFI.h.diff


livecheck.type  none
test.run        yes
use_parallel_build no

# -- BEGIN gcc variants

variant gcc43 conflicts gcc44 gcc45 gcc46 gcc47 gcc48 gcc49 description {Compile with gcc 4.3} {
    configure.compiler      macports-gcc-4.3
    configure.args-append   --with-gcc=${configure.cc}
    require_active_variants ghc-bootstrap gcc43
}

variant gcc44 conflicts gcc43 gcc45 gcc46 gcc47 gcc48 gcc49 description {Compile with gcc 4.4} {
    configure.compiler      macports-gcc-4.4
    configure.args-append   --with-gcc=${configure.cc}
    require_active_variants ghc-bootstrap gcc44
}

variant gcc45 conflicts gcc43 gcc44 gcc46 gcc47 gcc48 gcc49 description {Compile with gcc 4.5} {
    configure.compiler      macports-gcc-4.5
    configure.args-append   --with-gcc=${configure.cc}
    require_active_variants ghc-bootstrap gcc45
}

variant gcc46 conflicts gcc43 gcc44 gcc45 gcc47 gcc48 gcc49 description {Compile with gcc 4.6} {
    configure.compiler      macports-gcc-4.6
    configure.args-append   --with-gcc=${configure.cc}
    require_active_variants ghc-bootstrap gcc46
}

variant gcc47 conflicts gcc43 gcc44 gcc45 gcc46 gcc48 gcc49 description {Compile with gcc 4.7} {
    configure.compiler      macports-gcc-4.7
    configure.args-append   --with-gcc=${configure.cc}
    require_active_variants ghc-bootstrap gcc47
}

variant gcc48 conflicts gcc43 gcc44 gcc45 gcc46 gcc47 gcc49 description {Compile with gcc 4.8} {
    configure.compiler      macports-gcc-4.8
    configure.args-append   --with-gcc=${configure.cc}
    require_active_variants ghc-bootstrap gcc48
}

variant gcc49 conflicts gcc43 gcc44 gcc45 gcc46 gcc47 gcc48 description {Compile with gcc 4.9} {
    configure.compiler      macports-gcc-4.9
    configure.args-append   --with-gcc=${configure.cc}
    require_active_variants ghc-bootstrap gcc49
}

if {![variant_isset gcc43] && ![variant_isset gcc44] &&
    ![variant_isset gcc45] && ![variant_isset gcc46] &&
    ![variant_isset gcc47] && ![variant_isset gcc49]} {
    default_variants +gcc48
}

# -- END gcc variants

set bootstraproot ${prefix}/share/ghc-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

# 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}/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}/lib/${name}-${version}"
    # delete old ${prefix}/lib/ghc-${version} directories
    foreach fullpath [glob -directory ${prefix}/lib ghc-*] {
        if {$fullpath ne $libprefix} {
            ui_debug "Cleaning up remnant GHC library directory ${fullpath}"
            delete $fullpath
        }
    }

    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}/bin/ghc-pkg -v recache"}
}
