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

PortSystem          1.0
PortGroup           compiler_blacklist_versions 1.0

name                mlton-bootstrap
platforms           darwin macosx
supported_archs     arm64 i386 ppc x86_64

description         Bootstrap version of MLton
long_description    {*}${description}. MLton is a whole-program optimizing compiler for the Standard ML programming language.

categories          lang ml
license             HPND
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer

homepage            http://www.mlton.org

if {${configure.build_arch} eq "ppc"} {
    # This is the last version for PowerPC from upstream: https://github.com/MLton/mlton/issues/498
    # Please keep it pegged.
    version         20070826
    revision        1
    distfiles       mlton-${version}-1.powerpc-darwin.gmp-macports.tgz
    checksums       rmd160  d467be6e7d59f1b16f31a5f45211d5e55f70e58b \
                    sha256  288e0e86872733e6ba0d605f187a6d96097f143605105a2fdd554ad2e4e6303f \
                    size    12846527
    worksrcdir      ${worksrcpath}/local
    patchfiles-append \
                    patch-${version}.diff

} elseif {${configure.build_arch} eq "i386"} {
    version         20100608
    revision        0
    distfiles       mlton-${version}-1.x86-darwin.gmp-macports.tgz
    checksums       rmd160  3b2de5c958819ee494d71f23b0255ffc9be3a139 \
                    sha256  5e4d116f6950206ba0df20eb4f9cb308182546a26d125d9a51c9f5834ef85aa3 \
                    size    16167559
    worksrcdir      ${worksrcpath}/local
    patchfiles-append \
                    patch-${version}.diff

} elseif {${configure.build_arch} eq "x86_64"} {
    if {${os.major} > 18} {
        version     20210117
        revision    0
        # What can we do…
        distfiles   mlton-${version}-1.amd64-darwin-19.6.gmp-homebrew.tgz
        checksums   rmd160  fbceabd8dac11cff7f839f953391596a3f6222ed \
                    sha256  4f50973e9ec47ffb3e7ce5ecd14996e3894779eec788ce464e6c9a903719cf28 \
                    size    18056959
        patchfiles-append \
                    patch-x86_64.diff
    } elseif {${os.major} > 16} {
        version     20201002
        revision    0
        distfiles   mlton-${version}-1.amd64-darwin-17.7.gmp-homebrew.tgz
        checksums   rmd160  be93f0616386602f68ba33a981f4116545137465 \
                    sha256  0abd8ddb8dd51d078b4b7f400f488d069d53017333c78090d3b6a732b49f81ac \
                    size    17824913
        patchfiles-append \
                    patch-x86_64.diff
    } elseif {${os.major} > 11} {
        version     20180207
        revision    1
        distfiles   mlton-${version}-1.amd64-darwin.gmp-homebrew.tgz
        checksums   rmd160  3e5374ed9a12906409c74ba3cc790222b4d877ea \
                    sha256  c164c1f3a3bbce0a0008708984202ac8f7d4dc1a3d2aa83a4faeb41e6d68bf2f \
                    size    18874573
        patchfiles-append \
                    patch-${version}.diff
    } else {
        # 20180207 is too new for 10.6–10.7: https://github.com/MLton/mlton/issues/502
        version     20130715
        revision    0
        distfiles   mlton-${version}-1.amd64-darwin.gmp-macports.tgz
        checksums   rmd160  bc348fbab45b270932a24e6fc20c98166e484c5c \
                    sha256  a9bfff3bc5a793e84306f98cd60f195a8139902900f96e1072d13c0813b49c10 \
                    size    22767949
        worksrcdir  ${worksrcpath}/local
        patchfiles-append \
                    patch-${version}.diff
    }
} elseif {${configure.build_arch} eq "arm64"} {
        version     20210117
        revision    0
        distfiles   mlton-${version}-1.arm64-darwin-21.6-gmp-dynamic.tgz
        checksums   rmd160  64fa80a6ac321f7d36dafe41a3ef21df95bacf9a \
                    sha256  d8acf4bb925abd2aa509c318c59c3c5ccd10813b098aa99292498878e7da3bfa \
                    size    8397794
        patchfiles-append \
                    patch-aarch64.diff
}

if {${configure.build_arch} eq "arm64"} {
    master_sites    https://projects.laas.fr/tina/software/
} else {
    master_sites    sourceforge:project/mlton/mlton/${version}
}

extract.rename      yes

depends_lib         port:gmp

universal_variant   no

use_configure       no

post-patch {
    reinplace "s|@CC@|${configure.cc}|" ${worksrcpath}/bin/mlton
    reinplace "s|@MLTON@|${prefix}/libexec/mlton-bootstrap/lib/mlton|" ${worksrcpath}/bin/mlton
    reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/bin/mlton
}

# Not required here, but needed to build the current MLton, and we want to use the same compiler:
compiler.c_standard 2011
compiler.blacklist-append {clang < 900}

# mlton-bootstrap bakes in C compiler into its script, like R does.
# We have to ensure that both mlton-bootstrap and mlton use the identical compiler.
# For now set it to use clang-16 or gcc-13. When newer compilers are enabled,
# it should be done for mlton-bootstrap and mlton together, with revbumping both.
compiler.blacklist-append {macports-clang-1[7-9]} {macports-gcc-1[4-9]}

# While newer versions got makefiles in a bundle, they are optional: https://github.com/MLton/mlton/issues/501
build {}

destroot {
    xinstall -m 755 -d ${destroot}${prefix}/libexec/${name}

    foreach dir [glob ${worksrcpath}/*] {
        copy $dir ${destroot}${prefix}/libexec/${name}
    }
}

post-destroot {
    # These are dylibs which original binaries were built against:
    if {${configure.build_arch} eq "ppc"} {
        set old_gmp /opt/local/lib/libgmp.3.dylib
    } elseif {${configure.build_arch} eq "i386"} {
        set old_gmp /opt/local/lib/libgmp.10.dylib
    # Ugly paths below are intended:
    } elseif {${configure.build_arch} eq "x86_64"} {
        if {${os.major} < 12} {
            set old_gmp /opt/local/lib/libgmp.10.dylib
        } else {
            set old_gmp /usr/local/opt/gmp/lib/libgmp.10.dylib
        }
    } elseif {${configure.build_arch} eq "arm64"} {
        set old_gmp /opt/homebrew/opt/gmp/lib/libgmp.10.dylib
    }
    # Fix them to use the current libgmp:
    system -W ${destroot}${prefix}/libexec/${name}/lib/mlton "install_name_tool -change ${old_gmp} ${prefix}/lib/libgmp.dylib ./mlton-compile"
    foreach bin {mllex mlnlffigen mlprof mlyacc} {
        system -W ${destroot}${prefix}/libexec/${name}/bin "install_name_tool -change ${old_gmp} ${prefix}/lib/libgmp.dylib ./${bin}"
    }
}

livecheck.type      none