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

# strnlen
legacysupport.newest_darwin_requires_legacy 10

# remember to bump version of wxWidgets-common
github.setup        wxWidgets wxWidgets 3.2.11 v
revision            0

set branch          [join [lrange [split ${version} .] 0 1] .]

name                wxgtk-${branch}

set installname     wxGTK
set wxtype          gtk3
wxWidgets.use       wxGTK-${branch}

categories          graphics devel
license             wxwidgets-3.1
maintainers         {@barracuda156 macos-powerpc.org:barracuda} openmaintainer

description         C++ framework for cross-platform GUI development
long_description    wxWidgets ${branch} is an open-source cross-platform C++ \
                    GUI framework for Mac OS, Unix, Linux, Windows.

homepage            https://www.wxwidgets.org

distname            wxWidgets-${version}
use_bzip2           yes

checksums           rmd160  0c23bdcf0758bcb7dc4b8007325d4a2b82f47470 \
                    sha256  6a129015bce2e914e4bf61ec4411854ad962801d47e92f2eb8340adb6a90af08 \
                    size    27561301
github.tarball_from releases

dist_subdir         wxWidgets/${version}

set selectdir       ${workpath}/select
select.group        wxWidgets
select.file         ${selectdir}/${subport}

compiler.c_standard   2011
compiler.cxx_standard 2011
compiler.thread_local_storage yes

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

depends_lib-append  path:lib/pkgconfig/cairo.pc:cairo \
                    port:curl \
                    port:expat \
                    path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
                    port:jbigkit \
                    port:libGLU \
                    port:libiconv \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:libmspack \
                    port:libpng \
                    port:libsdl2 \
                    port:libsdl2_mixer \
                    port:mesa \
                    port:pcre2 \
                    port:tiff \
                    port:xorg-libice \
                    port:xorg-libsm \
                    port:zlib

require_active_variants path:lib/pkgconfig/cairo.pc:cairo x11
require_active_variants gtk3 x11

depends_run         port:wxWidgets-common \
                    port:wxWidgets_select

post-patch {
    file mkdir ${selectdir}
    system "echo \"${wxWidgets.wxdir}/wx-config\n${wxWidgets.wxdir}/wxrc-${branch}\" > ${select.file}"
}

# Enabled precomp results in weird errors, at least on powerpc:
# https://github.com/wxWidgets/wxWidgets/pull/25321
configure.args-append \
                    -DCMAKE_INSTALL_PREFIX=${wxWidgets.prefix} \
                    -DwxBUILD_COMPATIBILITY=3.0 \
                    -DwxBUILD_MONOLITHIC=OFF \
                    -DwxBUILD_OPTIMISE=ON \
                    -DwxBUILD_PRECOMP=OFF \
                    -DwxBUILD_SHARED=ON \
                    -DwxBUILD_TESTS=OFF \
                    -DwxCXX_STANDARD_DEFAULT=11 \
                    -DwxBUILD_TOOLKIT=gtk3 \
                    -DwxUSE_AUI=ON \
                    -DwxUSE_CAIRO_DEFAULT=ON \
                    -DwxUSE_COMPILER_TLS=ON \
                    -DwxUSE_DEBUGREPORT=OFF \
                    -DwxUSE_DISPLAY=ON \
                    -DwxUSE_EXPAT=sys \
                    -DwxUSE_GEOMETRY=ON \
                    -DwxUSE_GLCANVAS_EGL=OFF \
                    -DwxUSE_GUI=ON \
                    -DwxUSE_HTML=ON \
                    -DwxUSE_INTL=ON \
                    -DwxUSE_JOYSTICK=OFF \
                    -DwxUSE_LIBICONV=sys \
                    -DwxUSE_LIBJPEG=sys \
                    -DwxUSE_LIBLZMA=OFF \
                    -DwxUSE_LIBNOTIFY=OFF \
                    -DwxUSE_LIBPNG=sys \
                    -DwxUSE_LIBSDL=OFF \
                    -DwxUSE_LIBTIFF=sys \
                    -DwxUSE_MEDIACTRL=OFF \
                    -DwxUSE_MIMETYPE=ON \
                    -DwxUSE_OPENGL=OFF \
                    -DwxUSE_POSTSCRIPT=ON \
                    -DwxUSE_PRINTF_POS_PARAMS=ON \
                    -DwxUSE_REGEX=sys \
                    -DwxUSE_REPRODUCIBLE_BUILD=OFF \
                    -DwxUSE_RICHTEXT=ON \
                    -DwxUSE_SECRETSTORE=ON \
                    -DwxUSE_SOCKETS=ON \
                    -DwxUSE_SPELLCHECK=OFF \
                    -DwxUSE_STC=ON \
                    -DwxUSE_STDPATHS=ON \
                    -DwxUSE_THREADS=ON \
                    -DwxUSE_UNICODE=ON \
                    -DwxUSE_UNSAFE_WXSTRING_CONV=ON \
                    -DwxUSE_WEBREQUEST=ON \
                    -DwxUSE_WEBREQUEST_CURL=ON \
                    -DwxUSE_WEBREQUEST_URLSESSION=OFF \
                    -DwxUSE_WEBVIEW=OFF \
                    -DwxUSE_XLOCALE=ON \
                    -DwxUSE_XRC=ON \
                    -DwxUSE_ZLIB=sys

patch.pre_args-replace -p0 -p1

platform darwin {
    # These are backports from upstream now.
    patchfiles-append \
                    0001-uilocale-stdpaths.patch \
                    0002-dirctrlg.cpp-fix.patch \
                    0003-display-fix.patch \
                    0004-evtloop_cf-fix.patch \
                    0005-defs.h-fix.patch \
                    0006-cfstring-fix.patch \
                    0008-utilsunx-no-memset_s-before-10.9.patch \
                    0009-utils_base.mm-fix.patch \
                    0010-Install-forgotten-stdpaths.h.patch

    # Quick, non-portable hack for https://github.com/wxWidgets/wxWidgets/issues/25490
    # TODO: must be reviewed and then updated to what the upstream settles on.
    patchfiles-append \
                    0011-volume.mm-support-building-on-10.7-and-with-gcc.patch \
                    0012-Move-volume.mm-in-place-for-macOS.patch

    # Broken in 3.2.x at the moment (fixed in the master):
    # https://github.com/wxWidgets/wxWidgets/issues/25873
    # Might have been fixed in https://github.com/wxWidgets/wxWidgets/pull/25919
#    if {${os.major} > 12} {
#        configure.args-replace \
#                    -DwxUSE_WEBREQUEST_CURL=ON -DwxUSE_WEBREQUEST_CURL=OFF \
#                    -DwxUSE_WEBREQUEST_URLSESSION=OFF -DwxUSE_WEBREQUEST_URLSESSION=ON
#    }
    # Temporary fix to actually enable webrequest:
    # https://github.com/wxWidgets/wxWidgets/issues/26471
    patchfiles-append \
                    0013-unbreak-webrequest.patch

    patchfiles-append \
                    0001-osx-evtloopsrc.h-install-always-on-macOS.patch \
                    0003-mimetypes-fixes.patch \
                    0004-Fix-Cocoa-stuff.patch

    # TODO: a hack to support wxPython build:
    patchfiles-append \
                    0006-wx-mediactrl.h-a-hack-to-install-it.patch

    # TODO: a hack, may not be needed for 3.3, verify:
    patchfiles-append \
                    0007-powercmn.cpp.patch

    # UNSAFE, a hack for 10.5:
    if {${os.major} < 10} {
        patchfiles-append \
                    patch-leopard.diff
    }

    if {${os.major} < 12} {
        configure.cxxflags-append \
                    -F/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks
    }
}

platform darwin powerpc {
    if {![catch {sysctl hw.vectorunit} result] && $result > 0} {
        configure.cxxflags-append -faltivec
        configure.cflags-append -faltivec
    }
}

post-destroot {
    foreach dylib [ exec find ${destroot}${wxWidgets.prefix}/lib -name "\*.dylib" ] {
            regsub ":$" ${dylib} "" destroot_dylib_path
            regsub ${destroot} ${destroot_dylib_path} "" dylib_path
            system "install_name_tool -id ${dylib_path} ${destroot_dylib_path}"

            system "install_name_tool -change ${prefix}/lib/libwx_baseu-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_baseu-${branch}.0.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_baseu_net-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_baseu_net-${branch}.0.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_baseu_xml-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_baseu_xml-${branch}.0.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_adv-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_adv-${branch}.0.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_aui-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_aui-${branch}.0.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_core-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_core-${branch}.0.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_html-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_html-${branch}.0.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_propgrid-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_propgrid-${branch}.0.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_ribbon-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_ribbon-${branch}.0.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_richtext-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_richtext-${branch}.0.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_xrc-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_xrc-${branch}.0.dylib \
                ${destroot_dylib_path}"

            if {[variant_isset gstreamer]} {
                system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_media-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_media-${branch}.0.dylib \
                    ${destroot_dylib_path}"
            }

            if {[variant_isset opengl]} {
                system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_gl-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_gl-${branch}.0.dylib \
                    ${destroot_dylib_path}"
            }

            if {[variant_isset qa]} {
                system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_qa-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_qa-${branch}.0.dylib \
                    ${destroot_dylib_path}"
            }

            system "install_name_tool -change ${prefix}/lib/libwx_baseu-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_baseu-${branch}.0.dylib \
                ${destroot}${wxWidgets.prefix}/bin/wxrc-${branch}"
            system "install_name_tool -change ${prefix}/lib/libwx_baseu_xml-${branch}.0.dylib ${wxWidgets.prefix}/lib/libwx_baseu_xml-${branch}.0.dylib \
                ${destroot}${wxWidgets.prefix}/bin/wxrc-${branch}"
    }

    set confscript ${wxWidgets.prefix}/lib/wx/config/${wxtype}-unicode-${branch}
    ln -sf ${confscript} ${destroot}${wxWidgets.prefix}/bin/wx-config
}

variant gstreamer description "Enable GStreamer" {
    depends_lib-append \
            port:gstreamer1 \
            port:gstreamer1-gst-plugins-bad

    configure.args-replace \
            -DwxUSE_MEDIACTRL=OFF -DwxUSE_MEDIACTRL=ON
}

variant opengl description "Enable GL" {
    configure.args-replace \
            -DwxUSE_OPENGL=OFF -DwxUSE_OPENGL=ON
}

variant qa description "Enable DebugReport class" {
    configure.args-replace \
            -DwxUSE_DEBUGREPORT=OFF -DwxUSE_DEBUGREPORT=ON
}

# media library is needed by some dependents.
default_variants-append     +gstreamer

if {${os.platform} ne "darwin" || ${os.major} < 20} {
    default_variants-append +opengl
}

github.livecheck.regex  {([0-9.]+)}
