# -*- 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

github.setup        LLNL sundials 5.8.0 v
revision            0
checksums           rmd160  7f12c17966a46d98512407dd6c07ea180cc4c9c9 \
                    sha256  9c0dd043c29c60d23c489ca8c436f66e68b80fb90b45e05a2da4347b49c4e212 \
                    size    23973227

categories          math devel
license             BSD
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

conflicts           sundials2

depends_lib-append  port:SuiteSparse_KLU

compiler.cxx_standard \
                    2011

mpi.setup \
                    default \
                    require_fortran

configure.args-append \
                    -DEXAMPLES_ENABLE_C=OFF \
                    -DEXAMPLES_ENABLE_CXX=OFF \
                    -DEXAMPLES_ENABLE_F77=OFF \
                    -DEXAMPLES_ENABLE_F90=OFF \
                    -DENABLE_LAPACK=ON \
                    -DENABLE_KLU=ON \
                    -DKLU_LIBRARY_DIR=${prefix}/lib \
                    -DKLU_INCLUDE_DIR=${prefix}/include \
                    -DMPI_ENABLE=OFF

pre-configure {
    if {[mpi_variant_isset]} {
        configure.args-delete \
                    -DENABLE_MPI=OFF
        configure.args-append \
                    -DENABLE_MPI=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} {
    set example_langs {C CXX F90 F77}
    foreach lang ${example_langs} {
    configure.args-replace \
                    -DEXAMPLES_ENABLE_${lang}=OFF \
                    -DEXAMPLES_ENABLE_${lang}=ON
    }
    configure.args-append \
                    -DEXAMPLES_INSTALL_PATH=${prefix}/share/examples/${name}
    use_parallel_build  no
}

# from CMakeLists.txt:
#    LAPACK is not compatible with INT64_T precision
configure.args-append \
                    -DSUNDIALS_INDEX_SIZE=32

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 {} \\;"
    }
}

# LICENSE file is installed in a bad location
post-destroot {
    xinstall -d -m 0755 ${destroot}${prefix}/share/doc/${name}
    move ${destroot}${prefix}/include/sundials/LICENSE ${destroot}${prefix}/share/doc/${name}
}

livecheck.url       https://computing.llnl.gov/projects/sundials/sundials-software
livecheck.regex     ${name}-(\\d(?:\.\\d+)*)${extract.suffix}
