# -*- 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

PortSystem          1.0
PortGroup           compilers 1.0
PortGroup           cmake 1.1
PortGroup           gitlab 1.0

name                eigen3
license             {MPL-2 LGPL-2.1+} LGPL-3+
categories          math science
maintainers         {eborisch @eborisch} {@barracuda156 gmail.com:vital.had} \
                    openmaintainer
description         A C++ template library for linear algebra: vectors, \
                    matrices, and related algorithms.
long_description    {*}${description}

depends_build-append    path:bin/pkg-config:pkgconfig

if {${subport} eq ${name}} {
    gitlab.setup        libeigen eigen 3.4.0
    revision            1
    conflicts           eigen3-devel

    checksums           rmd160  4b70962c6c1454d0909ed04776c812a78ffdce03 \
                        sha256  b4c198460eba6f28d34894e3a5710998818515104d6e74e5cc331ce31e46e626 \
                        size    2143091

    # Backport of: https://gitlab.com/libeigen/eigen/-/commit/4d05765345e7e4a984d600039f797e2fede924f3
    patchfiles-append   patch-ppc.diff

    compiler.cxx_standard \
                        2011
}

subport eigen3-devel {
    gitlab.setup        libeigen eigen 8ad4344c
    # For when there is no current development version (other than rolling snapshot)
    version             3.4-tracking-20241122
    revision            0
    epoch               3
    gitlab.livecheck.branch master

    conflicts           eigen3

    long_description    {*}${description} This (-devel) version tracks \
                        development of the current (3.4) branch.

    checksums           rmd160  c7891fe0d6b45c82bb938a0fa596803148456abb \
                        sha256  2550f496965bc1409f421f3fb20e4b3adecb374712cdb3e57f173e1f4f23a97c \
                        size    2278889

    # Eigen's source code specifically checks the version of Apple Clang.
    compiler.blacklist-append \
                        {clang < 900}
    compiler.cxx_standard \
                        2014
    compilers.setup     require_fortran
}

# Exclude pre-release versions
gitlab.livecheck.regex {([0-9.]+)}

homepage            http://eigen.tuxfamily.org/

variant doc description \
    {Place local documentation into <prefix>/share/doc/eigen3/html} {
        depends_build-append    path:bin/doxygen: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 +gfortran} {
        PortGroup               muniversal 1.0

        build.target-append     blas
        universal_variant       yes
        configure.universal_args ""
}

configure.env-append    PKG_CONFIG_LIBDIR=${prefix}/lib/pkgconfig

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
    platforms           any
    compilers.setup
}

### Extra phases

# 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/eigen3
        xinstall -d ${docdir}

        foreach item [glob -directory ${worksrcpath} COPYING.*] {
            xinstall -m 644 -W ${worksrcpath} ${item} ${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 ${cmake.build_dir}${sfx}/doc/html ${docdir}
            }
        }

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

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

    # Install FindEigen3.cmake file, if it is provided.
    xinstall -d ${destroot}${cmake_share_module_dir}
    if {[file exists ${worksrcpath}/cmake/FindEigen3.cmake]} {
        xinstall -m 444 ${worksrcpath}/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.
"
