ff5b2f0b68af68a75c1e40a48e5f4fa186aa205f
[bertos.git] / rules.mk
1 #
2 # $Id$
3 # Copyright 2002, 2003, 2004 Develer S.r.l. (http://www.develer.com/)
4 # All rights reserved.
5 #
6 # Based on:
7 #   GCC-AVR standard Makefile part 2
8 #   Volker Oth 1/2000
9 #
10 # Author: Bernardo Innocenti <bernie@develer.com>
11 #
12 # $Log$
13 # Revision 1.5  2006/09/13 18:30:52  bernie
14 # Add CPPFLAGS to all rules.
15 #
16 # Revision 1.4  2006/07/19 12:56:24  bernie
17 # Convert to new Doxygen style.
18 #
19 # Revision 1.3  2006/05/27 22:42:24  bernie
20 # Search for verstag.h in app subdirs first.
21 #
22 # Revision 1.2  2006/03/27 04:48:33  bernie
23 # Add CXXFLAGS; Add recursive targets.
24 #
25 # Revision 1.1  2006/03/22 09:51:53  bernie
26 # Add build infrastructure.
27 #
28 # Revision 1.39  2005/11/22 12:10:24  batt
29 # Avoid double build version increment.
30 #
31 # Revision 1.38  2005/11/18 13:29:33  batt
32 # Bumprev now work on linking and not only on make all.
33 #
34 # Revision 1.37  2005/03/20 03:59:44  bernie
35 # Fix link message to display target file name.
36 #
37 # Revision 1.36  2004/10/29 17:05:33  customer_pw
38 # Allow overriding flash_foobar rules.
39 #
40 # Revision 1.35  2004/10/20 10:00:14  customer_pw
41 # Simplify variable
42 #
43 # Revision 1.34  2004/10/19 11:06:51  bernie
44 # Set top_srcdir.
45 #
46 # Revision 1.33  2004/10/19 10:56:20  bernie
47 # More specific logging messages.
48 #
49 # Revision 1.32  2004/10/18 14:40:45  customer_pw
50 # Add fuse var empty check
51 #
52 # Revision 1.31  2004/10/15 17:49:27  batt
53 # Do not verify avr chip after flashing to speed up the programming task.
54 #
55 # Revision 1.30  2004/10/09 10:34:16  aleph
56 # Fix broken linker rule
57 #
58 # Revision 1.29  2004/10/08 17:26:50  customer_pw
59 # No infinite loop in fuse programming rule; Better dependencies for linking.
60 #
61 # Revision 1.28  2004/10/03 18:26:52  bernie
62 # Unparenthesize $Q.
63 #
64 # Revision 1.27  2004/09/30 14:49:53  customer_pw
65 # Add silent build
66 #
67 # Revision 1.26  2004/09/28 16:58:52  customer_pw
68 # Repeat twice eeprom flashing
69 #
70 # Revision 1.25  2004/09/27 12:20:13  customer_pw
71 # Remove annoying flashing loop
72 #
73 # Revision 1.24  2004/09/23 17:19:50  customer_pw
74 # Per-target fuse rules, with retry.
75 #
76 # Revision 1.23  2004/09/20 02:49:28  bernie
77 # Use  for linking.
78 #
79 # Revision 1.22  2004/09/14 22:19:09  bernie
80 # Create missing dirs.
81 #
82 # Revision 1.21  2004/08/31 10:25:10  customer_pw
83 # Remove mainly useless -y write count option of avrdude
84 #
85
86 # Remove all default pattern rules
87 .SUFFIXES:
88
89 # Verbosity
90 ifeq ($(V),1)
91 # Verbose build
92 Q :=
93 L := @echo >/dev/null
94 else
95 # Quiet build
96 Q := @
97 L := @echo
98 endif
99
100 # Initialize $(top_srcdir) with current directory, unless it was already initialized
101 top_srcdir ?= $(shell pwd)
102
103 # Products
104 TRG_ELF = $(TRG:%=$(OUTDIR)/%.elf)
105 TRG_S19 = $(TRG:%=$(OUTDIR)/%.s19)
106 TRG_HEX = $(TRG:%=$(OUTDIR)/%.hex)
107 TRG_BIN = $(TRG:%=$(OUTDIR)/%.bin)
108 TRG_ROM = $(TRG:%=$(OUTDIR)/%.rom)
109 TRG_COF = $(TRG:%=$(OUTDIR)/%.cof)
110
111
112 RECURSIVE_TARGETS = all-recursive install-recursive clean-recursive
113
114 # The default target
115 .PHONY: all
116 all:: all-recursive $(TRG_S19) $(TRG_HEX)
117
118 # Generate project documentation
119 .PHONY: docs
120 docs:
121         $Q $(DOXYGEN)
122
123 define build_target
124
125 ifneq ($$(strip $$($(1)_MCU)),)
126 # Define all project specific object files
127 $(1)_CFLAGS    += -mmcu=$$($(1)_MCU)
128 $(1)_CXXFLAGS  += -mmcu=$$($(1)_MCU)
129 $(1)_ASFLAGS   += -mmcu=$$($(1)_MCU)
130 $(1)_CPPAFLAGS += -mmcu=$$($(1)_MCU)
131 $(1)_LDFLAGS   += -mmcu=$$($(1)_MCU)
132 endif
133 ifneq ($$(strip $$($(1)_LDSCRIPT)),)
134 $(1)_LDFLAGS += -Wl,-T$$($(1)_LDSCRIPT)
135 endif
136
137 $(1)_COBJ    = $$(foreach file,$$($(1)_CSRC:%.c=%.o),$$(OBJDIR)/$(1)/$$(file))
138 $(1)_CXXOBJ  = $$(foreach file,$$($(1)_CXXSRC:%.cpp=%.o),$$(OBJDIR)/$(1)/$$(file))
139 $(1)_PCOBJ   = $$(foreach file,$$($(1)_PCSRC:%.c=%_P.o),$$(OBJDIR)/$(1)/$$(file))
140 $(1)_AOBJ    = $$(foreach file,$$($(1)_ASRC:%.s=%.o),$$(OBJDIR)/$(1)/$$(file))
141 $(1)_CPPAOBJ = $$(foreach file,$$($(1)_CPPASRC:%.S=%.o),$$(OBJDIR)/$(1)/$$(file))
142 $(1)_OBJ    := $$($(1)_COBJ) $$($(1)_CXXOBJ) $$($(1)_PCOBJ) $$($(1)_AOBJ) $$($(1)_CPPAOBJ)
143 $(1)_SRC    := $$($(1)_CSRC) $$($(1)_CXXSRC) $$($(1)_PCSRC) $$($(1)_ASRC) $$($(1)_CPPASRC)
144 OBJ         += $$($(1)_OBJ)
145
146 # Compile: instructions to create assembler and/or object files from C source
147 $$($(1)_COBJ) : $$(OBJDIR)/$(1)/%.o : %.c
148         $L "$(1): Compiling $$< (C)"
149         @$$(MKDIR_P) $$(dir $$@)
150         $Q $$(CC) -c $$($(1)_CPPFLAGS) $$(CFLAGS) $$($(1)_CFLAGS) $$< -o $$@
151
152 # Compile: instructions to create assembler and/or object files from C++ source
153 $$($(1)_CXXOBJ) : $$(OBJDIR)/$(1)/%.o : %.cpp
154         $L "$(1): Compiling $$< (C++)"
155         @$$(MKDIR_P) $$(dir $$@)
156         $Q $$(CXX) -c $$($(1)_CPPFLAGS) $$(CXXFLAGS) $$($(1)_CXXFLAGS) $$< -o $$@
157
158 # Generate assembly sources from C files (debug)
159 $$(OBJDIR)/$(1)/%.s : %.c
160         $L "$(1): Generating asm source $$<"
161         @$$(MKDIR_P) $$(dir $$@)
162         $Q $$(CC) -S $$($(1)_CPPFLAGS) $$(CFLAGS) $$($(1)_CFLAGS) $$< -o $$@
163
164 # Generate special progmem variant of a source file
165 $$($(1)_PCOBJ) : $$(OBJDIR)/$(1)/%_P.o : %.c
166         $L "$(1): Compiling $$< (PROGMEM)"
167         @$$(MKDIR_P) $$(dir $$@)
168         $Q $$(CC) -c -D_PROGMEM $$($(1)_CPPFLAGS) $$(CFLAGS) $$($(1)_CFLAGS) $$< -o $$@
169
170 # Assemble: instructions to create object file from assembler files
171 $$($(1)_AOBJ): $$(OBJDIR)/$(1)/%.o : %.s
172         $L "$(1): Assembling $$<"
173         @$$(MKDIR_P) $$(dir $$@)
174         $Q $$(AS) -c $$(ASFLAGS) $$($(1)_ASFLAGS) $$< -o $$@
175
176 $$($(1)_CPPAOBJ): $$(OBJDIR)/$(1)/%.o : %.S
177         $L "$(1): Assembling with CPP $$<"
178         @$$(MKDIR_P) $$(dir $$@)
179         $Q $$(CC) -c $$($(1)_CPPFLAGS) $$(CPPAFLAGS) $$($(1)_CPPAFLAGS) $$< -o $$@
180
181 # Link: instructions to create elf output file from object files
182 $$(OUTDIR)/$(1).elf: bumprev $$($(1)_OBJ) $$($(1)_LDSCRIPT)
183         $L "$(1): Linking $$@"
184         @$$(MKDIR_P) $$(dir $$@)
185         $Q $$(LD) $$($(1)_OBJ) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
186
187 # Compile and link (program-at-a-time)
188 $$(OUTDIR)/$(1)_whole.elf: bumprev $$($(1)_SRC) $$($(1)_LDSCRIPT)
189         $L "$(1): Compiling and Linking whole program $$@"
190         @$$(MKDIR_P) $$(dir $$@)
191         $Q $$(CC) $$($(1)_SRC) $$(CFLAGS) $$($(1)_CFLAGS) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
192
193 # Flash target
194 # NOTE: we retry in case of failure because the STK500 programmer is crappy
195 .PHONY: flash_$(1)
196 flash_$(1): $(OUTDIR)/$(1).s19 flash_$(1)_local
197         if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; then \
198              $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; \
199         fi
200         #avarice --mkII -j usb --erase --program --verify --file images/triface.elf
201
202 .PHONY: flash_$(1)_local
203 flash_$(1)_local:
204
205 .PHONY: fuses_$(!)
206 fuses_$(1):
207         if [ ! -z "$$($(1)_efuse)" ] ; then \
208                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; then \
209                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; \
210                 fi \
211         fi
212         if [ ! -z "$$($(1)_hfuse)" ] ; then \
213                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; then \
214                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; \
215                 fi \
216         fi
217         if [ ! -z "$$($(1)_lfuse)" ] ; then \
218                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; then \
219                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; \
220                 fi \
221         fi
222         if [ ! -z "$$($(1)_lock)" ] ; then \
223                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; then \
224                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; \
225                 fi \
226         fi
227 endef
228
229 # Generate build rules for all targets
230 $(foreach t,$(TRG),$(eval $(call build_target,$(t))))
231
232
233 %.hex: %.elf
234         $(OBJCOPY) -O ihex $< $@
235
236 %.s19: %.elf
237         $(OBJCOPY) -O srec $< $@
238
239 %.bin: %.elf
240         $(OBJCOPY) -O binary $< $@
241
242 %.obj: %.elf
243         $(OBJCOPY) -O avrobj $< $@
244
245 %.rom: %.elf
246         $(OBJCOPY) -O $(FORMAT) $< $@
247 #       $(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" -O $(FORMAT) $< $(@:.rom=.eep)
248
249 %.cof: %.elf
250         $(COFFCONVERT) -O coff-ext-avr $< $@
251 #       $(COFFCONVERT) -O coff-avr $< $@   # For use with AVRstudio 3
252
253 #make instruction to delete created files
254 clean: clean-recursive
255         -$(RM_R) $(OBJDIR)
256         -$(RM_R) $(OUTDIR)
257
258 $(RECURSIVE_TARGETS):
259         @target=`echo $@ | sed s/-recursive//`; \
260         for dir in $(SUBDIRS); do \
261                 if [ -e $$dir/configure.in ] || [ -e $$dir/configure.ac ] && [ ! -x $$dir/configure ]; then \
262                         echo "Running autogen.sh in $$dir..."; \
263                         ( cd $$dir && chmod a+x autogen.sh && ./autogen.sh && rm -f Makefile || exit 1 ); \
264                 fi; \
265                 if [ ! -e $$dir/Makefile ]; then \
266                         if [ -e "$$dir/build-$(ARCH)" ]; then \
267                                 echo "Running build script in $$dir..."; \
268                                 ( cd $$dir && chmod a+x build && ./build || exit 1 ); \
269                         else \
270                                 echo "Running configure in $$dir..."; \
271                                 ( cd $$dir && ./configure --prefix=$(PREFIX) || exit 1 ); \
272                         fi; \
273                 fi; \
274                 $(MAKE) -C $$dir $$target || exit 1; \
275         done
276
277 BUILDREV_H = buildrev.h
278
279 ifeq ($(shell [ -e verstag.c ] && echo yes),yes)
280 .PHONY: bumprev
281 bumprev:
282         @buildnr=0; \
283         if [ -f $(BUILDREV_H) ]; then \
284                 buildnr=`sed <"$(BUILDREV_H)" -n -e 's/#define VERS_BUILD \([0-9][0-9]*\)/\1/p'`; \
285         fi; \
286         buildnr=`expr $$buildnr + 1`; \
287         buildhost=`hostname`; \
288         echo "#define VERS_BUILD $$buildnr" >"$(BUILDREV_H)"; \
289         echo "#define VERS_HOST  \"$$buildhost\"" >>"$(BUILDREV_H)"; \
290         echo "Building revision $$buildnr"
291 else
292 .PHONY: bumprev
293 bumprev:
294
295 endif
296
297 # Include dependencies
298 ifneq ($(strip $(OBJ)),)
299 -include $(OBJ:%.o=%.d)
300 endif