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)
40 TRG_ELF = $(TRG:%=$(OUTDIR)/%.elf)
41 TRG_S19 = $(TRG:%=$(OUTDIR)/%.s19)
42 TRG_HEX = $(TRG:%=$(OUTDIR)/%.hex)
43 TRG_BIN = $(TRG:%=$(OUTDIR)/%.bin)
44 TRG_ROM = $(TRG:%=$(OUTDIR)/%.rom)
45 TRG_COF = $(TRG:%=$(OUTDIR)/%.cof)
46 TRG_EXE = $(TRG:%=$(OUTDIR)/%)
49 RECURSIVE_TARGETS = all-recursive install-recursive clean-recursive
53 ifeq ($(EMBEDDED_TARGET),1)
54 all:: all-recursive $(TRG_S19) $(TRG_HEX) $(TRG_BIN)
56 all:: all-recursive $(TRG_EXE)
59 # Generate project documentation
62 $L "Building documentation"
68 $L "Rebuilding C tags database"
69 $Q ctags -R --exclude=doc
74 $L "Running testsuite"
79 ifneq ($$(strip $$($(1)_MCU)),)
80 # Define all project specific object files
81 $(1)_CFLAGS += -mmcu=$$($(1)_MCU)
82 $(1)_CXXFLAGS += -mmcu=$$($(1)_MCU)
83 $(1)_ASFLAGS += -mmcu=$$($(1)_MCU)
84 $(1)_CPPAFLAGS += -mmcu=$$($(1)_MCU)
85 $(1)_LDFLAGS += -mmcu=$$($(1)_MCU)
87 ifneq ($$(strip $$($(1)_CPU)),)
88 # Define all project specific object files
89 $(1)_CFLAGS += -mcpu=$$($(1)_CPU)
90 $(1)_CXXFLAGS += -mcpu=$$($(1)_CPU)
91 $(1)_ASFLAGS += -mcpu=$$($(1)_CPU)
92 $(1)_CPPAFLAGS += -mcpu=$$($(1)_CPU)
93 $(1)_LDFLAGS += -mcpu=$$($(1)_CPU)
95 ifneq ($$(strip $$($(1)_LDSCRIPT)),)
96 $(1)_LDFLAGS += -Wl,-T$$($(1)_LDSCRIPT)
99 $(1)_CC = $$($(1)_CROSS)$$(CC)
100 $(1)_CXX = $$($(1)_CROSS)$$(CXX)
101 $(1)_AS = $$($(1)_CROSS)$$(AS)
102 $(1)_OBJCOPY = $$($(1)_CROSS)$$(OBJCOPY)
103 $(1)_STRIP = $$($(1)_CROSS)$$(STRIP)
105 $(1)_COBJ = $$(foreach file,$$($(1)_CSRC:%.c=%.o),$$(OBJDIR)/$(1)/$$(file))
106 $(1)_CXXOBJ = $$(foreach file,$$($(1)_CXXSRC:%.cpp=%.o),$$(OBJDIR)/$(1)/$$(file))
107 $(1)_PCOBJ = $$(foreach file,$$($(1)_PCSRC:%.c=%_P.o),$$(OBJDIR)/$(1)/$$(file))
108 $(1)_AOBJ = $$(foreach file,$$($(1)_ASRC:%.s=%.o),$$(OBJDIR)/$(1)/$$(file))
109 $(1)_CPPAOBJ = $$(foreach file,$$($(1)_CPPASRC:%.S=%.o),$$(OBJDIR)/$(1)/$$(file))
110 $(1)_OBJ := $$($(1)_COBJ) $$($(1)_CXXOBJ) $$($(1)_PCOBJ) $$($(1)_AOBJ) $$($(1)_CPPAOBJ)
111 $(1)_SRC := $$($(1)_CSRC) $$($(1)_CXXSRC) $$($(1)_PCSRC) $$($(1)_ASRC) $$($(1)_CPPASRC)
114 ifneq ($$(strip $$($(1)_CXXSRC)),)
115 $(1)_LD = $$($(1)_CROSS)$$(LDXX)
117 $(1)_LD = $$($(1)_CROSS)$$(LD)
120 # Compile: instructions to create assembler and/or object files from C source
121 $$($(1)_COBJ) : $$(OBJDIR)/$(1)/%.o : %.c
122 $L "$(1): Compiling $$< (C)"
123 @$$(MKDIR_P) $$(dir $$@)
124 $Q $$($(1)_CC) -c $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
126 # Compile: instructions to create assembler and/or object files from C++ source
127 $$($(1)_CXXOBJ) : $$(OBJDIR)/$(1)/%.o : %.cpp
128 $L "$(1): Compiling $$< (C++)"
129 @$$(MKDIR_P) $$(dir $$@)
130 $Q $$($(1)_CXX) -c $$(CXXFLAGS) $$($(1)_CXXFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
132 # Generate assembly sources from C files (debug)
133 $$(OBJDIR)/$(1)/%.s : %.c
134 $L "$(1): Generating asm source $$<"
135 @$$(MKDIR_P) $$(dir $$@)
136 $Q $$($(1)_CC) -S $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$< -o $$@
138 # Generate special progmem variant of a source file
139 $$($(1)_PCOBJ) : $$(OBJDIR)/$(1)/%_P.o : %.c
140 $L "$(1): Compiling $$< (PROGMEM)"
141 @$$(MKDIR_P) $$(dir $$@)
142 $Q $$($(1)_CC) -c -D_PROGMEM $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
144 # Assemble: instructions to create object file from assembler files
145 $$($(1)_AOBJ): $$(OBJDIR)/$(1)/%.o : %.s
146 $L "$(1): Assembling $$<"
147 @$$(MKDIR_P) $$(dir $$@)
148 $Q $$($(1)_AS) -c $$(ASFLAGS) $$($(1)_ASFLAGS) $$< -o $$@
150 $$($(1)_CPPAOBJ): $$(OBJDIR)/$(1)/%.o : %.S
151 $L "$(1): Assembling with CPP $$<"
152 @$$(MKDIR_P) $$(dir $$@)
153 $Q $$($(1)_CC) -c $$(CPPAFLAGS) $$($(1)_CPPAFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
156 # Link: instructions to create elf output file from object files
157 $$(OUTDIR)/$(1).elf $$(OUTDIR)/$(1)_nostrip: bumprev $$($(1)_OBJ) $$($(1)_LDSCRIPT)
158 $L "$(1): Linking $$@"
159 @$$(MKDIR_P) $$(dir $$@)
160 $Q $$($(1)_LD) $$($(1)_OBJ) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
163 $$(OUTDIR)/$(1): $$(OUTDIR)/$(1)_nostrip
164 $L "$(1): Generating stripped executable $$@"
165 $Q $$($(1)_STRIP) -o $$@ $$^
167 # Compile and link (program-at-a-time)
168 $$(OUTDIR)/$(1)_whole.elf: bumprev $$($(1)_SRC) $$($(1)_LDSCRIPT)
169 $L "$(1): Compiling and Linking whole program $$@"
170 @$$(MKDIR_P) $$(dir $$@)
171 $Q $$($(1)_CC) $$($(1)_SRC) $$(CFLAGS) $$($(1)_CFLAGS) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
174 # NOTE: we retry in case of failure because the STK500 programmer is crappy
176 flash_$(1): $(OUTDIR)/$(1).s19 flash_$(1)_local
177 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; then \
178 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; \
180 #avarice --mkII -j usb --erase --program --verify --file images/triface.elf
182 .PHONY: flash_$(1)_local
187 if [ ! -z "$$($(1)_efuse)" ] ; then \
188 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; then \
189 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; \
192 if [ ! -z "$$($(1)_hfuse)" ] ; then \
193 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; then \
194 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; \
197 if [ ! -z "$$($(1)_lfuse)" ] ; then \
198 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; then \
199 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; \
202 if [ ! -z "$$($(1)_lock)" ] ; then \
203 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; then \
204 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; \
208 $$(OUTDIR)/$(1).hex: $$(OUTDIR)/$(1).elf
209 $$($(1)_OBJCOPY) -O ihex $$< $$@
211 $$(OUTDIR)/$(1).s19: $$(OUTDIR)/$(1).elf
212 $$($(1)_OBJCOPY) -O srec $$< $$@
214 $$(OUTDIR)/$(1).bin: $$(OUTDIR)/$(1).elf
215 $$($(1)_OBJCOPY) -O binary $$< $$@
217 $$(OUTDIR)/$(1).obj: $$(OUTDIR)/$(1).elf
218 $$($(1)_OBJCOPY) -O avrobj $$< $$@
220 $$(OUTDIR)/$(1).rom: $$(OUTDIR)/$(1).elf
221 $$($(1)_OBJCOPY) -O $$(FORMAT) $$< $$@
222 # $$($(1)_OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" -O $$(FORMAT) $$< $$(@:.rom=.eep)
226 # Generate build rules for all targets
227 $(foreach t,$(TRG),$(eval $(call build_target,$(t))))
229 # Generate Qt's moc files from headers
230 # NOTE: moc totally sucks and can generate empty files for some error conditions,
231 # leading to puzzling linker errors. Kill 'em and abort build.
234 if [ ! -s $< ]; then \
241 $(COFFCONVERT) -O coff-ext-avr $< $@
242 # $(COFFCONVERT) -O coff-avr $< $@ # For use with AVRstudio 3
244 #make instruction to delete created files
245 clean: clean-recursive
249 $(RECURSIVE_TARGETS):
250 @target=`echo $@ | sed s/-recursive//`; \
251 for dir in $(SUBDIRS); do \
252 if [ -e $$dir/configure.in ] || [ -e $$dir/configure.ac ] && [ ! -x $$dir/configure ]; then \
253 echo "Running autogen.sh in $$dir..."; \
254 ( cd $$dir && chmod a+x autogen.sh && ./autogen.sh && rm -f Makefile || exit 1 ); \
256 if [ ! -e $$dir/Makefile ]; then \
257 if [ -e "$$dir/build-$(ARCH)" ]; then \
258 echo "Running build script in $$dir..."; \
259 ( cd $$dir && chmod a+x build && ./build || exit 1 ); \
261 echo "Running configure in $$dir..."; \
262 ( cd $$dir && ./configure --prefix=$(PREFIX) || exit 1 ); \
265 $(MAKE) -C $$dir $$target || exit 1; \
268 BUILDREV_H = buildrev.h
270 ifeq ($(shell [ -e bertos/verstag.c ] && echo yes),yes)
274 if [ -f $(BUILDREV_H) ]; then \
275 buildnr=`sed <"$(BUILDREV_H)" -n -e 's/#define VERS_BUILD \([0-9][0-9]*\)/\1/p'`; \
277 buildnr=`expr $$buildnr + 1`; \
278 buildhost=`hostname | sed -n -e '1h;2,$$H;$${g;s/\n//g;p;}'`; \
279 echo "#define VERS_BUILD $$buildnr" >"$(BUILDREV_H)"; \
280 echo "#define VERS_HOST \"$$buildhost\"" >>"$(BUILDREV_H)"; \
281 echo "Building revision $$buildnr"
288 # Include dependencies
289 ifneq ($(strip $(OBJ)),)
290 -include $(OBJ:%.o=%.d)