# -*- 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           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.9 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  f817a6c651c7b1216ada291d4017b30be308781a \
                    sha256  ea3ac13e182c09f05b414b03a9bef7a4da99d45d67ee409112b8f11058447a7c \
                    size    4511318
github.tarball_from archive

# Avoid -mcpu=native on powerpc, see: https://github.com/iains/gcc-14-branch/issues/24
patchfiles-append   patch-optflags.diff

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

# 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_GSLIB=OFF \
                    -DMFEM_USE_HDF5=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_UMPIRE=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 2017
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
}

# FIXME: https://github.com/mfem/mfem/issues/4791
variant occa description "Enable libocca support" {
    depends_lib-append \
                    port:libocca
    configure.args-replace \
                    -DMFEM_USE_OCCA=OFF -DMFEM_USE_OCCA=ON
}

# FIXME: currently gslib conflicts with ghostscript.
# Either rename its library, or install it into a custom subprefix, or disable it here.
variant gs description "Enable gslib support" {
    depends_lib-append \
                    port:gslib
    configure.args-replace \
                    -DMFEM_USE_GSLIB=OFF -DMFEM_USE_GSLIB=ON
}

default_variants-append +gs

# 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
