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