3 # Copyright 2002,2003,2004,2005,2006 Develer S.r.l. (http://www.develer.com/)
7 # GCC-AVR standard Makefile part 2
10 # Author: Bernardo Innocenti <bernie@develer.com>
13 # Remove all default pattern rules
27 # Select Bourne Again SHell as default make shell
33 CFLAGS += -Wundef -D__x86_64__=1 -D__unix__=1 -D__linux__=1 -D__STDC_VERSION__=199901L
36 # Initialize $(top_srcdir) with current directory, unless it was already initialized
37 top_srcdir ?= $(shell pwd)
39 # Virtual Product: based on target products may be different.
40 # e.g. Embedded target = hex, s19, bin
42 TRG_TGT = $(TRG:%=$(OUTDIR)/%.tgt)
44 RECURSIVE_TARGETS = all-recursive install-recursive clean-recursive
48 all:: all-recursive $(TRG_TGT)
50 # Generate project documentation
53 $L "Building documentation"
59 $L "Rebuilding C tags database"
60 $Q ctags -R --exclude=doc
65 $L "Running testsuite"
70 ifeq ($$($(1)_EMBEDDED_TGT),1)
71 #use embedded specific map flags
72 $(1)_MAP_FLAGS = $$(MAP_FLAGS_EMB)
73 #In embedded we need s19, hex and bin
74 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1).s19 $$(OUTDIR)/$(1).hex $$(OUTDIR)/$(1).bin
76 #use hosted specific map flags
77 $(1)_MAP_FLAGS = $$(MAP_FLAGS_HOST)
78 #in hosted application we need only executable file.
79 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1)
82 $(1)_LDFLAGS += $$($(1)_MAP_FLAGS)
84 ifneq ($$(strip $$($(1)_MCU)),)
85 # Define all project specific object files
86 $(1)_CFLAGS += -mmcu=$$($(1)_MCU)
87 $(1)_CXXFLAGS += -mmcu=$$($(1)_MCU)
88 $(1)_ASFLAGS += -mmcu=$$($(1)_MCU)
89 $(1)_CPPAFLAGS += -mmcu=$$($(1)_MCU)
90 $(1)_LDFLAGS += -mmcu=$$($(1)_MCU)
92 ifneq ($$(strip $$($(1)_CPU)),)
93 # Define all project specific object files
94 $(1)_CFLAGS += -mcpu=$$($(1)_CPU)
95 $(1)_CXXFLAGS += -mcpu=$$($(1)_CPU)
96 $(1)_ASFLAGS += -mcpu=$$($(1)_CPU)
97 $(1)_CPPAFLAGS += -mcpu=$$($(1)_CPU)
98 $(1)_LDFLAGS += -mcpu=$$($(1)_CPU)
100 ifneq ($$(strip $$($(1)_LDSCRIPT)),)
101 $(1)_LDFLAGS += -Wl,-T$$($(1)_LDSCRIPT)
104 $(1)_CC = $$($(1)_CROSS)$$(CC)
105 $(1)_CXX = $$($(1)_CROSS)$$(CXX)
106 $(1)_AS = $$($(1)_CROSS)$$(AS)
107 $(1)_OBJCOPY = $$($(1)_CROSS)$$(OBJCOPY)
108 $(1)_STRIP = $$($(1)_CROSS)$$(STRIP)
110 $(1)_COBJ = $$(foreach file,$$($(1)_CSRC:%.c=%.o),$$(OBJDIR)/$(1)/$$(file))
111 $(1)_CXXOBJ = $$(foreach file,$$($(1)_CXXSRC:%.cpp=%.o),$$(OBJDIR)/$(1)/$$(file))
112 $(1)_PCOBJ = $$(foreach file,$$($(1)_PCSRC:%.c=%_P.o),$$(OBJDIR)/$(1)/$$(file))
113 $(1)_AOBJ = $$(foreach file,$$($(1)_ASRC:%.s=%.o),$$(OBJDIR)/$(1)/$$(file))
114 $(1)_CPPAOBJ = $$(foreach file,$$($(1)_CPPASRC:%.S=%.o),$$(OBJDIR)/$(1)/$$(file))
115 $(1)_OBJ := $$($(1)_COBJ) $$($(1)_CXXOBJ) $$($(1)_PCOBJ) $$($(1)_AOBJ) $$($(1)_CPPAOBJ)
116 $(1)_SRC := $$($(1)_CSRC) $$($(1)_CXXSRC) $$($(1)_PCSRC) $$($(1)_ASRC) $$($(1)_CPPASRC)
119 ifneq ($$(strip $$($(1)_CXXSRC)),)
120 $(1)_LD = $$($(1)_CROSS)$$(LDXX)
122 $(1)_LD = $$($(1)_CROSS)$$(LD)
125 # Compile: instructions to create assembler and/or object files from C source
126 $$($(1)_COBJ) : $$(OBJDIR)/$(1)/%.o : %.c
127 $L "$(1): Compiling $$< (C)"
128 @$$(MKDIR_P) $$(dir $$@)
129 $Q $$($(1)_CC) -c $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
131 # Compile: instructions to create assembler and/or object files from C++ source
132 $$($(1)_CXXOBJ) : $$(OBJDIR)/$(1)/%.o : %.cpp
133 $L "$(1): Compiling $$< (C++)"
134 @$$(MKDIR_P) $$(dir $$@)
135 $Q $$($(1)_CXX) -c $$(CXXFLAGS) $$($(1)_CXXFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
137 # Generate assembly sources from C files (debug)
138 $$(OBJDIR)/$(1)/%.s : %.c
139 $L "$(1): Generating asm source $$<"
140 @$$(MKDIR_P) $$(dir $$@)
141 $Q $$($(1)_CC) -S $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$< -o $$@
143 # Generate special progmem variant of a source file
144 $$($(1)_PCOBJ) : $$(OBJDIR)/$(1)/%_P.o : %.c
145 $L "$(1): Compiling $$< (PROGMEM)"
146 @$$(MKDIR_P) $$(dir $$@)
147 $Q $$($(1)_CC) -c -D_PROGMEM $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
149 # Assemble: instructions to create object file from assembler files
150 $$($(1)_AOBJ): $$(OBJDIR)/$(1)/%.o : %.s
151 $L "$(1): Assembling $$<"
152 @$$(MKDIR_P) $$(dir $$@)
153 $Q $$($(1)_AS) -c $$(ASFLAGS) $$($(1)_ASFLAGS) $$< -o $$@
155 $$($(1)_CPPAOBJ): $$(OBJDIR)/$(1)/%.o : %.S
156 $L "$(1): Assembling with CPP $$<"
157 @$$(MKDIR_P) $$(dir $$@)
158 $Q $$($(1)_CC) -c $$(CPPAFLAGS) $$($(1)_CPPAFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
161 # Link: instructions to create elf output file from object files
162 $$(OUTDIR)/$(1).elf $$(OUTDIR)/$(1)_nostrip: bumprev $$($(1)_OBJ) $$($(1)_LDSCRIPT)
163 $L "$(1): Linking $$@"
164 @$$(MKDIR_P) $$(dir $$@)
165 $Q $$($(1)_LD) $$($(1)_OBJ) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
168 $$(OUTDIR)/$(1): $$(OUTDIR)/$(1)_nostrip
169 $L "$(1): Generating stripped executable $$@"
170 $Q $$($(1)_STRIP) -o $$@ $$^
172 # Compile and link (program-at-a-time)
173 $$(OUTDIR)/$(1)_whole.elf: bumprev $$($(1)_SRC) $$($(1)_LDSCRIPT)
174 $L "$(1): Compiling and Linking whole program $$@"
175 @$$(MKDIR_P) $$(dir $$@)
176 $Q $$($(1)_CC) $$($(1)_SRC) $$(CFLAGS) $$($(1)_CFLAGS) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
179 # NOTE: we retry in case of failure because the STK500 programmer is crappy
181 flash_$(1): $(OUTDIR)/$(1).s19 flash_$(1)_local
182 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; then \
183 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; \
185 #avarice --mkII -j usb --erase --program --verify --file images/triface.elf
187 .PHONY: flash_$(1)_local
192 if [ ! -z "$$($(1)_efuse)" ] ; then \
193 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; then \
194 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; \
197 if [ ! -z "$$($(1)_hfuse)" ] ; then \
198 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; then \
199 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; \
202 if [ ! -z "$$($(1)_lfuse)" ] ; then \
203 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; then \
204 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; \
207 if [ ! -z "$$($(1)_lock)" ] ; then \
208 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; then \
209 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; \
213 $$(OUTDIR)/$(1).hex: $$(OUTDIR)/$(1).elf
214 $$($(1)_OBJCOPY) -O ihex $$< $$@
216 $$(OUTDIR)/$(1).s19: $$(OUTDIR)/$(1).elf
217 $$($(1)_OBJCOPY) -O srec $$< $$@
219 $$(OUTDIR)/$(1).bin: $$(OUTDIR)/$(1).elf
220 $$($(1)_OBJCOPY) -O binary $$< $$@
222 $$(OUTDIR)/$(1).obj: $$(OUTDIR)/$(1).elf
223 $$($(1)_OBJCOPY) -O avrobj $$< $$@
225 $$(OUTDIR)/$(1).rom: $$(OUTDIR)/$(1).elf
226 $$($(1)_OBJCOPY) -O $$(FORMAT) $$< $$@
227 # $$($(1)_OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" -O $$(FORMAT) $$< $$(@:.rom=.eep)
231 # Generate build rules for all targets
232 $(foreach t,$(TRG),$(eval $(call build_target,$(t))))
234 # Generate Qt's moc files from headers
235 # NOTE: moc totally sucks and can generate empty files for some error conditions,
236 # leading to puzzling linker errors. Kill 'em and abort build.
239 if [ ! -s $< ]; then \
246 $(COFFCONVERT) -O coff-ext-avr $< $@
247 # $(COFFCONVERT) -O coff-avr $< $@ # For use with AVRstudio 3
249 #make instruction to delete created files
251 clean: clean-recursive
255 $(RECURSIVE_TARGETS):
256 @target=`echo $@ | sed s/-recursive//`; \
257 for dir in $(SUBDIRS); do \
258 if [ -e $$dir/configure.in ] || [ -e $$dir/configure.ac ] && [ ! -x $$dir/configure ]; then \
259 echo "Running autogen.sh in $$dir..."; \
260 ( cd $$dir && chmod a+x autogen.sh && ./autogen.sh && rm -f Makefile || exit 1 ); \
262 if [ ! -e $$dir/Makefile ]; then \
263 if [ -e "$$dir/build-$(ARCH)" ]; then \
264 echo "Running build script in $$dir..."; \
265 ( cd $$dir && chmod a+x build && ./build || exit 1 ); \
267 echo "Running configure in $$dir..."; \
268 ( cd $$dir && ./configure --prefix=$(PREFIX) || exit 1 ); \
271 $(MAKE) -C $$dir $$target || exit 1; \
274 BUILDREV_H = buildrev.h
276 ifeq ($(shell [ -e bertos/verstag.c ] && echo yes),yes)
280 if [ -f $(BUILDREV_H) ]; then \
281 buildnr=`sed <"$(BUILDREV_H)" -n -e 's/#define VERS_BUILD \([0-9][0-9]*\)/\1/p'`; \
283 buildnr=`expr $$buildnr + 1`; \
284 buildhost=`hostname | sed -n -e '1h;2,$$H;$${g;s/\n//g;p;}'`; \
285 echo "#define VERS_BUILD $$buildnr" >"$(BUILDREV_H)"; \
286 echo "#define VERS_HOST \"$$buildhost\"" >>"$(BUILDREV_H)"; \
287 echo "Building revision $$buildnr"
294 # Include dependencies
295 ifneq ($(strip $(OBJ)),)
296 -include $(OBJ:%.o=%.d)