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
66 $L "Running testsuite"
71 ifeq ($$($(1)_HOSTED),1)
72 #On Darwin architecture the assembly doesn't link correctly if these flags are set.
73 ifeq ($(shell uname | grep -c "Darwin"),1)
78 #use hosted specific map flags
79 $(1)_MAP_FLAGS = $$(MAP_FLAGS_HOST)
81 #Handle library creation
82 ifeq ($$($(1)_MAKELIB),1)
83 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1).a
85 #Otherwise in hosted application we need only executable file.
86 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1)
89 #use embedded specific map flags
90 $(1)_MAP_FLAGS = $$(MAP_FLAGS_EMB)
91 #In embedded we need s19, hex and bin
92 $$(OUTDIR)/$(1).tgt : $$(OUTDIR)/$(1).s19 $$(OUTDIR)/$(1).hex $$(OUTDIR)/$(1).bin
95 $(1)_LDFLAGS += $$($(1)_MAP_FLAGS)
97 # In embedded systems the target CPU is needed,
98 # but there are different options on how to 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)
121 ifneq ($$($(1)_CROSS),)
122 #deprecated: use PREFIX, SUFFIX and HOSTED mechanism instead
123 $(1)_PREFIX = $$($(1)_CROSS)
127 $(1)_CC ?= $$($(1)_PREFIX)$$(CC)$$($(1)_SUFFIX)
128 $(1)_CXX ?= $$($(1)_PREFIX)$$(CXX)$$($(1)_SUFFIX)
129 $(1)_AS ?= $$($(1)_PREFIX)$$(AS)$$($(1)_SUFFIX)
130 $(1)_AR ?= $$($(1)_PREFIX)$$(AR)$$($(1)_SUFFIX)
131 $(1)_OBJCOPY ?= $$($(1)_PREFIX)$$(OBJCOPY)$$($(1)_SUFFIX)
132 $(1)_STRIP ?= $$($(1)_PREFIX)$$(STRIP)$$($(1)_SUFFIX)
133 ifneq ($$(strip $$($(1)_CXXSRC)),)
134 $(1)_LD = $$($(1)_PREFIX)$$(LDXX)$$($(1)_SUFFIX)
136 $(1)_LD = $$($(1)_PREFIX)$$(LD)$$($(1)_SUFFIX)
140 ifeq ($$($(1)_DEBUG),1)
141 # AVR is an harvard processor
142 # and needs debug module
143 # to be compiled in program memory
144 ifeq ($$(findstring avr, $$($(1)_PREFIX)),avr)
145 $(1)_DEBUGSRC = $(1)_PCSRC
147 $(1)_DEBUGSRC = $(1)_CSRC
150 $$($(1)_DEBUGSRC) += bertos/drv/kdebug.c
152 # Also add formatwr.c (printf) if not already present
153 ifneq ($$(findstring formatwr.c, $$($$($(1)_DEBUGSRC))),formatwr.c)
154 $$($(1)_DEBUGSRC) += bertos/mware/formatwr.c
157 # Also add hex.c (for printf) if not already present
158 ifneq ($$(findstring hex.c, $$($$($(1)_CSRC))),hex.c)
159 $$($(1)_CSRC) += bertos/mware/hex.c
162 $(1)_CFLAGS += -D_DEBUG
163 $(1)_CXXFLAGS += -D_DEBUG
165 $(1)_CFLAGS += -fomit-frame-pointer
166 $(1)_CXXFLAGS += -fomit-frame-pointer
169 $(1)_COBJ = $$(foreach file,$$($(1)_CSRC:%.c=%.o),$$(OBJDIR)/$(1)/$$(file))
170 $(1)_CXXOBJ = $$(foreach file,$$($(1)_CXXSRC:%.cpp=%.o),$$(OBJDIR)/$(1)/$$(file))
171 $(1)_PCOBJ = $$(foreach file,$$($(1)_PCSRC:%.c=%_P.o),$$(OBJDIR)/$(1)/$$(file))
172 $(1)_AOBJ = $$(foreach file,$$($(1)_ASRC:%.s=%.o),$$(OBJDIR)/$(1)/$$(file))
173 $(1)_CPPAOBJ = $$(foreach file,$$($(1)_CPPASRC:%.S=%.o),$$(OBJDIR)/$(1)/$$(file))
174 $(1)_OBJ := $$($(1)_COBJ) $$($(1)_CXXOBJ) $$($(1)_PCOBJ) $$($(1)_AOBJ) $$($(1)_CPPAOBJ)
175 $(1)_SRC := $$($(1)_CSRC) $$($(1)_CXXSRC) $$($(1)_PCSRC) $$($(1)_ASRC) $$($(1)_CPPASRC)
178 # Sometimes $(CC) is actually set to a C++ compiler in disguise, and it
179 # would whine if we passed it C-only flags. Checking for the presence of
180 # "++" in the name is a kludge that seems to work mostly.
181 ifeq (++,$$(findstring ++,$$($(1)_CC)))
182 $(1)_REAL_CFLAGS = $$(CXXFLAGS)
184 $(1)_REAL_CFLAGS = $$(CFLAGS)
187 # Compile: instructions to create assembler and/or object files from C source
188 $$($(1)_COBJ) : $$(OBJDIR)/$(1)/%.o : %.c
189 $L "$(1): Compiling $$< (C)"
190 @$$(MKDIR_P) $$(dir $$@)
191 $Q $$($(1)_CC) -c $$($(1)_REAL_CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$($$(*F)_CFLAGS) $$< -o $$@
193 # Compile: instructions to create assembler and/or object files from C++ source
194 $$($(1)_CXXOBJ) : $$(OBJDIR)/$(1)/%.o : %.cpp
195 $L "$(1): Compiling $$< (C++)"
196 @$$(MKDIR_P) $$(dir $$@)
197 $Q $$($(1)_CXX) -c $$(CXXFLAGS) $$($(1)_CXXFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$($$(*F)_CXXFLAGS) $$< -o $$@
199 # Generate assembly sources from C files (debug)
200 $$(OBJDIR)/$(1)/%.s : %.c
201 $L "$(1): Generating asm source $$<"
202 @$$(MKDIR_P) $$(dir $$@)
203 $Q $$($(1)_CC) -S $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$($$(*F)_CFLAGS) $$< -o $$@
205 # Generate special progmem variant of a source file
206 $$($(1)_PCOBJ) : $$(OBJDIR)/$(1)/%_P.o : %.c
207 $L "$(1): Compiling $$< (PROGMEM)"
208 @$$(MKDIR_P) $$(dir $$@)
209 $Q $$($(1)_CC) -c -D_PROGMEM $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$($$(*F)_CFLAGS) $$< -o $$@
211 # Assemble: instructions to create object file from assembler files
212 $$($(1)_AOBJ): $$(OBJDIR)/$(1)/%.o : %.s
213 $L "$(1): Assembling $$<"
214 @$$(MKDIR_P) $$(dir $$@)
215 $Q $$($(1)_AS) -c $$(ASFLAGS) $$($(1)_ASFLAGS) $$($$(*F)_ASFLAGS) $$< -o $$@
217 $$($(1)_CPPAOBJ): $$(OBJDIR)/$(1)/%.o : %.S
218 $L "$(1): Assembling with CPP $$<"
219 @$$(MKDIR_P) $$(dir $$@)
220 $Q $$($(1)_CC) -c $$(CPPAFLAGS) $$($(1)_CPPAFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$($$(*F)_CPPAFLAGS) $$< -o $$@
223 # Link: instructions to create elf output file from object files
224 $$(OUTDIR)/$(1).elf $$(OUTDIR)/$(1)_nostrip: bumprev $$($(1)_OBJ) $$($(1)_LDSCRIPT)
225 $L "$(1): Linking $$@"
226 @$$(MKDIR_P) $$(dir $$@)
227 $Q $$($(1)_LD) $$($(1)_OBJ) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
230 # Instructions to create a static library from object files
231 $$(OUTDIR)/$(1).a: bumprev $$($(1)_OBJ)
232 $L "$(1): Creating static library $$@"
233 @$$(MKDIR_P) $$(dir $$@)
234 $Q $$($(1)_AR) $$(ARFLAGS) $$($(1)_ARFLAGS) $$@ $$($(1)_OBJ)
237 $$(OUTDIR)/$(1): $$(OUTDIR)/$(1)_nostrip
238 $L "$(1): Generating stripped executable $$@"
239 $Q $$($(1)_STRIP) -o $$@ $$^
241 # Compile and link (program-at-a-time)
242 $$(OUTDIR)/$(1)_whole.elf: bumprev $$($(1)_SRC) $$($(1)_LDSCRIPT)
243 $L "$(1): Compiling and Linking whole program $$@"
244 @$$(MKDIR_P) $$(dir $$@)
245 $Q $$($(1)_CC) $$($(1)_SRC) $$(CFLAGS) $$($(1)_CFLAGS) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
249 flash_$(1): $(OUTDIR)/$(1).hex flash_$(1)_local
250 $L "$(1): Flashing target"
251 $Q if [ ! -f $$($(1)_FLASH_SCRIPT) ] ; then \
252 printf "CLDLG: No flash script found.\n" ; \
255 $Q if [ ! "$$($(1)_PROGRAMMER_TYPE)" == "none" ] ; then \
256 PROGRAMMER_CPU=$$($(1)_PROGRAMMER_CPU) PROGRAMMER_TYPE=$$($(1)_PROGRAMMER_TYPE) \
257 PROGRAMMER_PORT=$$($(1)_PROGRAMMER_PORT) IMAGE_FILE=$$< \
258 $$($(1)_FLASH_SCRIPT) ; \
260 printf "CLDLG: No programmer interface configured, see http://dev.bertos.org/wiki/ProgrammerInterface\n" ; \
264 .PHONY: flash_$(1)_local
267 .PHONY: stopflash_$(1)
269 $L "$(1): Stopping target flashing"
270 $Q if [ ! -f $$($(1)_STOPFLASH_SCRIPT) ] ; then \
271 printf "CLDLG: No stopflash script found.\n" ; \
274 $Q $$($(1)_STOPFLASH_SCRIPT) ;
279 debug_$(1): $(OUTDIR)/$(1).elf
280 $L "$(1): Debugging target"
281 $Q if [ ! -f $$($(1)_DEBUG_SCRIPT) ] ; then \
282 printf "CLDLG: No debug script found.\n" ; \
285 $Q if [ ! "$$($(1)_PROGRAMMER_TYPE)" == "none" ] ; then \
286 PROGRAMMER_CPU=$$($(1)_PROGRAMMER_CPU) PROGRAMMER_TYPE=$$($(1)_PROGRAMMER_TYPE) \
287 PROGRAMMER_PORT=$$($(1)_PROGRAMMER_PORT) GDB_PORT=3333 \
289 $$($(1)_DEBUG_SCRIPT) ; \
291 printf "CLDLG: No programmer interface configured, see http://dev.bertos.org/wiki/ProgrammerInterface\n" ; \
295 .PHONY: stopdebug_$(1)
297 $L "$(1): Stopping debugger"
298 $Q if [ ! -f $$($(1)_STOPDEBUG_SCRIPT) ] ; then \
299 printf "CLDLG: No stopdebug script found.\n" ; \
302 $Q $$($(1)_STOPDEBUG_SCRIPT) ;
306 if [ ! -z "$$($(1)_efuse)" ] ; then \
307 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; then \
308 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; \
311 if [ ! -z "$$($(1)_hfuse)" ] ; then \
312 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; then \
313 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; \
316 if [ ! -z "$$($(1)_lfuse)" ] ; then \
317 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; then \
318 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; \
321 if [ ! -z "$$($(1)_lock)" ] ; then \
322 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; then \
323 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; \
327 $$(OUTDIR)/$(1).hex: $$(OUTDIR)/$(1).elf
328 $$($(1)_OBJCOPY) -O ihex $$< $$@
330 $$(OUTDIR)/$(1).s19: $$(OUTDIR)/$(1).elf
331 $$($(1)_OBJCOPY) -O srec $$< $$@
333 $$(OUTDIR)/$(1).bin: $$(OUTDIR)/$(1).elf
334 $$($(1)_OBJCOPY) -O binary $$< $$@
336 $$(OUTDIR)/$(1).obj: $$(OUTDIR)/$(1).elf
337 $$($(1)_OBJCOPY) -O avrobj $$< $$@
339 $$(OUTDIR)/$(1).rom: $$(OUTDIR)/$(1).elf
340 $$($(1)_OBJCOPY) -O $$(FORMAT) $$< $$@
341 # $$($(1)_OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" -O $$(FORMAT) $$< $$(@:.rom=.eep)
345 # Generate build rules for all targets
346 $(foreach t,$(TRG),$(eval $(call build_target,$(t))))
348 # Generate Qt's moc files from headers
349 # NOTE: moc totally sucks and can generate empty files for some error conditions,
350 # leading to puzzling linker errors. Kill 'em and abort build.
353 if [ ! -s $< ]; then \
360 $(COFFCONVERT) -O coff-ext-avr $< $@
361 # $(COFFCONVERT) -O coff-avr $< $@ # For use with AVRstudio 3
363 #make instruction to delete created files
365 clean: clean-recursive
369 $(RECURSIVE_TARGETS):
370 @target=`echo $@ | sed s/-recursive//`; \
371 for dir in $(SUBDIRS); do \
372 if [ -e $$dir/configure.in ] || [ -e $$dir/configure.ac ] && [ ! -x $$dir/configure ]; then \
373 echo "Running autogen.sh in $$dir..."; \
374 ( cd $$dir && chmod a+x autogen.sh && ./autogen.sh && rm -f Makefile || exit 1 ); \
376 if [ ! -e $$dir/Makefile ]; then \
377 if [ -e "$$dir/build-$(ARCH)" ]; then \
378 echo "Running build script in $$dir..."; \
379 ( cd $$dir && chmod a+x build && ./build || exit 1 ); \
381 echo "Running configure in $$dir..."; \
382 ( cd $$dir && ./configure --prefix=$(PREFIX) || exit 1 ); \
385 $(MAKE) -C $$dir $$target || exit 1; \
388 BUILDREV_H = buildrev.h
392 @if [ -e bertos/verstag.c ]; then \
394 if [ -f $(BUILDREV_H) ]; then \
395 buildnr=`sed <"$(BUILDREV_H)" -n -e 's/#define VERS_BUILD \([0-9][0-9]*\)/\1/p'`; \
397 buildnr=`expr $$buildnr + 1`; \
398 buildhost=`hostname | sed -n -e '1h;2,$$H;$${g;s/\n//g;p;}'`; \
399 echo "#define VERS_BUILD $$buildnr" >"$(BUILDREV_H)"; \
400 echo "#define VERS_HOST \"$$buildhost\"" >>"$(BUILDREV_H)"; \
401 echo "Building revision $$buildnr"; \
405 # Include dependencies
406 ifneq ($(strip $(OBJ)),)
407 -include $(OBJ:%.o=%.d)