# -*- 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           cmake 1.1
PortGroup           gitlab 1.0
PortGroup           legacysupport 1.1
PortGroup           mpi 1.0
PortGroup           muniversal 1.0

# Undefined symbols for std::bad_variant_access
# 10.12 Sierra = Darwin 16
legacysupport.newest_darwin_requires_legacy 16
legacysupport.use_mp_libcxx                 yes

# Upstream says require C++17
compiler.cxx_standard 2017

# some older Clang say they support C++11 when they don't
# this is the same blacklisting as for jsoncpp, on which vtk depends.
# compiler.blacklist-append {clang < 900}

# VTK 9.5.1+ Cmake says: Apple Clang 11.0 or later is required
compiler.blacklist-append {clang < 1100}

# Note, gitlab instance downloads distfile from gitlab tag in bz2 format,
# not from the official VTK download page in gz format.

gitlab.instance     https://gitlab.kitware.com
gitlab.setup        vtk vtk 9.5.2 v
revision            1
categories          graphics devel
license             BSD

checksums           rmd160  06caa56eedf6a01d1e10bcc07ab2242e2dee1252 \
                    sha256  a85763e3c040b6ae3139f1097339369456e1cc9d2aa6920f46da3709ddbe48e4 \
                    size    40310394

maintainers         {@Dave-Allured noaa.gov:dave.allured} \
                    openmaintainer

description         Visualization Toolkit (VTK)
long_description    ${description} is an open-source, freely \
                    available software system for 3D computer graphics, \
                    image processing and visualization.

homepage            https://www.vtk.org

mpi.setup

set proj_version    proj9

depends_lib-append  path:share/pkgconfig/eigen3.pc:eigen3 \
                    port:hdf5 \
                    port:libxml2 \
                    port:${proj_version} \
                    port:tiff

configure.args-append \
                    -DVTK_MODULE_USE_EXTERNAL_VTK_eigen:BOOL=ON \
                    -DVTK_MODULE_USE_EXTERNAL_VTK_hdf5:BOOL=ON \
                    -DVTK_MODULE_USE_EXTERNAL_VTK_libproj:BOOL=ON \
                    -DVTK_MODULE_USE_EXTERNAL_VTK_libxml2:BOOL=ON \
                    -DVTK_MODULE_USE_EXTERNAL_VTK_tiff:BOOL=ON

mpi.enforce_variant hdf5

# PROJ needs extra help for multiple versioned subdirectories.
configure.args-append \
                    -DLibPROJ_ROOT=${prefix}/lib/${proj_version}

# Re restoring support for legacy macOS, see:
# https://gitlab.kitware.com/vtk/vtk/-/merge_requests/11171
# https://gitlab.kitware.com/vtk/vtk/-/issues/19352
patchfiles-append   patch-MACH_TASK_BASIC_INFO.diff

configure.pre_args-delete \
                    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON

configure.args-append \
                    ../${distname}/ \
                    -DCMAKE_BUILD_TYPE=Release \
                    -DBUILD_SHARED_LIBS=ON \
                    -DVTK_BUILD_EXAMPLES:BOOL=OFF \
                    -DVTK_WRAP_PYTHON:BOOL=OFF \
                    -DVTK_WRAP_JAVA:BOOL=OFF \
                    -DVTK_USE_COCOA:BOOL=ON

# Do not allow any remote downloads of code or test data.
configure.args-append \
                    -DVTK_FORBID_DOWNLOADS:BOOL=ON

# Support for Cocoa of 10.6 has been dropped in
# https://github.com/Kitware/VTK/commit/f2e249c0a6a93f12808822cc5e5dddda35a09a35
# However it is an optional module. Instead of breaking the port,
# just disable what is broken. Notice, it may also be possible
# to build with SDL2 backend (VTK_USE_SDL2) or X-less Mesa.
if {${os.platform} eq "darwin" && ${os.major} < 11} {
    # Avoid hardcoded failure:
    patchfiles-append   patch-vtkApple.cmake.diff
    # Fix for pthreads, only relevant for < 10.7:
    patchfiles-append   patch-pthread_threadid_np.diff
    # Ensure Cocoa does not get picked and fix GL linking:
    patchfiles-append   patch-use-x11.diff

    # Fix 32-bit builds with gcc:
    if {[string match *gcc* ${configure.compiler}] \
        && ${configure.build_arch} in [list arm i386 ppc]} {
        patchfiles-append \
                        patch-DataModel.diff
    }

    depends_lib-append  port:mesa \
                        port:xorg-libX11 \
                        port:xorg-libXcursor

    configure.args-append \
                        -DVTK_USE_X:BOOL=ON
    configure.args-replace \
                        -DVTK_USE_COCOA:BOOL=ON -DVTK_USE_COCOA:BOOL=OFF

    # As proposed at #46890
    configure.args-append \
                        -DIOKit:FILEPATH=/System/Library/Frameworks/IOKit.framework
}

# loguru.cpp:576:90: error: invalid conversion from 'long unsigned int'
# to 'size_t*' {aka 'long unsigned int*'} [-fpermissive]
# loguru.cpp:580:68: error: invalid conversion from 'const char*'
# to 'char*' [-fpermissive]. Perhaps specific to older SDKs,
# but gcc is not used by default elsewhere anyway.
if {[string match *gcc* ${configure.compiler}]} {
    configure.cxxflags-append \
                        -fpermissive
}

variant ffmpeg description {Add support for ffmpeg} {
    depends_lib-append port:ffmpeg
    configure.args-append \
        -DModule_vtkIOFFMPEG:BOOL=ON
}

variant qt5 description {Add Qt5 support.} {
    PortGroup           qt5 1.0

    configure.args-append \
                        -DQT_QMAKE_EXECUTABLE:PATH=${qt_qmake_cmd} \
                        -DQMLPLUGINDUMP_EXECUTABLE:PATH=${qt_bins_dir}/qmlplugindump \
                        -DVTK_QT_VERSION=5 \
                        -DVTK_GROUP_ENABLE_Qt:STRING=YES \
                        -DVTK_MODULE_ENABLE_VTK_GUISupportQt=YES \
                        -DVTK_MODULE_ENABLE_VTK_RenderingQt=YES \
                        -DVTK_MODULE_ENABLE_VTK_ViewsQt=YES
}

# Supported pythons
set python_versions {310 311 312 313}

foreach pyver ${python_versions} {
    # Conflicting python versions
    set other_python_versions {}
    foreach other_pyver ${python_versions} {
        if {${other_pyver} ne ${pyver}} {
            if {${other_pyver} ni ${other_python_versions}} {
                lappend other_python_versions python${other_pyver}
            }
        }
    }

    # Get python branch
    set python_branch  "[string index ${pyver} 0].[string range ${pyver} 1 end]"
    set python_major_ver "[string index ${pyver} 0]"

    variant python${pyver} conflicts {*}${other_python_versions} description "Add Python ${python_branch} support" "
            depends_lib-append  port:python${pyver}
            configure.args-delete \
                -DVTK_WRAP_PYTHON:BOOL=OFF

            configure.args-append \
                -DVTK_WRAP_PYTHON:BOOL=ON \
                -DPython${python_major_ver}_EXECUTABLE:STRING=${prefix}/bin/python${python_branch} \
                -DPython${python_major_ver}_INCLUDE_DIR=${frameworks_dir}/Python.framework/Versions/${python_branch}/Headers/ \
                -DPython${python_major_ver}_LIBRARY=${frameworks_dir}/Python.framework/Versions/${python_branch}/lib/libpython${python_branch}.dylib \
                -DVTK_PYTHON_SITE_PACKAGES_SUFFIX=Library/Frameworks/Python.framework/Versions/${python_branch}/lib/python${python_branch}/site-packages \

            if {[mpi_variant_isset]} {
                depends_lib-append port:py${pyver}-mpi4py
                configure.args-append \
                    -DVTK_USE_SYSTEM_MPI4PY:BOOL=ON
            }

            post-destroot {
                # generate setup.py and other necessary files to create .dist-info folder
                system -W ${workpath}/build \"cmake -DVTK_WHEEL_BUILD:BOOL=ON -DVTK_VERSION_SUFFIX= ${worksrcpath}\"
                set site_pack_dir Library/Frameworks/Python.framework/Versions/${python_branch}/lib/python${python_branch}/site-packages
                delete -force ${workpath}/build/vtkmodules
                copy ${workpath}/build/\${site_pack_dir}/vtkmodules ${workpath}/build
                # create the .dist-info folder and contents
                system -W ${workpath}/build \"${prefix}/bin/python${python_branch} setup.py dist_info\"
                copy ${workpath}/build/${name}-${version}.dist-info ${destroot}${prefix}/\${site_pack_dir}
            }
    "
}

if {[mpi_variant_isset]} {
    configure.args-append \
        -DVTK_Group_MPI:BOOL=ON \
        -DVTK_USE_MPI:BOOL=ON
}

# Avoid release candidates
gitlab.livecheck.regex  {([0-9.]+)}
