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

PortSystem          1.0
PortGroup           active_variants 1.1
PortGroup           github          1.0
PortGroup           select          1.0
PortGroup           wxWidgets       1.0

github.setup        wxWidgets wxWidgets 3.0.5.1 v
github.tarball_from releases
revision            3

name                wxWidgets-3.0
# ugly workaround to allow some C++11-only applications to be built on < 10.9
subport             wxWidgets-3.0-cxx11 {}
# for cases when wxPython gets out of sync with wxWidgets releases
subport             wxPython-3.0 {}
# build against GTK 3
subport             wxgtk-3.0 {}
# build against GTK 3 with C++11
subport             wxgtk-3.0-cxx11 {}
# TODO: fix dist_subdir and subset name at next release
set wxpython_ver    3.0.2.0
# when revbumping remember to change the revision also for wxPython-3.0

set installname     wxWidgets
set wxtype          osx_cocoa

if {${subport} eq ${name}} {
    wxWidgets.use   wxWidgets-3.0
} elseif {${subport} eq "wxWidgets-3.0-cxx11"} {
    # ugly workaround to allow some C++11-only applications to be built on < 10.9
    wxWidgets.use   wxWidgets-3.0-cxx11
    compiler.cxx_standard   2011
} elseif {${subport} eq "wxPython-3.0"} {
    wxWidgets.use   wxPython-3.0
    version         3.0.2
    revision        8
} elseif {${subport} eq "wxgtk-3.0"} {
    # wxgtk-3.0 is need to support older systems where wxWidgets-3.0
    # do not work correctly (they do build, but are unusable).
    # It might also be useful for testing purposes, to report GTK-related bugs
    # to upstream and play with quartz-based GTK
    # (and maybe backport changes to 2.8)
    wxWidgets.use   wxGTK-3.0
    set installname wxGTK
    set wxtype      gtk3
} elseif {${subport} eq "wxgtk-3.0-cxx11"} {
    # Subport to allow C++11-using apps to build correctly.
    wxWidgets.use   wxGTK-3.0-cxx11
    compiler.cxx_standard   2011
    set installname wxGTK
    set wxtype      gtk3
}
set branch          [join [lrange [split ${version} .] 0 1] .]

categories          graphics devel
platforms           darwin
license             wxwidgets-3.1
maintainers         {mojca @mojca} openmaintainer

description         C++ framework for cross-platform GUI development
long_description    wxWidgets ${branch} is an open-source cross-platform C++ \
                    GUI framework for Mac OS, Unix, Linux, Windows.

homepage            http://www.wxwidgets.org/

distname            wxWidgets
dist_subdir         wxWidgets/${version}
worksrcdir          ${distname}-${version}/build
patch.dir           ${worksrcpath}/..

use_bzip2           yes
distfiles           ${distname}-${version}${extract.suffix}

checksums           ${distname}-${version}${extract.suffix} \
                    rmd160  931ddabfbc54e20ced14c81c74e451be8073dee5 \
                    sha256  440f6e73cf5afb2cbf9af10cec8da6cdd3d3998d527598a53db87099524ac807 \
                    size    19739206

set selectdir       ${workpath}/select
select.group        wxWidgets
select.file         ${selectdir}/${subport}

depends_lib         path:include/turbojpeg.h:libjpeg-turbo \
                    port:tiff \
                    port:libpng \
                    port:zlib \
                    port:libiconv \
                    port:expat

depends_run         port:wxWidgets-common \
                    port:wxWidgets_select

# see https://trac.macports.org/ticket/57508
configure.env-append \
                    GREP=/usr/bin/grep

patchfiles-append   patch-configure.diff

# 10.6-on-PPC hack; tested also on Rosetta:
patchfiles-append   patch-ppc.diff

post-patch {
    reinplace "s|@@PREFIX@@|${prefix}|g" ${patch.dir}/configure

    file mkdir ${selectdir}
    system "echo \"${wxWidgets.wxdir}/wx-config\n${wxWidgets.wxdir}/wxrc-${branch}\" > ${select.file}"
}

configure.cmd       ../configure
configure.args      --prefix=${wxWidgets.prefix} \
                    --with-libiconv-prefix=${prefix} \
                    --with-libjpeg \
                    --with-libtiff \
                    --with-libpng \
                    --with-zlib \
                    --with-opengl \
                    --with-cocoa \
                    --without-sdl \
                    --enable-aui \
                    --disable-sdltest \
                    --enable-unicode \
                    --enable-display \
                    --enable-xrc \
                    --enable-graphics_ctx \
                    --with-macosx-sdk=no \
                    --with-macosx-version-min=no
if {[string match *clang* ${configure.cxx}] && ${configure.cxx_stdlib} ne ""} {
    configure.ldflags-append \
                    -stdlib=${configure.cxx_stdlib}
}

platform darwin powerpc {
    if {![catch {sysctl hw.vectorunit} result] && $result > 0} {
        # Work around buggy header. https://trac.macports.org/ticket/55251
        configure.cxxflags-append -faltivec
        configure.cflags-append -faltivec
    }
}


if {${subport} eq "wxPython-3.0"} {
    master_sites            sourceforge:project/wxwindows/${version} \
                            http://biolpc22.york.ac.uk/pub/${version}/ \
                            http://trac.macports.org/raw-attachment/ticket/19190/:trac

    use_bzip2               yes

    distfiles               ${distname}-${version}${extract.suffix}

    # drop _1 from the subset name on next update
    set wxpythonsubset      wxPython-${wxpython_ver}_1-MacPorts-subset
    distfiles-append        ${wxpythonsubset}${extract.suffix}:trac

    description             wxWidgets ${branch} for the wxPython distribution
    long_description        The port installs wxWidgets ${branch} for wxPython \
                            to guarantee compatibility even when the upstream versions \
                            of wxWidgets and wxPython differ.

    checksums               ${distname}-${version}${extract.suffix} \
                            rmd160  a7f404c8a64ca68fa31e408bded4387c2722196b \
                            sha256  346879dc554f3ab8d6da2704f651ecb504a22e9d31c17ef5449b129ed711585d \
                            size    20172909

    checksums-append        ${wxpythonsubset}${extract.suffix} \
                            rmd160  09280fd9ad1670162e38719e0055de5e6ddbc059 \
                            sha256  bc886064820ac90997e10623a591e26ee8b08d73711e8d6d0512eaaffb5e9362 \
                            size    426396

    patchfiles-append       patch-upstream-configure-highsierra.diff \
                            patch-upstream-src-osx-carbon-fontdlgosx.mm.diff \
                            patch-upstream-src-stc-scintilla-src-Editor.cxx.diff \
                            patch-upstream-strvararg.diff \
                            patch-upstream-webkit-proper-types.diff

    # https://trac.macports.org/ticket/52069
    if {${os.platform} eq "darwin" && ${os.major} >= 16} {
        # The patch probably works on all OS X versions,
        # but I don't want to risk breaking anything
        patchfiles-append \
                            patch-upstream-noquicktime.diff
        # the sources should be patched,
        # but this should work as a temporary workaround for Sierra users
        configure.args-append \
                            --disable-mediactrl
    }

    post-destroot {
        # copy a subset of wxPython to wxWidgets to prevent a conflict
        # between py2X-wxpython-3.0 and py2Y-wxpython-3.0
        copy ${workpath}/wxPython ${destroot}${wxWidgets.prefix}/include/wx-${branch}/wx/
    }

    livecheck.type          none
} elseif {${subport} eq "wxgtk-3.0"} {
    # patch could be added to wxWidgets-3.0, but it is not needed for Cocoa
    #
    # patch-sdl.diff: the patch allows using --with-sdl
    # patch-gtk-quartz.diff: avoid build failure for wxgtk3 +quartz (the patch is not yet complete)
    patchfiles-append       patch-sdl.diff \
                            patch-gtk-quartz.diff

    depends_build-append    port:pkgconfig
    depends_lib-append      path:lib/pkgconfig/cairo.pc:cairo \
                            port:libsdl \
                            port:libsdl_mixer

    configure.args-delete   --with-cocoa \
                            --without-sdl
    configure.args-append   --with-sdl

    #variant gtk2 conflicts gtk3 description {} {
    #    depends_lib-append      path:lib/pkgconfig/gtk+-2.0.pc:gtk2
    #    configure.args-append   --with-gtk=2
    #    set wxtype              gtk2
    #}
    #variant gtk3 conflicts gtk2 description {} {
        depends_lib-append      path:lib/pkgconfig/gtk+-3.0.pc:gtk3
        configure.args-append   --with-gtk=3
    #}
    variant quartz conflicts x11 description "(Doesn't work properly yet)" {
       require_active_variants path:lib/pkgconfig/cairo.pc:cairo quartz
       require_active_variants gtk3 quartz
    }
    variant x11 conflicts quartz {
        depends_lib-append  port:mesa port:libGLU
        require_active_variants path:lib/pkgconfig/cairo.pc:cairo x11
        require_active_variants gtk3 x11
    }
    #if{![variant_isset gtk2] && ![variant_isset gtk3]} {
    #    default_variants-append +gtk3
    #}
    if {![variant_isset quartz] && ![variant_isset x11]} {
    #   if {[active_variants gtk3 quartz ""]} {
    #       default_variants-append +quartz
    #   } else {
            default_variants-append +x11
    #   }
    }
    #if{[variant_isset gtk2]} {
    #    if{[variant_isset quartz]} {
    #        require_active_variants gtk2 quartz
    #    } else {
    #        require_active_variants gtk2 x11
    #    }
    #} else {
    #    if{[variant_isset quartz]} {
    #        require_active_variants gtk3 quartz
    #    } else {
    #        require_active_variants gtk3 x11
    #    }
    #}
} elseif {${subport} eq "wxgtk-3.0-cxx11"} {
    patchfiles-append       patch-sdl.diff

    depends_build-append    port:pkgconfig
    depends_lib-append      path:lib/pkgconfig/cairo.pc:cairo \
                            path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
                            port:libGLU \
                            port:libsdl2 \
                            port:libsdl2_mixer \
                            port:mesa

    require_active_variants path:lib/pkgconfig/cairo.pc:cairo x11
    require_active_variants gtk3 x11

    configure.args-replace  --with-cocoa --with-gtk=3 \
                            --without-sdl --with-sdl
}

if {${subport} eq "wxgtk-3.0" || ${subport} eq "wxgtk-3.0-cxx11"} {
    # Address this bug: https://trac.macports.org/ticket/70124
    patchfiles-append       patch-no-osx-evtloopsrc.h.diff
}

post-destroot {
    set confscript ${wxWidgets.prefix}/lib/wx/config/${wxtype}-unicode-${branch}
    ln -sf ${confscript} ${destroot}${wxWidgets.prefix}/bin/wx-config
}

variant universal {
    set archs [join ${configure.universal_archs} ,]
    # --disable-precomp-headers is automatically selected with cocoa, but not with gtk
    # http://trac.wxwidgets.org/ticket/15454
    configure.args-append   --enable-universal_binary=${archs} \
                            --disable-precomp-headers
}

variant monolithic description {build only one library} {
    configure.args-append   --enable-monolithic
}

variant debug description {add debug info to libraries} {
    configure.args-append   --enable-debug
}

variant stdlib description {add support for various standard library features} {
    configure.args-append   --enable-stl \
                            --enable-std_containers \
                            --enable-std_iostreams \
                            --enable-std_string \
                            --enable-std_string_conv_in_wxstring
}

if {${subport} eq ${name}} {
    livecheck.url   https://api.github.com/repos/${github.author}/${github.project}/releases
    livecheck.regex wxWidgets-(3\\.0\\.\[\\d\\.\]+)${extract.suffix}
} else {
    livecheck.type  none
}
