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"
66 $Q bertos/run_tests.sh
70 ifneq ($$($(1)_CROSS),)
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 set.
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 # In embedded systems the target CPU is needed,
91 # but there are different options on how pass
93 ifneq ($$(strip $$($(1)_MCU)),)
94 $(1)_MCPU = -mmcu=$$($(1)_MCU)
96 ifneq ($$(strip $$($(1)_CPU)),)
97 $(1)_MCPU = -mcpu=$$($(1)_CPU)
100 # If a CPU is specified add to
101 # project specific flags.
102 ifneq ($$($(1)_MCPU),)
103 $(1)_CFLAGS += $$($(1)_MCPU)
104 $(1)_CXXFLAGS += $$($(1)_MCPU)
105 $(1)_ASFLAGS += $$($(1)_MCPU)
106 $(1)_CPPAFLAGS += $$($(1)_MCPU)
107 $(1)_LDFLAGS += $$($(1)_MCPU)
110 ifneq ($$(strip $$($(1)_LDSCRIPT)),)
111 $(1)_LDFLAGS += -Wl,-T$$($(1)_LDSCRIPT)
115 ifeq ($$($(1)_DEBUG),1)
116 # AVR is an harvard processor
117 # and needs debug module
118 # to be compiled in program memory
119 ifeq ($$(findstring avr, $$($(1)_CROSS)),avr)
120 $(1)_DEBUGSRC = $(1)_PCSRC
122 $(1)_DEBUGSRC = $(1)_CSRC
125 $$($(1)_DEBUGSRC) += bertos/drv/kdebug.c
127 # Also add formatwr.c (printf) if not already present
128 ifneq ($$(findstring formatwr.c, $$($$($(1)_DEBUGSRC))),formatwr.c)
129 $$($(1)_DEBUGSRC) += bertos/mware/formatwr.c
132 $(1)_CFLAGS += -D_DEBUG
133 $(1)_CXXFLAGS += -D_DEBUG
136 $(1)_CC = $$($(1)_CROSS)$$(CC)
137 $(1)_CXX = $$($(1)_CROSS)$$(CXX)
138 $(1)_AS = $$($(1)_CROSS)$$(AS)
139 $(1)_OBJCOPY = $$($(1)_CROSS)$$(OBJCOPY)
140 $(1)_STRIP = $$($(1)_CROSS)$$(STRIP)
142 $(1)_COBJ = $$(foreach file,$$($(1)_CSRC:%.c=%.o),$$(OBJDIR)/$(1)/$$(file))
143 $(1)_CXXOBJ = $$(foreach file,$$($(1)_CXXSRC:%.cpp=%.o),$$(OBJDIR)/$(1)/$$(file))
144 $(1)_PCOBJ = $$(foreach file,$$($(1)_PCSRC:%.c=%_P.o),$$(OBJDIR)/$(1)/$$(file))
145 $(1)_AOBJ = $$(foreach file,$$($(1)_ASRC:%.s=%.o),$$(OBJDIR)/$(1)/$$(file))
146 $(1)_CPPAOBJ = $$(foreach file,$$($(1)_CPPASRC:%.S=%.o),$$(OBJDIR)/$(1)/$$(file))
147 $(1)_OBJ := $$($(1)_COBJ) $$($(1)_CXXOBJ) $$($(1)_PCOBJ) $$($(1)_AOBJ) $$($(1)_CPPAOBJ)
148 $(1)_SRC := $$($(1)_CSRC) $$($(1)_CXXSRC) $$($(1)_PCSRC) $$($(1)_ASRC) $$($(1)_CPPASRC)
151 ifneq ($$(strip $$($(1)_CXXSRC)),)
152 $(1)_LD = $$($(1)_CROSS)$$(LDXX)
154 $(1)_LD = $$($(1)_CROSS)$$(LD)
157 # Sometimes $(CC) is actually set to a C++ compiler in disguise, and it
158 # would whine if we passed it C-only flags. Checking for the presence of
159 # "++" in the name is a kludge that seems to work mostly.
160 ifeq (++,$$(findstring ++,$$($(1)_CC)))
161 REAL_CFLAGS = $$(CXXFLAGS)
163 REAL_CFLAGS = $$(CFLAGS)
166 # Compile: instructions to create assembler and/or object files from C source
167 $$($(1)_COBJ) : $$(OBJDIR)/$(1)/%.o : %.c
168 $L "$(1): Compiling $$< (C)"
169 @$$(MKDIR_P) $$(dir $$@)
170 $Q $$($(1)_CC) -c $$(REAL_CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
172 # Compile: instructions to create assembler and/or object files from C++ source
173 $$($(1)_CXXOBJ) : $$(OBJDIR)/$(1)/%.o : %.cpp
174 $L "$(1): Compiling $$< (C++)"
175 @$$(MKDIR_P) $$(dir $$@)
176 $Q $$($(1)_CXX) -c $$(CXXFLAGS) $$($(1)_CXXFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
178 # Generate assembly sources from C files (debug)
179 $$(OBJDIR)/$(1)/%.s : %.c
180 $L "$(1): Generating asm source $$<"
181 @$$(MKDIR_P) $$(dir $$@)
182 $Q $$($(1)_CC) -S $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$< -o $$@
184 # Generate special progmem variant of a source file
185 $$($(1)_PCOBJ) : $$(OBJDIR)/$(1)/%_P.o : %.c
186 $L "$(1): Compiling $$< (PROGMEM)"
187 @$$(MKDIR_P) $$(dir $$@)
188 $Q $$($(1)_CC) -c -D_PROGMEM $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
190 # Assemble: instructions to create object file from assembler files
191 $$($(1)_AOBJ): $$(OBJDIR)/$(1)/%.o : %.s
192 $L "$(1): Assembling $$<"
193 @$$(MKDIR_P) $$(dir $$@)
194 $Q $$($(1)_AS) -c $$(ASFLAGS) $$($(1)_ASFLAGS) $$< -o $$@
196 $$($(1)_CPPAOBJ): $$(OBJDIR)/$(1)/%.o : %.S
197 $L "$(1): Assembling with CPP $$<"
198 @$$(MKDIR_P) $$(dir $$@)
199 $Q $$($(1)_CC) -c $$(CPPAFLAGS) $$($(1)_CPPAFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
202 # Link: instructions to create elf output file from object files
203 $$(OUTDIR)/$(1).elf $$(OUTDIR)/$(1)_nostrip: bumprev $$($(1)_OBJ) $$($(1)_LDSCRIPT)
204 $L "$(1): Linking $$@"
205 @$$(MKDIR_P) $$(dir $$@)
206 $Q $$($(1)_LD) $$($(1)_OBJ) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
209 $$(OUTDIR)/$(1): $$(OUTDIR)/$(1)_nostrip
210 $L "$(1): Generating stripped executable $$@"
211 $Q $$($(1)_STRIP) -o $$@ $$^
213 # Compile and link (program-at-a-time)
214 $$(OUTDIR)/$(1)_whole.elf: bumprev $$($(1)_SRC) $$($(1)_LDSCRIPT)
215 $L "$(1): Compiling and Linking whole program $$@"
216 @$$(MKDIR_P) $$(dir $$@)
217 $Q $$($(1)_CC) $$($(1)_SRC) $$(CFLAGS) $$($(1)_CFLAGS) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
220 # NOTE: we retry in case of failure because the STK500 programmer is crappy
222 flash_$(1): $(OUTDIR)/$(1).s19 flash_$(1)_local
223 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; then \
224 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; \
226 #avarice --mkII -j usb --erase --program --verify --file images/triface.elf
228 .PHONY: flash_$(1)_local
233 if [ ! -z "$$($(1)_efuse)" ] ; then \
234 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; then \
235 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; \
238 if [ ! -z "$$($(1)_hfuse)" ] ; then \
239 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; then \
240 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; \
243 if [ ! -z "$$($(1)_lfuse)" ] ; then \
244 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; then \
245 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; \
248 if [ ! -z "$$($(1)_lock)" ] ; then \
249 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; then \
250 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; \
254 $$(OUTDIR)/$(1).hex: $$(OUTDIR)/$(1).elf
255 $$($(1)_OBJCOPY) -O ihex $$< $$@
257 $$(OUTDIR)/$(1).s19: $$(OUTDIR)/$(1).elf
258 $$($(1)_OBJCOPY) -O srec $$< $$@
260 $$(OUTDIR)/$(1).bin: $$(OUTDIR)/$(1).elf
261 $$($(1)_OBJCOPY) -O binary $$< $$@
263 $$(OUTDIR)/$(1).obj: $$(OUTDIR)/$(1).elf
264 $$($(1)_OBJCOPY) -O avrobj $$< $$@
266 $$(OUTDIR)/$(1).rom: $$(OUTDIR)/$(1).elf
267 $$($(1)_OBJCOPY) -O $$(FORMAT) $$< $$@
268 # $$($(1)_OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" -O $$(FORMAT) $$< $$(@:.rom=.eep)
272 # Generate build rules for all targets
273 $(foreach t,$(TRG),$(eval $(call build_target,$(t))))
275 # Generate Qt's moc files from headers
276 # NOTE: moc totally sucks and can generate empty files for some error conditions,
277 # leading to puzzling linker errors. Kill 'em and abort build.
280 if [ ! -s $< ]; then \
287 $(COFFCONVERT) -O coff-ext-avr $< $@
288 # $(COFFCONVERT) -O coff-avr $< $@ # For use with AVRstudio 3
290 #make instruction to delete created files
292 clean: clean-recursive
296 $(RECURSIVE_TARGETS):
297 @target=`echo $@ | sed s/-recursive//`; \
298 for dir in $(SUBDIRS); do \
299 if [ -e $$dir/configure.in ] || [ -e $$dir/configure.ac ] && [ ! -x $$dir/configure ]; then \
300 echo "Running autogen.sh in $$dir..."; \
301 ( cd $$dir && chmod a+x autogen.sh && ./autogen.sh && rm -f Makefile || exit 1 ); \
303 if [ ! -e $$dir/Makefile ]; then \
304 if [ -e "$$dir/build-$(ARCH)" ]; then \
305 echo "Running build script in $$dir..."; \
306 ( cd $$dir && chmod a+x build && ./build || exit 1 ); \
308 echo "Running configure in $$dir..."; \
309 ( cd $$dir && ./configure --prefix=$(PREFIX) || exit 1 ); \
312 $(MAKE) -C $$dir $$target || exit 1; \
315 BUILDREV_H = buildrev.h
319 @if [ -e bertos/verstag.c ]; then \
321 if [ -f $(BUILDREV_H) ]; then \
322 buildnr=`sed <"$(BUILDREV_H)" -n -e 's/#define VERS_BUILD \([0-9][0-9]*\)/\1/p'`; \
324 buildnr=`expr $$buildnr + 1`; \
325 buildhost=`hostname | sed -n -e '1h;2,$$H;$${g;s/\n//g;p;}'`; \
326 echo "#define VERS_BUILD $$buildnr" >"$(BUILDREV_H)"; \
327 echo "#define VERS_HOST \"$$buildhost\"" >>"$(BUILDREV_H)"; \
328 echo "Building revision $$buildnr"; \
332 # Include dependencies
333 ifneq ($(strip $(OBJ)),)
334 -include $(OBJ:%.o=%.d)