# -*- 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           compiler_blacklist_versions 1.0
PortGroup           compilers 1.0
PortGroup           github 1.0
PortGroup           linear_algebra 1.0

# Disabled for now. See below.
# PortGroup           mpi 1.0

github.setup        mfem mfem 4.6 v
revision            1
categories          science
license             BSD
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
description         Lightweight, general, scalable C++ library for finite element methods
long_description    MFEM is a modular parallel C++ library for finite element methods. \
                    Its goal is to enable high-performance scalable finite element discretization research \
                    and application development on a wide variety of platforms, ranging from laptops to supercomputers.
homepage            https://mfem.org
checksums           rmd160  0f44a9437f693a990ac871c6cdb7490242c8c48e \
                    sha256  250bb6aa0fd5f6a6002c072d357656241ed38acfc750e43e87d8c36a8f8a4b4f \
                    size    3577416
github.tarball_from archive

depends_lib-append  path:lib/pkgconfig/gnutls.pc:gnutls \
                    port:hdf5 \
                    port:mpfr \
                    port:netcdf \
                    port:zlib

# https://github.com/mfem/mfem/issues/3931
patchfiles-append   patch-cstdint.diff

# libocca module fails to build: https://github.com/mfem/mfem/issues/3697
# MPICH build works, however needs -fpermissive, and many tests fail. Avoid for now.
# Several dependencies require MPICH, therefore those are disabled.
# Notice that disabling MPICH does not work properly: it is not used as a compiler,
# but the build still expects to link against it. Eh. Testing needs miniapps and examples,
# as those compose tests. Please do not move this port to compile with MPICH by default
# unless it is well-tested, including PPC.
configure.args-append \
                    -DBUILD_SHARED_LIBS=ON \
                    -DGinkgo_DIR=${prefix}/lib \
                    -DHDF5_DIR=${prefix} \
                    -DHDF5_LIBRARIES=${prefix}/lib \
                    -DHDF5_INCLUDE_DIRS=${prefix}/lib \
                    -DMFEM_ENABLE_EXAMPLES=ON \
                    -DMFEM_ENABLE_MINIAPPS=ON \
                    -DMFEM_ENABLE_TESTING=ON \
                    -DMFEM_USE_CUDA=OFF \
                    -DMFEM_USE_GINKGO=OFF \
                    -DMFEM_USE_GNUTLS=ON \
                    -DMFEM_USE_HIOP=OFF \
                    -DMFEM_USE_HIP=OFF \
                    -DMFEM_USE_LAPACK=ON \
                    -DMFEM_USE_LIBUNWIND=OFF \
                    -DMFEM_USE_MEMALLOC=ON \
                    -DMFEM_USE_METIS=OFF \
                    -DMFEM_USE_METIS_5=OFF \
                    -DMFEM_USE_MPFR=ON \
                    -DMFEM_USE_MPI=OFF \
                    -DMFEM_USE_MUMPS=OFF \
                    -DMFEM_USE_NETCDF=ON \
                    -DMFEM_USE_OCCA=OFF \
                    -DMFEM_USE_OPENMP=ON \
                    -DMFEM_USE_RAJA=OFF \
                    -DMFEM_USE_SIMD=OFF \
                    -DMFEM_USE_STRUMPACK=OFF \
                    -DMFEM_USE_SUITESPARSE=OFF \
                    -DMFEM_USE_SUNDIALS=OFF \
                    -DMFEM_USE_SUPERLU=OFF \
                    -DMFEM_USE_ZLIB=ON \
                    -DVERBOSE=ON

# mfem as such can be built with C++11, but using some of its dependencies requires C++14:
compiler.cxx_standard 2014
compiler.openmp_version 4.0
compilers.setup     require_fortran

if {[string match *clang* ${configure.compiler}]} {
    configure.ldflags-append \
                    -L${prefix}/lib/libomp \
                    -lomp
}

variant ginkgo description "Enable Ginkgo" {
    # Ginkgo wants MPICH, we do not.
    depends_lib-append \
                    port:ginkgo
    configure.args-replace \
                    -DMFEM_USE_GINKGO=OFF -DMFEM_USE_GINKGO=ON
}

variant hiop description "Enable HIOP" {
    # Well, HIOP expects mpi.h to be available.
    depends_lib-append \
                    port:hiop
    configure.args-replace \
                    -DMFEM_USE_HIOP=OFF -DMFEM_USE_HIOP=ON
}

variant raja description "Enable Raja" {
    # For w/e reason, Raja wants Clang 16 now, and that Clang fails to build with MPICH.
    # Hide Raja into a variant.
    depends_lib-append \
                    port:raja
    configure.args-replace \
                    -DMFEM_USE_RAJA=OFF -DMFEM_USE_RAJA=ON
}

# Move examples to a sane location:
post-destroot {
    xinstall -d ${destroot}${prefix}/share/${name}
    move ${destroot}${prefix}/examples ${destroot}${prefix}/share/${name}/examples
}

test.run            yes
test.cmd-prepend    DYLD_LIBRARY_PATH=${cmake.build_dir}:${cmake.build_dir}/miniapps/common
