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

gitlab.instance         https://gitlab.nektar.info
gitlab.setup            nektar nektar 5.6.0 v

boost.version           1.76

name                    nektarpp
revision                0

categories              science
license                 MIT
homepage                http://www.nektar.info/
maintainers             {xyloid.org:dave @mdave} openmaintainer
description             spectral/hp element framework
long_description        Nektar++ is a spectral/hp element framework which aims to be easy \
                        to use and support a wide range of features, including 1D, 2D and \
                        3D element support, hybrid shaped elements and continuous and \
                        discontinuous implementations. As well as a modern C++ API, it \
                        ships with a range of solvers including CFD, advection-diffusion \
                        equations and cardiac electrophysiology.

# Additional dependencies for Tetgen and Triangle
master_sites            ${master_sites}:main \
                        ${homepage}thirdparty/:thirdparty

set main_distfile       ${distfiles}
distfiles               ${main_distfile}:main

checksums               nektar-5.6.0.tar.bz2 \
                        rmd160  508cebc13a9848180f6a7e49828478cbf1537450 \
                        sha256  797d4b72d0fdd80efd8b8f5e2f4a1205d7f8e3fc2c6bce6dc6fd62a688ec9d50 \
                        size    64546056 \

# Only the full Nektar++ source should be extracted - CMake will extract any
# third-party dependencies.
extract.only            ${main_distfile}

patchfiles              no-homebrew.patch

compiler.cxx_standard   2011
cmake.build_type        Release

mpi.setup

depends_lib-append      port:arpack \
                        port:fftw-3 \
                        port:opencascade \
                        port:scotch \
                        port:tetgen \
                        port:tinyxml \
                        port:triangle \
                        port:vtk \
                        port:zlib

# Don't build demos and tests by default.
configure.args-append   -DNEKTAR_BUILD_DEMOS=OFF \
                        -DNEKTAR_ERROR_ON_WARNINGS=OFF \
                        -DNEKTAR_BUILD_TESTS=OFF \
                        -DNEKTAR_BUILD_UNIT_TESTS=OFF \
                        -DNEKTAR_SOLVER_DIFFUSION=OFF \
                        -DNEKTAR_SOLVER_DUMMY=OFF \
                        -DNEKTAR_SOLVER_ELASTICITY=OFF \
                        -DNEKTAR_SOLVER_MMF=OFF \
                        -DNEKTAR_USE_ARPACK=ON \
                        -DNEKTAR_USE_FFTW=ON \
                        -DNEKTAR_USE_MESHGEN=ON \
                        -DNEKTAR_USE_SCOTCH=ON \
                        -DNEKTAR_USE_VTK=ON

variant petsc description {
    Enable PETSc support for linear algebra solvers.
} {
    depends_lib-append port:petsc

    configure.args-append \
                        -DNEKTAR_USE_PETSC=ON
}

if {[mpi_variant_isset]} {
    distfiles-append    gsmpi-1.2.1_1.tar.bz2:thirdparty
    checksums-append    gsmpi-1.2.1_1.tar.bz2 \
                        rmd160 3a1ad71c552ea9d62bebabd4ed528531f9c0eec6 \
                        sha256 c9bc966c92f45e81f4e9d1887dd7c59808e7a4ef9ec41e1e5f1e6c5c01745211 \
                        size 39737

    depends_lib-append port:hdf5

    require_active_variants hdf5 [mpi_variant_name]
    require_active_variants scotch [mpi_variant_name]

    configure.args-append \
                        -DNEKTAR_USE_HDF5=ON \
                        -DNEKTAR_USE_MPI=ON
}

variant oce description {
    use Community Edition version of OpenCASCADE
} {
    depends_lib-replace port:opencascade port:oce
}

# NOTE: v5.2.0 looking for OCE before OpenCASCADE, disable when it doesn't required
if {![variant_isset oce]} {
    post-patch {
        reinplace "/FIND_PACKAGE\(OCE /s/^/#/g" ${worksrcpath}/cmake/FindOCC.cmake
    }
}

post-extract {
    # Copy additional third-party sources into correct position.
    file mkdir ${worksrcpath}/ThirdParty
    foreach distfile ${distfiles} {
        set distfile [lindex [split ${distfile} :] 0]
        if {${distfile} ne ${main_distfile}} {
            copy ${distpath}/${distfile} ${worksrcpath}/ThirdParty/
        }
    }
}

set pythons_suffixes    {37 38 39 310 311 312}

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 range ${s} 1 2]
    set pe ${prefix}/bin/python${v}
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    variant ${p} description "Build the Python ${v} bindings" conflicts {*}${c} "
        depends_lib-append      port:${p} \
                                port:py${s}-numpy \
                                port:boost[boost::version_nodot]-numpy

        configure.args-append   -DNEKTAR_BUILD_PYTHON=ON \
                                -DNEKTAR_PYTHON3_STATUS=ON \
                                -DNEKTAR_USE_PYTHON3=ON \
                                -DPYTHON_EXECUTABLE=${pe}

        # Install Python libraries using setup.py.
        post-destroot {
            system -W ${build.dir}/python \"${pe} setup.py install --root=${destroot}\"
        }

        require_active_variants boost       ${p}
        require_active_variants boost-numpy ${p}
        require_active_variants vtk         ${p}
    "
}
