### this file is sourced not run
PKGVER=2.27
PKGBUILD=1
PKGARCH=i686

TARBALL=glibc-$PKGVER.tar.xz
MD5SUM=898cd5656519ffbc3a03fe811dd89e82
BUNDLE=fatdog-base-8.2.tar
SRC_URL=http://distro.ibiblio.org/fatdog/source/800/$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 &&
	
	# make dlopen works for static binaries
	patch -Np1 -i ../pkg/unsubmitted-dlopen-static-crash.diff &&
	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
	#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
	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 \
		--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 &&
	#sh &&
	rm -f /etc/ld.so.conf &&

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

	# LFS 8.1 export debug symbols
	cd /$LIBDIR &&	
	for LIB in ld-$PKGVER.so libc-$PKGVER.so libpthread-$PKGVER.so \
			libthread_db-1.0.so;
	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
} 
