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

name                wxgtk-devel
set branch          3.3
set patch           2
set gtk_ver         3

github.setup        wxWidgets wxWidgets 6152ec4e885e5076d668486dc799ce8f2d89bce4
version             ${branch}-20251104-[string range ${github.version} 0 7]
revision            0

set installname     wxGTK
set wxtype          gtk${gtk_ver}
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, \
                    development version
long_description    wxGTK ${branch} is an open-source cross-platform C++ \
                    GUI framework for Mac OS, Unix, Linux, Windows.

homepage            https://www.wxwidgets.org

fetch.type          git

post-fetch {
    system -W ${worksrcpath} "git submodule update --init --recursive"
}

dist_subdir         wxWidgets/devel

set selectdir       ${workpath}/select
select.group        wxWidgets
select.file         ${selectdir}/wxgtk-${branch}

patch.pre_args-replace  -p0 -p1

# https://github.com/wxWidgets/wxWidgets/pull/25919
patchfiles-append   0001-OSX-extend-private.h-usage-for-__DARWIN__-only-build.patch \
                    0002-move-non-gui-classes-to-base.patch \
                    0003-swapping-order-as-TARGET_OS_IPHONE-is-subclass-of-TA.patch \
                    0004-fixing-warnings-in-__DARWIN__-builds.patch \
                    0005-more-precise-macro-name-allows-former-order.patch \
                    0006-expose-CFLocale-methods-for-all-__DARWIN__-builds.patch \
                    0007-fix-warnings.patch \
                    0008-add-AppKit-framework-to-solve-NSWorkspace-errors.patch \
                    0009-fix-warning.patch \
                    0010-offer-volume.mm-to-all-baselib-variants-on-osx.patch \
                    0011-bring-mimetype.h-includes-to-the-same-rule-as-mimety.patch \
                    0012-use-proper-preproc-constants.patch \
                    0013-correct-condition.patch \
                    0014-guard-code-with-wxUSE_GUI-properly-again.patch \
                    0015-correcting-endif-comment-add-for-loop-macro-for-obj-.patch \
                    0016-trying-to-make-code-gcc-and-macOS-10.7-compatible.patch \
                    0017-guard-__OBJC__.patch \
                    0018-better-guard-for-OBJC_API_VERSION.patch \
                    0019-make-sure-availability-macros-are-always-available.patch \
                    0020-add-missing-frameworks.patch \
                    0021-have-only-one-set-of-source-files-on-osx-for-base.patch \
                    0022-AppKit-and-UIKit-linkage-for-base.patch \
                    0023-using-backwards-compatibly-OBJC_FOR.patch \
                    0024-test-to-bring-locale-resolution-in-line-with-unix.patch \
                    0025-use-the-precise-native-language-id-that-matched.patch \
                    0026-revert-change-test-seems-to-fail-for-a-different-rea.patch \
                    0027-NSNumber-Literals-clang-4-not-available-under-gcc.patch \
                    0028-using-key-value-access-to-support-older-macOS-SDKs.patch \
                    0029-guard-code-properly-for-non-iPhone-builds.patch \
                    0030-apparently-for-loop-was-unstable.patch \
                    0031-move-utilities-to-base.patch \
                    0032-make-sure-command-line-apps-are-properly-initialized.patch \
                    0033-proper-guard-non-iphone-code.patch \
                    0034-remove-debug-info-tests-passing-now.patch \
                    0035-look-for-gstreamer-when-using-gtk-also-under-macos.patch \
                    0036-make-sure-wxHAS_NSPROCESSINFO-is-always-defined.patch \
                    0037-guard-API.patch \
                    0038-revert-NSArray-literals.patch \
                    0039-use-backwards-compatible-APIs.patch \
                    0040-add-backwards-compatible-implementation-again.patch \
                    0041-fix-two-checks-problems.patch \
                    0042-modifications-according-to-review.patch \
                    0043-removing-SHARED-from-source-set-names-on-OSX.patch \
                    0044-additional-platform-defines.patch \
                    0045-start-using-__WXDARWIN_OSX__.patch \
                    0046-use-former-order-of-includes.patch \
                    0047-using-more-__WXDARWIN_OSX__.patch \
                    0048-remove-currently-unused-iPhone-condition.patch

compiler.c_standard     2014
compiler.cxx_standard   2014
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+-${gtk_ver}.0.pc:gtk${gtk_ver} \
                    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:webp \
                    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}"
}

# FIXME: mediactrl disabled for now, see:
# https://github.com/wxWidgets/wxWidgets/pull/25919#issuecomment-3469607639
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_LIBGNOMEVFS=OFF \
                    -DwxUSE_LIBICONV=sys \
                    -DwxUSE_LIBJPEG=sys \
                    -DwxUSE_LIBLZMA=OFF \
                    -DwxUSE_LIBNOTIFY=OFF \
                    -DwxUSE_LIBPNG=sys \
                    -DwxUSE_LIBSDL=OFF \
                    -DwxUSE_LIBTIFF=sys \
                    -DwxUSE_LIBWEBP=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=OFF \
                    -DwxUSE_STDPATHS=ON \
                    -DwxUSE_THREADS=ON \
                    -DwxUSE_UNICODE=ON \
                    -DwxUSE_UNSAFE_WXSTRING_CONV=ON \
                    -DwxUSE_WEBREQUEST=ON \
                    -DwxUSE_WEBREQUEST_CURL_DEFAULT=OFF \
                    -DwxUSE_WEBREQUEST_URLSESSION=ON \
                    -DwxUSE_WEBVIEW=OFF \
                    -DwxUSE_XLOCALE=ON \
                    -DwxUSE_XRC=ON \
                    -DwxUSE_ZLIB=sys

platform darwin {
    # Unsure where this can build, but on Catalina it still fails:
    # https://github.com/wxWidgets/wxWidgets/issues/25873
    if {${os.major} < 13} {
        configure.args-replace \
                    -DwxUSE_WEBREQUEST_CURL_DEFAULT=OFF -DwxUSE_WEBREQUEST_CURL_DEFAULT=ON \
                    -DwxUSE_WEBREQUEST_URLSESSION=ON -DwxUSE_WEBREQUEST_URLSESSION=OFF
    }

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

platform darwin powerpc {
    # https://github.com/macos-powerpc/powerpc-ports/issues/17
    # GTK implies X11, however avoid hardcoding this, otherwise it becomes a pain.
    depends_lib-replace \
        port:libsdl2 path:lib/pkgconfig/sdl2.pc:libsdl2-x11

    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}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_baseu-${branch}.${patch}.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_baseu_net-${branch}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_baseu_net-${branch}.${patch}.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_baseu_xml-${branch}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_baseu_xml-${branch}.${patch}.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_adv-${branch}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_adv-${branch}.${patch}.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_aui-${branch}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_aui-${branch}.${patch}.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_core-${branch}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_core-${branch}.${patch}.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_gl-${branch}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_gl-${branch}.${patch}.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_html-${branch}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_html-${branch}.${patch}.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_media-${branch}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_media-${branch}.${patch}.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_propgrid-${branch}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_propgrid-${branch}.${patch}.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_ribbon-${branch}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_ribbon-${branch}.${patch}.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_richtext-${branch}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_richtext-${branch}.${patch}.dylib \
                ${destroot_dylib_path}"
            system "install_name_tool -change ${prefix}/lib/libwx_gtk3u_xrc-${branch}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_gtk3u_xrc-${branch}.${patch}.dylib \
                ${destroot_dylib_path}"

            system "install_name_tool -change ${prefix}/lib/libwx_baseu-${branch}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_baseu-${branch}.${patch}.dylib \
                ${destroot}${wxWidgets.prefix}/bin/wxrc-${branch}"
            system "install_name_tool -change ${prefix}/lib/libwx_baseu_xml-${branch}.${patch}.dylib ${wxWidgets.prefix}/lib/libwx_baseu_xml-${branch}.${patch}.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

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

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

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