# -*- 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
# $Id: Portfile 119072 2014-04-16 16:33:54Z jeremyhu@macports.org $

# 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
PortGroup   compiler_blacklist_versions 1.0

name        webkit-gtk
epoch       2
version     2.4.1
description Apple's WebKit HTML rendering library for GTK+
long_description ${description}
maintainers jeremyhu devans
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           sha1    da83c4e7b36917e307155ebe3ae93621e4802d83 \
                    rmd160  0cc7fa02fe2867da2e86acd47e8f24c23403af04 \
                    sha256  6eed5d41ecdcfbafe3c2fbb0f0b79b9cda87a5ea0e7d0938afb6c6c82996e2f7

depends_lib \
        path:lib/pkgconfig/glib-2.0.pc:glib2 \
        port:bison \
        port:flex \
        port:enchant \
        port:geoclue \
        port:gobject-introspection \
        port:harfbuzz-icu \
        port:libxslt \
        port:libpng \
        port:libsecret \
        port:libsoup \
        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
# quartz-webcore.patch: https://bugs.webkit.org/show_bug.cgi?id=126326
# quartz-duplicate-symbols.patch: https://bugs.webkit.org/show_bug.cgi?id=126821
# clang-check.patch: https://bugs.webkit.org/show_bug.cgi?id=126324
# gstreamer.patch: https://bugs.webkit.org/show_bug.cgi?id=126437
# libedit.patch: https://bugs.webkit.org/show_bug.cgi?id=127059

patchfiles \
        clang-assertions.patch \
        case-insensitive.patch \
        our-icu.patch \
        quartz-duplicate-symbols.patch \
        quartz-webcore.patch \
        clang-check.patch \
        gstreamer.patch \
        libedit.patch

conflicts_build     google-test

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

# --disable-webkit2: https://bugs.webkit.org/show_bug.cgi?id=126416
# --disable-web-audio: https://bugs.webkit.org/show_bug.cgi?id=126438
configure.args  \
        --disable-webkit2 \
        --disable-web-audio \
        --enable-introspection \
        --disable-video \
        --enable-svg \
        --enable-geolocation \
        --enable-webgl

# TODO: Check ${configure.cxx_stdlib} directly once MacPorts 2.3 is released
platform darwin {
    pre-configure {
        set cxxstdlib {}

        if {[info exists configure.cxx_stdlib] &&
            ${configure.cxx_stdlib} ne {} &&
            [string match *clang* ${configure.cxx}]} {
            set cxxstdlib ${configure.cxx_stdlib}
        } elseif {[string match *clang* ${configure.cxx}] &&
                  ${os.major} >= 13} {
            set cxxstdlib libc++
        } else {
            set cxxstdlib libstdc++
        }

        if {${cxxstdlib} eq "libstdc++"} {
            ui_error "${subport} is not supported for this installation of MacPorts.  It requires libc++ be selected as your C++ runtime.  Please use ${subport}-2.0 instead"
            error "unsupported configuration"
        }
    }
}

subport webkit-gtk3 {
    conflicts   webkit-gtk3-devel webkit-gtk3-2.0
    depends_lib-append      port:gtk3
    configure.args-append   --with-gtk=3.0

#
# move gtk-docs to allow parallel install with webkit-gtk
#
    if {[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-devel webkit-gtk-2.0
    depends_lib-append      port: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.2, gcc 4.7, or better
compiler.blacklist-append gcc-3.3 gcc-4.0 apple-gcc-4.0 gcc-4.2 apple-gcc-4.2 llvm-gcc-4.2 macports-llvm-gcc-4.2
compiler.blacklist-append macports-gcc-4.2 macports-gcc-4.3 macports-gcc-4.4 macports-gcc-4.5 macports-gcc-4.6
compiler.blacklist-append {clang < 425} macports-clang-2.9 macports-clang-3.0 macports-clang-3.1

# gobject-introspection uses g-ir-scanner, which uses $CC from env
if {[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

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

# 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-append --enable-quartz-target=yes --enable-x11-target=no
    platform darwin 13 {
        # TODO: File upstream
        #       http://trac.macports.org/ticket/41663
        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

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

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

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

livecheck.type  regex
livecheck.url   http://webkitgtk.org/?page=download
livecheck.regex "webkitgtk-(\\d+(?:\\.\\d+)*)${extract.suffix}"
