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

# for strnlen, at least
legacysupport.newest_darwin_requires_legacy 10

# PLEASE DO NOT USE THIS PORT unless you know what you are doing.
# Provisionally it works on x86_86 and arm64, with some oddities.
# It does not build with gcc, on any system, so cannot build on PowerPC.
# Depspecs may or may not be accurate.

github.setup    XQuartz xorg-server 2.8.5 XQuartz-
revision        0
name            xquartz
conflicts       xorg-server-legacy xorg-server-1.18 xorg-server xorg-server-devel
categories      x11 devel
platforms       {darwin > 8}
license         X11
maintainers     {@barracuda156 macos-powerpc.org:barracuda}
homepage        https://www.x.org
description     The X.org / Xquartz X server
long_description \
                The X.org X server allows you to run X11 applications \
                on your computer.

github.tarball_from archive

set main_dist   ${distname}.tar.gz
checksums       ${main_dist} \
                rmd160  3e5b9111ecbcf26966ef95e931484fd875d1764f \
                sha256  b602add0aad88a8dc40e4098c464891dfc731ec7cef04c6c002bddcc82581cc6 \
                size    5422823

worksrcdir      xorg-server-XQuartz-${version}

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

# Disable for now, see below.
# compiler.c_standard 2011

patch.pre_args-replace  -p0 -p1

if {${os.platform} eq "darwin" && ${os.major} < 11} {
    # https://github.com/XQuartz/xorg-server/tree/server-1.20-legacy
    set legacy_ver      1.20.10
    set legacy_hash     16f52e51a203bdb90de5d94478aa9bca4e9c2e67
# Alternative way, using an additional distfile instead of the huge patch:
#     set legacy_distname xorg-server-${legacy_hash}
#     set legacy_dist     ${legacy_distname}.tar.gz
#     master_sites-append https://github.com/XQuartz/xorg-server/archive/${legacy_hash}/:legacy
#     distfiles-append    ${legacy_dist}:legacy
#     checksums-append    ${legacy_dist} \
#                         rmd160  2fd222dab05d16ba4abae1ec39f310cb3eed162a \
#                         sha256  c45df6f290c0acc99cdda8bcca98f8920fe41fb8ca0c6865cf60326669708a9e \
#                         size    5849227

#     pre-patch {
#         set xquartzsrc  hw/xquartz
#         delete ${worksrcpath}/${xquartzsrc}
#         move ${workpath}/${legacy_distname}/${xquartzsrc} ${worksrcpath}/${xquartzsrc}
#     }

    patchfiles-append   0001-hw-xquartz-replace-sources-with-working-1.20-legacy.patch \
                        0002-site.h-restore-missing-include.patch \
                        0003-Fixes-to-meson.build-files.patch \
                        0004-include-glx_extinit.h-fix-for-pre-C11-compilers.patch \
                        0005-os-inputthread.c-fix-compiling.patch \
                        0006-darwin.c-define-BUILD_DATE.patch
    if {[string match macports-gcc* ${configure.compiler}]} {
        # This builds, but possibly broken, needs further work:
        patchfiles-append \
                        0007-x-list.h-fix-for-private_extern.patch \
                        0008-Make-dispatch-blocks-free.patch
    }
    if {${os.major} == 10} {
        # Still needed on 10.6.8 ppc?
        patchfiles-append \
                        0009-Minor-fixes-for-10.6-ppc.patch
    }
    if {${os.major} < 10} {
        patchfiles-append \
                        patch-leopard-build.diff
    }
}

configure.args-append \
        -Dbundle-id-prefix=org.macports \
        -Ddocs=false \
        -Ddri2=false \
        -Ddri3=false \
        -Ddtrace=false \
        -Dlibunwind=false \
        -Dsha1=CommonCrypto \
        -Dsystemd_logind=false \
        -Dudev=false \
        -Dvendor_name="MacOS PowerPC" \
        -Dxcsecurity=false \
        -Dxephyr=true \
        -Dxf86bigfont=false \
        -Dxpbproxy=false \
        -Dxselinux=false \
        -Dxwin=false

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

# 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 {
    # Drop on the next update:
    incr revision

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

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

post-destroot {
        ln -s Xquartz ${destroot}${prefix}/bin/X
        system "chown -R root:admin ${destroot}${applications_dir}/X11.app"
}

variant xcsecurity description "Enable Security Extensions" {
    configure.args-replace \
        -Dxcsecurity=false -Dxcsecurity=true
}

# This might hold for recent macOS versions in general.
if {${os.platform} eq "darwin" && ${configure.build_arch} eq "arm64"} {
    notes-append "
        While ${name} works on aarch64, xorg-server port appears to be more stable.
    "
}
