# -*- 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       active_variants 1.1
PortGroup       github 1.0
PortGroup       legacysupport 1.1
PortGroup       meson 1.0

# for strnlen, at least
legacysupport.newest_darwin_requires_legacy 10

# Note, this is an experimental port.

name            xlibre-xserver
github.setup    X11Libre xserver 25.2.2 ${name}-
revision        0
conflicts       xorg-server-legacy xorg-server-1.18 xorg-server xorg-server-devel xquartz
categories      x11 devel
platforms       {darwin > 8}
license         X11
maintainers     {@barracuda156 macos-powerpc.org:barracuda}
description     XLibre Xserver
long_description \
                The XLibre Xserver allows you to run X11 applications \
                on your computer.

checksums       rmd160  6f64edfe19cf3f017bdc527df786326d8240a801 \
                sha256  3c1f07f4b5f8a483473097be4b964ee97a2b92adde9de6c0d8d9d7217a382810 \
                size    5609445
github.tarball_from archive

extract.rename  yes

depends_build-append \
        path:bin/pkg-config:pkgconfig \
        port:xorg-font-util \
        port:xorg-libxkbfile \
        port:xorg-util-macros \
        port:xorg-xorgproto \
        port:xorg-xtrans

# Mesa is a *lib* dependency now: /opt/local/bin/Xephyr
# links to /opt/local/lib/libGL.1.dylib
depends_lib-append \
        port:libepoxy \
        path:lib/pkgconfig/pixman-1.pc:libpixman \
        port:mesa \
        port:xorg-libX11 \
        port:xorg-libXau \
        port:xorg-libxcb \
        port:xorg-libXdmcp \
        port:xorg-libXext \
        port:xorg-libXfixes \
        port:xorg-libXfont2 \
        port:xorg-libXinerama \
        port:xorg-libXres \
        port:xorg-libXScrnSaver \
        port:xorg-libxshmfence \
        port:xorg-xcb-util \
        port:xorg-xcb-util-image \
        port:xorg-xcb-util-keysyms \
        port:xorg-xcb-util-renderutil \
        port:xorg-xcb-util-wm

platform darwin {
    depends_lib-append \
        port:xorg-libAppleWM
}

# This xinit dependency needs to be port: not bin:
# because we specifically run ${prefix}/bin/startx from bundle-main.c
depends_run-append \
        port:quartz-wm \
        port:xauth \
        port:xinit \
        port:xkbcomp \
        port:xkeyboard-config \
        port:xorg-fonts

compiler.c_standard     2011

patch.pre_args-replace  -p0 -p1

# https://github.com/X11Libre/xserver/pull/3463
patchfiles-append       0001-xquartz-GL-sanitize-QuickDraw-WindowPtr-in-capabilit.patch
# https://github.com/X11Libre/xserver/pull/3464
patchfiles-append       0002-xquartz-GL-include-xpr_dri.h-instead-of-dri.h.patch
# This part was not merged earlier, but required:
patchfiles-append       0003-xquartz-work-around-meson-bug.patch

# https://github.com/X11Libre/xserver/pull/2836
patchfiles-append       0001-xquartz-work-around-a-bug-in-10.5-system-headers.patch \
                        0002-xquartz-bundle-main.c-add-missing-header-for-pre-10..patch \
                        0003-xquartz-xprFrame.c-drop-duplicate-variable-in-fallba.patch \
                        0004-os-client.c-portability-fix-use-generic-code-on-macO.patch \
                        0005-xquartz-X11Application.m-fallbacks-to-support-10.6.patch \
                        0006-xquartz-xpr-support-pre-10.6-versions-of-Xplugin.patch \
                        0007-xquartz-X11Controller.m-add-one-missing-synthesize-c.patch
# This will be replaced, currently it merely allows to fail one step further (ppc64):
patchfiles-append       0008-xquartz-x-hash.h-a-hack-for-ppc64.patch

# Use org.macports to match other X servers that we have:
configure.args-append \
        -Dbundle-id-prefix=org.macports \
        -Ddocs=false \
        -Dlibunwind=false \
        -Dmitshm=false \
        -Dtests=false \
        -Dxwin=false

# -Dxorg=true triggers Linux-specific modules:
# pciaccess relies not only on libpciaccess but also linux kernel.
# However even if it is disabled, source pulls in libpciaccess header.
# https://github.com/X11Libre/xserver/issues/1731
# https://github.com/X11Libre/xserver/issues/1732

platform darwin {
    configure.args-append \
        -Dapple-applications-dir=${applications_dir} \
        -Ddrm=false
}

# Not needed when building with gcc-4.2.
# TODO: check what is still needed of these.
# render/picture.c:876:26: error: array subscript 'union _SourcePict[0]' is partly outside array bounds of 'unsigned char[16]' [-Werror=array-bounds=]
# os/client.c:162:14: error: assignment to 'char **' from 'int' makes pointer from integer without a cast [-Wint-conversion]
# xquartz/quartz.c:417:9: error: implicit declaration of function 'xp_disable_hot_keys'; did you mean 'xp_disable_update'? [-Wimplicit-function-declaration]
# xquartz/X11Controller.m:173:31: error: passing argument 2 of 'insertItemWithTitle:action:keyEquivalent:atIndex:' from incompatible pointer type [-Wincompatible-pointer-types]
# xquartz/X11Controller.m:596:1: error: control reaches end of non-void function [-Werror=return-type]
# xquartz/pbproxy/x-selection.m:1205:5: error: '-fobjc-exceptions' is required to enable Objective-C exception syntax
if {[string match macports-gcc* ${configure.compiler}]} {
    configure.cflags-append \
        -Wno-error=array-bounds= \
        -Wno-error=implicit-function-declaration \
        -Wno-error=int-conversion \
        -Wno-error=incompatible-pointer-types
    configure.objcflags-append \
        -fobjc-exceptions \
        -Wno-error=return-type
}

platform darwin 9 {
    # Xplugin.h is missing on Tiger and incorrect on Leopard.
    configure.cppflags-append \
        -I${filespath}/include

    if {[string match *gcc-4.* ${configure.compiler}]} {
        configure.objcflags-append \
            -fnested-functions
    }
}

destroot.args-append \
        --destdir=${destroot}

post-destroot {
        system "chown -R root:admin ${destroot}${applications_dir}/X11.app"

        ln -s Xquartz ${destroot}${prefix}/bin/X
}
