# -*- 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               php 1.1

name                    php-openswoole
categories-append       net devel
maintainers             {ryandesign @ryandesign} {mathiesen.info:macintosh @BjarneDMat} @ArtKoKo
license                 Apache-2

php.branches            7.2 7.3 7.4 8.0 8.1 8.2 8.3 8.4 8.5
php.pecl                yes

# This extension must load after the sockets extension
php.extension_ini       ~${php.extension_ini}

patch.pre_args-replace  -p0 -p1

if {[vercmp ${php.branch} >= 8.3]} {
    version             26.2.0
    revision            11
    checksums           rmd160  d1d7892096f78f27c1a611828202ef41544e3adc \
                        sha256  2b411b41e96916cc2856d0c1f208025e23d5b72f384c1ba176e467d25ee759d7 \
                        size    1301162
} elseif {[vercmp ${php.branch} >= 8.2]} {
    version             25.2.0
    revision            11
    checksums           rmd160  745e2859737fb01da2adb32ca032c2fd63a4e3ed \
                        sha256  bcc1fed4877b6646cb8f79b092ba03d5e495800465a3fff83c065c58d4312d40 \
                        size    1239054
} elseif {[vercmp ${php.branch} >= 8.1]} {
    version             22.1.2
    revision            1
    checksums           rmd160  554eb7fa11c1927e57f7bdb5ca4eb820fbf9f548 \
                        sha256  ec9d08e9484bf95a0080738342a84f09b9c5b8222f4a03c4736caacb7668cb46 \
                        size    1252932
} elseif {[vercmp ${php.branch} >= 7.2]} {
    version             4.10.0
    revision            2
    checksums           rmd160  074542644edb0ff9815a7179d4d3ad0172c0d1cd \
                        sha256  15225470bde025a6aae2bb942ba01b29e51856f9018f88093d992f4eb1ba0e6f \
                        size    1627513
    patchfiles          arm.patch
}

description             an event-driven asynchronous & concurrent & coroutine networking engine with high performance for PHP

long_description        ${name} is ${description}.

if {${name} ne ${subport}} {
    PortGroup           legacysupport 1.1
    # strndup
    legacysupport.newest_darwin_requires_legacy \
                        10
    # https://github.com/swoole/swoole-src/issues/3896
    # CLOCK_REALTIME
    legacysupport.newest_darwin_requires_legacy \
                        15

    conflicts           ${php}-swoole

    # macOS pthread compatibility (all PHP versions)
    # Disable Linux-specific features not available on macOS
    if {[vercmp ${version} >= 25.2.0]} {
        post-patch {
            reinplace "s|AC_CHECK_LIB(pthread, pthread_spin_lock|dnl DISABLED_ON_MACOS AC_CHECK_LIB(pthread, pthread_spin_lock|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(pthread, pthread_barrier_init|dnl DISABLED_ON_MACOS AC_CHECK_LIB(pthread, pthread_barrier_init|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(pthread, pthread_mutex_timedlock|dnl DISABLED_ON_MACOS AC_CHECK_LIB(pthread, pthread_mutex_timedlock|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(pthread, pthread_mutexattr_setrobust|dnl DISABLED_ON_MACOS AC_CHECK_LIB(pthread, pthread_mutexattr_setrobust|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(pthread, pthread_mutex_consistent|dnl DISABLED_ON_MACOS AC_CHECK_LIB(pthread, pthread_mutex_consistent|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(c, getrandom|dnl DISABLED_ON_MACOS AC_CHECK_LIB(c, getrandom|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(c, accept4|dnl DISABLED_ON_MACOS AC_CHECK_LIB(c, accept4|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(c, signalfd|dnl DISABLED_ON_MACOS AC_CHECK_LIB(c, signalfd|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(c, malloc_trim|dnl DISABLED_ON_MACOS AC_CHECK_LIB(c, malloc_trim|g" ${worksrcpath}/config.m4
            reinplace "s|AC_CHECK_LIB(c, epoll_create|dnl DISABLED_ON_MACOS AC_CHECK_LIB(c, epoll_create|g" ${worksrcpath}/config.m4
        }
    }

    compiler.cxx_standard 2011
    compiler.thread_local_storage yes

    depends_lib-append  port:brotli \
                        port:hiredis \
                        port:nghttp2 \
                        path:lib/libssl.dylib:openssl \
                        port:zlib

    configure.args      --enable-async-redis \
                        --enable-http2 \
                        --enable-openssl \
                        --enable-swoole \
                        --with-openssl-dir=${prefix}
}
