### Setup

tar_archive = archive/glibc-2.6.1.tar.bz2
src_dir     = glibc-2.6.1
headers_dir = build.headers
full_dir    = build.full

env_headers += install_root=$(SYSROOT_DIR)
env_headers += libc_cv_forced_unwind=yes
env_headers += libc_cv_c_cleanup=yes
env_full    += $(env_headers)
env_full    += CFLAGS="-g -Os"


### Rules

all:
	@echo "No default rule, use headers or full"

clean:
	rm -rf $(src_dir) $(headers_dir) $(full_dir)

$(src_dir): $(tar_archive)
	tar xjf $<
#	Headers build fix for gcc 3.2 (RedHat 9).
	patch -d $@ -p0 < gcc32.patch
#	Do not link glibc with libgcc_eh.a since it does not exist at this
#	stage. There is no harm in doing this.
	patch -d $@ -p0 < gcc_eh.patch
#	Build a non-static ldconfig.
	patch -d $@ -p0 < ldconfig.patch
#	Make it possible to accept DNS replies from a different server. Needed
#	by Telefonica.
	patch -d $@ -p0 < no-server-check.patch
#	Always read the timezone information. Needed by Telefonica.
	patch -d $@ -p0 < tzset.patch
#	Selected ST patches from stlinux23-target-glibc-2.6.1-38.src.rpm.
	patch -d $@ -p1 < glibc-2.6.1-alloca_fix.patch
	patch -d $@ -p1 < glibc-2.5-memcpy.patch
	patch -d $@ -p1 < glibc-2.5-shsyscall2.patch
	patch -d $@ -p1 < glibc-2.5-sh-atomic-fixes.patch
	patch -d $@ -p1 < glibc-2.5-mutex.patch
	patch -d $@ -p1 < glibc-2.5-clone.patch
	patch -d $@ -p1 < glibc-2.5-fegetexceptflag.patch
	patch -d $@ -p1 < glibc-2.5-sh-staticfix.patch
	patch -d $@ -p1 < glibc-2.5-sh-bsd_setjmp-fix.patch
	patch -d $@ -p1 < glibc-2.6.1-stm-ieee-fix.patch
	@touch $@

$(headers_dir): $(src_dir)
	mkdir $@
	cd $@ && $(env_headers) ../$</configure \
	  --prefix=/usr \
	  --build=$(ARCH_BUILD) \
	  --host=$(ARCH_CROSS) \
	  --disable-sanity-checks \
	  --with-headers=$(SYSROOT_DIR)/usr/include \
	  --without-cvs
	$(MAKE) $(env_headers) -C $@ install-headers
#	Create dummy stubs.h header file.
	touch $(SYSROOT_DIR)/usr/include/gnu/stubs.h
#	Copy uninstalled header file.
	cp $@/bits/stdio_lim.h $(SYSROOT_DIR)/usr/include/bits

headers: $(headers_dir)

$(full_dir): $(src_dir)
	mkdir $@
#	Link without -z defs for sh4.
	echo "no-z-defs=true" > $@/configparms
	cd $@ && $(env_full) ../$</configure \
	  --prefix=/usr \
	  --build=$(ARCH_BUILD) \
	  --host=$(ARCH_CROSS) \
	  --disable-profile \
	  --enable-kernel=2.6.17 \
	  --with-headers=$(SYSROOT_DIR)/usr/include \
	  --without-cvs \
	  --without-selinux
	$(MAKE) $(env_full) -C $@ all install

full: $(full_dir)

.PHONY: all clean headers full
