TARGET = sh-elf
BUILD_DIR = BUILD_$(TARGET)

BINUTILS = binutils-2.14
GCC = gcc-3.3.2
NEWLIB = newlib-1.11.0

all : binutils gcc newlib

$(BINUTILS).tar.bz2 :
	wget http://ftp.gnu.org/gnu/binutils/$(BINUTILS).tar.bz2

$(BINUTILS) : $(BINUTILS).tar.bz2
	tar jvxf $(BINUTILS).tar.bz2

$(GCC).tar.gz :
	wget ftp://ftp.ring.gr.jp/pub/GNU/gcc/$(GCC).tar.gz

$(GCC) : $(GCC).tar.gz
	tar zvxf $(GCC).tar.gz

$(NEWLIB).tar.gz :
	wget ftp://sources.redhat.com/pub/newlib/$(NEWLIB).tar.gz

$(NEWLIB) : $(NEWLIB).tar.gz
	tar zvxf $(NEWLIB).tar.gz

binutils : $(BINUTILS)
	cd $(BINUTILS); \
	if ! [ -d $(BUILD_DIR) ]; then mkdir $(BUILD_DIR); fi; \
	cd $(BUILD_DIR); \
	if ! [ -f Makefile ]; then ../configure --target=$(TARGET); fi; \
	make; \
	make install

gcc : $(GCC) $(NEWLIB)
	cd $(GCC); \
	if ! [ -d $(BUILD_DIR) ]; then mkdir $(BUILD_DIR); fi; \
	cd $(BUILD_DIR); \
	if ! [ -f Makefile ]; then ../configure --target=$(TARGET) --enable-languages="c++" --with-newlib --with-headers=../../$(NEWLIB)/newlib/libc/include; fi; \
	make LANGUAGES="c c++"; \
	make install

newlib : $(NEWLIB)
	cd $(NEWLIB); \
	if ! [ -d $(BUILD_DIR) ]; then mkdir $(BUILD_DIR); fi; \
	cd $(BUILD_DIR); \
	if ! [ -f Makefile ]; then ../configure --target=$(TARGET); fi; \
	make; \
	make install