# -*- 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 112789 2013-11-01 14:27:26Z takeshi@macports.org $

PortSystem          1.0
PortGroup           muniversal 1.0

name                fortrancl
version             0.1alpha4
categories          devel parallel science
platforms           darwin
license             LGPL
maintainers         takeshi
description         OpenCL Fortran 90 interface
long_description    \
   FortranCL is an OpenCL interface for Fortran 90. \
   It allows programmers to call the OpenCL parallel programming framework \
   directly from Fortran, so developers can accelerate their Fortran code \
   using graphical processing units (GPU) and other accelerators.
homepage            http://code.google.com/p/fortrancl/
master_sites        googlecode

checksums           rmd160  a709df9ffb914e9e37605dd5a800a58d1567f800 \
                    sha256  55d604a8659edbb0bc6fe9318c1bf04898c93fefcd0bbd03c62ddb778e167581

pre-fetch {
    if {${os.major} < 10} {
        return -code error "OpenCL is not available. Snow Leopard or newer is required."
    }
}

patchfiles          patch-fortrancl.pc.in.diff

configure.cppflags-append   -framework OpenCL
configure.args              --enable-shared

use_parallel_build  no

post-destroot {
    xinstall -d -m 755 ${destroot}${prefix}/share/${name}/examples
    xinstall -m 644 -W ${worksrcpath}/examples \
        devices.f90 sum.cl sum.f90 \
        ${destroot}${prefix}/share/${name}/examples/
}

set gcc_versions {4.3 4.4 4.5 4.6 4.7 4.8 4.9}
set default_fortran_variant +gcc48
set g95_conflicts {}

foreach ver ${gcc_versions} {
    set ver_no_dot [join [split ${ver} "."] ""]

    set variant_line {variant gcc${ver_no_dot} description "build with gfortran from gcc${ver_no_dot}" conflicts g95}

    foreach over ${gcc_versions} {
        if {${ver} == ${over}} {
            continue
        }

        set over_no_dot [join [split ${over} "."] ""]
        append variant_line " conflicts gcc${over_no_dot}"
    }
    append variant_line { {}}

    eval $variant_line

    append g95_conflicts " conflicts gcc${ver_no_dot}"

    if {[variant_isset gcc${ver_no_dot}]} {
        if {${default_fortran_variant} != "+gcc${ver_no_dot}"} {
            set default_fortran_variant ""
        }
    }
}

eval [concat {variant g95 description {build with g95}} $g95_conflicts {{}}]

if {[variant_isset g95]} {
    if {${default_fortran_variant} != "+g95"} {
        set default_fortran_variant ""
    }
}

if {${default_fortran_variant} != ""} {
    default_variants-append "${default_fortran_variant}"
}

foreach ver ${gcc_versions} {
    set ver_no_dot [join [split ${ver} "."] ""]

    if {[variant_isset gcc${ver_no_dot}]} {
        depends_lib-append path:lib/libgcc/libgcc_s.1.dylib:libgcc
        depends_build-append port:gcc${ver_no_dot}

        configure.fc  ${prefix}/bin/gfortran-mp-${ver}
        configure.f77 ${prefix}/bin/gfortran-mp-${ver}
        configure.f90 ${prefix}/bin/gfortran-mp-${ver}
    }
}

if {[variant_isset g95]} {
    depends_lib-append path:lib/libgcc/libgcc_s.1.dylib:libgcc
    depends_build-append port:g95

    configure.fc ${prefix}/bin/g95
    configure.f77 ${prefix}/bin/g95
    configure.f90 ${prefix}/bin/g95
}

# They differ when universal due to gcc multilib being messy non-universal
destroot.delete_la_files yes
