
# The name of the project
PRG            = ERA3
# Files owned by the user
OBJUSER        = ERA3.o 
# Files owned by the user to create pre-compiled headers
PCHUSER        = pch/ERA3.c 
# Generated files that are required
OBJREQD        = 
# Generated files that are optional
OBJOPT         = 

# The processor target
MCU_TARGET     = atmega640

# The processor speed
MCU_SPEED	   = 16000000

# The library where WebbotLib is installed
WEBBOT		   = ../Robot programming/ERA/ERA/lib

# The library to link against
WEBBOT_LIB = $(WEBBOT)/libWebbot-ATmega640.a


OPTIMIZE       = -O0
	

DEFS           = -gdwarf-2 -fpack-struct -fshort-enums  -funsigned-char -funsigned-bitfields -I"$(WEBBOT)" 
DEFS 		  +=  -MD -MP -MT $(*F).o -MF dep/$(@F).d


LIBS           = -L"$(WEBBOT)" -lWebbot-ATmega640  -lm -lc
	


# Define the tools to be used
CC             = avr-gcc
CPP            = avr-g++
OBJCOPY        = avr-objcopy
OBJDUMP        = avr-objdump
SIZE		   = avr-size
ARCHIVE		   = avr-ar

# Override is only needed by avr-lib build system.
# -Wall enable most warning errors
override CFLAGS        = -g -Wall  -DF_CPU=${MCU_SPEED} -mmcu=$(MCU_TARGET) $(DEFS)  -std=gnu99 
override CPPFLAGS      = -g -Wall  -DF_CPU=${MCU_SPEED} -mmcu=$(MCU_TARGET) $(DEFS) -fno-threadsafe-statics


# Use the minimal version of vfprintf
override LDFLAGS       = -Wl,-Map,$(PRG).map,-u,vfprintf -lprintf_min
	

# Default build target
all: $(PRG).elf lst text eeprom size

# Create pre-compiled headers for all the user files
pch: $(WEBBOT_LIB) $(PCHUSER)

pch/%.cpp: %.cpp
	$(CPP) -E $(CPPFLAGS) $(OPTIMIZE) -c -o $@ $<

pch/%.c: %.c
	$(CC) -E $(CFLAGS) $(OPTIMIZE) -c -o $@ $<

# How to create the elf file
$(PRG).elf:  $(WEBBOT_LIB) $(OBJUSER) $(OBJREQD)
	$(CC) -mmcu=$(MCU_TARGET) $(LDFLAGS) -o $@ $(OBJUSER) $(OBJREQD) $(LIBS)



# dependencies for compiling C files to O files

	
ERA3.o: ERA3.c
		$(CC) $(CFLAGS) $(OPTIMIZE) -c -o $@ ERA3.c

# dependencies for compiling CPP files to O files


size: $(PRG).elf
	$(SIZE) --format=avr --mcu=${MCU_TARGET} $(PRG).elf	

# Clean the compiled stuff but leave any codegen'd files in place
clean:
	rm -rf *.o dep/* pch/* lib/*.o lib/*.a $(PRG).elf example.txt  
	rm -r dep
	rm -r pch
	rm -rf $(PRG).lst $(PRG).map $(EXTRA_CLEAN_FILES)

# Clean out everything - including any codegen'd files
cleanall:
	rm -rf *.o dep/* pch/* $(PRG).elf example.txt hardware.h  
	rm -r lib
	rm -rf $(PRG).lst $(PRG).map $(EXTRA_CLEAN_FILES)

lst:  $(PRG).lst

%.lst: %.elf
	$(OBJDUMP) -h -S $< > $@

# 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: 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 $< $@ \
	|| { echo empty $@ not generated; exit 0; }

%_eeprom.srec: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@ \
	|| { echo empty $@ not generated; exit 0; }

%_eeprom.bin: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@ \
	|| { echo empty $@ not generated; exit 0; }

EXTRA_CLEAN_FILES       = $(PRG).hex $(PRG)_eeprom.hex *.bin *.srec

## Other dependencies
-include $(shell mkdir dep 2>/dev/null) $(wildcard dep/*)
-include $(shell mkdir pch 2>/dev/null) 

	