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
%.
cc $(VPATH_SRC
)
25 vpath
%.hpp
$(VPATH_SRC
)
26 vpath
%.S
$(VPATH_SRC
)
29 # Convert all SRC to OBJ
31 $(patsubst %.c
,$1/%.o
,$(patsubst %.
cpp,$1/%.o
,$(patsubst %.
cc,$1/%.o
,$(patsubst %.S
,$1/%.o
,$($1_SRC)))))
33 $(foreach OUTPUT
,$(OUTPUTS
),$(eval
$(OUTPUT
)_OBJ
+=$(call OBJ_FROM_SRC
,$(OUTPUT
))))
35 # Define a list of all objects
36 OBJ
:= $(foreach OUTPUT
,$(OUTPUTS
),$($(OUTPUT
)_OBJ
))
38 MASTER_OUTPUT
:= $(firstword $(OUTPUTS
))
42 # Output format. (can be srec, ihex, binary)
45 # Optimization level, can be [0, 1, 2, 3, s].
46 # 0 = turn off optimization. s = optimize for size.
47 # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
53 # Compiler flag to set the C Standard level.
55 # gnu89 = c89 plus GCC extensions
56 # c99 = ISO C99 standard (not yet fully implemented)
57 # gnu99 = c99 plus GCC extensions
58 CSTANDARD
= -std
=gnu99
61 # Place -D or -U options here for C sources
65 # Place -D or -U options here for ASM sources
69 # Place -D or -U options here for C++ sources
70 #CPPDEFS += -D__STDC_LIMIT_MACROS
71 #CPPDEFS += -D__STDC_CONSTANT_MACROS
77 #---------------- Compiler Options C ----------------
78 # -g*: generate debugging information
79 # -O*: optimization level
80 # -f...: tuning, see GCC manual and avr-libc documentation
81 # -Wall...: warning level
82 # -Wa,...: tell GCC to pass this to the assembler.
83 # -adhlns...: create assembler listing
89 ifeq ("$(shell echo "int main
(){}" | $(CC) -fdiagnostics-color -x c - -o /dev/null 2>&1)", "")
90 CFLAGS
+= -fdiagnostics-color
94 CFLAGS
+= -Wstrict-prototypes
95 ifneq ($(strip $(ALLOW_WARNINGS
)), yes
)
98 #CFLAGS += -mshort-calls
99 #CFLAGS += -fno-unit-at-a-time
101 #CFLAGS += -Wunreachable-code
102 #CFLAGS += -Wsign-compare
103 CFLAGS
+= -Wa
,-adhlns
=$(@
:%.o
=%.lst
)
104 CFLAGS
+= $(CSTANDARD
)
107 #---------------- Compiler Options C++ ----------------
108 # -g*: generate debugging information
109 # -O*: optimization level
110 # -f...: tuning, see GCC manual and avr-libc documentation
111 # -Wall...: warning level
112 # -Wa,...: tell GCC to pass this to the assembler.
113 # -adhlns...: create assembler listing
114 CPPFLAGS
+= -g
$(DEBUG
)
115 CPPFLAGS
+= $(CPPDEFS
)
117 # to supress "warning: only initialized variables can be placed into program memory area"
121 ifneq ($(strip $(ALLOW_WARNINGS
)), yes
)
124 #CPPFLAGS += -mshort-calls
125 #CPPFLAGS += -fno-unit-at-a-time
126 #CPPFLAGS += -Wstrict-prototypes
127 #CPPFLAGS += -Wunreachable-code
128 #CPPFLAGS += -Wsign-compare
129 CPPFLAGS
+= -Wa
,-adhlns
=$(@
:%.o
=%.lst
)
130 #CPPFLAGS += $(CSTANDARD)
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 #---------------- Library Options ----------------
145 # Minimalistic printf version
146 PRINTF_LIB_MIN
= -Wl
,-u
,vfprintf
-lprintf_min
148 # Floating point printf version (requires MATH_LIB = -lm below)
149 PRINTF_LIB_FLOAT
= -Wl
,-u
,vfprintf
-lprintf_flt
151 # If this is left blank, then it will use the Standard printf version.
153 #PRINTF_LIB = $(PRINTF_LIB_MIN)
154 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
157 # Minimalistic scanf version
158 SCANF_LIB_MIN
= -Wl
,-u
,vfscanf
-lscanf_min
160 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
161 SCANF_LIB_FLOAT
= -Wl
,-u
,vfscanf
-lscanf_flt
163 # If this is left blank, then it will use the Standard scanf version.
165 #SCANF_LIB = $(SCANF_LIB_MIN)
166 #SCANF_LIB = $(SCANF_LIB_FLOAT)
173 #---------------- Linker Options ----------------
174 # -Wl,...: tell GCC to pass this to linker.
175 # -Map: create map file
176 # --cref: add cross reference to map file
178 # Comennt out "--relax" option to avoid a error such:
179 # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
182 ifeq ($(CREATE_MAP
),yes
)
183 LDFLAGS
+= -Wl
,-Map
=$(BUILD_DIR
)/$(TARGET
).map
,--cref
185 #LDFLAGS += -Wl,--relax
186 LDFLAGS
+= $(EXTMEMOPTS
)
187 LDFLAGS
+= $(patsubst %,-L
%,$(EXTRALIBDIRS
))
188 LDFLAGS
+= $(PRINTF_LIB
) $(SCANF_LIB
) $(MATH_LIB
)
189 #LDFLAGS += -T linker_script.x
190 # You can give EXTRALDFLAGS at 'make' command line.
191 LDFLAGS
+= $(EXTRALDFLAGS
)
193 # Define programs and commands.
199 SECHO
= $(SILENT
) || echo
202 # Compiler flags to generate dependency files.
203 #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
204 GENDEPFLAGS
= -MMD
-MP
-MF
$(patsubst %.o
,%.td
,$@
)
207 # Combine all necessary flags and optional flags.
208 # Add target processor to flags.
209 # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
210 ALL_CFLAGS
= $(MCUFLAGS
) $(CFLAGS
) $(EXTRAFLAGS
)
211 ALL_CPPFLAGS
= $(MCUFLAGS
) -x c
++ $(CPPFLAGS
) $(EXTRAFLAGS
)
212 ALL_ASFLAGS
= $(MCUFLAGS
) -x assembler-with-cpp
$(ASFLAGS
) $(EXTRAFLAGS
)
214 MOVE_DEP
= mv
-f
$(patsubst %.o
,%.td
,$@
) $(patsubst %.o
,%.d
,$@
)
217 elf
: $(BUILD_DIR
)/$(TARGET
).elf
218 hex
: $(BUILD_DIR
)/$(TARGET
).hex
219 cpfirmware
: $(FIRMWARE_FORMAT
)
220 $(SILENT
) || printf
"Copying $(TARGET).$(FIRMWARE_FORMAT) to qmk_firmware folder" |
$(AWK_CMD
)
221 $(COPY
) $(BUILD_DIR
)/$(TARGET
).
$(FIRMWARE_FORMAT
) $(TARGET
).
$(FIRMWARE_FORMAT
) && $(PRINT_OK
)
222 eep
: $(BUILD_DIR
)/$(TARGET
).eep
223 lss
: $(BUILD_DIR
)/$(TARGET
).lss
224 sym
: $(BUILD_DIR
)/$(TARGET
).sym
225 LIBNAME
=lib
$(TARGET
).a
228 # Display size of file.
229 HEXSIZE
= $(SIZE
) --target
=$(FORMAT
) $(BUILD_DIR
)/$(TARGET
).hex
230 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
231 ELFSIZE
= $(SIZE
) $(BUILD_DIR
)/$(TARGET
).elf
234 @if
test -f
$(BUILD_DIR
)/$(TARGET
).hex
; then
$(SECHO
) $(MSG_SIZE_BEFORE
); $(SILENT
) ||
$(HEXSIZE
); \
235 2>/dev
/null
; $(SECHO
); fi
237 sizeafter
: $(BUILD_DIR
)/$(TARGET
).hex
238 @if
test -f
$(BUILD_DIR
)/$(TARGET
).hex
; then
$(SECHO
); $(SECHO
) $(MSG_SIZE_AFTER
); $(SILENT
) ||
$(HEXSIZE
); \
239 2>/dev
/null
; $(SECHO
); fi
241 # Display compiler version information.
243 @
$(SILENT
) ||
$(CC
) --version
245 # Create final output files (.hex, .eep) from ELF output file.
247 @
$(SILENT
) || printf
"$(MSG_FLASH) $@" |
$(AWK_CMD
)
248 $(eval CMD
=$(HEX
) $< $@
)
250 @if
$(AUTOGEN
); then \
251 $(SILENT
) || printf
"Copying $(TARGET).hex to keymaps/$(KEYMAP)/$(TARGET).hex\n"; \
252 $(COPY
) $@
$(KEYMAP_PATH
)/$(TARGET
).hex
; \
256 @
$(SILENT
) || printf
"$(MSG_EEPROM) $@" |
$(AWK_CMD
)
257 $(eval CMD
=$(EEP
) $< $@ || exit
0)
260 # Create extended listing file from ELF output file.
262 @
$(SILENT
) || printf
"$(MSG_EXTENDED_LISTING) $@" |
$(AWK_CMD
)
263 $(eval CMD
=$(OBJDUMP
) -h
-S
-z
$< > $@
)
266 # Create a symbol table from ELF output file.
268 @
$(SILENT
) || printf
"$(MSG_SYMBOL_TABLE) $@" |
$(AWK_CMD
)
269 $(eval CMD
=$(NM
) -n
$< > $@
)
273 @
$(SILENT
) || printf
"$(MSG_BIN) $@" |
$(AWK_CMD
)
274 $(eval CMD
=$(BIN
) $< $@ || exit
0)
277 BEGIN
= gccversion sizebefore
279 # Link: create ELF output file from object files.
280 .SECONDARY
: $(BUILD_DIR
)/$(TARGET
).elf
282 # Note the obj.txt depeendency is there to force linking if a source file is deleted
283 %.elf
: $(OBJ
) $(MASTER_OUTPUT
)/cflags.txt
$(MASTER_OUTPUT
)/ldflags.txt
$(MASTER_OUTPUT
)/obj.txt |
$(BEGIN
)
284 @
$(SILENT
) || printf
"$(MSG_LINKING) $@" |
$(AWK_CMD
)
285 $(eval CMD
=$(CC
) $(ALL_CFLAGS
) $(filter-out %.txt
,$^
) --output
$@
$(LDFLAGS
))
290 $1_INCFLAGS := $$(patsubst %,-I
%,$$($1_INC))
292 $1_CONFIG_FLAGS += $$(patsubst %,-include %,$$($1_CONFIG))
294 $1_CFLAGS = $$(ALL_CFLAGS
) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
295 $1_CPPFLAGS= $$(ALL_CPPFLAGS
) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
296 $1_ASFLAGS= $$(ALL_ASFLAGS
) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
298 # Compile: create object files from C source files.
299 $1/%.o
: %.c
$1/%.d
$1/cflags.txt
$1/compiler.txt |
$(BEGIN
)
301 @
$$(SILENT
) || printf
"$$(MSG_COMPILING) $$<" |
$$(AWK_CMD
)
302 $$(eval CMD
:= $$(CC
) -c
$$($1_CFLAGS) $$(GENDEPFLAGS
) $$< -o
$$@
&& $$(MOVE_DEP
))
305 # Compile: create object files from C++ source files.
306 $1/%.o
: %.
cpp $1/%.d
$1/cppflags.txt
$1/compiler.txt |
$(BEGIN
)
308 @
$$(SILENT
) || printf
"$$(MSG_COMPILING_CPP) $$<" |
$$(AWK_CMD
)
309 $$(eval CMD
=$$(CC
) -c
$$($1_CPPFLAGS) $$(GENDEPFLAGS
) $$< -o
$$@
&& $$(MOVE_DEP
))
312 $1/%.o
: %.
cc $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 .PRECIOUS
: $(MASTER_OUTPUT
)/obj.txt
341 $(MASTER_OUTPUT
)/obj.txt
: $(MASTER_OUTPUT
)/force
342 echo
'$(OBJ)' | cmp
-s
- $@ || echo
'$(OBJ)' > $@
344 .PRECIOUS
: $(MASTER_OUTPUT
)/ldflags.txt
345 $(MASTER_OUTPUT
)/ldflags.txt
: $(MASTER_OUTPUT
)/force
346 echo
'$(LDFLAGS)' | cmp
-s
- $@ || echo
'$(LDFLAGS)' > $@
349 # We have to use static rules for the .d files for some reason
350 DEPS
= $(patsubst %.o
,%.d
,$(OBJ
))
353 # Empty rule to force recompilation if the .d file is missing
357 $(foreach OUTPUT
,$(OUTPUTS
),$(eval
$(call GEN_OBJRULE
,$(OUTPUT
))))
359 # Create preprocessed source for use in sending a bug report.
361 $(CC
) -E
-mmcu
=$(MCU
) $(CFLAGS
) $< -o
$@
363 # Target: clean project.
365 $(foreach OUTPUT
,$(OUTPUTS
), $(REMOVE
) -r
$(OUTPUT
) 2>/dev
/null
)
366 $(REMOVE
) $(BUILD_DIR
)/$(TARGET
).
*
373 ifeq ($(findstring avr-gcc
,$(CC
)),avr-gcc
)
375 $(eval MAX_SIZE
=$(shell n
=`$(CC) -E -mmcu=$(MCU) $(CFLAGS) $(OPT_DEFS) tmk_core/common/avr/bootloader_size.c 2> /dev/null | sed -ne '/^#/n;/^AVR_SIZE:/,$${s/^AVR_SIZE: //;p;}'` && echo
$$(($$n)) || echo
0))
376 $(eval CURRENT_SIZE
=$(shell if
[ -f
$(BUILD_DIR
)/$(TARGET
).hex
]; then
$(SIZE
) --target
=$(FORMAT
) $(BUILD_DIR
)/$(TARGET
).hex |
$(AWK
) 'NR==2 {print $$4}'; else printf
0; fi
))
377 $(eval FREE_SIZE
=$(shell expr
$(MAX_SIZE
) - $(CURRENT_SIZE
)))
378 $(eval OVER_SIZE
=$(shell expr
$(CURRENT_SIZE
) - $(MAX_SIZE
)))
379 if
[ $(MAX_SIZE
) -gt
0 ] && [ $(CURRENT_SIZE
) -gt
0 ]; then \
380 $(SILENT
) || printf
"$(MSG_CHECK_FILESIZE)" |
$(AWK_CMD
); \
381 if
[ $(CURRENT_SIZE
) -gt
$(MAX_SIZE
) ]; then printf
"\n * $(MSG_FILE_TOO_BIG)"; $(PRINT_ERROR_PLAIN
); else $(PRINT_OK
); $(SILENT
) || printf
" * $(MSG_FILE_JUST_RIGHT)"; fi \
385 echo
"(Firmware size check does not yet support $(MCU) microprocessors; skipping.)"
388 # Create build directory
389 $(shell mkdir
-p
$(BUILD_DIR
) 2>/dev
/null
)
391 # Create object files directory
392 $(eval
$(foreach OUTPUT
,$(OUTPUTS
),$(shell mkdir
-p
$(OUTPUT
) 2>/dev
/null
)))
394 # Include the dependency files.
395 -include $(patsubst %.o
,%.d
,$(OBJ
))
398 # Listing of phony targets.
399 .PHONY
: all finish sizebefore sizeafter qmkversion \
400 gccversion build elf hex eep lss sym coff extcoff \
401 clean clean_list debug gdb-config show_path \
402 program teensy dfu flip dfu-ee flip-ee dfu-start