include ../config

#	wget $(DEBURL)/g/gcc-2.95/gcc-2.95_2.95.4.ds8.orig.tar.gz
#	wget $(DEBURL)/g/gcc-2.95/gcc-2.95_2.95.4.ds8-1.dsc
#	wget $(DEBURL)/g/gcc-2.95/gcc-2.95_2.95.4.ds8-1.diff.gz

NAME = gcc-20011006

PWD := $(shell pwd)

LSRCDIR= $(SRCDIR)/$(NAME)
LBUILDDIR = $(BUILDDIR)/$(NAME)
LUNPACKDIR = $(UNPACKDIR)/gcc-2.95-2.95.4.ds8

all: install

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

unpack: stamp-unpack
stamp-unpack: stamp-dirs
	cd $(UNPACKDIR) && tar xfz $(DEBSRCDIR)/gcc-2.95_2.95.4.ds8.orig.tar.gz
	mv $(UNPACKDIR)/gcc-2.95-2.95.4.ds8.orig $(UNPACKDIR)/gcc-2.95-2.95.4.ds8
	cd $(UNPACKDIR) && zcat $(DEBSRCDIR)/gcc-2.95_2.95.4.ds8-1.diff.gz | patch -p0
	touch stamp-unpack

untar: stamp-untar
stamp-untar: stamp-unpack
	cd $(SRCDIR) && bzcat $(LUNPACKDIR)/gcc-20011006.tar.bz2 | tar xf -
	touch stamp-untar

#	#cd $(LSRCDIR) && tar xfz $(LUNPACKDIR)/libg++-2.8.1.3.tar.gz

patch_dir = $(LUNPACKDIR)/debian/patches

# Needs the whole Debian changelog
#debian_patches = gcc-version
debian_patches = 

debian_patches += cross-libc-version

debian_patches += bootstrap cpp-macro-doc \
	gcc-default-arch \
	libstdc++-out-of-mem libstdc++-wstring \
	libstdc++-bastring \
	gcc-manpage cpp-dos-newlines libio-doc gcc-doc \
	strength-red

debian_patches += gcc-gas-hidden

# not an alpha
debian_patches += ppc-fsirl-updates

debian_patches += gcc-mips-linux gcc-mips-linux-abi gcc-mips-linux-ctors \
	gcc-mips-mad gcc-mips-reg_zero

list-patches:
	@echo $(debian_patches)

debian-patch: stamp-debian-patch
stamp-debian-patch: stamp-untar
	set -e; \
	for f in $(debian_patches); do \
	  sh -e $(patch_dir)/$$f.dpatch -patch -d $(LSRCDIR); \
	done
	touch stamp-debian-patch

custom_patches = gcc-crossinhibit gcc-named-bss-sections2 gcc-gval-fix gcc-softfloat \
  gcc-snow

custom-patch: stamp-custom-patch
stamp-custom-patch: stamp-debian-patch
	set -e; \
	for f in $(custom_patches); do \
	  echo "custom patch $$f:"; \
	  patch -d $(LSRCDIR) -p1 <$(PWD)/$$f.patch; \
	done
	touch stamp-custom-patch

patch: stamp-patch
stamp-patch: stamp-debian-patch stamp-custom-patch
	touch stamp-patch

# leaving out --enable-threads=posix for now
configure: stamp-configure
stamp-configure: stamp-patch
	cd $(LBUILDDIR) && PATH=$(INSTALLDIR)/bin:$$PATH \
		$(LSRCDIR)/configure --enable-shared --disable-checking --target=$(HOST_ALIAS) --build=$(BUILD_ALIAS) --host=$(BUILD_ALIAS) --prefix=$(INSTALLDIR) --enable-languages=c 
	touch stamp-configure

fake-includes: stamp-fake-includes
stamp-fake-includes: stamp-configure
	cd $(INSTALLDIR)/mipsel-linux && tar xfv $(PWD)/fake-includes.tar
	touch stamp-fake-includes

build: stamp-build
stamp-build: stamp-configure stamp-fake-includes
	cd $(LBUILDDIR) && PATH=$(INSTALLDIR)/bin:$$PATH \
		make all-gcc
	touch stamp-build

remove-fake-includes: stamp-remove-fake-includes
stamp-remove-fake-includes: stamp-build
	cd $(INSTALLDIR)/mipsel-linux && \
	  tar tf $(PWD)/fake-includes.tar | fgrep .h | xargs rm
	touch stamp-remove-fake-includes

install: stamp-install
stamp-install: stamp-remove-fake-includes
	cd $(LBUILDDIR) && PATH=$(INSTALLDIR)/bin:$$PATH \
		make install-gcc
	cd $(INSTALLDIR)/bin && mv gcov mipsel-linux-gcov
	cd $(INSTALLDIR)/bin && mv cpp mipsel-linux-cpp
	cd $(INSTALLDIR)/bin && strip -g mipsel-linux-gcov mipsel-linux-cpp mipsel-linux-gcc mipsel-linux-protoize mipsel-linux-unprotoize
	cd $(INSTALLDIR)/lib/gcc-lib && strip */2.95.4/cc1 */2.95.4/collect2 */2.95.4/cpp0
	touch stamp-install
