# $Id: Portfile 90820 2012-03-15 03:13:40Z jmr@macports.org $

PortSystem          1.0

name                eigen3
version             3.0.5
license             {{GPL-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.N was stealth updated; see #TTTTT
#dist_subdir         ${name}/${version}_TTTTT

use_bzip2           yes

checksums           md5     43070952464a5bf21694e082e7fb8fce \
                    sha1    f2c726897860b4ebc63f74f6097d315aacee1eaf \
                    rmd160  33cc3544e3d5151c8670c87420e74b7f8bd1cdce

universal_variant   yes
configure.universal_args  ""

# 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} conflicts gcc45 gcc46 {
  depends_build-append  port:gcc44
  configure.compiler  macports-gcc-4.4
}

variant gcc45 description {Use MacPorts' gcc45 compilers} conflicts gcc44 gcc46 {
  depends_build-append  port:gcc45
  configure.compiler  macports-gcc-4.5
}

variant gcc46 description {Use MacPorts' gcc46 compilers} conflicts gcc44 gcc45 {
  depends_build-append  port:gcc46
  configure.compiler  macports-gcc-4.6
}

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

variant blas description {Build eigen-based blas (libeigen_blas*)} requires cmake {
  if {![variant_isset gcc44] && ![variant_isset gcc45] && ![variant_isset gcc46]} {
    ui_error "eigen3: ##### +blas needs either +gcc44 or +gcc45 or +gcc46 enabled #####"
    return -code error
  } else {
    build.target-append blas
  }
}

variant cmake description {CMake-based install: Disabling removes CMake support} {
  PortGroup           muniversal 1.0
  # 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
# 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}
    }
  }
}

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

