# -*- 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                   compiler_blacklist_versions 1.0
PortGroup                   clang_dependency 1.0
PortGroup                   meson 1.0
PortGroup                   muniversal 1.0

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

name                        glib2
conflicts                   glib2-devel
set my_name                 glib
version                     2.64.6
revision                    1
checksums                   rmd160  27938fd75bdcd4ca0afc8ebce6b137ed3c1fe6a5 \
                            sha256  c36ee07a70164c71f046016fe6aaacd6368333c42590bc0cba47c344ffb853f1 \
                            size    4781576

set branch                  [join [lrange [split ${version} .] 0 1] .]
categories                  devel
maintainers                 {ryandesign @ryandesign} openmaintainer
license                     LGPL-2+
homepage                    https://wiki.gnome.org/Projects/GLib
platforms                   darwin
dist_subdir                 glib2
distname                    ${my_name}-${version}
use_xz                      yes
use_parallel_build          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}/

patchfiles                  libintl.patch \
                            implicit.patch \
                            patch-gio-tests-meson.build.diff \
                            patch-glib-gmain.c.diff \
                            patch-glib_gunicollate.c.diff \
                            patch-gio_xdgmime_xdgmime.c.diff \
                            patch-gio_gdbusprivate.c.diff \
                            patch-get-launchd-dbus-session-address.diff \
                            patch-gmodule-gmodule-dl.c.diff \
                            patch-meson_build-meson_options-appinfo.diff \
                            patch-meson-build-python-path.diff \
                            patch-meson_build-atomic-test-older-clang-versions.diff \
                            universal.patch \
                            patch-glib2-findfolders-before-SL.diff

# this test fails (I believe wrongly) when configuring arm64 on Intel
patchfiles-append           patch-glib2-allow-frexpl-test-to-pass.diff

depends_build-append        port:gettext \
                            bin:xmllint:libxml2 \
                            port:pkgconfig

depends_lib                 port:gettext-runtime \
                            port:libffi \
                            port:libiconv \
                            port:pcre \
                            port:python39 \
                            port:zlib

if {[vercmp ${macosx_deployment_target} 10.9] < 0} {
    # fatal error: error in backend: Cannot select: 0x103357f10: i8,ch = AtomicLoad 0x10334b410, 0x103354b10<Volatile LD1[@is_running.b]> [ID=18]
    compiler.blacklist-append   {clang < 500}
} else {
    # gcocoanotificationbackend.c:115:52: error: array subscript is not an integer
    compiler.blacklist-append   {clang < 600}
}

# https://bugzilla.gnome.org/show_bug.cgi?id=697017
compiler.blacklist-append   gcc-3.3 *gcc-4.0 *clang*
compiler.whitelist macports-gcc-7

# -lresolv is needed at least on Tiger PPC.
configure.ldflags-append    -lresolv \
                            -bind_at_load

configure.cflags-append     -fstrict-aliasing

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

configure.args              -Ddefault_library=both \
                            -Dwarning_level=0

configure.perl              /usr/bin/perl
configure.python            ${prefix}/bin/python3.9
configure.env-append        PERL_PATH=${configure.perl}


if {![info exists universal_possible]} {
    set universal_possible [expr {${os.universal_supported} && [llength ${configure.universal_archs}] >= 2}]
}
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/gdbusprivate.c gio/xdgmime/xdgmime.c glib/gi18n-lib.h glib/gi18n.h gio/gdbusaddress.c
    reinplace "s|data_dirs = \"/usr|data_dirs = \"${prefix}/share:/usr|g" ${worksrcpath}/glib/gutils.c
    reinplace "s|path = \"/bin|path = \"${prefix}/bin:/bin|g" ${worksrcpath}/glib/gutils.c ${worksrcpath}/glib/gspawn.c
}

# this edit does not, for some reason, apply against the build-arm64 config.h
# and should not be needed if we're using the muniversal PortGroup
# needs testing on 32 bit systems to be certain of this
# post-configure {
#     system "ed - ${build.dir}/config.h < ${filespath}/config.h.ed"
# }

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

    set docdir ${prefix}/share/doc/${my_name}
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} AUTHORS COPYING HACKING NEWS README.md \
        ${destroot}${docdir}
}

platform darwin {
    configure.args-append   -Ddtrace=false
}

platform darwin 8 {
    # meson on Tiger cannot use rpaths, so we workaround with this to find dylibs
    foreach my_phase {build test destroot} {
        ${my_phase}.env-append  "DYLD_LIBRARY_PATH=${build_dir}/glib:${build_dir}/gobject:${build_dir}/gio:${build_dir}/gthread:${build_dir}/gmodule"
    }
}

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

livecheck.type              gnome
livecheck.name              ${my_name}
