# -*- 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.1.5 ${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  42bec2b235b9fbd2bac5975ceb1d3b147dff3237 \
                sha256  7ee3612e7674595d7cef337d6955fddf02e63719cbc2607b29d80a17891eca0e \
                size    5491211
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     1999

patch.pre_args-replace  -p0 -p1

# Backports from https://github.com/X11Libre/xserver/pull/1743
patchfiles-append       0001-hw-xquartz-remove-apple-block-extension-code.patch \
                        0002-hw-xquartz-Rewrite-XQuartzDefaults-without-Apple-blo.patch \
                        0003-hw-xquartz-Purge-Apple-blocks-from-X11Application.m.patch \
                        0004-os-client-replace-apple-block-with-proper-function.patch \
                        0005-os-client-rename-get_argmax_fptr-to-get_argmax_from_.patch

# https://github.com/X11Libre/xserver/pull/2832
patchfiles-append       0001-xquartz-X11Controller.h-define-strong-to-retain-if-u.patch \
                        0002-xquartz-xpr-x-list.h-use-__attribute__-if-__private_.patch \
                        0003-xquartz-applewmExt.h-support-build-with-XPLUGIN_VERS.patch \
                        0004-xquartz-xpr-xprEvent.c-support-build-with-XPLUGIN_VE.patch \
                        0005-xquartz-osxcompat.h-compat-header.patch \
                        0006-xquartz-use-asl_log_descriptor-where-supported.patch \
                        0007-xquartz-objc_autoreleasePool-only-supported-with-cla.patch \
                        0008-meson.build-test-for-possibly-unsupported-flags.patch \
                        0009-xquartz-work-around-meson-bug.patch \
                        0010-xquartz-fix-non-portable-ObjC-syntax.patch \
                        0011-xquartz-add-non-dispatch-fallbacks-for-10.5.patch \
                        0012-xquartz-support-legacy-CG-APIs-as-a-fallback.patch

patchfiles-append       0013-Support-Leopard.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
}
