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

# Keep the wine-stable, wine-devel and wine-crossover portfiles as similar as possible.

github.setup                wine-mirror wine 10.18 wine-
github.tarball_from         archive
name                        wine-devel
conflicts                   wine-stable wine-staging wine-crossover
set my_name                 wine
revision                    0
# MacPorts does not support i386 on macOS Mojave and new WoW64 requires macOS Catalina 10.15.4
platforms                   {darwin >= 16 != 18.*}
set branch                  [lindex [split ${version} .] 0].x
license                     LGPL-2.1+
categories                  emulators
supported_archs             i386 x86_64
maintainers                 {@Gcenx gmail.com:gcenx83}
homepage                    https://www.winehq.org
dist_subdir                 ${my_name}

description \
    A Windows API implementation

long_description \
    Wine is a compatibility layer capable of running \
    Windows applications. \
    Instead of simulating internal Windows logic like \
    a virtual machine or emulator, Wine translates \
    Windows API calls into POSIX calls on-the-fly, \
    eliminating the performance and memory penalties \
    of other methods and allowing you to cleanly \
    integrate Windows applications into your desktop.

checksums \
    ${distname}${extract.suffix} \
    rmd160  c11be56aa7d3380805d2fdb4c94dbf516615a7ef \
    sha256  3c0f135d78aaded9bd1368eeb74b2a13906eaba1647dc92e13fcce3c0468461a \
    size    53449832

depends_build \
    port:bison \
    bin:flex:flex \
    port:gettext \
    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.4 \
    port:mingw-w64-wine-mono-10.3.0

post-extract {
    # https://gitlab.winehq.org/wine/wine/-/commit/c7a97b5d5d56ef00a0061b75412c6e0e489fdc99
    reinplace -q "/PKG_CONFIG_LIBDIR/d"  ${worksrcpath}/configure
    reinplace -q "/PKG_CONFIG_LIBDIR/d"  ${worksrcpath}/configure.ac
}

patch.pre_args-replace -p0 -p1

patchfiles-append \
    0001-winehq_macos_hacks.diff

# https://bugs.winehq.org/show_bug.cgi?id=58916
patchfiles-append \
    bugzilla_58916.diff

# 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

configure.args.x86_64 \
    --enable-win64

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

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

configure.env-append        ac_cv_lib_soname_vulkan=

# We need to tell the linker to add MacPorts to the rpath stack.
configure.ldflags-append    -Wl,-rpath,${compiler.library_path}

# wine requires clang >= 3.8
# FSF GCC cannot compile code using Apple's "blocks" language extensions
compiler.blacklist-append   {*gcc*} {clang < 800} {macports-clang-3.*}

subport wine-staging {
    conflicts               wine-stable wine-devel wine-crossover
    set staging_version     ${version}
    set wine_staging_distfile v${staging_version}${extract.suffix}
    distfiles-append        ${wine_staging_distfile}:stagingsource
    master_sites-append     https://github.com/wine-staging/wine-staging/archive/:stagingsource
    worksrcdir              wine-${distname}
    extract.rename          no

    checksums-append \
        ${wine_staging_distfile} \
        rmd160  027c17381345b113b10d50c805292ffa38dee526 \
        sha256  e8f3a522e9c02b2caff15d857ab12071f10a47101f68b8d2b17c846aec2f4bb9 \
        size    9450101

    depends_patch-append    port:autoconf

    patchfiles-append       1001-staging-macos_hacks.diff

    set py_ver              3.13
    set py_ver_nodot        [string map {. {}} ${py_ver}]
    depends_patch-append    port:python${py_ver_nodot}

    # Applying staging before other patchfiles
    pre-patch {
        system -W ${worksrcpath} \
            "${frameworks_dir}/Python.framework/Versions/${py_ver}/bin/python3 ${workpath}/wine-staging-${staging_version}/staging/patchinstall.py --all -W ntdll-Syscall_Emulation"
    }
}

# Makes destroot take significantly longer
variant dev description "Install ${subport} development environment" {
    configure.optflags-prepend -g
    build.target            all
    destroot.target-append  install-dev
}

variant gphoto description "Build ${subport} with support for digital cameras" {
    depends_lib-append      port:libgphoto2
    configure.args-replace  --without-gphoto --with-gphoto
}

variant ffmpeg description "Build ${subport} with FFmpeg-based implementation for the MF byte stream handlers" {
    # Use the same version of ffmpeg as gstreamer1-gst-libav
    set ffmpeg_ver          6
    depends_lib-append      port:ffmpeg${ffmpeg_ver}
    configure.args-replace  --without-ffmpeg --with-ffmpeg
    configure.pkg_config_path   ${prefix}/libexec/ffmpeg${ffmpeg_ver}/lib/pkgconfig

    notes-append "
        \n
        The new implementation can be enabled by setting the DWORD value:
        \n
        DisableGstByteStreamHandler = 1
        \n
        in the HKCU\Software\Wine\MediaFoundation registry key.
    "
}

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
}

variant kerberos description "Build ${subport} with Kerberos, for network authentication protocol support" {
    depends_lib-append      port:kerberos5
    configure.args-replace  --without-krb5 --with-krb5
}

if {${os.major} < 19} {
    if {${os.major} == 16} {
        # Using the 10.13 SDK as that's what CodeWeavers tests against for i386
        configure.sdk_version   10.13
        if {${configure.sdkroot} eq ""} {
            pre-fetch {
                error "Building ${subport} @${version} requires the MacOSX10.13.sdk to be present in ${developer_dir}/SDKs/"
            }
        }
    }

    default_variants            +universal

    if {${universal_possible} && [variant_isset universal]} {
        configure.args.i386     --with-wine64=${workpath}/${worksrcdir}-x86_64

        notes-append "
            \n
            Wine supports both 32-bit and 64-bit now. It is compatible with your\
            existing 32-bit wine prefix, but it will now default to 64-bit when you\
            create a new wine prefix. The architecture can be selected using the\
            WINEARCH environment variable which can be set to either \"win32\" or\
            \"win64\".
            \n
            To create a new pure 32-bit prefix, you can run:
                \$ WINEARCH=win32 WINEPREFIX=~/.wine32 winecfg
            \n
            See the Wine FAQ for details: https://wiki.winehq.org/FAQ#Wineprefixes
        "
    } else {
        supported_archs         i386
    }
}

if {${os.major} > 18} {
    depends_lib-append          path:lib/libMoltenVK.dylib:MoltenVK-latest

    configure.args.x86_64-append --enable-archs=i386,x86_64
    configure.args-replace      --without-vulkan --with-vulkan

    # Setting an older deployment target avoids build error with MacOSX15.sdk
    # https://gitlab.winehq.org/wine/wine/-/merge_requests/5935#note_74758
    macosx_deployment_target    10.15

    if {${os.arch} ne "arm"} {
        default_variants    +ffmpeg +gstreamer
    }
}

triplet.add_build           cross
triplet.add_host            none

# 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          -O2
configure.env-append        "CROSSCFLAGS=${configure.optflags}"

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

post-destroot {
    set docdir ${prefix}/share/doc/${my_name}
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} \
        ANNOUNCE.md \
        AUTHORS \
        COPYING.LIB \
        LICENSE \
        README.md \
        ${destroot}${docdir}
}

pre-activate {
    if {${os.major} == 19 && ${os.minor} < 4 && ${os.platform} eq "darwin"} {
        ui_error "You must first update to macOS 10.15.4."
        return -code error "You must first update to macOS 10.15.4."
    }
}
