# -*- 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           active_variants 1.1
PortGroup           cmake 1.1
PortGroup           github 1.0
PortGroup           boost 1.0

github.setup        ukoethe vigra 1-11-1 Version-
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
version             [strsed ${github.version} {g/-/./}]
revision            23
categories          graphics
platforms           darwin
license             MIT
maintainers         {gmail.com:benjamin.seppke @BSeppke}
description         Generic Image Processing Library for C++
long_description    VIGRA stands for \"Vision with Generic Algorithms\". \
                    It's a novel computer vision library that puts its \
                    main emphasis on customizable algorithms and data \
                    structures. By using template techniques similar to \
                    those in the C++ Standard Template Library, you can \
                    easily adapt any VIGRA component to the needs of your \
                    application, without thereby giving up execution speed.
homepage            https://ukoethe.github.io/vigra/

checksums           rmd160  21378c72a0702cbdd4de4d79c59f0587f6bcde8d \
                    sha256  b0cdf67e283d2b52e35a45cdcff44453b5b73a9b7d7a1ca84d6508107e49832f \
                    size    34212258

depends_lib         path:include/turbojpeg.h:libjpeg-turbo \
                    port:tiff \
                    port:libpng \
                    port:szip \
                    port:zlib \
                    port:openexr2 \
                    port:fftw-3-single \
                    port:hdf5

compiler.cxx_standard   2011
configure.args-append \
                    -DOPENEXR_INCLUDE_DIR=${prefix}/libexec/openexr2/include/OpenEXR \
                    -DOPENEXR_ILMIMF_LIBRARY=${prefix}/libexec/openexr2/lib/libIlmImf.dylib \
                    -DOPENEXR_HALF_LIBRARY=${prefix}/libexec/openexr2/lib/libHalf.dylib \
                    -DOPENEXR_IEX_LIBRARY=${prefix}/libexec/openexr2/lib/libIex.dylib \
                    -DOPENEXR_ILMTHREAD_LIBRARY=${prefix}/libexec/openexr2/lib/libIlmThread.dylib \
                    -DOPENEXR_IMATH_LIBRARY=${prefix}/libexec/openexr2/lib/libImath.dylib

# modify tests for existence of c++11 std::unique_ptr
# see https://github.com/ukoethe/vigra/pull/421
patchfiles-append   patch-uniq_ptr.diff

# see https://trac.macports.org/ticket/54548
patchfiles-append   patch-template.diff

# see https://github.com/ukoethe/vigra/pull/451/commits/a6fa62663c6a6b752ed0707e95f643e25867a0f9
patchfiles-append   patch-python.diff

# allow cmake to find MacPorts boost_python library
patchfiles-append   patch-boost_python.diff

# see https://trac.macports.org/ticket/59970
patchfiles-append   patch-sifImport.diff

# upstream patch for hdf5 1.12+ compatibility
# see https://trac.macports.org/ticket/69007
patchfiles-append   patch-hdf5-1.12.diff

post-patch {
    reinplace "s|@DOCDIR@|${prefix}/share/doc/${name}|g" ${worksrcpath}/config/vigra-config.in
}

# Some additional parameters for cmake. All other params have already been set
# by the cmake PortGroup
configure.args-append   -DWITH_OPENEXR=ON \
                        -DBUILD_SHARED_LIBS=ON \
                        -DWITH_VIGRANUMPY=NO

# recursive template instantiation exceeded maximum depth of 128
configure.cxxflags-append -ftemplate-depth-1024

# The -Os setting causes errors w.r.t. libc++ and clang++. Use -O3 instead
configure.cxxflags-replace -Os -O3

destroot.env-append DESTDIR=${destroot}

post-destroot {
    if {[variant_isset docs]} {
        # move documentation to a unversioned directory
        xinstall -m 755 -d ${destroot}${prefix}/share/doc
        move ${destroot}${prefix}/doc/${name} \
             ${destroot}${prefix}/share/doc/${name}
    }
    # delete whatever is left in the doc directory
    delete ${destroot}${prefix}/doc
}

variant docs description "Build documentation" {}
if {[variant_isset docs]} {
    depends_build-append path:bin/doxygen:doxygen
    depends_skip_archcheck doxygen
} else {
    configure.args-append   -DDOXYGEN_DOT_EXECUTABLE= \
                            -DDOXYGEN_EXECUTABLE=
}

variant valgrind description "Include support for VALGRIND" {}
if {[variant_isset valgrind]} {
    depends_lib-append port:valgrind
} else {
    configure.args-append -DWITH_VALGRIND=NO
}

set py_vers [list 2.7 3.9 3.10 3.11 3.12]

proc py_conflict_list {py_vers py_ver} {
    set py_conf_vers [lsearch -inline -all -not -exact $py_vers $py_ver]
    set conf [list]
    foreach v [string map {. {}} ${py_conf_vers}] {
        lappend conf python${v}
    }
    return ${conf}
}

set active_py ""
foreach py_ver ${py_vers} {
    set py_ver_nodot [string map {. {}} ${py_ver}]
    if { [variant_isset python${py_ver_nodot}] } {
        set active_py ${py_ver}
        set active_py_nodot [string map {. {}} ${py_ver}]
    }
    variant python${py_ver_nodot} conflicts [py_conflict_list ${py_vers} ${py_ver}] \
        description "Also build vigranumpy python ${py_ver} bindings" {
            configure.args-delete  -DWITH_VIGRANUMPY=NO
            configure.args-append  -DPYTHON_EXECUTABLE=${prefix}/bin/python${active_py} \
                                   -DPYTHON_SPHINX=${prefix}/bin/sphinx-build-${active_py}
            depends_lib-append  port:python${active_py_nodot} \
                                port:py${active_py_nodot}-numpy \
                                port:py${active_py_nodot}-sphinx
            require_active_variants [boost::depends_portname] python${active_py_nodot}
        }
}

if { ${active_py} eq "" } {
    # default for boost
    set active_py 3.12
    set active_py_nodot [string map {. {}} ${active_py}]
    default_variants +python${active_py_nodot}
}
