# -*- 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           wxWidgets 1.0
PortGroup           legacysupport 1.1
PortGroup           compiler_blacklist_versions 1.0

# strndup, TARGET_OS_OSX
legacysupport.newest_darwin_requires_legacy 10

name                erlang
version             28.1.1
revision            0

set doc_version     ${version}

categories          lang erlang
maintainers         {ciserlohn @ci42} openmaintainer
license             Apache-2

description         The Erlang Programming Language
long_description    Erlang is a programming language designed at the        \
                    Ericsson Computer Science Laboratory. Open-source       \
                    Erlang is being released to help encourage the spread   \
                    of Erlang outside Ericsson.                             \
                                                                            \
                    We are releasing free of charge:                        \
                        The entire source code of the current Erlang        \
                        system.                                             \
                        Extensive libraries of code for building robust     \
                        fault-tolerant distributed applications.            \
                        All with documentation.                             \
                                                                            \
                    All the above software has been battle tested in a      \
                    number of Ericsson products, for example the new        \
                    Ericsson ATM switch.

homepage            https://www.erlang.org/
master_sites        https://www.erlang.org/download/ \
                    https://github.com/erlang/otp/releases/download/OTP-${version}/

universal_variant   no

distfiles           otp_src_${version}${extract.suffix}                    \
                    otp_doc_man_${doc_version}${extract.suffix}                \
                    otp_doc_html_${doc_version}${extract.suffix}

checksums           otp_src_${version}.tar.gz \
                    rmd160  43f4007cead170a0ae2898dd894cf9d049d1c378 \
                    sha256  03e1b26e846b2cc1b49271656da6d5ecf4d5f4c2e34224d869a23c61a3d5660a \
                    size    104587348 \
                    otp_doc_man_${doc_version}.tar.gz \
                    rmd160  67329a373e3250442d91ca2dd2e055c0986102d3 \
                    sha256  90a7e5d84285d1b3487a869edeacba16dd0d3a6cdd33c0f910949a256c7c6c81 \
                    size    45185 \
                    otp_doc_html_${doc_version}.tar.gz \
                    rmd160  605ec837cd3d224bb6fd2f7ce2ab0cd733919e42 \
                    sha256  d54bcc53b8ed0537d5595fca06beba5b3220b869aaeab41c1a40d9a21af892c7 \
                    size    26640642

worksrcdir          otp_src_${version}

configure.args      --prefix=${prefix}       \
                    --enable-threads         \
                    --enable-dynamic-ssl-lib \
                    --enable-smp-support     \
                    --without-ssl            \
                    --without-odbc           \
                    --without-javac          \
                    --without-wx

depends_build-append \
                    port:gawk
# ${prefix}/lib/erlang/erts-5.8.4/bin/beam.smp links with ncurses
depends_lib-append  port:ncurses

# Erlang seems to have an on-again/off-again relationship with Clang.
# As of Xcode 4.2, it's off again.
# GCC 4.2 also fails: https://trac.macports.org/ticket/52507
compiler.blacklist  {clang < 300} *gcc-4.*

post-destroot   {
    system "tar -C ${destroot}${prefix}/lib/erlang -zxvf [shellescape ${distpath}/otp_doc_html_${doc_version}${extract.suffix}]"
    system "tar -C ${destroot}${prefix}/lib/erlang -zxvf [shellescape ${distpath}/otp_doc_man_${doc_version}${extract.suffix}]"

    set erts_dir            [glob -tails -directory ${destroot}${prefix}/lib/erlang/lib/ erts-*]
    set erl_interface_dir   [glob -tails -directory ${destroot}${prefix}/lib/erlang/lib/ erl_interface-*]

    foreach x {dialyzer ear ecc elink epmd erl erlc escript run_erl start to_erl typer} {
        delete ${destroot}${prefix}/bin/${x}
    }
    foreach x {dialyzer erl erlc escript run_erl start to_erl typer} {
        ln -s ../lib/erlang/bin/${x} ${destroot}${prefix}/bin/${x}
    }

    delete ${destroot}${prefix}/lib/erlang/bin/epmd
    ln -s ../${erts_dir}/bin/epmd ${destroot}${prefix}/lib/erlang/bin/epmd
    ln -s ../lib/erlang/${erts_dir}/bin/epmd ${destroot}${prefix}/bin/epmd
    ln -s ../lib/erlang/lib/${erl_interface_dir}/bin/erl_call ${destroot}${prefix}/bin/erl_call
}

platform darwin 10 {
    # 10.6 (and 10.6.1) has a bad bug related to using dlopen in a thread.
    # rdar://7209349 - http://www.openradar.appspot.com/7209349
    # This is a workaround.
    patchfiles-append           patch-erts_emulator_sys_unix_ddll.c.diff
}

platform darwin {
    if {${configure.build_arch} in [list arm64 ppc64 x86_64]} {
        configure.args-append   --enable-darwin-64bit
    } else {
        # https://github.com/erlang/otp/issues/8506
        configure.args-append   --disable-darwin-64-bit \
                                --disable-year2038
    }
    # TODO: not sure about the minimum version
    if {${os.major} >= 10} {
        # also "-framework Appkit"?
        configure.ldflags-append -framework CoreFoundation
    }

    # asmjit fix for Sonoma annoying popup with JIT on amd64
    # Upstream included it but forgot to enable JIT on amd64
    patchfiles-append           patch-enable-jit-x86.diff
}

# FIXME: allow building with wxGTK on older systems.
# See: https://github.com/erlang/otp/issues/8370
variant wxwidgets description {Build wxWidgets support} {
    wxWidgets.use               wxWidgets-3.2
    depends_lib-append          port:${wxWidgets.port}
    depends_lib-append          port:libGLU
    configure.args-append       --with-wxdir=${wxWidgets.wxdir}
    configure.args-delete       --without-wx
}

variant ssl description {Build SSL support} {
    PortGroup                   openssl 1.0

    openssl.branch              3
    configure.args-delete       --without-ssl
    configure.args-append       --with-ssl=[openssl::install_area]

    depends_lib-append          port:zlib
    configure.ldflags-append    -lz
}

variant odbc description {Build ODBC support} {
    depends_lib-append          port:unixODBC
    configure.args-delete       --without-odbc
    configure.args-append       --with-odbc=${prefix}
}

default_variants    +ssl

livecheck.type      regex
livecheck.version   ${version}
livecheck.url       ${homepage}downloads/
livecheck.regex     "otp_src_(\[0-9\]+\\.\[0-9\]+(\\.\[0-9\])?)\\.tar\\.gz"
