# Contributor: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
# Contributor: Jan de Groot <jgc@archlinux.org>

_pkgname=glib2
pkgname=static-compat-$_pkgname
pkgver=2.85.2
pkgrel=1
pkgdesc="Low level core library"
url="https://gitlab.gnome.org/GNOME/glib"
arch=(x86_64)
depends=(static-compat-pcre2 static-compat-libffi static-compat-util-linux static-compat-zlib)
makedepends=(gettext shared-mime-info python libelf git static-compat-meson)
checkdepends=(desktop-file-utils)
options=(!strip !emptydirs staticlibs)
source=(
  "git+https://gitlab.gnome.org/GNOME/glib.git#tag=$pkgver"
  "git+https://gitlab.gnome.org/GNOME/gvdb.git"
  0001-glib-compile-schemas-Remove-noisy-deprecation-warnin.patch
  0002-gdesktopappinfo-Add-more-known-terminals.patch
)
b2sums=('c48a8b560ee8dd90de08f9c8d044db2b368bdc956f7eb7d83697dbdf44efa7ec6443f6d53a7d16e8dd9b1b995ab8ef7a06fcb3f833c733f53c0cf57ac3f87a61'
        'SKIP'
        '4ddbd31f5f466fce99d82890292ff922555a9ab379d22202aeea5127f58798668f871dea0485cc0f458069276ad512412285ede6c8f3e36bea899358f49e931a'
        'bf57425e3081a8f5d36d6a54eff1bfa93ba6bab8f0a4d3f3bf1e319ebfa71d99ce6a0466166fc694f53c5bd151e9cc65339e222c48e963f0cdc075852d0e1f7c')

pkgver() {
  cd glib
  git describe --tags | sed 's/[^-]*-g/r&/;s/-/+/g'
}

prepare() {
  cd glib

  # Drop dep on libatomic
  # https://gitlab.archlinux.org/archlinux/packaging/packages/qemu/-/issues/6
  git revert -n 4e6dc4dee0e1c6407113597180d9616b4f275f94
  sed -i '/    atomic_dep,/d' glib/meson.build

  # Suppress noise from glib-compile-schemas.hook
  git apply -3 ../0001-glib-compile-schemas-Remove-noisy-deprecation-warnin.patch

  # Add ghostty and ptyxis to known terminals list
  # This is a downstream only patch; GNOME will not add new terminal emulators.
  # https://gitlab.gnome.org/GNOME/glib/-/issues/338#note_1076172
  git apply -3 ../0002-gdesktopappinfo-Add-more-known-terminals.patch

  git submodule init
  git submodule set-url subprojects/gvdb "$srcdir/gvdb"
  git -c protocol.file.allow=always submodule update
}

build() {
  export CFLAGS+=" -DG_DISABLE_CAST_CHECKS"
  export LDFLAGS+=" -lblkid"
  source static-compat-environment
  static-compat-meson glib build \
    -D selinux=disabled \
    -Dsysprof=disabled \
    -D man-pages=disabled \
    -D documentation=false
  meson compile -C build
}

check() {
  source static-compat-environment
  meson test -C build --no-suite flaky --no-suite slow --print-errorlogs || true
}

package() {
  source static-compat-environment
  DESTDIR="$pkgdir" meson install -C build

  # Strip messes up the LTO-enabled archive's index so strip libs manually and then run ranlib again
  find "$pkgdir" -name '*.a' -exec strip $STRIP_STATIC {} \;
  find "$pkgdir" -name '*.a' -exec ranlib {} \;

  export PYTHONHASHSEED=0
  python -m compileall -d "$static_compat_prefix"/share/glib-2.0/codegen \
    "$pkgdir/$static_compat_prefix/share/glib-2.0/codegen"
  python -O -m compileall -d "$static_compat_prefix"/share/glib-2.0/codegen \
    "$pkgdir/$static_compat_prefix/share/glib-2.0/codegen"
}

# vim:set sw=2 et:
