# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4

# We have an umbrella radar tracking webkit-gtk issues on OS X.  Please relate
# any future issues with this tracker.
# http://bugs.webkit.org/show_bug.cgi?id=126492

PortSystem          1.0
PortGroup           active_variants 1.1
PortGroup           cmake 1.1
PortGroup           legacysupport 1.1

compiler.cxx_standard 2023
compiler.blacklist-append {clang < 1600}
compiler.blacklist-append macports-clang-17
cmake.generator     Ninja
# Fix compilation for Angle subproject: platform.h: error: 'TARGET_OS_OSX' is not defined
legacysupport.newest_darwin_requires_legacy 15

name                webkit2-gtk-devel
conflicts           webkit2-gtk
version             2.52.4

description         Apple's WebKit2 HTML rendering library for GTK+3
long_description    {*}${description}
maintainers         {devans @dbevans} {mascguy @mascguy}
categories          www gnome
platforms           darwin freebsd
license             LGPL-2+ BSD
homepage            https://webkitgtk.org/
master_sites        https://webkitgtk.org/releases/

use_xz              yes
distname            webkitgtk-${version}

dist_subdir         webkit-gtk

checksums           rmd160  247b1624c7502e35e20e873f462eb68f631f26b3 \
                    sha256  cf4076a1ca2a64788edca8c452d8ebb68d5e2965e588fe46a388a016513edce4 \
                    size    65093228

depends_build-append \
                    port:gperf \
                    port:perl5.34 \
                    port:pkgconfig \
                    port:python313 \
                    port:ruby33 \
                    port:unifdef

depends_lib-append  port:atk \
                    port:at-spi2-atk \
                    path:lib/pkgconfig/cairo.pc:cairo \
                    port:fontconfig \
                    port:freetype \
                    port:enchant2 \
                    port:geoclue2 \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    path:lib/pkgconfig/gobject-introspection-1.0.pc:gobject-introspection \
                    port:gstreamer1 \
                    port:gstreamer1-gst-plugins-bad \
                    port:gstreamer1-gst-plugins-base \
                    port:gstreamer1-gst-plugins-good \
                    path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
                    path:lib/pkgconfig/harfbuzz-icu.pc:harfbuzz-icu \
                    port:hyphen \
                    path:lib/pkgconfig/icu-uc.pc:icu \
                    port:libavif \
                    port:libjxl \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:lcms2 \
                    port:libepoxy \
                    port:libgcrypt \
                    port:libnotify \
                    port:libpng \
                    port:libsecret \
                    path:lib/pkgconfig/libsoup-3.0.pc:libsoup \
                    port:libtasn1 \
                    port:libxml2 \
                    port:libxslt \
                    port:sqlite3 \
                    port:webp \
                    port:woff2 \
                    port:zlib

configure.cppflags-append \
    -Wno-unknown-warning-option \
    -Wno-missing-template-arg-list-after-template-kw \
    -Wno-deprecated-declarations \
    -isystem ${worksrcpath}/stub-headers

# Update WTF CMakeLists for stubs added in post-extract.
patchfiles-append   patch-wtf-apple-cocoa-missing-files.diff

# Add missing EventLoop.h include in DocumentLoader.cpp
patchfiles-append   patch-webcore-documentloader-eventloop-include.diff

# ANGLE assumes ANGLE_PLATFORM_LINUX for GTK; use ANGLE_PLATFORM_APPLE
# instead.
patchfiles-append   patch-angle-platform-gtk-apple.diff

# bmalloc ProcessCheck: provide inline stubs for processNameString() and
# shouldAllowMiniMode() on Darwin without Cocoa (implementations are in
# a .mm file not included in the GTK tarball).
patchfiles-append   patch-bmalloc-processcheck-darwin-gtk.diff

# Disable TZONE (not included in the GTK tarball)
patchfiles-append   patch-bmalloc-disable-tzone.diff

# Use portable TLS sentinel instead of private pthread_self_is_exiting_np
patchfiles-append   patch-bmalloc-pthread-self-is-exiting.diff

# Replace forward declarations with #include to avoid incomplete type
# errors with std::span in older libc++ (macports-clang-16)
patchfiles-append   patch-webkit-gtkpagerange-incomplete-type.diff

# Fix GStreamer API calls to use gint64/guint64 instead of int64_t.
patchfiles-append   patch-gstreamer-int64-type-mismatch.diff

# On macOS with GTK, both OS(DARWIN) and USE(UNIX_DOMAIN_SOCKETS) are true.
# Prefer UnixFileDescriptor (GTK) over MachSendRight (Cocoa-only).
patchfiles-append   patch-webkit-ipc-darwin-gtk.diff

# ...and use only unix sockets in serialization.in.
patchfiles-append   patch-webkit-serialization-elif-darwin.diff

# rename include of ArgumentCodersGlib.h to avoid case-insensitive
# collision (see post-extract)
patchfiles-append   patch-webkit-argumentcoders-glib-case.diff

# Fix jsc/JSC include path collision on case-insensitive FS
patchfiles-append   patch-jsc-glib-include-case-collision.diff

# Fall back to CPU rendering when no EGL display can be created
# instead of crashing
patchfiles-append   patch-egl-display-no-crash.diff

# Add missing SkPathDump.cpp to Skia CMakeLists to fix linker error
patchfiles-append   patch-skia-add-skpathdump.diff

# macOS does not support SOCK_SEQPACKET; use SOCK_DGRAM for IPC sockets
# with enlarged buffers to accommodate WebKit's 4KB max message size.
patchfiles-append   patch-ipc-darwin-sock-dgram.diff

# Skip broken Darwin path for sqlite3 init
patchfiles-append   patch-sqlite-init-deadlock.diff

# KERN_NOT_FOUND missing from SDKs before macOS 12
platform darwin {
    # mach_vm_offset_t not declared in older SDK headers
    patchfiles-append patch-wtf-mach-vm-types-include.diff
    if {${os.major} < 21} {
        patchfiles-append patch-bmalloc-kern-not-found.diff
    }

}

# Create empty stub headers for Apple-specific WTF headers referenced under
# OS(DARWIN) guards. The GTK tarball omits these but code still includes them.
post-extract {
    set wtfdir ${worksrcpath}/Source/WTF/wtf
    foreach subdir {cf cocoa spi/cf spi/cocoa spi/mac text/cf} {
        file mkdir ${wtfdir}/${subdir}
    }
    foreach stub {
        cf/CFURLExtras.h cf/NotificationCenterCF.h cf/TypeCastsCF.h cf/VectorCF.h
        cocoa/CrashReporter.h cocoa/Entitlements.h cocoa/NSURLExtras.h
        cocoa/RuntimeApplicationChecksCocoa.h cocoa/SoftLinking.h
        cocoa/TollFreeBridging.h cocoa/TypeCastsCocoa.h cocoa/VectorCocoa.h
        spi/cf/CFBundleSPI.h spi/cf/CFStringSPI.h
        spi/cocoa/CFXPCBridgeSPI.h spi/cocoa/CrashReporterClientSPI.h
        spi/cocoa/IOSurfaceSPI.h spi/cocoa/MachVMSPI.h spi/cocoa/NSLocaleSPI.h
        spi/cocoa/NSObjCRuntimeSPI.h spi/cocoa/SecuritySPI.h spi/cocoa/objcSPI.h
        spi/mac/MetadataSPI.h
        text/cf/StringConcatenateCF.h text/cf/TextBreakIteratorCF.h
    } {
        close [open ${wtfdir}/${stub} w]
    }

    # OSLogPrintStream.mm is not included in the GTK tarball (Apple-only).
    # Provide a .cpp implementation using the public os_log API.
    file copy ${filespath}/OSLogPrintStream.cpp ${wtfdir}/darwin/OSLogPrintStream.cpp

    # TypeCastsOSObject.h uses Objective-C constructs (bridge_id_cast, object_getClass)
    # that cannot compile in C++ mode. Replace with a stub providing no-op macros.
    file copy -force ${filespath}/stub-TypeCastsOSObject.h \
        ${wtfdir}/darwin/TypeCastsOSObject.h

    # We do not have EGL but WebCore and Skia need the headers to
    # compile. Provide stubs with no-op implementations so they can
    # compile. The EGL paths will never be taken at runtime because
    # there is no EGL display.
    set stubdir ${worksrcpath}/stub-headers/epoxy
    file mkdir ${stubdir}
    file copy ${filespath}/stub-epoxy-egl.h ${stubdir}/egl.h

    # Rename Platform/IPC/ArgumentCodersGlib.h to prevent collision
    # with Shared/glib/ArgumentCodersGLib.h on case-insensitive FS.
    file rename ${worksrcpath}/Source/WebKit/Platform/IPC/glib/ArgumentCodersGlib.h \
        ${worksrcpath}/Source/WebKit/Platform/IPC/glib/ArgumentCodersGlibStrv.h
}

# it is preferred to use the WebKit built in bmalloc if it builds on a given os.
# it has improved security features, but not all systems can build it at present.
configure.args-append \
    -DPORT=GTK \
    -DENABLE_BUBBLEWRAP_SANDBOX=OFF \
    -DENABLE_DOCUMENTATION=OFF \
    -DENABLE_GAMEPAD=OFF \
    -DENABLE_INTROSPECTION=ON \
    -DENABLE_JOURNALD_LOG=OFF \
    -DENABLE_MINIBROWSER=OFF \
    -DENABLE_SPEECH_SYNTHESIS=OFF \
    -DENABLE_WAYLAND_TARGET=OFF \
    -DENABLE_WEBGL=OFF \
    -DUSE_FLITE=OFF \
    -DUSE_GBM=OFF \
    -DUSE_GSTREAMER_GL=OFF \
    -DUSE_GTK4=OFF \
    -DUSE_LIBBACKTRACE=OFF \
    -DUSE_LIBDRM=OFF \
    -DUSE_LIBHYPHEN=ON \
    -DUSE_SPIEL=OFF \
    -DUSE_SYSTEM_MALLOC=OFF \
    -DUSE_SYSTEM_SYSPROF_CAPTURE=OFF \
    -DUSE_SYSPROF_CAPTURE=OFF \
    -DUSE_APPLE_ICU=OFF \
    -DICU_ROOT=${prefix} \
    -DPERL_EXECUTABLE=${prefix}/bin/perl5.34 \
    -DPython_EXECUTABLE=${prefix}/bin/python3.13 \
    -DRuby_EXECUTABLE=${prefix}/bin/ruby3.3

pre-configure {
    if {![variant_isset quartz] && ![variant_isset x11]} {
        error "Either +x11 or +quartz is required"
    }
}

if {![variant_isset quartz]} {
    default_variants-append +x11
}

# this is nice to have, and should be the default
default_variants-append +minibrowser

variant quartz conflicts x11 {
    require_active_variants path:lib/pkgconfig/gtk+-3.0.pc:gtk3 quartz

    configure.args-append \
        -DENABLE_QUARTZ_TARGET=ON \
        -DENABLE_X11_TARGET=OFF
}

variant x11 conflicts quartz {
    require_active_variants path:lib/pkgconfig/gtk+-3.0.pc:gtk3 x11

    configure.args-append \
        -DENABLE_QUARTZ_TARGET=OFF \
        -DENABLE_X11_TARGET=ON

    depends_lib-append \
        port:mesa \
        port:xorg-libXt
}

variant minibrowser description {Build and install MiniBrowser (for testing)} {
    configure.args-delete   -DENABLE_MINIBROWSER=OFF
    configure.args-append   -DENABLE_MINIBROWSER=ON
    depends_run-append       port:adwaita-icon-theme
    patchfiles-append       patch-minibrowser-show-all.diff
    # [[fallthrough]] is a C23 attribute; use __attribute__ for compatibility
    patchfiles-append       patch-minibrowser-fallthrough-c23.diff
}

# see https://trac.macports.org/ticket/56792
# no success at building universal after considerable attempts
# errors out in the javascript interpreter
# muniversal PG does not fix it. disabling the JIT does not fix it
# help wanted, if suitably motivated
universal_variant   no

# Fix builds if ccache happens to be installed when configure.ccache=no
if {![option configure.ccache]} {
    build.env-append        CCACHE_DIR=${workpath}/.ccache
}

notes-append "
If you are porting new software to use webkit2gtk on Apple systems,\
you will likely need to define BUILDING_GTK__ to get the correct\
behaviour from WebKitAvailability.h.
"

livecheck.type      regex
livecheck.url       https://webkitgtk.org/releases/
livecheck.regex     "webkitgtk-(\\d+\\.\\d*\[02468\](?:\\.\\d+)*)"
