#
# Makefile
#
PROG    = adam2_app

#
LOADADDR  = 0x94192000
KERNEL_TOP = /mips/linux-2.4.17_mvl21

# export CROSS_COMPILE=mipsel-linux-
# before compiling

AS      = $(CROSS_COMPILE)as
LD      = $(CROSS_COMPILE)ld
CC      = $(CROSS_COMPILE)gcc
CPP     = $(CC) -E
AR      = $(CROSS_COMPILE)ar
NM      = $(CROSS_COMPILE)nm
STRIP   = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
OBJCOPY = $(CROSS_COMPILE)objcopy
STRIP   = $(CROSS_COMPILE)strip

LDSTATIC  = -static
ELF2SREC = ${OBJCOPY} -O srec
GP      = -G 0
LDSCRIPT   = ld.script.stand

# strip debug
LINKFLAGS = -Map ${PROG}.map \
            $(LDSTATIC) \
            --omagic \
            --discard-all \
            --strip-all \
            --script ${LDSCRIPT} \
            -Ttext ${LOADADDR} \
            --entry=start \
            ${GP}

# include files search path
#INCLUDES = -I.
#INCSLUDES = -nostdinc -I$(GCCINCS) -I$(KERNEL_TOP)/include

# Linux Kernel isolation
DEFS	= -D__KERNEL__

# use -mips1 or 4KEc
CFLAGS  = -O2 -mips1 \
          -mno-half-pic -fno-pic \
          -mno-abicalls \
          -Wall

#SUBDIRS = lib

OBJS	= start.o adam2_app.o

#LIBS	= lib/libstand.a
.S.o:
	$(CC) $(CFLAGS) -c $< -o $*.o
%.o: %.c
	$(CC) $(CFLAGS) $(DEFS) $(INCLUDES) -c $< -o $@
	$(STRIP) --discard-all $@

#
${PROG}: srec2bin adam2_dump start.o $(PROG).o
	${LD} ${LINKFLAGS} -o ${PROG}.elf ${OBJS} $(LIBS)
#        @${SIZE} ${PROG}
	${ELF2SREC} ${PROG}.elf ${PROG}.srec
	./srec2bin $(PROG).srec $(PROG).bin
	./adam2_dump $(PROG).bin

#subdir:	
#	@list='$(SUBDIRS)'; \
#	for subdir in $$list; do \
#	echo "Making all in $$subdir"; \
#	(cd $$subdir && $(MAKE)) \
#	done;

# Must use HOST's gcc
srec2bin:
	gcc -o srec2bin srec2bin.c

adam2_dump:
	gcc -o adam2_dump adam2_dump.c

clean:
#	@list='$(SUBDIRS)'; \
#	for subdir in $$list; do \
#	echo "Making clean in $$subdir"; \
#	(cd $$subdir && $(MAKE) clean) \
#	done;
	-rm -f *.o core *.core
	-rm -f *.elf *.map *.srec *.bin
#	 srec2bin

