# -*- 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
# $Id: Portfile 106281 2013-05-21 05:58:23Z blair@macports.org $

PortSystem			1.0
PortGroup           muniversal 1.0

name                qrupdate
version             1.1.2
revision            2
# 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}

checksums           rmd160  9d23b2d13278c335f6208ebb6775df4b4049785c \
                    sha256  e2a1c711dc8ebc418e21195833814cb2f84b878b90a2774365f0166402308e08

use_configure       no
build.target        lib solib

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

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
}

# Fortran compilers can not cross-compile
if { ${os.arch}=="i386" } {
    set universal_archs_supported "i386 x86_64"
} else {
    set universal_archs_supported "ppc ppc64"
}

post-configure {
    if { ! [variant_isset universal] } {
        if { ${build_arch} == "x86_64" || ${build_arch} == "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}=="x86_64" || ${arch}=="ppc64" } {
                reinplace "s|^FFLAGS=|FFLAGS=-m64 |"  ${worksrcpath}-${arch}/Makeconf
            } else {
                reinplace "s|^FFLAGS=|FFLAGS=-m32 |"  ${worksrcpath}-${arch}/Makeconf
            }
        }
    }
}


variant no_atlas description {Legacy compatibility variant} {}

variant atlas description {build with atlas instead of Accelerate framework} {
    depends_lib-append      port:atlas
    build.args-append       BLAS="-L${prefix}/lib -ltatlas" LAPACK="-L${prefix}/lib -llapack"
    destroot.args-append    BLAS="-L${prefix}/lib -ltatlas" LAPACK="-L${prefix}/lib -llapack"
    test.args-append        BLAS="-L${prefix}/lib -ltatlas" LAPACK="-L${prefix}/lib -llapack"
}
if {![variant_isset atlas]} {
    build.args-append       BLAS="-framework Accelerate" LAPACK="-framework Accelerate"
    destroot.args-append    BLAS="-framework Accelerate" LAPACK="-framework Accelerate"
    test.args-append        BLAS="-framework Accelerate" LAPACK="-framework Accelerate"
}

if {[variant_isset g95] || [variant_isset no_atlas]} {
    default_variants -atlas
} else {
    default_variants +atlas
}


variant g95 conflicts gcc45 gcc46 gcc47 gcc48 universal description {build with g95} {
    depends_build-append    port:g95
    post-patch {
        reinplace "s|Makeconf|Makeconf.g95|" ${worksrcpath}/Makefile
        reinplace "s|Makeconf|Makeconf.g95|" ${worksrcpath}/src/Makefile
    }
}

variant gcc45 conflicts g95 gcc46 gcc47 gcc48 universal description {build with gcc45 fortran} {
    depends_lib-append    port:gcc45
    post-patch {
        reinplace "s|gfortran|gfortran-mp-4.5|" ${worksrcpath}/Makeconf
    }
}

variant gcc46 conflicts g95 gcc45 gcc47 gcc48 description {build with gcc46 fortran} {
    depends_lib-append    port:gcc46
    post-patch {
        reinplace "s|gfortran|gfortran-mp-4.6|" ${worksrcpath}/Makeconf
    }
}

variant gcc47 conflicts g95 gcc45 gcc46 gcc48 description {build with gcc47 fortran} {
    depends_lib-append    port:gcc47
    post-patch {
        reinplace "s|gfortran|gfortran-mp-4.7|" ${worksrcpath}/Makeconf
    }
}

variant gcc48 conflicts g95 gcc45 gcc46 gcc47 description {build with gcc48 fortran} {
    depends_lib-append    port:gcc48
    post-patch {
        reinplace "s|gfortran|gfortran-mp-4.8|" ${worksrcpath}/Makeconf
    }
}

if {![variant_isset gcc45] && ![variant_isset gcc46] \
        && ![variant_isset g95] && ![variant_isset gcc48]} {
	default_variants +gcc47
}


test.run yes
