Remove libunittest: this will be handled by the run_tests script.
[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 ifneq ($$($(1)_CROSS),)
72         #use embedded specific map flags
73         $(1)_MAP_FLAGS = $$(MAP_FLAGS_EMB)
74         #In embedded we need s19, hex and bin
75         $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1).s19 $$(OUTDIR)/$(1).hex $$(OUTDIR)/$(1).bin
76 else
77         #On Darwin architecture the assembly doesn't link correctly if these flags are set.
78         ifeq ($(shell uname | grep -c "Darwin"),1)
79                 LIST_FLAGS := ""
80                 MAP_FLAGS := ""
81                 LDFLAGS := ""
82         endif
83         #use hosted specific map flags
84         $(1)_MAP_FLAGS = $$(MAP_FLAGS_HOST)
85
86         #Handle library creation
87         ifeq ($$($(1)_MAKELIB),1)
88                 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1).a
89         else
90                 #Otherwise in  hosted application we need only executable file.
91                 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1)
92         endif
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 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 # Debug stuff
122 ifeq ($$($(1)_DEBUG),1)
123         # AVR is an harvard processor
124         # and needs debug module
125         # to be compiled in program memory
126         ifeq ($$(findstring avr, $$($(1)_CROSS)),avr)
127                 $(1)_DEBUGSRC = $(1)_PCSRC
128         else
129                 $(1)_DEBUGSRC = $(1)_CSRC
130         endif
131
132         $$($(1)_DEBUGSRC) += bertos/drv/kdebug.c
133
134         # Also add formatwr.c (printf) if not already present
135         ifneq ($$(findstring formatwr.c, $$($$($(1)_DEBUGSRC))),formatwr.c)
136                 $$($(1)_DEBUGSRC) += bertos/mware/formatwr.c
137         endif
138
139         $(1)_CFLAGS += -D_DEBUG 
140         $(1)_CXXFLAGS += -D_DEBUG
141 endif
142
143 $(1)_CC      ?= $$($(1)_CROSS)$$(CC)
144 $(1)_CXX     ?= $$($(1)_CROSS)$$(CXX)
145 $(1)_AS      ?= $$($(1)_CROSS)$$(AS)
146 $(1)_AR      ?= $$($(1)_CROSS)$$(AR)
147 $(1)_OBJCOPY ?= $$($(1)_CROSS)$$(OBJCOPY)
148 $(1)_STRIP   ?= $$($(1)_CROSS)$$(STRIP)
149
150 $(1)_COBJ    = $$(foreach file,$$($(1)_CSRC:%.c=%.o),$$(OBJDIR)/$(1)/$$(file))
151 $(1)_CXXOBJ  = $$(foreach file,$$($(1)_CXXSRC:%.cpp=%.o),$$(OBJDIR)/$(1)/$$(file))
152 $(1)_PCOBJ   = $$(foreach file,$$($(1)_PCSRC:%.c=%_P.o),$$(OBJDIR)/$(1)/$$(file))
153 $(1)_AOBJ    = $$(foreach file,$$($(1)_ASRC:%.s=%.o),$$(OBJDIR)/$(1)/$$(file))
154 $(1)_CPPAOBJ = $$(foreach file,$$($(1)_CPPASRC:%.S=%.o),$$(OBJDIR)/$(1)/$$(file))
155 $(1)_OBJ    := $$($(1)_COBJ) $$($(1)_CXXOBJ) $$($(1)_PCOBJ) $$($(1)_AOBJ) $$($(1)_CPPAOBJ)
156 $(1)_SRC    := $$($(1)_CSRC) $$($(1)_CXXSRC) $$($(1)_PCSRC) $$($(1)_ASRC) $$($(1)_CPPASRC)
157 OBJ         += $$($(1)_OBJ)
158
159 ifneq ($$(strip $$($(1)_CXXSRC)),)
160 $(1)_LD = $$($(1)_CROSS)$$(LDXX)
161 else
162 $(1)_LD = $$($(1)_CROSS)$$(LD)
163 endif
164
165 # Sometimes $(CC) is actually set to a C++ compiler in disguise, and it
166 # would whine if we passed it C-only flags.  Checking for the presence of
167 # "++" in the name is a kludge that seems to work mostly.
168 ifeq (++,$$(findstring ++,$$($(1)_CC)))
169         $(1)_REAL_CFLAGS = $$(CXXFLAGS)
170 else
171         $(1)_REAL_CFLAGS = $$(CFLAGS)
172 endif
173
174 # Compile: instructions to create assembler and/or object files from C source
175 $$($(1)_COBJ) : $$(OBJDIR)/$(1)/%.o : %.c
176         $L "$(1): Compiling $$< (C)"
177         @$$(MKDIR_P) $$(dir $$@)
178         $Q $$($(1)_CC) -c $$($(1)_REAL_CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
179
180 # Compile: instructions to create assembler and/or object files from C++ source
181 $$($(1)_CXXOBJ) : $$(OBJDIR)/$(1)/%.o : %.cpp
182         $L "$(1): Compiling $$< (C++)"
183         @$$(MKDIR_P) $$(dir $$@)
184         $Q $$($(1)_CXX) -c $$(CXXFLAGS) $$($(1)_CXXFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
185
186 # Generate assembly sources from C files (debug)
187 $$(OBJDIR)/$(1)/%.s : %.c
188         $L "$(1): Generating asm source $$<"
189         @$$(MKDIR_P) $$(dir $$@)
190         $Q $$($(1)_CC) -S $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$< -o $$@
191
192 # Generate special progmem variant of a source file
193 $$($(1)_PCOBJ) : $$(OBJDIR)/$(1)/%_P.o : %.c
194         $L "$(1): Compiling $$< (PROGMEM)"
195         @$$(MKDIR_P) $$(dir $$@)
196         $Q $$($(1)_CC) -c -D_PROGMEM $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
197
198 # Assemble: instructions to create object file from assembler files
199 $$($(1)_AOBJ): $$(OBJDIR)/$(1)/%.o : %.s
200         $L "$(1): Assembling $$<"
201         @$$(MKDIR_P) $$(dir $$@)
202         $Q $$($(1)_AS) -c $$(ASFLAGS) $$($(1)_ASFLAGS) $$< -o $$@
203
204 $$($(1)_CPPAOBJ): $$(OBJDIR)/$(1)/%.o : %.S
205         $L "$(1): Assembling with CPP $$<"
206         @$$(MKDIR_P) $$(dir $$@)
207         $Q $$($(1)_CC) -c $$(CPPAFLAGS) $$($(1)_CPPAFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
208
209
210 # Link: instructions to create elf output file from object files
211 $$(OUTDIR)/$(1).elf $$(OUTDIR)/$(1)_nostrip: bumprev $$($(1)_OBJ) $$($(1)_LDSCRIPT)
212         $L "$(1): Linking $$@"
213         @$$(MKDIR_P) $$(dir $$@)
214         $Q $$($(1)_LD) $$($(1)_OBJ) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
215
216
217 # Instructions to create a static library from object files
218 $$(OUTDIR)/$(1).a: bumprev $$($(1)_OBJ)
219         $L "$(1): Creating static library $$@"
220         @$$(MKDIR_P) $$(dir $$@)
221         $Q $$($(1)_AR) $$(ARFLAGS) $$($(1)_ARFLAGS) $$@ $$($(1)_OBJ)
222
223 # Strip debug info
224 $$(OUTDIR)/$(1): $$(OUTDIR)/$(1)_nostrip
225         $L "$(1): Generating stripped executable $$@"
226         $Q $$($(1)_STRIP) -o $$@ $$^
227
228 # Compile and link (program-at-a-time)
229 $$(OUTDIR)/$(1)_whole.elf: bumprev $$($(1)_SRC) $$($(1)_LDSCRIPT)
230         $L "$(1): Compiling and Linking whole program $$@"
231         @$$(MKDIR_P) $$(dir $$@)
232         $Q $$($(1)_CC) $$($(1)_SRC) $$(CFLAGS) $$($(1)_CFLAGS) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
233
234 # Flash target
235 # NOTE: we retry in case of failure because the STK500 programmer is crappy
236 .PHONY: flash_$(1)
237 flash_$(1): $(OUTDIR)/$(1).s19 flash_$(1)_local
238         if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; then \
239              $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; \
240         fi
241         #avarice --mkII -j usb --erase --program --verify --file images/triface.elf
242
243 .PHONY: flash_$(1)_local
244 flash_$(1)_local:
245
246 .PHONY: fuses_$(!)
247 fuses_$(1):
248         if [ ! -z "$$($(1)_efuse)" ] ; then \
249                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; then \
250                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; \
251                 fi \
252         fi
253         if [ ! -z "$$($(1)_hfuse)" ] ; then \
254                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; then \
255                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; \
256                 fi \
257         fi
258         if [ ! -z "$$($(1)_lfuse)" ] ; then \
259                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; then \
260                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; \
261                 fi \
262         fi
263         if [ ! -z "$$($(1)_lock)" ] ; then \
264                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; then \
265                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; \
266                 fi \
267         fi
268
269 $$(OUTDIR)/$(1).hex: $$(OUTDIR)/$(1).elf
270         $$($(1)_OBJCOPY) -O ihex $$< $$@
271
272 $$(OUTDIR)/$(1).s19: $$(OUTDIR)/$(1).elf
273         $$($(1)_OBJCOPY) -O srec $$< $$@
274
275 $$(OUTDIR)/$(1).bin: $$(OUTDIR)/$(1).elf
276         $$($(1)_OBJCOPY) -O binary $$< $$@
277
278 $$(OUTDIR)/$(1).obj: $$(OUTDIR)/$(1).elf
279         $$($(1)_OBJCOPY) -O avrobj $$< $$@
280
281 $$(OUTDIR)/$(1).rom: $$(OUTDIR)/$(1).elf
282         $$($(1)_OBJCOPY) -O $$(FORMAT) $$< $$@
283 #       $$($(1)_OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" -O $$(FORMAT) $$< $$(@:.rom=.eep)
284
285 endef
286
287 # Generate build rules for all targets
288 $(foreach t,$(TRG),$(eval $(call build_target,$(t))))
289
290 # Generate Qt's moc files from headers
291 # NOTE: moc totally sucks and can generate empty files for some error conditions,
292 #       leading to puzzling linker errors.  Kill 'em and abort build.
293 %_moc.cpp: %.h
294         $(QT_MOC) -o $@ $<
295         if [ ! -s $< ]; then \
296                 rm -f $@; \
297                 exit 1; \
298         fi
299
300
301 %.cof: %.elf
302         $(COFFCONVERT) -O coff-ext-avr $< $@
303 #       $(COFFCONVERT) -O coff-avr $< $@   # For use with AVRstudio 3
304
305 #make instruction to delete created files
306 cleanall: clean
307 clean: clean-recursive
308         -$(RM_R) $(OBJDIR)
309         -$(RM_R) $(OUTDIR)
310
311 $(RECURSIVE_TARGETS):
312         @target=`echo $@ | sed s/-recursive//`; \
313         for dir in $(SUBDIRS); do \
314                 if [ -e $$dir/configure.in ] || [ -e $$dir/configure.ac ] && [ ! -x $$dir/configure ]; then \
315                         echo "Running autogen.sh in $$dir..."; \
316                         ( cd $$dir && chmod a+x autogen.sh && ./autogen.sh && rm -f Makefile || exit 1 ); \
317                 fi; \
318                 if [ ! -e $$dir/Makefile ]; then \
319                         if [ -e "$$dir/build-$(ARCH)" ]; then \
320                                 echo "Running build script in $$dir..."; \
321                                 ( cd $$dir && chmod a+x build && ./build || exit 1 ); \
322                         else \
323                                 echo "Running configure in $$dir..."; \
324                                 ( cd $$dir && ./configure --prefix=$(PREFIX) || exit 1 ); \
325                         fi; \
326                 fi; \
327                 $(MAKE) -C $$dir $$target || exit 1; \
328         done
329
330 BUILDREV_H = buildrev.h
331
332 .PHONY: bumprev
333 bumprev:
334         @if [ -e bertos/verstag.c ]; then \
335                 buildnr=0; \
336                 if [ -f $(BUILDREV_H) ]; then \
337                         buildnr=`sed <"$(BUILDREV_H)" -n -e 's/#define VERS_BUILD \([0-9][0-9]*\)/\1/p'`; \
338                 fi; \
339                 buildnr=`expr $$buildnr + 1`; \
340                 buildhost=`hostname | sed -n -e '1h;2,$$H;$${g;s/\n//g;p;}'`; \
341                 echo "#define VERS_BUILD $$buildnr" >"$(BUILDREV_H)"; \
342                 echo "#define VERS_HOST  \"$$buildhost\"" >>"$(BUILDREV_H)"; \
343                 echo "Building revision $$buildnr"; \
344         fi; \
345         #
346
347 # Include dependencies
348 ifneq ($(strip $(OBJ)),)
349 -include $(OBJ:%.o=%.d)
350 endif