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 # List any extra directories to look for include files here.
53 # Each directory must be seperated by a space.
54 # Use forward slashes for directory separators.
55 # For a directory that has spaces, enclose it in quotes.
56 EXTRAINCDIRS += $(subst :, ,$(VPATH_SRC))
59 # Compiler flag to set the C Standard level.
61 # gnu89 = c89 plus GCC extensions
62 # c99 = ISO C99 standard (not yet fully implemented)
63 # gnu99 = c99 plus GCC extensions
64 CSTANDARD = -std=gnu99
67 # Place -D or -U options here for C sources
71 # Place -D or -U options here for ASM sources
75 # Place -D or -U options here for C++ sources
76 #CPPDEFS += -D__STDC_LIMIT_MACROS
77 #CPPDEFS += -D__STDC_CONSTANT_MACROS
83 #---------------- Compiler Options C ----------------
84 # -g*: generate debugging information
85 # -O*: optimization level
86 # -f...: tuning, see GCC manual and avr-libc documentation
87 # -Wall...: warning level
88 # -Wa,...: tell GCC to pass this to the assembler.
89 # -adhlns...: create assembler listing
95 ifeq ("$(shell echo "int main(){}" | $(CC) -fdiagnostics-color -x c - -o /dev/null 2>&1)", "")
96 CFLAGS+= -fdiagnostics-color
100 CFLAGS += -Wstrict-prototypes
101 #CFLAGS += -mshort-calls
102 #CFLAGS += -fno-unit-at-a-time
104 #CFLAGS += -Wunreachable-code
105 #CFLAGS += -Wsign-compare
106 CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
107 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
108 CFLAGS += $(CSTANDARD)
110 CFLAGS += -include $(CONFIG_H)
114 #---------------- Compiler Options C++ ----------------
115 # -g*: generate debugging information
116 # -O*: optimization level
117 # -f...: tuning, see GCC manual and avr-libc documentation
118 # -Wall...: warning level
119 # -Wa,...: tell GCC to pass this to the assembler.
120 # -adhlns...: create assembler listing
121 CPPFLAGS += -g$(DEBUG)
122 CPPFLAGS += $(CPPDEFS)
124 # to supress "warning: only initialized variables can be placed into program memory area"
128 #CPPFLAGS += -mshort-calls
129 #CPPFLAGS += -fno-unit-at-a-time
130 #CPPFLAGS += -Wstrict-prototypes
131 #CPPFLAGS += -Wunreachable-code
132 #CPPFLAGS += -Wsign-compare
133 CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
134 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
135 #CPPFLAGS += $(CSTANDARD)
137 CPPFLAGS += -include $(CONFIG_H)
141 #---------------- Assembler Options ----------------
142 # -Wa,...: tell GCC to pass this to the assembler.
143 # -adhlns: create listing
144 # -gstabs: have the assembler create line number information; note that
145 # for use in COFF files, additional information about filenames
146 # and function names needs to be present in the assembler source
147 # files -- see avr-libc docs [FIXME: not yet described there]
148 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
149 # dump that will be displayed for a given single line of source input.
151 ASFLAGS += -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
152 ASFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
154 ASFLAGS += -include $(CONFIG_H)
157 #---------------- Library Options ----------------
158 # Minimalistic printf version
159 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
161 # Floating point printf version (requires MATH_LIB = -lm below)
162 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
164 # If this is left blank, then it will use the Standard printf version.
166 #PRINTF_LIB = $(PRINTF_LIB_MIN)
167 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
170 # Minimalistic scanf version
171 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
173 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
174 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
176 # If this is left blank, then it will use the Standard scanf version.
178 #SCANF_LIB = $(SCANF_LIB_MIN)
179 #SCANF_LIB = $(SCANF_LIB_FLOAT)
185 #---------------- Linker Options ----------------
186 # -Wl,...: tell GCC to pass this to linker.
187 # -Map: create map file
188 # --cref: add cross reference to map file
190 # Comennt out "--relax" option to avoid a error such:
191 # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
193 LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref
194 #LDFLAGS += -Wl,--relax
195 LDFLAGS += $(EXTMEMOPTS)
196 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
197 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
198 #LDFLAGS += -T linker_script.x
199 # You can give EXTRALDFLAGS at 'make' command line.
200 LDFLAGS += $(EXTRALDFLAGS)
202 # Define programs and commands.
208 SECHO = $(SILENT) || echo
211 # Compiler flags to generate dependency files.
212 #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
213 GENDEPFLAGS = -MMD -MP -MF $(patsubst %.o,%.td,$@)
216 # Combine all necessary flags and optional flags.
217 # Add target processor to flags.
218 # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
219 ALL_CFLAGS = $(MCUFLAGS) $(CFLAGS) $(EXTRAFLAGS)
220 ALL_CPPFLAGS = $(MCUFLAGS) -x c++ $(CPPFLAGS) $(EXTRAFLAGS)
221 ALL_ASFLAGS = $(MCUFLAGS) -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
223 MOVE_DEP = mv -f $(patsubst %.o,%.td,$@) $(patsubst %.o,%.d,$@)
228 # Change the build target to build a HEX file or a library.
230 #build: elf hex eep lss sym
234 elf: $(BUILD_DIR)/$(TARGET).elf
235 hex: $(BUILD_DIR)/$(TARGET).hex
236 eep: $(BUILD_DIR)/$(TARGET).eep
237 lss: $(BUILD_DIR)/$(TARGET).lss
238 sym: $(BUILD_DIR)/$(TARGET).sym
239 LIBNAME=lib$(TARGET).a
242 # Display size of file.
243 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
244 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
245 ELFSIZE = $(SIZE) $(BUILD_DIR)/$(TARGET).elf
248 @if test -f $(TARGET).hex; then $(SECHO) $(MSG_SIZE_BEFORE); $(SILENT) || $(HEXSIZE); \
249 2>/dev/null; $(SECHO); fi
251 sizeafter: $(BUILD_DIR)/$(TARGET).hex
252 @if test -f $(TARGET).hex; then $(SECHO); $(SECHO) $(MSG_SIZE_AFTER); $(SILENT) || $(HEXSIZE); \
253 2>/dev/null; $(SECHO); fi
254 # test file sizes eventually
255 # @if [[ $($(SIZE) --target=$(FORMAT) $(TARGET).hex | $(AWK) 'NR==2 {print "0x"$5}') -gt 0x200 ]]; then $(SECHO) "File is too big!"; fi
257 # Display compiler version information.
259 @$(SILENT) || $(CC) --version
261 # Create final output files (.hex, .eep) from ELF output file.
263 @$(SILENT) || printf "$(MSG_FLASH) $@" | $(AWK_CMD)
264 $(eval CMD=$(HEX) $< $@)
266 @if $(AUTOGEN); then \
267 $(SILENT) || printf "Copying $(TARGET).hex to keymaps/$(KEYMAP)/$(KEYBOARD)_$(KEYMAP).hex\n"; \
268 $(COPY) $@ $(KEYMAP_PATH)/$(KEYBOARD)_$(KEYMAP).hex; \
270 $(COPY) $@ $(TARGET).hex; \
274 @$(SILENT) || printf "$(MSG_EEPROM) $@" | $(AWK_CMD)
275 $(eval CMD=$(EEP) $< $@ || exit 0)
278 # Create extended listing file from ELF output file.
280 @$(SILENT) || printf "$(MSG_EXTENDED_LISTING) $@" | $(AWK_CMD)
281 $(eval CMD=$(OBJDUMP) -h -S -z $< > $@)
284 # Create a symbol table from ELF output file.
286 @$(SILENT) || printf "$(MSG_SYMBOL_TABLE) $@" | $(AWK_CMD)
287 $(eval CMD=$(NM) -n $< > $@ )
291 @$(SILENT) || printf "$(MSG_BIN) $@" | $(AWK_CMD)
292 $(eval CMD=$(BIN) $< $@ || exit 0)
295 BEGIN = gccversion sizebefore
297 # Link: create ELF output file from object files.
298 .SECONDARY : $(BUILD_DIR)/$(TARGET).elf
300 # Note the obj.txt depeendency is there to force linking if a source file is deleted
301 %.elf: $(OBJ) $(MASTER_OUTPUT)/cflags.txt $(MASTER_OUTPUT)/ldflags.txt $(MASTER_OUTPUT)/obj.txt | $(BEGIN)
302 @$(SILENT) || printf "$(MSG_LINKING) $@" | $(AWK_CMD)
303 $(eval CMD=$(CC) $(ALL_CFLAGS) $(filter-out %.txt,$^) --output $@ $(LDFLAGS))
308 $1_CFLAGS = $$(ALL_CFLAGS) $$($1_DEFS)
309 $1_CPPFLAGS= $$(ALL_CPPFLAGS) $$($1_DEFS)
310 $1_ASFLAGS= $$(ALL_ASFLAGS) $$($1_DEFS)
312 # Compile: create object files from C source files.
313 $1/%.o : %.c $1/%.d $1/cflags.txt $1/compiler.txt | $(BEGIN)
315 @$$(SILENT) || printf "$$(MSG_COMPILING) $$<" | $$(AWK_CMD)
316 $$(eval CMD := $$(CC) -c $$($1_CFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
319 # Compile: create object files from C++ source files.
320 $1/%.o : %.cpp $1/%.d $1/cppflags.txt $1/compiler.txt | $(BEGIN)
322 @$$(SILENT) || printf "$$(MSG_COMPILING_CPP) $$<" | $$(AWK_CMD)
323 $$(eval CMD=$$(CC) -c $$($1_CPPFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
326 # Assemble: create object files from assembler source files.
327 $1/%.o : %.S $1/asflags.txt $1/compiler.txt | $(BEGIN)
329 @$(SILENT) || printf "$$(MSG_ASSEMBLING) $$<" | $$(AWK_CMD)
330 $$(eval CMD=$$(CC) -c $$($1_ASFLAGS) $$< -o $$@)
335 $1/cflags.txt: $1/force
336 echo '$$($1_CFLAGS)' | cmp -s - $$@ || echo '$$($1_CFLAGS)' > $$@
338 $1/cppflags.txt: $1/force
339 echo '$$($1_CPPFLAGS)' | cmp -s - $$@ || echo '$$($1_CPPFLAGS)' > $$@
341 $1/asflags.txt: $1/force
342 echo '$$($1_ASFLAGS)' | cmp -s - $$@ || echo '$$($1_ASFLAGS)' > $$@
344 $1/compiler.txt: $1/force
345 $$(CC) --version | cmp -s - $$@ || $$(CC) --version > $$@
348 $(MASTER_OUTPUT)/obj.txt: $(MASTER_OUTPUT)/force
349 echo '$(OBJ)' | cmp -s - $$@ || echo '$(OBJ)' > $$@
351 $(MASTER_OUTPUT)/ldflags.txt: $(MASTER_OUTPUT)/force
352 echo '$(LDFLAGS)' | cmp -s - $$@ || echo '$(LDFLAGS)' > $$@
355 # We have to use static rules for the .d files for some reason
356 DEPS = $(patsubst %.o,%.d,$(OBJ))
359 # Empty rule to force recompilation if the .d file is missing
363 $(foreach OUTPUT,$(OUTPUTS),$(eval $(call GEN_OBJRULE,$(OUTPUT))))
365 # Create preprocessed source for use in sending a bug report.
367 $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
369 # Target: clean project.
371 $(REMOVE) -r $(OBJDIR) 2>/dev/null
372 $(REMOVE) -r $(KBOBJDIR) 2>/dev/null
373 $(REMOVE) $(BUILD_DIR)/$(TARGET).*
380 # Create build directory
381 $(shell mkdir $(BUILD_DIR) 2>/dev/null)
383 # Create object files directory
384 $(eval $(foreach OUTPUT,$(OUTPUTS),$(shell mkdir $(OUTPUT) 2>/dev/null)))
386 # Include the dependency files.
387 -include $(patsubst %.o,%.d,$(OBJ))
390 # Listing of phony targets.
391 .PHONY : all finish sizebefore sizeafter gccversion \
392 build elf hex eep lss sym coff extcoff \
393 clean clean_list debug gdb-config show_path \
394 program teensy dfu flip dfu-ee flip-ee dfu-start