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