# -*- 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           conflicts_build 1.0
PortGroup           muniversal 1.0

name                webkit-gtk
epoch               2
version             2.4.11
revision            12
description         Apple's WebKit HTML rendering library for GTK+
long_description    ${description}
maintainers         nomaintainer
categories          www gnome
platforms           darwin freebsd
license             LGPL-2+ BSD
homepage            http://webkitgtk.org/
master_sites        http://webkitgtk.org/releases/

use_xz              yes
distname            webkitgtk-${version}

dist_subdir         webkit-gtk

checksums           rmd160  e1f72498f911465f5ba152313dd9c62ae8d5eee6 \
                    sha256  588aea051bfbacced27fdfe0335a957dca839ebe36aa548df39c7bbafdb65bf7 \
                    size    9869100

depends_build       port:pkgconfig \
                    port:gtk-doc \
                    port:python27 \
                    port:py27-simplejson

depends_lib         path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    port:bison \
                    port:flex \
                    port:enchant \
                    port:geoclue2 \
                    path:lib/pkgconfig/gobject-introspection-1.0.pc:gobject-introspection \
                    path:lib/pkgconfig/harfbuzz-icu.pc:harfbuzz-icu \
                    path:lib/pkgconfig/icu-uc.pc:icu \
                    port:libxslt \
                    port:libpng \
                    port:libsecret \
                    port:libsoup-2.4 \
                    port:mesa \
                    port:sqlite3 \
                    port:webp \
                    port:xorg-libXt

# case-insensitive.patch: https://bugs.webkit.org/show_bug.cgi?id=65811
# gtk-doc.patch: https://aur.archlinux.org/cgit/aur.git/tree/gtk-doc.patch?h=webkitgtk
# our-icu.patch: No upstream bug report, probably not wanted
# ruby-1.8.patch: https://bugs.webkit.org/show_bug.cgi?id=126327
# quartz-webcore.patch: https://bugs.webkit.org/show_bug.cgi?id=126326
# gstreamer.patch: https://bugs.webkit.org/show_bug.cgi?id=126437
# libedit.patch: https://bugs.webkit.org/show_bug.cgi?id=127059
# leopard-malloc.patch: https://bugs.webkit.org/show_bug.cgi?id=140143
# leopard-platform.patch: https://bugs.webkit.org/show_bug.cgi?id=140143
# remove-cf-available.patch: https://trac.macports.org/ticket/49849
# RenderThemeGtk2-toRenderBox.patch: https://trac.macports.org/ticket/51406
# patch-icu_fix.diff: https://bugs.webkit.org/show_bug.cgi?id=171612
#                     https://bugs.webkit.org/show_bug.cgi?id=202600

patchfiles          clang-assertions.patch \
                    case-insensitive.patch \
                    gtk-doc.patch \
                    our-icu.patch \
                    ruby-1.8.patch \
                    quartz-webcore.patch \
                    gstreamer.patch \
                    leopard-platform.patch \
                    leopard-malloc.patch \
                    libedit.patch \
                    remove-cf-available.patch \
                    RenderThemeGtk2-toRenderBox.patch \
                    patch-icu_fix.diff \
                    bison-3.7.patch \
                    glibc-2.68.patch \
                    patch-fix_page_shift.diff \
                    patch-support-arm64.diff \
                    patch-icu-68-true-false.diff \
                    patch-cxx17.diff \
                    patch-fix-xml.diff

conflicts_build     google-test

# Required for ICU
compiler.cxx_standard   2017
configure.cxxflags-append   -std=c++17 -Wno-error=register

# https://trac.macports.org/ticket/36329
configure.perl      /usr/bin/perl
configure.python    ${prefix}/bin/python2.7

# --disable-webkit2: https://bugs.webkit.org/show_bug.cgi?id=126416
# --disable-web-audio: https://bugs.webkit.org/show_bug.cgi?id=126438
# --disable-jit: https://bugs.webkit.org/show_bug.cgi?id=133293
configure.args      --disable-webkit2 \
                    --disable-web-audio \
                    --enable-x11-target=yes \
                    --enable-quartz-target=no \
                    --enable-wayland-target=no \
                    --enable-introspection \
                    --disable-video \
                    --enable-svg \
                    --enable-geolocation \
                    --enable-webgl \
                    --disable-jit

subport webkit-gtk3 {
    conflicts webkit-gtk3-2.0
    depends_lib-append      path:lib/pkgconfig/gtk+-3.0.pc:gtk3
    configure.args-append   --with-gtk=3.0

#
# move gtk-docs to allow parallel install with webkit-gtk
#
    if {${universal_possible} && [variant_isset universal]} {
#
# using diff to merge derived include files sometimes
# results in a corrupted file due to nested /* */ blocks
#
        global merger_dont_diff
        set merger_dont_diff    "${prefix}/include/webkitgtk-3.0/webkitdom/WebKitDOMNavigator.h"
        merger-post-destroot {
            foreach arch ${universal_archs_to_use} {
                set docpath ${workpath}/destroot-${arch}${prefix}/share/gtk-doc/html
                move ${docpath}/webkitgtk ${docpath}/webkitgtk3
                move ${docpath}/webkitdomgtk ${docpath}/webkitdomgtk3
                move ${docpath}/webkitgtk3/webkitgtk.devhelp2 ${docpath}/webkitgtk3/webkitgtk3.devhelp2
            }
        }
    } else {
        post-destroot {
           set docpath ${destroot}${prefix}/share/gtk-doc/html
           move ${docpath}/webkitgtk ${docpath}/webkitgtk3
           move ${docpath}/webkitdomgtk ${docpath}/webkitdomgtk3
           move ${docpath}/webkitgtk3/webkitgtk.devhelp2 ${docpath}/webkitgtk3/webkitgtk3.devhelp2
        }
    }
}

if {${name} eq ${subport}} {
    conflicts webkit-gtk-2.0
    depends_lib-append      path:lib/pkgconfig/gtk+-2.0.pc:gtk2
    configure.args-append   --with-gtk=2.0
}

# https://bugs.webkit.org/show_bug.cgi?id=94488
configure.universal_args-delete --disable-dependency-tracking

# gobject-introspection uses g-ir-scanner, which uses $CC from env
if {${universal_possible} && [variant_isset universal]} {
    foreach arch ${configure.universal_archs} {
        lappend merger_build_args(${arch})     CC='${configure.cc} -arch ${arch}'
        lappend merger_destroot_args(${arch})  CC='${configure.cc} -arch ${arch}'
    }
} else {
    build.args-append       CC="${configure.cc} ${configure.cc_archflags}"
    destroot.args-append    CC="${configure.cc} ${configure.cc_archflags}"
}

# We don't want to use the headers from the installed WebKit
configure.cppflags-delete   -I${prefix}/include

# google-test suite build fix
configure.cppflags-append   -DGTEST_USE_OWN_TR1_TUPLE=1

# https://trac.macports.org/ticket/41560#comment:2
configure.cflags-append     -ftemplate-depth=256
configure.cxxflags-append   -ftemplate-depth=256

autoreconf.env-append "ACLOCAL=aclocal -I Source/autotools"
use_autoreconf      yes
autoreconf.args     -fvi

lappend merger_dont_diff ${prefix}/include/webkitgtk-1.0/webkitdom/WebKitDOMNavigator.h

post-patch {
    # https://bugs.webkit.org/show_bug.cgi?id=126433
    reinplace "s/def __APPLE__/ 0/" \
        ${worksrcpath}/Source/JavaScriptCore/API/WebKitAvailability.h

    # https://bugs.webkit.org/show_bug.cgi?id=58737
    reinplace "s:OS(MAC_OS_X):PLATFORM(MAC):" \
        ${worksrcpath}/Source/WTF/wtf/ThreadingPthreads.cpp

    # https://bugs.webkit.org/show_bug.cgi?id=126325
    reinplace "s:-stdlib=libstdc\+\+::" \
        ${worksrcpath}/Source/autotools/SetupCompilerFlags.m4

    # C++17 is required by ICU
    reinplace "s:-std=c++11:-std=c++17:" \
        ${worksrcpath}/Source/autotools/SetupCompilerFlags.m4

    # https://bugs.webkit.org/show_bug.cgi?id=126329
    # In file included from Source/JavaScriptCore/runtime/JSGlobalObject.cpp:83:
    # ./Source/JavaScriptCore/API/ObjCCallbackFunction.h:32:9: fatal error: 'JavaScriptCore/JSCallbackFunction.h' file not found
    # #import <JavaScriptCore/JSCallbackFunction.h>
    #         ^
    reinplace "/^#define JSC_OBJC_API_ENABLED/s/^.*$/#define JSC_OBJC_API_ENABLED 0/" \
        ${worksrcpath}/Source/JavaScriptCore/API/JSBase.h

    # macOS 12+ hasn't got python, use MacPorts python instead
    reinplace "s|#!/usr/bin/env python|#!${configure.python}|" \
        ${worksrcpath}/Tools/gtk/check-for-webkitdom-api-breaks \
        ${worksrcpath}/Tools/gtk/webkitdom.py \
        ${worksrcpath}/Tools/gtk/common.py \
        ${worksrcpath}/Tools/gtk/generate-feature-defines-files \
        ${worksrcpath}/Tools/gtk/generate-inspector-gresource-manifest.py \
        ${worksrcpath}/Tools/gtk/generate-gtkdoc \
        ${worksrcpath}/Source/JavaScriptCore/inspector/scripts/inline-and-minify-stylesheets-and-scripts.py \
        ${worksrcpath}/Source/JavaScriptCore/inspector/scripts/CodeGeneratorInspector.py \
        ${worksrcpath}/Source/WebKit2/Scripts/generate-messages-header.py \
        ${worksrcpath}/Source/WebKit2/Scripts/generate-message-receiver.py \
        ${worksrcpath}/Source/WebCore/html/parser/create-html-entity-table
}

# TODO: fix this for muniversal
# keep it for debug
#post-destroot {
#    file copy ${worksrcpath}/Programs/.libs/GtkLauncher ${destroot}${prefix}/bin
#}

# Apple's gnumake (patched 3.81) gets wedged at some point during the build process
depends_build-append    port:gmake
build.cmd               ${prefix}/bin/gmake

build.args-append V=1

# see bug #24622
variant quartz {
    configure.args-replace --enable-x11-target=yes --enable-x11-target=no
    configure.args-replace --enable-quartz-target=no --enable-quartz-target=yes
    configure.args-append --disable-accelerated-compositing

    # TODO: See if this will build with OpenGL.framework
    configure.args-delete --enable-webgl
    depends_lib-delete \
        port:mesa \
        port:xorg-libXt
}

variant video description {Enable HTML5 video support using gstreamer} {
        depends_lib-append      port:gstreamer1-gst-plugins-base
        configure.args-delete   --disable-video
        configure.args-append   --enable-video
}

default_variants    +video

livecheck.type      regex
livecheck.url       http://webkitgtk.org/releases/
livecheck.regex     "webkitgtk-(2\\.4(?:\\.\\d+)*)"
