# makefile for maze program for barello.net AVR1.1 kit

# REVIEW: put created files in debug dir! [gcc arg for this?]
# REVIEW: cmd line arg (or something... target?) for build type... debug, release...

# IMPORTANT: heap.o MUST go last!!!

PRG            = maze
OBJ            = main.o debug.o iop.o di.o ai.o do.o mtr.o comm.o eeprom.o \
				 eyes.o timer.o maze.o heap.o
MCU_TARGET     = atmega16
OPTIMIZE       = -Os
#OPTIMIZE       = -O2

#VPATH          = debug

DEFS           =
LIBS           =

# compile of .c auto with $(CC) -c $(CPPFLAGS) $(CFLAGS)
# compile of .cpp auto with (CXX) -c $(CPPFLAGS) $(CXXFLAGS)

CC             = avr-gcc
CXX            = avr-g++

# Override is only needed by avr-lib build system.

override CFLAGS        = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS)
override CPPFLAGS      = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS)
override LDFLAGS       = -Wl,-Map,$(PRG).map

OBJCOPY        = avr-objcopy
OBJDUMP        = avr-objdump

# (this is the default rule since it's first)
all: $(PRG).elf lst text eeprom_files

$(PRG).elf: $(OBJ)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)

clean:
	rm -rf *.o $(PRG).elf *.eps *.png *.pdf *.bak 
	rm -rf *.lst *.map $(EXTRA_CLEAN_FILES)

lst:  $(PRG).lst

%.lst: %.elf
	$(OBJDUMP) -h -S $< > $@

main.o:		abc.h debug.h iop.h di.h ai.h do.h mtr.h comm.h eeprom.h eyes.h timer.h maze.h
debug.o:	abc.h debug.h
iop.o:		abc.h debug.h iop.h
di.o:		abc.h debug.h di.h iop.h
ai.o:		abc.h debug.h ai.h iop.h comm.h
do.o:		abc.h debug.h do.h iop.h timer.h
mtr.o:		abc.h debug.h mtr.h iop.h
comm.o:		abc.h debug.h comm.h
eeprom.o:	abc.h debug.h eeprom.h ai.h
eyes.o:		abc.h debug.h eyes.h eeprom.h ai.h
timer.o:	abc.h debug.h timer.h
maze.o:		abc.h debug.h maze.h

# Rules for building the .text rom images

text: hex bin srec

hex:  $(PRG).hex
bin:  $(PRG).bin
srec: $(PRG).srec

%.hex: %.elf
	$(OBJCOPY) -j .text -j .data -O ihex $< $@

%.srec: %.elf
	$(OBJCOPY) -j .text -j .data -O srec $< $@

%.bin: %.elf
	$(OBJCOPY) -j .text -j .data -O binary $< $@

# Rules for building the .eeprom rom images

eeprom_files: ehex ebin esrec

ehex:  $(PRG)_eeprom.hex
ebin:  $(PRG)_eeprom.bin
esrec: $(PRG)_eeprom.srec

%_eeprom.hex: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@

%_eeprom.srec: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@

%_eeprom.bin: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@

# Every thing below here is used by avr-libc's build system and can be ignored
# by the casual user.

FIG2DEV                 = fig2dev
EXTRA_CLEAN_FILES       = *.hex *.bin *.srec

dox: eps png pdf

eps: $(PRG).eps
png: $(PRG).png
pdf: $(PRG).pdf

%.eps: %.fig
	$(FIG2DEV) -L eps $< $@

%.pdf: %.fig
	$(FIG2DEV) -L pdf $< $@

%.png: %.fig
	$(FIG2DEV) -L png $< $@

