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
30 CFLAGS += -Wundef -D__x86_64__=1 -D__unix__=1 -D__linux__=1 -D__STDC_VERSION__=199901L
33 # Initialize $(top_srcdir) with current directory, unless it was already initialized
34 top_srcdir ?= $(shell pwd)
37 TRG_ELF = $(TRG:%=$(OUTDIR)/%.elf)
38 TRG_S19 = $(TRG:%=$(OUTDIR)/%.s19)
39 TRG_HEX = $(TRG:%=$(OUTDIR)/%.hex)
40 TRG_BIN = $(TRG:%=$(OUTDIR)/%.bin)
41 TRG_ROM = $(TRG:%=$(OUTDIR)/%.rom)
42 TRG_COF = $(TRG:%=$(OUTDIR)/%.cof)
45 RECURSIVE_TARGETS = all-recursive install-recursive clean-recursive
49 ifeq ($(EMBEDDED_TARGET),1)
50 all:: all-recursive $(TRG_S19) $(TRG_HEX) $(TRG_BIN)
52 all:: all-recursive $(TRG_ELF)
55 # Generate project documentation
62 ifneq ($$(strip $$($(1)_MCU)),)
63 # Define all project specific object files
64 $(1)_CFLAGS += -mmcu=$$($(1)_MCU)
65 $(1)_CXXFLAGS += -mmcu=$$($(1)_MCU)
66 $(1)_ASFLAGS += -mmcu=$$($(1)_MCU)
67 $(1)_CPPAFLAGS += -mmcu=$$($(1)_MCU)
68 $(1)_LDFLAGS += -mmcu=$$($(1)_MCU)
70 ifneq ($$(strip $$($(1)_CPU)),)
71 # Define all project specific object files
72 $(1)_CFLAGS += -mcpu=$$($(1)_CPU)
73 $(1)_CXXFLAGS += -mcpu=$$($(1)_CPU)
74 $(1)_ASFLAGS += -mcpu=$$($(1)_CPU)
75 $(1)_CPPAFLAGS += -mcpu=$$($(1)_CPU)
76 $(1)_LDFLAGS += -mcpu=$$($(1)_CPU)
78 ifneq ($$(strip $$($(1)_LDSCRIPT)),)
79 $(1)_LDFLAGS += -Wl,-T$$($(1)_LDSCRIPT)
82 $(1)_COBJ = $$(foreach file,$$($(1)_CSRC:%.c=%.o),$$(OBJDIR)/$(1)/$$(file))
83 $(1)_CXXOBJ = $$(foreach file,$$($(1)_CXXSRC:%.cpp=%.o),$$(OBJDIR)/$(1)/$$(file))
84 $(1)_PCOBJ = $$(foreach file,$$($(1)_PCSRC:%.c=%_P.o),$$(OBJDIR)/$(1)/$$(file))
85 $(1)_AOBJ = $$(foreach file,$$($(1)_ASRC:%.s=%.o),$$(OBJDIR)/$(1)/$$(file))
86 $(1)_CPPAOBJ = $$(foreach file,$$($(1)_CPPASRC:%.S=%.o),$$(OBJDIR)/$(1)/$$(file))
87 $(1)_OBJ := $$($(1)_COBJ) $$($(1)_CXXOBJ) $$($(1)_PCOBJ) $$($(1)_AOBJ) $$($(1)_CPPAOBJ)
88 $(1)_SRC := $$($(1)_CSRC) $$($(1)_CXXSRC) $$($(1)_PCSRC) $$($(1)_ASRC) $$($(1)_CPPASRC)
91 # Compile: instructions to create assembler and/or object files from C source
92 $$($(1)_COBJ) : $$(OBJDIR)/$(1)/%.o : %.c
93 $L "$(1): Compiling $$< (C)"
94 @$$(MKDIR_P) $$(dir $$@)
95 $Q $$(CC) -c $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
97 # Compile: instructions to create assembler and/or object files from C++ source
98 $$($(1)_CXXOBJ) : $$(OBJDIR)/$(1)/%.o : %.cpp
99 $L "$(1): Compiling $$< (C++)"
100 @$$(MKDIR_P) $$(dir $$@)
101 $Q $$(CXX) -c $$(CXXFLAGS) $$($(1)_CXXFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
103 # Generate assembly sources from C files (debug)
104 $$(OBJDIR)/$(1)/%.s : %.c
105 $L "$(1): Generating asm source $$<"
106 @$$(MKDIR_P) $$(dir $$@)
107 $Q $$(CC) -S $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$< -o $$@
109 # Generate special progmem variant of a source file
110 $$($(1)_PCOBJ) : $$(OBJDIR)/$(1)/%_P.o : %.c
111 $L "$(1): Compiling $$< (PROGMEM)"
112 @$$(MKDIR_P) $$(dir $$@)
113 $Q $$(CC) -c -D_PROGMEM $$(CFLAGS) $$($(1)_CFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
115 # Assemble: instructions to create object file from assembler files
116 $$($(1)_AOBJ): $$(OBJDIR)/$(1)/%.o : %.s
117 $L "$(1): Assembling $$<"
118 @$$(MKDIR_P) $$(dir $$@)
119 $Q $$(AS) -c $$(ASFLAGS) $$($(1)_ASFLAGS) $$< -o $$@
121 $$($(1)_CPPAOBJ): $$(OBJDIR)/$(1)/%.o : %.S
122 $L "$(1): Assembling with CPP $$<"
123 @$$(MKDIR_P) $$(dir $$@)
124 $Q $$(CC) -c $$(CPPAFLAGS) $$($(1)_CPPAFLAGS) $$($(1)_CPPFLAGS) $$(CPPFLAGS) $$< -o $$@
127 # Link: instructions to create elf output file from object files
128 $$(OUTDIR)/$(1).elf: bumprev $$($(1)_OBJ) $$($(1)_LDSCRIPT)
129 $L "$(1): Linking $$(OUTDIR)/$(1)"
130 @$$(MKDIR_P) $$(dir $$@)
131 ifeq ($(EMBEDDED_TARGET), 1)
132 $Q $$(LD) $$($(1)_OBJ) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
134 $Q $$(LD) $$($(1)_OBJ) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$(OUTDIR)/$(1)_nostrip
135 $Q $$(STRIP) -o $$(OUTDIR)/$(1) $$(OUTDIR)/$(1)_nostrip
138 # Compile and link (program-at-a-time)
139 $$(OUTDIR)/$(1)_whole.elf: bumprev $$($(1)_SRC) $$($(1)_LDSCRIPT)
140 $L "$(1): Compiling and Linking whole program $$@"
141 @$$(MKDIR_P) $$(dir $$@)
142 $Q $$(CC) $$($(1)_SRC) $$(CFLAGS) $$($(1)_CFLAGS) $$(LIB) $$(LDFLAGS) $$($(1)_LDFLAGS) -o $$@
145 # NOTE: we retry in case of failure because the STK500 programmer is crappy
147 flash_$(1): $(OUTDIR)/$(1).s19 flash_$(1)_local
148 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; then \
149 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U flash:w:$$< ; \
151 #avarice --mkII -j usb --erase --program --verify --file images/triface.elf
153 .PHONY: flash_$(1)_local
158 if [ ! -z "$$($(1)_efuse)" ] ; then \
159 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; then \
160 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U efuse:w:$$($(1)_efuse):m ; \
163 if [ ! -z "$$($(1)_hfuse)" ] ; then \
164 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; then \
165 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U hfuse:w:$$($(1)_hfuse):m ; \
168 if [ ! -z "$$($(1)_lfuse)" ] ; then \
169 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; then \
170 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lfuse:w:$$($(1)_lfuse):m ; \
173 if [ ! -z "$$($(1)_lock)" ] ; then \
174 if ! $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; then \
175 $(AVRDUDE) $(DPROG) -p $$($(1)_MCU) -U lock:w:$$($(1)_lock):m ; \
180 # Generate build rules for all targets
181 $(foreach t,$(TRG),$(eval $(call build_target,$(t))))
183 # Generate Qt's moc files from headers
184 # NOTE: moc totally sucks and can generate empty files for some error conditions,
185 # leading to puzzling linker errors. Kill 'em and abort build.
194 $(OBJCOPY) -O ihex $< $@
197 $(OBJCOPY) -O srec $< $@
200 $(OBJCOPY) -O binary $< $@
203 $(OBJCOPY) -O avrobj $< $@
206 $(OBJCOPY) -O $(FORMAT) $< $@
207 # $(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" -O $(FORMAT) $< $(@:.rom=.eep)
210 $(COFFCONVERT) -O coff-ext-avr $< $@
211 # $(COFFCONVERT) -O coff-avr $< $@ # For use with AVRstudio 3
213 #make instruction to delete created files
214 clean: clean-recursive
218 $(RECURSIVE_TARGETS):
219 @target=`echo $@ | sed s/-recursive//`; \
220 for dir in $(SUBDIRS); do \
221 if [ -e $$dir/configure.in ] || [ -e $$dir/configure.ac ] && [ ! -x $$dir/configure ]; then \
222 echo "Running autogen.sh in $$dir..."; \
223 ( cd $$dir && chmod a+x autogen.sh && ./autogen.sh && rm -f Makefile || exit 1 ); \
225 if [ ! -e $$dir/Makefile ]; then \
226 if [ -e "$$dir/build-$(ARCH)" ]; then \
227 echo "Running build script in $$dir..."; \
228 ( cd $$dir && chmod a+x build && ./build || exit 1 ); \
230 echo "Running configure in $$dir..."; \
231 ( cd $$dir && ./configure --prefix=$(PREFIX) || exit 1 ); \
234 $(MAKE) -C $$dir $$target || exit 1; \
237 BUILDREV_H = buildrev.h
239 ifeq ($(shell [ -e verstag.c ] && echo yes),yes)
243 if [ -f $(BUILDREV_H) ]; then \
244 buildnr=`sed <"$(BUILDREV_H)" -n -e 's/#define VERS_BUILD \([0-9][0-9]*\)/\1/p'`; \
246 buildnr=`expr $$buildnr + 1`; \
247 buildhost=`hostname`; \
248 echo "#define VERS_BUILD $$buildnr" >"$(BUILDREV_H)"; \
249 echo "#define VERS_HOST \"$$buildhost\"" >>"$(BUILDREV_H)"; \
250 echo "Building revision $$buildnr"
257 # Include dependencies
258 ifneq ($(strip $(OBJ)),)
259 -include $(OBJ:%.o=%.d)