# Template file for 'glibc'
pkgname=glibc
version=2.44
revision=6
short_desc="GNU C library"
maintainer="finwo <finwo@pm.me>"
license="LGPL-2.1-or-later"
homepage="https://www.gnu.org/software/libc/"
distfiles="https://ftp.gnu.org/gnu/glibc/glibc-2.44.tar.xz"
checksum=37f600f2bef3c5e8300147059568b2a2e40a7ad6ccc65ce942556d49429cc667
# runtime needs from base-files: /etc/nsswitch.conf, /etc/ld.so.conf.
# busybox first: the post-install runs under /bin/sh (see libmnl).
depends="base-files busybox"
hostmakedepends="python3 bison gawk sed make"

# Notes:
# - glibc forbids in-source builds; configure runs in a sibling build dir.
# - Upstream mandates python3 for the build (allowed: build-only, never
#   shipped). No `make check` locally (slow/flaky outside CI); CI runs it.
# - --enable-kernel=5.4: floor for any plausible UNOS kernel (generic target
#   and switch kernels are 6.x; ONIE's kernel is irrelevant).
# - --libdir=/usr/lib (slibdir stays /lib64 for the loader + libc): without
#   it glibc defaults libdir to /usr/lib64 on x86_64, scattering libraries
#   across /lib64 + /usr/lib64 while every other UNOS package installs to
#   /usr/lib - and the loader's baked-in search then misses /usr/lib
#   entirely (`ip` failed to find libmnl). With /usr/lib as libdir the
#   loader searches /lib64 + /usr/lib and one layout covers everything.
# - Single fat package (runtime + headers + static libs); split if size
#   ever matters. No debuginfo split either.
# - Locales: C.UTF-8 only. No tzdata (UTC everywhere).

do_configure() {
  export LC_ALL=C
  mkdir -p "${WORK}/build"
  cd "${WORK}/build"
  "${WRKSRC}/configure" \
    --prefix=/usr \
    --libdir=/usr/lib \
    --enable-kernel=5.4 \
    --disable-werror
}

do_build() {
  cd "${WORK}/build"
  make -j"$(nproc)"
}

do_install() {
  cd "${WORK}/build"
  make install DESTDIR="${DESTDIR}"
  # make install does not create the locale dir itself
  mkdir -p "${DESTDIR}/usr/lib/locale"
  # localedef is linked against the just-built libc (2.44) but the host is
  # bookworm (2.36); run it against its own libs so GLIBC_2.38+ symbols resolve.
  LD_LIBRARY_PATH="${WORK}/build:${WORK}/build/elf${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}" \
    ./locale/localedef --prefix="${DESTDIR}" -i C -f UTF-8 C.UTF-8
}
