-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
70 lines (62 loc) · 2.26 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
TARGET = distance
PROGRAMMER = usbtiny
MCU = atmega328
F_CPU = 16000000UL
SRCS = $(wildcard *.c) $(wildcard util/*.c)
OBJS = $(subst .c,.o,$(SRCS))
INC = -I. -Iutil
GIT_VERSION = $(shell git describe --abbrev=4 --dirty --always --tags)
TOOLS_PREFIX = avr-
CC = $(TOOLS_PREFIX)gcc
#--------------------------------
# Compiler flags
#--------------------------------
## Warnings, standards
CFLAGS = -O2 -Wall -std=gnu99
## Hardware definitions
CFLAGS += -DF_CPU=$(F_CPU) -mmcu=$(MCU) -DGIT_VERSION=\"$(GIT_VERSION)\"
## Use short (8-bit) data types
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
## Splits up object files per function
CFLAGS += -ffunction-sections -fdata-sections
#--------------------------------
# Linker flags
#--------------------------------
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
# Optional, but often ends up with smaller code
LDFLAGS += -Wl,--gc-sections
## Relax shrinks code even more, but makes disassembly messy
LDFLAGS += -Wl,--relax
## for floating-point printf
# LDFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm
## for smaller printf
# LDFLAGS += -Wl,-u,vfprintf -lprintf_min
all: clean $(TARGET).hex $(TARGET).lst flash
flash: $(TARGET).hex
avrdude -c $(PROGRAMMER) -p $(MCU) -U flash:w:$<
%.o: %.c
@echo -----------------------------------------------
@echo Compiling $@
@echo -----------------------------------------------
$(CC) $(INC) $(CFLAGS) -o $@ -c $<
%.lst: %.elf
@echo -----------------------------------------------
@echo Generating assembly listing
@echo -----------------------------------------------
$(TOOLS_PREFIX)objdump -h -S -z $< > $@
%.hex: %.elf
@echo -----------------------------------------------
@echo Generating intel hex file
@echo -----------------------------------------------
$(TOOLS_PREFIX)objcopy -O ihex -R .eeprom $< $@
$(TARGET).elf: $(OBJS)
@echo -----------------------------------------------
@echo Linking together an application .elf file
@echo -----------------------------------------------
$(CC) -mmcu=$(MCU) $(LDFLAGS) -o $@ $^
chmod -x $@
$(TOOLS_PREFIX)size $@
clean:
rm -f $(TARGET).hex $(TARGET).lst $(TARGET).elf $(TARGET).map $(OBJS)
.PHONY: clean all
#.INTERMEDIATE: $(TARGET).lst $(TARGET).elf