# -*- 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.9.1
revision                    0
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-append          port:metis \
                            port:parmetis \
                            port:scalapack \
                            port:scotch

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

distname                    MUMPS_${version}

checksums                   rmd160  86d387b8cf0c80f9a4f36531f65a45b587946df8 \
                            sha256  659c9b57646b5a003ac618baa1faf9dd2044e46c732b3daaccbc7158003e1b46 \
                            size    4983937

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

# https://github.com/hpddm/hpddm/issues/141
# Since then upstream added a macro, however a) the define is not actually set,
# and b) tools_common remained broken. Here we fix that.
if {${configure.build_arch} in [list arm i386 ppc]} {
    patchfiles-append       patch-hack-to-unbreak.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}
}

# Find correct scotch Fortran header
if {!${universal_possible} || ![variant_isset universal]} {
    if {[file exists ${prefix}/include/${build_arch}]} {
        configure.cppflags-prepend -I${prefix}/include/${build_arch}
    }
} else {
    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
