# -*- 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                   compiler_blacklist_versions 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.78.4
revision                    0

checksums                   rmd160  7941be85af18c428d86f4a00e52f41542b061aa9 \
                            sha256  24b8e0672dca120cc32d394bccb85844e732e04fe75d18bb0573b2dbc7548f63 \
                            size    5331892

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

# Disable unexpected download of subprojects
meson.wrap_mode             nodownload

# Needed for clock_gettime
legacysupport.newest_darwin_requires_legacy 15

patchfiles-append           libintl.patch \
                            patch-gio-tests-meson.build.diff \
                            patch-glib_gmem.c.diff \
                            patch-glib_gspawn.c.diff \
                            patch-glib_gunicollate.c.diff \
                            patch-gio_xdgmime_xdgmime.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 \
                            patch-declarations.diff \
                            no-distutils.patch

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

    if {${os.major} == 10 && ${build_arch} eq "ppc"} {
        patchfiles-replace  patch-glib2-findfolders-before-SL.diff patch-glib2-findfolders-before-Lion.diff
    }
}

depends_build-append        port:gettext \
                            bin:xmllint:libxml2 \
                            path:bin/pkg-config:pkgconfig

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

depends_lib-append          port:gettext-runtime \
                            port:libelf \
                            port:libffi \
                            port:libiconv \
                            port:pcre2 \
                            port:python${py_ver_nodot} \
                            port:py${py_ver_nodot}-packaging \
                            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}

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

# -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-append \
                            -Ddefault_library=both \
                            -Dlibelf=enabled \
                            -Dlibmount=disabled \
                            -Dwarning_level=0

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

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

    reinplace -W ${worksrcpath} "s|data_dirs = \"/usr|data_dirs = \"${prefix}/share:/usr|g" \
        glib/gutils.c

    reinplace -W ${worksrcpath} "s|path = \"/bin|path = \"${prefix}/bin:/bin|g" \
        glib/gutils.c \
        glib/gspawn.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}
        }
    }
}

# 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} COPYING NEWS README.md \
        ${destroot}${docdir}
}

platform darwin {
    configure.args-append   -Ddtrace=false
    
    # some tests fail to build on 10.6.8 Rosetta: https://trac.macports.org/ticket/65517
    if {${os.major} == 10 && ${build_arch} eq "ppc"} {
        configure.args-append   -Dtests=false
    }
}

platform darwin 8 {
    # https://trac.macports.org/ticket/67307
    configure.cflags-append -D__DARWIN_NON_CANCELABLE=1

    # the @loader_path linkages in the test binaries can't find the dylibs
    configure.args-append   -Dtests=false

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