# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Maintainer: team/gnome <pabloyoyoista@postmarketos.org>
pkgname=glib
pkgver=2.84.3
_gi_version=1.82.0
pkgrel=0
pkgdesc="Common C routines used by Gtk+ and other libs"
url="https://developer.gnome.org/glib/"
arch="all"
license="LGPL-2.1-or-later"
triggers="$pkgname.trigger=/usr/share/glib-2.0/schemas:/usr/lib/gio/modules:/usr/lib/gtk-4.0"
depends_dev="
	bzip2-dev
	docbook-xml
	docbook-xsl
	gettext-dev
	libxml2-utils
	libxslt
	python3
	py3-packaging
	"
makedepends="$depends_dev
	bash
	bison
	flex
	libffi-dev
	meson
	pcre2-dev
	py3-setuptools
	python3-dev
	util-linux-dev
	zlib-dev
	py3-docutils
	"
subpackages="
	$pkgname-dbg
	$pkgname-doc
	$pkgname-static
	$pkgname-dev
	$pkgname-lang
	$pkgname-bash-completion
	"
source="https://download.gnome.org/sources/glib/${pkgver%.*}/glib-$pkgver.tar.xz
	https://download.gnome.org/sources/gobject-introspection/${_gi_version%.*}/gobject-introspection-$_gi_version.tar.xz
	tests-machine-id.patch
	flaky-tests.patch
	"

# 2.80 moves gir here
replaces="gobject-introspection<1.80.0"

[ "$CARCH" = "ppc64le" ] && options="$options !check" # https://gitlab.gnome.org/GNOME/glib/-/issues/3474

# secfixes:
#   2.80.1-r0:
#     - CVE-2024-34397
#   2.66.6-r0:
#     - CVE-2021-27219 GHSL-2021-045
#   2.62.5-r0:
#     - CVE-2020-6750
#   2.60.4-r0:
#     - CVE-2019-12450

build() {
	export CFLAGS="$CFLAGS -ffat-lto-objects -O2"
	export CXXFLAGS="$CXXFLAGS -O2"
	export CPPFLAGS="$CPPFLAGS -O2"
	local _prefix="$PWD/boostrap-glib"

	msg "build bootstrap glib"
	meson setup \
		--default-library=shared \
		--prefix="$_prefix" \
		-Dman-pages=disabled \
		-Dlibmount=disabled \
		-Dtests=false \
		-Dintrospection=disabled \
		-Dnls=disabled \
		. output
	meson compile -C output
	meson install --no-rebuild -C output

	msg "build bootstrap gobject-introspection"
	meson setup \
		--pkg-config-path="$_prefix"/lib/pkgconfig \
		--prefix="$_prefix" \
		-Dbuild_introspection_data=false \
		-Dcairo=disabled \
		-Ddoctool=disabled \
		"$srcdir"/gobject-introspection-$_gi_version \
		gioutput
	meson compile -C gioutput
	meson install --no-rebuild -C gioutput

	msg "re-build glib with introspection"
	PATH="$_prefix/bin:$PATH" LD_LIBRARY_PATH="$_prefix/lib" \
		meson setup \
		--reconfigure \
		--pkg-config-path="$_prefix"/lib/pkgconfig \
		--default-library=both \
		--prefix=/usr \
		-Dman-pages=enabled \
		-Dlibmount=enabled \
		-Dtests="$(want_check && echo true || echo false)" \
		-Dintrospection=enabled \
		-Dnls=enabled \
		. output
	PATH="$_prefix/bin:$PATH" LD_LIBRARY_PATH="$_prefix/lib" \
		meson compile -C output
}

check() {
	# riscv64 and armhf may time out with the standard timeout
	meson test --timeout-multiplier 10 --print-errorlogs -C output
}

package() {
	DESTDIR="$pkgdir" meson install --no-rebuild -C output
}

dev() {
	# 2.80 moves gir here
	replaces="gobject-introspection-dev<1.80.0"
	default_dev
	mkdir -p "$subpkgdir"/usr/bin "$subpkgdir"/usr/share
	find "$pkgdir"/usr/bin ! -name "glib-compile-schemas" -a \( \
		-name "gdbus-codegen" -o \
		-name "gobject-query" -o \
		-name "gresource" -o \
		-name "gtester*" -o \
		-name "glib-*" \) \
		-exec mv {} "$subpkgdir"/usr/bin \;

	amove usr/share/gdb usr/share/glib-2.0
}

static() {
	default_static
	depends="gettext-static"
}

sha512sums="
73f2d67d2ef5b4dc8cd2f6df9ce7903853ec619924e2927adbc73d706974a7d660afea55be18e12ccb0dee1145cf4149b743278d2b128fd466e3df2bbf90ef57  glib-2.84.3.tar.xz
e139fadb4174c72b648914f3774d89fc0e5eaee45bba0c13edf05de883664dad8276dbc34006217bb09871ed4bad23adab51ff232a17b9eb131329b2926cafb7  gobject-introspection-1.82.0.tar.xz
4f561bfede4a559b314195aa4b7b5923a52c62063e6dd33b4933ad53b29c32ef789d69b9f7b17d8d701323006366e6b7acbb083a68674c1c535c185d35cc5d1f  tests-machine-id.patch
268cf6f089274f29285242f1e29bf5650e85f9fcb5744df884deb139493cc25792ad78487113732665f1c5393c94b2f76f97093a1a003e3caaf65c07960a1bf9  flaky-tests.patch
"
