# -*- 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 108325 2013-07-19 15:14:15Z ryandesign@macports.org $

PortSystem          1.0

name                eigen3
version             3.1.3
revision            0
license             {{MPL-2} {LGPL-3+}}
categories          math science
maintainers         eborisch \
                    openmaintainer
description         A C++ template library for linear algebra: vectors, \
                    matrices, and related algorithms.
long_description    ${description}
homepage            http://eigen.tuxfamily.org/
platforms           darwin

master_sites        http://bitbucket.org/eigen/eigen/get/
distname            ${version}

# Eigen seems to like to stealth update; keeping this around for the next time
# 3.0.4 was stealth updated; see #32613
#dist_subdir         ${name}/${version}_32613

use_bzip2           yes

checksums \
    rmd160  32f8a3accd026f296dfb913e184efb3a1791f30f \
    sha256  1a443145f321dd47a5ca11f176e7319056198a6124a7577723d92e3c3cd59ece
conflicts           eigen3-devel

livecheck.type      regex
livecheck.url       ${homepage}index.php?title=Main_Page
livecheck.regex     {stable release.*Eigen (\d+(?:\.\d+)*).}

subport eigen3-devel {
    conflicts           eigen3
    version             3.2-rc1
    revision            0
    # rc version seen as older than beta
    epoch               1 
    distname            ${version}
    checksums \
        rmd160  15b955890dfced658768036e86743419140e15e6 \
        sha256  b73fdb6951ddf588a411c42583b19800bdaa6673521a8776e796364ab5c0db44
    livecheck.regex \
        {development release.*Eigen (([\d]|\.|beta|rc|-)+)\.}
}

# Install via CMake by default. Slower than a header-only install, but makes
# other CMake-based builds happy
default_variants    +cmake

# Variants
variant gcc44 description {Use MacPorts' gcc44 compilers for +blas} \
    conflicts gcc45 gcc46 gcc47 gcc48 gcc49 {
    depends_build-append  port:gcc44
    configure.compiler    macports-gcc-4.4
}

variant gcc45 description {Use MacPorts' gcc45 compilers for +blas} \
    conflicts gcc44 gcc46 gcc47 gcc48 gcc49 {
    depends_build-append  port:gcc45
    configure.compiler    macports-gcc-4.5
}

variant gcc46 description {Use MacPorts' gcc46 compilers for +blas} \
    conflicts gcc44 gcc45 gcc47 gcc48 gcc49 {
    depends_build-append  port:gcc46
    configure.compiler    macports-gcc-4.6
}

variant gcc47 description {Use MacPorts' gcc47 compilers for +blas} \
    conflicts gcc44 gcc45 gcc46 gcc48 gcc49 {
    depends_build-append  port:gcc47
    configure.compiler    macports-gcc-4.7
}

variant gcc48 description {Use MacPorts' gcc48 compilers for +blas} \
    conflicts gcc44 gcc45 gcc46 gcc47 gcc49 {
    depends_build-append  port:gcc48
    configure.compiler    macports-gcc-4.8
}

variant gcc49 description {Use MacPorts' gcc49 compilers for +blas} \
    conflicts gcc44 gcc45 gcc46 gcc47 gcc48 {
    depends_build-append  port:gcc49
    configure.compiler    macports-gcc-4.9
}

variant doc \
    description \
    {Place local documentation into <prefix>/share/doc/eigen3/html}\
    requires cmake {
        depends_build-append    port:doxygen \
                                port:texlive-latex \
                                path:bin/dot:graphviz
        build.target-append     doc

}

variant blas description \
    {Build eigen-based blas (libeigen_blas*) : needs +gcc4N} \
    requires cmake {
    if {![variant_isset gcc44] &&
        ![variant_isset gcc45] &&
        ![variant_isset gcc46] &&
        ![variant_isset gcc47] &&
        ![variant_isset gcc48] &&
        ![variant_isset gcc49]} {
        ui_error "eigen3: To use +blas please select +gcc4\[456789\]!!!"
        return -code error
    } else {
        PortGroup               muniversal 1.0
        build.target-append     blas
        universal_variant       yes
        configure.universal_args ""
    }
}

if {([variant_isset gcc44] ||
     [variant_isset gcc45] ||
     [variant_isset gcc46] || 
     [variant_isset gcc47] || 
     [variant_isset gcc48] || 
     [variant_isset gcc49]) && ![variant_isset blas]} {
    ui_warn "eigen3: gcc4* only impacts +blas"
}

variant cmake description \
{CMake-based install: Disabling removes CMake support} {
    # Not using cmake portgroup as it sets -DCMAKE_OSX_ARCHITECTURES
    # in a way we don't seem to be able disable.
    set cmake_share_module_dir ${prefix}/share/cmake/modules

    depends_build-append port:cmake

    configure.cmd       cmake

    configure.pre_args  -DCMAKE_INSTALL_PREFIX=${prefix}

    configure.args      -DCMAKE_VERBOSE_MAKEFILE=ON \
                        -DCMAKE_COLOR_MAKEFILE=ON \
                        -DCMAKE_BUILD_TYPE=Release \
                        -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
                        -DCMAKE_INSTALL_NAME_DIR=${prefix}/lib \
                        -DCMAKE_SYSTEM_PREFIX_PATH=\"${prefix}\;/usr\" \
                        -DCMAKE_MODULE_PATH=${cmake_share_module_dir} \
                        -Wno-dev

    # Out of source build
    configure.args-append   ../eigen_src

    # Enable test cases if we've actually configured
    test.run            yes
    test.target         check
    test.env-append     EIGEN_MAKE_ARGS=-j{build.jobs} \
                        EIGEN_CTEST_ARGS=-j{build.jobs}

    # Enable parallel builds
    build.env-append    EIGEN_MAKE_ARGS=-j{build.jobs}
}

# Conditional phase modifications
if {![variant_isset blas]} {
    # No architecture-dependent files installed; set noarch
    supported_archs     noarch
}

# Are we doing a header-only or cmake (for 'port test' or +doc/+blas) build?
if {![variant_isset cmake]} {
    # We can skip everything and just copy the headers -- fast!
    use_configure       no
    build {}
    destroot {
        set incldir ${destroot}${prefix}/include/${name}
        xinstall -d ${incldir}
        file copy ${workpath}/eigen_src/Eigen ${incldir}
        file copy ${workpath}/eigen_src/unsupported ${incldir}
    }

    # Fail with message if user tries to test
    test.run            yes
    test {
        ui_error "!!! TESTING IS UNSUPPORTED WITHOUT +cmake VARIANT !!!"
    }
}

# Phase modifications
pre-extract {
    file mkdir ${worksrcpath}
}

# Avoid issues when archive changes the top-level directory name
extract.post_args-append -C ${worksrcpath} --strip-components 1

post-patch {
    # Move directory for out-of-source build
    move ${worksrcpath} ${workpath}/eigen_src
    file mkdir ${worksrcpath}
}

# Install licenses and documentation (if +doc)
pre-destroot {
    set build_suffix {}
    if {![info exists universal_archs_to_use]} {
        lappend build_suffix ""
    } else {
        set dash "-"
        foreach arch ${universal_archs_to_use} {
            lappend build_suffix ${dash}${arch}
        }
    }

    foreach sfx ${build_suffix} {
        set docdir ${destroot}${sfx}${prefix}/share/doc/${name}
        xinstall -d ${docdir}
        xinstall -m 644 -W ${workpath}/eigen_src \
            COPYING.GPL COPYING.LGPL ${docdir}

        # Install documentation if requested
        if {[variant_isset doc]} {
            if {[string equal ${sfx} [lindex ${build_suffix} 0]]} {
                # The documentation created is slightly different for each arch
                # which makes muniversal hiccup. Just copy one set of docs
                # into destroot directories.
                file copy ${worksrcpath}${sfx}/doc/html ${docdir}
            }
        }

        if {[variant_isset blas]} {
            set libdir ${destroot}${sfx}${prefix}/lib
            xinstall -d ${libdir}
            eval file copy \
                [glob ${worksrcpath}${sfx}/blas/libeigen_blas*] ${libdir}
        }
    }
}


