# -*- 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           cmake 1.1
PortGroup           github 1.0
PortGroup           active_variants 1.1

github.setup        KarypisLab METIS e0f1b88b8efcb24ffa0ec55eabb78fbe61e58ae7
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
name                metis
version             20230403
revision            0
categories          math
maintainers         nomaintainer
license             Apache-2

description         Serial Graph Partitioning and Fill-reducing Matrix Ordering
long_description    {*}${description}

checksums           rmd160  a6bf574143d1b06e0338c29cbdd20c16f0cd9ea4 \
                    sha256  40fcaee592b4cff1c39d9800595475f43c0679fc726edc374df503792d5e43e7 \
                    size    4838584

patchfiles          patch-shared-gklib.diff

depends_lib-append  port:gklib

# /opt/local/include/gk_externs.h:19: error: thread-local storage not supported for this target
compiler.thread_local_storage yes

pre-configure {
    reinplace       {s|build/xinclude|${CMAKE_SOURCE_DIR}/include|g} \
                    ${worksrcpath}/CMakeLists.txt
}

configure.args-append \
                    -DGKLIB_PATH=${prefix} \
                    -DGKRAND=ON \
                    -DGKREGEX=ON \
                    -DSHARED=ON

post-destroot {
    # Install documentation
    xinstall -d -m 755 ${destroot}${prefix}/share/doc/${name}
    xinstall -m 644 ${worksrcpath}/manual/manual.pdf \
        ${destroot}${prefix}/share/doc/${name}
}

variant openmp description {Enable openmp support} {
    require_active_variants gklib openmp
    compiler.openmp_version 4.5
    configure.args-append \
                    -DOPENMP=ON
}

variant longindex description {Build with index type being long} { }

variant single description {Build with single precision} { }

post-patch {
    if {[variant_isset longindex]} {
        reinplace   {s|//#define IDXTYPEWIDTH 32|#define IDXTYPEWIDTH 64|g} \
                    ${worksrcpath}/include/metis.h
    } else {
        reinplace   {s|//#define IDXTYPEWIDTH 32|#define IDXTYPEWIDTH 32|g} \
                    ${worksrcpath}/include/metis.h
    }

    if {[variant_isset single]} {
        reinplace   {s|//#define REALTYPEWIDTH 32|#define REALTYPEWIDTH 64|g} \
                    ${worksrcpath}/include/metis.h
    } else {
        reinplace   {s|//#define REALTYPEWIDTH 32|#define REALTYPEWIDTH 32|g} \
                    ${worksrcpath}/include/metis.h
    }
}
