]> cloudbase.mooo.com Git - z180-stamp.git/blob - mk/avr.rules.mk
wildcards for ls command (*, ?)
[z180-stamp.git] / mk / avr.rules.mk
1
2 # Be silent per default, but 'make V=1' will show all compiler calls.
3 ifneq ($(V),1)
4 Q := @
5 NULL := 2>/dev/null
6 else
7 QP := \#
8 endif
9
10 ###############################################################################
11 # Executables
12
13 CC = avr-gcc
14 LD = avr-gcc
15 OBJCOPY = avr-objcopy
16 OBJDUMP = avr-objdump
17 SIZE = avr-size
18 NM = avr-nm
19 AVRDUDE = avrdude
20 RM = rm -f
21 COPY = cp
22
23 OPT := -Os
24 CSTD ?= -std=gnu11
25
26 ###############################################################################
27 # Define all object files.
28
29 OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)
30
31 ###############################################################################
32 # C flags
33
34 TGT_CFLAGS += -mmcu=$(MCU) $(OPT) $(CSTD) -g
35 TGT_CFLAGS += $(ARCH_FLAGS)
36 TGT_CFLAGS += -Wextra -Wshadow -Wimplicit-function-declaration -Wimplicit-fallthrough=1
37 TGT_CFLAGS += -Wredundant-decls -Wstrict-prototypes
38 TGT_CFLAGS += -fshort-enums -funsigned-bitfields
39 TGT_CFLAGS += -fno-common -ffunction-sections -fdata-sections
40 TGT_CFLAGS += -mrelax
41
42 ###############################################################################
43 # C & C++ preprocessor common flags
44
45 TGT_CPPFLAGS += -MMD -MP -MF .dep/$(@F:%.o=%.d)
46 TGT_CPPFLAGS += -Wall -Wundef
47 TGT_CPPFLAGS += -DF_CPU=$(F_CPU)UL $(CDEFS) $(CINCS)
48
49 ###############################################################################
50 # Linker flags
51
52 TGT_LDFLAGS += $(ARCH_FLAGS)
53 TGT_LDFLAGS += -Wl,-Map=$(TARGET).map,--cref
54 TGT_LDFLAGS += -Wl,--gc-sections
55
56 ###############################################################################
57 # Avrdude
58
59 AVRDUDE_PORT ?= usb
60 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
61 AVRDUDE_BASIC = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
62 AVRDUDE_FLAGS = $(AVRDUDE_BASIC) $(AVRDUDE_NO_VERIFY) $(AVRDUDE_VERBOSE)
63
64 ###############################################################################
65
66 .SUFFIXES: .elf .hex .lss .map .sym
67 .SECONDARY:
68
69 .PHONY: elf hex lss sym size
70 elf: $(TARGET).elf
71 hex: $(TARGET).hex
72 lss: $(TARGET).lss
73 sym: $(TARGET).sym
74
75 size: $(TARGET).elf
76 @#$(QP)printf " SIZE $(TARGET).elf\n"
77 $(Q)$(SIZE) $(TARGET).elf
78
79 # Program the device.
80 .PHONY: flash fuses
81 flash: $(TARGET).hex
82 $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)
83
84 fuses:
85 $(AVRDUDE) $(AVRDUDE_FLAGS) -u -U hfuse:w:$(HFUSE):m -U lfuse:w:$(LFUSE):m
86
87 %.hex: %.elf
88 @$(QP)printf " OBJCOPY $(*).hex\n"
89 $(Q)$(OBJCOPY) -Oihex -R .eeprom $< $@
90
91 # Create extended listing file from ELF output file.
92 %.lss: %.elf
93 @$(QP)printf " OBJDUMP $@\n"
94 $(Q)$(OBJDUMP) -h -S $< > $@
95
96 # Create a symbol table from ELF output file.
97 %.sym: %.elf
98 @$(QP)printf " NM $@\n"
99 $(Q)$(NM) -n $< > $@
100
101 # Link: create ELF output file from object files.
102 %.elf %.map: $(OBJ)
103 @$(QP)printf " LD $@\n"
104 $(Q)$(LD) $(TGT_CFLAGS) $(CFLAGS) $(TGT_LDFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
105
106
107 # Compile: create object files from C source files.
108 %.o: %.c
109 @$(QP)printf " CC $<\n"
110 $(Q)$(CC) $(TGT_CFLAGS) $(CFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $@ -c $<
111
112 # Target: clean project.
113 .PHONY: clean distclean dist
114 clean:
115 @$(QP)printf " CLEAN\n"
116 $(Q)$(RM) $(OBJ) $(SRC:.c=.s) .dep/* $(TARGET).elf \
117 $(TARGET).hex $(TARGET).lss $(TARGET).map $(TARGET).sym
118
119 distclean: clean
120 @$(QP)printf " DISTCLEAN\n"
121 $(Q)$(RM) $(TARGET).tar.gz
122
123 dist: build
124 tar cvfz $(TARGET).tar.gz \
125 $(SRC) \
126 $(TARGET).hex $(TARGET).eep
127
128 # Include the dependency files.
129 -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)