# -*- 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           active_variants 1.1
PortGroup           boost 1.0
PortGroup           python 1.0

name                py-graph-tool
version             3.9
revision            0
epoch               20190711
supported_archs     arm64 x86_64

categories          python science
license             LGPL-3
maintainers         {ieee.org:s.t.smith @essandess} openmaintainer

description         ${python.rootname} is an efficient Python module for \
                    manipulation and statistical analysis of graphs

long_description    {*}${description}. 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  d4c1894ac5921eec35a0c380525eba69cd825f86 \
                    sha256  812ab3575cae13dd6faad96e4d932797a7a71a84480a1eacbb874f2ebafc7c69 \
                    size    15533406

# please ensure that this boost.version matches that specified in cgal6
boost.version       1.91

python.versions     313 314

if {${os.major} <= 12 && ${os.platform} eq "darwin"} {
    version         2.2.26
    revision        1
    master_sites    https://downloads.skewed.de/graph-tool/old/
    checksums       rmd160  cd2e8506522821750d70a97b951254f0a133d218 \
                    sha256  a8ba1f286704f2f54badcb262e6f56055a81f2d66ee1ccedfe31d5c494ee039d \
                    size    29644168
} else {
    if {${name} ne ${subport}} {
        PortGroup buildjobs 1.0

        #------------------------------------------------------------------------------
        # Note: parallel builds observed to work, but consume ~2 GB per clang instance
        #------------------------------------------------------------------------------
        buildjobs.limit_build_jobs yes
        buildjobs.max_ram_fraction 0.5

        compiler.cxx_standard   2023

        # blacklist clang < 22
        compiler.blacklist-append \
                    {clang < 1600} \
                    {macports-clang-[3-9].0} \
                    {macports-clang-1[0-9].0} \
                    {macports-clang-2[0-1]}
        compiler.fallback  \
                    macports-clang-22 clang

        variant openmp description "Enable OpenMP" {
            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:autoconf \
                    port:automake \
                    port:cairomm \
                    port:cctools \
                    port:cgal6 \
                    port:expat \
                    port:libtool \
                    port:py${python.version}-cairo \
                    port:py${python.version}-gobject3 \
                    port:py${python.version}-numpy \
                    port:py${python.version}-scipy

    depends_lib-append \
                    port:py${python.version}-matplotlib

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

    use_configure   yes

    pre-configure {
        system -W ${worksrcpath} ./autogen.sh
    }

    # 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}"

    # https://trac.macports.org/ticket/61583#comment:8
    macosx_deployment_target

    configure.cppflags-append -I${prefix}/include -I${python.include}/..

    configure.args-append \
                    --disable-silent-rules \
                    --exec-prefix=${python.prefix} \
                    --with-boost=[boost::install_area] \
                    --with-cgal=${prefix} \
                    --with-expat=yes \
                    --with-expat-inc=${prefix}/include \
                    --with-expat-lib="-L${prefix}/lib -lexpat"

    # Suppress most Clang compilation warnings, which utterly pollute build log.
    # Note: Likely due to project Makefile, which sets '-Wall' and '-Wextra'.
    # TODO: Try removing this block, when updating to next release.
    if {[string match *clang* ${configure.compiler}]} {
        configure.cxxflags-append \
                    -Wno-delete-non-abstract-non-virtual-dtor \
                    -Wno-deprecated-copy \
                    -Wno-deprecated-copy-with-user-provided-copy \
                    -Wno-missing-field-initializers \
                    -Wno-parentheses \
                    -Wno-unused-parameter \
                    -Wno-error=unknown-warning-option \
                    -Wno-unknown-warning-option
    }

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

    # ccache configuration
    if {[option configure.ccache]} {
        configure.env-append    CCACHE_DIR=${ccache_dir} USE_CCACHE=ON
        build.env-append        CCACHE_DIR=${ccache_dir} USE_CCACHE=ON
        destroot.env-append     CCACHE_DIR=${ccache_dir} USE_CCACHE=ON
    } else {
        # commands to disable ccache
        configure.env-append    CCACHE_DISABLE=1 USE_CCACHE=OFF
        build.env-append        CCACHE_DISABLE=1 USE_CCACHE=OFF
        destroot.env-append     CCACHE_DISABLE=1 USE_CCACHE=OFF
    }

    build.cmd       make
    build.target    all

    destroot.cmd    make
    destroot.destdir \
                    DESTDIR=${destroot}
    destroot.target install

    variant gtk3 description "Enable interactive drawing with gtk3" {
        depends_lib-append \
                    path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
                    path:lib/pkgconfig/librsvg-2.0.pc:librsvg
    }

    post-destroot {
        set doc_dir ${destroot}${prefix}/share/doc
        move ${doc_dir}/graph-tool \
            ${doc_dir}/py${python.version}-graph-tool
    }
} else {
    livecheck.type  regex
    livecheck.url   ${master_sites}
    livecheck.regex ${python.rootname}-(\[0-9.\]+)[string map {"." "\\."} ${extract.suffix}]
}
