3 # Copyright 2002,2003,2004,2005,2006 Develer S.r.l. (http://www.develer.com/)
7 # GCC-AVR standard Makefile part 2
10 # Author: Bernie Innocenti <bernie@codewiz.org>
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 #On Darwin architecture the assembly doesn't link correctly if this flag is setted.
77 ifeq ($(shell uname | grep -c "Darwin"),1)
82 #use hosted specific map flags
83 $(1)_MAP_FLAGS = $$(MAP_FLAGS_HOST)
84 #in hosted application we need only executable file.
85 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1)
88 $(1)_LDFLAGS += $$($(1)_MAP_FLAGS)
90 ifneq ($$(strip $$($(1)_MCU)),)
91 # Define all project specific object files
92 $(1)_CFLAGS += -mmcu=$$($(1)_MCU)
93 $(1)_CXXFLAGS += -mmcu=$$($(1)_MCU)
94 $(1)_ASFLAGS += -mmcu=$$($(1)_MCU)
95 $(1)_CPPAFLAGS += -mmcu=$$($(1)_MCU)
96 $(1)_LDFLAGS += -mmcu=$$($(1)_MCU)
98 ifneq ($$(strip $$($(1)_CPU)),)
99 # Define all project specific object files
100 $(1)_CFLAGS += -mcpu=$$($(1)_CPU)
101 $(1)_CXXFLAGS += -mcpu=$$($(1)_CPU)
102 $(1)_ASFLAGS += -mcpu=$$($(1)_CPU)
103 $(1)_CPPAFLAGS += -mcpu=$$($(1)_CPU)
104 $(1)_LDFLAGS += -mcpu=$$($(1)_CPU)
106 ifneq ($$(strip $$($(1)_LDSCRIPT)),)
107 $(1)_LDFLAGS += -Wl,-T$$($(1)_LDSCRIPT)
110 $(1)_CC = $$($(1)_CROSS)$$(CC)
111 $(1)_CXX = $$($(1)_CROSS)$$(CXX)
112 $(1)_AS = $$($(1)_CROSS)$$(AS)
113 $(1)_OBJCOPY = $$($(1)_CROSS)$$(OBJCOPY)
114 $(1)_STRIP = $$($(1)_CROSS)$$(STRIP)
116 $(1)_COBJ = $$(foreach file,$$($(1)_CSRC:%.c=%.o),$$(OBJDIR)/$(1)/$$(file))
117 $(1)_CXXOBJ = $$(foreach file,$$($(1)_CXXSRC:%.cpp=%.o),$$(OBJDIR)/$(1)/$$(file))
118 $(1)_PCOBJ = $$(foreach file,$$($(1)_PCSRC:%.c=%_P.o),$$(OBJDIR)/$(1)/$$(file))
119 $(1)_AOBJ = $$(foreach file,$$($(1)_ASRC:%.s=%.o),$$(OBJDIR)/$(1)/$$(file))
120 $(1)_CPPAOBJ = $$(foreach file,$$($(1)_CPPASRC:%.S=%.o),$$(OBJDIR)/$(1)/$$(file))
121 $(1)_OBJ := $$($(1)_COBJ) $$($(1)_CXXOBJ) $$($(1)_PCOBJ) $$($(1)_AOBJ) $$($(1)_CPPAOBJ)
122 $(1)_SRC := $$($(1)_CSRC) $$($(1)_CXXSRC) $$($(1)_PCSRC) $$($(1)_ASRC) $$($(1)_CPPASRC)
125 ifneq ($$(strip $$($(1)_CXXSRC)),)
126 $(1)_LD = $$($(1)_CROSS)$$(LDXX)
128 $(1)_LD = $$($(1)_CROSS)$$(LD)
131 # Sometimes $(CC) is actually set to a C++ compiler in disguise, and it
132 # would whine if we passed it C-only flags. Checking for the presence of
133 # "++" in the name is a kludge that seems to work mostly.
134 ifeq (++,$$(findstring ++,$$($(1)_CC)))
135 REAL_CFLAGS = $$(CXXFLAGS)
137 REAL_CFLAGS = $$(CFLAGS)
140 # Compile: instructions to create assembler and/or object files from C source
141 $$($(1)_COBJ) : $$(OBJDIR)/$(1)/%.o : %.c
142 $L "$(1): Compiling $$< (C)"
143 @$$(MKDIR_P) $$(dir $$@)
144 $Q $$($(1)_CC) -c $$(REAL_CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
146 # Compile: instructions to create assembler and/or object files from C++ source
147 $$($(1)_CXXOBJ) : $$(OBJDIR)/$(1)/%.o : %.cpp
148 $L "$(1): Compiling $$< (C++)"
149 @$$(MKDIR_P) $$(dir $$@)
150 $Q $$($(1)_CXX) -c $$(CXXFLAGS) $$($(1)_CXXFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
152 # Generate assembly sources from C files (debug)
153 $$(OBJDIR)/$(1)/%.s : %.c
154 $L "$(1): Generating asm source $$<"
155 @$$(MKDIR_P) $$(dir $$@)
156 $Q $$($(1)_CC) -S $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$< -o $$@
158 # Generate special progmem variant of a source file
159 $$($(1)_PCOBJ) : $$(OBJDIR)/$(1)/%_P.o : %.c
160 $L "$(1): Compiling $$< (PROGMEM)"
161 @$$(MKDIR_P) $$(dir $$@)
162 $Q $$($(1)_CC) -c -D_PROGMEM $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
164 # Assemble: instructions to create object file from assembler files
165 $$($(1)_AOBJ): $$(OBJDIR)/$(1)/%.o : %.s
166 $L "$(1): Assembling $$<"
167 @$$(MKDIR_P) $$(dir $$@)
168 $Q $$($(1)_AS) -c $$(ASFLAGS) $$($(1)_ASFLAGS) $$< -o $$@
170 $$($(1)_CPPAOBJ): $$(OBJDIR)/$(1)/%.o : %.S
171 $L "$(1): Assembling with CPP $$<"
172 @$$(MKDIR_P) $$(dir $$@)
173 $Q $$($(1)_CC) -c $$(CPPAFLAGS) $$($(1)_CPPAFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
176 # Link: instructions to create elf output file from object files
177 $$(OUTDIR)/$(1).elf $$(OUTDIR)/$(1)_nostrip: bumprev $$($(1)_OBJ) $$($(1)_LDSCRIPT)
178 $L "$(1): Linking $$@"
179 @$$(MKDIR_P) $$(dir $$@)
180 $Q $$($(1)_LD) $$($(1)_OBJ) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
183 $$(OUTDIR)/$(1): $$(OUTDIR)/$(1)_nostrip
184 $L "$(1): Generating stripped executable $$@"
185 $Q $$($(1)_STRIP) -o $$@ $$^
187 # Compile and link (program-at-a-time)
188 $$(OUTDIR)/$(1)_whole.elf: bumprev $$($(1)_SRC) $$($(1)_LDSCRIPT)
189 $L "$(1): Compiling and Linking whole program $$@"
190 @$$(MKDIR_P) $$(dir $$@)
191 $Q $$($(1)_CC) $$($(1)_SRC) $$(CFLAGS) $$($(1)_CFLAGS) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
194 # NOTE: we retry in case of failure because the STK500 programmer is crappy
196 flash_$(1): $(OUTDIR)/$(1).s19 flash_$(1)_local
197 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; then \
198 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; \
200 #avarice --mkII -j usb --erase --program --verify --file images/triface.elf
202 .PHONY: flash_$(1)_local
207 if [ ! -z "$$($(1)_efuse)" ] ; then \
208 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; then \
209 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; \
212 if [ ! -z "$$($(1)_hfuse)" ] ; then \
213 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; then \
214 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; \
217 if [ ! -z "$$($(1)_lfuse)" ] ; then \
218 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; then \
219 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; \
222 if [ ! -z "$$($(1)_lock)" ] ; then \
223 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; then \
224 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; \
228 $$(OUTDIR)/$(1).hex: $$(OUTDIR)/$(1).elf
229 $$($(1)_OBJCOPY) -O ihex $$< $$@
231 $$(OUTDIR)/$(1).s19: $$(OUTDIR)/$(1).elf
232 $$($(1)_OBJCOPY) -O srec $$< $$@
234 $$(OUTDIR)/$(1).bin: $$(OUTDIR)/$(1).elf
235 $$($(1)_OBJCOPY) -O binary $$< $$@
237 $$(OUTDIR)/$(1).obj: $$(OUTDIR)/$(1).elf
238 $$($(1)_OBJCOPY) -O avrobj $$< $$@
240 $$(OUTDIR)/$(1).rom: $$(OUTDIR)/$(1).elf
241 $$($(1)_OBJCOPY) -O $$(FORMAT) $$< $$@
242 # $$($(1)_OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" -O $$(FORMAT) $$< $$(@:.rom=.eep)
246 # Generate build rules for all targets
247 $(foreach t,$(TRG),$(eval $(call build_target,$(t))))
249 # Generate Qt's moc files from headers
250 # NOTE: moc totally sucks and can generate empty files for some error conditions,
251 # leading to puzzling linker errors. Kill 'em and abort build.
254 if [ ! -s $< ]; then \
261 $(COFFCONVERT) -O coff-ext-avr $< $@
262 # $(COFFCONVERT) -O coff-avr $< $@ # For use with AVRstudio 3
264 #make instruction to delete created files
266 clean: clean-recursive
270 $(RECURSIVE_TARGETS):
271 @target=`echo $@ | sed s/-recursive//`; \
272 for dir in $(SUBDIRS); do \
273 if [ -e $$dir/configure.in ] || [ -e $$dir/configure.ac ] && [ ! -x $$dir/configure ]; then \
274 echo "Running autogen.sh in $$dir..."; \
275 ( cd $$dir && chmod a+x autogen.sh && ./autogen.sh && rm -f Makefile || exit 1 ); \
277 if [ ! -e $$dir/Makefile ]; then \
278 if [ -e "$$dir/build-$(ARCH)" ]; then \
279 echo "Running build script in $$dir..."; \
280 ( cd $$dir && chmod a+x build && ./build || exit 1 ); \
282 echo "Running configure in $$dir..."; \
283 ( cd $$dir && ./configure --prefix=$(PREFIX) || exit 1 ); \
286 $(MAKE) -C $$dir $$target || exit 1; \
289 BUILDREV_H = buildrev.h
293 @if [ -e bertos/verstag.c ]; then \
295 if [ -f $(BUILDREV_H) ]; then \
296 buildnr=`sed <"$(BUILDREV_H)" -n -e 's/#define VERS_BUILD \([0-9][0-9]*\)/\1/p'`; \
298 buildnr=`expr $$buildnr + 1`; \
299 buildhost=`hostname | sed -n -e '1h;2,$$H;$${g;s/\n//g;p;}'`; \
300 echo "#define VERS_BUILD $$buildnr" >"$(BUILDREV_H)"; \
301 echo "#define VERS_HOST \"$$buildhost\"" >>"$(BUILDREV_H)"; \
302 echo "Building revision $$buildnr"; \
306 # Include dependencies
307 ifneq ($(strip $(OBJ)),)
308 -include $(OBJ:%.o=%.d)