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

github.setup        igraph igraph 0.10.16
revision            0
github.tarball_from releases

categories          math science devel
license             GPL-2+
maintainers         {snc @nerdling} {gmail.com:szhorvat @szhorvat} openmaintainer
description         Network analysis and graph theory library for C.
long_description    igraph is a C library for network analysis and graph theory, \
                    with an emphasis on efficiency, portability and ease of use.
homepage            https://igraph.org

depends_lib         port:arpack \
                    port:glpk \
                    port:gmp \
                    port:libxml2 \
                    port:plfit

checksums           rmd160  0559b8a40da9c448b36898da8bc1a89be7fa270a \
                    sha256  15a1540a8d270232c9aa99adeeffb7787bea96289d6bef6646ec9c91a9a93992 \
                    size    4444095

# Testing notes:
#
# There is a bug in macOS 13.x to 14.0 that will cause the safelocale test to fail.
# See https://stackoverflow.com/q/76133503/695132 and https://github.com/igraph/igraph/issues/2340
# This is fixed in macOS 14.1.
test.run            yes
test.target         check

compiler.c_standard 1999
compiler.cxx_standard \
                    2011

linalg.setup        noveclibfort

# Build options for igraph:
#  - Do not use ccache to build this port unless MacPorts tells it to.
#  - Build a shared library.
#  - Enable link-time optimization when available.
#  - Set features and the use of external libraries explicitly---do not leave this to auto-detection.
configure.args-append   -DUSE_CCACHE=OFF \
                        -DBUILD_SHARED_LIBS=ON \
                        -DIGRAPH_ENABLE_LTO=AUTO \
                        -DIGRAPH_GLPK_SUPPORT=ON \
                        -DIGRAPH_GRAPHML_SUPPORT=ON \
                        -DIGRAPH_USE_INTERNAL_ARPACK=OFF \
                        -DIGRAPH_USE_INTERNAL_BLAS=OFF \
                        -DIGRAPH_USE_INTERNAL_GLPK=OFF \
                        -DIGRAPH_USE_INTERNAL_GMP=OFF \
                        -DIGRAPH_USE_INTERNAL_LAPACK=OFF \
                        -DIGRAPH_USE_INTERNAL_PLFIT=OFF \
                        -DIGRAPH_OPENMP_SUPPORT=OFF \
                        -DIGRAPH_WARNINGS_AS_ERRORS=OFF

pre-configure {
    # Link to chosen BLAS/LAPACK
    configure.args-append   ${cmake_linalglib}
}

variant tls description {Build thread-safe version} {
    compiler.thread_local_storage   yes
    configure.args-append           -DIGRAPH_ENABLE_TLS=ON
}

# The openmp variant is not enabled by default because in MacPorts,
# the compilers that support OpenMP won't support LTO (without workarounds).
# Overall, igraph performance benefits more from LTO than OpenMP.
variant openmp description {Enable OpenMP support} {
    # This compiler gives an invalid warning about uninitialized variables.
    # It is unclear if it also miscompiles the code.
    compiler.blacklist-append       {macports-clang-9.0}

    configure.args-replace          -DIGRAPH_OPENMP_SUPPORT=OFF -DIGRAPH_OPENMP_SUPPORT=ON
}

# Do not enable TLS on OS X 10.6, as it does not support it.
if {${os.platform} eq "darwin" && ${os.major} >= 11} {
    default_variants +tls
}

# See https://github.com/macports/macports-base/commit/7c91604891fa0d071b8d598490c4dc2edb8e0031
if {[variant_isset openmp]} {
    compiler.openmp_version 2.0
    if {[info exists compiler.log_verbose_output]} {
        compiler.log_verbose_output no
    } else {
        configure.cppflags-append -fopenmp
        configure.ldflags-append  -fopenmp
    }
}

post-destroot {
    set docdir ${prefix}/share/doc/${name}
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 {*}[glob ${worksrcpath}/doc/html/*] ${destroot}${docdir}
}
