NAME := gpsrl
HEX := $(NAME).hex
OUT := $(NAME).out
MAP := $(NAME).map
SOURCES := $(wildcard *.c)
HEADERS := $(wildcard *.h)
OBJECTS := $(patsubst %.c,%.o,$(SOURCES))

MCU := atmega8
MCU_AVRDUDE := m8

CC := avr-gcc
OBJCOPY := avr-objcopy
SIZE := avr-size -A
DOXYGEN := doxygen

CFLAGS := -Wall -pedantic -mmcu=$(MCU) -std=c99 -g -Os

all: $(HEX)

clean:
        rm -f $(HEX) $(OUT) $(MAP) $(OBJECTS)
        rm -rf doc/html

flash: $(HEX)
        avrdude -y -p $(MCU_AVRDUDE) -P /dev/ttyUSB0 -c stk500v2 -U flash:w:$(HEX)

$(HEX): $(OUT)
        $(OBJCOPY) -R .eeprom -O ihex $< $@

$(OUT): $(OBJECTS)
        $(CC) $(CFLAGS) -o $@ -Wl,-Map,$(MAP) $^
        @echo
        @$(SIZE) $@
        @echo

%.o: %.c $(HEADERS)
        $(CC) $(CFLAGS) -c -o $@ $<

%.pp: %.c
        $(CC) $(CFLAGS) -E -o $@ $<

%.ppo: %.c
        $(CC) $(CFLAGS) -E $<

doc: $(HEADERS) $(SOURCES) Doxyfile
        $(DOXYGEN) Doxyfile

.PHONY: all clean flash doc