# -*- 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                   conflicts_build 1.0
PortGroup                   linear_algebra 1.0
PortGroup                   makefile 1.0
PortGroup                   mpi 1.0
PortGroup                   muniversal 1.0

name                        mumps
version                     5.7.3
revision                    1
categories                  math
license                     CeCILL-C
maintainers                 {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
description                 MUltifrontal Massively Parallel sparse direct Solver
long_description            MUMPS: a parallel sparse direct solver

depends_lib                 port:scotch \
                            port:scalapack \
                            port:metis \
                            port:parmetis

homepage                    https://mumps-solver.org
master_sites                ${homepage}

distname                    MUMPS_${version}

checksums                   rmd160  a11df7b1399aa5ffbc74723c662ef01c0702c2d6 \
                            sha256  84a47f7c4231b9efdf4d4f631a2cae2bdd9adeaabc088261d15af040143ed112 \
                            size    4542883

conflicts_build-append      ${name}

mpi.setup                   require \
                            require_fortran

mpi.enforce_variant         scalapack

linalg.setup                blas_only

patchfiles-append           patch-shared.diff \
                            patch-Makefile.inc.diff

use_parallel_build          no

build.target                all allshared
build.args-append           LIBEXT_SHARED=".dylib"

configure.optflags          -O3
configure.cflags-append     -fPIC
configure.fflags-append     -fPIC

# Xcode clang of 10.7 fails with error: invalid instruction mnemonic 'cvtsi2ssl'
compiler.blacklist-append \
                    {clang < 500} *gcc-4.* macports-gcc-5 macports-gcc-6

# when using a non-GCC compiler, avoid:
#     Undefined symbols for architecture i386:
#       "___divmoddi4", referenced from:
#           _dmumps_facto_root_ in libdmumps.a(dfac_root_parallel.o)
#     ld: symbol(s) not found for architecture i386
# see https://gitlab.haskell.org/ghc/ghc/issues/15094
lappend merger_configure_fcflags(i386) \
                            -fno-expensive-optimizations
if {(!${universal_possible} || ![variant_isset universal]) && ${build_arch} eq "i386"} {
    configure.fflags-append -fno-expensive-optimizations
}

pre-build {
    build.env-append        MACPORTS_BLAS=${linalglib}
}

if {!${universal_possible} || ![variant_isset universal]} {
    # find correct scotch Fortran header
    if {[file exists ${prefix}/include/${build_arch}]} {
        configure.cppflags-prepend -I${prefix}/include/${build_arch}
    }
} else {
    # find correct scotch Fortran header
    foreach arch ${configure.universal_archs} {
        if {[file exists ${prefix}/include/${arch}]} {
            lappend merger_configure_cppflags(${arch}) -I${prefix}/include/${arch}
        }
    }
}

post-extract {
    file copy ${worksrcpath}/Make.inc/Makefile.inc.generic ${worksrcpath}/Makefile.inc
}

destroot {
    xinstall -m 644 {*}[glob ${worksrcpath}/include/*.h] ${destroot}${prefix}/include
    xinstall -m 644 {*}[glob ${worksrcpath}/lib/*.a] ${destroot}${prefix}/lib
    xinstall -m 644 {*}[glob ${worksrcpath}/lib/*.dylib] ${destroot}${prefix}/lib
}

livecheck.url               ${homepage}/index.php?page=dwnld
livecheck.regex             {>(\d+(?:\.\d+)*)<}

test.run                    yes
test.target                 all
test.args-append            LPORD=../lib/libpord.a

post-test {
    set runcmd "${mpi.exec} -np ${build.jobs}"

    system -W ${worksrcpath}/examples "${runcmd} ./ssimpletest < input_simpletest_real"
    system -W ${worksrcpath}/examples "${runcmd} ./dsimpletest < input_simpletest_real"
    system -W ${worksrcpath}/examples "${runcmd} ./csimpletest < input_simpletest_cmplx"
    system -W ${worksrcpath}/examples "${runcmd} ./zsimpletest < input_simpletest_cmplx"
    system -W ${worksrcpath}/examples "${runcmd} ./c_example"
}

# see https://listes.ens-lyon.fr/sympa/arc/mumps-users/2020-05/msg00000.html
compilers.allow_arguments_mismatch  yes
