summaryrefslogtreecommitdiff
path: root/n/avr/make/Makefile.avr
blob: 26f3fd17fc6db78aa71392ad0c71b7bdd12f7417 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Makefile.avr - AVR part Makefile.
#
# Flags. {{{1

AVR_CFLAGS = $(CFLAGS) -mmcu=$(AVR_MCU)
AVR_CPPFLAGS = $(CPPFLAGS) $(AVR_DEFS)
AVR_LDFLAGS = $(LDFLAGS)
AVR_LDLIBS = $(LDLIBS) $(AVR_LIBS)

AVR_CC = avr-gcc
AVR_OBJCOPY = avr-objcopy
AVR_OBJDUMP = avr-objdump
AVR_COMPILE.c = $(AVR_CC) $(AVR_CFLAGS) $(AVR_CPPFLAGS) -c
AVR_LINK.o = $(AVR_CC) $(AVR_CFLAGS) $(AVR_LDFLAGS)

# Main rules. {{{1

avr: elf lst hex

.PHONY: avr clean.avr elf lst text hex srec bin eeprom ehex esrec ebin

# General rules. {{{1

AVR_PROGS += $(PROGS)
AVR_ELFS = $(AVR_PROGS:%=%.avr.elf)
AVR_SOURCES = $(filter-out %.host.c,$(SOURCES))
AVR_OBJECTS = $(AVR_SOURCES:%.c=%.avr.o)

elf: $(AVR_ELFS)
lst: $(AVR_PROGS:%=%.avr.lst)

define AVR_PROG_template
$(1).avr.elf: $$(patsubst %.c,%.avr.o,$$(filter-out %.host.c,$$($(1)_SOURCES)))
endef

$(foreach prog,$(AVR_PROGS),$(eval $(call AVR_PROG_template,$(prog))))

$(AVR_ELFS):
	$(AVR_LINK.o) $^ $(AVR_LDLIBS) -o $@

%.avr.lst: %.avr.elf
	$(AVR_OBJDUMP) -h -S $< > $@

%.avr.o: %.c
	$(AVR_COMPILE.c) -o $@ $<

# Dependency checking.
-include $(AVR_OBJECTS:%.avr.o=%.avr.d)

# Rules for building the .text rom images. {{{1

TEXTS = $(AVR_PROGS:%=%.hex) $(AVR_PROGS:%=%.bin) $(AVR_PROGS:%=%.srec)

text: hex

hex: $(AVR_PROGS:%=%.hex)
bin: $(AVR_PROGS:%=%.bin)
srec: $(AVR_PROGS:%=%.srec)

%.hex: %.avr.elf
	$(AVR_OBJCOPY) -j .text -j .data -O ihex $< $@

%.srec: %.avr.elf
	$(AVR_OBJCOPY) -j .text -j .data -O srec $< $@

%.bin: %.avr.elf
	$(AVR_OBJCOPY) -j .text -j .data -O binary $< $@

# Rules for building the .eeprom rom images. {{{1

EEPROMS = $(AVR_PROGS:%=%_eeprom.hex) $(AVR_PROGS:%=%_eeprom.bin) \
	  $(AVR_PROGS:%=%_eeprom.srec)

eeprom: ehex

ehex: $(AVR_PROGS:%=%_eeprom.hex)
ebin: $(AVR_PROGS:%=%_eeprom.bin)
esrec: $(AVR_PROGS:%=%_eeprom.srec)

%_eeprom.hex: %.avr.elf
	$(AVR_OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@

%_eeprom.srec: %.avr.elf
	$(AVR_OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@

%_eeprom.bin: %.avr.elf
	$(AVR_OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@

# Cleaning. {{{1

clean.avr:
	rm -f *.avr.o *.avr.d $(AVR_ELFS) *.avr.lst *.avr.map $(TEXTS) $(EEPROMS)