
# The name of the project
PRG            = final
# Files owned by the user
OBJUSER        = final.o 
# Generated files that are required
OBJREQD        = lib/lib_timers.o lib/gen_clock.o lib/lib_hardware.o lib/lib_iopins.o 
# Generated files that are optional
OBJOPT         = lib/opt_TimerOverflow.o lib/opt_pcint.o lib/opt_TimerCapture.o lib/opt_TimerCompare.o 

# The processor target
MCU_TARGET     = atmega8

# The processor speed
MCU_SPEED	   = 1000000

# The library where WebbotLib is installed
WEBBOT		   = "C:/webbotavrclib-2.02"


OPTIMIZE       = -O0

DEFS           = -gdwarf-2 -std=gnu99 -fpack-struct -fshort-enums  -funsigned-char -funsigned-bitfields -I$(WEBBOT)
DEFS 		  +=  -MD -MP -MT $(*F).o -MF $(*F).d


LIBS           = -L$(WEBBOT) -L"lib" -lWebbot-ATmega8  -lPost -lm -lc
	

# Define the tools to be used
CC             = avr-gcc
OBJCOPY        = avr-objcopy
OBJDUMP        = avr-objdump
SIZE		   = avr-size
ARCHIVE		   = avr-ar

# Override is only needed by avr-lib build system.
override CFLAGS        = -g -Wall -DF_CPU=${MCU_SPEED} -mmcu=$(MCU_TARGET) $(DEFS)
override LDFLAGS       = -Wl,-Map,$(PRG).map

# Default build target
all: $(PRG).elf lst text eeprom size

# How to create the elf file
$(PRG).elf: lib/libPost.a  $(OBJUSER) $(OBJREQD)
	$(CC) -mmcu=$(MCU_TARGET) $(LDFLAGS) -o $@ $(OBJUSER) $(OBJREQD) $(LIBS)


# Create an archive for the optional items so they are only added if called
lib/libPost.a: $(OBJOPT)
	$(ARCHIVE) -r $@ $(OBJOPT)


# dependencies for compiling C files to O files

	
lib/opt_TimerOverflow.o: lib/opt_TimerOverflow.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/opt_TimerOverflow.c
	
lib/lib_timers.o: lib/lib_timers.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/lib_timers.c
	
final.o: final.c
		$(CC) $(CFLAGS) $(OPTIMIZE) -c -o $@ final.c
	
lib/opt_pcint.o: lib/opt_pcint.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/opt_pcint.c
	
lib/gen_clock.o: lib/gen_clock.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/gen_clock.c
	
lib/opt_TimerCapture.o: lib/opt_TimerCapture.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/opt_TimerCapture.c
	
lib/opt_TimerCompare.o: lib/opt_TimerCompare.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/opt_TimerCompare.c
	
lib/lib_hardware.o: lib/lib_hardware.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/lib_hardware.c
	
lib/lib_iopins.o: lib/lib_iopins.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/lib_iopins.c

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 *.d lib/*.o lib/*.a $(PRG).elf example.txt  
	rm -rf *.lst *.map $(EXTRA_CLEAN_FILES)

# Clean out everything - including any codegen'd files
cleanall:
	rm -rf *.o *.d lib/*.o lib/*.a lib/*.c $(PRG).elf example.txt hardware.h  
	rm -rf *.lst *.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       = *.hex *.bin *.srec

	