# -*- 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 11.0 wine-
github.tarball_from         archive
name                        wine-stable
conflicts                   wine-devel wine-staging wine-crossover
revision                    0
platforms                   {darwin > 15}
set branch                  [lindex [split ${version} .] 0].0
license                     LGPL-2.1+
categories                  emulators
supported_archs             i386 x86_64
maintainers                 {@Gcenx}
homepage                    https://www.winehq.org
dist_subdir                 wine

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  b700678636ea8a9a48f650d49d06eb10ed3a97bc \
    sha256  f09e8153aa46a581d2b56a5b1363b04832070b9409d9244a68cf482b243ff14a \
    size    53784681

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

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

depends_run \
    port:mingw-w64-wine-gecko-2.47.4 \
    port:mingw-w64-wine-mono-10.4.1

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

# Upstream bug fixes here
patchfiles-append \
    llvm-mingw_fixes.patch

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

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 \
    --with-inotify \
    --without-krb5 \
    --with-mingw=${prefix}/libexec/llvm-mingw/bin/clang \
    --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.*}

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

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

    pre-configure {
        configure.env-append    "FFMPEG_CFLAGS=[exec ${frameworks_dir}/GStreamer.framework/Commands/pkg-config --cflags libavutil libavformat libavcodec]"
        configure.env-append    "FFMPEG_LIBS=[exec ${frameworks_dir}/GStreamer.framework/Commands/pkg-config --libs libavutil libavformat libavcodec]"
        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 gstreamer-tag-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 gstreamer-tag-1.0]"
    }

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

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} >= 24} {
    # https://gitlab.winehq.org/wine/wine/-/merge_requests/5935#note_74758
    macosx_deployment_target    14.0
}

if {${os.major} >= 19} {
    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
    default_variants            +gstreamer
}

if {${os.major} < 19} {
    # llvm-mingw only is compiled for arm64 & x86_64
    depends_skip_archcheck-append llvm-mingw

    # CodeWeavers only tests i386 using MacOSX10.13.sdk
    configure.sdk_version       10.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
    }

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

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/${subport}
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} \
        ANNOUNCE.md \
        AUTHORS \
        COPYING.LIB \
        LICENSE \
        README.md \
        ${destroot}${docdir}

    if {[variant_isset gstreamer]} {
        system -W ${destroot}${compiler.library_path}/wine/x86_64-unix "/usr/bin/install_name_tool -delete_rpath ${compiler.library_path} winedmo.so"
        system -W ${destroot}${compiler.library_path}/wine/x86_64-unix "/usr/bin/install_name_tool -delete_rpath ${compiler.library_path} winegstreamer.so"
        system -W ${destroot}${compiler.library_path}/wine/x86_64-unix "/usr/bin/codesign --force --sign - winedmo.so"
        system -W ${destroot}${compiler.library_path}/wine/x86_64-unix "/usr/bin/codesign --force --sign - winegstreamer.so"
    }
}
