
all : check_kdevelop_charset config-backup linux linuxbin
	@if [ -f $(LINUXDIR)/linux-build.h ] ; then cat $(LINUXDIR)/linux-build.h ; fi

export PROJWD=$(shell pwd)
export COLORTERM=0
export LANG=C
############################################################################
#
# Vendor specific settings
#

PROJNAME=labpro
DEVNAME=cubis
DEVICE=$(PROJNAME)-$(DEVNAME)
COPYDEST=/tftpboot/

firm:
	make -C ../$(DEVICE)-firmware

appl:
	make -C ../$(DEVICE)-appl

CONSOLE_BAUD_RATE = 19200
LINUXDIR=linux-2.6.10
BASETAG=BASE020610

PATCHNAME=sagcobra
############################################################################

BINDIR=$(dir $(PROJWD))local/m68k-uclinux/bin

export BINDIR

ifneq "$(MAKECMDGOALS)" "clean"
ifneq "$(MAKECMDGOALS)" "distclean"
ifeq "$(wildcard $(BINDIR)/m68k-uclinux-gcc)" ""
    $(error cannot find cross software, build toolchain first)
endif
endif
endif



ARCH          = m68knommu
CROSS_COMPILE = $(BINDIR)/m68k-uclinux-

############################################################################

MAKEARCH = $(MAKE) ARCH=$(ARCH) CROSS_COMPILE="$(CROSS_COMPILE)"
MAKEARCH_KERNEL = $(MAKEARCH)

##---------------------------------------
## this roule should sync configs in
## both directions but only when
## real content is changed
##---------------------------------------
$(LINUXDIR)/.config : $(DEVICE).config FORCE
	# drop old log
	test ! -f config.log || mv -v config.log config-last.log
	# force existence
	test -f $@ || echo "$@ missing" >>config.log && cp -vau $< $@ >>config.log
	# check for delta
	if diff -s -q -I '^#' $< $@ ; \
	then echo "File $< and $@ are same, just touching" >>config.log ; touch --reference=$< --no-create $@ ;\
	else echo "File $< and $@ are diff" >>config.log ; cp -vau $< $@ >>config.log ;  cp -vau $@ $< >>config.log ;\
	fi
	find $< $@ -printf "%T+ %p\n" >>config.log

config-backup: $(LINUXDIR)/.config
	-find $< $@ -printf "%T+ %p\n" >>config.log
	cp -v $< $@ >>config.log
	cp -v $< $(HOSTNAME)-$(USER)-$(LINUXDIR).config


###########################################################################
RC=$(HOME)/.kde/share/config/kdeveloprc
RC=$(HOME)/.kde/share/config/kdeveloprc
check_kdevelop_charset:
	if [ -f $(RC) ] ; then \
		if grep -q "Encoding=" $(RC) && ! grep -q "Encoding=ISO 8859-1" $(RC) ; then \
			kdialog --error "Bitte in Einstellungen/Editor/Öffnen-Speichern\nISO 8859-1 auswählen!" ;\
			exit 2; \
		fi \
	fi

############################################################################
# generate a binary image from the elf file
linuxbin :
	$(CROSS_COMPILE)objcopy -O binary $(LINUXDIR)/vmlinux $(LINUXDIR)/linux.bin
	$(CROSS_COMPILE)objdump -th $(LINUXDIR)/vmlinux >$(LINUXDIR)/linux.map
	$(CROSS_COMPILE)objdump --full-contents --prefix-addresses --show-raw-insn -d  $(LINUXDIR)/vmlinux >$(LINUXDIR)/linux.lst
	test -z "$(COPYDEST)" || cp -vu $(LINUXDIR)/linux.bin $(COPYDEST)/$(DEVNAME)-vmlinux

############## this is required for ROM or HIRAM just as a reference ########

linuxbin2 :
	$(CROSS_COMPILE)objcopy -O binary --strip-all \
			--remove-section=.romvec \
			--remove-section=.text \
			--remove-section=.ramvec \
			--remove-section=.bss \
			--remove-section=.eram \
			--remove-section=.erom \
			--remove-section=.comment \
			$(LINUXDIR)/vmlinux $(LINUXDIR)/linux.data
	$(CROSS_COMPILE)objcopy -O binary --strip-all \
			--remove-section=.ramvec \
			--remove-section=.romvec \
			--remove-section=.bss \
			--remove-section=.data \
			--remove-section=.init \
			--remove-section=.eram \
			--remove-section=.erom \
			--remove-section=.comment \
			--set-section-flags=.romvec=CONTENTS,ALLOC,LOAD,READONLY,CODE \
			$(LINUXDIR)/vmlinux $(LINUXDIR)/linux.text
	cat $(LINUXDIR)/linux.text $(LINUXDIR)/linux.data > $(LINUXDIR)/linux2.bin


dist:	distclean
	mkdir -p saglinux
	(cd $(LINUXDIR) && svn diff --old=$(VENDORBASE) --new=.) >saglinux/$(LINUXDIR)-$(PATCHNAME).patch
	cp config-backup saglinux/$(LINUXDIR)-$(PATCHNAME).config
	cp Makefile saglinux/$(LINUXDIR)-$(PATCHNAME).Makefile
	cd saglinux && tar -cjf ../$(LINUXDIR)-$(PATCHNAME).tar.bz2 $(LINUXDIR)-$(PATCHNAME).* README

.PHONY: menuconfig

menuconfig: $(LINUXDIR)/.config
	$(MAKEARCH_KERNEL) -C $(LINUXDIR) menuconfig

.PHONY: xconfig

xconfig: $(LINUXDIR)/.config
	$(MAKEARCH_KERNEL) -C $(LINUXDIR) xconfig

clean:
	$(MAKEARCH_KERNEL) -C $(LINUXDIR) clean

distclean:
	$(MAKEARCH_KERNEL) -C $(LINUXDIR) distclean
	rm -f $(LINUXDIR)/linux.{bin,map}

linux:
	if [ -x bin/which_build.pl ] ; then \
		bin/which_build.pl -dLINUX_BUILD $(LINUXDIR)/linux-build.h ; \
	fi
	$(MAKEARCH_KERNEL) -C $(LINUXDIR)

FORCE:
	@true
