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

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

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

# 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.11.0
    revision            0
    checksums           rmd160  368fbb122e20fe97accf30cb604fa36581738059 \
                        sha256  2f1833b95a7e3bf5110c3badc6d1ec7aaf0e92afa6e80a949a1520ca515cd999 \
                        size    1238990
} elseif {[vercmp ${php.branch} >= 8.2]} {
    version             25.2.0
    revision            11
    checksums           rmd160  765b0e9a306882b2ff04af08aa1d23b84fcdd142 \
                        sha256  6de2ca64020939243bf76f6515d8b8bae4d253b811d0724429a434470b5ac03e \
                        size    1142060
} elseif {[vercmp ${php.branch} >= 8.1]} {
    version             22.1.2
    revision            1
    checksums           rmd160  1a57d36a565abbc209fe5882985693d1ed549c70 \
                        sha256  1a45b8d2a50bd41879b2ce8e84a1550a750d5a46b5a74a3fd1cb36e19eb48641 \
                        size    1151190
} elseif {[vercmp ${php.branch} >= 7.2]} {
    version             4.10.0
    revision            2
    checksums           rmd160  b0d52a8b10ca3f4028726ea2a752d1c0b9eff855 \
                        sha256  37703e7403c375223b628d112f5dac4598681abd57ca108fdf2f1a161cd341dd \
                        size    1526249
    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}} {
    # remove w/ next update
    dist_subdir                     ${name}/${version}_1

    github.tarball_from     archive
    github.setup            openswoole ext-openswoole ${version} v

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