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)
102 CFLAGS += -include $(CONFIG_H)
106 #---------------- Compiler Options C++ ----------------
107 # -g*: generate debugging information
108 # -O*: optimization level
109 # -f...: tuning, see GCC manual and avr-libc documentation
110 # -Wall...: warning level
111 # -Wa,...: tell GCC to pass this to the assembler.
112 # -adhlns...: create assembler listing
113 CPPFLAGS += -g$(DEBUG)
114 CPPFLAGS += $(CPPDEFS)
116 # to supress "warning: only initialized variables can be placed into program memory area"
120 #CPPFLAGS += -mshort-calls
121 #CPPFLAGS += -fno-unit-at-a-time
122 #CPPFLAGS += -Wstrict-prototypes
123 #CPPFLAGS += -Wunreachable-code
124 #CPPFLAGS += -Wsign-compare
125 CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
126 #CPPFLAGS += $(CSTANDARD)
128 CPPFLAGS += -include $(CONFIG_H)
132 #---------------- Assembler Options ----------------
133 # -Wa,...: tell GCC to pass this to the assembler.
134 # -adhlns: create listing
135 # -gstabs: have the assembler create line number information; note that
136 # for use in COFF files, additional information about filenames
137 # and function names needs to be present in the assembler source
138 # files -- see avr-libc docs [FIXME: not yet described there]
139 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
140 # dump that will be displayed for a given single line of source input.
142 ASFLAGS += -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
144 ASFLAGS += -include $(CONFIG_H)
147 #---------------- Library Options ----------------
148 # Minimalistic printf version
149 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
151 # Floating point printf version (requires MATH_LIB = -lm below)
152 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
154 # If this is left blank, then it will use the Standard printf version.
156 #PRINTF_LIB = $(PRINTF_LIB_MIN)
157 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
160 # Minimalistic scanf version
161 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
163 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
164 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
166 # If this is left blank, then it will use the Standard scanf version.
168 #SCANF_LIB = $(SCANF_LIB_MIN)
169 #SCANF_LIB = $(SCANF_LIB_FLOAT)
175 #---------------- Linker Options ----------------
176 # -Wl,...: tell GCC to pass this to linker.
177 # -Map: create map file
178 # --cref: add cross reference to map file
180 # Comennt out "--relax" option to avoid a error such:
181 # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
183 LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref
184 #LDFLAGS += -Wl,--relax
185 LDFLAGS += $(EXTMEMOPTS)
186 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
187 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
188 #LDFLAGS += -T linker_script.x
189 # You can give EXTRALDFLAGS at 'make' command line.
190 LDFLAGS += $(EXTRALDFLAGS)
192 # Define programs and commands.
198 SECHO = $(SILENT) || echo
201 # Compiler flags to generate dependency files.
202 #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
203 GENDEPFLAGS = -MMD -MP -MF $(patsubst %.o,%.td,$@)
206 # Combine all necessary flags and optional flags.
207 # Add target processor to flags.
208 # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
209 ALL_CFLAGS = $(MCUFLAGS) $(CFLAGS) $(EXTRAFLAGS)
210 ALL_CPPFLAGS = $(MCUFLAGS) -x c++ $(CPPFLAGS) $(EXTRAFLAGS)
211 ALL_ASFLAGS = $(MCUFLAGS) -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
213 MOVE_DEP = mv -f $(patsubst %.o,%.td,$@) $(patsubst %.o,%.d,$@)
218 # Change the build target to build a HEX file or a library.
220 #build: elf hex eep lss sym
224 elf: $(BUILD_DIR)/$(TARGET).elf
225 hex: $(BUILD_DIR)/$(TARGET).hex
226 eep: $(BUILD_DIR)/$(TARGET).eep
227 lss: $(BUILD_DIR)/$(TARGET).lss
228 sym: $(BUILD_DIR)/$(TARGET).sym
229 LIBNAME=lib$(TARGET).a
232 # Display size of file.
233 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
234 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
235 ELFSIZE = $(SIZE) $(BUILD_DIR)/$(TARGET).elf
238 @if test -f $(TARGET).hex; then $(SECHO) $(MSG_SIZE_BEFORE); $(SILENT) || $(HEXSIZE); \
239 2>/dev/null; $(SECHO); fi
241 sizeafter: $(BUILD_DIR)/$(TARGET).hex
242 @if test -f $(TARGET).hex; then $(SECHO); $(SECHO) $(MSG_SIZE_AFTER); $(SILENT) || $(HEXSIZE); \
243 2>/dev/null; $(SECHO); fi
244 # test file sizes eventually
245 # @if [[ $($(SIZE) --target=$(FORMAT) $(TARGET).hex | $(AWK) 'NR==2 {print "0x"$5}') -gt 0x200 ]]; then $(SECHO) "File is too big!"; fi
247 # Display compiler version information.
249 @$(SILENT) || $(CC) --version
251 # Create final output files (.hex, .eep) from ELF output file.
253 @$(SILENT) || printf "$(MSG_FLASH) $@" | $(AWK_CMD)
254 $(eval CMD=$(HEX) $< $@)
256 @if $(AUTOGEN); then \
257 $(SILENT) || printf "Copying $(TARGET).hex to keymaps/$(KEYMAP)/$(KEYBOARD)_$(KEYMAP).hex\n"; \
258 $(COPY) $@ $(KEYMAP_PATH)/$(KEYBOARD)_$(KEYMAP).hex; \
260 $(COPY) $@ $(TARGET).hex; \
264 @$(SILENT) || printf "$(MSG_EEPROM) $@" | $(AWK_CMD)
265 $(eval CMD=$(EEP) $< $@ || exit 0)
268 # Create extended listing file from ELF output file.
270 @$(SILENT) || printf "$(MSG_EXTENDED_LISTING) $@" | $(AWK_CMD)
271 $(eval CMD=$(OBJDUMP) -h -S -z $< > $@)
274 # Create a symbol table from ELF output file.
276 @$(SILENT) || printf "$(MSG_SYMBOL_TABLE) $@" | $(AWK_CMD)
277 $(eval CMD=$(NM) -n $< > $@ )
281 @$(SILENT) || printf "$(MSG_BIN) $@" | $(AWK_CMD)
282 $(eval CMD=$(BIN) $< $@ || exit 0)
285 BEGIN = gccversion sizebefore
287 # Link: create ELF output file from object files.
288 .SECONDARY : $(BUILD_DIR)/$(TARGET).elf
290 # Note the obj.txt depeendency is there to force linking if a source file is deleted
291 %.elf: $(OBJ) $(MASTER_OUTPUT)/cflags.txt $(MASTER_OUTPUT)/ldflags.txt $(MASTER_OUTPUT)/obj.txt | $(BEGIN)
292 @$(SILENT) || printf "$(MSG_LINKING) $@" | $(AWK_CMD)
293 $(eval CMD=$(CC) $(ALL_CFLAGS) $(filter-out %.txt,$^) --output $@ $(LDFLAGS))
298 $1_INCFLAGS := $$(patsubst %,-I%,$$($1_INC))
299 $1_CFLAGS = $$(ALL_CFLAGS) $$($1_DEFS) $$($1_INCFLAGS)
300 $1_CPPFLAGS= $$(ALL_CPPFLAGS) $$($1_DEFS) $$($1_INCFLAGS)
301 $1_ASFLAGS= $$(ALL_ASFLAGS) $$($1_DEFS) $$($1_INCFLAGS)
302 $$(info $$($1_INCFLAGS))
304 # Compile: create object files from C source files.
305 $1/%.o : %.c $1/%.d $1/cflags.txt $1/compiler.txt | $(BEGIN)
307 @$$(SILENT) || printf "$$(MSG_COMPILING) $$<" | $$(AWK_CMD)
308 $$(eval CMD := $$(CC) -c $$($1_CFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
311 # Compile: create object files from C++ source files.
312 $1/%.o : %.cpp $1/%.d $1/cppflags.txt $1/compiler.txt | $(BEGIN)
314 @$$(SILENT) || printf "$$(MSG_COMPILING_CPP) $$<" | $$(AWK_CMD)
315 $$(eval CMD=$$(CC) -c $$($1_CPPFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
318 # Assemble: create object files from assembler source files.
319 $1/%.o : %.S $1/asflags.txt $1/compiler.txt | $(BEGIN)
321 @$(SILENT) || printf "$$(MSG_ASSEMBLING) $$<" | $$(AWK_CMD)
322 $$(eval CMD=$$(CC) -c $$($1_ASFLAGS) $$< -o $$@)
327 $1/cflags.txt: $1/force
328 echo '$$($1_CFLAGS)' | cmp -s - $$@ || echo '$$($1_CFLAGS)' > $$@
330 $1/cppflags.txt: $1/force
331 echo '$$($1_CPPFLAGS)' | cmp -s - $$@ || echo '$$($1_CPPFLAGS)' > $$@
333 $1/asflags.txt: $1/force
334 echo '$$($1_ASFLAGS)' | cmp -s - $$@ || echo '$$($1_ASFLAGS)' > $$@
336 $1/compiler.txt: $1/force
337 $$(CC) --version | cmp -s - $$@ || $$(CC) --version > $$@
340 $(MASTER_OUTPUT)/obj.txt: $(MASTER_OUTPUT)/force
341 echo '$(OBJ)' | cmp -s - $$@ || echo '$(OBJ)' > $$@
343 $(MASTER_OUTPUT)/ldflags.txt: $(MASTER_OUTPUT)/force
344 echo '$(LDFLAGS)' | cmp -s - $$@ || echo '$(LDFLAGS)' > $$@
347 # We have to use static rules for the .d files for some reason
348 DEPS = $(patsubst %.o,%.d,$(OBJ))
351 # Empty rule to force recompilation if the .d file is missing
355 $(foreach OUTPUT,$(OUTPUTS),$(eval $(call GEN_OBJRULE,$(OUTPUT))))
357 # Create preprocessed source for use in sending a bug report.
359 $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
361 # Target: clean project.
363 $(REMOVE) -r $(OBJDIR) 2>/dev/null
364 $(REMOVE) -r $(KBOBJDIR) 2>/dev/null
365 $(REMOVE) $(BUILD_DIR)/$(TARGET).*
372 # Create build directory
373 $(shell mkdir $(BUILD_DIR) 2>/dev/null)
375 # Create object files directory
376 $(eval $(foreach OUTPUT,$(OUTPUTS),$(shell mkdir $(OUTPUT) 2>/dev/null)))
378 # Include the dependency files.
379 -include $(patsubst %.o,%.d,$(OBJ))
382 # Listing of phony targets.
383 .PHONY : all finish sizebefore sizeafter gccversion \
384 build elf hex eep lss sym coff extcoff \
385 clean clean_list debug gdb-config show_path \
386 program teensy dfu flip dfu-ee flip-ee dfu-start