# -*- 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
github.setup        libsdl-org SDL 2.32.10 release-
revision            1
categories          devel graphics multimedia wayland x11
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.
conflicts           libsdl2-cocoa libsdl2-powerpc libsdl2-snowleopard libsdl2-x11

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 libunwind-headers

configure.args-append \
                    --disable-dbus \
                    --disable-esd \
                    --disable-libsamplerate \
                    --disable-pulseaudio \
                    --disable-render-metal \
                    --disable-video-metal \
                    --disable-video-opengl \
                    --disable-video-opengles \
                    --disable-video-wayland \
                    --disable-video-x11 \
                    --disable-x11-shared \
                    --without-x

# Disable Jack until this is fixed:
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61759
configure.args-append \
                    --disable-jack

platform darwin {
    configure.args-append \
                    --enable-video-cocoa
}

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} < 10} {
    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
} elseif {${os.platform} eq "darwin" && ${os.major} < 12} {
    patchfiles-append   0003-cocoa-legacy-macos.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):
if {${os.platform} eq "darwin" && ${os.major} < 11} {
    patchfiles-append   0004-enable-iokit-joystick.diff
}

patchfiles-append       0005-fix-x11.diff

platform macosx {
    if {(!($universal_possible && [variant_isset universal]) && ${configure.build_arch} eq "i386")
        || (($universal_possible && [variant_isset universal]) && "i386" in ${configure.universal_archs})} {
        compiler.blacklist-append   {clang < 703}
    } else {
        compiler.blacklist-append   {clang < 500}
    }
}

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

default_variants-append \
                    +opengl

if {${os.subplatform} ne "macosx"} {
    default_variants-append \
                    +x11
}

# Config header hardcodes wrong paths.
post-patch {
    reinplace "s|@PREFIX@|${prefix}|g" \
                    ${worksrcpath}/include/SDL_config_macosx.h \
                    ${worksrcpath}/configure
    # This is specifically inside X11, so assume mesa:
    reinplace "s|@GL_LIB@|${prefix}/lib/libGL.1.dylib|" \
            ${worksrcpath}/src/video/x11/SDL_x11opengl.c
}

# 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 powerpc {
    variant oldapi description "Build for older CoreGraphics" {
        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 -oldapi`.
    default_variants-append \
                    +oldapi
}

variant dbus description "Enable DBus support" {
    depends_lib-append \
                    port:dbus

    configure.args-replace \
                    --disable-dbus --enable-dbus
}

# Notice, currently only Jack1 is available.
variant jack description "Build with Jack support" {
    depends_lib-append \
                    port:jack

    configure.args-replace \
                    --disable-jack --enable-jack
}

variant opengl description "Enable OpenGL" {
    if {[variant_isset wayland] || [variant_isset x11]} {
        depends_lib-append \
                    port:mesa
    } else {
        patchfiles-append \
                    patch-opengl_apple.diff
    }

    configure.args-replace \
                    --disable-video-opengl --enable-video-opengl
}

variant pulseaudio description "Build with PulseAudio support" {
    depends_lib-append \
                    port:pulseaudio

    configure.args-replace \
                    --disable-pulseaudio --enable-pulseaudio
}

variant samplerate description "Build with libsamplerate support" {
    depends_lib-append \
                    port:libsamplerate

    configure.args-replace \
                    --disable-libsamplerate --enable-libsamplerate
}

variant wayland description "Build with Wayland support" {
    patchfiles-append \
                    0006-wayland-macos.patch

    # error: invalid suffix "b000" on integer constant
    if [string match *gcc-4.* ${configure.compiler}] {
        patchfiles-append \
                    0007-wayland-avoid-gnu-binary-literals.patch
    # SDL_cocoamodes.m: error: 'for' loop initial declarations are only allowed in C99 or C11 mode
    } elseif [string match macports-gcc* ${configure.compiler}] {
        configure.objcflags-append \
                    -std=c99
    }

    depends_build-append \
                    path:bin/pkg-config:pkgconfig \
                    port:wayland-protocols
    depends_lib-append \
                    port:epoll-shim \
                    port:libxkbcommon \
                    port:wayland

    # Drop this flag, otherwise configure refuses to acknowledge wayland presence:
    configure.args-delete \
                    --disable-video-opengles
    configure.args-replace \
                    --disable-video-wayland --enable-video-wayland

    notes-append "
    To run sdl under wayland, install Owl port.

    You may need to set DYLD_LIBRARY_PATH=${prefix}/lib/libgcc at runtime.

    To disable GL at runtime, set SDL_FRAMEBUFFER_ACCELERATION=0
    "
}

variant x11 {
    depends_lib-append \
                    port:xorg-libX11 \
                    port:xorg-libXcursor \
                    port:xorg-libXext \
                    port:xorg-libXfixes \
                    port:xorg-libXi \
                    port:xorg-libXinerama \
                    port:xorg-libXrandr \
                    port:xorg-libXScrnSaver \
                    port:xorg-libXxf86vm \
                    port:xrender

    configure.args-delete \
                    --without-x
    configure.args-replace \
                    --disable-video-x11 --enable-video-x11
    configure.args-append \
                    --x-includes=${prefix}/include \
                    --x-libraries=${prefix}/lib
}

# FIXME: CoreAudio uses AudioQueue, which is 10.5+, 10.4 will need AudioUnit instead.
# Until fixed, use pulseaudio.
platform darwin 8 {
    configure.args-append \
                    --disable-haptic \
                    --disable-hidapi \
                    --disable-hidapi-joystick \
                    --disable-locale

    # Normally on Mac OS X configure forces coreaudio, and never even checks 
    # for pulseaudio. This patch makes it only check for pulseaudio.
    patchfiles-append   configure_tiger.diff

    # Restore old joystick workaround:
    patchfiles-replace  0004-enable-iokit-joystick.diff \
                        tiger-patch-joystick.diff

    default_variants-append +pulseaudio
}

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,
you may need to rely on wayland or x11 back-ends.\
Also notice that 10.6.8 A5 image needs +oldapi variant.
"
