# -*- 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           legacysupport 1.1
PortGroup           linear_algebra 1.0

# _strnlen, getline
legacysupport.newest_darwin_requires_legacy 10

# Remember to keep qt4 as a fallback until qt5 is available on old systems.

github.setup        ElmerCSC elmerfem 394d98700a395d5fbcc4bc47a8f186252f41c13f
version             2024.11.29
revision            0
categories          science
license             GPL-2
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
description         Elmer is a tool that can solve a large number of partial differential equations
long_description    {*}${description} making it an ideal tool for multiphysical problems. \
                    It includes models for structural mechanics, fluid dynamics, heat transfer, \
                    electromagnetics and beyond. Elmer can also be used on massively parallel HPC platforms.
homepage            https://www.elmerfem.org
checksums           rmd160  514a904c3e446f8cf641a6d856be5400921b103b \
                    sha256  5bda90a60965bbe12621729548c07d8e046b90ac4c998c8fa3d6d60eb800b2d0 \
                    size    70567034
github.tarball_from archive

compilers.choose    fc f90
compilers.setup     require_fortran
compiler.blacklist-append \
                    {*gcc-[34].*} {macports-gcc-[56]} {clang < 700}

depends_lib-append  port:SuiteSparse_AMD \
                    port:SuiteSparse_CAMD \
                    port:SuiteSparse_CCOLAMD \
                    port:SuiteSparse_CHOLMOD \
                    port:SuiteSparse_COLAMD \
                    port:SuiteSparse_UMFPACK

post-patch {
    reinplace "s,@CC@,${configure.cc}," ${worksrcpath}/CMakeLists.txt
    reinplace "s,@CXX@,${configure.cxx}," ${worksrcpath}/CMakeLists.txt
    reinplace "s,@FC@,${configure.fc}," ${worksrcpath}/CMakeLists.txt
    reinplace "s,CMAKE_SYSTEM_PROCESSOR,CMAKE_OSX_ARCHITECTURES,g" ${worksrcpath}/cpack/ElmerCPack.cmake
}

# See: https://trac.macports.org/ticket/67502
configure.pre_args-delete \
                    -DCMAKE_INSTALL_NAME_DIR="${cmake.install_prefix}/lib"

pre-configure {
    if {[variant_isset openblas]} {
        configure.args-append  \
                    -DBLAS_LIBRARIES=${prefix}/lib/libopenblas.dylib \
                    -DLAPACK_LIBRARIES=${prefix}/lib/libopenblas.dylib
    } elseif {[variant_isset accelerate]} {
        configure.args-append  \
                    -DBLAS_LIBRARIES="-framework Accelerate" \
                    -DLAPACK_LIBRARIES="-framework Accelerate"
    }
}

set qt4_dir         ${prefix}/libexec/qt4

if {${os.platform} eq "darwin" && ${os.major} < 13} {
    PortGroup       qt4 1.0

    configure.args-append \
                    -DQt4_LIBRARIES=${qt4_dir}/lib \
                    -DQWT_INCLUDE_DIR=${qt4_dir}/include/qwt \
                    -DQWT_LIBRARY=${qt4_dir}/lib/libqwt.dylib \
                    -DWITH_QT5=OFF

    depends_lib-append  port:qwt61
} else {
    PortGroup       qt5 1.0

    qt5.depends_component   qtscript qtsvg qttools

    configure.args-append \
                    -DQWT_INCLUDE_DIR=${qt_dir}/lib/qwt.framework/Versions/6/Headers \
                    -DQWT_LIBRARY=${qt_dir}/lib/qwt.framework/qwt \
                    -DWITH_QT5=ON

    depends_lib-append  port:qwt-qt5
}

# TODO: add variant to support MPICH, Hypre and Mumps.
configure.args-append \
                    -DCMAKE_Fortran_COMPILER=${configure.fc} \
                    -DEXTERNAL_UMFPACK=ON \
                    -DGLX_INCLUDE_DIR=${prefix}/include \
                    -DUSE_CONTIGUOUS=OFF \
                    -DWITH_CONTRIB=OFF \
                    -DWITH_ELMERGUI=ON \
                    -DWITH_ELMERGUITESTER=OFF \
                    -DWITH_ELMERGUILOGGER=ON \
                    -DWITH_ElmerIce=OFF \
                    -DWITH_ELMERPOST=OFF \
                    -DWITH_FETI4I=OFF \
                    -DWITH_Hypre=OFF \
                    -DWITH_LUA=OFF \
                    -DWITH_MKL=OFF \
                    -DWITH_MMG=OFF \
                    -DWITH_MPI=OFF \
                    -DWITH_Mumps=OFF \
                    -DWITH_NETCDF=OFF \
                    -DWITH_OCC=OFF \
                    -DWITH_OpenMP=OFF \
                    -DWITH_QWT=ON \
                    -DWITH_Trilinos=OFF \
                    -DWITH_XIOS=OFF \
                    -DWITH_Zoltan=OFF

if {[string match macports-gcc* ${configure.compiler}]} {
    configure.args-replace \
                    -DWITH_OpenMP=OFF -DWITH_OpenMP=ON
}

if {[string match *clang* ${configure.compiler}]} {
    # Load.c: error: call to undeclared function 'var_copy_transpose'; ISO C99 and later do not support implicit function declarations
    configure.cflags-append \
                    -Wno-error=implicit-function-declaration
}

variant ice description "Enable ElmerIce with support for NetCDF and HDF5" {
    depends_lib-append \
                    port:hdf5 \
                    port:netcdf \
                    port:netcdf-fortran
    configure.args-replace \
                    -DWITH_ElmerIce=OFF -DWITH_ElmerIce=ON \
                    -DWITH_NETCDF=OFF -DWITH_NETCDF=ON
}