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

name                libsdl2-cocoa
conflicts           libsdl2-powerpc libsdl2-snowleopard
github.setup        libsdl-org SDL 2.32.8 release-
revision            0
# Drop this on the next update.
# Revbumped due to altivec flag being added.
platform darwin powerpc {
    incr revision
}
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  55e18428ade6421c51e05d7a8df0100bfe86adb8 \
                    sha256  0ca83e9c9b31e18288c7ec811108e58bac1f1bb5ec6577ad386830eac51c787e \
                    size    7627356

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

# Should be fixable once this is backported:
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105522
# Until then, disable it.
configure.args-append \
                    --disable-joystick

# 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
}

build.args          V=1

# Avoid broken clang. Xcode gcc works, as of 2.32.x at least.
compiler.blacklist  *clang*

platform darwin powerpc {
    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

# To be dropped once this is backported:
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105522
patchfiles-append   0002-hidapi-revert-a-commit-that-triggers-ICE-with-GCC.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
}

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