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

github.setup                DrTimothyAldenDavis SuiteSparse 5.13.0 v
# subports have independent revisions
revision                    0
epoch                       20200517
categories                  math science
maintainers                 {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
license                     none

description                 Sparse matrix routines

long_description            SuiteSparse is a single archive that contains all packages \
                            authored by Tim Davis.

homepage                    https://people.engr.tamu.edu/davis/suitesparse.html

checksums                   rmd160  c418ad3400283e59ea528fe1d80c1dd9422bb998 \
                            sha256  8eb2e49e14c0a1a497ed147bc13608e791200e190306ffa50f00be5150fc5306 \
                            size    65007739

use_parallel_build          no

patchfiles-append           patch-shared_lib.diff \
                            patch-Mongoose.diff \
                            patch-KLU-Include-klu_version.h.diff

configure.optflags          -O3

# see https://github.com/macports/macports-ports/pull/2292#issuecomment-408526461
configure.cppflags-replace  -I${prefix}/include \
                            -isystem${prefix}/include

subport SuiteSparse_config {
    version                 5.13.0
    revision                0
    # from the README.txt:
    #    "[n]o licensing restrictions apply"
    license                 Permissive
}

subport SuiteSparse_GraphBLAS {
    version                 7.2.0
    revision                1
    license                 Apache-2
    long_description-append ${subport}: graph algorithms in the language of linear algebra.
    compiler.c_standard     2011
    compiler.openmp_version 4.0
}

subport SuiteSparse_Mongoose {
    version                 2.0.4
    revision                1
    depends_lib-append      port:SuiteSparse_config
    license                 GPL-3
    long_description-append ${subport}: graph partitioning.
    compiler.cxx_standard   2011
}

subport SuiteSparse_AMD {
    version                 2.4.6
    revision                0
    depends_lib-append      port:SuiteSparse_config
    license                 BSD
    long_description-append ${subport}: approximate minimum degree ordering.
}

subport SuiteSparse_BTF {
    version                 1.2.6
    revision                0
    depends_lib-append      port:SuiteSparse_config
    license                 LGPL-2.1+
    long_description-append ${subport}: permutation to block triangular form.
}

subport SuiteSparse_CAMD {
    version                 2.4.6
    revision                0
    depends_lib-append      port:SuiteSparse_config
    license                 BSD
    long_description-append ${subport}: constrained approximate minimum degree ordering.
}

subport SuiteSparse_CCOLAMD {
    version                 2.9.6
    revision                0
    depends_lib-append      port:SuiteSparse_config
    license                 BSD
    long_description-append ${subport}: constrained column approximate minimum degree ordering.
}

subport SuiteSparse_COLAMD {
    version                 2.9.6
    revision                0
    depends_lib-append      port:SuiteSparse_config
    license                 BSD
    long_description-append ${subport}: column approximate minimum degree ordering.
}

subport SuiteSparse_CHOLMOD {
    PortGroup               linear_algebra 1.0

    version                 3.0.14
    revision                2
    depends_lib-append      port:SuiteSparse_config port:SuiteSparse_AMD port:SuiteSparse_CAMD port:SuiteSparse_COLAMD port:SuiteSparse_CCOLAMD
    depends_lib-append      port:metis
    build.args-append       MY_METIS_LIB="-L${prefix}/lib -lmetis" \
                            MY_METIS_INC="${prefix}/include"
    pre-build {
        build.args-append   BLAS="-L${prefix}/lib ${linalglib}" \
                            LAPACK="-L${prefix}/lib ${linalglib}"
    }
    license                 LGPL-2.1+
    long_description-append ${subport}: sparse Cholesky factorization.
    linalg.setup            noveclibfort
}

subport SuiteSparse_CSparse {
    # see Doc/ChangeLog
    version                 3.2.0
    revision                0
    license                 LGPL-2.1+
    long_description-append ${subport}: a concise sparse matrix package.
}

subport SuiteSparse_CXSparse {
    version                 3.2.0
    revision                0
    depends_lib-append      port:SuiteSparse_config
    license                 LGPL-2.1+
    long_description-append ${subport}: CSparse Extended.
}

subport SuiteSparse_LDL {
    version                 2.2.6
    revision                0
    depends_lib-append      port:SuiteSparse_config
    license                 LGPL-2.1+
    long_description-append ${subport}: a very concise LDL' factorization package.
}

subport SuiteSparse_KLU {
    version                 1.3.8
    revision                0
    depends_lib-append      port:SuiteSparse_config port:SuiteSparse_AMD port:SuiteSparse_BTF port:SuiteSparse_COLAMD
    license                 LGPL-2.1+
    long_description-append ${subport}: sparse LU factorization, primarily for circuit simulation.
}

subport SuiteSparse_UMFPACK {
    PortGroup               linear_algebra 1.0

    version                 5.7.9
    revision                1
    depends_lib-append      port:SuiteSparse_config port:SuiteSparse_AMD port:SuiteSparse_CHOLMOD
    pre-build {
        build.args-append   BLAS="-L${prefix}/lib ${linalglib}" \
                            LAPACK="-L${prefix}/lib ${linalglib}"
    }
    license                 GPL-2+
    long_description-append ${subport}: sparse LU factorization.
    linalg.setup            noveclibfort
}

subport SuiteSparse_RBio {
    version                 2.2.6
    revision                0
    depends_lib-append      port:SuiteSparse_config
    license                 GPL-2+
    long_description-append ${subport}: read/write sparse matrices in Rutherford/Boeing format.
}

subport SuiteSparse_SPQR {
    PortGroup               linear_algebra 1.0

    version                 2.1.0
    revision                1
    depends_lib-append      port:SuiteSparse_config port:SuiteSparse_AMD port:SuiteSparse_CHOLMOD port:SuiteSparse_COLAMD
    pre-build {
        build.args-append   BLAS="-L${prefix}/lib ${linalglib}" \
                            LAPACK="-L${prefix}/lib ${linalglib}"
    }
    license                 GPL-2+
    long_description-append ${subport}: sparse QR factorization.
    linalg.setup            noveclibfort
}

subport SuiteSparse_SLIP_LU {
    # This version is incorrect. The real version number is 1.0.2.
    # Remember to fix this when the port is next updated.
    version                 2.0.9
    revision                0
    depends_lib-append      port:SuiteSparse_config port:SuiteSparse_AMD port:SuiteSparse_COLAMD \
                            port:gmp \
                            port:mpfr
    license                 {LGPL-3+ GPL-2+}
    long_description-append ${subport}: sparse left-looking integer-preserving LU factorization.
}

if {${subport} eq ${name}} {
    depends_lib-append      port:SuiteSparse_config \
                            port:SuiteSparse_GraphBLAS \
                            port:SuiteSparse_Mongoose \
                            port:SuiteSparse_AMD \
                            port:SuiteSparse_BTF \
                            port:SuiteSparse_CAMD \
                            port:SuiteSparse_CCOLAMD \
                            port:SuiteSparse_COLAMD \
                            port:SuiteSparse_CHOLMOD \
                            port:SuiteSparse_CSparse \
                            port:SuiteSparse_CXSparse \
                            port:SuiteSparse_LDL \
                            port:SuiteSparse_KLU \
                            port:SuiteSparse_UMFPACK \
                            port:SuiteSparse_RBio \
                            port:SuiteSparse_SPQR \
                            port:SuiteSparse_SLIP_LU

    master_sites
    distfiles
    extract.rename          no
    patchfiles
    use_configure           no
    supported_archs         noarch
    installs_libs           no
    universal_variant       no
    build {}
    # create a dummy file so the port can be successfully activated
    destroot {
        xinstall -d -m 0755 ${destroot}${prefix}/share/doc/${subport}
        set    docfile      [open ${destroot}${prefix}/share/doc/${subport}/README.txt "w"]
        puts  ${docfile}    "Meta-port for ${name}"
        puts  ${docfile}    "${long_description}"
        close ${docfile}
    }
} else {
    PortGroup               compilers 1.0

    compilers.choose        cc cpp cxx
    compilers.setup

    if {${subport} eq "SuiteSparse_config"} {
        set SuiteSparse_name    ${subport}
    } else {
        set SuiteSparse_name    [join [lrange [split ${subport} _] 1 end] _]
    }

    if {${subport} eq "SuiteSparse_GraphBLAS" || ${subport} eq "SuiteSparse_Mongoose"} {
        PortGroup           cmake 1.1

        cmake.source_dir    ${worksrcpath}/${SuiteSparse_name}
    } else {
        PortGroup           makefile 1.0

        if {${subport} eq "SuiteSparse_config"} {
            build.dir       ${worksrcpath}/${SuiteSparse_name}
        } else {
            build.dir       ${worksrcpath}/${SuiteSparse_name}/Lib
        }
        destroot.dir        ${build.dir}

        configure.universal_cflags
        configure.cc_archflags
        configure.universal_cxxflags
        configure.cxx_archflags
        pre-build {
            build.args-append   TARGET_ARCH="[get_canonical_archflags ld]"
        }

        # INSTALL variable has non-standard meaning
        configure.install

        build.args-append       MACPORTS_PREFIX="${prefix}" \
                                OPTIMIZATION="${configure.optflags}" \
                                AUTOCC=no
        # avoid duplicate values
        configure.optflags

        # see https://trac.macports.org/ticket/60492
        configure.cxx-append    {*}${configure.cxxflags}

        if {${subport} eq "SuiteSparse_CSparse"} {
            build.target    all
        } else {
            build.target    default
        }

        destroot {
            proc check_copy {directory pattern prefix_directory} {
                global destroot prefix
                set files   [glob -nocomplain -directory ${directory} ${pattern}]
                if {${files} ne ""} {
                    xinstall -d -m 0755 ${destroot}${prefix}/${prefix_directory}
                    copy    {*}${files} ${destroot}${prefix}/${prefix_directory}
                }
            }
            check_copy ${worksrcpath}/lib                                     *   lib
            check_copy ${destroot.dir}                                        *.a lib
            check_copy ${worksrcpath}/include                                 *   include
            check_copy ${worksrcpath}/share/doc/suitesparse-${github.version} *   share/doc/suitesparse
        }

        livecheck.type      none
    }
}
