# -*- 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           muniversal 1.0
PortGroup           compilers 1.0
PortGroup           linear_algebra 1.0

compilers.choose    fc
compilers.setup     require_fortran

name                qrupdate
version             1.1.2
revision            7
checksums           rmd160 9d23b2d13278c335f6208ebb6775df4b4049785c \
                    sha256 e2a1c711dc8ebc418e21195833814cb2f84b878b90a2774365f0166402308e08

# upstream follows a weird folder naming policy, we need last two digits of version separated by a dot
set branch          [join [lrange [split ${version} .] 1 2] .]
categories          math
license             GPL-3+
maintainers         nomaintainer
description         library for fast updates of QR and Cholesky decompositions
long_description    a Fortran library for fast updates of QR and Cholesky decompositions
homepage            http://sourceforge.net/projects/${name}/
platforms           darwin
master_sites        sourceforge:project/qrupdate/qrupdate/${branch}

use_configure       no
build.target        lib solib

build.args          PREFIX=${prefix} LAPACK=""
destroot.args       PREFIX=${prefix} LAPACK=""
test.args           PREFIX=${prefix} LAPACK=""

patchfiles          patch-Makefile.diff

post-patch {
    # Mac install program does not support -D flag.
    reinplace "s|install -D|install|"  ${worksrcpath}/src/Makefile

    # Running ranlib on static libraries make universal builds a little more complicated.
    reinplace "s|ar -cr |libtool -o |"  ${worksrcpath}/src/Makefile

    if {[variant_isset g95]} {
        reinplace "s|Makeconf|Makeconf.g95|" ${worksrcpath}/Makefile
        reinplace "s|Makeconf|Makeconf.g95|" ${worksrcpath}/src/Makefile
        reinplace "s|Makeconf|Makeconf.g95|" ${worksrcpath}/test/Makefile
    } else {
        reinplace "s|gfortran|${configure.fc}|" ${worksrcpath}/Makeconf
    }
}

# Fortran compilers cannot cross-compile
if {${os.arch} eq "i386"} {
    set universal_archs_supported "i386 x86_64"
} elseif {${os.arch} eq "arm64"} {
    set universal_archs_supported "arm64"
} else {
    set universal_archs_supported "ppc ppc64"
}

post-configure {
    if {![variant_exists universal] || ![variant_isset universal]} {
        if {${build_arch} eq "arm64" || ${build_arch} eq "x86_64" || ${build_arch} eq "ppc64"} {
            reinplace "s|^FFLAGS=|FFLAGS=-m64 |"  ${worksrcpath}/Makeconf
        } else {
            reinplace "s|^FFLAGS=|FFLAGS=-m32 |"  ${worksrcpath}/Makeconf
        }
    } else {
        foreach arch ${universal_archs_to_use} {
            if {${arch} eq "arm64" || ${arch} eq "x86_64" || ${arch} eq "ppc64"} {
                reinplace "s|^FFLAGS=|FFLAGS=-m64 |"  ${worksrcpath}-${arch}/Makeconf
            } else {
                reinplace "s|^FFLAGS=|FFLAGS=-m32 |"  ${worksrcpath}-${arch}/Makeconf
            }
        }
    }

    build.args-append       BLAS="${linalglib}"
    destroot.args-append    BLAS="${linalglib}"
    test.args-append        BLAS="${linalglib}"
}

test.run    yes
