# -*- 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           conflicts_build 1.0
PortGroup           github 1.0

name                InsightToolkit
github.setup        InsightSoftwareConsortium ITK 5.3.0 v
revision            0
categories          graphics science devel
license             Apache
conflicts_build     InsightToolkit4

maintainers         {stromnov @stromnov} {yahoo.com:n_larsson @nilason} openmaintainer

description         Insight Segmentation and Registration Toolkit (ITK)

long_description    {*}${description} is an open-source, cross-platform system that \
                    provides developers with an extensive suite of software tools \
                    for image analysis. Developed through extreme programming \
                    methodologies, ITK builds on a proven, spatially-oriented \
                    architecture for processing, segmentation, and registration \
                    of scientific images in two, three, or more dimensions.

homepage            https://itk.org

distname            InsightToolkit-${version}
dist_subdir         InsightToolkit

checksums           rmd160  39799367b41bf86b49fcc8a96260c99db4996fd6 \
                    sha256  aace3267f84da4f89d6de404fe07b2300ee5c3e65e44ba3eb8a4e7b5de5942fb \
                    size    21843788

# find out include dir for "mpi.h"
set mpl_include_dir ""
if {![catch {set result [active_variants hdf5 openmpi]}]} {
    if {$result} {
        set mpl_include_dir "-I${prefix}/include/openmpi-mp"
    }
}
if {![catch {set result [active_variants hdf5 mpich]}]} {
    if {$result} {
        set mpl_include_dir "-I${prefix}/include/mpich-mp"
    }
}
if {$mpl_include_dir ne ""} {
    configure.cxxflags-append ${mpl_include_dir}
}

compiler.cxx_standard 2014

patchfiles-append   patch-OpenCL-CMake.diff \
                    patch-swigpy-sliceobject.diff

depends_lib-append  path:include/turbojpeg.h:libjpeg-turbo \
                    port:dcmtk \
                    port:double-conversion \
                    port:eigen3 \
                    port:expat \
                    port:fftw-3 \
                    port:fftw-3-single \
                    port:gdcm \
                    port:gtest \
                    port:hdf5 \
                    port:libaec \
                    port:libminc \
                    port:libpng \
                    port:tiff \
                    port:vxl \
                    port:zlib

configure.args-append \
                    -DBUILD_EXAMPLES=OFF \
                    -DBUILD_SHARED_LIBS=ON \
                    -DBUILD_TESTING=OFF \
                    -DCMAKE_INSTALL_NAME_DIR=${prefix}/lib/itk \
                    -DITK_FORBID_DOWNLOADS=ON \
                    -DITK_INSTALL_LIBRARY_DIR=${prefix}/lib/itk \
                    -DITK_USE_FFTWD=ON \
                    -DITK_USE_FFTWF=ON \
                    -DITK_USE_GPU=OFF \
                    -DITK_USE_KWSTYLE=OFF \
                    -DITK_USE_SYSTEM_DCMTK=ON \
                    -DITK_USE_SYSTEM_DOUBLECONVERSION=ON \
                    -DITK_USE_SYSTEM_EIGEN=ON \
                    -DITK_USE_SYSTEM_EXPAT=ON \
                    -DITK_USE_SYSTEM_FFTW=ON \
                    -DITK_USE_SYSTEM_GDCM=ON \
                    -DITK_USE_SYSTEM_JPEG=ON \
                    -DITK_USE_SYSTEM_LIBRARIES=ON \
                    -DITK_USE_SYSTEM_MINC=ON \
                    -DITK_USE_SYSTEM_PNG=ON \
                    -DITK_USE_SYSTEM_TIFF=ON \
                    -DITK_USE_SYSTEM_VXL=ON \
                    -DITK_USE_SYSTEM_ZLIB=ON \
                    -DITK_WRAPPING=OFF \
                    -DITK_WRAP_PYTHON=OFF \
                    -DUSE_FFTWF=ON \
                    -DVXL_DIR=${prefix}/share/vxl/cmake

configure.ldflags-append \
                    -L${prefix}/lib/vxl

# create Python subports
set python_versions {310 311}
foreach v ${python_versions} {
    subport py${v}-${name} {
        PortGroup           python 1.0

        categories          graphics science devel python

        depends_build-append \
                            port:castxml \
                            port:swig \
                            port:swig-python

        depends_lib-append  port:${name}

        use_configure       yes

        python.default_version  ${v}
        python.pep517       no

        build.cmd           make
        build.target
        destroot.cmd        make install
        destroot.destdir    DESTDIR=${destroot}

        master_sites        ${github.master_sites}

        post-patch {
            fs-traverse f ${worksrcpath} {
                if {[file extension ${f}] eq ".py"} {
                    reinplace -q "s|/usr/bin/env python|${python.bin}|" ${f}
                }
            }
        }
    }
}

# Python bindings for supported Python versions
if {[string match "py*" ${subport}]} {
    description             Python ${python.branch} bindings for {*}${description}
    long_description        This package provides {*}${description}.

    configure.args-append   -DITK_USE_SYSTEM_CASTXML=ON \
                            -DITK_USE_SYSTEM_SWIG=ON \
                            -DPY_SITE_PACKAGES_PATH=${python.pkgd} \
                            -DPython3_EXECUTABLE=${python.bin}

    configure.args-replace  -DITK_WRAPPING=OFF    -DITK_WRAPPING=ON \
                            -DITK_WRAP_PYTHON=OFF -DITK_WRAP_PYTHON=ON

    post-destroot {
        delete ${destroot}${prefix}/bin
        delete ${destroot}${prefix}/include
        delete ${destroot}${prefix}/lib
        delete ${destroot}${prefix}/share
    }
}
