### this file is sourced not run
PKGVER=2.43
PKGBUILD=1
PKGARCH=x86_64

TARBALL=glibc-$PKGVER.tar.xz
MD5SUM=7ec2588300b299215a65aec7e6afa04f
BUNDLE=
SRC_URL=https://ftp.gnu.org/gnu/glibc/$TARBALL

MIN_KERNEL_VERSION=${MIN_KERNEL_VERSION:-3.2.0}

SLACKREQ='libxcrypt'
SLACKDESC="glibc: glibc $PKGVER (Core system library)  
glibc:  
glibc: The Glibc package contains the main C library. This library provides 
glibc: the basic routines for allocating memory, searching directories, 
glibc: opening and closing files, reading and writing files, string handling, 
glibc: pattern matching, arithmetic, and so on.  
glibc: 
glibc: This is the 64-bit version of glibc.
glibc: 
glibc: 
glibc:
"

### override download for LFS sources bundle
#pkg_download() {
#	pkg_download_bundle
#}

### prepare
pkg_prepare() {
	pkg_prepare_default &&
	cd tmp/glibc* &&
	
	# upstream patches
	#for p in ../pkg/upstream/*; do
	#	patch -Np1 -i $p || return 1
	#done &&

	# LFS 11.0 - glibc 2.34 security fix
	#sed -e '/NOTIFY_REMOVED)/s/)/ \&\& data.attr != NULL)/' \
	#	-i sysdeps/unix/sysv/linux/mq_notify.c	

	# LFS 11.3 - glibc 2.37 security fix
	#sed '/width -=/s/workend - string/number_length/' \
	#	-i stdio-common/vfprintf-process-arg.c &&

	# LFS 13.0
	patch -Np1 -i ../pkg/glibc-2.43-upstream_fixes-1.patch &&
		
	# make dlopen works for static binaries
	patch -Np1 -i ../pkg/unsubmitted-dlopen-static-crash-2.34.patch &&
	true
}

### default pkg_package
### build
pkg_build() {
	cd /tmp/glibc*
	echo applying patches... &&
		
	# apply patches (LFS-7.1 for glibc-2.14.1, also applicable for LFS 7.4)
	#DL=/lib/ld-linux-armhf.so.3
	#sed -i "s|libs -o|libs -L/usr/lib -Wl,-dynamic-linker=$DL -o|" \
	#	scripts/test-installation.pl &&

	# CLFS 3.0.0 adjust linker patch - disabled by LFS 8.1 below (the compat symlink)
	#LINKER=$(readelf -l /tools/bin/bash | sed -n 's@.*interpret.*/tools\(.*\)]$@\1@p') &&
	#sed -i "s|libs -o|libs -L/usr/lib64 -Wl,-dynamic-linker=${LINKER} -o|" \
	#  scripts/test-installation.pl &&
	#unset LINKER &&
	#sed -i 's/\\$$(pwd)/`pwd`/' timezone/Makefile

	# LFS 8.1 - disable test altogether
	sed '/test-installation/s@$(PERL)@echo not running@' -i Makefile &&
	# LFS 8.1 - compat symlink to avoid dependency on /tools
	ln -sfv /tools/lib64/gcc /usr/lib64 &&
	# LFS 8.1 - remove a possibly leftover file
	rm -f /usr/include/limits.h && 
	
	# build dir
	mkdir -v ../glibc-build
	cd ../glibc-build
	
	# extra CFLAGS for default native environment
	#echo 'CFLAGS += -mfloat-abi=hard -mfpu=vfpv3-d16 -march=armv7-a -mcpu=cortex-a8 -mtune=cortex-a8 -pipe -O3' > /tmp/glibc-build/configparms
	
	# use /usr/lib64
	#echo "slibdir=/lib64" >> configparms
	echo "libc_cv_slibdir=/lib64" >> config.cache

	# Starting from 2.27, anything less than -O2 will cause problems for 32-bit
	# We also put -O2 here just in case
	CFLAGS="-O2" CC="gcc ${BUILD64}" CXX="g++ ${BUILD64}" \
		../glibc-[0-9]*/configure --prefix=/usr \
		--disable-werror \
		--enable-kernel=$MIN_KERNEL_VERSION \
		--libexecdir=/usr/lib64/glibc --libdir=/usr/lib64 \
		--enable-stack-protector=strong --enable-crypt \
		--cache-file=config.cache &&
		#--enable-obsolete-rpc \ # removed and depcreated somewhere after 2.27
		#--enable-obsolete-nsl && # new for glibc 2.26
		
	#sh &&
	make $MAKEFLAGS &&
	#sh &&
	touch /etc/ld.so.conf &&
	make install &&
	#rm -v /usr/include/rpcsvc/*.x && # we don't have this anymore
	#sh &&
	cp -v ../glibc-$PKGVER/nscd/nscd.conf /etc/nscd.conf &&
	mkdir -pv /var/db/nscd && # LFS does /var/cache/nscd after glibc-fhs patch, but we don't use patch
		
	mkdir -pv /usr/lib64/locale &&
	localedef -i en_US -f ISO-8859-1 --no-archive en_US &&
	localedef -i en_US -f UTF-8 --no-archive en_US.UTF-8 &&
	localedef -i en_AU -f ISO-8859-1 --no-archive en_AU &&
	localedef -i en_AU -f UTF-8 --no-archive en_AU.UTF-8 &&
	cp -v ../glibc-$PKGVER/localedata/SUPPORTED /usr/share/i18n &&

	# generate nsswitch.conf
	cat << "EOF" > /etc/nsswitch.conf &&
# Begin /etc/nsswitch.conf

passwd: files
group: files
shadow: files

hosts: files dns
networks: files

protocols: files
services: files
ethers: files
rpc: files
# End /etc/nsswitch.conf
EOF

	# now generate a proper ld.so.conf
	cat << "EOF" > /etc/ld.so.conf &&
# Begin /etc/ld.so.conf
/usr/local/lib
/usr/local/lib64
/opt/lib
/opt/lib64

# Add an include directory
include /etc/ld.so.conf.d/*.conf

EOF
	mkdir -pv /etc/ld.so.conf.d &&

	# LFS 8.1 - remove the temporary symlink we created earlier
	rm -f /usr/lib64/gcc &&

	cd /$LIBDIR &&
	# LFS 8.1 export debug symbols	
	#for LIB in ld-$PKGVER.so libc-$PKGVER.so libpthread-$PKGVER.so \
	#		libthread_db-1.0.so;
	# LFS 11.0 export debug symbols
	for LIB in ld-linux-x86-64.so.2 libc.so.6 libthread_db.so.1 libpthread.so.0
	do
		objcopy --only-keep-debug $LIB $LIB.dbg &&
		strip --strip-unneeded $LIB &&
		objcopy --add-gnu-debuglink=$LIB.dbg $LIB || return 1
	done &&

	pkg_build_slackdesc
} 
