Update preset.
[bertos.git] / bertos / rules.mk
1 #
2 # $Id$
3 # Copyright 2002,2003,2004,2005,2006 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: Bernie Innocenti <bernie@codewiz.org>
11 #
12
13 # Remove all default pattern rules
14 .SUFFIXES:
15
16 # Verbosity
17 ifeq ($(V),1)
18 # Verbose build
19 Q :=
20 L := @echo >/dev/null
21 else
22 # Quiet build
23 Q := @
24 L := @echo
25 endif
26
27 # Select Bourne Again SHell as default make shell
28 SHELL := bash
29
30 # Checker build
31 ifeq ($(C),1)
32 CC = $(CHECKER)
33 CFLAGS += -Wundef -D__x86_64__=1 -D__unix__=1 -D__linux__=1 -D__STDC_VERSION__=199901L
34 endif
35
36 # Initialize $(top_srcdir) with current directory, unless it was already initialized
37 top_srcdir ?= $(shell pwd)
38
39 # Virtual Product: based on target products may be different.
40 # e.g. Embedded target = hex, s19, bin
41 #      Hosted = exe
42 #      Library = lib.a
43 TRG_TGT = $(TRG:%=$(OUTDIR)/%.tgt)
44
45 RECURSIVE_TARGETS = all-recursive install-recursive clean-recursive
46
47 # The default target
48 .PHONY: all
49 all:: all-recursive $(TRG_TGT)
50
51 # Generate project documentation
52 .PHONY: docs
53 docs:
54         $L "Building documentation"
55         $Q $(DOXYGEN)
56
57 # Generate ctags
58 .PHONY: tags
59 tags:
60         $L "Rebuilding C tags database"
61         $Q ctags -R --exclude=doc
62
63 # Run testsuite
64 .PHONY: check
65 check:
66         $L "Running testsuite"
67         $Q test/run_tests.sh
68
69 define build_target
70
71 ifeq ($$($(1)_HOSTED),1)
72         #On Darwin architecture the assembly doesn't link correctly if these flags are set.
73         ifeq ($(shell uname | grep -c "Darwin"),1)
74                 LIST_FLAGS := ""
75                 MAP_FLAGS := ""
76                 LDFLAGS := ""
77         endif
78         #use hosted specific map flags
79         $(1)_MAP_FLAGS = $$(MAP_FLAGS_HOST)
80
81         #Handle library creation
82         ifeq ($$($(1)_MAKELIB),1)
83                 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1).a
84         else
85                 #Otherwise in  hosted application we need only executable file.
86                 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1)
87         endif
88 else
89         #use embedded specific map flags
90         $(1)_MAP_FLAGS = $$(MAP_FLAGS_EMB)
91         #In embedded we need s19, hex and bin
92         $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1).s19 $$(OUTDIR)/$(1).hex $$(OUTDIR)/$(1).bin
93 endif
94
95 $(1)_LDFLAGS += $$($(1)_MAP_FLAGS)
96
97 # In embedded systems the target CPU is needed,
98 # but there are different options on how to pass
99 # it to the compiler.
100 ifneq ($$(strip $$($(1)_MCU)),)
101         $(1)_MCPU = -mmcu=$$($(1)_MCU)
102 endif
103 ifneq ($$(strip $$($(1)_CPU)),)
104         $(1)_MCPU = -mcpu=$$($(1)_CPU)
105 endif
106
107 # If a CPU is specified add to
108 # project specific flags.
109 ifneq ($$($(1)_MCPU),)
110         $(1)_CFLAGS    += $$($(1)_MCPU)
111         $(1)_CXXFLAGS  += $$($(1)_MCPU)
112         $(1)_ASFLAGS   += $$($(1)_MCPU)
113         $(1)_CPPAFLAGS += $$($(1)_MCPU)
114         $(1)_LDFLAGS   += $$($(1)_MCPU)
115 endif
116
117 ifneq ($$(strip $$($(1)_LDSCRIPT)),)
118         $(1)_LDFLAGS += -Wl,-T$$($(1)_LDSCRIPT)
119 endif
120
121 ifneq ($$($(1)_CROSS),)
122         #deprecated: use PREFIX, SUFFIX and HOSTED mechanism instead
123         $(1)_PREFIX = $$($(1)_CROSS)
124         $(1)_SUFFIX =
125 endif
126
127 $(1)_CC      ?= $$($(1)_PREFIX)$$(CC)$$($(1)_SUFFIX)
128 $(1)_CXX     ?= $$($(1)_PREFIX)$$(CXX)$$($(1)_SUFFIX)
129 $(1)_AS      ?= $$($(1)_PREFIX)$$(AS)$$($(1)_SUFFIX)
130 $(1)_AR      ?= $$($(1)_PREFIX)$$(AR)$$($(1)_SUFFIX)
131 $(1)_OBJCOPY ?= $$($(1)_PREFIX)$$(OBJCOPY)$$($(1)_SUFFIX)
132 $(1)_STRIP   ?= $$($(1)_PREFIX)$$(STRIP)$$($(1)_SUFFIX)
133 ifneq ($$(strip $$($(1)_CXXSRC)),)
134         $(1)_LD = $$($(1)_PREFIX)$$(LDXX)$$($(1)_SUFFIX)
135 else
136         $(1)_LD = $$($(1)_PREFIX)$$(LD)$$($(1)_SUFFIX)
137 endif
138
139 # Debug stuff
140 ifeq ($$($(1)_DEBUG),1)
141         # AVR is an harvard processor
142         # and needs debug module
143         # to be compiled in program memory
144         ifeq ($$(findstring avr, $$($(1)_PREFIX)),avr)
145                 $(1)_DEBUGSRC = $(1)_PCSRC
146         else
147                 $(1)_DEBUGSRC = $(1)_CSRC
148         endif
149
150         $$($(1)_DEBUGSRC) += bertos/drv/kdebug.c
151
152         # Also add formatwr.c (printf) if not already present
153         ifneq ($$(findstring formatwr.c, $$($$($(1)_DEBUGSRC))),formatwr.c)
154                 $$($(1)_DEBUGSRC) += bertos/mware/formatwr.c
155         endif
156
157         # Also add hex.c (for printf) if not already present
158         ifneq ($$(findstring hex.c, $$($$($(1)_CSRC))),hex.c)
159                 $$($(1)_CSRC) += bertos/mware/hex.c
160         endif
161
162         $(1)_CFLAGS += -D_DEBUG
163         $(1)_CXXFLAGS += -D_DEBUG
164 else
165         $(1)_CFLAGS += -fomit-frame-pointer
166         $(1)_CXXFLAGS += -fomit-frame-pointer
167 endif
168
169 $(1)_COBJ    = $$(foreach file,$$($(1)_CSRC:%.c=%.o),$$(OBJDIR)/$(1)/$$(file))
170 $(1)_CXXOBJ  = $$(foreach file,$$($(1)_CXXSRC:%.cpp=%.o),$$(OBJDIR)/$(1)/$$(file))
171 $(1)_PCOBJ   = $$(foreach file,$$($(1)_PCSRC:%.c=%_P.o),$$(OBJDIR)/$(1)/$$(file))
172 $(1)_AOBJ    = $$(foreach file,$$($(1)_ASRC:%.s=%.o),$$(OBJDIR)/$(1)/$$(file))
173 $(1)_CPPAOBJ = $$(foreach file,$$($(1)_CPPASRC:%.S=%.o),$$(OBJDIR)/$(1)/$$(file))
174 $(1)_OBJ    := $$($(1)_COBJ) $$($(1)_CXXOBJ) $$($(1)_PCOBJ) $$($(1)_AOBJ) $$($(1)_CPPAOBJ)
175 $(1)_SRC    := $$($(1)_CSRC) $$($(1)_CXXSRC) $$($(1)_PCSRC) $$($(1)_ASRC) $$($(1)_CPPASRC)
176 OBJ         += $$($(1)_OBJ)
177
178 # Compile: instructions to create assembler and/or object files from C source
179 $$($(1)_COBJ) : $$(OBJDIR)/$(1)/%.o : %.c
180         $L "$(1): Compiling $$< (C)"
181         @$$(MKDIR_P) $$(dir $$@)
182         $Q $$($(1)_CC) -c $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$($$(*F)_CFLAGS) $$< -o $$@
183
184 # Compile: instructions to create assembler and/or object files from C++ source
185 $$($(1)_CXXOBJ) : $$(OBJDIR)/$(1)/%.o : %.cpp
186         $L "$(1): Compiling $$< (C++)"
187         @$$(MKDIR_P) $$(dir $$@)
188         $Q $$($(1)_CXX) -c $$(CXXFLAGS) $$($(1)_CXXFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$($$(*F)_CXXFLAGS) $$< -o $$@
189
190 # Generate assembly sources from C files (debug)
191 $$(OBJDIR)/$(1)/%.s : %.c
192         $L "$(1): Generating asm source $$<"
193         @$$(MKDIR_P) $$(dir $$@)
194         $Q $$($(1)_CC) -S $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$($$(*F)_CFLAGS) $$< -o $$@
195
196 # Generate special progmem variant of a source file
197 $$($(1)_PCOBJ) : $$(OBJDIR)/$(1)/%_P.o : %.c
198         $L "$(1): Compiling $$< (PROGMEM)"
199         @$$(MKDIR_P) $$(dir $$@)
200         $Q $$($(1)_CC) -c -D_PROGMEM $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$($$(*F)_CFLAGS) $$< -o $$@
201
202 # Assemble: instructions to create object file from assembler files
203 $$($(1)_AOBJ): $$(OBJDIR)/$(1)/%.o : %.s
204         $L "$(1): Assembling $$<"
205         @$$(MKDIR_P) $$(dir $$@)
206         $Q $$($(1)_AS) -c $$(ASFLAGS) $$($(1)_ASFLAGS) $$($$(*F)_ASFLAGS) $$< -o $$@
207
208 $$($(1)_CPPAOBJ): $$(OBJDIR)/$(1)/%.o : %.S
209         $L "$(1): Assembling with CPP $$<"
210         @$$(MKDIR_P) $$(dir $$@)
211         $Q $$($(1)_CC) -c $$(CPPAFLAGS) $$($(1)_CPPAFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$($$(*F)_CPPAFLAGS) $$< -o $$@
212
213
214 # Link: instructions to create elf output file from object files
215 $$(OUTDIR)/$(1).elf $$(OUTDIR)/$(1)_nostrip: bumprev $$($(1)_OBJ) $$($(1)_LDSCRIPT)
216         $L "$(1): Linking $$@"
217         @$$(MKDIR_P) $$(dir $$@)
218         $Q $$($(1)_LD) $$($(1)_OBJ) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
219
220
221 # Instructions to create a static library from object files
222 $$(OUTDIR)/$(1).a: bumprev $$($(1)_OBJ)
223         $L "$(1): Creating static library $$@"
224         @$$(MKDIR_P) $$(dir $$@)
225         $Q $$($(1)_AR) $$(ARFLAGS) $$($(1)_ARFLAGS) $$@ $$($(1)_OBJ)
226
227 # Strip debug info
228 $$(OUTDIR)/$(1): $$(OUTDIR)/$(1)_nostrip
229         $L "$(1): Generating stripped executable $$@"
230         $Q $$($(1)_STRIP) -o $$@ $$^
231
232 # Compile and link (program-at-a-time)
233 $$(OUTDIR)/$(1)_whole.elf: bumprev $$($(1)_SRC) $$($(1)_LDSCRIPT)
234         $L "$(1): Compiling and Linking whole program $$@"
235         @$$(MKDIR_P) $$(dir $$@)
236         $Q $$($(1)_CC) $$($(1)_SRC) $$(CFLAGS) $$($(1)_CFLAGS) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
237
238 # Flash target
239 .PHONY: flash_$(1)
240 flash_$(1): $(OUTDIR)/$(1).hex flash_$(1)_local
241         $L "$(1): Flashing target"
242         $Q if [ ! -f $$($(1)_FLASH_SCRIPT) ] ; then \
243                 printf "CLDLG: No flash script found.\n" ; \
244                 exit 1 ; \
245         fi
246         $Q if [ ! "$$($(1)_PROGRAMMER_TYPE)" == "none" ] ; then \
247                 PROGRAMMER_CPU=$$($(1)_PROGRAMMER_CPU) PROGRAMMER_TYPE=$$($(1)_PROGRAMMER_TYPE) \
248                 PROGRAMMER_PORT=$$($(1)_PROGRAMMER_PORT) IMAGE_FILE=$$< \
249                 $$($(1)_FLASH_SCRIPT) ; \
250         else \
251                 printf "CLDLG: No programmer interface configured, see http://dev.bertos.org/wiki/ProgrammerInterface\n" ; \
252                 exit 1 ; \
253         fi 
254
255 .PHONY: flash_$(1)_local
256 flash_$(1)_local:
257
258 .PHONY: stopflash_$(1)
259 stopflash_$(1): 
260         $L "$(1): Stopping target flashing"
261         $Q if [ ! -f $$($(1)_STOPFLASH_SCRIPT) ] ; then \
262                 printf "CLDLG: No stopflash script found.\n" ; \
263                 exit 1 ; \
264         fi
265         $Q $$($(1)_STOPFLASH_SCRIPT) ;
266
267
268 # Debug target
269 .PHONY: debug_$(1)
270 debug_$(1): $(OUTDIR)/$(1).elf
271         $L "$(1): Debugging target"
272         $Q if [ ! -f $$($(1)_DEBUG_SCRIPT) ] ; then \
273                 printf "CLDLG: No debug script found.\n" ; \
274                 exit 1 ; \
275         fi
276         $Q if [ ! "$$($(1)_PROGRAMMER_TYPE)" == "none" ] ; then \
277                 PROGRAMMER_CPU=$$($(1)_PROGRAMMER_CPU) PROGRAMMER_TYPE=$$($(1)_PROGRAMMER_TYPE) \
278                 PROGRAMMER_PORT=$$($(1)_PROGRAMMER_PORT) GDB_PORT=3333 \
279                 ELF_FILE=$$< \
280                 $$($(1)_DEBUG_SCRIPT) ; \
281         else \
282                 printf "CLDLG: No programmer interface configured, see http://dev.bertos.org/wiki/ProgrammerInterface\n" ; \
283                 exit 1 ; \
284         fi
285
286 .PHONY: stopdebug_$(1)
287 stopdebug_$(1): 
288         $L "$(1): Stopping debugger"
289         $Q if [ ! -f $$($(1)_STOPDEBUG_SCRIPT) ] ; then \
290                 printf "CLDLG: No stopdebug script found.\n" ; \
291                 exit 1 ; \
292         fi
293         $Q $$($(1)_STOPDEBUG_SCRIPT) ;
294
295 .PHONY: fuses_$(!)
296 fuses_$(1):
297         if [ ! -z "$$($(1)_efuse)" ] ; then \
298                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; then \
299                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; \
300                 fi \
301         fi
302         if [ ! -z "$$($(1)_hfuse)" ] ; then \
303                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; then \
304                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; \
305                 fi \
306         fi
307         if [ ! -z "$$($(1)_lfuse)" ] ; then \
308                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; then \
309                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; \
310                 fi \
311         fi
312         if [ ! -z "$$($(1)_lock)" ] ; then \
313                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; then \
314                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; \
315                 fi \
316         fi
317
318 $$(OUTDIR)/$(1).hex: $$(OUTDIR)/$(1).elf
319         $$($(1)_OBJCOPY) -O ihex $$< $$@
320
321 $$(OUTDIR)/$(1).s19: $$(OUTDIR)/$(1).elf
322         $$($(1)_OBJCOPY) -O srec $$< $$@
323
324 $$(OUTDIR)/$(1).bin: $$(OUTDIR)/$(1).elf
325         $$($(1)_OBJCOPY) -O binary $$< $$@
326
327 $$(OUTDIR)/$(1).obj: $$(OUTDIR)/$(1).elf
328         $$($(1)_OBJCOPY) -O avrobj $$< $$@
329
330 $$(OUTDIR)/$(1).rom: $$(OUTDIR)/$(1).elf
331         $$($(1)_OBJCOPY) -O $$(FORMAT) $$< $$@
332 #       $$($(1)_OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" -O $$(FORMAT) $$< $$(@:.rom=.eep)
333
334 endef
335
336 # Generate build rules for all targets
337 $(foreach t,$(TRG),$(eval $(call build_target,$(t))))
338
339 # Generate Qt's moc files from headers
340 # NOTE: moc totally sucks and can generate empty files for some error conditions,
341 #       leading to puzzling linker errors.  Kill 'em and abort build.
342 %_moc.cpp: %.h
343         $(QT_MOC) -o $@ $<
344         if [ ! -s $< ]; then \
345                 rm -f $@; \
346                 exit 1; \
347         fi
348
349
350 %.cof: %.elf
351         $(COFFCONVERT) -O coff-ext-avr $< $@
352 #       $(COFFCONVERT) -O coff-avr $< $@   # For use with AVRstudio 3
353
354 #make instruction to delete created files
355 cleanall: clean
356 clean: clean-recursive
357         -$(RM_R) $(OBJDIR)
358         -$(RM_R) $(OUTDIR)
359
360 $(RECURSIVE_TARGETS):
361         @target=`echo $@ | sed s/-recursive//`; \
362         for dir in $(SUBDIRS); do \
363                 if [ -e $$dir/configure.in ] || [ -e $$dir/configure.ac ] && [ ! -x $$dir/configure ]; then \
364                         echo "Running autogen.sh in $$dir..."; \
365                         ( cd $$dir && chmod a+x autogen.sh && ./autogen.sh && rm -f Makefile || exit 1 ); \
366                 fi; \
367                 if [ ! -e $$dir/Makefile ]; then \
368                         if [ -e "$$dir/build-$(ARCH)" ]; then \
369                                 echo "Running build script in $$dir..."; \
370                                 ( cd $$dir && chmod a+x build && ./build || exit 1 ); \
371                         else \
372                                 echo "Running configure in $$dir..."; \
373                                 ( cd $$dir && ./configure --prefix=$(PREFIX) || exit 1 ); \
374                         fi; \
375                 fi; \
376                 $(MAKE) -C $$dir $$target || exit 1; \
377         done
378
379 BUILDREV_H = buildrev.h
380
381 .PHONY: bumprev
382 bumprev:
383         @if [ -e bertos/verstag.c ]; then \
384                 buildnr=0; \
385                 if [ -f $(BUILDREV_H) ]; then \
386                         buildnr=`sed <"$(BUILDREV_H)" -n -e 's/#define VERS_BUILD \([0-9][0-9]*\)/\1/p'`; \
387                 fi; \
388                 buildnr=`expr $$buildnr + 1`; \
389                 buildhost=`hostname | sed -n -e '1h;2,$$H;$${g;s/\n//g;p;}'`; \
390                 echo "#define VERS_BUILD $$buildnr" >"$(BUILDREV_H)"; \
391                 echo "#define VERS_HOST  \"$$buildhost\"" >>"$(BUILDREV_H)"; \
392                 echo "Building revision $$buildnr"; \
393         fi; \
394         #
395
396 # Include dependencies
397 ifneq ($(strip $(OBJ)),)
398 -include $(OBJ:%.o=%.d)
399 endif