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