#
# (C) Copyright 2000
# Wolfgang Denk, DENX Software Engineering, wd@denx.de.
#
# (C) Copyright 2002
# Sysgo Real-Time Solutions, GmbH <www.elinos.com>
# Marius Groeger <mgroeger@sysgo.de>
#
# See file CREDITS for list of people who contributed to this
# project.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#

#@echo -e "bulid lib common!?"

HOSTARCH := $(shell uname -m | \
	sed -e s/i.86/i386/ \
	    -e s/sun4u/sparc64/ \
	    -e s/arm.*/arm/ \
	    -e s/sa110/arm/)

ifeq ($(HOSTARCH),arm)
CROSS_COMPILE = arm-elf-
else
CROSS_COMPILE = $(ARCH)-elf-
endif
CROSS_DIR := /home/compiler/ckcore0907/bin/
ifeq ($(CROSS_DIR)ckcore-elf-gcc, $(wildcard $(CROSS_DIR)ckcore-elf-gcc))
CROSS_COMPILE := $(CROSS_DIR)ckcore-elf-
else
CROSS_COMPILE := ckcore-elf-
endif
export	CROSS_COMPILE HOSTARCH

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

TOPDIR	:= $(shell if [ "$$PWD" != "" ]; then echo $$PWD; else pwd; fi)
export	TOPDIR

ifeq (include/config.mk,$(wildcard include/config.mk))
# load ARCH, BOARD, and CPU configuration
include include/config.mk
export	ARCH CPU BOARD DRIVER VENDOR
# load other configuration
include $(TOPDIR)/config.mk

SUBDIRS	= tools cpu/$(CPU) board/$(BOARDDIR) drivers common fs net disk

#########################################################################
# armboot objects....order is important (i.e. start must be first)

OBJS  =	cpu/$(CPU)/start.o common/libcommon.a

#OBJS +=	fs/jffs2/libjffs2.a fs/cramfs/libcramfs.a net/libnet.a disk/libdisk.a
#OBJS +=	fs/cramfs/libcramfs.a net/libnet.a disk/libdisk.a
#OBJS +=	fs/cramfs/libcramfs.a net/libnet.a 

OBJS +=	board/$(BOARDDIR)/lib$(BOARD).a	cpu/$(CPU)/lib$(CPU).a


# include libcommon last for common low level functions referenced
# by the other libraries
# OBJS +=	common/libcommon.a

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

all:	version	$(ARCH)boot.srec $(ARCH)boot-x.bin getasm

version:
		@clear
		@echo "+-+-+-+-+-"
		$(CC) --version

$(ARCH)boot.srec:	$(ARCH)boot
		$(OBJCOPY) ${OBJCFLAGS} -O srec $< $@

BASEDIR=./
$(ARCH)boot-x.bin: $(ARCH)boot
		$(OBJCOPY) ${OBJCFLAGS} -O binary -R .note -R .comment -S $< $@
#		@touch flag && ./mkimageforflash $(ARCH)boot-x.bin flag && cat flag $(ARCH)boot-x.bin > uboot.img
#		@rm flag
#		cat $@ linux_hard_check.img > linux.img
#		cp -vf $@ $(BASEDIR)/images/imgarmboot-x

# $(ARCH)boot:	depend subdirs $(OBJS) $(LDSCRIPT)
$(ARCH)boot:	depend subdirs $(OBJS)
		$(LD) $(LDFLAGS) $(OBJS) -Map $(ARCH)boot.map -o $(ARCH)boot $(LIBGCC)

subdirs:
		@for dir in $(SUBDIRS) ; do $(MAKE) -C $$dir || exit 1 ; done

depend dep:
		@for dir in $(SUBDIRS) ; do $(MAKE) -C $$dir .depend ; done

tags:
		ctags -w `find $(SUBDIRS) include \
			\( -name CVS -prune \) -o \( -name '*.[ch]' -print \)`

#add by zxj @2008-05-22 => asm code view
getasm:
		@echo "Now Get Asm Code"
		$(OBJDUMP) -S $(ARCH)boot  > $(VIEW_ASM)
		@echo "Now Get the img"
		@chmod 777 genuimg.elf
		@chmod 777 mkimage
		./genuimg.elf $(ARCH)boot-x.bin $(ARCH)boot-y.img
#add by zxj @2008-05-26 => lrzsz
getres:
		@echo "Now send the imgae file"
		cp -f $(ARCH)boot-y.img /tftpboot
		cp -f $(ARCH)boot-x.bin /tftpboot
		@echo "Now send the asm file"
#		lsz $(ARCH)boot-y.img
		lsz $(ARCH)boot-x.bin
#		lsz $(VIEW_ASM)

#########################################################################
else
all install $(ARCH)boot $(ARCH)boot.srec depend dep:
	@echo "System not configured - see README" >&2
	@ exit 1
endif

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

unconfig:
	rm -f include/config.h include/config.mk include/config1.h

clean:
	find . -type f \
		\( -name 'core' -o -name '*.bak' -o -name '*~' \
		-o -name '*.o'  -o -name '*.a'  -o -name '*.depend' \) -print \
		| xargs rm -f
	rm -f examples/hello_world examples/timer
	rm -f tools/img2srec tools/mkimage tools/envcrc tools/gen_eth_addr
	rm -f tools/easylogo/easylogo
	rm -f tools/gdb/astest tools/gdb/gdbcont tools/gdb/gdbsend
	rm -f $(ARCH)boot $(ARCH)boot-x.bin $(ARCH)boot.elf $(ARCH)boot.srec $(ARCH)boot.map
	rm -f $(ARCH)boot-x.bin.img
	rm -f $(VIEW_ASM)

clobber:	clean
	rm -f $(OBJS) *.bak tags TAGS
	rm -fr *.*~
	rm -f $(ARCH)boot $(ARCH)boot-x.bin $(ARCH)boot.elf $(ARCH)boot.srec $(ARCH)boot.map
	rm -f tools/crc32.c tools/environment.S
	find . -name ".depend" -exec rm {} \;

distclean:	clobber unconfig

backup:
	F=`basename $(TOPDIR)` ; cd .. ; \
	tar --force-local -zcvf `date "+$$F-%Y-%m-%d-%H%M.tar.gz"` $$F
	cd .. ; mv *.gz /share/share_a/8947/armboot

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