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