# -*- 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           codeberg 1.0
PortGroup           conflicts_build 1.0

codeberg.setup      emzeat xdispatch2 2.1.4 v
revision            0
categories          devel
license             Apache-2
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
description         Grand Central Dispatch-like C++ threading library
long_description    {*}${description}, built around queues, thread pools \
                    and flexible backends.
homepage            https://emzeat.de/xdispatch2
checksums           rmd160  84d03ffa9f18ef854943a19371644707e3b76ee6 \
                    sha256  ede47fd751ae1bb4d680ff4cf60f210f6a73e5a4f1a73330251415377814a401 \
                    size    125886

cmake.generator     Ninja

compiler.cxx_standard \
                    2011
# cancelable.cpp:30:8: error: thread-local storage
# is not supported for the current target
compiler.thread_local_storage \
                    yes

depends_build-append \
                    port:mz-cmaketools

conflicts_build     libunwind

# See: https://github.com/emzeat/xdispatch2/issues/1
patchfiles-append   0001-Avoid-a-need-for-Conan.patch

post-patch {
    reinplace "s,@PREFIX@,${prefix}," ${worksrcpath}/CMakeLists.txt
    reinplace "s,@VERSION@,${version}," ${worksrcpath}/CMakeLists.txt
}

# Backends
configure.args-append \
                    -DBUILD_XDISPATCH2_BACKEND_NAIVE=ON \
                    -DBUILD_XDISPATCH2_BACKEND_LIBDISPATCH=OFF \
                    -DBUILD_XDISPATCH2_BACKEND_QT5=OFF
# Build options
configure.args-append \
                    -DBUILD_XDISPATCH2_AS_FRAMEWORK=OFF \
                    -DBUILD_XDISPATCH2_STATIC=OFF \
                    -DBUILD_XDISPATCH2_TESTS=OFF

variant libdispatch conflicts libdispatch_legacy description "Enable libdispatch back-end" {
    configure.args-replace \
                    -DBUILD_XDISPATCH2_BACKEND_LIBDISPATCH=OFF \
                    -DBUILD_XDISPATCH2_BACKEND_LIBDISPATCH=ON
}

platform darwin {
    variant framework description "Build as a framework" {
        configure.args-replace \
                    -DBUILD_XDISPATCH2_AS_FRAMEWORK=OFF \
                    -DBUILD_XDISPATCH2_AS_FRAMEWORK=ON
    }

    default_variants-append \
                    +framework

    # Does not yet build with libdispatch on 10.6:
    # https://github.com/emzeat/xdispatch2/issues/2
    # It also utilizes the QOS API available in macOS >10.10
    # libdispatch_queue.cpp:139:38: error: use of undeclared
    # identifier 'dispatch_queue_attr_make_with_qos_class'
    if {${os.major} > 13} {
            default_variants-append \
                    +libdispatch
    }

    # This builds now, but remains untested. Do not make the default one.
    variant libdispatch_legacy conflicts libdispatch description "Enable libdispatch support for legacy systems" {
        depends_lib-append \
                    port:libdispatch-legacy
        configure.args-replace \
                    -DBUILD_XDISPATCH2_BACKEND_LIBDISPATCH=OFF \
                    -DBUILD_XDISPATCH2_BACKEND_LIBDISPATCH=ON
        configure.cppflags-append \
                    -I${prefix}/libexec/dispatch/usr/include
        # Should be passed via C++ flags:
        configure.cxxflags-append \
                    -Wl,${prefix}/libexec/dispatch/usr/lib/libdispatch.a
    }
}

variant tests description "Enable testing" {
    configure.pre_args-replace \
                    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON \
                    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF
    configure.args-replace \
                    -DBUILD_XDISPATCH2_TESTS=OFF -DBUILD_XDISPATCH2_TESTS=ON

    # runner.c: error: variable 'no' set but not used [-Werror,-Wunused-but-set-variable]
    # MUnit_tools.h: error: 'vsprintf' is deprecated: This function is provided
    # for compatibility reasons only [-Werror,-Wdeprecated-declarations]
    patchfiles-append \
                    0002-Fix-tests.patch

    test.run        yes
    test.cmd        ${cmake.build_dir}/bin/xdispatch2_tests
    test.target
}