# -*- 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                   clang_dependency 1.0
PortGroup                   debug 1.0
PortGroup                   legacysupport 1.1
PortGroup                   meson 1.0
PortGroup                   muniversal 1.0

# Please keep the glib2 and glib2-devel ports as similar as possible.

name                        glib2-devel
conflicts                   glib2
set my_name                 glib
version                     2.88.2
revision                    0
epoch                       1

checksums                   rmd160  4e3ac2590a0720de12ac3827067156c10485efd8 \
                            sha256  cf3f215a640c8a4257f14317586b8f1fdd25a10a93cb4bdda147c0f9ad88e74f \
                            size    5782636

set branch                  [join [lrange [split ${version} .] 0 1] .]
categories                  devel
maintainers                 {mascguy @mascguy} openmaintainer
license                     LGPL-2+
homepage                    https://wiki.gnome.org/Projects/GLib
dist_subdir                 glib2
distname                    ${my_name}-${version}
use_xz                      yes

description                 Library with data structure functions and other constructs
long_description            Glib is a library which includes support routines \
                            for C, such as lists, trees, hashes, memory \
                            allocation, and many other things.

master_sites                gnome:sources/${my_name}/${branch}/

# Disable unexpected download of subprojects
meson.wrap_mode             nodownload

# Needed for clock_gettime
legacysupport.newest_darwin_requires_legacy 15

patchfiles-append           libintl.patch
patchfiles-append           patch-gio-tests-meson.build.diff
patchfiles-append           patch-glib_gmem.c.diff
patchfiles-append           patch-glib_gunicollate.c.diff
patchfiles-append           patch-get-launchd-dbus-session-address.diff
patchfiles-append           patch-gmodule-gmodule-dl.c.diff
patchfiles-append           patch-meson_build-meson_options-appinfo.diff
patchfiles-append           patch-meson-build-python-path.diff
patchfiles-append           patch-meson_build-atomic-test-older-clang-versions.diff
patchfiles-append           universal.patch
patchfiles-append           patch-glib2-findfolders-before-SL.diff
patchfiles-append           patch-declarations.diff
patchfiles-append           patch-hardcoded-paths.diff
patchfiles-append           patch-do-not-break-build-deliberately.diff
patchfiles-append           patch-restore-macos-macros.diff

platform darwin {
    if {${os.major} < 11} {
        patchfiles-append   patch-gio_gcredentialsprivate.h.diff
        patchfiles-append   patch-gio_gcredentials.c.diff
        patchfiles-append   patch-gio_gsocket.h.diff

        # see: https://github.com/macos-powerpc/powerpc-ports/issues/102
        # needed on 10.5, possibly relevant for 10a190, safe for 10.6.8:
        patchfiles-append   patch-gio_gipv6tclassmessage.diff
    }
}

set py_ver                  3.14
set py_ver_nodot            [string map {. {}} ${py_ver}]

depends_build-append        port:gettext \
                            path:bin/pkg-config:pkgconfig \
                            port:gobject-introspection-bootstrap-devel

depends_lib-append          port:gettext-runtime \
                            port:libelf \
                            port:libffi \
                            port:libiconv \
                            port:pcre2 \
                            port:python${py_ver_nodot} \
                            port:zlib

# Python only needed for scripts
depends_skip_archcheck-append \
                            python${py_ver_nodot}

compiler.cxx_standard       2011

# Issues for Clang 5.x:
# - fatal error: error in backend: Cannot select: 0x103357f10: i8,ch = AtomicLoad 0x10334b410,
#   0x103354b10<Volatile LD1[@is_running.b]> [ID=18]
# - ERROR: Problem encountered: GLib requires a C++ compiler with support for C99 __VA_ARG__ in macros.
# Issues for Clang 6.x:
# - gcocoanotificationbackend.c:115:52: error: array subscript is not an integer
# - Checking if "GCC size_t typedef is long" compiles: NO
compiler.blacklist-append   {clang < 700}

configure.cflags-append     -fstrict-aliasing

# stop excessive warnings
configure.cflags-append     -Wno-deprecated-declarations
configure.objcflags-append  -Wno-deprecated-declarations

configure.args-append       -Ddefault_library=both \
                            -Dlibelf=enabled \
                            -Dlibmount=disabled \
                            -Dwarning_level=0 \
                            -Dintrospection=enabled \
                            --localstatedir=${prefix}/var \
                            -Druntime_dir=${prefix}/run \
                            -Dgio_module_dir=${prefix}/lib/gio/modules \
                            -Dbsymbolic_functions=false

configure.perl              /usr/bin/perl
configure.python            ${prefix}/bin/python${py_ver}
configure.env-append        PERL_PATH=${configure.perl} \
                            PATH=${prefix}/libexec/gobject-introspection-bootstrap-devel/bin:$env(PATH)
configure.pkg_config_path-prepend   ${prefix}/libexec/gobject-introspection-bootstrap-devel/lib/pkgconfig \
                                    ${prefix}/libexec/glib2-bootstrap-devel/lib/pkgconfig

build.env-append            PKG_CONFIG_PATH=${prefix}/libexec/gobject-introspection-bootstrap-devel/lib/pkgconfig:${prefix}/libexec/glib2-bootstrap-devel/lib/pkgconfig \
                            PATH=${prefix}/libexec/gobject-introspection-bootstrap-devel/bin:$env(PATH) \
                            CC=${configure.cc}

if {${universal_possible} && [variant_isset universal]} {
    foreach my_arch {ppc ppc64 i386 x86_64 arm64} {
        # strip the automatic setting of host, meson does not accept
        set merger_host(${my_arch}) ""
    }
}

post-patch {
    reinplace -W ${worksrcpath} "s|@PYTHON@|${configure.python}|" \
        meson.build

    reinplace -W ${worksrcpath} "s|@PREFIX@|${prefix}|g" \
        gio/xdgmime/xdgmime.c \
        glib/gi18n-lib.h \
        glib/gi18n.h \
        gio/gdbusaddress.c \
        girepository/girepository.c \
        glib/gutils.c

    # Upstream mixes three variations of Python shebangs, so account for all
    fs-traverse f ${worksrcpath} {
        if {[string match *.py ${f}]} {
            ui_info "patching testpath: ${f}"
            reinplace -q "s|/usr/bin/python3$|${configure.python}|" ${f}
            reinplace -q "s|/usr/bin/env python$|${configure.python}|" ${f}
            reinplace -q "s|/usr/bin/env python3$|${configure.python}|" ${f}
        }
    }
}

build.args-append           --verbose

post-build {
    if {[variant_exists universal] && [variant_isset universal]} {
        set dirs {}
        foreach arch ${universal_archs_to_use} {
            lappend dirs ${workpath}/build-${arch}
        }
    } else {
        set dirs ${workpath}/build
    }
    foreach dir ${dirs} {
        # -lm is spuriously added by meson NYD to some builds (arm64)
        reinplace -q {s| -lm||g}              ${dir}/meson-private/glib-2.0.pc
    }
}

test.args-append            --verbose
test.run                    yes
test.target                 test

post-destroot {
    delete ${destroot}${prefix}/lib/charset.alias

    if {${subport} ne "glib2-bootstrap-devel"} {
        set docdir ${prefix}/share/doc/${my_name}
        xinstall -d ${destroot}${docdir}
        xinstall -m 0644 -W ${worksrcpath} COPYING NEWS README.md \
            ${destroot}${docdir}
    }
}

if {${os.platform} ne "linux"} {
    configure.args-append   -Ddtrace=disabled
}

platform darwin {
    # some tests fail to build on 10.6.8 Rosetta: https://trac.macports.org/ticket/65517
    # for version 2.88.0 there are also failures on 10.6 and x86_64:
    # ../glib-2.88.0/glib/tests/testing.c:470:39: error: use of undeclared identifier 'F_DUPFD_CLOEXEC'
    # so just disable the tests on 10.6 and below
    if {${os.major} <= 10} {
        configure.args-append   -Dtests=false
    }

    if {${os.major} == 9 && ${configure.build_arch} in [list ppc64 x86_64]} {
        # https://trac.macports.org/ticket/67307
        configure.cflags-append -D__DARWIN_NON_CANCELABLE=1
    }
}

variant quartz conflicts x11 {
    configure.args-append   -Dappinfo_backend=native
}

variant x11 conflicts quartz {
    configure.args-append   -Dappinfo_backend=generic
}

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

subport glib2-bootstrap-devel {
    conflicts
    description             Glib2 build with introspection disabled, used to build gobject-introspection \
                            and avoid circular dependency.
    long_description        {*}${description} \nSee \
                            https://discourse.gnome.org/t/dealing-with-glib-and-gobject-introspection-circular-dependency/18701

    depends_build-delete    port:gobject-introspection-bootstrap-devel

    configure.pre_args      setup --prefix=${prefix}/libexec/glib2-bootstrap-devel
    configure.args-append   -Ddocumentation=false -Dman-pages=disabled -Dnls=disabled
    configure.args-replace  -Dintrospection=enabled -Dintrospection=disabled
}

livecheck.type              gnome
livecheck.name              ${my_name}

pre-activate {
    # the new glib2 port now installs files that were previously in gobject-introspection({,-devel} ports
    if {![catch {lindex [registry_active gobject-introspection] 0} installed]} {
        set _version [lindex $installed 1]
        if {[vercmp $_version 1.86.0] < 0} {
            registry_deactivate_composite gobject-introspection "" [list ports_nodepcheck 1]
        }
    }

    if {![catch {lindex [registry_active gobject-introspection-devel] 0} installed]} {
        set _version [lindex $installed 1]
        if {[vercmp $_version 1.86.0] < 0} {
            registry_deactivate_composite gobject-introspection-devel "" [list ports_nodepcheck 1]
        }
    }
}
