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