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