# -*- 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
name                sundials5
revision            2

categories          math devel
license             BSD
maintainers         {mascguy @mascguy} openmaintainer

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

set ver_major       [lindex [split ${version} .] 0]
github.tarball_from releases
dist_subdir         sundials

checksums           rmd160  22866440a727c63ee187ac855852eda6c6b9c840 \
                    sha256  d4ed403351f72434d347df592da6c91a69452071860525385b3339c824e8a213 \
                    size    24012815

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

compiler.cxx_standard \
                    2011

mpi.setup \
                    default \
                    require_fortran

depends_lib-append  \
                    port:SuiteSparse_KLU

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

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

post-extract {
    # fix file perms; tarball contents deny group and world read
    system "find ${worksrcpath} -type d -print0 | xargs -0 chmod a+rx"
    system "find ${worksrcpath} -type f -print0 | xargs -0 chmod a+r"
}

pre-configure {
    if {[mpi_variant_isset]} {
        configure.args-replace \
                    -DENABLE_MPI=OFF \
                    -DENABLE_MPI=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=/

if { [vercmp ${xcodeversion} >= 15.0] || [vercmp ${xcodecltversion} >= 15.0] } {
    # On macOS13 and newer ensure the 'legacy' linker is used as GCC currently has problems
    # with the new default linker in Xcode 15. See e.g.
    # https://developer.apple.com/documentation/xcode-release-notes/xcode-15-release-notes#Linking
    # https://discussions.apple.com/thread/255137447
    # https://developer.apple.com/forums/thread/737707
    # https://github.com/Homebrew/homebrew-core/issues/145991
    configure.env-append \
                    LDFLAGS=-ld_classic
}

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=${port_install_examples}
    use_parallel_build  no
}

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}/
        }
    }
}

# LICENSE file is installed in a bad location
post-destroot {
    xinstall -d ${port_doc_dir}
    move ${destroot}${port_install_include}/sundials/LICENSE ${port_doc_dir}/
}

livecheck.url       https://api.github.com/repos/${github.author}/${github.project}/tags?per_page=200
livecheck.curloptions
livecheck.regex     "\"name\": \"v(${ver_major}\.\[0-9\.\]+)\","
