# -*- 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           cmake   1.1
PortGroup           github  1.0
PortGroup           legacysupport 1.1

# strnlen
legacysupport.newest_darwin_requires_legacy 10

epoch               3
github.setup        FreeRDP FreeRDP 3.17.2
revision            0
categories          net
license             Apache
maintainers         nomaintainer
description         A Remote Desktop Protocol implementation
long_description    FreeRDP is a free implementation of the Remote Desktop Protocol (RDP), \
                    released under the Apache license.

homepage            https://www.freerdp.com

checksums           rmd160  ee60f05ec2fb784da454dfac9e441962d4d86823 \
                    sha256  c42c712ad879bf06607b78b8c3fad98e08c82f73f4e0bc1693552900041e692a \
                    size    10556445
github.tarball_from releases

depends_build-append \
                    path:bin/pkg-config:pkgconfig

depends_lib-append  path:lib/pkgconfig/cairo.pc:cairo \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    path:lib/pkgconfig/libusb-1.0.pc:libusb \
                    path:lib/libssl.dylib:openssl \
                    port:xorg-libX11 \
                    port:xorg-libXcursor \
                    port:xorg-libXext \
                    port:xorg-libXi \
                    port:xorg-libXinerama \
                    port:xorg-libxkbfile \
                    port:xorg-libXrandr \
                    port:xorg-libXv \
                    port:xrender \
                    port:zlib

worksrcdir          freerdp-${version}

if {${os.platform} eq "darwin" && ${os.major} < 14} {
    # winpr/libwinpr/thread/thread.c:281:38: error: 'ENOTRECOVERABLE' undeclared
    patchfiles-append \
        patch-errno.diff

    # AVAudioBuffer was introduced in OS X 10.10
    configure.args-append \
        -DCHANNEL_RDPSND=OFF
}

# Disable check on cmake build type so we can control compiler flags
patchfiles-append patch-cmake_ignore_build_type.diff

# A common bug
patchfiles-append patch-fix-cinttypes.diff

post-patch {
    if {[string match "*gcc*" ${configure.compiler}]} {
        reinplace "s:-fobjc-nonfragile-abi::" \
            ${worksrcpath}/client/Mac/cli/CMakeLists.txt \
            ${worksrcpath}/client/Mac/CMakeLists.txt
    }
}

use_xcode           yes

# Target "cmTC_c199e" requires the language dialect "C11"
compiler.c_standard     2011
compiler.cxx_standard   2017

# Enabled LTO breaks linking!
pre-configure {
    configure.args-append \
                    -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=OFF
}

# Related to state on macOS: https://github.com/FreeRDP/FreeRDP/issues/10558
# Server is non-functional, even if it compiles, thus -DWITH_SERVER=OFF
# SDL client is currently recommended over native Mac client
configure.args-append \
                    -DENABLE_WARNING_ERROR=OFF \
                    -DWITH_ALSA=OFF \
                    -DWITH_AVX2=OFF \
                    -DWITH_CAIRO=ON \
                    -DWITH_CCACHE=OFF \
                    -DWITH_CLIENT_MAC=OFF \
                    -DWITH_CLIENT_SDL_VERSIONED=ON \
                    -DWITH_CLIENT_SDL2=OFF \
                    -DWITH_CLIENT_SDL3=OFF \
                    -DWITH_CUPS=ON \
                    -DWITH_FAAC=OFF \
                    -DWITH_FAAD2=OFF \
                    -DWITH_FFMPEG=OFF \
                    -DWITH_JPEG=ON \
                    -DWITH_LAME=OFF \
                    -DWITH_MANPAGES=OFF \
                    -DWITH_PKCS11=OFF \
                    -DWITH_PULSE=OFF \
                    -DWITH_SERVER=OFF \
                    -DWITH_SIMD=OFF \
                    -DWITH_SOXR=OFF \
                    -DWITH_WAYLAND=OFF \
                    -DWITH_X11=ON

if {${os.platform} eq "darwin" && ${os.major} < 11} {
    # Do not use non-existing framework:
    patchfiles-append \
                    patch-audin-mac.diff
    # The code using libdispatch was added in https://github.com/FreeRDP/FreeRDP/commit/9bd13c25c96610b847c43333513cae5e2476f447
    # and already used unsupported features. Disabling timer is a legit option, do it.
    patchfiles-append \
                    patch-disable-unsupported-dispatch.diff

    # cups in 10.6.x is too old:
    # Could NOT find Cups: Found unsuitable version "1.4.4"
    configure.args-replace \
                    -DWITH_CUPS=ON \
                    -DWITH_CUPS=OFF
}

# ffmpeg only supports VDA (video hardware acceleration, mostly H264) on 10.6.3+ (#48397)
if {${os.platform} ne "darwin" || ${os.major} > 10 || (${os.major} == 10 && ${os.minor} >= 3)} {
    set ffmpeg_ver          6
    configure.args-replace  -DWITH_FFMPEG=OFF -DWITH_FFMPEG=ON
    depends_lib-append      port:ffmpeg${ffmpeg_ver}
    configure.pkg_config_path \
                            ${prefix}/libexec/ffmpeg${ffmpeg_ver}/lib/pkgconfig
}

# I've seen manpage building fail for unclear reasons, so provide a variant to turn it off
variant docs description {build the manpages} {
    depends_build-append \
                    port:docbook-xsl-nons \
                    port:libxslt
    configure.args-replace \
                    -DWITH_MANPAGES=OFF \
                    -DWITH_MANPAGES=ON
}
default_variants-append +docs

variant pulseaudio description {build PulseAudio support} {
    depends_lib-append \
                    port:pulseaudio
    configure.args-replace \
                    -DWITH_PULSE=OFF \
                    -DWITH_PULSE=ON
}

variant codecs description {support MP3, FAAD2, FAAC and SOXR codecs} {
    depends_lib-append \
                    port:faac \
                    port:faad2 \
                    port:lame \
                    port:soxr
    configure.args-replace \
                    -DWITH_FAAC=OFF \
                    -DWITH_FAAC=ON \
                    -DWITH_FAAD2=OFF \
                    -DWITH_FAAD2=ON \
                    -DWITH_LAME=OFF \
                    -DWITH_LAME=ON \
                    -DWITH_SOXR=OFF \
                    -DWITH_SOXR=ON
}

build.post_args-append -k

set app_dir         ${applications_dir}/MacFreeRDP.app/Contents
variant cocoa description {build native macOS/Cocoa client} {
    configure.args-replace \
                    -DWITH_CLIENT_MAC=OFF \
                    -DWITH_CLIENT_MAC=ON

    post-destroot {
        xinstall -d ${destroot}${app_dir}
        move ${destroot}${prefix}/Info.plist ${destroot}${prefix}/PkgInfo \
            ${destroot}${app_dir}

        xinstall -d ${destroot}${app_dir}/MacOS
        move ${destroot}${prefix}/bin/MacFreeRDP ${destroot}${app_dir}/MacOS

        xinstall -d ${destroot}${app_dir}/Resources
        move ${destroot}${prefix}/share/FreeRDP.icns \
            {*}[glob ${destroot}${prefix}/share/*.nib] \
            ${destroot}${app_dir}/Resources

        xinstall -d ${destroot}${app_dir}/Resources/en.lproj
        move {*}[glob ${destroot}${prefix}/share/*.strings] \
            ${destroot}${app_dir}/Resources/en.lproj
    }
}

variant sdl2 description {build SDL2 client} {
    depends_lib-append port:libsdl2_ttf
    configure.args-replace \
                    -DWITH_CLIENT_SDL2=OFF \
                    -DWITH_CLIENT_SDL2=ON
}

variant sdl3 description {build SDL3 client} {
    depends_lib-append port:SDL3_ttf
    configure.args-replace \
                    -DWITH_CLIENT_SDL3=OFF \
                    -DWITH_CLIENT_SDL3=ON
}
default_variants-append +sdl3
