# -*- 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
PortGroup                   muniversal 1.1

# Using custom mirror to avoid embedding huge patchfiles
github.setup                gcenx wine 3a8755fc3e6067fb55b5156812b7418696ef0407
github.tarball_from         archive
name                        game-porting-toolkit
version                     1.1
revision                    7
platforms                   {darwin any >= 23}
license                     LGPL-2.1+
categories                  devel
supported_archs             x86_64
maintainers                 {@Gcenx}
homepage                    https://developer.apple.com/games
dist_subdir                 wine

description                 Game Porting Toolkit ${version}
long_description            Use the game porting toolkit to eliminate months of up-front work and \
                            evaluate how well your game could run on Mac before writing any code.

checksums \
    rmd160  505178f6a42b53fe1c7297ba7ba2249fbf7d0b99 \
    sha256  972ff467cc73dcd3d69448f6baf013693c5841d28107959c266504058651c8c0 \
    size    46810271

depends_build \
    port:bison \
    bin:flex:flex \
    port:gettext \
    port:llvm-cx \
    port:mingw-w64 \
    path:bin/pkg-config:pkgconfig

depends_lib \
    port:freetype \
    port:gettext-runtime \
    port:gnutls-devel \
    port:libpcap \
    port:libsdl2

depends_run \
    port:d3dmetal \
    port:mingw-w64-wine-gecko-2.47.2 \
    port:mingw-w64-wine-mono-7.4.1

patch.pre_args-replace -p0 -p1

patchfiles-append \
    1001-configure-remove-cross_compiling-check.diff \
    1002-ntdll-d3dmetal-env.diff \
    1003-build_fixes.diff

configure.checks.implicit_function_declaration.whitelist-append \
    __clear_cache \
    fallocate \
    gethostbyaddr_r \
    gethostbyname_r \
    sched_setaffinity

# wine requires the program specified in INSTALL to create intermediate
# directories; /usr/bin/install doesn't.
# http://bugs.winehq.org/show_bug.cgi?id=35310
configure.install \
    ${worksrcpath}/tools/install-sh

# Installing into libexec to not confict with wine
configure.pre_args \
    --prefix=${prefix}/libexec/${name}

configure.pre_args.i386 \
    --with-wine64=${workpath}/${worksrcdir}-x86_64 \
    --enable-win32on64 \
    --disable-loader

configure.args.x86_64 \
    --enable-win64

configure.args \
    --without-alsa \
    --without-capi \
    --with-coreaudio \
    --with-cups \
    --without-dbus \
    --without-fontconfig \
    --with-freetype \
    --with-gettext \
    --without-gettextpo \
    --without-gphoto \
    --with-gnutls \
    --without-gssapi \
    --without-gstreamer \
    --without-inotify \
    --without-krb5 \
    --with-mingw \
    --without-netapi \
    --without-openal \
    --with-opencl \
    --with-opengl \
    --without-oss \
    --with-pcap \
    --with-pthread \
    --without-pulse \
    --without-sane \
    --with-sdl \
    --without-udev \
    --with-unwind \
    --without-usb \
    --without-v4l2 \
    --without-vulkan \
    --without-x

configure.args-append \
    --disable-tests \
    --disable-winedbg \
    --disable-winemenubuilder

# We need to tell the linker to add MacPorts & d3dmetal to the rpath stack.
configure.ldflags-append    -Wl,-rpath,${compiler.library_path} -Wl,-rpath,${prefix}/libexec/d3dmetal/external

# Xcode15 linker requires -ld_classic to enable legacy behaviours required to build wine
configure.ldflags-prepend   -Wl,-ld_classic

# Xcode15.3 default SDK causes the build to fail so fallback to a working SDK
configure.sdk_version       13
if {${configure.sdkroot} != "${developer_dir}/SDKs/MacOSX${configure.sdk_version}.sdk"} {
    depends_build-append    port:MacOSX${configure.sdk_version}.sdk
    configure.sdkroot       ${prefix}/Developer/SDKs/MacOSX${configure.sdk_version}.sdk
}

# Use an older deployment target to avoid new dyld behaviors.
# The custom compiler is too old to accept "13.0", so we use "10.14".
macosx_deployment_target    10.14

variant gstreamer description "Build ${subport} with GStreamer, for multimedia support" {
    depends_lib-append      path:Library/Frameworks/GStreamer.framework:gstreamer.framework
    configure.args-replace  --without-gstreamer --with-gstreamer

    pre-configure {
        configure.env-append    "GSTREAMER_CFLAGS=[exec ${frameworks_dir}/GStreamer.framework/Commands/pkg-config --cflags gstreamer-1.0 gstreamer-video-1.0 gstreamer-audio-1.0]"
        configure.env-append    "GSTREAMER_LIBS=[exec ${frameworks_dir}/GStreamer.framework/Commands/pkg-config --libs gstreamer-1.0 gstreamer-video-1.0 gstreamer-audio-1.0]"
    }

    post-configure {
        reinplace -q "s|Versions/1.0/lib/pkgconfig/../../lib|Libraries|g" ${worksrcpath}-i386/config.status
        reinplace -q "s|Versions/1.0/lib/pkgconfig/../../lib|Libraries|g" ${worksrcpath}-x86_64/config.status
    }
}

default_variants            +gstreamer

triplet.add_build           cross
triplet.add_host            none

configure.cc                ${prefix}/libexec/llvm-cx/bin/clang
configure.cxx               ${prefix}/libexec/llvm-cx/bin/clang++
configure.cmd               ${worksrcpath}/configure
configure.compiler.add_deps no

# macports-base injects a number of flags that now propagate to mingw-gcc this causes the build to fail since, wine-7.21
# https://gitlab.winehq.org/wine/wine/-/commit/b1f59bc679a8c2dea18a6789a5b9b1a1ae825129
compiler.limit_flags        yes
muniversal.arch_flag        no
muniversal.arch_compiler    yes
configure.ldflags-delete    -L${compiler.library_path}
configure.optflags          -O3
# gcc14 Sets -Werror=incompatible-pointer-types & -Werror=int-conversion by default
# gcc15 Sets -std=gnu23 by default
configure.env-append        "CROSSCFLAGS=${configure.optflags} -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -std=gnu17"

# Were only installing wine not the development files
destroot.target             install-lib

configure {
    set configure.dir ${worksrcpath}-x86_64
    portconfigure::configure_main

    configure.args.x86_64
    set configure.dir ${worksrcpath}-i386
    configure.args-delete --enable-win64
    configure.args ${configure.pre_args.i386} ${configure.args} ${configure.args.i386}
    portconfigure::configure_main
}

build {
    set build.dir ${worksrcpath}-x86_64
    portbuild::build_main

    set build.dir ${worksrcpath}-i386
    portbuild::build_main
}

destroot {
    set destroot.dir ${worksrcpath}-x86_64
    portdestroot::destroot_main

    set destroot.dir ${worksrcpath}-i386
    portdestroot::destroot_main
}

post-destroot {
    ln -sf ${prefix}/share/wine/gecko ${destroot}${prefix}/libexec/${name}/share/wine/gecko
    ln -sf ${prefix}/share/wine/mono ${destroot}${prefix}/libexec/${name}/share/wine/mono

    xinstall -W ${filespath} \
        gameportingtoolkit \
        gameportingtoolkit-no-esync \
        gameportingtoolkit-no-hud \
        ${destroot}${prefix}/bin/

    reinplace -W ${destroot}${prefix}/bin "s|@@PREFIX@@|${prefix}|g" \
        gameportingtoolkit \
        gameportingtoolkit-no-esync \
        gameportingtoolkit-no-hud

    if {[variant_isset gstreamer]} {
        system -W ${destroot}${prefix}/libexec/${name}/lib/wine/x86_64-unix "/usr/bin/install_name_tool -delete_rpath ${compiler.library_path} winegstreamer.so"
        system -W ${destroot}${prefix}/libexec/${name}/lib/wine/x86_64-unix "/usr/bin/install_name_tool -delete_rpath ${prefix}/libexec/d3dmetal/external winegstreamer.so"
        system -W ${destroot}${prefix}/libexec/${name}/lib/wine/x86_64-unix "/usr/bin/codesign --force --sign - winegstreamer.so"
        system -W ${destroot}${prefix}/libexec/${name}/lib/wine/x86_32on64-unix "/usr/bin/install_name_tool -delete_rpath ${compiler.library_path} winegstreamer.so"
        system -W ${destroot}${prefix}/libexec/${name}/lib/wine/x86_32on64-unix "/usr/bin/install_name_tool -delete_rpath ${prefix}/libexec/d3dmetal/external winegstreamer.so"
        system -W ${destroot}${prefix}/libexec/${name}/lib/wine/x86_32on64-unix "/usr/bin/codesign --force --sign - winegstreamer.so"
    }
}

platform darwin i386 {
    pre-fetch {
        if {${os.major} >= 23} {
            set is_rosetta2 [exec sysctl -in sysctl.proc_translated]
            if { ${is_rosetta2} != 1 } {
                error "${name} does not work on Intel hardware"
            }
        }
    }
}

livecheck   none
