# -*- 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 2.7.0 v
name                sundials2
revision            7
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
dist_subdir         sundials

checksums           rmd160  7bbbff19828c8235422497e11891d07fe02e4021 \
                    sha256  d39fcac7175d701398e4eb209f7e92a5b30a78358d4a0c0fcc23db23c11ba104 \
                    size    12258093

#------------------------------------------------------------------------------
# Path-Related Variables - START
#------------------------------------------------------------------------------

# The "install name," meaning, the subdirectory name for this port.
# Should correspond to the major version.
set port_install_name \
                    ${subport}

# Define all of our base paths up-front
set port_install_prefix \
                    ${prefix}/libexec/${port_install_name}
set port_install_cmake \
                    ${port_install_prefix}/cmake
set port_install_include \
                    ${port_install_prefix}/include
set port_install_lib \
                    ${port_install_prefix}/lib
set port_install_doc \
                    ${prefix}/share/doc/${port_install_name}
set port_install_examples \
                    ${prefix}/share/examples/${port_install_name}

# Populate CMake options currently available
cmake.install_prefix \
                    ${port_install_prefix}
cmake_share_module_dir \
                    ${port_install_cmake}
cmake.install_rpath \
                    ${port_install_lib}

configure.args-append \
                    -DCMAKE_INSTALL_LIBEXECDIR=${port_install_prefix} \
                    -DCMAKE_INSTALL_BINDIR=${port_install_prefix}/bin \
                    -DCMAKE_INSTALL_SBINDIR=${port_install_prefix}/sbin \
                    -DCMAKE_INSTALL_SHAREDIR=${port_install_prefix}/share \
                    -DCMAKE_INSTALL_INCLUDEDIR=${port_install_include} \
                    -DCMAKE_INSTALL_LIBDIR=${port_install_lib} \
                    -DCMAKE_INSTALL_NAME_DIR=${port_install_lib}

#------------------------------------------------------------------------------
# Path-Related Variables - END
#------------------------------------------------------------------------------

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

mpi.setup \
                    default \
                    require_fortran

depends_lib-append \
                    port:SuiteSparse_KLU

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

pre-configure {
    if {[mpi_variant_isset]} {
        configure.args-replace \
                    -DMPI_ENABLE=OFF \
                    -DMPI_ENABLE=ON
        configure.args-append \
                    -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 environment 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=${port_install_examples}
}

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."
    }
}

set port_doc_dir ${destroot}${port_install_doc}

variant doc description {install pdf documentation} {
    post-destroot {
        xinstall -d ${port_doc_dir}
        foreach f [glob ${worksrcpath}/doc/*] {
            xinstall -m 0644 ${f} ${port_doc_dir}/
        }
    }
}

# 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+)*)}
