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