# -*- coding: utf-8; mode: tcl; tab-width: 4; c-basic-offset: 4 -*-

PortSystem          1.0
PortGroup           python 1.0
PortGroup           active_variants 1.1

set realname        graph-tool
name                py-${realname}
version             2.33
revision            1
epoch               20190711
categories          python science
platforms           darwin
license             LGPL-3
maintainers         nomaintainer
description         Efficient python graph module
long_description    graph-tool is an efficient python module for manipulation \
                    and statistical analysis of graphs. The internal data \
                    structures and most algorithms are implemented in C++ with \
                    the Boost Graph Library.
homepage            https://graph-tool.skewed.de
master_sites        https://downloads.skewed.de/graph-tool/
use_bzip2           yes
checksums           rmd160  5eecb61bacb6a26d211f9c3c43c2b3641cb9f9bf \
                    sha256  f07025160a8cb376551508c6d8aa5fd05a146c67c4706ea4635d2766aa5c9fcb \
                    size    15153528
distname            ${realname}-${version}

python.versions     27 37 38 39
python.default_version 27

if {${os.major} <= 12 && ${os.platform} eq "darwin"} {
    version         2.2.26
    revision        0
    master_sites    https://downloads.skewed.de/graph-tool/old/
    distname        ${realname}-${version}
    checksums       md5     317b29de0d3ef715fdc9281e078cfb17 \
                    sha1    108be4cf6212eb6886f172ea03813187f73e4c3c \
                    rmd160  cd2e8506522821750d70a97b951254f0a133d218
} else {
    if {${name} ne ${subport}} {
        compiler.cxx_standard   2017

        variant openmp description "Enable OpenMP" {
            # TODO: verify OpenMP version required
            compiler.openmp_version 2.5
            configure.args-append --enable-openmp
        }

        default_variants +openmp
    }
}


if {${name} ne ${subport}} {
    universal_variant  no
    depends_build-append port:pkgconfig
    depends_lib-append port:boost \
                       port:cairomm \
                       port:cgal5 \
                       port:expat \
                       path:bin/dot:graphviz \
                       port:py${python.version}-numpy \
                       port:py${python.version}-scipy \
                       port:py${python.version}-gobject3 \
                       port:py${python.version}-cairo
    use_configure      yes
    # parallel build starts swapping with 8GB of RAM.
    #use_parallel_build no

    # graph-tool relies on Boost.Python, so make sure it is installed.
    require_active_variants boost python${python.version}

    # PYTHON_EXTRA_LDFLAGS is set to work around incorrect detection of
    # link flags by configure
    configure.env-append PYTHON=${python.bin} \
                         PYTHON_VERSION=${python.branch} \
                         PYTHON_CPPFLAGS=-I${python.include} \
                         "PYTHON_LDFLAGS=-L${python.libdir}/.. -lpython${python.branch}" \
                         "PYTHON_EXTRA_LDFLAGS=-L${python.libdir}/.. -lpython${python.branch}"
    # With python2.7 PYTHON_EXTRA_LIBS is determined to be
    # '-u _PyMac_Error Python.framework/Versions/2.7/Python'. Not clear whether
    # python2.7 or py-graph-tool's configure script is to blame, but we can easily
    # work around this:
    if {${python.version} eq "27"} {
        configure.env-append "PYTHON_EXTRA_LIBS=-u _PyMac_Error"
    }
    # Something similar is happening with python3.6
    if {${python.version} eq "36"} {
        configure.env-append "PYTHON_EXTRA_LIBS=-Wl,-stack_size,1000000 -framework CoreFoundation ${python.lib}"
    }
    configure.cppflags-append -I${prefix}/include -I${python.include}/..
    configure.ldflags-append -L${prefix}/lib
    configure.args-append \
        --with-boost=${prefix} \
        --exec-prefix=${python.prefix} \
        --with-expat=yes \
        --with-expat-inc=${prefix}/include \
        --with-expat-lib="-L${prefix}/lib -lexpat"

    # Clang uses the old libstc++ from gcc 4.2 before OS X 10.9. Boost doesn't
    # include some of the tr1 headers in libstdc++ and defines its own tr1
    # classes. This causes conflicts with sparsehash which insists on using
    # the old tr1 headers.
    if {[string match *gcc* ${configure.compiler}] ||
        ${os.major} >= 13 && ${os.platform} eq "darwin"} {
        depends_lib-append port:sparsehash
    } else {
        configure.args-append --disable-sparsehash
    }
    build.cmd          make
    build.target       all
    destroot.cmd       make
    destroot.destdir   DESTDIR=${destroot}

    variant gtk3 description "Enable interactive drawing with gtk3" {
        depends_lib-append port:gtk3 \
                           port:librsvg
    }

    post-destroot {
        file rename ${destroot}${prefix}/share/doc/graph-tool \
            ${destroot}${prefix}/share/doc/py${python.version}-graph-tool
    }
}

if {${name} eq ${subport}} {
    livecheck.type      regex
    livecheck.url       $homepage
    livecheck.regex     Download version (\[0-9.\]+)
} else {
    livecheck.type      none
}
