DISTFILES=~/Work-ARM/Files.toolchain
TMP=~/tmp

ARCH=arm
TARGET=arm-linux
PREFIX=$(HOME)/$(TARGET)
CROSS_COMPILE=$(TARGET)-
PATH=$(PREFIX)/bin:$(shell echo $$PATH)

MAKE=make -j1

BINV=binutils-2.18
GCCV=gcc-4.2.2
UCLIBCV=uClibc-0.9.29

# Этот elf2flt я достал с сайта http://sourceforge.net/projects/devkitpro
E2FV=elf2flt-20060506


.PHONY: binutils gcc-stage1 linux-headers gcc-stage2 uclibc uclibc-config

all: $(BINV).success $(E2FV).success linux-headers.success \
	$(UCLIBCV).success $(GCCV).stage1.success $(GCCV).stage2.success

# binutils stuff
$(BINV).success binutils :
	echo Building binutils...
	rm -rf $(BINV)*
	tar -xjf $(DISTFILES)/$(BINV).tar.bz2
	mkdir $(BINV).BUILD
	cd $(BINV).BUILD; ../$(BINV)/configure \
		--prefix=$(PREFIX) --with-sysroot=${PREFIX} \
		--target=$(TARGET) --disable-nls \
		2>&1 | tee ../$(BINV).configure.log
	$(MAKE) -C $(BINV).BUILD \
		2>&1 | tee $(BINV).build.log
	$(MAKE) -C $(BINV).BUILD install \
		2>&1 | tee $(BINV).install.log
	touch $(BINV).success
	echo Build binutils done.

# linuxheaders
linux-headers.success linux-headers :
	echo Building linux headers...
	cp ../linux-config ../linux/.config

	# ядро свежее 2.6.18 умеет делать так... но не наше
	#make ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) headers_install INSTALL_HDR_PATH=$(PREFIX)/include
	# Наше ядро умеет только ставить симлинки, поэтому
	$(MAKE) ARCH=arm CROSS_COMPILE="$(TARGET)-" -C ../linux prepare

	mkdir -p $(PREFIX)/usr/include
	cp -a ../linux/include/linux $(PREFIX)/usr/include/linux
	cp -a ../linux/include/asm-arm $(PREFIX)/usr/include/asm
	cp -a ../linux/include/asm-generic $(PREFIX)/usr/include/asm-generic
	touch linux-headers.success
	echo Build linux headers done.

# gcc stuff
$(GCCV).stage1.success gcc-stage1 : $(BINV).success
	echo Building gcc stage1...
	rm -rf $(GCCV)*
	tar -xjf $(DISTFILES)/$(GCCV).tar.bz2
	# Gentoo uclibc patch for gcc
	cd $(GCCV); patch -p1 < $(DISTFILES)/gcc-arm-softfloat.patch
	mkdir $(GCCV).stage1.BUILD
	cd $(GCCV).stage1.BUILD; ../$(GCCV)/configure \
		--prefix=$(PREFIX) --target=$(TARGET) \
		--with-sysroot=${PREFIX} --with-float=soft \
		--with-arch=armv4t --with-tune=arm7tdmi \
		--with-newlib --enable-languages=c --enable-threads=no \
		--disable-shared --disable-nls --disable-multilib \
		--disable-tls --disable-mudflap \
		2>&1 | tee ../$(GCCV).stage1.configure.log
	$(MAKE) -C $(GCCV).stage1.BUILD \
		2>&1 | tee $(GCCV).stage1.build.log
	$(MAKE) -C $(GCCV).stage1.BUILD install \
		2>&1 | tee $(GCCV).stage1.install.log
	touch $(GCCV).stage1.success
	echo Build gcc stage1 done.

$(UCLIBCV).success uclibc : $(GCCV).stage1.success linux-headers.success
	echo Building uclibc...
	rm -rf $(UCLIBCV)*
	tar -xjf $(DISTFILES)/$(UCLIBCV).tar.bz2
	cd $(UCLIBCV); patch -p1 < $(DISTFILES)/uClibc-msync-redeclaration.patch
	cp uclibc-config $(UCLIBCV)/.config
	$(MAKE) -C $(UCLIBCV) CROSS=$(CROSS_COMPILE) \
		KERNEL_HEADERS=$(PREFIX)/usr/include \
		2>&1 | tee $(UCLIBCV).build.log
	$(MAKE) -C $(UCLIBCV) CROSS=$(CROSS_COMPILE) install \
		PREFIX=$(PREFIX)/ RUNTIME_PREFIX= DEVEL_PREFIX=usr/ \
		KERNEL_HEADERS=$(PREFIX)/usr/include \
		2>&1 | tee $(UCLIBCV).install.log
	touch $(UCLIBCV).success
	echo Build uClibc done.

$(GCCV).stage2.success gcc-stage2 : $(UCLIBCV).success
	echo Building gcc stage2...
	rm -rf $(GCCV).stage2.*
	mkdir $(GCCV).stage2.BUILD
	cd $(GCCV).stage2.BUILD; ../$(GCCV)/configure \
		--prefix=$(PREFIX) --target=$(TARGET) \
		--with-sysroot=${PREFIX} --with-float=soft \
		--with-arch=armv4t --with-tune=arm7tdmi \
		--enable-languages=c --enable-threads=posix \
		--disable-shared --disable-nls --disable-multilib \
		--disable-mudflap \
		2>&1 | tee ../$(GCCV).stage2.configure.log

	$(MAKE) -C $(GCCV).stage2.BUILD \
		2>&1 | tee $(GCCV).stage2.build.log
	$(MAKE) -C $(GCCV).stage2.BUILD install \
		2>&1 | tee $(GCCV).stage2.install.log
	touch $(GCCV).stage2.success
	echo Build gcc stage2 done.

# elf2flt stuff
$(E2FV).success elf2flt : $(BINV).success $(GCCV).stage2.success
	echo Building elf2flt...
	rm -rf $(E2FV)*
	#tar -xjf $(DISTFILES)/$(E2FV).tar.bz2

	# Архив не хочется перепаковывать
	tar -xjf $(DISTFILES)/elf2flt-src-20060506.tar.bz2
	mv elf2flt $(E2FV)
	cd $(E2FV); patch -p1 < $(DISTFILES)/elf2flt-nogot.patch

	mkdir $(E2FV).BUILD
	cd $(E2FV).BUILD; ../$(E2FV)/configure \
		--prefix=$(PREFIX) --target=$(TARGET) \
		--with-binutils-include-dir=../$(BINV).BUILD/include/ \
		--with-bfd-include-dir=../$(BINV).BUILD/bfd/ \
		--with-libbfd=../$(BINV).BUILD/bfd/libbfd.a \
		--disable-got-check \
		2>&1 | tee ../$(E2FV).configure.log
	$(MAKE) -C $(E2FV).BUILD\
		2>&1 | tee $(E2FV).build.log
	$(MAKE) -C $(E2FV).BUILD install\
		2>&1 | tee $(E2FV).install.log

	echo Build elf2flt done.

# configuration stuff
uclibc-config:
	tar -xjf $(DISTFILES)/$(UCLIBCV).tar.bz2
	cd $(UCLIBCV); patch -p1 < $(DISTFILES)/uClibc-msync-redeclaration.patch
	cp uclibc-config $(UCLIBCV)/.config
	$(MAKE) -C $(UCLIBCV) CROSS="$(TARGET)-" menuconfig
	cp $(UCLIBCV)/.config uclibc-config

# cleanup stuff
uninstall:
	rm -rf $(PREFIX)

clean:
	rm -rf $(BINV)*
	rm -rf $(GCCV)*
	rm -rf $(UCLIBCV)*
	rm -rf $(E2FV)*
	rm -rf linux-headers.success