# -*- 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           meson 1.0

name                wayland
categories          wayland devel graphics
license             MIT
maintainers         nomaintainer

depends_build-append \
                    path:bin/pkg-config:pkgconfig

if {${subport} eq "${name}"} {
    PortGroup       gitlab 1.0
    PortGroup       legacysupport 1.1

    # CLOCK_MONOTONIC
    legacysupport.newest_darwin_requires_legacy 15

    gitlab.instance https://gitlab.freedesktop.org

    gitlab.setup    wayland wayland 1.26.0
    revision        0
    epoch           1
    description     Core Wayland protocol and libraries
    long_description \
                    Wayland is intended as a simpler replacement for X \
                    easier to develop and maintain. GTk, Qt, GNOME and KDE \
                    have backends for it.
    checksums       rmd160  e027b54369e7650f151eb6c7e20383bdee98a3d5 \
                    sha256  ebf5fff1c8b11c24ceec74ff3047aefdb07efee8ce09bf3b856975aba3540d15 \
                    size    686406

    depends_build-append \
                    path:bin/cmake:cmake

    depends_lib-append \
                    port:epoll-shim \
                    port:expat \
                    port:libffi \
                    port:libxml2

    patch.pre_args-replace  -p0 -p1

    # Patches from https://github.com/XQuartz/wayland/commits/04654ba7b57ccbf583db899a327f8f7000f90546
    patchfiles-append \
                    0001-os-Add-an-implementation-of-wl_os_socket_peercred-fo.patch \
                    0002-os-Use-fallback-implementations-when-_CLOEXEC-are-un.patch \
                    0003-os-Provide-wl_os_socketpair_cloexec-wrapper-for-syst.patch \
                    0004-shm-Avoid-unused-variable-warning-HAVE_MEMFD_CREATE-.patch \
                    0005-darwin-Compile-with-D_DARWIN_C_SOURCE.patch \
                    0006-darwin-Set-rt_dep-to-empty-as-it-s-part-of-libSystem.patch \
                    0007-darwin-Locate-libffi-with-cc.find_library-rather-tha.patch \
                    0008-darwin-Locate-libexpat-with-cc.find_library-rather-t.patch \
                    0009-darwin-Use-epoll-shim.patch \
                    0010-wayland-egl-Update-wayland-egl-symbols-check-for-dar.patch \
                    0012-tests-Specify-the-test-section-as-__RODATA-on-darwin.patch

    # Patches from @macos-powerpc
    patchfiles-append \
                    0013-Darwin-fixes-from-owl-compositor.patch \
                    0014-wayland-os.c-LOCAL_PEERPID-may-not-be-defined.patch \
                    0015-os-wrappers-test-F_DUPFD_CLOEXEC-may-not-be-defined.patch \
                    0016-wayland-client.c-use-epoll-shim.patch \
                    0017-tests-use-wl_os_socketpair_cloexec-wrapper.patch \
                    0018-queue-test.c-provide-memrchr.patch

    # Patches from @RJVB
    patchfiles-append \
                    patch-darwin-dylib-versions.diff
    # Don't abort on a NULL listener in wl_invoke_closure 
    patchfiles-append \
                    patch-no-abort-null-closure-listener.diff
    # Fall back to TMPDIR if XDG_RUNTIME_DIR isn't set
    patchfiles-append \
                    patch-xdg-or-tmpdir.diff

    # stdatomic.h
    compiler.c_standard 2011

    configure.args-append \
                    -Ddocumentation=false \
                    -Ddtd_validation=true \
                    -Dlibraries=true \
                    -Dscanner=true \
                    -Dtests=true

    variant docs description "Install docs" {
        configure.args-replace \
                    -Ddocumentation=false \
                    -Ddocumentation=true
    }

    test.run        yes
}

subport wayland-protocols {
    PortGroup       gitlab 1.0

    gitlab.instance https://gitlab.freedesktop.org

    gitlab.setup    wayland wayland-protocols 1.49
    revision        0
    platforms       any
    description     Wayland protocols
    long_description \
                    ${description}
    checksums       rmd160  398c5baa7e0f288db335d7659cb27c83119c2de7 \
                    sha256  7330d2d8fbda81991548738e81da493829bc55db2daf1ae15b9d4952e4d7d83b \
                    size    170445
    supported_archs noarch

    set py_ver          3.14
    set py_ver_nodot    [string map {. {}} ${py_ver}]
    configure.python    ${prefix}/bin/python${py_ver}

    # Our wayland-scanner fails to generate certain xdg_?? structures (incl. xdg_toplevel_interface)
    # so link the tests in flat_namespace with `-undefined warning` (and let the corresponding tests fail).
    # patchfiles-append \
    #                 patch-wprots-undefined-warn.diff

    post-patch {
        reinplace "s|/usr/bin/env python3|${configure.python}|" ${worksrcpath}/tests/replace.py
    }

    depends_build-append \
                    port:python${py_ver_nodot} \
                    port:wayland
    depends_run-append \
                    port:wayland

    # It is broken now, and flags are not passed.
    # Undefined symbols for architecture x86_64: "_zwp_tablet_tool_v2_interface"
    # Anyway, just disable for the time-being.
    configure.args-append \
                    -Dtests=false

    # test.run        yes

    livecheck.url   https://wayland.freedesktop.org/releases.html
    livecheck.regex "${subport}-(\\d+(?:\\.\\d+)*)"
}
