# -*- 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           github 1.0
PortGroup           makefile 1.0

github.setup        kfl mosml 13c581aec46eea134e478f2e2b6456278e36ecce
version             2.10.1
revision            0
categories          lang devel ml
platforms           {darwin < 24}
license             GPL-2+
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer

description         Moscow ML is an implementation of Standard ML (SML)
long_description    Moscow ML is a light-weight implementation of Standard ML \
                    (SML), a strict functional language widely used in teaching \
                    and research. The latest release of Moscow ML implements \
                    the full Standard ML language, as revised 1997, including Modules \
                    and some extensions, yet is backwards compatible with versions \
                    prior to 2.00, and implements large parts of the new SML Basis Library.
homepage            https://mosml.org

checksums           rmd160  a684b544fdd204a7408930c7b77611fcbf28dc44 \
                    sha256  9c18aaac26f6cf8b8394ea7ef3d2fc1e57de34715feea5860b4ea139757e4b95 \
                    size    2577422

github.tarball_from archive
extract.rename      yes

patchfiles-append   0001-mosml.patch

post-patch {
    reinplace -W ${worksrcpath} "s|@PREFIX@|${prefix}|g" \
                    src/Makefile.inc \
                    src/launch/Makefile \
                    src/dynlibs/intinf/Makefile
    reinplace -W ${worksrcpath} "s|@CC@|${configure.cc}|" \
                    src/Makefile.inc
}

depends_lib-append \
                    port:gmp

build.dir           ${worksrcpath}/src
build.target        world

subport ${name}-dynlibs {
    revision        1

    PortGroup       openssl 1.0

    openssl.branch  1.1

    set pgs_v       15

    depends_lib-append \
                    port:gd2 \
                    port:gdbm \
                    port:libpng \
                    port:mosml \
                    port:postgresql${pgs_v} \
                    port:zlib

    # https://github.com/kfl/mosml/issues/71
    patchfiles-append \
                    0002-mosml-dynlibs.patch \
                    0003-Add-some-missing-headers.patch \
                    0004-Fix-for-undeclared-flush.patch \
                    0005-Placeholders-for-disabling-werrors.patch

    post-patch {
        reinplace -W ${worksrcpath} "s|@DESTDIR@|${destroot}|" \
                    src/Makefile.inc
        foreach d {mgd mpq mregex} {
            reinplace -W ${worksrcpath} "s|@PREFIX@|${prefix}|g" \
                    src/dynlibs/${d}/Makefile
        }
        reinplace -W ${worksrcpath} "s|@PGSQL@|postgresql${pgs_v}|g" \
                    src/dynlibs/mpq/Makefile

        if {[string match *clang* ${configure.compiler}]} {
            reinplace -W ${worksrcpath} "s|@WERROR@|-Wno-error=incompatible-pointer-types|" \
                    src/dynlibs/mgdbm/Makefile
            reinplace -W ${worksrcpath} "s|@WERROR@|-Wno-error=implicit-function-declaration|" \
                    src/dynlibs/mpq/Makefile \
                    src/dynlibs/mregex/Makefile
        } elseif {[string match macports-gcc* ${configure.compiler}]} {
            reinplace -W ${worksrcpath} "s|@WERROR@|-Wno-incompatible-pointer-types|" \
                    src/dynlibs/mgdbm/Makefile
            reinplace -W ${worksrcpath} "s|@WERROR@|-Wno-implicit-function-declaration|" \
                    src/dynlibs/mpq/Makefile \
                    src/dynlibs/mregex/Makefile
        } else {
            reinplace -W ${worksrcpath} "s|@WERROR@||" \
                    src/dynlibs/mgdbm/Makefile \
                    src/dynlibs/mpq/Makefile \
                    src/dynlibs/mregex/Makefile
        }
    }

    set port_libdir ${prefix}/lib/mosml

    build.dir       ${worksrcpath}/src/dynlibs

    build.env-append \
                    ROOTDIR="${port_libdir}" \
                    CAMLRT="${port_libdir}"

    build.target    all

    pre-destroot {
        xinstall -d ${destroot}${port_libdir}
    }
}
