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

name            libsdl2
github.setup    libsdl-org SDL 2.30.0 release-
categories      devel multimedia
platforms       macosx freebsd
license         zlib
maintainers     {jmr @jmroot} 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.

homepage        https://www.libsdl.org/

github.tarball_from releases
distname        SDL2-${version}
checksums       rmd160  eb2bab97460b4f9874e6853fb459db965d7ac1be \
                sha256  36e2e41557e0fa4a1519315c0f5958a87ccb27e25c51776beb6f1239526447b0 \
                size    7428037

configure.args  --without-x \
                --disable-jack \
                --disable-esd \
                --disable-pulseaudio \
                --disable-libsamplerate \
                --disable-dbus
build.args      V=1

if {${os.subplatform} eq "macosx" && ${os.major} <= 10 && (${configure.build_arch} ni [list ppc ppc64])} {
    depends_lib   port:libsdl2-snowleopard
    distfiles
    patchfiles
    use_configure no
    build {}
    destroot {
        xinstall -d ${destroot}${prefix}/share/doc/${name}
        system "echo $name is a stub port > ${destroot}${prefix}/share/doc/${name}/README"
    }
} elseif {${os.subplatform} eq "macosx" && ${os.major} <= 12 && (${configure.build_arch} ni [list ppc ppc64])} {
    # Patch to revert fix for issue: https://github.com/libsdl-org/SDL/issues/6948
    # Necessary to fix builds for 10.7/10.8
    patchfiles-append patch-revert-issue-6948.diff
} else {
    conflicts libsdl2-snowleopard

platform macosx {
    if {${configure.build_arch} ni [list ppc ppc64]} {
        # Uses ObjC ARC, which can't be used with the legacy runtime (as
        # used on 32-bit) before LLVM 3.8, which is roughly Xcode 7.3.
        compiler.blacklist *gcc*
        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 < 211}
        }
    } else {
        PortGroup conflicts_build 1.0

        # These are only for Darwin PowerPC:
        compiler.blacklist *clang* *gcc-4.*
        patchfiles-append \
            0001-Fixes-for-PowerPC.patch \
            0002-hidapi-revert-a-commit-that-triggers-ICE-with-GCC.patch

        conflicts_build mesa
        # We have to disable joystick, until GCC upstream fixes this: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105522
        # Unlike with hidapi, there is no clear solution otherwise.
        configure.args-append \
                          --disable-joystick \
                          --disable-video-cocoa
    }
}

variant x11 description "Use X11 back-end" {
    configure.args-delete --without-x
    configure.args-append --enable-video-x11 \
                          --x-includes=${prefix}/include \
                          --x-libraries=${prefix}/lib \
                          --disable-x11-shared
    depends_lib-append \
        port:xorg-libX11 \
        port:xorg-libXcursor \
        port:xorg-libXext \
        port:xorg-libXi \
        port:xorg-libXinerama \
        port:xorg-libXrandr \
        port:xorg-libXScrnSaver \
        port:xorg-libXxf86vm \
        port:xrender

    patchfiles-append \
        0003-fix-x11.diff

    # Config header hardcodes wrong paths.
    post-patch {
        reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/include/SDL_config_macosx.h ${worksrcpath}/src/video/x11/SDL_x11opengl.c
    }
}
# On Darwin PowerPC Cocoa video backend is broken, so use X11 instead.
if {${os.subplatform} ne "macosx" || (${os.subplatform} eq "macosx" && ${configure.build_arch} in [list ppc ppc64])} {
    default_variants +x11
}

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

}
