1 # Hey Emacs, this is a -*- makefile -*-
2 #----------------------------------------------------------------------------
3 # WinAVR Makefile Template written by Eric B. Weddington, Jg Wunsch, et al.
5 # Released to the Public Domain
7 # Additional material for this makefile was written by:
18 # Enable vpath seraching for source files only
19 # Without this, output files, could be read from the wrong .build directories
21 vpath %.c $(VPATH_SRC)
22 vpath %.h $(VPATH_SRC)
23 vpath %.cpp $(VPATH_SRC)
24 vpath %.hpp $(VPATH_SRC)
25 vpath %.S $(VPATH_SRC)
28 # Convert all SRC to OBJ
30 $(patsubst %.c,$1/%.o,$(patsubst %.cpp,$1/%.o,$(patsubst %.S,$1/%.o,$($1_SRC))))
32 $(foreach OUTPUT,$(OUTPUTS),$(eval $(OUTPUT)_OBJ +=$(call OBJ_FROM_SRC,$(OUTPUT))))
34 # Define a list of all objects
35 OBJ := $(foreach OUTPUT,$(OUTPUTS),$($(OUTPUT)_OBJ))
37 MASTER_OUTPUT := $(firstword $(OUTPUTS))
41 # Output format. (can be srec, ihex, binary)
44 # Optimization level, can be [0, 1, 2, 3, s].
45 # 0 = turn off optimization. s = optimize for size.
46 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
52 # Compiler flag to set the C Standard level.
54 # gnu89 = c89 plus GCC extensions
55 # c99 = ISO C99 standard (not yet fully implemented)
56 # gnu99 = c99 plus GCC extensions
57 CSTANDARD = -std=gnu99
60 # Place -D or -U options here for C sources
64 # Place -D or -U options here for ASM sources
68 # Place -D or -U options here for C++ sources
69 #CPPDEFS += -D__STDC_LIMIT_MACROS
70 #CPPDEFS += -D__STDC_CONSTANT_MACROS
76 #---------------- Compiler Options C ----------------
77 # -g*: generate debugging information
78 # -O*: optimization level
79 # -f...: tuning, see GCC manual and avr-libc documentation
80 # -Wall...: warning level
81 # -Wa,...: tell GCC to pass this to the assembler.
82 # -adhlns...: create assembler listing
88 ifeq ("$(shell echo "int main(){}" | $(CC) -fdiagnostics-color -x c - -o /dev/null 2>&1)", "")
89 CFLAGS+= -fdiagnostics-color
93 CFLAGS += -Wstrict-prototypes
94 #CFLAGS += -mshort-calls
95 #CFLAGS += -fno-unit-at-a-time
97 #CFLAGS += -Wunreachable-code
98 #CFLAGS += -Wsign-compare
99 CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
100 CFLAGS += $(CSTANDARD)
103 #---------------- Compiler Options C++ ----------------
104 # -g*: generate debugging information
105 # -O*: optimization level
106 # -f...: tuning, see GCC manual and avr-libc documentation
107 # -Wall...: warning level
108 # -Wa,...: tell GCC to pass this to the assembler.
109 # -adhlns...: create assembler listing
110 CPPFLAGS += -g$(DEBUG)
111 CPPFLAGS += $(CPPDEFS)
113 # to supress "warning: only initialized variables can be placed into program memory area"
117 #CPPFLAGS += -mshort-calls
118 #CPPFLAGS += -fno-unit-at-a-time
119 #CPPFLAGS += -Wstrict-prototypes
120 #CPPFLAGS += -Wunreachable-code
121 #CPPFLAGS += -Wsign-compare
122 CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
123 #CPPFLAGS += $(CSTANDARD)
125 #---------------- Assembler Options ----------------
126 # -Wa,...: tell GCC to pass this to the assembler.
127 # -adhlns: create listing
128 # -gstabs: have the assembler create line number information; note that
129 # for use in COFF files, additional information about filenames
130 # and function names needs to be present in the assembler source
131 # files -- see avr-libc docs [FIXME: not yet described there]
132 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
133 # dump that will be displayed for a given single line of source input.
135 ASFLAGS += -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
137 #---------------- Library Options ----------------
138 # Minimalistic printf version
139 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
141 # Floating point printf version (requires MATH_LIB = -lm below)
142 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
144 # If this is left blank, then it will use the Standard printf version.
146 #PRINTF_LIB = $(PRINTF_LIB_MIN)
147 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
150 # Minimalistic scanf version
151 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
153 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
154 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
156 # If this is left blank, then it will use the Standard scanf version.
158 #SCANF_LIB = $(SCANF_LIB_MIN)
159 #SCANF_LIB = $(SCANF_LIB_FLOAT)
165 #---------------- Linker Options ----------------
166 # -Wl,...: tell GCC to pass this to linker.
167 # -Map: create map file
168 # --cref: add cross reference to map file
170 # Comennt out "--relax" option to avoid a error such:
171 # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
173 LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref
174 #LDFLAGS += -Wl,--relax
175 LDFLAGS += $(EXTMEMOPTS)
176 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
177 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
178 #LDFLAGS += -T linker_script.x
179 # You can give EXTRALDFLAGS at 'make' command line.
180 LDFLAGS += $(EXTRALDFLAGS)
182 # Define programs and commands.
188 SECHO = $(SILENT) || echo
191 # Compiler flags to generate dependency files.
192 #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
193 GENDEPFLAGS = -MMD -MP -MF $(patsubst %.o,%.td,$@)
196 # Combine all necessary flags and optional flags.
197 # Add target processor to flags.
198 # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
199 ALL_CFLAGS = $(MCUFLAGS) $(CFLAGS) $(EXTRAFLAGS)
200 ALL_CPPFLAGS = $(MCUFLAGS) -x c++ $(CPPFLAGS) $(EXTRAFLAGS)
201 ALL_ASFLAGS = $(MCUFLAGS) -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
203 MOVE_DEP = mv -f $(patsubst %.o,%.td,$@) $(patsubst %.o,%.d,$@)
208 # Change the build target to build a HEX file or a library.
210 #build: elf hex eep lss sym
214 elf: $(BUILD_DIR)/$(TARGET).elf
215 hex: $(BUILD_DIR)/$(TARGET).hex
216 eep: $(BUILD_DIR)/$(TARGET).eep
217 lss: $(BUILD_DIR)/$(TARGET).lss
218 sym: $(BUILD_DIR)/$(TARGET).sym
219 LIBNAME=lib$(TARGET).a
222 # Display size of file.
223 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
224 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
225 ELFSIZE = $(SIZE) $(BUILD_DIR)/$(TARGET).elf
228 @if test -f $(TARGET).hex; then $(SECHO) $(MSG_SIZE_BEFORE); $(SILENT) || $(HEXSIZE); \
229 2>/dev/null; $(SECHO); fi
231 sizeafter: $(BUILD_DIR)/$(TARGET).hex
232 @if test -f $(TARGET).hex; then $(SECHO); $(SECHO) $(MSG_SIZE_AFTER); $(SILENT) || $(HEXSIZE); \
233 2>/dev/null; $(SECHO); fi
234 # test file sizes eventually
235 # @if [[ $($(SIZE) --target=$(FORMAT) $(TARGET).hex | $(AWK) 'NR==2 {print "0x"$5}') -gt 0x200 ]]; then $(SECHO) "File is too big!"; fi
237 # Display compiler version information.
239 @$(SILENT) || $(CC) --version
241 # Create final output files (.hex, .eep) from ELF output file.
243 @$(SILENT) || printf "$(MSG_FLASH) $@" | $(AWK_CMD)
244 $(eval CMD=$(HEX) $< $@)
246 @if $(AUTOGEN); then \
247 $(SILENT) || printf "Copying $(TARGET).hex to keymaps/$(KEYMAP)/$(KEYBOARD)_$(KEYMAP).hex\n"; \
248 $(COPY) $@ $(KEYMAP_PATH)/$(KEYBOARD)_$(KEYMAP).hex; \
250 $(COPY) $@ $(TARGET).hex; \
254 @$(SILENT) || printf "$(MSG_EEPROM) $@" | $(AWK_CMD)
255 $(eval CMD=$(EEP) $< $@ || exit 0)
258 # Create extended listing file from ELF output file.
260 @$(SILENT) || printf "$(MSG_EXTENDED_LISTING) $@" | $(AWK_CMD)
261 $(eval CMD=$(OBJDUMP) -h -S -z $< > $@)
264 # Create a symbol table from ELF output file.
266 @$(SILENT) || printf "$(MSG_SYMBOL_TABLE) $@" | $(AWK_CMD)
267 $(eval CMD=$(NM) -n $< > $@ )
271 @$(SILENT) || printf "$(MSG_BIN) $@" | $(AWK_CMD)
272 $(eval CMD=$(BIN) $< $@ || exit 0)
275 BEGIN = gccversion sizebefore
277 # Link: create ELF output file from object files.
278 .SECONDARY : $(BUILD_DIR)/$(TARGET).elf
280 # Note the obj.txt depeendency is there to force linking if a source file is deleted
281 %.elf: $(OBJ) $(MASTER_OUTPUT)/cflags.txt $(MASTER_OUTPUT)/ldflags.txt $(MASTER_OUTPUT)/obj.txt | $(BEGIN)
282 @$(SILENT) || printf "$(MSG_LINKING) $@" | $(AWK_CMD)
283 $(eval CMD=$(CC) $(ALL_CFLAGS) $(filter-out %.txt,$^) --output $@ $(LDFLAGS))
288 $1_INCFLAGS := $$(patsubst %,-I%,$$($1_INC))
290 $1_CONFIG_FLAGS += -include $$($1_CONFIG)
292 $1_CFLAGS = $$(ALL_CFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
293 $1_CPPFLAGS= $$(ALL_CPPFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
294 $1_ASFLAGS= $$(ALL_ASFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
296 # Compile: create object files from C source files.
297 $1/%.o : %.c $1/%.d $1/cflags.txt $1/compiler.txt | $(BEGIN)
299 @$$(SILENT) || printf "$$(MSG_COMPILING) $$<" | $$(AWK_CMD)
300 $$(eval CMD := $$(CC) -c $$($1_CFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
303 # Compile: create object files from C++ source files.
304 $1/%.o : %.cpp $1/%.d $1/cppflags.txt $1/compiler.txt | $(BEGIN)
306 @$$(SILENT) || printf "$$(MSG_COMPILING_CPP) $$<" | $$(AWK_CMD)
307 $$(eval CMD=$$(CC) -c $$($1_CPPFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
310 # Assemble: create object files from assembler source files.
311 $1/%.o : %.S $1/asflags.txt $1/compiler.txt | $(BEGIN)
313 @$(SILENT) || printf "$$(MSG_ASSEMBLING) $$<" | $$(AWK_CMD)
314 $$(eval CMD=$$(CC) -c $$($1_ASFLAGS) $$< -o $$@)
319 $1/cflags.txt: $1/force
320 echo '$$($1_CFLAGS)' | cmp -s - $$@ || echo '$$($1_CFLAGS)' > $$@
322 $1/cppflags.txt: $1/force
323 echo '$$($1_CPPFLAGS)' | cmp -s - $$@ || echo '$$($1_CPPFLAGS)' > $$@
325 $1/asflags.txt: $1/force
326 echo '$$($1_ASFLAGS)' | cmp -s - $$@ || echo '$$($1_ASFLAGS)' > $$@
328 $1/compiler.txt: $1/force
329 $$(CC) --version | cmp -s - $$@ || $$(CC) --version > $$@
332 $(MASTER_OUTPUT)/obj.txt: $(MASTER_OUTPUT)/force
333 echo '$(OBJ)' | cmp -s - $$@ || echo '$(OBJ)' > $$@
335 $(MASTER_OUTPUT)/ldflags.txt: $(MASTER_OUTPUT)/force
336 echo '$(LDFLAGS)' | cmp -s - $$@ || echo '$(LDFLAGS)' > $$@
339 # We have to use static rules for the .d files for some reason
340 DEPS = $(patsubst %.o,%.d,$(OBJ))
343 # Empty rule to force recompilation if the .d file is missing
347 $(foreach OUTPUT,$(OUTPUTS),$(eval $(call GEN_OBJRULE,$(OUTPUT))))
349 # Create preprocessed source for use in sending a bug report.
351 $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
353 # Target: clean project.
355 $(foreach OUTPUT,$(OUTPUTS), $(REMOVE) -r $(OUTPUT) 2>/dev/null)
356 $(REMOVE) $(BUILD_DIR)/$(TARGET).*
363 # Create build directory
364 $(shell mkdir $(BUILD_DIR) 2>/dev/null)
366 # Create object files directory
367 $(eval $(foreach OUTPUT,$(OUTPUTS),$(shell mkdir $(OUTPUT) 2>/dev/null)))
369 # Include the dependency files.
370 -include $(patsubst %.o,%.d,$(OBJ))
373 # Listing of phony targets.
374 .PHONY : all finish sizebefore sizeafter gccversion \
375 build elf hex eep lss sym coff extcoff \
376 clean clean_list debug gdb-config show_path \
377 program teensy dfu flip dfu-ee flip-ee dfu-start