# -*- 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$

PortSystem          1.0
PortGroup           wxWidgets 1.0
PortGroup           compiler_blacklist_versions 1.0

name                erlang
version             19.1

categories          lang erlang
maintainers         ciserlohn
platforms           darwin
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            http://www.erlang.org/
master_sites        http://www.erlang.org/download/

# Parallel build doesn't work as per R13B01
use_parallel_build  no

universal_variant   no

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

checksums           otp_src_19.1.tar.gz \
                    rmd160  7a850068e9b46f8b6f54f83e95d2d5d09ec674e7 \
                    sha256  fed4cbbc1ee5cc8efc2b8f14c3db97bfde1de967703f5f9a95f8ab6852737e23 \
                    otp_doc_man_19.1.tar.gz \
                    rmd160  0e681889adb81c2bb241fc196e709b80719a2226 \
                    sha256  7200e9e5b3a229a6b3838046e1b3e64afc869265539d49d0e4853212f19c0c79 \
                    otp_doc_html_19.1.tar.gz \
                    rmd160  7c3f91b2925e442426c9ea70ec940786f6c51b1d \
                    sha256  76c89aee1ac69b5107114b0065189101e3f42d53e929d8ef8f08b5c586f15930

pre-patch           { file rename ${workpath}/otp_src_${version} ${workpath}/${name}-${version} }

# http://www.erlang.org/pipermail/erlang-bugs/2009-January/001171.html
patchfiles          patch-erts_emulator_Makefile.in.diff \
                    patch-erts_configure.diff 

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

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

# Erlang seems to have an on-again/off-again relationship with Clang.
# As of XCode 4.2, it's off again.
compiler.blacklist  {clang < 300}

post-destroot   {
    system "tar -C ${destroot}${prefix}/lib/erlang -zxvf ${distpath}/otp_doc_html_${version}${extract.suffix}"
    system "tar -C ${destroot}${prefix}/lib/erlang -zxvf ${distpath}/otp_doc_man_${version}${extract.suffix}"
 
    set erts_dir            erts-8.1
    set erl_interface_dir   erl_interface-3.9.1
    set wx_dir              wx-1.7.1

    reinplace s|${destroot}|| ${destroot}${prefix}/lib/erlang/bin/erl
    reinplace s|${destroot}|| ${destroot}${prefix}/lib/erlang/bin/start
    reinplace s|${destroot}|| ${destroot}${prefix}/lib/erlang/${erts_dir}/bin/erl
    reinplace s|${destroot}|| ${destroot}${prefix}/lib/erlang/${erts_dir}/bin/start

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

    file delete -force ${destroot}${prefix}/lib/erlang/bin/epmd
    system "ln -s ../${erts_dir}/bin/epmd ${destroot}${prefix}/lib/erlang/bin/epmd"
    system "ln -s ../lib/erlang/${erts_dir}/bin/epmd ${destroot}${prefix}/bin/epmd"
    system "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} eq "x86_64" || ${configure.build_arch} eq "ppc64"} {
        configure.args-append   --enable-darwin-64bit
    } else {
        configure.args-append   --disable-darwin-64-bit
    }
    # TODO: not sure about the minimum version
    if {${os.major} >= 10} {
        # also "-framework Appkit"?
        configure.ldflags-append -framework CoreFoundation
    }
}

variant wxwidgets description {Build wxWidgets support} {
    wxWidgets.use               wxWidgets-3.0
    patchfiles-delete           patch-disable_wx.diff
    patchfiles-append           patch-lib_wx_configure.in.diff \
                                patch-lib_wx_configure.diff
    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} {
    configure.args-delete       --without-ssl
    configure.args-append       --with-ssl=${prefix}
    configure.ldflags-append    -lz
    depends_lib-append          path:lib/libssl.dylib:openssl
}

variant hipe conflicts nohipe description {Enable HiPE (native-code bytecode compiler)} {
    configure.args-delete       --disable-hipe
    configure.args-append       --enable-hipe
}

variant nohipe conflicts hipe description {Legacy compatibility variant} {}

if {[variant_isset nohipe]} {
    default_variants -hipe
} else {
    default_variants +hipe
}

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
livecheck.type      regex
livecheck.version   ${version}
livecheck.url       ${homepage}download/
livecheck.regex     "otp_src_(\[0-9\]+\\.\[0-9\]+(\\.\[0-9\])?)\\.tar\\.gz"