# -*- 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
PortGroup           legacysupport 1.1

name                zmq
categories          devel sysutils net
license             LGPL-3+

maintainers         {stromnov @stromnov} {michaelld @michaelld} openmaintainer

description         0MQ (ZeroMQ) lightweight messaging kernel

long_description    \
    The ${description} is a library which extends \
    the standard socket interfaces with features traditionally provided \
    by specialised messaging middleware products. 0MQ sockets provide \
    an abstraction of asynchronous message queues, multiple messaging patterns, \
    message filtering (subscriptions), seamless access to multiple transport \
    protocols and more.

if {${name} eq ${subport}} {
    PortGroup cmake 1.1

    github.setup    zeromq libzmq 4.3.5 v
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    checksums       rmd160  b26b2c2368d76e93b67607e0e8703714f8c0c4eb \
                    sha256  5c7ec9f212ff04e196c126464661764d3f054107e0ceac3b030f748106d53424 \
                    size    875612
    revision        1

    conflicts zmq-devel zmq22 zmq3

    configure.args-append \
                    -DWITH_PERF_TOOL=OFF \
                    -DZMQ_BUILD_TESTS=OFF \
                    -DENABLE_CPACK=OFF \
                    -DWITH_LIBBSD=OFF \
                    -DWITH_LIBSODIUM=OFF \
                    -DWITH_NSS=OFF \
                    -DWITH_TLS=OFF \
                    -DENABLE_INTRINSICS=OFF \
                    -DZMQ_BUILD_FRAMEWORK=OFF

    patch.pre_args-replace  -p0 -p1
    patchfiles-append \
        patch-cxx11.release.diff \
        patch-c11.release.diff \
        patch-fix-docs-dir.release.diff \
        patch-fix-no-librt-APPLE.release.diff

    # https://github.com/zeromq/libzmq/issues/4595
    if {${os.platform} eq "darwin" && ${os.major} < 16} {
        patchfiles-append \
            patch-clock.hpp.diff
    }

    # overload the github livecheck regex to look for versions that
    # are just numbers and '.', no letters (e.g., "3.7.3_rc2").

    github.livecheck.regex  {([0-9.]+)}

}

# Legacy subport (no longer supported)
subport zmq3 {
    github.setup zeromq zeromq3-x 3.2.5 v
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    revision    2

    checksums   rmd160 959e1403e4c8e36603711c9cf3bb5ff5cd5727aa \
                sha256 3ba8fbdee4845e26f25f3d1d43492bc182077338f4e857fa50a06e1b21bade7f

    conflicts   zmq zmq-devel zmq22

    use_autoconf    yes
    autoconf.cmd    ./autogen.sh

    depends_build-append port:automake port:autoconf

    configure.args-append --disable-silent-rules

    # https://trac.macports.org/ticket/60771
    patchfiles-append patch-zmq3-src_select.cpp.diff
}

# Legacy subport (as a dependency for p5-zeromq)
subport zmq22 {
    github.setup zeromq zeromq2-x 2.2.0 v
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    revision    3
    checksums   rmd160 5bf0cfe29e26348dc4c201764d099a8117c43735 \
                sha256 e021b62c6be211b5a1ac4b9e038068e0a8caaf81c22ee050183b25e991825e05

    conflicts   zmq zmq-devel zmq3

    use_autoconf    yes
    autoconf.cmd    ./autogen.sh

    depends_build-append port:automake port:autoconf

    configure.args-append --disable-silent-rules

    # https://trac.macports.org/ticket/60771
    patchfiles-append patch-zmq22-src_select.cpp.diff
}

subport zmq-devel {
    PortGroup cmake 1.1

    github.setup    zeromq libzmq 64db7d28fea695132834f6d2c5949cfea2f22d01
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    version         20240906-[string range ${github.version} 0 7]
    checksums       rmd160  df863572089bb89e4894ea978c458f9c5f015d33 \
                    sha256  08196cf55fa59d15e4413095936e0be4d2925fba9c8beb0fb7b938ddefaccc5a \
                    size    880003
    revision        0

    conflicts zmq zmq22 zmq3

    configure.checks.implicit_function_declaration.whitelist-append \
        pthread_set_name_np pthread_setaffinity_np

    configure.args-append \
                    -DWITH_PERF_TOOL=OFF \
                    -DZMQ_BUILD_TESTS=OFF \
                    -DENABLE_CPACK=OFF \
                    -DWITH_LIBBSD=OFF \
                    -DWITH_LIBSODIUM=OFF \
                    -DWITH_NSS=OFF \
                    -DWITH_TLS=OFF \
                    -DENABLE_INTRINSICS=OFF \
                    -DZMQ_BUILD_FRAMEWORK=OFF

    patch.pre_args-replace  -p0 -p1
    patchfiles-append \
        patch-fix-docs-dir.devel.diff

    # https://github.com/zeromq/libzmq/issues/4595
    if {${os.platform} eq "darwin" && ${os.major} < 16} {
        patchfiles-append \
            patch-clock.hpp.diff
    }

    compiler.c_standard     2011
    compiler.cxx_standard   2011
}

# override github information

homepage            https://zeromq.org/

# common directory for storing downloaded tarballs

dist_subdir         zmq

# macports libtool allows linking against libc++,
# the libtool in the distribution does not

depends_build-append \
                    port:libtool \
                    port:pkgconfig
build.args          LIBTOOL=${prefix}/bin/glibtool

if {${subport} eq "zmq" || ${subport} eq "zmq-devel"} {

    variant gnutls description "Use TLS for WSS support" {
        depends_lib-append  path:lib/pkgconfig/gnutls.pc:gnutls
        configure.args-replace \
                    -DWITH_TLS=OFF -DWITH_TLS=ON
    }

    variant nss description "Use NSS instead of builtin sha1" {
        depends_lib-append  port:nss
        configure.args-replace \
                    -DWITH_NSS=OFF -DWITH_NSS=ON
        pre-configure {
            # We should not need to do this manually but there
            # is a deficiency in the zmq build system.
            # https://github.com/zeromq/libzmq/issues/4517
            configure.cxxflags-append [exec ${prefix}/bin/pkg-config nss --cflags]
            configure.ldflags-append [exec ${prefix}/bin/pkg-config nss --libs-only-L]
        }
    }

    variant sodium description "Build with libsodium support" {
        depends_lib-append \
                    port:libsodium
        configure.args-replace \
                    -DWITH_LIBSODIUM=OFF -DWITH_LIBSODIUM=ON
        configure.args-append \
                    -DENABLE_LIBSODIUM_RANDOMBYTES_CLOSE=ON \
                    -DWITH_LIBSODIUM_STATIC=OFF
    }

    variant curve requires sodium description "Enable CURVE security support" {
        configure.args-append \
                    -DENABLE_CURVE=ON
    }

    default_variants    +sodium +curve

    variant tests description "Build and run tests" {
        configure.args-replace \
                    -DZMQ_BUILD_TESTS=OFF -DZMQ_BUILD_TESTS=ON
        pre-test {
            fs-traverse test ${cmake.build_dir}/bin {
                if {[file isfile ${test}] && [string match "*test*" ${test}]} {
                    system -W ${cmake.build_dir} "install_name_tool -change \
                        ${prefix}/lib/libzmq.5.dylib ${cmake.build_dir}/lib/libzmq.5.dylib ${test}"
                }
            }
        }
        test.run        yes
        test.cmd        ctest
    }
}
