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