# -*- 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           github 1.0
PortGroup           mpi 1.0

github.setup        QcmPlab SciFortran 4.10.12
name                scifor
revision            0
categories          science math fortran
license             LGPL-3
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
description         A library of Fortran modules and routines for scientific calculations
long_description    This is a unitary collection of Fortran modules and procedures \
                    for scientific calculations. The library aims to provide a simple \
                    and generic environment for any scientific or mathematic computations. \
                    The project is largely inspired by SciPy for Python \
                    and tries to closely follow its guidelines and naming convention.
homepage            https://QcmPlab.github.io/SciFortran
checksums           rmd160  462959d02e88351192c7615af079424c5db4d457 \
                    sha256  5bec76ce45f0c70bb2a3f36046e5439dfd1672c170758d0198231b1a096afd37 \
                    size    4113717
github.tarball_from archive

# cmake.generator     Ninja
# Avoid Ninja for now due to:
# https://github.com/QcmPlab/SciFortran/issues/1
# The issue seems to affect only non-MPICH build,
# but we rather use a safer option for both.

depends_lib-append  path:lib/libopenblas.dylib:OpenBLAS

patchfiles          patch-prefix.diff \
                    patch-scifor.pc.diff

post-patch {
    reinplace "s,@PREFIX@,${prefix}," ${worksrcpath}/cmake/MainConfig.cmake
    reinplace "s,@PREFIX@,${prefix}," ${worksrcpath}/etc/scifor.pc.in
    reinplace "s,@VERSION@,${version}," ${worksrcpath}/etc/scifor.pc.in
}

compilers.choose    fc f90
mpi.setup           require_fortran -g95

# Otherwise configure may pick a random BLAS implementation, like FlexiBLAS.
pre-configure {
    configure.args-append  \
                    -DBLAS_LIBRARIES=${prefix}/lib/libopenblas.dylib \
                    -DLAPACK_LIBRARIES=${prefix}/lib/libopenblas.dylib
}

configure.args-append \
                    -DWITH_BLAS_LAPACK=OFF

if {[variant_isset mpich] || [variant_isset openmpi]} {
    depends_lib-append \
                    port:scalapack
    configure.args-append \
                    -DUSE_MPI=ON \
                    -DWITH_SCALAPACK=ON
} else {
    configure.args-append \
                    -DUSE_MPI=OFF \
                    -DWITH_SCALAPACK=OFF
}

# Provided install scripts are a mess, avoid them.
destroot {
    copy ${build.dir}/libscifor.a ${destroot}/${prefix}/lib/
    xinstall -d ${destroot}${prefix}/lib/pkgconfig
    move ${worksrcpath}/etc/scifor.pc.in ${destroot}${prefix}/lib/pkgconfig/scifor.pc
    xinstall -d ${destroot}${prefix}/include/${name}
    fs-traverse f ${build.dir}/include {
        if {[file isfile ${f}] && [file extension ${f}] == ".mod"} {
            copy ${f} ${destroot}${prefix}/include/${name}/
        }
    }
}
