# -*- 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.1.1
revision        0
checksums       rmd160  ff2415693369f0e8cfb8c9c6bf32ab3d6fd9465e \
                sha256  2af08aced8fcd65cdb5cc62426768914bedc735b1c250325203716f78e39ac9b \
                size    8600805

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

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

build.target

test.run        yes
test.target     check

configure.args-append --without-javascript

array set bindings {
    chicken     {port:chicken           chicken}
    clisp       {port:clisp             clisp}
    csharp      {port:mono              csharp}
    gcj         {port:gcc47             "gcj=${prefix}/bin/gcj-mp-4.7 --with-gcjh=${prefix}/bin/gcjh-mp-4.7"}
    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         {port:php73             "php=${prefix}/bin/php73"}
    pike        {port:pike              pike}
    python      {port:python_select     python}
    r           {port:R                 r}
    ruby        {port:ruby23            "ruby=${prefix}/bin/ruby2.3"}
    tcl         {port:tcl               tcl}
}
array set prettynames {chicken Chicken clisp CLISP csharp "C#"
    d D gcj GCJ go Go guile Guile java Java lua Lua
    ocaml "Objective Caml" octave Octave perl5 Perl php "PHP 7" pike Pike
    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

    variant php70 conflicts php71 php72 php73 description {build using PHP 7.0} {
        depends_lib-replace port:php73 port:php70
        configure.args-replace --with-php=${prefix}/bin/php73 \
                               --with-php=${prefix}/bin/php70
    }

    variant php71 conflicts php70 php72 php73 description {build using PHP 7.1} {
        depends_lib-replace port:php73 port:php71
        configure.args-replace --with-php=${prefix}/bin/php73 \
                               --with-php=${prefix}/bin/php71
    }

    variant php72 conflicts php70 php71 php73 description {build using PHP 7.2} {
        depends_lib-replace port:php73 port:php72
        configure.args-replace --with-php=${prefix}/bin/php73 \
                               --with-php=${prefix}/bin/php72
    }

    variant php73 conflicts php70 php71 php72 description {build using PHP 7.3} {}

    if {![variant_isset php70] && ![variant_isset php71] &&
        ![variant_isset php72] && ![variant_isset php73]} {
        default_variants +php73
    }
    if {![variant_isset php70] && ![variant_isset php71] &&
        ![variant_isset php72] && ![variant_isset php73]} {
        ui_error "\n\nA +phpXY variant must be selected; the variant '-php72' cannot be used alone.\n"
        return -code error "Invalid variant selection"
    }
}

subport swig-gcj {
    variant gcc43 conflicts gcc44 gcc45 gcc47 gcc48 description {build using GCJ 4.3} {
        depends_lib-delete port:gcc47
        depends_lib-append port:gcc43
        configure.args-delete --with-gcj=${prefix}/bin/gcj-mp-4.7 --with-gcjh=${prefix}/bin/gcjh-mp-4.7
        configure.args-append --with-gcj=${prefix}/bin/gcj-mp-4.3 --with-gcjh=${prefix}/bin/gcjh-mp-4.3
    }

    variant gcc44 conflicts gcc43 gcc45 gcc47 gcc48 description {build using GCJ 4.4} {
        depends_lib-delete port:gcc47
        depends_lib-append port:gcc44
        configure.args-delete --with-gcj=${prefix}/bin/gcj-mp-4.7 --with-gcjh=${prefix}/bin/gcjh-mp-4.7
        configure.args-append --with-gcj=${prefix}/bin/gcj-mp-4.4 --with-gcjh=${prefix}/bin/gcjh-mp-4.4
    }

    variant gcc45 conflicts gcc43 gcc44 gcc47 gcc48 description {build using GCJ 4.5} {
        depends_lib-delete port:gcc47
        depends_lib-append port:gcc45
        configure.args-delete --with-gcj=${prefix}/bin/gcj-mp-4.7 --with-gcjh=${prefix}/bin/gcjh-mp-4.7
        configure.args-append --with-gcj=${prefix}/bin/gcj-mp-4.5 --with-gcjh=${prefix}/bin/gcjh-mp-4.5
    }

    # No +gcc46 because the gcc46 port does not build GCJ.

    variant gcc48 conflicts gcc43 gcc44 gcc45 gcc47 description {build using GCJ 4.8} {
        depends_lib-delete port:gcc47
        depends_lib-append port:gcc48
        configure.args-delete --with-gcj=${prefix}/bin/gcj-mp-4.7 --with-gcjh=${prefix}/bin/gcjh-mp-4.7
        configure.args-append --with-gcj=${prefix}/bin/gcj-mp-4.8 --with-gcjh=${prefix}/bin/gcjh-mp-4.8
    }

    # Placeholder variant for the default.
    variant gcc47 conflicts gcc43 gcc44 gcc45 gcc48 description {build using GCJ 4.7} {}

    if {![variant_isset gcc43] && ![variant_isset gcc44] && ![variant_isset gcc45] &&
        ![variant_isset gcc47] && ![variant_isset gcc48]} {
        default_variants +gcc47
    }
    if {![variant_isset gcc43] && ![variant_isset gcc44] && ![variant_isset gcc45] &&
        ![variant_isset gcc47] && ![variant_isset gcc48]} {
        ui_error "\n\nA +gcc4X variant must be selected; the variant '-gcc47' 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
            }
        }
    }
}
