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

PortSystem  1.0
PortGroup   conflicts_build 1.0
PortGroup   muniversal 1.0

name        webkit-gtk-2.0
version     2.0.4
revision    16
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  19643cac3f53928757a7c3fe8ea33f144a581456 \
                    sha256  bd1550b7f6e1a55f803efe7d64438163ff6c04f02186002cb81d0c090aac734f \
                    size    9849620

depends_lib \
        path:lib/pkgconfig/glib-2.0.pc:glib2 \
        port:bison \
        port:flex \
        port:enchant \
        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

depends_build   \
        port:gtk-doc \
        port:pkgconfig

# case-insensitive.patch: https://bugs.webkit.org/show_bug.cgi?id=65811
# our-icu.patch: No upstream bug report, probably not wanted
# ruby-1.8.patch: https://bugs.webkit.org/show_bug.cgi?id=126327
# ListableHandler.patch: https://trac.macports.org/ticket/37828
# execinfo.patch: https://trac.macports.org/ticket/37882
# ppc.patch: https://bugs.webkit.org/show_bug.cgi?id=96005
# tiger.patch: No upstream bug, http://trac.macports.org/ticket/35923
# quartz-webcore.patch: https://bugs.webkit.org/show_bug.cgi?id=126326
# bison3.patch: https://bugs.webkit.org/show_bug.cgi?id=119798

patchfiles \
        clang-assertions.patch \
        case-insensitive.patch \
        gtk-doc.patch \
        our-icu.patch \
        execinfo.patch \
        ruby-1.8.patch \
        ListableHandler.patch \
        ppc.patch \
        tiger.patch \
        quartz-webcore.patch \
        patch-freetype-includes.diff \
        bison3.patch \
        glibc-2.68.patch

conflicts_build     google-test

# https://trac.macports.org/ticket/36329
configure.python    /usr/bin/python

configure.args  \
        --disable-geolocation \
        --disable-webkit2 \
        --enable-introspection \
        --disable-video \
        --enable-svg \
        --enable-webgl

platform darwin {
    if {${configure.cxx_stdlib} eq "libc++"} {
        known_fail  yes
        pre-configure {
            ui_error "${name} is provided for configurations that are unable to use webkit-gtk.  Since you are on Mavericks or later (or have configured MacPorts to use libc++), please use webkit-gtk or webkit-gtk-devel instead."
            error "unsupported configuration"
        }
    }
}

subport webkit-gtk3-2.0 {
    conflicts   webkit-gtk3

    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}/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}/webkitgtk3/webkitgtk.devhelp2 ${docpath}/webkitgtk3/webkitgtk3.devhelp2
        }
    }
}

if {${name} eq ${subport}} {
    conflicts   webkit-gtk

    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

# configure checks that we're clang 3.0, gcc 4.7, or better
# clang 3.4+ fail (error: no template named 'allocateCell'; did you mean 'validateCell' (https://trac.macports.org/ticket/43987))
# Ideally we'd just do this, but https://trac.macports.org/ticket/51549
# compiler.whitelist {clang >= 300 <= 503} macports-clang-3.3
compiler.blacklist-append *gcc*
compiler.blacklist-append {clang < 300} {clang > 503}
compiler.blacklist-append macports-clang-3.4 macports-clang-3.7
compiler.fallback-append macports-clang-3.3

# 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}"
}

# In 1.10.1:
# Source/WebCore/platform/audio/gstreamer/FFTFrameGStreamer.cpp: In constructor 'WebCore::FFTFrame::FFTFrame(unsigned int)':
# Source/WebCore/platform/audio/gstreamer/FFTFrameGStreamer.cpp:48: error: 'm_complexData' was not declared in this scope
# Source/WebCore/platform/audio/gstreamer/FFTFrameGStreamer.cpp:48: error: 'GstFFTF32Complex' was not declared in this scope
# Source/WebCore/platform/audio/gstreamer/FFTFrameGStreamer.cpp:48: error: no matching function for call to 'fastNewArray(size_t)'
# Source/WebCore/platform/audio/gstreamer/FFTFrameGStreamer.cpp:50: error: 'gst_fft_next_fast_length' was not declared in this scope
# Source/WebCore/platform/audio/gstreamer/FFTFrameGStreamer.cpp:51: error: 'm_fft' was not declared in this scope
# Source/WebCore/platform/audio/gstreamer/FFTFrameGStreamer.cpp:51: error: 'FALSE' was not declared in this scope
# Source/WebCore/platform/audio/gstreamer/FFTFrameGStreamer.cpp:51: error: 'gst_fft_f32_new' was not declared in this scope
# Source/WebCore/platform/audio/gstreamer/FFTFrameGStreamer.cpp:52: error: 'm_inverseFft' was not declared in this scope
# Source/WebCore/platform/audio/gstreamer/FFTFrameGStreamer.cpp:52: error: 'TRUE' was not declared in this scope
#        --enable-web-audio \

# 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

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=88407
    reinplace "s:echo -n:/bin/echo -n:g" \
        ${worksrcpath}/Source/WebCore/GNUmakefile.am

    # https://bugs.webkit.org/show_bug.cgi?id=99683
    reinplace "s:PLATFORM(MAC):OS(DARWIN):g" \
        ${worksrcpath}/Source/JavaScriptCore/heap/VTableSpectrum.cpp \
        ${worksrcpath}/Source/JavaScriptCore/jit/ThunkGenerators.cpp \
        ${worksrcpath}/Source/JavaScriptCore/tools/CodeProfile.cpp

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

    # old macOS may have old python, use MacPorts python instead
    reinplace "s|#!/usr/bin/env python|#!${configure.python}|" \
        ${worksrcpath}/Tools/gtk/common.py \
        ${worksrcpath}/Tools/gtk/generate-feature-defines-files \
        ${worksrcpath}/Tools/gtk/generate-gtkdoc \
        ${worksrcpath}/Tools/Scripts/run-gtk-tests \
        ${worksrcpath}/Source/WebCore/inspector/generate-inspector-protocol-version \
        ${worksrcpath}/Source/WebCore/inspector/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
#}

build.args-append V=1

# see bug #24622
variant quartz {
    configure.args-append --with-target=quartz

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

    # quartz-include-widgetbackingstorecairo.patch
    # https://trac.macports.org/ticket/38203
    # https://bugs.webkit.org/show_bug.cgi?id=111598
    patchfiles-append quartz-duplicate-symbols.patch \
                      quartz-include-widgetbackingstorecairo.patch
}

variant geolocation description {Enable geolocation support using geoclue} {
        depends_lib-append      port:geoclue
        configure.args-delete   --disable-geolocation
        configure.args-append   --enable-geolocation
}

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

platform darwin {
    if {${os.major} < 10} {
        depends_build-append      port:python27
        # https://trac.macports.org/ticket/35793
        configure.python          ${prefix}/bin/python2.7

        # https://trac.macports.org/ticket/38682
        configure.optflags-append -fno-blocks
    }

    if {[string match "*10.5*" ${configure.sdkroot}] ||
        (${os.major} == 9 && ${configure.sdkroot} eq "")} {

        # https://trac.macports.org/ticket/37418
        configure.cppflags-append -D__MAC_OS_X_VERSION_MAX_ALLOWED=1050
    }

    if {[string match "*10.4*" ${configure.sdkroot}] ||
        (${os.major} == 8 && ${configure.sdkroot} eq "")} {

        # https://trac.macports.org/ticket/37828
        configure.cppflags-append -D__MAC_OS_X_VERSION_MAX_ALLOWED=1040
    }

    if {$macosx_deployment_target eq "10.4" ||
        (${os.major} == 8 && $macosx_deployment_target eq "")} {

        # https://trac.macports.org/ticket/37828
        configure.cppflags-append -D__MAC_OS_X_VERSION_MIN_REQUIRED=1040
    }
}

platform powerpc {
    # https://trac.macports.org/ticket/37839
    configure.optflags-replace -O2 -Os
}

if {[string match *clang* ${configure.compiler}]} {
    configure.cxxflags-append -Wno-c++11-extensions
}

livecheck.type  none
