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

name                sundials2
github.setup        LLNL sundials 2.7.0 v
revision            4
categories          math devel
license             BSD
platforms           darwin
maintainers         nomaintainer

description         SUite of Nonlinear and DIfferential/ALgebraic equation Solvers

long_description    SUNDIALS consists of the following five solvers: CVODE\
                    solves initial value problems for ordinary differential\
                    equation (ODE) systems.  CVODES solves ODE systems and\
                    includes sensitivity analysis capabilities (forward and\
                    adjoint).  IDA solves initial value problems for\
                    differential-algebraic equation (DAE) systems.  IDAS solves\
                    DAE systems and includes sensitivity analysis capabilities\
                    (forward and adjoint).  KINSOL solves nonlinear algebraic\
                    systems.

homepage            https://computing.llnl.gov/projects/sundials

github.tarball_from releases

checksums           rmd160  7bbbff19828c8235422497e11891d07fe02e4021 \
                    sha256  d39fcac7175d701398e4eb209f7e92a5b30a78358d4a0c0fcc23db23c11ba104 \
                    size    12258093

conflicts           sundials

dist_subdir         sundials
distfiles           sundials-${version}${extract.suffix}
worksrcdir          sundials-${version}

# use default cmake behavior to find Lapack
patchfiles-append   patch-find_lapack.diff

depends_lib-append  port:SuiteSparse_KLU

mpi.setup \
        require_fortran \
        default

configure.args-append \
                    -DEXAMPLES_ENABLE=OFF \
                    -DCXX_ENABLE=ON \
                    -DF90_ENABLE=ON \
                    -DLAPACK_ENABLE=ON \
                    -DFCMIX_ENABLE=ON \
                    -DKLU_ENABLE=ON \
                    -DKLU_LIBRARY_DIR=${prefix}/lib \
                    -DKLU_INCLUDE_DIR=${prefix}/include \
                    -DMPI_ENABLE=OFF

pre-configure {
    if {[mpi_variant_isset]} {
        configure.args-delete -DMPI_ENABLE=OFF
        configure.args-append -DMPI_ENABLE=ON \
                              -DMPI_MPICC=${prefix}/bin/${mpi.cc} \
                              -DMPI_MPICXX=${prefix}/bin/${mpi.cxx} \
                              -DMPI_MPIF77=${prefix}/bin/${mpi.f77} \
                              -DMPI_MPIF90=${prefix}/bin/${mpi.f90} \
                              -DMPI_RUN_COMMAND=${prefix}/bin/${mpi.exec}
    }
}

# without this, previously installed sundials headers are found first
# this can cause problem when upgrading
# see https://cmake.org/cmake/help/latest/variable/CMAKE_INCLUDE_DIRECTORIES_BEFORE.html
configure.args-append \
    -DCMAKE_INCLUDE_DIRECTORIES_BEFORE=ON

# cmake files (e.g. config/SundialsFortran.cmake) create CMakeLists.txt files
#    to test for functionality (e.g. a Fortran compiler).
# CMAKE_OSX_SYSROOT is not passed to the functionality test.
# Because CMAKE_OSX_DEPLOYMENT_TARGET is set as an environmental variable,
#    -isysroot is added to the compiler flags.
# Only Apple Clang compilers support tbd files.
# See https://trac.macports.org/ticket/53151
# See https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_SYSROOT.html
configure.env-append SDKROOT=/

variant examples description {Install Examples} {
    configure.args-replace \
        -DEXAMPLES_ENABLE=OFF \
        -DEXAMPLES_ENABLE=ON

    configure.args-append \
        -DEXAMPLES_INSTALL_PATH=${prefix}/share/examples/${name}
}

variant accelerate conflicts atlas openblas description {Use Apple Accelerate Libraries} {
    configure.args-append -DBLA_VENDOR=Apple
}

variant atlas conflicts accelerate openblas description {Use MacPorts ATLAS Libraries} {
    depends_lib-append port:atlas

    configure.args-append   \
        -DBLA_VENDOR=ATLAS

    # FindBLAS.cmake and FindLAPACK.cmake do not seem to find MacPorts Atlas properly
    configure.args-append          \
        -DBLAS_LIBRARIES=satlas    \
        -DLAPACK_LIBRARIES=satlas
}

variant openblas conflicts atlas accelerate description {Use MacPorts OpenBLAS Libraries} {
    depends_lib-append path:lib/libopenblas.dylib:OpenBLAS
    require_active_variants path:lib/libopenblas.dylib:OpenBLAS lapack

    configure.args-append  -DBLA_VENDOR=OpenBLAS
}

if {![variant_isset accelerate] && ![variant_isset atlas] && ![variant_isset openblas]} {
    default_variants-append +accelerate
}
pre-fetch {
    if {![variant_isset accelerate] && ![variant_isset openblas] && ![variant_isset atlas] } {
        ui_error "You must select either the +accelerate, +atlas, or +openblas variant for linear algebra."
        return -code error "No linear-algebra variant selected."
    }
}

variant doc description {install pdf documentation} {
    post-destroot {
        xinstall -d -m 0755 ${destroot}${prefix}/share/doc/${name}
        copy {*}[glob ${worksrcpath}/doc/*] ${destroot}${prefix}/share/doc/${name}
        system "find ${destroot}${prefix}/share/doc -type f -exec chmod 644 {} \\;"
    }
}

# this version is too old to work with the default livecheck from github PG
livecheck.url       https://computing.llnl.gov/projects/sundials/sundials-software
livecheck.regex     {/v(2(?:\.\d+)*)}
