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

name                        game-porting-toolkit
set my_name                 wine
version                     1.1
set my_name                 wine
revision                    1
platforms                   {darwin any >= 23}
license                     LGPL-2.1+
categories                  devel

supported_archs             x86_64
maintainers                 {@Gcenx gmail.com:gcenx83}
homepage                    https://developer.apple.com/games
distname                    crossover-sources-22.1.1
dist_subdir                 ${my_name}
worksrcdir                  sources/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.

master_sites                https://media.codeweavers.com/pub/crossover/source/

checksums \
    rmd160  e514c407e3b7f75339a60fac84360f00791e97bb \
    sha256  cdfe282ce33788bd4f969c8bfb1d3e2de060eb6c296fa1c3cdf4e4690b8b1831 \
    size    147879629

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 \
    path:lib/pkgconfig/gnutls.pc:gnutls \
    port:libpcap \
    port:libsdl2

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

patch.pre_args-replace -p0 -p1

# Patches made from game-porting-toolkit commits
patchfiles-append \
    0001-game_porting_toolkit_1.0_beta1.diff \
    0002-game_porting_toolkit_1.0_beta2.diff \
    0004-game_porting_toolkit_1.0_beta4.diff \
    0005-game_porting_toolkit_1.1.diff

# Additional patchsets
patchfiles-append \
    1001-configure-remove-cross_compiling-check.diff \
    1002-server-remove-cxhack-21217.diff \
    1003-default-win10-19043.diff \
    1004-mscoree-update-wine-mono-to-7.4.1.diff \
    1005-ntdll-d3dmetal-env.diff \
    1006-build_fixes.diff

post-patch {
    reinplace -q "s|;(cw)||g"               ${worksrcpath}/loader/wine.inf.in
}

# 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 \
    --enable-win64 \
    --disable-tests \
    --disable-winedbg \
    --disable-winemenubuilder \
    --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

# 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"} {
    pre-fetch {
        error "Building ${name} requires the MacOSX${configure.sdk_version}.sdk to be present in ${developer_dir}/SDKs/"
    }
}

# 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      port:gstreamer1 port:gstreamer1-gst-plugins-base port:gstreamer1-gst-plugins-good \
        port:gstreamer1-gst-plugins-bad port:gstreamer1-gst-plugins-ugly port:gstreamer1-gst-libav
    configure.args-replace  --without-gstreamer --with-gstreamer
}

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
compiler.limit_flags        yes
muniversal.arch_flag        no
muniversal.arch_compiler    yes

configure.ldflags-delete    -L${compiler.library_path}
configure.optflags          -O3

# Compiler flags for PE binaries.
# GCC 15 defaults to gnu23 causing build failures, so force gnu17
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

    file copy ${destroot}${prefix}/libexec/${name}/bin/wine64           ${destroot}${prefix}/libexec/${name}/bin/wine
    file copy ${destroot}${prefix}/libexec/${name}/bin/wine64-preloader ${destroot}${prefix}/libexec/${name}/bin/wine-preloader

    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
}

platform darwin arm {
    depends_run-append      port:d3dmetal
}

# MacPorts arm buildbots won't build for x86_64 so don't error out on Intel so the Intel buildbot can build it
platform darwin i386 {
    if {${os.major} >= 23} {
        set is_rosetta2 [exec sysctl -in sysctl.proc_translated]
        if { ${is_rosetta2} != 1 } {
            notes-append "${name} isn't designed for Intel hardware"
        } else {
            notes-append "${name} requires a manual install of the d3dmetal port"
        }
    }
}

livecheck   none
