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

name                gobject-introspection-devel
conflicts           gobject-introspection
set my_name         gobject-introspection

version             1.86.0
revision            0
epoch               1

categories          gnome
# library under LGPL-2+, tools under GPL-2+
license             {LGPL GPL-2+}
maintainers         {mascguy @mascguy} openmaintainer

description         GObject Introspection
long_description    Gobject Introspection acts as a bridge between low-level C+GObject code and \
                    a managed runtime (JavaScript, Python, Java, Lua, .NET, Scheme, etc.).
homepage            https://wiki.gnome.org/Projects/GObjectIntrospection

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

checksums           rmd160  eac2231a5cd5fe900b122c845e66650be69334b3 \
                    sha256  920d1a3fcedeadc32acff95c2e203b319039dd4b4a08dd1a2dfd283d19c0b9ae \
                    size    1083172

# Disable unexpected download of subprojects
meson.wrap_mode     nodownload

# https://trac.macports.org/ticket/71191
legacysupport.newest_darwin_requires_legacy 0

if {${subport} eq ${name}} {
    legacysupport.redirect_bins g-ir-compiler g-ir-generate g-ir-inspect g-ir-scanner
}

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

depends_build-append \
                    port:bison \
                    port:flex

depends_lib-append  path:lib/pkgconfig/cairo.pc:cairo-devel \
                    path:lib/pkgconfig/glib-2.0.pc:glib2-devel \
                    path:bin/pkg-config:pkgconfig \
                    port:libffi \
                    port:python${py_ver_nodot} \
                    port:py${py_ver_nodot}-mako \
                    port:py${py_ver_nodot}-markdown \
                    port:py${py_ver_nodot}-setuptools

patchfiles-append   patch-fix-rpath-gir-typelib.diff
patchfiles-append   patch-fix-scanner-in-build-execution.diff
patchfiles-append   patch-fix-tools-python.diff
patchfiles-append   patch-prefix-giscanner_transformer.py.diff

# Fix library search path on non-/usr/local installs (e.g. Apple Silicon)
# See: https://github.com/Homebrew/homebrew-core/issues/75020
#      https://gitlab.gnome.org/GNOME/gobject-introspection/-/merge_requests/273
patchfiles-append   patch-library-search-path.diff

post-patch {
    reinplace "s|libcairo-gobject\\.2\\.dylib|${prefix}/lib/libcairo-gobject.2.dylib|g" ${worksrcpath}/gir/meson.build
    reinplace "s|@MP_LIB@|${prefix}/lib|g" ${worksrcpath}/giscanner/shlibs.py ${worksrcpath}/giscanner/ccompiler.py
    reinplace "s|@MP_PYTHON_CMD@|${configure.python}|g" ${worksrcpath}/tools/meson.build
    reinplace "s|#!/usr/bin/env python3|#!${configure.python}|g" \
        misc/update-gtkdoc-tests.py \
        misc/update-vulkan-gir.py \
        tests/gi-tester
    reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/giscanner/transformer.py
}

configure.python    ${prefix}/bin/python${py_ver}

configure.args-append \
                    -Ddoctool=enabled \
                    -Dpython=${configure.python} \
                    -Dextra_library_paths=${prefix}/lib

compiler.c_standard 2011

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

    depends_lib-delete      path:lib/pkgconfig/cairo.pc:cairo-devel
    depends_lib-replace     path:lib/pkgconfig/glib-2.0.pc:glib2-devel \
                            port:glib2-bootstrap-devel

    configure.pre_args      setup --prefix=${prefix}/libexec/gobject-introspection-bootstrap-devel
    configure.args-append   -Dcairo=disabled
    configure.pkg_config_path-prepend   ${prefix}/libexec/glib2-bootstrap-devel/lib/pkgconfig

    build.env-append        PKG_CONFIG_PATH=${prefix}/libexec/glib2-bootstrap-devel/lib/pkgconfig
}

# By default, gir will attempt to link programs using
# the same compiler used to build Python itself. However,
# the system clang on 10.6 does not appear to respect
# LIBRARY_PATH, resulting in g-ir-scanner errors such as:
#
# ld: library not found for -lgio-2.0
#
# The following workaround selects a more up-to-date linker
# when building this project. Identical logic already exists
# in the gobject_introspection PortGroup, so dependent projects
# should not require modification.
#
# 10.6.8 Rosetta requires a specific solution: https://github.com/mesonbuild/meson/issues/10351

build.env-append    CC=${configure.cc}

#--------------------------------------------------------------------------------------------------
# Note that several tests fail, due to @rpath issues with temporary test-only libs.
# Needs to be reported to upstream.
#
#  7/65 test_shlibs.py                          FAIL            2.67s   exit status 1
#    sanitize_shlib_path('@rpath/libbarapp-1.0.dylib'),
#    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#    TypeError: sanitize_shlib_path() missing 1 required positional argument: 'libdir'
#
#
# 53/65 gitypelibtest                           FAIL            0.33s   killed by signal 5 SIGTRAP
#   Failed to load shared library '/opt/local/lib/libregress-1.0.dylib' referenced by the
#   typelib: dlopen(/opt/local/lib/libregress-1.0.dylib, 9): image not found
#--------------------------------------------------------------------------------------------------
test.run            yes

livecheck.type      gnome
livecheck.name      ${my_name}
