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

name                shogun
version             2.1.0
set branch          [join [lrange [split ${version} .] 0 1] .]
revision            26
categories          math science
platforms           darwin
license             GPL-3

conflicts           shogun-devel

maintainers         nomaintainer

description         The machine-learning toolbox's focus is on large-scale \
                    kernel methods

long_description    ${description} and especially on Support Vector Machines \
                    (SVM). It provides a generic SVM object interfacing to \
                    several different SVM implementations, among them the \
                    state of the art OCAS, LibSVM, SVMLight, SVMLin and GPDT.

homepage            http://shogun-toolbox.org
master_sites        ${homepage}/archives/shogun/releases/${branch}/sources/

use_bzip2           yes

checksums           rmd160  5fece569be7d95bbe9ad406f1b5e10f06f4ac20e \
                    sha256  6c47941a98d506d7b1e21d5bfd511f8684a2e1f371accd4756cdc8821916a63b \
                    size    4241518

worksrcdir          ${name}-${version}/src

patchfiles          patch-src_configure.diff

post-patch {
    reinplace "s|@@MAKEDEPEND@@|${configure.cc} -MM|g" ${worksrcpath}/configure
}

depends_build-append port:pkgconfig

# FIXME: enable use of OpenBLAS(-devel) and Accelerate/vecLibFort?
depends_lib-append  port:hdf5 \
                    port:json-c \
                    port:libxml2 \
                    port:readline \
                    port:atlas \
                    port:arpack \
                    path:share/pkgconfig/eigen3.pc:eigen3 \
                    port:lp_solve \
                    port:glpk \
                    port:lzo2 \
                    port:snappy \
                    port:gzip \
                    port:bzip2 \
                    port:xz

universal_variant   no

pre-fetch {
    if {${os.major} <= 8} {
        return -code error "Shogun is only supported on Leopard or above"
    }
}

compilers.choose   fc f77 f90
compilers.setup    require_fortran
pre-configure {
    configure.args-append   --ldflags=${compilers.libfortran}
}

# These compiler warnings appear:
#clang: warning: argument unused during compilation: '-mcpu=native'
#clang: warning: argument unused during compilation: '-pthread'
#ld: warning: directory not found for option '-L../libshogun'
#ld: warning: directory not found for option '-LDYLD_LIBRARY_PATH'

# Define python variants
set pythons_suffixes {27}

set pythons_ports {}
foreach s ${pythons_suffixes} {
    lappend pythons_ports python${s}
}

foreach s ${pythons_suffixes} {
    set p python${s}
    set v [string index ${s} 0].[string index ${s} 1]
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    variant ${p} description "Use Python ${v} for python bindings" conflicts {*}${c} "
        depends_lib-append      port:${p} \
                                port:py${s}-numpy \
                                port:swig-python

        configure.args-append   --python=${prefix}/bin/python${v}
        post-patch {
            reinplace \"s|^PYTHON=python$|PYTHON=${prefix}/bin/python${v}|g\" ${worksrcpath}/configure
        }
    "
}

pre-configure {
    foreach s ${pythons_suffixes} {
        if {[variant_isset python${s}]} {
            set v [string index ${s} 0].[string index ${s} 1]
            set py_prefix [file normalize [exec ${prefix}/bin/python${v} -c "import sys; print(sys.prefix)"]]
            configure.args-append   --pydir=${py_prefix}/lib/python${v}/site-packages \
                                    --includes=${py_prefix}/include/python${v}
            break
        }
    }
}

variant perl description {Build the Perl API} {
    depends_lib-append  port:swig-perl
}

variant r description {Build the R API} {
    depends_lib-append  port:swig-r
}

variant octave description {Build the Octave API} {
    depends_lib-append  port:swig-octave
}

variant java description {Build the Java API} {
    depends_lib-append  port:swig-java
}

variant csharp description {Build the C# API} {
    depends_lib-append  port:swig-csharp
}

variant lua description {Build the Lua API} {
    depends_lib-append  port:swig-lua
}

variant ruby description {Build the Ruby API} {
    depends_lib-append  port:swig-ruby
}

variant elwms requires python27 octave r description {Build the elwms (eierlegende wollmilchsau) interface. Requires python plus one other interface} {
}

variant hmm_parallel description {Enable parallel structures in hmm training} {
    configure.args-delete --disable-hmm-parallel
    configure.args-append --enable-hmm-parallel
}

proc lremove {list elem} {
    set index [lsearch -exact $list $elem]
    return [lreplace $list $index $index]
}

# Interfaces are added by a comma delimited argument list to --interfaces.
# We check for variants and then build this list accordingly
set interfaces "cmdline_static"

if {[variant_isset python27]} {
    lappend interfaces "python_modular"
}

if {[variant_isset perl]} {
    lappend interfaces "perl_modular"
}

if {[variant_isset r]} {
    lappend interfaces "r_modular"
}

if {[variant_isset octave]} {
    lappend interfaces "octave_modular"
}

if {[variant_isset java]} {
    lappend interfaces "java_modular"
}

if {[variant_isset csharp]} {
    lappend interfaces "csharp_modular"
}

if {[variant_isset lua]} {
    lappend interfaces "lua_modular"
}

if {[variant_isset ruby]} {
    lappend interfaces "ruby_modular"
}

if {[variant_isset elwms]} {
    lappend interfaces "elwms_static"
}

configure.args-append \
                    --enable-hdf5 \
                    --enable-json \
                    --enable-xml \
                    --enable-readline \
                    --enable-largefile \
                    --disable-spinlocks \
                    --enable-lapack \
                    --enable-arpack \
                    --disable-mosek \
                    --disable-superlu \
                    --disable-nlopt \
                    --enable-eigen3 \
                    --disable-cplex \
                    --enable-lpsolve \
                    --enable-glpk \
                    --enable-lzo \
                    --enable-snappy \
                    --enable-gzip \
                    --enable-bzip2 \
                    --enable-lzma \
                    --enable-bigstates \
                    --enable-hmmcache \
                    --enable-svm-light \
                    --disable-logcache \
                    --enable-shortrealkernelcache \
                    --enable-logsum-array \
                    --disable-hmm-parallel \
                    --disable-doxygen \
                    --enable-optimization \
                    --disable-cpudetection \
                    --includes=${prefix}/include \
                    --libs=${prefix}/lib \
                    --destdir=${destroot}

set interfaces [join $interfaces ","]

configure.args-append --interfaces=$interfaces

livecheck.type      regex
livecheck.url       http://www.shogun-toolbox.org
livecheck.regex     {SHOGUN ([0-9.]+)}

if {[string match "*clang*" ${configure.compiler}]} {
    configure.args-delete   --disable-cpudetection
    configure.args-append   --enable-cpudetection
}
