###############################################################
#####
##### Makefile for boop - OpenSource firmware for Betty
##### adapted for "Lernbetty"
#####
##### original (C) 
##### Makefile V0.1 by alterego - alteregon@gmx.net
##### Makefile v0.2 by Tobias - tobias-betty@23.gs
##### Created at 15.11.2012 
#####
##### Adapted Makefile version for "Lernbetty"
##### V0.3 by swausd 18.11.2012
#####
###############################################################

###############################################################
#####
##### PATHS (default installation)
#####
##### You can put your path-config into Makefile.local
##### to override these defaults
#####
###############################################################

ARMBASE = C:/yagarto
INCLUDEPATH = $(ARMBASE)/include
LIBPATH = $(ARMBASE)/lib/gcc/arm-none-eabi/4.7.2 -L$(ARMBASE)/arm-none-eabi/lib
ARMPATH = $(ARMBASE)/bin
TOOLPREFIX = arm-none-eabi-

CFLAGS = -Wall -mthumb-interwork -msoft-float -ggdb

#OPENOCD = openocd -f betty.cfg -f interface/parport.cfg

###############################################################
#####
##### Compiler, Linker and Tools
#####
###############################################################

CC = $(ARMPATH)/$(TOOLPREFIX)gcc
AS = $(ARMPATH)/$(TOOLPREFIX)as
LD = $(ARMPATH)/$(TOOLPREFIX)ld
OC = $(ARMPATH)/$(TOOLPREFIX)objcopy
OD = $(ARMPATH)/$(TOOLPREFIX)objdump

CPUFLAGS = -mcpu=arm7tdmi-s
OPTFLAGS = -O3
CFLAGS = -Wall -mthumb-interwork -msoft-float -DGCC
INC = -I$(INCLUDEPATH) -I.
#INC = -I$(INCLUDEPATH) -I. -Iinterrupt -Idisplay -Ikeyboard -Iaudio -Iinfrared -Iserial -Iflash -Icc1100 -Igui -Itimer -Igames -Iadc -Irtc  -Itools
ASFLAGS = --gstabs -mthumb-interwork -mfpu=softfpa
LDFLAGS = -Tlpc2220.ld -Map BettyApp.map
LIBS = -lc -lgcc -lm
THUMBFLAGS = -mthumb

COMPILE = $(CC) $(CPUFLAGS) $(CFLAGS) $(INC)

###############################################################
#####
##### Do it
#####
###############################################################

# Recursive expansion of Makefile rules.
define expand_dir
 # Reset vars for subdir for the case that Make.conf does not exist
 SUBDIRS :=
 SRCS :=
 THUMBSRCS :=
 THUMBSRCSUNOPT :=
 -include $(1)Make.conf
 ALLSRCS += $$(SRCS:%=$(1)%)
 ALLTHUMBSRCS += $$(THUMBSRCS:%=$(1)%)
 ALLTHUMBSRCSUNOPT += $$(THUMBSRCSUNOPT:%=$(1)%)
 DEPS += $(1).deps
 $$(foreach adir,$$(SUBDIRS),$$(eval $$(call expand_dir,$(1)$$(adir)/)))
endef

ALLSRCS :=
ALLTHUMBSRCS :=
ALLTHUMBSRCSUNOPT :=

$(eval $(call expand_dir,))

OBJS := $(patsubst %.asm,%.o,$(ALLSRCS:.c=.o)) $(ALLTHUMBSRCS:.c=.thumb.o) $(ALLTHUMBSRCSUNOPT:.c=.thumbunopt.o)

all:	version $(DEPS) BettyApp.bin BettyApp.hex

debug:	version.h $(DEPS) BettyApp.bin BettyApp.hex

release: clean version $(DEPS) BettyApp.bin BettyApp.hex
#	@echo -n '\n\nRelease erstellt SVN Version ++'
#	@cat .svn/entries | sed -n '4p'
	
version: 
#	echo -n '#define SVNVERSION ' > version.h
#	sed -n '4p' .svn/entries >> version.h

version.h:
	echo -n '#define SVNVERSION ' > version.h
	sed -n '4p' .svn/entries >> version.h
	
test: BettyApp.elf
	$(OD) -h $<

%.bin: %.elf
	$(OC) -O binary $< $@

%.hex: %.elf
	$(OC) -O ihex $< $@

BettyApp.elf: $(OBJS)
	$(LD) $(LDFLAGS) -L$(LIBPATH) -o $@ $^ $(LIBS)

%.o: %.asm
	$(AS) $(CPUFLAGS) $(ASFLAGS) -o $@ $<

%.o: %.c
	$(COMPILE) $(OPTFLAGS) -c -MMD -MF $(dir $<).deps/$(notdir $@) -o $@ $<

%.thumb.o: %.c
	$(COMPILE) $(THUMBFLAGS) $(OPTFLAGS) -c -MMD -MF $(dir $<).deps/$(notdir $@) -o $@ $<
	
%.thumbunopt.o: %.c
	$(COMPILE) $(THUMBFLAGS) -c -MMD -MF $(dir $<).deps/$(notdir $@) -o $@ $<

$(DEPS):
	mkdir -p $@	

uresident: resident
resident: BettyApp.bin
	$(LPCTOOL) -i -v -e -a $<

program: BettyApp.bin 
	$(OPENOCD) -c init -c 'flash_boop $<' -c shutdown

clean:
	-rm -Rf $(DEPS)
	-rm -f $(OBJS) *.elf *.bin *.hex *~

-include $(DEPS:=/*)
