# -*- 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.0
PortGroup           compiler_blacklist_versions 1.0
PortGroup           cxx11 1.1

name                webkit2-gtk
conflicts           webkit2-gtk-devel
version             2.20.5
description         Apple's WebKit2 HTML rendering library for GTK+3 (with optional support for GTK+2 plugins)
long_description    ${description}
maintainers         {devans @dbevans}
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  6a4cda986d2a4c4762b0daf0ff2007cd95b72b79 \
                    sha256  15c0f8d26e9605afe0948d161ba5fd82efee8eda2debd409fd40a440ac3af990 \
                    size    16625200

# don't overwrite build dependencies provided by cmake portgroup
depends_build-append \
                    port:gperf \
                    port:gtk-doc \
                    port:pkgconfig \
                    port:python27 \
                    port:py27-simplejson

depends_lib         port:atk \
                    port:at-spi2-atk \
                    port:bison \
                    path:lib/pkgconfig/cairo.pc:cairo \
                    port:flex \
                    port:fontconfig \
                    port:freetype \
                    port:enchant \
                    port:geoclue2 \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    port:gobject-introspection \
                    port:gstreamer1 \
                    port:gstreamer1-gst-plugins-bad \
                    port:gstreamer1-gst-plugins-base \
                    port:gstreamer1-gst-plugins-good \
                    port:gtk3 \
                    port:harfbuzz-icu \
                    port:hyphen \
                    port:icu \
                    port:libnotify \
                    port:libpng \
                    port:libsecret \
                    port:libsoup \
                    port:libxml2 \
                    port:libxslt \
                    port:sqlite3 \
                    port:webp \
                    port:woff2 \
                    port:zlib

# PR-152650-2.patch: WIP to re-enable gtk-doc support
# PR-153138.patch: https://bugs.webkit.org/show_bug.cgi?id=153138
# PR-157574.patch: https://bugs.webkit.org/show_bug.cgi?id=157574
# patch-name-conflicts.diff: avoid conflicts between Style in /usr/include/MacTypes.h
#                            and WebCore::Style
patchfiles-append \
    PR-152650-2.patch \
    PR-153138.patch \
    PR-157574.patch \
    patch-bundle-link-webcore.diff \
    patch-enable-plugin-architecture-unix.diff \
    patch-name-conflicts.diff
    
# Source/WTF/wtf/RAMSize.cpp has changed, and the Darwin parts were
# stripped out. Replace it with the previous version
patchfiles-append \
    patch-ramsize.diff



# Build out-of-tree
configure.post_args     ../${worksrcdir}
default configure.dir   {${workpath}/build}
default build.dir       {${workpath}/build}

configure.args-append \
    -DPORT=GTK \
    -DUSE_SYSTEM_MALLOC=ON \
    -DENABLE_INTROSPECTION=ON \
    -DENABLE_MINIBROWSER=OFF \
    -DENABLE_PLUGIN_PROCESS_GTK2=OFF \
    -DENABLE_VIDEO=ON

# TODO: File a bug about this not working
configure.args-append -DENABLE_GTKDOC=OFF

# <rdar://problem/24031030>
configure.optflags  -Os

# clang 3.4, 503.0.35, and 600.0.57 (error: call to 'make_unique' is ambiguous, error: call to 'exchange' is ambiguous)
# compiler.blacklist-append {clang < 602} macports-clang-3.4
# clang 3.3 (error: no type named 'make_index_sequence' in namespace 'std')
compiler.fallback-append macports-clang-4.0
compiler.blacklist-append {clang < 602} macports-clang-3.4 macports-clang-3.3

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

post-configure {
    # https://bugs.webkit.org/show_bug.cgi?id=153176
    reinplace {s|\.\./\.\./lib/libWTFGTK\.a||} \
        ${build.dir}/Source/WebKit/CMakeFiles/WebKit.dir/link.txt
    reinplace {s|[\./]*\.\./lib/lib[^\.]*\.a||g} \
        ${build.dir}/Source/JavaScriptCore/CMakeFiles/LLIntOffsetsExtractor.dir/link.txt \
        ${build.dir}/Source/JavaScriptCore/shell/CMakeFiles/jsc.dir/link.txt \
        ${build.dir}/Source/WebKit/CMakeFiles/StorageProcess.dir/link.txt \
        ${build.dir}/Source/WebKit/CMakeFiles/NetworkProcess.dir/link.txt \
        ${build.dir}/Source/WebKit/CMakeFiles/webkit2gtkinjectedbundle.dir/link.txt \
        ${build.dir}/Source/WebKit/CMakeFiles/WebProcess.dir/link.txt
    if {[variant_isset x11]} {
        # ENABLE_PLUGIN_PROCESS is only enabled with +x11
        reinplace {s|[\./]*\.\./lib/lib[^\.]*\.a||g} \
            ${build.dir}/Source/WebKit/CMakeFiles/PluginProcess.dir/link.txt

        if {[variant_isset gtk2]} {
            reinplace {s|\.\./\.\./lib/libWTFGTK\.a||} \
                ${build.dir}/Source/WebKit/CMakeFiles/WebKitPluginProcess2.dir/link.txt
            reinplace {s|-Wl,-all_load||g} \
                ${build.dir}/Source/WebKit/CMakeFiles/WebKitPluginProcess2.dir/link.txt
        }
    }
    if {[variant_isset minibrowser]} {
        reinplace {s|[\./]*\.\./lib/lib[^\.]*\.a||g} \
            ${build.dir}/Tools/MiniBrowser/gtk/CMakeFiles/MiniBrowser.dir/link.txt
    }

    # https://bugs.webkit.org/show_bug.cgi?id=153192
    reinplace {s|\.\./\.\./lib/libWTFGTK\.a|-Wl,-all_load ../../lib/libWTFGTK.a|} \
        ${build.dir}/Source/JavaScriptCore/CMakeFiles/JavaScriptCore.dir/link.txt
}

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

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

# fix build with +quartz variant
# see https://trac.macports.org/ticket/56724
# TO DO: is there a more elegant fix ?
if {[variant_isset quartz]} {
    configure.args-append -DUSE_GSTREAMER_GL=OFF
}

# fix build on older systems
if {${os.platform} eq "darwin" && ${os.major} <= 13} {

    # disable Apple internal security libraries, available only on new systems
    patchfiles-append  patch-Webcore-page-crypto.diff

    # gl cocoa build continues to fail due to use of API not available on darwin 12 or earlier
    # so gstreamergl is not available at present for darwin 12 or earlier
    configure.args-append -DUSE_GSTREAMER_GL=OFF
    
    # ENABLE_SUBTLE_CRYPTO used to be off by default, but now is on by default
    # there are > 100 instances of #if ENABLE(SUBTLE_CRYPTO)
    # fails up to darwin 13
    configure.args-append -DENABLE_SUBTLE_CRYPTO=OFF

    # the darwin build uses security features that were Apple internal only, but 
    # the unix version does not, and seems to build OK so use that
    # the ifdefs were too cumbersome to sort out, so for now I just stripped those out and left
    # the unix build behind - FIXME: sort the ifdefs out
    # https://bugs.webkit.org/show_bug.cgi?id=157554
    patchfiles-append   patch-WTF-wtf-Randomdevice.diff

    # add dep for newer ruby and spec this for build
    # https://trac.macports.org/ticket/52016
    depends_build-append    port:ruby24
    configure.args-append   -DRUBY_EXECUTABLE=${prefix}/bin/ruby2.4
    
    if { ${os.major} < 11 } {
        # disabling JIT fixes the following build error on 10.6.8
        # offlineasm: No magic values found. Skipping assembly file generation.
        configure.args-append -DENABLE_JIT=OFF
    }

    # fix missing PRId64 definitions on systems
    # that don't define __STDC_FORMAT_MACROS by default
    # https://bugs.webkit.org/show_bug.cgi?id=156596
    # https://trac.macports.org/ticket/52016
    patchfiles-append patch-snowleopard-cmakelists-stdcformatmacros.diff
    
    # special case: fix build on 10.6 with macports-libstdc++
    # contents of snowmath.h should someday become part of gcc6 cmath
    if { ${configure.cxx_stdlib} eq "macports-libstdc++"  && ${os.major} < 11 } {
        configure.cxxflags-append -D_GLIBCXX_USE_C99_MATH_TR1=1
        configure.cxxflags-append -include ${filespath}/snowmath.h
    }
}

variant quartz conflicts x11 gtk2 {
    require_active_variants port:gtk3 quartz

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

variant x11 conflicts quartz {
    require_active_variants port:gtk3 x11

    configure.args-append \
        -DENABLE_QUARTZ_TARGET=OFF \
        -DENABLE_X11_TARGET=ON \
        -DENABLE_OPENGL=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
}

variant gtk2 conflicts quartz description {Enable support for GTK+2 plugins} {
    require_active_variants port:gtk2 x11

    depends_lib-append      port:gtk2
    configure.args-delete   -DENABLE_PLUGIN_PROCESS_GTK2=OFF
    configure.args-append   -DENABLE_PLUGIN_PROCESS_GTK2=ON
}

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