# Template file for 'glibc'
pkgname=glibc
version=2.39
revision=4
bootstrap=yes
short_desc="GNU C library"
maintainer="Enno Boland <gottox@voidlinux.org>"
license="GPL-2.0-or-later, LGPL-2.1-or-later, BSD-3-Clause"
homepage="http://www.gnu.org/software/libc"
distfiles="${GNU_SITE}/libc/glibc-${version}.tar.xz"
checksum=f77bd47cf8170c57365ae7bf86696c118adb3b120d3259c64c502d3dc1e2d926
# Do not strip these files, objcopy errors out.
nostrip_files="
	XBS5_ILP32_OFFBIG
	XBS5_ILP32_OFF32
	POSIX_V6_ILP32_OFF32
	POSIX_V6_ILP32_OFFBIG
	POSIX_V7_ILP32_OFF32
	POSIX_V7_ILP32_OFFBIG
	POSIX_V6_LP64_OFF64
	POSIX_V7_LP64_OFF64
	XBS5_LP64_OFF64
	ld-linux-x86-64.so.2
	ld-linux.so.2
	ld-linux.so.3
	ld-linux-aarch64.so.1
	ld64.so.2
	ld.so.1
	ld-linux-armhf.so.3
	libresolv.so.2
	libm.so.6
	libthread_db.so.1
	libnsl.so.1
	libc.so.6
	libc_malloc_debug.so.0
	libmemusage.so
	libmvec.so.1
	libBrokenLocale.so.1
	libnss_compat.so.2
	libnss_db.so.2
	libnss_dns.so.2
	libnss_files.so.2
	libnss_hesiod.so.2"

conf_files="
	/etc/rpc
	/etc/gai.conf
	/etc/ld.so.conf"
if [ "$CHROOT_READY" ]; then
	hostmakedepends="bison gettext perl python3 texinfo"
	# XXX: remove this on the next update
	# ensures partial upgrades don't break things like PAM
	depends="libxcrypt-compat"
fi
makedepends="kernel-libc-headers"
lib32files="/usr/lib/gconv/gconv-modules /usr/lib/gconv/gconv-modules.d"
lib32symlinks="ld-linux.so.2"
archs="~*-musl"
if [ "$XBPS_TARGET_LIBC" = musl ]; then
	broken="no point in building this for musl"
fi

do_configure() {
	mkdir build
	cd build

	local _libdir=/usr/lib${XBPS_TARGET_WORDSIZE}

	echo "slibdir=${_libdir}" > configparms
	echo "rtlddir=${_libdir}" >> configparms
	echo "bindir=/usr/bin" >> configparms
	echo "sbindir=/usr/bin" >> configparms
	echo "rootsbindir=/usr/bin" >> configparms

	case "$XBPS_TARGET_MACHINE" in
		# Build with -mno-tls-direct-seg-refs to avoid performance
		# problems with Xen on x86 32bit.
		i686) export CFLAGS+=" -mno-tls-direct-seg-refs";;
		# Force soft float ABI on armv5.
		armv5*) configure_args+=" --without-fp";;
	esac

	if [ "$CROSS_BUILD" ]; then
		configure_args+=" --host=${XBPS_CROSS_TRIPLET}"
		configure_args+=" --with-sysroot=${XBPS_CROSS_BASE}"
		configure_args+=" --with-headers=${XBPS_CROSS_BASE}/usr/include"
	else
		configure_args+=" --with-headers=${XBPS_MASTERDIR}/usr/include"
	fi

	# Disable SSP from CFLAGS (enabled below)
	export CFLAGS="${CFLAGS/-fstack-protector-strong/}"
	# Disable FORTIFY_SOURCE
	export CFLAGS="${CFLAGS/-D_FORTIFY_SOURCE=?/}"

	SHELL=/bin/bash ../configure ${configure_args} \
		--bindir=/usr/bin --sbindir=/usr/bin \
		--libdir=${_libdir} --libexecdir=${_libdir} \
		--enable-multi-arch --enable-bind-now \
		--enable-stack-protector=strong \
		--disable-profile --enable-kernel=3.2.0 \
		--without-selinux --without-gd \
		--disable-werror --enable-option-checking
}

post_configure() {
	# Less verbose output when compiling and assembling
	vsed -i Makerules \
		-e 's|compile-command.c = \(.*\)|compile-command.c = @echo " CC     $<"; \1|' \
		-e 's|compile-command.cc = \(.*\)|compile-command.cc = @echo " CXX    $<"; \1|' \
		-e 's|compile-command.S = \(.*\)|compile-command.S = @echo " ASM    $<"; \1|'
}

do_build() {
	cd build
	# Enable both DT_GNU_HASH and DT_HASH for ld.so and DSOs to improve
	# compatibility with applications that expect DT_HASH.
	env LDFLAGS.so="-Wl,--hash-style=both" LDFLAGS-rtld="-Wl,--hash-style=both" \
		make ${makejobs}
}

do_install() {
	vlicense LICENSES
	# Create DESTDIR/etc/ld.so.conf
	mkdir -p ${DESTDIR}/etc
	echo "include /etc/ld.so.conf.d/*.conf" > ${DESTDIR}/etc/ld.so.conf

	( cd build && make install_root=${DESTDIR} install )

	# The C.UTF-8 locale should always be available
	#
	# Use the recently compiled localedef when compiling to/from the same
	# architecture, and the host localedef binary otherwise.
	if [ "${XBPS_MACHINE%-*}" = "${XBPS_TARGET_MACHINE%-*}" ]; then
		make -C localedata DESTDIR="${DESTDIR}" \
			objdir="../build" install-files-C.UTF-8/UTF-8
	elif command -v localedef >/dev/null 2>&1; then
		local endian=
		case "$XBPS_TARGET_ENDIAN" in
			be) endian="--big-endian" ;;
			le) endian="--little-endian" ;;
		esac

		vmkdir usr/lib/locale
		I18NPATH=./localedata localedef --prefix="${DESTDIR}" ${endian} \
			-i C -f UTF-8 --no-archive "${DESTDIR}/usr/lib/locale/C.utf8"
	else
		msg_warn "localedef not available; skipping generation of C.UTF-8 locale\n"
	fi

	# create /etc/default/libc-locales
	vinstall ${FILESDIR}/libc-locales 644 etc/default
	sed -e "s|/| |g" \
		-e 's|\\| |g' \
		-e "s|SUPPORTED-LOCALES=||" \
		-e "s|^|#|g" localedata/SUPPORTED \
			>> "${DESTDIR}/etc/default/libc-locales"

	# ldd is a bash script, so make it run as such.
	replace_interpreter bash ${DESTDIR}/usr/bin/ldd

	# wrong 32bit dynamic linker when using elfv2 ABI
	case "$XBPS_TARGET_MACHINE" in
		ppc64*)
			vsed -i '/RTLDLIST=/s/ld.so.2/ld.so.1/g' \
				${DESTDIR}/usr/bin/ldd
			;;
	esac

	install -d ${DESTDIR}/etc/ld.so.conf.d
	echo "/usr/local/lib" > ${DESTDIR}/etc/ld.so.conf.d/usr_local.conf
	chmod 644 ${DESTDIR}/etc/ld.so.conf.d/usr_local.conf
	rm -f ${DESTDIR}/etc/ld.so.cache

	# Create ld-linux.so.3 symlink for ARM.
	case "$XBPS_TARGET_MACHINE" in
		armv?l) ln -sfr ${DESTDIR}/usr/lib/ld-linux-armhf.so.3 ${DESTDIR}/usr/lib/ld-linux.so.3;;
	esac

	vinstall posix/gai.conf 644 etc
	rm -rf ${DESTDIR}/var/db
	# Remove tzselect and z{dump,ic}, provided by tzutils.
	rm -f ${DESTDIR}/usr/bin/tzselect
	rm -f ${DESTDIR}/usr/bin/z{dump,ic}
}

glibc-devel_package() {
	lib32depends="glibc>=${version}_${revision}"
	lib32files="/usr/include/gnu/stubs-32.h"
	depends="${makedepends} glibc>=${version}_${revision}"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove "usr/lib/*.a"
		vmove "usr/lib/*.o"
		if [ -d "${DESTDIR}/usr/share/info" ]; then
			vmove usr/share/info
		fi
	}
}

glibc-locales_package() {
	conf_files="/etc/default/libc-locales"
	short_desc+=" - locale data files"
	pkg_install() {
		vmove etc/default
		vmove usr/share/i18n
		vmove usr/share/locale
	}
}

nscd_package() {
	conf_files="/etc/nscd.conf"
	short_desc+=" - Name Service Cache Daemon"
	pkg_install() {
		vmove usr/bin/nscd
		vinstall ${wrksrc}/nscd/nscd.conf 644 etc
		vsv nscd
	}
}
