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

name            swig
version         4.3.1
revision        0
checksums       sha1 a111f4433c2348a3186698e1b750b4944bc34c7b \
                rmd160 a7a24420731ee469e557b9761063bec602f17db6 \
                sha256 44fc829f70f1e17d635a2b4d69acab38896699ecc24aa023e516e0eabbec61b8 \
                size   8620774

categories      devel
maintainers     {michaelld @michaelld} openmaintainer

# The swig tool itself is GPL, but code incorporated into dependents has a
# permissive license.
license         {GPL-3+ Permissive}

description     Tool to wrap C/C++ libraries in high-level languages.
long_description \
    SWIG is a software development tool that connects programs written in C \
    and C++ with a variety of high-level programming languages. SWIG is \
    primarily used with common scripting languages such as Perl, Python, \
    Tcl/Tk, Ruby, Guile and MzScheme, however the list of supported languages \
    also includes non-scripting languages such as Java and OCAML. SWIG is \
    most commonly used to create high-level interpreted programming \
    environments, user interfaces, and as a tool for testing and \
    prototyping C/C++ software. SWIG may be freely used, distributed, and \
    modified for commercial and noncommercial use.

homepage        http://www.swig.org/
master_sites    sourceforge:project/swig/swig/swig-${version}

depends_build   port:bison \
                port:gsed

patchfiles      csharp.cxx.patch
post-patch {
    # The configure script should use GNU sed
    reinplace "s/\[\[:<:\]\]sed\[\[:>:\]\]/gsed/g" ${worksrcpath}/configure
}

# FIXME: This is set regardless of the subport because while the
# language binding subports install only platform-independent files,
# they still build all of swig's C++ (and C) code.
# Modules/java.cxx:1433: error: ISO C++ does not support ‘long long’
compiler.cxx_standard   2011

build.target

test.run        yes
test.target     check

configure.args-append --without-javascript

array set bindings {
    csharp      {port:mono              csharp}
    d           {port:phobos            d}
    go          {port:go                go}
    guile       {port:guile-2.2         "guile=${prefix}/bin/guile-2.2 --with-guile-config=${prefix}/bin/guile-config-2.2"}
    java        {{}                     java}
    lua         {port:lua               "lua=${prefix}/bin/lua"}
    ocaml       {port:ocaml             ocaml}
    octave      {path:bin/octave:octave octave}
    perl        {path:bin/perl:perl5    perl5=${prefix}/bin/perl}
    php         {{}                     php}
    python      {port:python_select     python}
    r           {port:R                 r}
    ruby        {port:ruby23            "ruby=${prefix}/bin/ruby2.3"}
    tcl         {port:tcl               tcl}
}
array set prettynames {csharp "C#"
    d D go Go guile Guile java Java lua Lua
    ocaml "Objective Caml" octave Octave perl5 Perl php "PHP 8"
    python Python r R ruby Ruby tcl Tcl}

options         swig.lang
default         swig.lang ""

foreach lang [lsort [array names bindings]] {
    set arg      [lindex $bindings($lang) 1]
    set arg_name [lindex [split ${arg} =] 0]
    subport swig-${lang} "
        depends_lib-append      [lindex $bindings($lang) 0]
        configure.args-append   --with-${arg}
        destroot.args           lib-languages=\"${arg_name}\"
        swig.lang               $arg_name
        platforms               any
        supported_archs         noarch
    "
    if {${swig.lang} != $arg_name} {
        configure.args-append --without-${arg_name}
    }
}

subport swig-java {
    PortGroup               java 1.0

    java.version            1.4+
    # Set fallback to an LTS Java version
    java.fallback           openjdk8
}

subport swig-php {

    # patch to fix finding of "php-configXY"
    patchfiles-append patch-configure.diff

    set php_vers {80 81 82 83 84}
    set php_portnames [lmap ver $php_vers {string cat php${ver}}]
    set php_default_ver 83
    foreach php_ver $php_vers {
        variant php${php_ver} \
            conflicts {*}[ldelete $php_portnames php${php_ver}] \
            description "build using PHP 8.[string index $php_ver 1]" "
            depends_lib-append  port:php${php_ver}
            configure.args-replace --with-php \
                                   --with-php=${prefix}/bin/php${php_ver}
        "
        if {$php_ver ne $php_default_ver && [variant_isset php${php_ver}]} {
            set any_php_variant_set 1
        }
    }
    if {![info exists any_php_variant_set]} {
        default_variants +php${php_default_ver}
        if {[variant_isset php${php_default_ver}]} {
            set any_php_variant_set 1
        }
    }
    if {![info exists any_php_variant_set]} {
        ui_error "\n\nA +phpXY variant must be selected; the variant '-php${php_default_ver}' cannot be used alone.\n"
        return -code error "Invalid variant selection"
    }
}

subport swig-ruby {

    variant ruby186 conflicts ruby187 ruby19 ruby20 ruby21 ruby22 ruby23 ruby24 description {build using Ruby 1.8.6} {
        depends_lib-delete port:ruby23
        depends_lib-append port:ruby186
        configure.args-delete --with-ruby=${prefix}/bin/ruby2.3
        configure.args-append --with-ruby=${prefix}/bin/ruby
    }

    variant ruby187 conflicts ruby186 ruby19 ruby20 ruby21 ruby22 ruby23 ruby24 description {build using Ruby 1.8.7} {
        depends_lib-delete port:ruby23
        depends_lib-append port:ruby
        configure.args-delete --with-ruby=${prefix}/bin/ruby2.3
        configure.args-append --with-ruby=${prefix}/bin/ruby1.8
    }

    variant ruby19 conflicts ruby186 ruby187 ruby20 ruby21 ruby22 ruby23 ruby24 description {build using Ruby 1.9} {
        depends_lib-delete port:ruby23
        depends_lib-append port:ruby19
        configure.args-delete --with-ruby=${prefix}/bin/ruby2.3
        configure.args-append --with-ruby=${prefix}/bin/ruby1.9
    }

    variant ruby20 conflicts ruby186 ruby187 ruby19 ruby21 ruby22 ruby23 ruby24 description {build using Ruby 2.0} {
        depends_lib-delete port:ruby23
        depends_lib-append port:ruby20
        configure.args-delete --with-ruby=${prefix}/bin/ruby2.3
        configure.args-append --with-ruby=${prefix}/bin/ruby2.0
    }

    variant ruby21 conflicts ruby186 ruby187 ruby19 ruby20 ruby22 ruby23 ruby24 description {build using Ruby 2.1} {
        depends_lib-delete port:ruby23
        depends_lib-append port:ruby21
        configure.args-delete --with-ruby=${prefix}/bin/ruby2.3
        configure.args-append --with-ruby=${prefix}/bin/ruby2.1
    }

    variant ruby22 conflicts ruby186 ruby187 ruby19 ruby20 ruby21 ruby23 ruby24 description {build using Ruby 2.2} {
        depends_lib-delete port:ruby23
        depends_lib-append port:ruby22
        configure.args-delete --with-ruby=${prefix}/bin/ruby2.3
        configure.args-append --with-ruby=${prefix}/bin/ruby2.2
    }

    variant ruby23 conflicts ruby186 ruby187 ruby19 ruby20 ruby21 ruby22 ruby24 description {build using Ruby 2.3} {
#        depends_lib-delete port:ruby
#        depends_lib-append port:ruby23
#        configure.args-delete --with-ruby=${prefix}/bin/ruby2.3
#        configure.args-append --with-ruby=${prefix}/bin/ruby2.3
    }

    variant ruby24 conflicts ruby186 ruby187 ruby19 ruby20 ruby21 ruby22 ruby23 description {build using Ruby 2.4} {
        depends_lib-delete port:ruby23
        depends_lib-append port:ruby24
        configure.args-delete --with-ruby=${prefix}/bin/ruby2.3
        configure.args-append --with-ruby=${prefix}/bin/ruby2.4
    }

    if {![variant_isset ruby186] && ![variant_isset ruby187] &&
        ![variant_isset ruby19] && ![variant_isset ruby20] &&
        ![variant_isset ruby21] && ![variant_isset ruby22] &&
        ![variant_isset ruby23] && ![variant_isset ruby24]} {
        default_variants +ruby23
    }
    if {![variant_isset ruby186] && ![variant_isset ruby187] &&
        ![variant_isset ruby19] && ![variant_isset ruby20] &&
        ![variant_isset ruby21] && ![variant_isset ruby22] &&
        ![variant_isset ruby23] && ![variant_isset ruby24]} {
        ui_error "\n\nA +rubyXY variant must be selected; the variant '-ruby23' cannot be used alone.\n"
        return -code error "Invalid variant selection"
    }
}

if {${swig.lang} eq ""} {
    depends_lib     port:pcre2

    set docdir      ${prefix}/share/doc/${name}-${version}

    destroot.args   lib-languages="typemaps"

    post-destroot {
        xinstall -d ${destroot}${docdir}
        xinstall -m 0644 -W ${worksrcpath} ANNOUNCE CHANGES LICENSE README \
            TODO ${destroot}${docdir}
    }

    variant doc description {Install extra documentation} {
        post-destroot {
            xinstall -d ${destroot}${docdir}/html
            xinstall -m 0644 {*}[glob ${worksrcpath}/Doc/Manual/*.{css,html,pdf,png}] \
                ${destroot}${docdir}/html
        }
    }

    livecheck.type      regex
    livecheck.url       http://swig.org/download.html
    livecheck.regex     The latest release is <.*>${name}-(\\d+(?:\\.\\d+)*)

} else {

    description     $prettynames(${swig.lang}) binding for swig
    long_description    \
        SWIG is a software development tool that connects programs written in C \
        and C++ with a variety of high-level programming languages. This is the \
        $prettynames(${swig.lang}) binding.
    depends_lib-append  port:swig
    livecheck.type      none
    post-destroot {
        delete ${destroot}${prefix}/bin
        delete ${destroot}${prefix}/share/man
        foreach f [glob -directory ${destroot}${prefix}/share/swig/${version} *] {
            if {[file tail $f] != ${swig.lang}} {
                delete $f
            }
        }
    }
}
