# -*- 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           conflicts_build 1.0
PortGroup           github 1.0

name                libsdl2-cocoa
conflicts           libsdl2-powerpc libsdl2-snowleopard libsdl2-x11
github.setup        libsdl-org SDL 2.32.10 release-
revision            2
categories          devel multimedia
# Arbitrary threshold, but avoid on modern systems:
platforms           {darwin < 13}
supported_archs     i386 ppc ppc64 x86_64
license             zlib
maintainers         {@barracuda156 macos-powerpc.org:barracuda} openmaintainer
description         Cross-platform multi-media development API
long_description    Simple DirectMedia Layer is a cross-platform development library \
                    designed to provide low-level access to audio, keyboard, mouse, \
                    joystick and graphics hardware via OpenGL and Direct3D. This port \
                    is aimed at supporting legacy macOS.

homepage            https://www.libsdl.org

github.tarball_from releases
distname            SDL2-${version}
set main_dist       ${distname}.tar.gz

checksums           ${main_dist} \
                    rmd160  4212372b886f8dbab92141c520951e245e0c9657 \
                    sha256  5f5993c530f084535c65a6879e9b26ad441169b3e25d789d83287040a9ca5165 \
                    size    7630262

# /usr/include/hfs/hfs_format.h: error: expected specifier-qualifier-list before ‘uuid_string_t’
conflicts_build     libuuid

configure.args-append \
                    --disable-dbus \
                    --disable-esd \
                    --disable-jack \
                    --disable-libsamplerate \
                    --disable-pulseaudio \
                    --disable-video-opengl \
                    --disable-video-opengles \
                    --disable-video-wayland \
                    --disable-video-x11 \
                    --disable-x11-shared \
                    --enable-video-cocoa \
                    --without-x

# FIXME: look into fixing CoreAudio module for Tiger.
if {${os.platform} eq "darwin" && ${os.major} < 9} {
    configure.args-append \
                    --disable-audio \
                    --disable-haptic \
                    --disable-hidapi \
                    --disable-hidapi-joystick \
                    --disable-locale

    # restore old joystick workaround
    patchfiles-replace \
                    0004-enable-iokit-joystick.diff \
                    tiger-patch-joystick.diff
}

build.args          V=1

platform darwin powerpc {
    compiler.blacklist  *clang*

    if {![catch {sysctl hw.vectorunit} result] && $result > 0} {
        # Work around buggy header. https://trac.macports.org/ticket/55251
        configure.cflags-append -faltivec
        # There is no C++ code in this port at the moment, but does not hurt.
        configure.cxxflags-append -faltivec
    }
}

# Fix-ups for PowerPC systems:
patchfiles-append   0001-Fixes-for-PowerPC.patch

# Trash clangisms:
if {[string match *gcc* ${configure.compiler}]} {
    patchfiles-append \
                    0002-gcc-build.patch
}

if {${os.platform} eq "darwin" && ${os.major} < 12} {
    set legacy_ver      2.0.3
    set legacy_distname release-${legacy_ver}
    set legacy_dist     ${legacy_distname}.tar.gz
    master_sites-append https://github.com/libsdl-org/SDL/archive/refs/tags/
    distfiles-append    ${legacy_dist}
    checksums-append    ${legacy_dist} \
                        rmd160  2e54e3a55cbf66fb94c0a0426b47f553746ee2d6 \
                        sha256  d080afdb075fa87254cc727bc1454b55f45ba4d879afce8307a3aa74db6bbf61 \
                        size    5708400

    pre-patch {
        set cocoasrc    src/video/cocoa
        delete ${worksrcpath}/${cocoasrc}
        move ${workpath}/SDL-${legacy_distname}/${cocoasrc} ${worksrcpath}/${cocoasrc}
    }

    # See: https://forums.macrumors.com/threads/sdl2-for-older-macos-x.2456453
    patchfiles-append   0003-Cocoa-adjustments-to-fix-build.patch
}

# Enable the IOKit joystick driver on legacy macOS by shimming the 10.6-only
# IOHIDDeviceGetService() (force feedback is disabled pre-10.6, input works):
patchfiles-append   0004-enable-iokit-joystick.diff

post-destroot {
    set docdir ${prefix}/share/doc/${name}
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} BUGS.txt CREDITS.txt LICENSE.txt \
        README-SDL.txt TODO.txt WhatsNew.txt \
        {*}[glob ${worksrcpath}/docs/*.md] ${destroot}${docdir}
}

variant opengl description "Enable OpenGL" {
    configure.args-replace \
                    --disable-video-opengl --enable-video-opengl
}

default_variants    +opengl

# Only relevant on 10.6.x, specifically with A5 image.
# See: https://forums.macrumors.com/threads/sdl2-for-older-macos-x.2456453/page-10?post=34277598#post-34277598
platform darwin 10 {
    variant oldapi description "Build for older CoreGraphics" {
        patchfiles-append \
                    0005-FORCE_OLD_API.patch
        configure.cppflags-append \
                    -DFORCE_OLD_API=1
    }

    # This is not great, but given that the main 10.6.8 image needs this hack,
    # it is easier to enable it by default, so that users get working apps.
    # Anyone with Nvidia-specific 10.6.8 image (or 10a190) can build with `libsdl2-cocoa -oldapi`.
    default_variants-append \
                    +oldapi
}

notes "
Notice that video playback via Cocoa backend in SDL may not work with ATI GPUs.\
If you have ATI GPU and see video output failure with a dependent of libsdl2,
consider using X11-based version of the latter, provided by libsdl2-x11 port.\
Also notice that 10.6.8 A5 image needs +oldapi variant.
"
