# -*- 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.0

name                zmq
categories          devel sysutils net
platforms           darwin
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.4 v
    checksums       rmd160  540c9bc003266579d84985c52cc27fe6cbb098cd \
                    sha256  5aa12435d152435a441ecfa8ad49b56eb845829b9bde22f90ea232cebaff055f \
                    size    918351
    revision        2

    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 -p1
    patchfiles-append \
        patch-cxx11.release.diff \
        patch-c11.release.diff \
        patch-fix-docs-dir.release.diff \
        patch-fix-no-librt-APPLE.release.diff \
        patch-tests.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
    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
    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 8c725093ac4b44a97e6cb64566989ef12b71986c
    version         20230206-[string range ${github.version} 0 7]
    checksums       rmd160  c870f927dc0418a401bbe0847bb8ce0aa3576168 \
                    sha256  4dc235243ae91cbe16680d3408fa16ae777c6f277190f44cd271292ae4a231db \
                    size    939179
    revision        1

    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 -p1
    patchfiles-append \
        patch-fix-docs-dir.devel.diff \
        patch-fix-no-librt-APPLE.devel.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 "Use libsodium instead of built-in tweetnacl" {
        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
    }

    default_variants    +sodium

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