### this file is sourced not run
PKGVER=2.37
PKGBUILD=2
PKGARCH=i686

TARBALL=glibc-$PKGVER.tar.xz
MD5SUM=e89cf3dcb64939d29f04b4ceead5cc4e
BUNDLE=fatdog-base-11.3.tar
SRC_URL=http://distro.ibiblio.org/fatdog/source/900/$BUNDLE

MIN_KERNEL_VERSION=${MIN_KERNEL_VERSION:-3.2.0}

SLACKREQ=
SLACKDESC="glibc32: glibc32 $PKGVER (Core system library)  
glibc32:  
glibc32: The Glibc package contains the main C library. This library provides 
glibc32: the basic routines for allocating memory, searching directories, 
glibc32: opening and closing files, reading and writing files, string handling, 
glibc32: pattern matching, arithmetic, and so on.  
glibc32: 
glibc32: This is the 32-bit version of glibc. You only need this if you plan to
glibc32: run or build 32-bit programs.
glibc32: 
glibc32:
"

### override download for LFS sources
# $1-src-dir $2-tarball 
pkg_download() {
	echo downloading $BUNDLE for $TARBALL
	wget -P $1 $SRC_URL
	tar -xf $1/$BUNDLE --strip-components=1 -C $1 
	rm $1/$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 &&

	# make dlopen works for static binaries
	patch -Np1 -i ../pkg/unsubmitted-dlopen-static-crash-2.34.patch &&
	true
}

### package - only collect the libraries
pkg_package() {
	rm -rf sbin usr/bin usr/sbin usr/share/i18n usr/share/locale usr/share/info
}

### 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/lib -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 && # this must be lib64 because we have 64-bit gcc
	# 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

	# 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 ${BUILD32}" CXX="g++ ${BUILD32}" \
		../glibc-[0-9]*/configure --prefix=/usr \
		--disable-werror \
		--enable-kernel=$MIN_KERNEL_VERSION \
		--libexecdir=/usr/lib/glibc --host=${CLFS_TARGET32} \
		--enable-stack-protector=strong &&
		#--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 && # temporary blank ld.so
	make install &&
	#rm -v /usr/include/rpcsvc/*.x && # we don't have this anymore
	#sh &&
	rm -f /etc/ld.so.conf &&

	# 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.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
} 
