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