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