# -*- 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 90820 2012-03-15 03:13:40Z jmr@macports.org $

PortSystem			1.0
PortGroup           muniversal 1.0

name                qrupdate
version             1.1.1
set branch          [join [lrange [split ${version} .] 0 1] .]
revision            3
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           md5     7782c7ee6c234e56a72d6b86c1856fa0 \
                    sha1    8fbaba202b0d4bf80852b2dc6c8d1d4b90b816d4 \
                    rmd160  fd63306abe91adcd5d47e408d9cd4af3e1b32b0c

depends_lib         port:atlas

use_configure       no
build.target        lib solib

build.args          PREFIX=${prefix} BLAS="-L${prefix}/lib" LAPACK="-L${prefix}/lib -llapack"
destroot.args       PREFIX=${prefix} BLAS="-L${prefix}/lib" LAPACK="-L${prefix}/lib -llapack"
test.args           PREFIX=${prefix} BLAS="-L${prefix}/lib" LAPACK="-L${prefix}/lib -llapack"

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 {build with Accelerate framework instead of atlas} {
    depends_lib-delete      port:atlas
    build.args          PREFIX=${prefix} BLAS="-framework Accelerate" LAPACK="-framework Accelerate"
    destroot.args       PREFIX=${prefix} BLAS="-framework Accelerate" LAPACK="-framework Accelerate"
    test.args           PREFIX=${prefix} BLAS="-framework Accelerate" LAPACK="-framework Accelerate"
}

variant accelerate requires no_atlas description {compatibility variant} {}

variant g95 conflicts gcc43 gcc44 gcc45 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 gcc43 conflicts g95 gcc44 gcc45 universal description {build with gcc43 fortran} {
    depends_lib-append    port:gcc43
    post-patch {
        reinplace "s|gfortran|gfortran-mp-4.3|" ${worksrcpath}/Makeconf
    }
}

variant gcc44 conflicts g95 gcc43 gcc45 description {build with gcc44 fortran} {
    depends_lib-append    port:gcc44
    post-patch {
        reinplace "s|gfortran|gfortran-mp-4.4|" ${worksrcpath}/Makeconf
    }
}

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

if {![variant_isset gcc43] && ![variant_isset gcc44] \
        && ![variant_isset g95]} {
    default_variants +gcc45
}

if {[variant_isset g95]} {
    default_variants +no_atlas
}

test.run yes
