# -*- 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           active_variants 1.1
PortGroup           legacysupport 1.1
PortGroup           meson 1.0

# O_CLOEXEC
legacysupport.newest_darwin_requires_legacy 10

name                gtk4
conflicts           gtk4-devel
set my_name         gtk4

# GNOME project name
set gname           gtk

version             4.14.3
revision            0

categories          gnome
license             LGPL-2.1+
maintainers         {mascguy @mascguy} openmaintainer

description         ${my_name} is a companion library to GObject and Gtk+.
long_description    ${description}  It includes a collection of dazzling Gtk widgets, \
                    data structures, search engines, a shortcut engine, panels, \
                    desktop integration, and those missing pieces from common libraries \
                    that help you write cleaner and safer code
homepage            https://gitlab.gnome.org/GNOME/${gname}

set branch          [join [lrange [split ${version} .] 0 1] .]
master_sites        gnome:sources/${gname}/${branch}/
distname            ${gname}-${version}
dist_subdir         ${my_name}
use_xz              yes

checksums           rmd160  058ea1318ea0e21d678a95278e1f19696763ae10 \
                    sha256  2be5c858bdef1104d37848c9779c089372c8d31503f6efc4b94e53b546fc9a43 \
                    size    18340444

# Disable unexpected download of subprojects
meson.wrap_mode     nodownload

set python_branch   3.12
set python_version  [string map {. {}} ${python_branch}]
set port_ver_major  [lindex [split ${version} .] 0]

# Specify name of executable for 'rst2man'. MacPorts-specific.
patchfiles-append   patch-docs-rst2man.diff

# Don't allow build to promote warnings to errors. Full details in patchfile.
# See: https://trac.macports.org/ticket/67985
patchfiles-append   patch-meson-build-debug.diff

post-patch {
    reinplace "s|@@PYTHON_VERSION@@|${python_branch}|g" \
        docs/reference/gtk/meson.build

    # build assumes python3 exists
    reinplace "s|^#!/usr/bin/env python3|#!${prefix}/bin/python${python_branch}|" \
        build-aux/meson/dist-data.py \
        build-aux/meson/gen-profile-conf.py \
        build-aux/meson/gen-visibility-macros.py \
        demos/gtk-demo/geninclude.py \
        gdk/broadway/gen-c-array.py \
        gdk/gen-gdk-gresources-xml.py \
        gsk/gen-gsk-gresources-xml.py \
        gsk/gpu/shaders/generate-header.py \
        gsk/gpu/shaders/process-glsl-includes.py \
        gtk/gen-gtk-gresources-xml.py \
        gtk/gentypefuncs.py \
        testsuite/introspection/api.py \
        tools/generate-uac-manifest.py

    # these are slightly different
    reinplace "s|python3|${prefix}/bin/python${python_branch}|" \
        examples/squares.py
}

depends_build-append \
                    port:docbook-xml \
                    port:docbook-xsl-nons \
                    port:gettext \
                    path:lib/pkgconfig/gobject-introspection-1.0.pc:gobject-introspection \
                    port:pkgconfig \
                    port:py${python_version}-docutils \
                    port:sassc

depends_lib-append \
                    path:lib/pkgconfig/cairo.pc:cairo \
                    port:fribidi \
                    path:lib/pkgconfig/gdk-pixbuf-2.0.pc:gdk-pixbuf2 \
                    port:gettext-runtime \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    port:graphene \
                    path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                    port:hicolor-icon-theme \
                    port:iso-codes \
                    port:libepoxy \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:libpng \
                    path:lib/pkgconfig/librsvg-2.0.pc:librsvg \
                    port:mesa \
                    path:lib/pkgconfig/pango.pc:pango \
                    port:tiff

license_noconflict  gobject-introspection

compiler.c_standard 2011
compiler.cxx_standard 2017

configure.args-append \
    -Dbuild-examples=false \
    -Dbuild-tests=false \
    -Dbuild-testsuite=false \
    -Ddocumentation=false \
    -Dintrospection=enabled \
    -Dman-pages=true \
    -Dmedia-gstreamer=disabled \
    -Dprint-cups=disabled

# 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_env(${arch}) \
                           "CC=${configure.cc} -arch ${arch}"
        lappend merger_destroot_env(${arch}) \
                           "CC=${configure.cc} -arch ${arch}"
    }
} else {
    build.env-append       "CC=${configure.cc} ${configure.cc_archflags}"
    destroot.env-append    "CC=${configure.cc} ${configure.cc_archflags}"
}

variant broadway description {Enable the broadway (HTML5) gdk backend} {
    configure.args-append   -Dbroadway-backend=true
}

variant quartz conflicts x11 {
    require_active_variants path:lib/pkgconfig/cairo.pc:cairo quartz
    require_active_variants path:lib/pkgconfig/pango.pc:pango quartz

    configure.args-append   -Dx11-backend=false -Dmacos-backend=true

    if {${os.platform} eq "darwin" && ${os.major} < 16} {
        # Add AppKit wrapper header to compiler library paths
        configure.cflags-prepend        -isystem ${filespath}/old_appkit_compat
        configure.cxxflags-prepend      -isystem ${filespath}/old_appkit_compat
        configure.objcflags-prepend     -isystem ${filespath}/old_appkit_compat
        configure.objcxxflags-prepend   -isystem ${filespath}/old_appkit_compat
    }

    if {${os.platform} eq "darwin" && ${os.major} < 15} {
        depends_build
        depends_lib
        depends_run
        archive_sites
        known_fail yes
        pre-fetch {
            ui_error "${name} +quartz is not supported on this os version at present."
            return -code error {unsupported platform}
        }
    }

}

variant x11 conflicts quartz {
    require_active_variants path:lib/pkgconfig/cairo.pc:cairo x11
    require_active_variants path:lib/pkgconfig/pango.pc:pango x11

    patchfiles-append        patch-gtk4-x11.diff

    depends_lib-append       port:libxkbcommon-x11 \
                             port:xorg-libXrandr \
                             port:xorg-libXcursor \
                             port:xorg-libXinerama \
                             port:xorg-libXi

    configure.args-append   -Dx11-backend=true -Dmacos-backend=false
}

if {![variant_isset quartz]} {
    # it is somewhat illogical to default to a variant that cannot build, however
    # this matches the rest of macports and will tweak users to build as +quartz manually
    default_variants +x11
}

variant tests description "build tests" {
    depends_test-append      port:py${python_version}-gobject3

    # NB. Most of the tests fail if gtk4 has already been installed before running them
    configure.args-replace   -Dbuild-tests=false \
                             -Dbuild-tests=true
    configure.args-replace   -Dbuild-testsuite=false \
                             -Dbuild-testsuite=true

    test.run                 yes
    test.cmd                 meson
    test.target              test
}

platform darwin 8 {
    # meson on Tiger cannot use rpaths, so we workaround with this to find dylib
    build.env-append       "DYLD_LIBRARY_PATH=${build_dir}/${gname}"
    destroot.env-append    "DYLD_LIBRARY_PATH=${build_dir}/${gname}"
}

livecheck.type      gnome
livecheck.name      ${gname}
livecheck.regex     "LATEST-IS-(${port_ver_major}\\.\\d*\[02468\](?:\\.\\d+)*)"
