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

# Please keep the wine, wine-devel and wine-crossover ports as similar as possible.

# Crossover-specific bug reports are accepted at info@codeweavers.com

name                        wine-crossover
conflicts                   wine wine-devel
set my_name                 wine
version                     18.5.0
revision                    1
license                     LGPL-2.1+
categories                  x11
maintainers                 {ryandesign @ryandesign} {jeremyhu @jeremyhu} openmaintainer
homepage                    https://www.codeweavers.com/products/more-information/source
platforms                   darwin
distname                    crossover-sources-${version}
dist_subdir                 ${my_name}
set wine_distfile           ${distname}${extract.suffix}
set wine_gecko_version      2.47
set wine_gecko_distfile     wine_gecko-${wine_gecko_version}-x86.msi
set wine_gecko64_distfile   wine_gecko-${wine_gecko_version}-x86_64.msi
set wine_mono_version       4.7.5
set wine_mono_distfile      wine-mono-${wine_mono_version}.msi
set wine_version            4.0
worksrcdir                  sources/wine
use_parallel_build          yes

description                 A Windows API implementation (Wine ${wine_version} \
                            with CodeWeavers modifications for CrossOver)

long_description            Wine is a compatibility layer capable of running \
                            Windows applications. \
                            This version contains the CodeWeavers patches for \
                            CrossOver. \
                            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.

master_sites                http://ftp.codeweavers.com/pub/crossover/source/:winesource \
                            http://dl.winehq.org/wine/wine-gecko/${wine_gecko_version}/:winegecko \
                            sourceforge:project/wine/Wine%20Gecko/${wine_gecko_version}/:winegecko \
                            http://dl.winehq.org/wine/wine-mono/${wine_mono_version}/:winemono \
                            sourceforge:project/wine/Wine%20Mono/${wine_mono_version}/:winemono

extract.only                ${wine_distfile}

distfiles                   ${wine_distfile}:winesource \
                            ${wine_gecko_distfile}:winegecko \
                            ${wine_gecko64_distfile}:winegecko \
                            ${wine_mono_distfile}:winemono

checksums                   ${wine_distfile} \
                            rmd160  8e2c9e4a5321685e2fe55e988f7d8927d251c12d \
                            sha256  ca4027d6c2f6f85f9baef0e19e50682ebadcaa3e35df883089f9342c4e3384fe \
                            size    74953768 \
                            ${wine_gecko_distfile} \
                            rmd160  abf7cc78b49dd0623bc8fe87ae0e32bb8694e13d \
                            sha256  3b8a361f5d63952d21caafd74e849a774994822fb96c5922b01d554f1677643a \
                            size    49266176 \
                            ${wine_gecko64_distfile} \
                            rmd160  254da21cb2503f20d065167b385b3e83ea3ab327 \
                            sha256  c565ea25e50ea953937d4ab01299e4306da4a556946327d253ea9b28357e4a7d \
                            size    50806272 \
                            ${wine_mono_distfile} \
                            rmd160  0fa40e1701cd76e5590929beb20a3cb8b2c3184d \
                            sha256  154d68d476cdedef56f159d837fbb5eef9358a9f85de89f86c189ec4da004b3f \
                            size    56470528

# Note: Most of the X11 libs are dlopen()'d
depends_lib                 port:fontconfig \
                            port:freetype \
                            port:gstreamer1 \
                            port:gstreamer1-gst-plugins-base \
                            path:include/turbojpeg.h:libjpeg-turbo \
                            port:lcms2 \
                            port:libiconv \
                            port:libpcap \
                            port:libpng \
                            port:libxml2 \
                            port:libxslt \
                            port:ncurses \
                            path:lib/pkgconfig/gnutls.pc:gnutls \
                            port:openal-soft \
                            port:tiff \
                            port:zlib

depends_build               port:bison \
                            port:flex \
                            port:pkgconfig

post-extract {
    set fp [open ${worksrcpath}/dlls/appwiz.cpl/addons.c r]
    set file_contents [read ${fp}]
    close ${fp}
    set ok yes
    foreach addon {gecko mono wine} {
        if {${addon} eq {wine}} {
            set fp [open ${worksrcpath}/VERSION r]
            set file_contents [read ${fp}]
            close ${fp}
            set addon_version_var wine_version
            set addon_version_re {^Wine version ([^[:space:]]+)$}
        } else {
            set addon_version_var wine_${addon}_version
            set addon_version_re "^#define [string toupper $addon]_VERSION \"(\[^\"\]+)\"$"
        }
        set addon_version_in_port [set ${addon_version_var}]
        if {[regexp -lineanchor ${addon_version_re} ${file_contents} -> addon_version_in_file] == 1} {
            if {${addon_version_in_port} ne ${addon_version_in_file}} {
                set ok no
                ui_error "${addon_version_var} should be ${addon_version_in_file} not ${addon_version_in_port}"
            }
        } else {
            set ok no
            ui_error "Could not determine what ${addon_version_var} should be"
        }
    }
    if {!${ok}} {
        return -code error
    }
}

patchfiles                  BOOL.patch \
                            cups_headers.patch \
                            mach_machine.patch \
                            patch-include-distversion.h.diff

pre-configure {
    if {[variant_exists universal] && [variant_isset universal]} {
        foreach arch ${universal_archs_to_use} {
            file mkdir ${worksrcpath}-${arch}
        }

        configure.cmd ${worksrcpath}/configure
    }
}

# 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              --without-alsa \
                            --without-capi \
                            --with-cms \
                            --with-coreaudio \
                            --with-cups \
                            --with-curses \
                            --without-dbus \
                            --with-fontconfig \
                            --with-freetype \
                            --without-gettext \
                            --without-gettextpo \
                            --without-gphoto \
                            --with-glu \
                            --with-gnutls \
                            --without-gsm \
                            --with-gstreamer \
                            --without-hal \
                            --with-jpeg \
                            --without-ldap \
                            --without-mpg123 \
                            --without-netapi \
                            --with-openal \
                            --with-opencl \
                            --with-opengl \
                            --without-osmesa \
                            --without-oss \
                            --with-pcap \
                            --with-png \
                            --with-pthread \
                            --without-pulse \
                            --without-sane \
                            --with-tiff \
                            --without-udev \
                            --without-v4l \
                            --without-xcomposite \
                            --without-xcursor \
                            --without-xinerama \
                            --without-xinput \
                            --without-xinput2 \
                            --with-xml \
                            --without-xrandr \
                            --without-xrender \
                            --without-xshape \
                            --without-xshm \
                            --with-xslt \
                            --without-xxf86vm \
                            --with-zlib \
                            --without-x

# Wine has a native macOS driver since 1.5.26 so X11 is optional
variant x11 {
    depends_lib-append          port:libGLU \
                                port:mesa \
                                port:xorg-libX11 \
                                port:xorg-libXcomposite \
                                port:xorg-libXcursor \
                                port:xorg-libXext \
                                port:xorg-libXi \
                                port:xorg-libXinerama \
                                port:xorg-libXrandr \
                                port:xorg-libXxf86vm \
                                port:xorg-libsm \
                                port:xorg-xorgproto \
                                port:xrender

    configure.args-delete       --without-osmesa \
                                --without-xcomposite \
                                --without-xcursor \
                                --without-xinerama \
                                --without-xinput \
                                --without-xrandr \
                                --without-xrender \
                                --without-xshape \
                                --without-xxf86vm \
                                --without-x

    configure.args-append       --with-osmesa \
                                --with-xcomposite \
                                --with-xcursor \
                                --with-xinerama \
                                --with-xinput \
                                --with-xrandr \
                                --with-xrender \
                                --with-xshape \
                                --with-xxf86vm \
                                --with-x \
                                --x-include=${prefix}/include \
                                --x-lib=${prefix}/lib
}

if {${os.platform} ne "darwin" || (${os.major} >= 9 && ${os.major} <= 17)} {
    default_variants        +x11
}

configure.universal_args

set merger_build_args(x86_64)     "dlldir=\\\\\${libdir}/wine64"
set merger_destroot_args(x86_64)  "dlldir=\\\\\${libdir}/wine64"
set merger_configure_args(x86_64) "--enable-win64 --libdir=${prefix}/lib"
set merger_configure_args(i386)   --with-wine64=${workpath}/${worksrcdir}-x86_64

if {${build_arch} eq "x86_64" && ${os.major} < 18} {
    default_variants            +universal
}

if {${universal_possible} && [variant_isset universal]} {
    supported_archs         i386 x86_64
} else {
    supported_archs         i386
}

# error: Xcode 3.x cannot build 16-bit code correctly
compiler.blacklist-append   {gcc-4.2 < 5600}

# llvm-gcc-4.2 doesn't respect force_align_arg_pointer; wine builds but fails to run
# http://bugs.winehq.org/show_bug.cgi?id=28030
compiler.blacklist-append   *llvm-gcc-4.2

# clang < 3.1 fails to build wine with "error: invalid operand for instruction lretw"
# http://bugs.winehq.org/show_bug.cgi?id=32872
# clang < 3.2 have code generation problems:
# http://llvm.org/bugs/show_bug.cgi?id=9707
compiler.blacklist-append   {clang < 500}

# FSF GCC cannot compile code using Apple's "blocks" language extensions
compiler.blacklist-append   macports-gcc-*

if {${universal_possible} && [variant_isset universal]} {
    # wine for 64-bit requires clang >= 3.8
    compiler.blacklist-append   {clang < 800} \
                                gcc-4.0 \
                                gcc-4.2 \
                                apple-gcc-4.2 \
                                macports-clang-3.3 \
                                macports-clang-3.4 \
                                macports-clang-3.7
    compiler.fallback-append    macports-clang-5.0
}

platform darwin {
    # On macOS, Core Text is used instead of fontconfig since Wine 1.5.10
    depends_lib-delete          port:fontconfig
    configure.args-delete       --with-fontconfig
    configure.args-append       --without-fontconfig

    # Wine uses the native Security framework on macOS since Wine 1.3.16
    depends_lib-delete          path:lib/pkgconfig/gnutls.pc:gnutls
    configure.args-delete       --with-gnutls
    configure.args-append       --without-gnutls

    # Vulkan is supported on macOS through MoltenVK since Wine 3.13, but because
    # it uses Metal it only works on 64-bit, and because it only builds with
    # Xcode 9 or later it requires Sierra or later.
    if {${os.major} >= 16 && ${universal_possible} && [variant_isset universal]} {
        depends_lib-append                      port:MoltenVK
        depends_skip_archcheck-append           MoltenVK
        lappend merger_configure_args(x86_64)   --with-vulkan
        lappend merger_configure_args(i386)     --without-vulkan
    } else {
        configure.args-append   --without-vulkan
    }

    if {${os.major} < 10} {
        configure.args-delete   --with-opencl
    }

    notes {
        When using the Mac Driver, it might be useful to enable RetinaMode\
        when you're using a high DPI monitor. To do that, open up Wine's\
        Registry Editor:

          wine regedit

        and then navigate to:

          [HKEY_CURRENT_USER\Software\Wine\Mac Driver]

        and then set "RetinaMode" to "y". You may also want to adjust your\
        DPI under the Graphics tab in winecfg.}

    if {${os.major} < 9} {
        known_fail yes
        archive_sites
        distfiles
        depends_build
        depends_lib
        pre-fetch {
            ui_error "${name} @${version} requires Mac OS X 10.5 or later."
            return -code error "incompatible Mac OS X version"
        }
    } elseif {${os.major} > 17} {
        known_fail yes
        archive_sites
        distfiles
        depends_build
        depends_lib
        pre-fetch {
            ui_error "${name} cannot be built on macOS 10.14 or later."
            return -code error "incompatible macOS version"
        }
    }
}

if {${os.endian} eq "big"} {
    known_fail yes
    archive_sites
    distfiles
    depends_build
    depends_lib
    pre-fetch {
        ui_error "${name} can only be used on an Intel Mac or other computer with a little-endian processor."
        return -code error "incompatible processor"
    }
}

post-destroot {
    xinstall -d ${destroot}${prefix}/share/wine/gecko
    xinstall -m 0644 -W ${distpath} ${wine_gecko_distfile} ${wine_gecko64_distfile} ${destroot}${prefix}/share/wine/gecko

    xinstall -d ${destroot}${prefix}/share/wine/mono
    xinstall -m 0644 ${distpath}/${wine_mono_distfile} ${destroot}${prefix}/share/wine/mono

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

if {${universal_possible} && [variant_isset universal]} {
    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\".

        To create a new pure 32-bit prefix, you can run:
            \$ WINEARCH=win32 WINEPREFIX=~/.wine32 winecfg

        See the Wine FAQ for details: https://wiki.winehq.org/FAQ#Wineprefixes
    "
}

livecheck.type              regex
livecheck.url               http://ftp.codeweavers.com/pub/crossover/source/
livecheck.regex             crossover-sources-(\[0-9.\]+)\\.tar