include ../config

NAME = glibc-2.2.4

PWD := $(shell pwd)

LSRCDIR= $(SRCDIR)/$(NAME)
LBUILDDIR = $(BUILDDIR)/$(NAME)
LUNPACKDIR = $(UNPACKDIR)/glibc-2.2.4-19.4.src.rpm-unpacked
LTMPDIR = $(BUILDDIR)/$(NAME)-inst


#AR=ar
#CC=gcc
#LD=ld
#NM=nm
#RANLIB=ranlib

#export AR CC LD NM RANLIB

all: install

dirs: stamp-dirs
stamp-dirs: 
	-mkdir $(LSRCDIR) $(LBUILDDIR) $(LTMPDIR)
	touch stamp-dirs

unpack: stamp-unpack
stamp-unpack: stamp-dirs
	cd $(UNPACKDIR) && tar xfz $(RPMSRCDIR)/glibc-2.2.4-19.4.src.rpm-unpacked.tar.gz
	touch stamp-unpack

untar: stamp-untar
stamp-untar: stamp-unpack
	cd $(SRCDIR) && tar xfI $(LUNPACKDIR)/glibc-2.2.4.tar.bz2
	touch stamp-untar

patch: stamp-patch
stamp-patch: stamp-untar
	patchdir=$(LUNPACKDIR) glibcroot=$(LSRCDIR) sh ./patch-glibc
	cd $(SRCDIR) && patch -p0 <$(PWD)/glibc-softfp.patch
	touch stamp-patch

# leaving out --enable-threads=posix for now
configure: stamp-configure
stamp-configure: stamp-patch
	cd $(LBUILDDIR) && PATH=$(INSTALLDIR)/bin:$$PATH \
		BUILD_CC=gcc \
		CC=mipsel-linux-gcc \
		$(LSRCDIR)/configure --enable-add-ons=yes --without-cvs --enable-kernel=2.4.0 --with-headers=$(KERNEL_HEADERS) --prefix=/usr --build=$(BUILD_ALIAS) --host=$(HOST_ALIAS)
	touch stamp-configure

# This bombs out configuring libiberty but who cares
build: stamp-build
stamp-build: stamp-configure
	cd $(LBUILDDIR) && PATH=$(INSTALLDIR)/bin:$$PATH \
		make -k
	touch stamp-build

upstream-install: stamp-upstream-install
stamp-upstream-install: stamp-build
	cd $(LBUILDDIR) && PATH=$(INSTALLDIR)/bin:$$PATH \
		make install install_root=$(LTMPDIR)
	cd $(LTMPDIR) && mipsel-linux-strip -g sbin/* 
	cd $(LTMPDIR)/usr/bin && mipsel-linux-strip -g gencat getconf getent iconv locale localedef pcprofiledump rpcgen sprof
	touch stamp-upstream-install

fix-temp: stamp-fix-temp
stamp-fix-temp: stamp-upstream-install
	cd $(LTMPDIR)/usr/lib && \
		find . -type l -printf "rm %p; ln -s \`basename %l\` %p\n" | grep " ../../lib" | sh
	touch stamp-fix-temp

install: stamp-install
stamp-install: stamp-fix-temp
	cd $(LTMPDIR)/usr/lib && \
		find . -print | cpio -pumd $(INSTALLDIR)/mipsel-linux/lib
	cd $(LTMPDIR)/lib && \
		find . -print | cpio -pumd $(INSTALLDIR)/mipsel-linux/lib
	cd $(LTMPDIR)/usr/include && \
		find . -print | cpio -pumd $(INSTALLDIR)/mipsel-linux/include
	cd $(LBUILDDIR) && cp libc_pic.a $(INSTALLDIR)/mipsel-linux/lib
	-mkdir $(INSTALLDIR)/lib/libc_pic
	cd $(LBUILDDIR)/elf && \
		cp sofini.os soinit.os $(INSTALLDIR)/lib/libc_pic/
	sed 's@/usr/lib/@@;s@/lib/@@' <$(LTMPDIR)/usr/lib/libc.so >$(INSTALLDIR)/mipsel-linux/lib/libc.so
	touch stamp-install
