# -*- 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 119179 2014-04-19 00:11:10Z ryandesign@macports.org $

PortSystem          1.0
PortGroup           compilers 1.0
PortGroup           cmake 1.0

name                eigen3
version             3.2.1
revision            2
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  0140af248d9b76bf8c7faef6ddf38762627a0084 \
    sha256  66e6f2ede78d0ef650ae95aa570f548c095690b011000f9163c32618736c928a
conflicts           eigen3-devel

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

patchfiles          unsupported.patch

subport eigen3-devel {
    conflicts           eigen3
    # No current development version (other than rolling snapshot)
    version             3.2.1
    revision            2
    # rc version seen as older than beta
    epoch               1 
    distname            ${version}
    checksums \
        rmd160  0140af248d9b76bf8c7faef6ddf38762627a0084 \
        sha256  66e6f2ede78d0ef650ae95aa570f548c095690b011000f9163c32618736c928a
    livecheck.regex \
        {development release.*Eigen (([\d]|\.|beta|rc|-)+)\.}
}

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

variant blas description \
    {Build eigen's blas (libeigen_blas*) : needs +gccNN, +g95, or +gfortan} {
        PortGroup               muniversal 1.0
        build.target-append     blas
        universal_variant       yes
        configure.universal_args ""
}

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

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}

### END CMAKE SUPPORT

# Setup compilers support / requirements

if {[variant_isset blas]} {
    compilers.choose    cc cxx cpp fc f77 f90
    compilers.setup     require_fortran
} elseif {[variant_isset doc]} {
    compilers.choose    cc cxx cpp
    compilers.setup     
} else { 
    # No architecture-dependent files installed; set noarch
    supported_archs     noarch
    compilers.setup
}

### Extra phases

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
    copy ${filespath}/CMakeLists-eigenvalues.txt \
        ${workpath}/eigen_src/unsupported/Eigen/src/Eigenvalues/CMakeLists.txt
    file mkdir ${worksrcpath}
}

# Install licenses and (if +doc) documentation
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}
        }
    }
}

post-destroot {
    # Grab extra items install doesn't out of unsupported
    foreach n {README.txt bench doc test} {
        file copy ${workpath}/eigen_src/unsupported/${n} \
            ${destroot}${prefix}/include/${name}/unsupported
    }

    # Install FindEigen3.cmake file
    xinstall -d ${destroot}${cmake_share_module_dir}
    xinstall -m 444 ${workpath}/eigen_src/cmake/FindEigen3.cmake \
        ${destroot}${cmake_share_module_dir}
}

notes "
This product includes software developed by the University of Chicago, as\
Operator of Argonne National Laboratory.
"
