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
43 TRG_TGT = $(TRG:%=$(OUTDIR)/%.tgt)
45 RECURSIVE_TARGETS = all-recursive install-recursive clean-recursive
49 all:: all-recursive $(TRG_TGT)
51 # Generate project documentation
54 $L "Building documentation"
60 $L "Rebuilding C tags database"
61 $Q ctags -R --exclude=doc
65 check: $(OUTDIR)/libunittest.a
66 $L "Running testsuite"
71 ifneq ($$($(1)_CROSS),)
72 #use embedded specific map flags
73 $(1)_MAP_FLAGS = $$(MAP_FLAGS_EMB)
74 #In embedded we need s19, hex and bin
75 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1).s19 $$(OUTDIR)/$(1).hex $$(OUTDIR)/$(1).bin
77 #On Darwin architecture the assembly doesn't link correctly if these flags are set.
78 ifeq ($(shell uname | grep -c "Darwin"),1)
83 #use hosted specific map flags
84 $(1)_MAP_FLAGS = $$(MAP_FLAGS_HOST)
86 #Handle library creation
87 ifeq ($$($(1)_MAKELIB),1)
88 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1).a
90 #Otherwise in hosted application we need only executable file.
91 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1)
95 $(1)_LDFLAGS += $$($(1)_MAP_FLAGS)
97 # In embedded systems the target CPU is needed,
98 # but there are different options on how pass
100 ifneq ($$(strip $$($(1)_MCU)),)
101 $(1)_MCPU = -mmcu=$$($(1)_MCU)
103 ifneq ($$(strip $$($(1)_CPU)),)
104 $(1)_MCPU = -mcpu=$$($(1)_CPU)
107 # If a CPU is specified add to
108 # project specific flags.
109 ifneq ($$($(1)_MCPU),)
110 $(1)_CFLAGS += $$($(1)_MCPU)
111 $(1)_CXXFLAGS += $$($(1)_MCPU)
112 $(1)_ASFLAGS += $$($(1)_MCPU)
113 $(1)_CPPAFLAGS += $$($(1)_MCPU)
114 $(1)_LDFLAGS += $$($(1)_MCPU)
117 ifneq ($$(strip $$($(1)_LDSCRIPT)),)
118 $(1)_LDFLAGS += -Wl,-T$$($(1)_LDSCRIPT)
122 ifeq ($$($(1)_DEBUG),1)
123 # AVR is an harvard processor
124 # and needs debug module
125 # to be compiled in program memory
126 ifeq ($$(findstring avr, $$($(1)_CROSS)),avr)
127 $(1)_DEBUGSRC = $(1)_PCSRC
129 $(1)_DEBUGSRC = $(1)_CSRC
132 $$($(1)_DEBUGSRC) += bertos/drv/kdebug.c
134 # Also add formatwr.c (printf) if not already present
135 ifneq ($$(findstring formatwr.c, $$($$($(1)_DEBUGSRC))),formatwr.c)
136 $$($(1)_DEBUGSRC) += bertos/mware/formatwr.c
139 $(1)_CFLAGS += -D_DEBUG
140 $(1)_CXXFLAGS += -D_DEBUG
143 $(1)_CC ?= $$($(1)_CROSS)$$(CC)
144 $(1)_CXX ?= $$($(1)_CROSS)$$(CXX)
145 $(1)_AS ?= $$($(1)_CROSS)$$(AS)
146 $(1)_AR ?= $$($(1)_CROSS)$$(AR)
147 $(1)_OBJCOPY ?= $$($(1)_CROSS)$$(OBJCOPY)
148 $(1)_STRIP ?= $$($(1)_CROSS)$$(STRIP)
150 $(1)_COBJ = $$(foreach file,$$($(1)_CSRC:%.c=%.o),$$(OBJDIR)/$(1)/$$(file))
151 $(1)_CXXOBJ = $$(foreach file,$$($(1)_CXXSRC:%.cpp=%.o),$$(OBJDIR)/$(1)/$$(file))
152 $(1)_PCOBJ = $$(foreach file,$$($(1)_PCSRC:%.c=%_P.o),$$(OBJDIR)/$(1)/$$(file))
153 $(1)_AOBJ = $$(foreach file,$$($(1)_ASRC:%.s=%.o),$$(OBJDIR)/$(1)/$$(file))
154 $(1)_CPPAOBJ = $$(foreach file,$$($(1)_CPPASRC:%.S=%.o),$$(OBJDIR)/$(1)/$$(file))
155 $(1)_OBJ := $$($(1)_COBJ) $$($(1)_CXXOBJ) $$($(1)_PCOBJ) $$($(1)_AOBJ) $$($(1)_CPPAOBJ)
156 $(1)_SRC := $$($(1)_CSRC) $$($(1)_CXXSRC) $$($(1)_PCSRC) $$($(1)_ASRC) $$($(1)_CPPASRC)
159 ifneq ($$(strip $$($(1)_CXXSRC)),)
160 $(1)_LD = $$($(1)_CROSS)$$(LDXX)
162 $(1)_LD = $$($(1)_CROSS)$$(LD)
165 # Sometimes $(CC) is actually set to a C++ compiler in disguise, and it
166 # would whine if we passed it C-only flags. Checking for the presence of
167 # "++" in the name is a kludge that seems to work mostly.
168 ifeq (++,$$(findstring ++,$$($(1)_CC)))
169 $(1)_REAL_CFLAGS = $$(CXXFLAGS)
171 $(1)_REAL_CFLAGS = $$(CFLAGS)
174 # Compile: instructions to create assembler and/or object files from C source
175 $$($(1)_COBJ) : $$(OBJDIR)/$(1)/%.o : %.c
176 $L "$(1): Compiling $$< (C)"
177 @$$(MKDIR_P) $$(dir $$@)
178 $Q $$($(1)_CC) -c $$($(1)_REAL_CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
180 # Compile: instructions to create assembler and/or object files from C++ source
181 $$($(1)_CXXOBJ) : $$(OBJDIR)/$(1)/%.o : %.cpp
182 $L "$(1): Compiling $$< (C++)"
183 @$$(MKDIR_P) $$(dir $$@)
184 $Q $$($(1)_CXX) -c $$(CXXFLAGS) $$($(1)_CXXFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
186 # Generate assembly sources from C files (debug)
187 $$(OBJDIR)/$(1)/%.s : %.c
188 $L "$(1): Generating asm source $$<"
189 @$$(MKDIR_P) $$(dir $$@)
190 $Q $$($(1)_CC) -S $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$< -o $$@
192 # Generate special progmem variant of a source file
193 $$($(1)_PCOBJ) : $$(OBJDIR)/$(1)/%_P.o : %.c
194 $L "$(1): Compiling $$< (PROGMEM)"
195 @$$(MKDIR_P) $$(dir $$@)
196 $Q $$($(1)_CC) -c -D_PROGMEM $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
198 # Assemble: instructions to create object file from assembler files
199 $$($(1)_AOBJ): $$(OBJDIR)/$(1)/%.o : %.s
200 $L "$(1): Assembling $$<"
201 @$$(MKDIR_P) $$(dir $$@)
202 $Q $$($(1)_AS) -c $$(ASFLAGS) $$($(1)_ASFLAGS) $$< -o $$@
204 $$($(1)_CPPAOBJ): $$(OBJDIR)/$(1)/%.o : %.S
205 $L "$(1): Assembling with CPP $$<"
206 @$$(MKDIR_P) $$(dir $$@)
207 $Q $$($(1)_CC) -c $$(CPPAFLAGS) $$($(1)_CPPAFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
210 # Link: instructions to create elf output file from object files
211 $$(OUTDIR)/$(1).elf $$(OUTDIR)/$(1)_nostrip: bumprev $$($(1)_OBJ) $$($(1)_LDSCRIPT)
212 $L "$(1): Linking $$@"
213 @$$(MKDIR_P) $$(dir $$@)
214 $Q $$($(1)_LD) $$($(1)_OBJ) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
217 # Instructions to create a static library from object files
218 $$(OUTDIR)/$(1).a: bumprev $$($(1)_OBJ)
219 $L "$(1): Creating static library $$@"
220 @$$(MKDIR_P) $$(dir $$@)
221 $Q $$($(1)_AR) $$(ARFLAGS) $$($(1)_ARFLAGS) $$@ $$($(1)_OBJ)
224 $$(OUTDIR)/$(1): $$(OUTDIR)/$(1)_nostrip
225 $L "$(1): Generating stripped executable $$@"
226 $Q $$($(1)_STRIP) -o $$@ $$^
228 # Compile and link (program-at-a-time)
229 $$(OUTDIR)/$(1)_whole.elf: bumprev $$($(1)_SRC) $$($(1)_LDSCRIPT)
230 $L "$(1): Compiling and Linking whole program $$@"
231 @$$(MKDIR_P) $$(dir $$@)
232 $Q $$($(1)_CC) $$($(1)_SRC) $$(CFLAGS) $$($(1)_CFLAGS) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
235 # NOTE: we retry in case of failure because the STK500 programmer is crappy
237 flash_$(1): $(OUTDIR)/$(1).s19 flash_$(1)_local
238 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; then \
239 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; \
241 #avarice --mkII -j usb --erase --program --verify --file images/triface.elf
243 .PHONY: flash_$(1)_local
248 if [ ! -z "$$($(1)_efuse)" ] ; then \
249 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; then \
250 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; \
253 if [ ! -z "$$($(1)_hfuse)" ] ; then \
254 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; then \
255 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; \
258 if [ ! -z "$$($(1)_lfuse)" ] ; then \
259 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; then \
260 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; \
263 if [ ! -z "$$($(1)_lock)" ] ; then \
264 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; then \
265 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; \
269 $$(OUTDIR)/$(1).hex: $$(OUTDIR)/$(1).elf
270 $$($(1)_OBJCOPY) -O ihex $$< $$@
272 $$(OUTDIR)/$(1).s19: $$(OUTDIR)/$(1).elf
273 $$($(1)_OBJCOPY) -O srec $$< $$@
275 $$(OUTDIR)/$(1).bin: $$(OUTDIR)/$(1).elf
276 $$($(1)_OBJCOPY) -O binary $$< $$@
278 $$(OUTDIR)/$(1).obj: $$(OUTDIR)/$(1).elf
279 $$($(1)_OBJCOPY) -O avrobj $$< $$@
281 $$(OUTDIR)/$(1).rom: $$(OUTDIR)/$(1).elf
282 $$($(1)_OBJCOPY) -O $$(FORMAT) $$< $$@
283 # $$($(1)_OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" -O $$(FORMAT) $$< $$(@:.rom=.eep)
287 # Generate build rules for all targets and libunittest
288 $(foreach t,$(TRG) libunittest,$(eval $(call build_target,$(t))))
290 # Generate Qt's moc files from headers
291 # NOTE: moc totally sucks and can generate empty files for some error conditions,
292 # leading to puzzling linker errors. Kill 'em and abort build.
295 if [ ! -s $< ]; then \
302 $(COFFCONVERT) -O coff-ext-avr $< $@
303 # $(COFFCONVERT) -O coff-avr $< $@ # For use with AVRstudio 3
305 #make instruction to delete created files
307 clean: clean-recursive
311 $(RECURSIVE_TARGETS):
312 @target=`echo $@ | sed s/-recursive//`; \
313 for dir in $(SUBDIRS); do \
314 if [ -e $$dir/configure.in ] || [ -e $$dir/configure.ac ] && [ ! -x $$dir/configure ]; then \
315 echo "Running autogen.sh in $$dir..."; \
316 ( cd $$dir && chmod a+x autogen.sh && ./autogen.sh && rm -f Makefile || exit 1 ); \
318 if [ ! -e $$dir/Makefile ]; then \
319 if [ -e "$$dir/build-$(ARCH)" ]; then \
320 echo "Running build script in $$dir..."; \
321 ( cd $$dir && chmod a+x build && ./build || exit 1 ); \
323 echo "Running configure in $$dir..."; \
324 ( cd $$dir && ./configure --prefix=$(PREFIX) || exit 1 ); \
327 $(MAKE) -C $$dir $$target || exit 1; \
330 BUILDREV_H = buildrev.h
334 @if [ -e bertos/verstag.c ]; then \
336 if [ -f $(BUILDREV_H) ]; then \
337 buildnr=`sed <"$(BUILDREV_H)" -n -e 's/#define VERS_BUILD \([0-9][0-9]*\)/\1/p'`; \
339 buildnr=`expr $$buildnr + 1`; \
340 buildhost=`hostname | sed -n -e '1h;2,$$H;$${g;s/\n//g;p;}'`; \
341 echo "#define VERS_BUILD $$buildnr" >"$(BUILDREV_H)"; \
342 echo "#define VERS_HOST \"$$buildhost\"" >>"$(BUILDREV_H)"; \
343 echo "Building revision $$buildnr"; \
347 # Include dependencies
348 ifneq ($(strip $(OBJ)),)
349 -include $(OBJ:%.o=%.d)