# -*- 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           qt5 1.0
PortGroup           mpi 1.0

name                paraview
version             5.6.2
revision            19

# TODO: Remove, if/when this port is fixed
known_fail          yes

categories          science graphics
platforms           darwin
license             BSD

set branch          [join [lrange [split ${version} .] 0 1] .]

maintainers         {gmail.com:jjstickel @jjstickel} {dstrubbe @dstrubbe} openmaintainer

description         3D data analysis and visualization application

long_description    An open-source, multi-platform data \
    analysis and visualization application. ParaView users can quickly \
    build visualizations to analyze their data using qualitative and \
    quantitative techniques. The data exploration can be done \
    interactively in 3D or programmatically using ParaView's batch \
    processing capabilities.

homepage            https://www.paraview.org/
master_sites        ${homepage}/files/v${branch}/

distname            ParaView-v${version}
use_xz              yes

checksums           sha256  1f3710b77c58a46891808dbe23dc59a1259d9c6b7bb123aaaeaa6ddf2be882ea \
                    rmd160  7608b8dc8f2c1daf8772564b9fbac6634a0b76f2 \
                    size    38739692

# Fix the icon size of the render area. This should be already included
# version > 5.6.0. So when you bump the version, you should be able to remove
# this patch file.
patchfiles          patch-icon-size.diff \
                    patch-vtk_target_export.diff \
                    patch-ogg_dependency.diff \
                    patch-system_ok.diff \
                    patch-case-sensitivity.diff

depends_lib-append  path:share/pkgconfig/eigen3.pc:eigen3 \
                    port:glew \
                    port:double-conversion \
                    port:lz4 \
                    port:xz \
                    port:zlib \
                    port:expat \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:libpng \
                    port:tiff \
                    port:icet \
                    port:jsoncpp \
                    port:pugixml \
                    port:protobuf3-cpp \
                    port:libogg \
                    port:libtheora \
                    port:libxml2 \
                    port:freetype \
                    port:hdf5 \
                    port:netcdf \
                    port:netcdf-cxx \
                    port:gl2ps \
                    port:cgnslib \
                    port:PEGTL \
                    port:qttesting \
                    port:readline


compiler.cxx_standard   2011

qt5.depends_component  sqlite-plugin qttools qtxmlpatterns
qt5.min_version        5.6

cmake.install_rpath

# paraview sets its own optimization flags
configure.optflags

# avoid `Unknown build type "MacPorts".  Allowed values are Debug, Release,`
if {[variant_isset debug]} {
    cmake.build_type    Debug
} else {
    cmake.build_type    Release
}


configure.pre_args-delete \
    -DCMAKE_INSTALL_PREFIX="${cmake.install_prefix}"

configure.post_args-append \
    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF

# ${destroot} needs to be part of MACOSX_APP_INSTALL_PREFIX; see further
# comments below, JJS 12/21/15
configure.args-append \
    -DBUILD_TESTING:BOOL=OFF \
    -DMACOSX_APP_INSTALL_PREFIX=${destroot}${applications_dir} \
    -DBUILD_SHARED_LIBS:BOOL=ON \
    -DPARAVIEW_QT_VERSION=5 \
    -DPARAVIEW_USE_MPI:BOOL=OFF \
    -DQt5_DIR=${qt_dir} \
    -DQT_HELP_GENERATOR=${qt_bins_dir}/qhelpgenerator \
    -DQT_XMLPATTERNS_EXECUTABLE=${qt_bins_dir}/xmlpatterns \
    -DVTK_USE_SYSTEM_LIBRARIES:BOOL=ON \
    -DVTK_USE_SYSTEM_XDMF2:BOOL=OFF

pre-configure {
    configure.args-append -DCMAKE_CXX_COMPILER=${configure.cxx} \
        -DCMAKE_C_COMPILER=${configure.cc}

       if {[mpi_variant_isset]} {
           configure.args-delete -DPARAVIEW_USE_MPI:BOOL=OFF
           configure.args-append  \
               -DPARAVIEW_USE_MPI:BOOL=ON \
               -DMPI_C_COMPILER=${mpi.cc} \
               -DMPI_CXX_COMPILER=${mpi.cxx} \
               -DMPIEXEC=${mpi.exec} \
               -DMPI_Fortran_COMPILER=${mpi.fc} \
               -DVTK_USE_SYSTEM_DIY2:BOOL=OFF

            if {[vercmp ${xcodeversion} 9.0] >= 0 && [vercmp ${xcodeversion} 10.0] < 0} {
               # resolves a problem on Sierra:
               # /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__threading_support:156:1: error: unknown type name 'mach_port_t'
               # mach_port_t __libcpp_thread_get_port();
               # https://github.com/arvidn/libtorrent/issues/2364#issuecomment-336175406
               configure.cxxflags-append \
                   -D_DARWIN_C_SOURCE
            }
       }
}

# Some cmake tests will fail with "error: unrecognized option '-arch'" from gcc 4.6 and earlier.
compilers.choose    fc
mpi.setup           require -gcc44 -gcc45 -gcc46
mpi.enforce_variant icet

if {![mpi_variant_isset]} {
    default_variants +mpich
}

# no compilers.choose cc cxx because:
# gcc6,7 C compiler fails with:
#:info:build In file included from /usr/include/dispatch/dispatch.h:51:0,
#:info:build                  from /System/Library/Frameworks/CoreFoundation.framework/Headers/CFStream.h:15,
#:info:build                  from /System/Library/Frameworks/CoreFoundation.framework/Headers/CFPropertyList.h:13,
#:info:build                  from /System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h:55,
#:info:build                  from /System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h:19,
#:info:build                  from /opt/local/var/macports/build/_Users_dstrubbe_Software_MacPorts_macports-trunk_dports_science_paraview/paraview/work/ParaView-v5.0.1-source/VTK/ThirdParty/freetype/vtkfreetype/src/base/ftmac.c:100,
#:info:build                  from /opt/local/var/macports/build/_Users_dstrubbe_Software_MacPorts_macports-trunk_dports_science_paraview/paraview/work/ParaView-v5.0.1-source/VTK/ThirdParty/freetype/vtkfreetype/src/base/ftbase.c:38:
#:info:build /usr/include/dispatch/object.h:143:15: error: expected identifier or '(' before '^' token
#:info:build  typedef void (^dispatch_block_t)(void);
#:info:build                ^
#:info:build /usr/include/dispatch/object.h:362:3: error: unknown type name 'dispatch_block_t'
#:info:build    dispatch_block_t notification_block);
#:info:build    ^~~~~~~~~~~~~~~~

# I do not understand why, but the default "destroot" makes a mess of things;
# even setting full path of MACOSX_APP_INSTALL_PREFIX above and an empty
# DESTDIR does not work; JJS 12/21/15
destroot {
    system "cd ${build.dir} && make install"
}

# Error using default destroot is:
#Warning: Error determining file type of `/opt/local/var/macports/build/_Users_dstrubbe_Software_MacPorts_macports-trunk_dports_science_paraview/paraview/work/destroot/opt/local/var/macports/build/_Users_dstrubbe_Software_MacPorts_macports-trunk_dports_science_paraview/paraview/work/build/CMakeFiles/__macos_install/bin/paraview.app/Contents/www/lib/widgets/vtkweb-widgets.js': lstat(/opt/local/var/macports/build/_Users_dstrubbe_Software_MacPorts_macports-trunk_dports_science_paraview/paraview/work/destroot/opt/local/var/macports/build/_Users_dstrubbe_Software_MacPorts_macports-trunk_dports_science_paraview/paraview/work/build/CMakeFiles/__macos_install/bin/paraview.app/Contents/www/lib/widgets/vtkweb-widgets.js):no such file or directory
#Warning: A file belonging to the `paraview' port is missing or unreadable. Consider reinstalling it.

# Supported pythons
set python_versions {27 35 36 37}

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]"

    variant python${pyver} conflicts {*}${other_python_versions} description "Add Python ${python_branch} support" "
            depends_lib-append port:py${pyver}-matplotlib
            configure.args-append \
                -DPARAVIEW_ENABLE_PYTHON:BOOL=ON \
                -DPYTHON_EXECUTABLE=${prefix}/bin/python${python_branch} \
                -DPYTHON_INCLUDE_DIR=${frameworks_dir}/Python.framework/Versions/${python_branch}/Headers/ \
                -DPYTHON_LIBRARY=${frameworks_dir}/Python.framework/Versions/${python_branch}/lib/libpython${python_branch}.dylib

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

}

foreach pyver ${python_versions} {
    if {[variant_isset python${pyver}]} {
        notes-append\
            "\n Currently Paraview installs as an app, including the Python modules. To use with MacPorts Python, add ${applications_dir}/paraview.app/Contents/Python/:${applications_dir}/paraview.app/Contents/Libraries/ to PYTHONPATH and ${applications_dir}/paraview.app/Contents/Libraries/ to DYLD_LIBRARY_PATH. Also, simultaneous use of independent vtk libraries, e.g. with Mayavi, may result in segfault."
    }

}

variant ffmpeg description {Add support for FFMPEG} {
   depends_lib-append path:lib/libavcodec.dylib:ffmpeg
   configure.args-append -DPARAVIEW_ENABLE_FFMPEG:BOOL=ON
}

variant xdmf description {Add support for XDMF files I/O} {
    depends_lib-append port:netcdf-cxx \
                       port:xdmf
    configure.args-append -DPARAVIEW_ENABLE_XDMF3:BOOL=ON
}

default_variants-append +ffmpeg

livecheck.type      regex
livecheck.url       https://gitlab.kitware.com/paraview/paraview/tags
livecheck.regex     ParaView (\[0-9.\]+)\$

test.run            yes
pre-test {
    if {![variant_isset testing]} {
        ui_error "You must build with +testing variant to be able to run the tests."
        return -code error "Build with +testing for test phase."
    }
}

variant testing description {Build executables and download data to be able to run tests.} {
    configure.args-replace  -DBUILD_TESTING:BOOL=OFF -DBUILD_TESTING:BOOL=ON
}
