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

PortSystem      1.0

name            nhc98
version         1.22
revision        1
categories      lang
maintainers     nomaintainer
# nhc98 currently builds for 32 bit
# See: /nhc98-1.22/configure
supported_archs ppc i386
license         {LGPL BSD}
description     The nhc98 Haskell Compiler
long_description \
                nhc98 is a small, easy to install, standards-compliant \
                compiler for Haskell 98, the lazy functional \
                programming language. It is very portable, and aims \
                to produce small executables that run in small amounts \
                of memory. It produces medium-fast code, and compilation \
                is itself quite fast. It also comes with extensive tool \
                support for automatic compilation, foreign language \
                interfacing, heap and time profiling, tracing \
                and debugging. Some of its advanced kinds of heap \
                profiles are not found in any other Haskell compiler.

homepage        http://www.haskell.org/${name}
master_sites    http://www.cs.york.ac.uk/fp/${name} \
                ftp://ftp.cs.york.ac.uk/pub/haskell/${name} \
                https://www.haskell.org/nhc98/

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

checksums       rmd160  99a90d00f77f66938c817c813f11c3d0ea5bf244 \
                sha256  14c5c16c336d0bb15e6e3d1baad8a2bca93c0538a129fb18df1d47d1f4eb6f3a \
                size    29854160

depends_lib     port:readline \
                port:ncurses

depends_build   port:pkgconfig \
                port:gmake

build.cmd       ${prefix}/bin/gmake

configure.cxx_stdlib   libstdc++

# Supported compilers are gcc, ghc and hbc
compiler.blacklist     *clang*

platform darwin {
    if {${os.major} > 10 && ${os.major} < 16} {
        # Newer gcc fails to build it
        # apple-gcc-4.2 builds through 10.11
        depends_build-append    port:apple-gcc42
        configure.compiler      apple-gcc-4.2
    }

    if {${os.major} > 15 && ${os.major} < 19} {
        # depends_build-append    port:ghc-bootstrap
        known_fail  yes
        pre-fetch {
            ui_error "Building with GHC is not yet implemented"
            return -code error "incompatible OS version"
        }
    }

    if {${os.major} > 18} {
        known_fail  yes
        pre-fetch {
            ui_error "${name} requires MacOS 10.14 or earlier"
            return -code error "incompatible OS version"
        }
    }
}

# Donʼt build in parallel, seriously
use_parallel_build  no

patch.pre_args-replace  -p0 -p1
patchfiles-append \
                0001-asmlong.h-add-PPC-implementation.patch \
                0002-harch.inst-add-missing-archs.patch \
                0004-configure-use-sensible.patch \
                0005-Makefile.inc-use-O1-and-gnu89.patch \
                0006-Fix-some-missing-headers.patch \
                0007-runtests-tests-should-exit-with-an-error-code-if-the.patch \
                0008-Makefile-fix-gcc-targets.patch

platform darwin {
    if {${os.major} > 10} {
        patchfiles-append \
                0001-asmlong.h-fix-i386-for-llvm-gcc.patch \
                0002-alloca.c-add-ifndef.patch \
                0003-Makefile.inc-try-forcing-32-bit-to-fix-build-on-64-b.patch
    }
}

post-patch {
    reinplace "s|@CC@|${configure.cc}|g" ${worksrcpath}/configure
    reinplace "s|@CC@|${configure.cc}|g" ${worksrcpath}/Makefile
}

configure.args  --buildwith=gcc \
                +docs \
                --docdir=${prefix}/share/doc/${name} \
                --mandir=${prefix}/share/man/man1 \
                --heap=8M

post-destroot {
    set rexp1 "'s|${worksrcpath}/script|${prefix}/bin|g'"
    set rexp2 "'s|${worksrcpath}/include|${prefix}/include/nhc98|g'"
    set rfile "${destroot}${prefix}/lib/hmake/`${worksrcpath}/script/harch`/hmakerc"
    system "sed -i '' -e ${rexp1} -e ${rexp2} ${rfile}"
}

universal_variant no

test.run         yes
test.dir         ${worksrcpath}/tests
test.cmd         ./runtests
test.target

livecheck.type   regex
livecheck.regex  "Version (.*) released"
