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