Refactor how cpu flags are added.
[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 TRG_TGT = $(TRG:%=$(OUTDIR)/%.tgt)
43
44 RECURSIVE_TARGETS = all-recursive install-recursive clean-recursive
45
46 # The default target
47 .PHONY: all
48 all:: all-recursive $(TRG_TGT)
49
50 # Generate project documentation
51 .PHONY: docs
52 docs:
53         $L "Building documentation"
54         $Q $(DOXYGEN)
55
56 # Generate ctags
57 .PHONY: tags
58 tags:
59         $L "Rebuilding C tags database"
60         $Q ctags -R --exclude=doc
61
62 # Run testsuite
63 .PHONY: check
64 check:
65         $L "Running testsuite"
66         $Q ./run_tests.sh
67
68 define build_target
69
70 ifneq ($$($(1)_CROSS),)
71 #use embedded specific map flags
72 $(1)_MAP_FLAGS = $$(MAP_FLAGS_EMB)
73 #In embedded we need s19, hex and bin
74 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1).s19 $$(OUTDIR)/$(1).hex $$(OUTDIR)/$(1).bin
75 else
76 #On Darwin architecture the assembly doesn't link correctly if this flag is set.
77 ifeq ($(shell uname | grep -c "Darwin"),1)
78 LIST_FLAGS := ""
79 MAP_FLAGS := ""
80 LDFLAGS := ""
81 endif
82 #use hosted specific map flags
83 $(1)_MAP_FLAGS = $$(MAP_FLAGS_HOST)
84 #in hosted application we need only executable file.
85 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1)
86 endif
87
88 $(1)_LDFLAGS += $$($(1)_MAP_FLAGS)
89
90 # In embedded systems the target CPU is needed,
91 # but there are different options on how pass
92 # it to the compiler.
93 ifneq ($$(strip $$($(1)_MCU)),)
94 $(1)_MCPU = -mmcu=$$($(1)_MCU)
95 endif
96 ifneq ($$(strip $$($(1)_CPU)),)
97 $(1)_MCPU = -mcpu=$$($(1)_CPU)
98 endif
99
100 # If a CPU is specified add to 
101 # project specific flags.
102 ifneq ($$($(1)_MCPU),)
103 $(1)_CFLAGS    += $$($(1)_MCPU)
104 $(1)_CXXFLAGS  += $$($(1)_MCPU)
105 $(1)_ASFLAGS   += $$($(1)_MCPU)
106 $(1)_CPPAFLAGS += $$($(1)_MCPU)
107 $(1)_LDFLAGS   += $$($(1)_MCPU)
108 endif
109
110 ifneq ($$(strip $$($(1)_LDSCRIPT)),)
111 $(1)_LDFLAGS += -Wl,-T$$($(1)_LDSCRIPT)
112 endif
113
114 $(1)_CC      = $$($(1)_CROSS)$$(CC)
115 $(1)_CXX     = $$($(1)_CROSS)$$(CXX)
116 $(1)_AS      = $$($(1)_CROSS)$$(AS)
117 $(1)_OBJCOPY = $$($(1)_CROSS)$$(OBJCOPY)
118 $(1)_STRIP   = $$($(1)_CROSS)$$(STRIP)
119
120 $(1)_COBJ    = $$(foreach file,$$($(1)_CSRC:%.c=%.o),$$(OBJDIR)/$(1)/$$(file))
121 $(1)_CXXOBJ  = $$(foreach file,$$($(1)_CXXSRC:%.cpp=%.o),$$(OBJDIR)/$(1)/$$(file))
122 $(1)_PCOBJ   = $$(foreach file,$$($(1)_PCSRC:%.c=%_P.o),$$(OBJDIR)/$(1)/$$(file))
123 $(1)_AOBJ    = $$(foreach file,$$($(1)_ASRC:%.s=%.o),$$(OBJDIR)/$(1)/$$(file))
124 $(1)_CPPAOBJ = $$(foreach file,$$($(1)_CPPASRC:%.S=%.o),$$(OBJDIR)/$(1)/$$(file))
125 $(1)_OBJ    := $$($(1)_COBJ) $$($(1)_CXXOBJ) $$($(1)_PCOBJ) $$($(1)_AOBJ) $$($(1)_CPPAOBJ)
126 $(1)_SRC    := $$($(1)_CSRC) $$($(1)_CXXSRC) $$($(1)_PCSRC) $$($(1)_ASRC) $$($(1)_CPPASRC)
127 OBJ         += $$($(1)_OBJ)
128
129 ifneq ($$(strip $$($(1)_CXXSRC)),)
130 $(1)_LD = $$($(1)_CROSS)$$(LDXX)
131 else
132 $(1)_LD = $$($(1)_CROSS)$$(LD)
133 endif
134
135 # Sometimes $(CC) is actually set to a C++ compiler in disguise, and it
136 # would whine if we passed it C-only flags.  Checking for the presence of
137 # "++" in the name is a kludge that seems to work mostly.
138 ifeq (++,$$(findstring ++,$$($(1)_CC)))
139         REAL_CFLAGS = $$(CXXFLAGS)
140 else
141         REAL_CFLAGS = $$(CFLAGS)
142 endif
143
144 # Compile: instructions to create assembler and/or object files from C source
145 $$($(1)_COBJ) : $$(OBJDIR)/$(1)/%.o : %.c
146         $L "$(1): Compiling $$< (C)"
147         @$$(MKDIR_P) $$(dir $$@)
148         $Q $$($(1)_CC) -c $$(REAL_CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
149
150 # Compile: instructions to create assembler and/or object files from C++ source
151 $$($(1)_CXXOBJ) : $$(OBJDIR)/$(1)/%.o : %.cpp
152         $L "$(1): Compiling $$< (C++)"
153         @$$(MKDIR_P) $$(dir $$@)
154         $Q $$($(1)_CXX) -c $$(CXXFLAGS) $$($(1)_CXXFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
155
156 # Generate assembly sources from C files (debug)
157 $$(OBJDIR)/$(1)/%.s : %.c
158         $L "$(1): Generating asm source $$<"
159         @$$(MKDIR_P) $$(dir $$@)
160         $Q $$($(1)_CC) -S $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$< -o $$@
161
162 # Generate special progmem variant of a source file
163 $$($(1)_PCOBJ) : $$(OBJDIR)/$(1)/%_P.o : %.c
164         $L "$(1): Compiling $$< (PROGMEM)"
165         @$$(MKDIR_P) $$(dir $$@)
166         $Q $$($(1)_CC) -c -D_PROGMEM $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
167
168 # Assemble: instructions to create object file from assembler files
169 $$($(1)_AOBJ): $$(OBJDIR)/$(1)/%.o : %.s
170         $L "$(1): Assembling $$<"
171         @$$(MKDIR_P) $$(dir $$@)
172         $Q $$($(1)_AS) -c $$(ASFLAGS) $$($(1)_ASFLAGS) $$< -o $$@
173
174 $$($(1)_CPPAOBJ): $$(OBJDIR)/$(1)/%.o : %.S
175         $L "$(1): Assembling with CPP $$<"
176         @$$(MKDIR_P) $$(dir $$@)
177         $Q $$($(1)_CC) -c $$(CPPAFLAGS) $$($(1)_CPPAFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
178
179
180 # Link: instructions to create elf output file from object files
181 $$(OUTDIR)/$(1).elf $$(OUTDIR)/$(1)_nostrip: bumprev $$($(1)_OBJ) $$($(1)_LDSCRIPT)
182         $L "$(1): Linking $$@"
183         @$$(MKDIR_P) $$(dir $$@)
184         $Q $$($(1)_LD) $$($(1)_OBJ) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
185
186 # Strip debug info
187 $$(OUTDIR)/$(1): $$(OUTDIR)/$(1)_nostrip
188         $L "$(1): Generating stripped executable $$@"
189         $Q $$($(1)_STRIP) -o $$@ $$^
190
191 # Compile and link (program-at-a-time)
192 $$(OUTDIR)/$(1)_whole.elf: bumprev $$($(1)_SRC) $$($(1)_LDSCRIPT)
193         $L "$(1): Compiling and Linking whole program $$@"
194         @$$(MKDIR_P) $$(dir $$@)
195         $Q $$($(1)_CC) $$($(1)_SRC) $$(CFLAGS) $$($(1)_CFLAGS) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
196
197 # Flash target
198 # NOTE: we retry in case of failure because the STK500 programmer is crappy
199 .PHONY: flash_$(1)
200 flash_$(1): $(OUTDIR)/$(1).s19 flash_$(1)_local
201         if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; then \
202              $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; \
203         fi
204         #avarice --mkII -j usb --erase --program --verify --file images/triface.elf
205
206 .PHONY: flash_$(1)_local
207 flash_$(1)_local:
208
209 .PHONY: fuses_$(!)
210 fuses_$(1):
211         if [ ! -z "$$($(1)_efuse)" ] ; then \
212                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; then \
213                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; \
214                 fi \
215         fi
216         if [ ! -z "$$($(1)_hfuse)" ] ; then \
217                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; then \
218                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; \
219                 fi \
220         fi
221         if [ ! -z "$$($(1)_lfuse)" ] ; then \
222                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; then \
223                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; \
224                 fi \
225         fi
226         if [ ! -z "$$($(1)_lock)" ] ; then \
227                 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; then \
228                      $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; \
229                 fi \
230         fi
231
232 $$(OUTDIR)/$(1).hex: $$(OUTDIR)/$(1).elf
233         $$($(1)_OBJCOPY) -O ihex $$< $$@
234
235 $$(OUTDIR)/$(1).s19: $$(OUTDIR)/$(1).elf
236         $$($(1)_OBJCOPY) -O srec $$< $$@
237
238 $$(OUTDIR)/$(1).bin: $$(OUTDIR)/$(1).elf
239         $$($(1)_OBJCOPY) -O binary $$< $$@
240
241 $$(OUTDIR)/$(1).obj: $$(OUTDIR)/$(1).elf
242         $$($(1)_OBJCOPY) -O avrobj $$< $$@
243
244 $$(OUTDIR)/$(1).rom: $$(OUTDIR)/$(1).elf
245         $$($(1)_OBJCOPY) -O $$(FORMAT) $$< $$@
246 #       $$($(1)_OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" -O $$(FORMAT) $$< $$(@:.rom=.eep)
247
248 endef
249
250 # Generate build rules for all targets
251 $(foreach t,$(TRG),$(eval $(call build_target,$(t))))
252
253 # Generate Qt's moc files from headers
254 # NOTE: moc totally sucks and can generate empty files for some error conditions,
255 #       leading to puzzling linker errors.  Kill 'em and abort build.
256 %_moc.cpp: %.h
257         $(MOC) -o $@ $<
258         if [ ! -s $< ]; then \
259                 rm -f $@; \
260                 exit 1; \
261         fi
262
263
264 %.cof: %.elf
265         $(COFFCONVERT) -O coff-ext-avr $< $@
266 #       $(COFFCONVERT) -O coff-avr $< $@   # For use with AVRstudio 3
267
268 #make instruction to delete created files
269 cleanall: clean
270 clean: clean-recursive
271         -$(RM_R) $(OBJDIR)
272         -$(RM_R) $(OUTDIR)
273
274 $(RECURSIVE_TARGETS):
275         @target=`echo $@ | sed s/-recursive//`; \
276         for dir in $(SUBDIRS); do \
277                 if [ -e $$dir/configure.in ] || [ -e $$dir/configure.ac ] && [ ! -x $$dir/configure ]; then \
278                         echo "Running autogen.sh in $$dir..."; \
279                         ( cd $$dir && chmod a+x autogen.sh && ./autogen.sh && rm -f Makefile || exit 1 ); \
280                 fi; \
281                 if [ ! -e $$dir/Makefile ]; then \
282                         if [ -e "$$dir/build-$(ARCH)" ]; then \
283                                 echo "Running build script in $$dir..."; \
284                                 ( cd $$dir && chmod a+x build && ./build || exit 1 ); \
285                         else \
286                                 echo "Running configure in $$dir..."; \
287                                 ( cd $$dir && ./configure --prefix=$(PREFIX) || exit 1 ); \
288                         fi; \
289                 fi; \
290                 $(MAKE) -C $$dir $$target || exit 1; \
291         done
292
293 BUILDREV_H = buildrev.h
294
295 .PHONY: bumprev
296 bumprev:
297         @if [ -e bertos/verstag.c ]; then \
298                 buildnr=0; \
299                 if [ -f $(BUILDREV_H) ]; then \
300                         buildnr=`sed <"$(BUILDREV_H)" -n -e 's/#define VERS_BUILD \([0-9][0-9]*\)/\1/p'`; \
301                 fi; \
302                 buildnr=`expr $$buildnr + 1`; \
303                 buildhost=`hostname | sed -n -e '1h;2,$$H;$${g;s/\n//g;p;}'`; \
304                 echo "#define VERS_BUILD $$buildnr" >"$(BUILDREV_H)"; \
305                 echo "#define VERS_HOST  \"$$buildhost\"" >>"$(BUILDREV_H)"; \
306                 echo "Building revision $$buildnr"; \
307         fi; \
308         #
309
310 # Include dependencies
311 ifneq ($(strip $(OBJ)),)
312 -include $(OBJ:%.o=%.d)
313 endif