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

name            SDL3
github.setup    libsdl-org SDL 3.2.20 release-
revision        0
categories      devel multimedia
license         zlib
maintainers     {@barracuda156 macos-powerpc.org:barracuda}
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. This port provides the latest \
                3.x release series.

homepage        https://www.libsdl.org

github.tarball_from releases
distname        ${name}-${version}
checksums       rmd160  a0bbb37fbcc879488414757fb83a87f4c6d2e2ff \
                sha256  467600ae090dd28616fa37369faf4e3143198ff1da37729b552137e47f751a67 \
                size    15895143

patchfiles      0001-Fixes-for-X11-on-macOS.patch \
                0002-SDL_build_config_macos.h-fix-macro-for-PowerPC.patch \
                0003-CMakeLists-fix-silly-bug.patch \
                0004-include-SDL3-fix-wrong-headers.patch \
                0005-Fix-some-broken-ObjC-syntax.patch \
                0006-sdlcpu.cmake-fix-arch-macros.patch \
                0007-sdlchecks.cmake-fix-x11-detection.patch \
                0008-Do-not-smuggle-in-a-non-existing-framework.patch \
                0009-_FILE_OFFSET_BITS-64-should-not-be-Linux-only.patch \
                0010-Fix-tray.patch \
                0011-And-fix-linking.patch

# https://github.com/libsdl-org/SDL/issues/12749
patchfiles-append \
                0012-Altivec-fixes.patch

# This is a backport, but for now reverted, see:
# https://github.com/libsdl-org/SDL/pull/12768
# TODO: submit a minimal PR to support Darwin ppc.
# https://github.com/libsdl-org/SDL/issues/12749
patchfiles-append \
                0013-PowerPC-bool.patch

# Fix a bug in atomics header which breaks ppc64 build:
patchfiles-append \
                0014-SDL_atomic.h-fix-a-bug-for-ppc64.patch

if {${os.platform} eq "darwin" && ${os.major} < 10} {
    patchfiles-append \
                patch-leopard-build.diff
}

post-patch {
    reinplace "s|@PREFIX@|${prefix}|g" \
                ${worksrcpath}/include/build_config/SDL_build_config_macos.h \
                ${worksrcpath}/src/video/x11/SDL_x11opengl.c
}

# Disable broken compilers:
compiler.blacklist  *clang* *gcc-4.*

depends_build-append \
                path:bin/pkg-config:pkgconfig

depends_lib-append \
                port:libsamplerate \
                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-append \
                -DSDL_ASAN=OFF \
                -DSDL_ASSEMBLY=ON \
                -DSDL_AUDIO=ON \
                -DSDL_CAMERA=OFF \
                -DSDL_COCOA=OFF \
                -DSDL_COCOA_TRAY=OFF \
                -DSDL_DBUS=OFF \
                -DSDL_DIALOG=OFF \
                -DSDL_DUMMYCAMERA=OFF \
                -DSDL_EXAMPLES=OFF \
                -DSDL_HAPTIC=OFF \
                -DSDL_HIDAPI=OFF \
                -DSDL_HIDAPI_JOYSTICK=OFF \
                -DSDL_IBUS=OFF \
                -DSDL_JACK=OFF \
                -DSDL_JOYSTICK=OFF \
                -DSDL_LIBICONV=OFF \
                -DSDL_LIBUDEV=OFF \
                -DSDL_METAL=OFF \
                -DSDL_OFFSCREEN=ON \
                -DSDL_OPENGL=OFF \
                -DSDL_OPENGLES=OFF \
                -DSDL_OPENVR=OFF \
                -DSDL_POWER=OFF \
                -DSDL_PULSEAUDIO=OFF \
                -DSDL_RENDER_METAL=OFF \
                -DSDL_ROCKCHIP=OFF \
                -DSDL_TESTS=OFF \
                -DSDL_TESTS_LINK_SHARED=OFF \
                -DSDL_VIDEO=ON \
                -DSDL_VIDEO_DRIVER_X11=ON \
                -DSDL_VIRTUAL_JOYSTICK=OFF \
                -DSDL_VULKAN=OFF \
                -DSDL_WAYLAND=OFF \
                -DX11_INCLUDEDIR=${prefix}/include \
                -DX11_LIBDIR=${prefix}/lib \
                -DSDL_X11=ON \
                -DSDL_X11_XCURSOR=ON \
                -DSDL_X11_XFIXES=ON \
                -DSDL_X11_XRANDR=ON \
                -DSDL_X11_XSCRNSAVER=ON \
                -DSDL_XINPUT=OFF

platform powerpc {
    configure.args-append \
                -DSDL_ALTIVEC=ON

    if {${configure.build_arch} eq "ppc"} {
        configure.args-append \
                -DSDL_CPU_POWERPC32=ON
    } elseif {${configure.build_arch} eq "ppc64"} {
        configure.args-append \
                -DSDL_CPU_POWERPC64=ON
    }
}

# TODO: add jack and wayland variants.

variant dbus description "Enable DBus support" {
    depends_lib-append \
                port:dbus
    configure.args-replace \
                -DSDL_DBUS=OFF -DSDL_DBUS=ON
}

# OpenGL via Mesa may lead to system freezes.
# For now, do not enable by default.
variant opengl description "Enable OpenGL support" {
    # Mesa dependency due to: https://trac.macports.org/ticket/61943
    # We cannot depend on XQuartz.
    depends_lib-append \
                port:mesa
    configure.args-replace \
                -DSDL_OPENGL=OFF -DSDL_OPENGL=ON
}

variant pulseaudio description "Build with PulseAudio support" {
    depends_lib-append \
                port:pulseaudio
    configure.args-replace \
                -DSDL_PULSEAUDIO=OFF -DSDL_PULSEAUDIO=ON
}

# SDL_coreaudio.m:142:5: error: 'for' loop initial declarations are only allowed in C99 or C11 mode
configure.cflags-append \
                -std=c99

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

# FIXME: in Rosetta linking fails with ridiculous errors:
# ld: warning: ignoring file CMakeFiles/SDL3-shared.dir/src/filesystem/SDL_filesystem.c.o, file was built for unsupported file format ( 0x00 0x00 0x10 0xEC 0x00 0x00 0x65 0xD3 0x00 0x00 0x10 0xE4 0x00 0x00 0x61 0xD3 ) which is not the architecture being linked (ppc): CMakeFiles/SDL3-shared.dir/src/filesystem/SDL_filesystem.c.o
# ld: warning: ignoring file CMakeFiles/SDL3-shared.dir/src/render/gpu/SDL_pipeline_gpu.c.o, file was built for unsupported file format ( 0x00 0x00 0x0A 0x37 0x0E 0x05 0x00 0x00 0x00 0x00 0x20 0xA8 0x00 0x00 0x0A 0x3C ) which is not the architecture being linked (ppc): CMakeFiles/SDL3-shared.dir/src/render/gpu/SDL_pipeline_gpu.c.o
# ld: warning: alignment for symbol at address 0x41424752 in CMakeFiles/SDL3-shared.dir/src/video/SDL_blit_copy.c.o exceeds 2^16
# ld: in section __TEXT,__text reloc 0: R_ABS reloc but no absolute symbol at target address file 'CMakeFiles/SDL3-shared.dir/src/stdlib/SDL_qsort.c.o' for architecture ppc
# collect2: error: ld returned 1 exit status
# Nothing like this happens on 10.6.8 PowerPC, where everything builds normally.
