3 # Copyright 2002, 2003, 2004 Develer S.r.l. (http://www.develer.com/)
7 # GCC-AVR standard Makefile part 2
10 # Author: Bernardo Innocenti <bernie@develer.com>
13 # Revision 1.1 2006/03/22 09:51:53 bernie
14 # Add build infrastructure.
16 # Revision 1.39 2005/11/22 12:10:24 batt
17 # Avoid double build version increment.
19 # Revision 1.38 2005/11/18 13:29:33 batt
20 # Bumprev now work on linking and not only on make all.
22 # Revision 1.37 2005/03/20 03:59:44 bernie
23 # Fix link message to display target file name.
25 # Revision 1.36 2004/10/29 17:05:33 customer_pw
26 # Allow overriding flash_foobar rules.
28 # Revision 1.35 2004/10/20 10:00:14 customer_pw
31 # Revision 1.34 2004/10/19 11:06:51 bernie
34 # Revision 1.33 2004/10/19 10:56:20 bernie
35 # More specific logging messages.
37 # Revision 1.32 2004/10/18 14:40:45 customer_pw
38 # Add fuse var empty check
40 # Revision 1.31 2004/10/15 17:49:27 batt
41 # Do not verify avr chip after flashing to speed up the programming task.
43 # Revision 1.30 2004/10/09 10:34:16 aleph
44 # Fix broken linker rule
46 # Revision 1.29 2004/10/08 17:26:50 customer_pw
47 # No infinite loop in fuse programming rule; Better dependencies for linking.
49 # Revision 1.28 2004/10/03 18:26:52 bernie
52 # Revision 1.27 2004/09/30 14:49:53 customer_pw
55 # Revision 1.26 2004/09/28 16:58:52 customer_pw
56 # Repeat twice eeprom flashing
58 # Revision 1.25 2004/09/27 12:20:13 customer_pw
59 # Remove annoying flashing loop
61 # Revision 1.24 2004/09/23 17:19:50 customer_pw
62 # Per-target fuse rules, with retry.
64 # Revision 1.23 2004/09/20 02:49:28 bernie
67 # Revision 1.22 2004/09/14 22:19:09 bernie
68 # Create missing dirs.
70 # Revision 1.21 2004/08/31 10:25:10 customer_pw
71 # Remove mainly useless -y write count option of avrdude
74 # Remove all default pattern rules
88 # Initialize $(top_srcdir) with current directory, unless it was already initialized
89 top_srcdir ?= $(shell pwd)
92 TRG_ELF = $(TRG:%=$(OUTDIR)/%.elf)
93 TRG_S19 = $(TRG:%=$(OUTDIR)/%.s19)
94 TRG_HEX = $(TRG:%=$(OUTDIR)/%.hex)
95 TRG_BIN = $(TRG:%=$(OUTDIR)/%.bin)
96 TRG_ROM = $(TRG:%=$(OUTDIR)/%.rom)
97 TRG_COF = $(TRG:%=$(OUTDIR)/%.cof)
101 all:: $(TRG_S19) $(TRG_HEX)
103 # Generate project documentation
110 ifneq ($$(strip $$($(1)_MCU)),)
111 # Define all project specific object files
112 $(1)_CFLAGS += -mmcu=$$($(1)_MCU)
113 $(1)_CXXFLAGS += -mmcu=$$($(1)_MCU)
114 $(1)_ASFLAGS += -mmcu=$$($(1)_MCU)
115 $(1)_CPPAFLAGS += -mmcu=$$($(1)_MCU)
116 $(1)_LDFLAGS += -mmcu=$$($(1)_MCU)
118 ifneq ($$(strip $$($(1)_LDSCRIPT)),)
119 $(1)_LDFLAGS += -Wl,-T$$($(1)_LDSCRIPT)
122 $(1)_COBJ = $$(foreach file,$$($(1)_CSRC:%.c=%.o),$$(OBJDIR)/$(1)/$$(file))
123 $(1)_CXXOBJ = $$(foreach file,$$($(1)_CXXSRC:%.cpp=%.o),$$(OBJDIR)/$(1)/$$(file))
124 $(1)_PCOBJ = $$(foreach file,$$($(1)_PCSRC:%.c=%_P.o),$$(OBJDIR)/$(1)/$$(file))
125 $(1)_AOBJ = $$(foreach file,$$($(1)_ASRC:%.s=%.o),$$(OBJDIR)/$(1)/$$(file))
126 $(1)_CPPAOBJ = $$(foreach file,$$($(1)_CPPASRC:%.S=%.o),$$(OBJDIR)/$(1)/$$(file))
127 $(1)_OBJ := $$($(1)_COBJ) $$($(1)_CXXOBJ) $$($(1)_PCOBJ) $$($(1)_AOBJ) $$($(1)_CPPAOBJ)
128 $(1)_SRC := $$($(1)_CSRC) $$($(1)_CXXSRC) $$($(1)_PCSRC) $$($(1)_ASRC) $$($(1)_CPPASRC)
131 # Compile: instructions to create assembler and/or object files from C source
132 $$($(1)_COBJ) : $$(OBJDIR)/$(1)/%.o : %.c
133 $L "$(1): Compiling $$<"
134 @$$(MKDIR_P) $$(dir $$@)
135 $Q $$(CC) -c $$(CFLAGS) $$($(1)_CFLAGS) $$< -o $$@
137 # Compile: instructions to create assembler and/or object files from C++ source
138 $$($(1)_CXXOBJ) : $$(OBJDIR)/$(1)/%.o : %.cpp
139 $L "$(1): Compiling $$<"
140 @$$(MKDIR_P) $$(dir $$@)
141 $Q $$(CXX) -c $$(CFLAGS) $$($(1)_CFLAGS) $$< -o $$@
143 # Generate assembly sources from C files (debug)
144 $$(OBJDIR)/$(1)/%.s : %.c
145 $L "$(1): Generating asm source $$<"
146 @$$(MKDIR_P) $$(dir $$@)
147 $Q $$(CC) -S $$(CFLAGS) $$($(1)_CFLAGS) $$< -o $$@
149 # Generate special progmem variant of a source file
150 $$($(1)_PCOBJ) : $$(OBJDIR)/$(1)/%_P.o : %.c
151 $L "$(1): Compiling $$< (PROGMEM)"
152 @$$(MKDIR_P) $$(dir $$@)
153 $Q $$(CC) -c -D_PROGMEM $$(CFLAGS) $$($(1)_CFLAGS) $$< -o $$@
155 # Assemble: instructions to create object file from assembler files
156 $$($(1)_AOBJ): $$(OBJDIR)/$(1)/%.o : %.s
157 $L "$(1): Assembling $$<"
158 @$$(MKDIR_P) $$(dir $$@)
159 $Q $$(AS) -c $$(ASFLAGS) $$($(1)_ASFLAGS) $$< -o $$@
161 $$($(1)_CPPAOBJ): $$(OBJDIR)/$(1)/%.o : %.S
162 $L "$(1): Assembling with CPP $$<"
163 @$$(MKDIR_P) $$(dir $$@)
164 $Q $$(CC) -c $$(CPPAFLAGS) $$($(1)_CPPAFLAGS) $$< -o $$@
166 # Link: instructions to create elf output file from object files
167 $$(OUTDIR)/$(1).elf: bumprev $$($(1)_OBJ) $$($(1)_LDSCRIPT)
168 $L "$(1): Linking $$@"
169 @$$(MKDIR_P) $$(dir $$@)
170 $Q $$(LD) $$($(1)_OBJ) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
172 # Compile and link (program-at-a-time)
173 $$(OUTDIR)/$(1)_whole.elf: bumprev $$($(1)_SRC) $$($(1)_LDSCRIPT)
174 $L "$(1): Compiling and Linking whole program $$@"
175 @$$(MKDIR_P) $$(dir $$@)
176 $Q $$(CC) $$($(1)_SRC) $$(CFLAGS) $$($(1)_CFLAGS) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
179 # NOTE: we retry in case of failure because the STK500 programmer is crappy
181 flash_$(1): $(OUTDIR)/$(1).s19 flash_$(1)_local
182 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; then \
183 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; \
186 .PHONY: flash_$(1)_local
191 if [ ! -z "$$($(1)_efuse)" ] ; then \
192 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; then \
193 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; \
196 if [ ! -z "$$($(1)_hfuse)" ] ; then \
197 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; then \
198 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; \
201 if [ ! -z "$$($(1)_lfuse)" ] ; then \
202 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; then \
203 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; \
206 if [ ! -z "$$($(1)_lock)" ] ; then \
207 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; then \
208 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; \
213 # Generate build rules for all targets
214 $(foreach t,$(TRG),$(eval $(call build_target,$(t))))
218 $(OBJCOPY) -O ihex $< $@
221 $(OBJCOPY) -O srec $< $@
224 $(OBJCOPY) -O binary $< $@
227 $(OBJCOPY) -O avrobj $< $@
230 $(OBJCOPY) -O $(FORMAT) $< $@
231 # $(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" -O $(FORMAT) $< $(@:.rom=.eep)
234 $(COFFCONVERT) -O coff-ext-avr $< $@
235 # $(COFFCONVERT) -O coff-avr $< $@ # For use with AVRstudio 3
237 #make instruction to delete created files
242 BUILDREV_H = buildrev.h
246 if [ -f $(BUILDREV_H) ]; then \
247 buildnr=`sed <"$(BUILDREV_H)" -n -e 's/#define VERS_BUILD \([0-9][0-9]*\)/\1/p'`; \
249 buildnr=`expr $$buildnr + 1`; \
250 buildhost=`hostname`; \
251 echo "#define VERS_BUILD $$buildnr" >"$(BUILDREV_H)"; \
252 echo "#define VERS_HOST \"$$buildhost\"" >>"$(BUILDREV_H)"; \
253 echo "Building revision $$buildnr"
255 # Include dependencies
256 -include $(OBJ:%.o=%.d)