RELEASE_VERSION=0.0.4

LCDIRS?=$(foreach d,$(shell find config/linux/* -maxdepth 1 -type d | grep -v svn),$(shell basename $d))
LSDIRS?=$(foreach d,$(shell find patches/linux/* -maxdepth 1 -type d | grep -v svn),$(shell basename $d))

help: all
all: 
	@echo
	@echo -e "\t" "Build all:"
	@echo -e "\t" "Type \"make all-build\" (this will take very long (> 8h) and uses much diskspace. Note that /opt has to writable.)"
	@echo
	@echo -e "\t" "Avalaible Buildroot configuratons:"
	@echo -e "\t" $(BUILDTOOTDIRS)
	@echo -e "\t" "Type \"make buildroots\" to build all configurations"
	@echo -e "\t" "Type \"make buildroots BUILDTOOTDIRS=<config>\" to build one configuration"
	@echo
	@echo -e "\t" "Avalaible Crosstool configuratons:"
	@echo -e "\t" $(CTDIRS)
	@echo -e "\t" "Type \"make cts\" to build all configurations"
	@echo -e "\t" "Type \"make cts CTDIRS=<config>\" to build one configuration"
	@echo
	@echo -e "\t" "Avalaible LINUX configuratons <config>:"
	@echo -e "\t" $(LCDIRS)
	@echo -e "\t" "Avalaible LINUX source trees <src>:"
	@echo -e "\t" $(LSDIRS)
	@echo -e "\t" "Type \"make linux-src SRC=<src> CONFIG=<config>\" to build one configuration"
	@echo -e "\t" "Example: make linux-src SRC=2.6.36-rc3 CONFIG=linux_base"
	@echo -e "\t" "Use TARGET=<dir> to use a initromfs dir other than the default target/sfleon_simple"
	@echo -e "\t" "Note: a valid sparc-linux toolchain has to be in your PATH. Use make cts CTDIRS=<config>"
	@echo -e "\t" "to build one."
	@echo

all-build:
	echo > built_image.txt
	make all-linux | tee build_all.log

all-linux: cts buildroots
	# multi-lib glibc
	make linux-src SRC=2.6.36-rc3 CONFIG=linux_base TARGET=build-br/buildroot/hfleon_multilib_glibc_basic/output/target   TARGET_COPY_INITTAB=y
	make linux-src SRC=2.6.36-rc3 CONFIG=linux_base TARGET=build-br/buildroot/hfleonv8_multilib_glibc_basic/output/target TARGET_COPY_INITTAB=y
	make linux-src SRC=2.6.36-rc3 CONFIG=linux_base TARGET=build-br/buildroot/sfleon_multilib_glibc_basic/output/target   TARGET_COPY_INITTAB=y
	make linux-src SRC=2.6.36-rc3 CONFIG=linux_base TARGET=build-br/buildroot/sfleonv8_multilib_glibc_basic/output/target TARGET_COPY_INITTAB=y
	# single-lib glibc
	make linux-src SRC=2.6.36-rc3 CONFIG=linux_base TARGET=build-br/buildroot/hfleonv8_glibc_basic/output/target          TARGET_COPY_INITTAB=y
	make linux-src SRC=2.6.36-rc3 CONFIG=linux_base TARGET=build-br/buildroot/sfleon_glibc_basic/output/target            TARGET_COPY_INITTAB=y
	# uclibc
	make linux-src SRC=2.6.36-rc3 CONFIG=linux_base TARGET=build-br/buildroot/hfleon_shared_basic/output/target           TARGET_COPY_INITTAB=y
	make linux-src SRC=2.6.36-rc3 CONFIG=linux_base TARGET=build-br/buildroot/hfleonv8_shared_basic/output/target         TARGET_COPY_INITTAB=y
	make linux-src SRC=2.6.36-rc3 CONFIG=linux_base TARGET=build-br/buildroot/sfleon_shared_basic/output/target           TARGET_COPY_INITTAB=y
	make linux-src SRC=2.6.36-rc3 CONFIG=linux_base TARGET=build-br/buildroot/sfleonv8_shared_asic/output/target          TARGET_COPY_INITTAB=y


#################### buildroot ###################
GITPREFIX=$(CURDIR)/usr
GITSRCPREFIX=$(CURDIR)/usr/build
BUILDROOTDIR?=buildroot
TAR_BUILDROOT=reference/buildroot-20101007.tar.bz2
BUILDTOOTDIRS?=$(foreach d,$(shell find config/buildroot/* -maxdepth 1 -type d | grep -v svn),$(shell basename $d))

buildroots-prepare:
	-rm -rf $(CURDIR)/build-br-git
	(export PATH=$(GITPREFIX)/bin:$$PATH; \
	if ! which git >/dev/null 2>&1; then \
		cd $(GITSRCPREFIX); wget http://www.kernel.org/pub/software/scm/git/git-1.6.4.1.tar.gz; \
			tar xvf git-1.6.4.1.tar.gz; cd git-1.6.4.1; ./configure --prefix=$(GITPREFIX); make ; make install; \
	fi;)
	git=`cat $(CURDIR)/patches/buildroot/README | grep git: | awk '{ print $$2 }';`; \
	rev=`cat $(CURDIR)/patches/buildroot/README | grep rev: | awk '{ print $$2 }';`; \
	echo "Patching $$n from git $$git:$$rev"; \
	mkdir build-br; \
	(export PATH=$(GITPREFIX)/bin:$$PATH; cd $(CURDIR)/build-br/; rm -rf build-br-git; \
			echo git clone $$git build-br-git; \
			git clone $$git build-br-git; \
			cd $(CURDIR)/build-br/build-br-git; \
			git checkout $$rev -b gaisler; ); \
	cat $(CURDIR)/patches/buildroot/0* | patch -p1 -d $(CURDIR)/build-br/build-br-git; 
	for d in $(BUILDTOOTDIRS); do \
		n=$$d; \
		if [ ! -f config/buildroot/$$n/config ]; then echo config $$n doesnt exist; exit 1; fi; \
		if [ -d $(CURDIR)/build-br/buildroot/$$n ]; then rm -rf $(CURDIR)/build-br/buildroot/$$n; fi; \
		cp -r $(CURDIR)/build-br/build-br-git $(CURDIR)/build-br/buildroot/$$n; \
		cat $(CURDIR)/config/buildroot/$$n/config | sed \
		-e 's/$$(CURDIR)/$(subst /,\/,$(CURDIR))/g' \
		> $(CURDIR)/build-br/buildroot/$$n/.config; \
	done

buildroots: buildroots-prepare
	mkdir -p $(CURDIR)/build-br/buildroot
	for d in $(BUILDTOOTDIRS); do \
		n=`basename $$d`; \
		echo try remove $(CURDIR)/build-br/buildroot/$$n/output;\
		if [ -d $(CURDIR)/build-br/buildroot/$$n/output ]; then rm -rf $(CURDIR)/build-br/buildroot/$$n/output; fi; \
	done;
	export PATH=$(GITPREFIX)/bin:$$PATH; \
	source $(CURDIR)/path_prepare.sh; \
	for d in $(BUILDTOOTDIRS); do \
		n=$$d; \
		if [ ! -f config/buildroot/$$n/config ]; then echo config $$n doesnt exist; exit 1; fi; \
		echo "Build buildroot $$n"; \
		(cd build-br/buildroot/$$n; nice make VERBOSE=2 | tee _compile.log;); \
		make TOOLCHAIN=$$n testprogs-compile gdb-copy; \
	done

show-buildroots:
	@echo $(BUILDTOOTDIRS)

#################### crosstool-ng ###################

CTPREFIX=$(CURDIR)/usr
CTSRCPREFIX=$(CURDIR)/usr/build
CTDIR?=ct
CTDIRS?=$(foreach d,$(shell find config/ct/* -maxdepth 1 -type d | grep -v svn),$(shell basename $d))
TAR_CT=reference/crosstool-ng-1.8.2.tar.bz2
.PHONY: ct
install-ct:
	-rm -rf $(CTSRCPREFIX)
	-mkdir -p $(CTSRCPREFIX)
		tar xvf $(TAR_CT) --keep-old-files -C $(CTSRCPREFIX);\
		cat patches/crosstool-ng/*.patch | patch -p1 -d $(CTSRCPREFIX);\
		(cd $(CTSRCPREFIX); ./configure --prefix=$(CTPREFIX); \
			make; make install ;);

	#(export PATH=$(CTPREFIX)/bin:$$PATH; \
	#if ! which curl >/dev/null 2>&1; then \
	#	cd $(CTSRCPREFIX); wget http://curl.haxx.se/download/curl-7.21.2.tar.gz; \
	#		tar xvf curl-7.21.2.tar.gz; cd curl-7.21.2; ./configure --prefix=$(CTPREFIX); make ; make install; \
	#fi;)


cts2:
	cat config/ct/hfleon_basic/config | grep CT_PREFIX_DIR= | awk '{ if (match($$0,"\".*\"")) { a = substr($$0,RSTART+1,RLENGTH-2); printf("%s",a);}; }'

cts: install-ct
	for d in $(CTDIRS); do \
		n=`basename $$d`; \
		echo try remove $(CURDIR)/build-ct/ct/$$n/x-tools;\
		if [ -d $(CURDIR)/build-ct/ct/$$n ]; then rm -rf $(CURDIR)/build-ct/ct/$$n/*; fi; \
	done;
	source $(CURDIR)/path_prepare.sh; \
	for d in $(CTDIRS); do \
		n=$$d; \
		if [ ! -d build-ct/ct/$$n ]; then mkdir -p build-ct/ct/$$n; fi; \
		if [ ! -f config/ct/$$n/config ]; then echo config $$n doesnt exist; exit 1; fi; \
		echo "Build crosstool $$n"; \
		cp config/ct/$$n/config build-ct/ct/$$n/.config; \
		(export PATH=$(CTPREFIX)/bin:$$PATH; cd build-ct/ct/$$n; \
		 	nice $(CTPREFIX)//bin/ct-ng build V=2; ); \
		make cts-dolink TOOLDIR=`cat config/ct/$$n/config | grep CT_PREFIX_DIR= | awk '{ if (match($$0,"\".*\"")) { a = substr($$0,RSTART+1,RLENGTH-2); printf("%s",a);}; }'`; \
	done


cts-dolink:
		if [ "x$(TOOLDIR)" == "x"]; then echo "TOOLDIR undefined"; exit 1; fi
		chmod a+w  $(TOOLDIR)/; \
		chmod a+w  $(TOOLDIR)/bin; \
		chmod a+w  $(TOOLDIR)/sparc-leon-linux-gnu; 
		-rm $(TOOLDIR)/sparc-linux
		( cd $(TOOLDIR); \
			ln -sf sparc-leon-linux-gnu sparc-linux; \
		); 
		-rm $(TOOLDIR)/sparc-leon-linux-gnu/lib
		( cd $(TOOLDIR)/sparc-leon-linux-gnu; \
			ln -sf sys-root/lib lib; \
		); 
		( cd $(TOOLDIR)/bin; \
		for p in as ar c++ cpp g++ gcc ld ldd nm objcopy objdump ranlib readelf size strings strip; do \
			ln -sf sparc-leon-linux-gnu-$${p} sparc-linux-$${p}; \
		done; \
		);\


cts-createdist: createdist-tar
	mkdir -p $(CURDIR)/release;
	for d in $(CTDIRS); do \
		n=$$d; \
		prefix=`cat config/ct/$$n/config | grep CT_PREFIX_DIR= | awk '{ if (match($$0,"\".*\"")) { a = substr($$0,RSTART+1,RLENGTH-2); printf("%s",a);}; }'`; \
		if expr "$$n" : '.*_basic' >/dev/null 2>&1; then n=`expr "$$n" : '\(.*\)_basic'`; fi; \
		n=`basename $$prefix`; \
		t="sparc-linux-ct-$$n-$(RELEASE_VERSION)"; \
		if [ ! -d $$prefix ]; then echo tolchain $$prefix doesnt exist; exit 1; fi; \
		echo "Create crosstool $$prefix dist"; \
		chmod a+w $$prefix/; \
		chmod a+w $$prefix/build.log*; \
		echo "$(RELEASE_VERSION)" > $$prefix/VERSION; \
		rm $$prefix/build.log*; \
		mkdir -p $$prefix/src; cp $(CURDIR)/dist/linuxbuild-$(RELEASE_VERSION).tar.bz2 $$prefix/src/; \
		(cd /opt/; tar cvf $(CURDIR)/release/$${t}.tar $$prefix; \
			   cp $(CURDIR)/release/$${t}.tar $(CURDIR)/release/$${t}.tar.bk; \
			   bzip2 -f $(CURDIR)/release/$${t}.tar; \
			   md5sum $(CURDIR)/release/$${t}.tar.bz2 > $(CURDIR)/release/$${t}.tar.bz2.md5; \
		); \
	done

cts-createdist-upload: #cts-createdist
	if [ -f ../upload_linuxbuild_tools.py ]; then \
		python ../upload_linuxbuild_tools.py $(RELEASE_VERSION); \
	fi
	-rm -rf dist-test
	-mkdir dist-test
	for f in `(cd release; ls sparc-linux-ct-*-$(RELEASE_VERSION).tar.bz2;)`; do \
		(cd dist-test; wget ftp://gaisler.com/gaisler.com/linux/linux-2.6/toolchains/sparc-linux-4.4.2/$$f; \
		tar xvf $$f;); \
	done;

clean-buildroot:
	-rm -rf build-br/*

clean-ct:
	-rm -rf build-ct/*

clean: clean-buildroot clean-ct

createdist-tar:
	cat docbook/xml/books/linuxbuild/linuxbuild-titlepage.xml.sed | sed \
	-e 's/RELVERSION/$(RELEASE_VERSION)/g' >docbook/xml/books/linuxbuild/linuxbuild-titlepage.xml
	#-cd docbook; make -f Makefile.linuxbuild pdf
	-rm -rf dist
	mkdir dist
	svn export $(CURDIR) dist/linuxbuild-$(RELEASE_VERSION)
	echo $(RELEASE_VERSION) > dist/linuxbuild-$(RELEASE_VERSION)/VERSION
	-rm -f dist/linuxbuild-$(RELEASE_VERSION)/Makefile.nodist.mk 
	-cp docbook/pdf/linuxbuild.pdf dist/linuxbuild-$(RELEASE_VERSION)/;
	cd dist; \
	    	rm -rf linuxbuild-$(RELEASE_VERSION)/docbook; \
	 	tar cvf linuxbuild-$(RELEASE_VERSION).tar linuxbuild-$(RELEASE_VERSION); bzip2 linuxbuild-$(RELEASE_VERSION).tar

createdist: createdist-tar
	if [ -f ../upload_linuxbuild.py ]; then \
		python ../upload_linuxbuild.py $(CURDIR)/dist/linuxbuild-$(RELEASE_VERSION).tar.bz2; \
	fi
	#-rm -rf dist-test
	-mkdir dist-test
	cd dist-test; rm -rf linuxbuild-$(RELEASE_VERSION)* ; wget ftp://gaisler.com/gaisler.com/linux/linuxbuild/linuxbuild-$(RELEASE_VERSION).tar.bz2; \
		tar xvf linuxbuild-$(RELEASE_VERSION).tar.bz2

show-cts:
	@echo $(CTDIRS)


#################### target gdb ###################

# CCDIR=$(CURDIR)/build-br/buildroot/sfleon_shared_basic/output/staging/usr/bin
# TGTDIR=$(CURDIR)/build-br/buildroot/sfleon_shared_basic/output/target

# gdb-static-prepare:r
# 	-if [ ! -f reference/gdb-7.1.tar.bz2 ] then cd reference; wget http://ftp.gnu.org/gnu/gdb/gdb-7.2.tar.bz2; fi
# 	-rm -rf gdb-7.1
# 	-tar xvf reference/gdb-7.1.tar.bz2
# 	make -f Makefile BUILDTOOTDIRS=config/buildroot/gdb_static buildroots 

# gdb-static-configure:
# 	-rm -rf gdb-7.1-build
# 	mkdir gdb-7.1-build;
# 	cd gdb-7.1-build; export PATH=$(CCDIR):$$PATH; \
# 		../gdb-7.1/configure --prefix=$(TGTDIR) \
# 		--target=sparc-linux \
# 		--host=sparc-linux \
# 			| tee make_gdb_config.txt

# #--with-curses

# gdb-static-compile:
# 	cd gdb-7.1-build; export PATH=$(CCDIR):$$PATH; \
# 		make | tee make_gdb_compile.txt
#

TOOLCHAIN?=hfleonv8_shared_basic
testprogs-compile:
	export PATH=$(CURDIR)/build-br/buildroot/$(TOOLCHAIN)/output/staging/usr/bin:$$PATH; \
	cd $(CURDIR)/testprogs/pthread; make; make DIR=$(CURDIR)/build-br/buildroot/$(TOOLCHAIN)/output/target/bin install; \
	cd $(CURDIR)/testprogs/fpu;     make; make DIR=$(CURDIR)/build-br/buildroot/$(TOOLCHAIN)/output/target/bin install;

gdb-copy:
	-rm -rf tmp/gdb
	mkdir -p tmp/gdb
	cp reference/gdb.bz2 tmp/gdb/
	bunzip2 tmp/gdb/gdb.bz2
	cp tmp/gdb/gdb $(CURDIR)/build-br/buildroot/$(TOOLCHAIN)/output/target/bin
	chmod a+rwx $(CURDIR)/build-br/buildroot/$(TOOLCHAIN)/output/target/bin/gdb


######################################################################
# use offcial 2.6.36-rc3 rev 2bfc96a127bc1cc94d26bfaa40159966064f9c8c as base
git-base-achive:
	-mkdir -p linux-2.6-ngmp-2bfc96a127bc1cc94d26bfaa40159966064f9c8c
	git archive 2bfc96a127bc1cc94d26bfaa40159966064f9c8c |  tar -x -C linux-2.6-ngmp-2bfc96a127bc1cc94d26bfaa40159966064f9c8c

git-patches:
	git format-patch -C -n  2bfc96a127bc1cc94d26bfaa40159966064f9c8c..$(shell git log --pretty=oneline | head -n 1 | awk '{ print $$1 }')

DEVICES = \
	ram,b,1,0	ram1,b,1,1 \
	null,c,1,3	tty1,c,4,1	tty2,c,4,2 \
	tty,c,5,0	console,c,5,1	ttyS0,c,4,64	ttyS1,c,4,65\
	\
	random,c,1,8	urandom,c,1,9 \
	\
	fb0,c,29,0 \
	\
	hda,b,3,0      hda1,b,3,1         hda2,b,3,2       hda3,b,3,3\
	hda4,b,3,4     hdb,b,3,64         hdb1,b,3,65\
\
	ptmx,c,5,2 \
\
	gpio0,c,212,0 \
	gpio1,c,212,1 \
	gpio1,c,212,2 \
	gpio1,c,212,3 \
\
	ptyp0,c,2,0    ptyp1,c,2,1        ptyp2,c,2,2      ptyp3,c,2,3 \
	ptyp4,c,2,4    ptyp5,c,2,5        ptyp6,c,2,6      ptyp7,c,2,7 \
	ptyp8,c,2,8    ptyp9,c,2,9        ptypa,c,2,10     ptypb,c,2,11 \
	ptypc,c,2,12   ptypd,c,2,13       ptype,c,2,14     ptypf,c,2,15 \
	\
	tty0,c,4,0     tty1,c,4,1         tty2,c,4,2       tty3,c,4,3 \
	tty4,c,4,4     tty5,c,1,3         tty6,c,4,6       tty7,c,4,7 \
	tty8,c,4,8     tty9,c,4,9   \
	\
	ttyp0,c,3,0    ttyp1,c,3,1        ttyp2,c,3,2      ttyp3,c,3,3 \
	ttyp4,c,3,4    ttyp5,c,3,5        ttyp6,c,3,6      ttyp7,c,3,7 \
	ttyp8,c,3,8    ttyp9,c,3,9        ttypa,c,3,10     ttypb,c,3,11 \
	ttypc,c,3,12   ttypd,c,3,13       ttype,c,3,14     ttypf,c,3,15 \
	\
	i2c-0,c,89,0	i2c-1,c,89,1	i2c-2,c,89,2	i2c-3,c,89,3 \
	i2c-4,c,89,4	i2c-5,c,89,5	i2c-6,c,89,6	i2c-7,c,89,7 \
	i2c-8,c,89,8	i2c-9,c,89,9	i2c-10,c,89,10	i2c-11,c,89,11 \
	i2c-12,c,89,12	i2c-13,c,89,13	i2c-14,c,89,14	i2c-15,c,89,15 \
	i2c-16,c,89,16	i2c-17,c,89,17	i2c-18,c,89,18	i2c-19,c,89,19 \
	i2c-20,c,89,20	i2c-21,c,89,21	i2c-22,c,89,22	i2c-23,c,89,23 \
	i2c-24,c,89,24	i2c-25,c,89,25	i2c-26,c,89,26	i2c-27,c,89,27 \
	i2c-28,c,89,28	i2c-29,c,89,29	i2c-30,c,89,30	i2c-31,c,89,31 \
	\
	etherd/err,c,152,2 		  etherd/discover,c,152,3  \
	etherd/interfaces,c,152,4	  etherd/revalidate,c,152,5 \
	etherd/flush,c,152,6 	\
	etherd/e0.0,b,152,0    		  etherd/e0.1,b,152,1    \
	etherd/e0.2,b,152,2    		  etherd/e0.3,b,152,3    \
	etherd/e0.4,b,152,4    		  etherd/e0.5,b,152,5    \
	etherd/e0.6,b,152,6    		  etherd/e0.7,b,152,7    \
	etherd/e0.8,b,152,8    		  etherd/e0.9,b,152,9    \
	etherd/e0.10,b,152,10  		  etherd/e0.11,b,152,11    \
	etherd/e0.12,b,152,12   	  etherd/e0.13,b,152,13    \
	etherd/e0.14,b,152,14  		  etherd/e0.15,b,152,15    \
	\
	sda,b,8,0      sda1,b,8,1	  sda2,b,8,2       sda3,b,8,3 \
	sdb,b,8,16     sdb1,b,8,17	  sdb2,b,8,18      sdb3,b,16,19 \
	sdc,b,8,32     sdc1,b,8,33	  sdc2,b,8,34      sdc3,b,8,35 \
	sdd,b,8,48     sdd1,b,8,49	  sdd2,b,8,50      sdd3,b,8,51 \
	sde,b,8,64     sde1,b,8,65	  sde2,b,8,66      sde3,b,8,67 \
	sdf,b,8,80     sdf1,b,8,81	  sdf2,b,8,82      sdf3,b,8,83 \
	\
	input/mouse,c,10,149	input/keyboard,c,10,150	\
	\
	input/mouse0,c,13,32 	input/mouse1,c,13,33	input/mouse2,c,13,34 \
	input/mouse3,c,13,35 	input/mouse4,c,13,36	input/mouse5,c,13,37 \
	input/mouse6,c,13,38 	input/mouse7,c,13,39	input/mouse8,c,13,40 \
	input/mouse9,c,13,41 	input/mouse10,c,13,42	input/mouse11,c,13,43 \
	input/mouse12,c,13,44 	input/mouse13,c,13,45	input/mouse14,c,13,46 \
	\
	input/mice,c,13,63 \
	\
	input/event0,c,13,64 	input/event1,c,13,65 	input/event2,c,13,66 \
	input/event3,c,13,67 	input/event4,c,13,68 	input/event5,c,13,69 \
	input/event6,c,13,70 	input/event7,c,13,71 	input/event8,c,13,72 \
	input/event9,c,13,73 	input/event10,c,13,74 	input/event11,c,13,75 \
	input/event12,c,13,76 	input/event13,c,13,77 	input/event14,c,13,78 \
	\
	usb/hiddev0,c,180,96	usb/hiddev1,c,180,97	usb/hiddev2,c,180,98 \
	usb/hiddev3,c,180,99	usb/hiddev4,c,180,100	usb/hiddev5,c,180,101 \
	usb/hiddev6,c,180,102	usb/hiddev7,c,180,103	usb/hiddev8,c,180,104 \
	usb/hiddev9,c,180,105	usb/hiddev10,c,180,106	usb/hiddev11,c,180,107 \
	\
	video0,c,81,0	video1,c,81,1	video2,c,81,2 	video3,v,81,3 \
	video4,c,81,4	video5,c,81,5	video6,c,81,6 	video7,v,81,7 \
	\
	mtd0,c,90,0	mtdr0,c,90,1	mtd1,c,90,2	mtdr1,c,90,3 \
	mtd2,c,90,4	mtdr2,c,90,5	mtd3,c,90,6	mtdr3,c,90,7 \
	mtd4,c,90,8	mtdr4,c,90,9	mtd4,c,90,10	mtdr4,c,90,11 \
	\
	xsysace,b,254,0	xsysace1,b,254,1 xsysace2,b,254,2  xsysace3,b,254,3 


TARGET?=targets/sfleon_simple
LINUX_WRAP_TAR=linuxwrap-1.0.4.tar.gz
LINUX_WRAP=reference/linuxwrap/linuxwrap.sh
linux-src:
	@if [ "x" == "x$(SRC)" ]; then echo "Usage: \"make SRC=[src] CONFIG=[config] linux-src\" [src]=one dir in patches/linux/*, [config]=one dir in config/linux/*"; exit 1 ; fi
	@if [ "x" == "x$(CONFIG)" ]; then echo "Usage: \"make SRC=[src] CONFIG=[config] linux-src\" [src]=one dir in patches/linux/*, [config]=one dir in config/linux/*"; exit 1; fi
	@if [ ! -d patches/linux/$(SRC) ]; then echo "Cannot find SRC:patches/linux/$(SRC)"; exit 1; fi;
	@if [ ! -d config/linux/$(CONFIG) ]; then echo "Cannot find CONFIG:config/linux/$(CONFIG)"; exit 1; fi;
	-mkdir build-linux;
	cd reference/; tar xvf $(LINUX_WRAP_TAR);
	for d in $(SRC); do \
		c=$(CONFIG); \
		n=$$d; echo "Try install src of $$n in build-linux/$$c";\
		f=`cat patches/linux/$$n/README | head -n 1 `;\
		rm -rf build-linux/$$c; \
		mkdir -p build-linux/$$c; \
		(cd build-linux/$$c; wget $$f; tar xf `basename $$f`); \
		ld=`cd build-linux/$$c; find . -maxdepth 1 -type d | grep -v ^\.$$ | grep -v ^\.\.$$`; \
		echo "patch directory: build-linux/$$c/$$ld";\
		if [ -d "build-linux/$$c/$$ld" ]; then \
			for p in `ls patches/linux/$${n}/*.patch`; do \
				echo "cat $$p | patch -d build-linux/$$c/$$ld -p1"; \
				cat $$p | patch -d build-linux/$$c/$$ld -p1; \
			done; \
		fi; \
		mv build-linux/$$c/$$ld/Makefile build-linux/$$c/$$ld/Makefile.bck; \
		echo    "ARCH=sparc" > build-linux/$$c/$$ld/Makefile; \
		echo -e 'ifeq ("$$(CONFIG_CROSS_COMPILE)","")\nCROSS_COMPILE=sparc-linux-\nendif\n' >> build-linux/$$c/$$ld/Makefile; \
		cat build-linux/$$c/$$ld/Makefile.bck >> build-linux/$$c/$$ld/Makefile; \
		cp config/linux/$(CONFIG)/config build-linux/$$c/$$ld/.config; \
		chmod a+x $(CURDIR)/build-linux/$$c/$$ld/scripts/gen_initramfs_list.sh; \
		t=$(TARGET); \
		if [ -d $(CURDIR)/$$t/.svn ]; then if [ -d $(CURDIR)/$$t.export ]; then rm -rf $(CURDIR)/$$t.export; fi; svn export $(CURDIR)/$$t $(CURDIR)/$$t.export; t=$$t.export; fi; \
		echo "Generating a intramfs using $$t: build-linux/$$c/$$ld/initramfs-root.txt"; \
		(cd $$t; ln -sf sbin/init init); \
		$(if $(TARGET_COPY_INITTAB),cp $(CURDIR)/reference/build-root-inittab $$t/etc/inittab;,) \
		(cd build-linux/$$c/$$ld/; $(CURDIR)/build-linux/$$c/$$ld/scripts/gen_initramfs_list.sh $(CURDIR)/$$t \
	          > $(CURDIR)/build-linux/$$c/$$ld/initramfs-root.txt;); \
		for i in $(DEVICES); do     \
			name=$${i%%,*};     \
			i=$${i##$${name},}; \
			type=$${i%%,*};     \
			i=$${i##$${type},}; \
			maj=$${i%%,*};      \
			i=$${i##$${maj},};  \
			min=$${i%%,*};      \
			echo "nod /dev/$$name 0600 0 0 $$type $$maj $$min" >> $(CURDIR)/build-linux/$$c/$$ld/initramfs-root.txt; \
		done; \
		a=$(CURDIR)/archive/$$c-$$n-$(subst /,.,$(TARGET))`date +"%y%m%d-%H%M"`; mkdir -p $$a; \
		echo "Building kernel image and copying to $$a/"; \
		(cd build-linux/$$c/$$ld/; nice make V=1 ; cp arch/sparc/boot/image $$a/; echo "Image at $$a/image"; echo -e "##################\nUse the following command to create a runnable image:\nlinuxwrap $$a/image $$a/image.grmon\n##################\n";); \
		(sh $(LINUX_WRAP) $$a/image $$a/image.grmon;); \
		echo "$$a/image.grmon" >> built_image.txt; \
	done




append_devs:
	for i in $(DEVICES); do \
		name=$${i%%,*}; \
		i=$${i##$${name},}; \
		type=$${i%%,*}; \
		i=$${i##$${type},}; \
		maj=$${i%%,*}; \
		i=$${i##$${maj},}; \
		min=$${i%%,*}; \
		echo "nod /dev/$$name 0600 0 0 $$type $$maj $$min" >> initramfs-root.txt; \
	done



#################### open embedded ###################

OE_PYTHON_PREFIX=$(CURDIR)/usr
OE_PYTHON_BUILD=$(CURDIR)/usr/build
BITBAKE_VERSION=1.10.1
BITBAKE_PREFIX=$(CURDIR)/usr/bitbake-$(BITBAKE_VERSION)
PYTHON_VERSION=2.7
CONFIG_PATH = $(PATH)
EXPORT_PATH = export PATH=$(OE_PYTHON_PREFIX)/bin:$(BITBAKE_PREFIX)/bin:$(CONFIG_PATH);\
	      export BBPATH=$(CURDIR)/build-oe/build:$(CURDIR)/build-oe/openembedded

oe-config:
	-mkdir -p build-oe/sources
	-mkdir -p build-oe/build/conf
	cd build-oe; \
	cat $(CURDIR)/config/oe/$(OEDIR)/local.conf | sed \
	-e 's/OEROOT/$(subst /,\/,$(CURDIR)/build-oe)/g' \
	> build/conf/local.conf

oe-prepare: oe-python oe-bitbake-prepare 
	-mkdir build-oe
	cd build-oe; \
		git clone git://git.openembedded.org/openembedded openembedded

oe-bitbake-nano:
	$(EXPORT_PATH); cd build-oe; \
		bitbake -c clean linux;\
		bitbake nano | tee _oe-bitbake-nano.txt; \

oe-bitbake-task-base:
	$(EXPORT_PATH); cd build-oe; \
		bitbake task-base | tee _oe-bitbake-task-base.txt; \




oe-bitbake-prepare:
	-mkdir -p build-oe
	cd build-oe; \
		wget http://download.berlios.de/bitbake/bitbake-$(BITBAKE_VERSION).tar.gz bitbake-$(BITBAKE_VERSION).tar.gz; \
		tar xvf bitbake-$(BITBAKE_VERSION).tar.gz; \
		if [ -d $(CURDIR)/patches/bitbake/$(BITBAKE_VERSION) ]; then \
			for f in `ls $(CURDIR)/patches/bitbake/$(BITBAKE_VERSION)/*.patch`; do \
				cat $$f | patch -p1 -d bitbake-$(BITBAKE_VERSION) ; \
			done; \
		fi; \
		if [ -d $(BITBAKE_PREFIX) ]; then rm -rf $(BITBAKE_PREFIX); fi; \
		cp -r bitbake-$(BITBAKE_VERSION) $(BITBAKE_PREFIX)

oe-python: oe-python-clean oe-python-prepare oe-python-configure oe-python-compile oe-python-install

oe-python-clean:
	-rm -rf $(CURDIR)/build-oe/python-root build-oe/Python-$(PYTHON_VERSION)-build;

oe-python-prepare:
	-mkdir -p $(OE_PYTHON_BUILD)
	cd $(OE_PYTHON_BUILD); \
		wget http://www.python.org/ftp/python/$(PYTHON_VERSION)/Python-$(PYTHON_VERSION).tar.bz2; \
		tar xvf Python-$(PYTHON_VERSION).tar.bz2;

oe-python-configure:
	-mkdir -p $(OE_PYTHON_BUILD)/Python-$(PYTHON_VERSION)-build
	cd $(OE_PYTHON_BUILD)/Python-$(PYTHON_VERSION)-build; \
		../Python-$(PYTHON_VERSION)/configure \
		--prefix=$(OE_PYTHON_PREFIX) \
		--with-ncurses \
		--with-threads \
	| tee _configure.txt

oe-python-compile:
	cd $(OE_PYTHON_BUILD)/Python-$(PYTHON_VERSION)-build; \
		make | tee _compile.txt

oe-python-install:
	cd $(OE_PYTHON_BUILD)/Python-$(PYTHON_VERSION)-build; \
		make install | tee _install.txt



#oe-python
ct-prepare: 
	cd $(OE_PYTHON_BUILD); \
		wget http://mercurial.selenic.com/release/mercurial-1.7.tar.gz; \
		tar xvf mercurial-1.7.tar.gz;

ct-prepare-configure:
	-mkdir -p $(OE_PYTHON_BUILD)/mercurial-1.7-build
	$(EXPORT_PATH); cd $(OE_PYTHON_BUILD)/mercurial-1.7; \
		make PREFIX=$(OE_PYTHON_PREFIX) build install-bin \
	| tee _configure.txt


-include Makefile.nodist.mk
