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 #CFLAGS += -mshort-calls
96 #CFLAGS += -fno-unit-at-a-time
98 #CFLAGS += -Wunreachable-code
99 #CFLAGS += -Wsign-compare
100 CFLAGS
+= -Wa
,-adhlns
=$(@
:%.o
=%.lst
)
101 CFLAGS
+= $(CSTANDARD
)
104 #---------------- Compiler Options C++ ----------------
105 # -g*: generate debugging information
106 # -O*: optimization level
107 # -f...: tuning, see GCC manual and avr-libc documentation
108 # -Wall...: warning level
109 # -Wa,...: tell GCC to pass this to the assembler.
110 # -adhlns...: create assembler listing
111 CPPFLAGS
+= -g
$(DEBUG
)
112 CPPFLAGS
+= $(CPPDEFS
)
114 # to supress "warning: only initialized variables can be placed into program memory area"
118 #CPPFLAGS += -mshort-calls
119 #CPPFLAGS += -fno-unit-at-a-time
120 #CPPFLAGS += -Wstrict-prototypes
121 #CPPFLAGS += -Wunreachable-code
122 #CPPFLAGS += -Wsign-compare
123 CPPFLAGS
+= -Wa
,-adhlns
=$(@
:%.o
=%.lst
)
124 #CPPFLAGS += $(CSTANDARD)
126 #---------------- Assembler Options ----------------
127 # -Wa,...: tell GCC to pass this to the assembler.
128 # -adhlns: create listing
129 # -gstabs: have the assembler create line number information; note that
130 # for use in COFF files, additional information about filenames
131 # and function names needs to be present in the assembler source
132 # files -- see avr-libc docs [FIXME: not yet described there]
133 # -listing-cont-lines: Sets the maximum number of continuation lines of hex
134 # dump that will be displayed for a given single line of source input.
136 ASFLAGS
+= -Wa
,-adhlns
=$(@
:%.o
=%.lst
),-gstabs
,--listing-cont-lines
=100
138 #---------------- Library Options ----------------
139 # Minimalistic printf version
140 PRINTF_LIB_MIN
= -Wl
,-u
,vfprintf
-lprintf_min
142 # Floating point printf version (requires MATH_LIB = -lm below)
143 PRINTF_LIB_FLOAT
= -Wl
,-u
,vfprintf
-lprintf_flt
145 # If this is left blank, then it will use the Standard printf version.
147 #PRINTF_LIB = $(PRINTF_LIB_MIN)
148 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
151 # Minimalistic scanf version
152 SCANF_LIB_MIN
= -Wl
,-u
,vfscanf
-lscanf_min
154 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
155 SCANF_LIB_FLOAT
= -Wl
,-u
,vfscanf
-lscanf_flt
157 # If this is left blank, then it will use the Standard scanf version.
159 #SCANF_LIB = $(SCANF_LIB_MIN)
160 #SCANF_LIB = $(SCANF_LIB_FLOAT)
167 #---------------- Linker Options ----------------
168 # -Wl,...: tell GCC to pass this to linker.
169 # -Map: create map file
170 # --cref: add cross reference to map file
172 # Comennt out "--relax" option to avoid a error such:
173 # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
176 ifeq ($(CREATE_MAP
),yes
)
177 LDFLAGS
+= -Wl
,-Map
=$(BUILD_DIR
)/$(TARGET
).map
,--cref
179 #LDFLAGS += -Wl,--relax
180 LDFLAGS
+= $(EXTMEMOPTS
)
181 LDFLAGS
+= $(patsubst %,-L
%,$(EXTRALIBDIRS
))
182 LDFLAGS
+= $(PRINTF_LIB
) $(SCANF_LIB
) $(MATH_LIB
)
183 #LDFLAGS += -T linker_script.x
184 # You can give EXTRALDFLAGS at 'make' command line.
185 LDFLAGS
+= $(EXTRALDFLAGS
)
187 # Define programs and commands.
193 SECHO
= $(SILENT
) || echo
196 # Compiler flags to generate dependency files.
197 #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
198 GENDEPFLAGS
= -MMD
-MP
-MF
$(patsubst %.o
,%.td
,$@
)
201 # Combine all necessary flags and optional flags.
202 # Add target processor to flags.
203 # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
204 ALL_CFLAGS
= $(MCUFLAGS
) $(CFLAGS
) $(EXTRAFLAGS
)
205 ALL_CPPFLAGS
= $(MCUFLAGS
) -x c
++ $(CPPFLAGS
) $(EXTRAFLAGS
)
206 ALL_ASFLAGS
= $(MCUFLAGS
) -x assembler-with-cpp
$(ASFLAGS
) $(EXTRAFLAGS
)
208 MOVE_DEP
= mv
-f
$(patsubst %.o
,%.td
,$@
) $(patsubst %.o
,%.d
,$@
)
211 elf
: $(BUILD_DIR
)/$(TARGET
).elf
212 hex
: $(BUILD_DIR
)/$(TARGET
).hex
213 eep
: $(BUILD_DIR
)/$(TARGET
).eep
214 lss
: $(BUILD_DIR
)/$(TARGET
).lss
215 sym
: $(BUILD_DIR
)/$(TARGET
).sym
216 LIBNAME
=lib
$(TARGET
).a
219 # Display size of file.
220 HEXSIZE
= $(SIZE
) --target
=$(FORMAT
) $(TARGET
).hex
221 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
222 ELFSIZE
= $(SIZE
) $(BUILD_DIR
)/$(TARGET
).elf
225 @if
test -f
$(TARGET
).hex
; then
$(SECHO
) $(MSG_SIZE_BEFORE
); $(SILENT
) ||
$(HEXSIZE
); \
226 2>/dev
/null
; $(SECHO
); fi
228 sizeafter
: $(BUILD_DIR
)/$(TARGET
).hex
229 @if
test -f
$(TARGET
).hex
; then
$(SECHO
); $(SECHO
) $(MSG_SIZE_AFTER
); $(SILENT
) ||
$(HEXSIZE
); \
230 2>/dev
/null
; $(SECHO
); fi
231 # test file sizes eventually
232 # @if [[ $($(SIZE) --target=$(FORMAT) $(TARGET).hex | $(AWK) 'NR==2 {print "0x"$5}') -gt 0x200 ]]; then $(SECHO) "File is too big!"; fi
234 # Display compiler version information.
236 @
$(SILENT
) ||
$(CC
) --version
238 # Create final output files (.hex, .eep) from ELF output file.
240 @
$(SILENT
) || printf
"$(MSG_FLASH) $@" |
$(AWK_CMD
)
241 $(eval CMD
=$(HEX
) $< $@
)
243 @if
$(AUTOGEN
); then \
244 $(SILENT
) || printf
"Copying $(TARGET).hex to keymaps/$(KEYMAP)/$(KEYBOARD)_$(KEYMAP).hex\n"; \
245 $(COPY
) $@
$(KEYMAP_PATH
)/$(KEYBOARD
)_
$(KEYMAP
).hex
; \
247 $(COPY
) $@
$(TARGET
).hex
; \
251 @
$(SILENT
) || printf
"$(MSG_EEPROM) $@" |
$(AWK_CMD
)
252 $(eval CMD
=$(EEP
) $< $@ || exit
0)
255 # Create extended listing file from ELF output file.
257 @
$(SILENT
) || printf
"$(MSG_EXTENDED_LISTING) $@" |
$(AWK_CMD
)
258 $(eval CMD
=$(OBJDUMP
) -h
-S
-z
$< > $@
)
261 # Create a symbol table from ELF output file.
263 @
$(SILENT
) || printf
"$(MSG_SYMBOL_TABLE) $@" |
$(AWK_CMD
)
264 $(eval CMD
=$(NM
) -n
$< > $@
)
268 @
$(SILENT
) || printf
"$(MSG_BIN) $@" |
$(AWK_CMD
)
269 $(eval CMD
=$(BIN
) $< $@ || exit
0)
272 BEGIN
= gccversion sizebefore
274 # Link: create ELF output file from object files.
275 .SECONDARY
: $(BUILD_DIR
)/$(TARGET
).elf
277 # Note the obj.txt depeendency is there to force linking if a source file is deleted
278 %.elf
: $(OBJ
) $(MASTER_OUTPUT
)/cflags.txt
$(MASTER_OUTPUT
)/ldflags.txt
$(MASTER_OUTPUT
)/obj.txt |
$(BEGIN
)
279 @
$(SILENT
) || printf
"$(MSG_LINKING) $@" |
$(AWK_CMD
)
280 $(eval CMD
=$(CC
) $(ALL_CFLAGS
) $(filter-out %.txt
,$^
) --output
$@
$(LDFLAGS
))
285 $1_INCFLAGS := $$(patsubst %,-I
%,$$($1_INC))
287 $1_CONFIG_FLAGS += -include $$($1_CONFIG)
289 $1_CFLAGS = $$(ALL_CFLAGS
) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
290 $1_CPPFLAGS= $$(ALL_CPPFLAGS
) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
291 $1_ASFLAGS= $$(ALL_ASFLAGS
) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
293 # Compile: create object files from C source files.
294 $1/%.o
: %.c
$1/%.d
$1/cflags.txt
$1/compiler.txt |
$(BEGIN
)
296 @
$$(SILENT
) || printf
"$$(MSG_COMPILING) $$<" |
$$(AWK_CMD
)
297 $$(eval CMD
:= $$(CC
) -c
$$($1_CFLAGS) $$(GENDEPFLAGS
) $$< -o
$$@
&& $$(MOVE_DEP
))
300 # Compile: create object files from C++ source files.
301 $1/%.o
: %.
cpp $1/%.d
$1/cppflags.txt
$1/compiler.txt |
$(BEGIN
)
303 @
$$(SILENT
) || printf
"$$(MSG_COMPILING_CPP) $$<" |
$$(AWK_CMD
)
304 $$(eval CMD
=$$(CC
) -c
$$($1_CPPFLAGS) $$(GENDEPFLAGS
) $$< -o
$$@
&& $$(MOVE_DEP
))
307 $1/%.o
: %.
cc $1/%.d
$1/cppflags.txt
$1/compiler.txt |
$(BEGIN
)
309 @
$$(SILENT
) || printf
"$$(MSG_COMPILING_CPP) $$<" |
$$(AWK_CMD
)
310 $$(eval CMD
=$$(CC
) -c
$$($1_CPPFLAGS) $$(GENDEPFLAGS
) $$< -o
$$@
&& $$(MOVE_DEP
))
313 # Assemble: create object files from assembler source files.
314 $1/%.o
: %.S
$1/asflags.txt
$1/compiler.txt |
$(BEGIN
)
316 @
$(SILENT
) || printf
"$$(MSG_ASSEMBLING) $$<" |
$$(AWK_CMD
)
317 $$(eval CMD
=$$(CC
) -c
$$($1_ASFLAGS) $$< -o
$$@
)
322 $1/cflags.txt
: $1/force
323 echo
'$$($1_CFLAGS)' | cmp
-s
- $$@ || echo
'$$($1_CFLAGS)' > $$@
325 $1/cppflags.txt
: $1/force
326 echo
'$$($1_CPPFLAGS)' | cmp
-s
- $$@ || echo
'$$($1_CPPFLAGS)' > $$@
328 $1/asflags.txt
: $1/force
329 echo
'$$($1_ASFLAGS)' | cmp
-s
- $$@ || echo
'$$($1_ASFLAGS)' > $$@
331 $1/compiler.txt
: $1/force
332 $$(CC
) --version | cmp
-s
- $$@ ||
$$(CC
) --version
> $$@
335 .PRECIOUS
: $(MASTER_OUTPUT
)/obj.txt
336 $(MASTER_OUTPUT
)/obj.txt
: $(MASTER_OUTPUT
)/force
337 echo
'$(OBJ)' | cmp
-s
- $@ || echo
'$(OBJ)' > $@
339 .PRECIOUS
: $(MASTER_OUTPUT
)/ldflags.txt
340 $(MASTER_OUTPUT
)/ldflags.txt
: $(MASTER_OUTPUT
)/force
341 echo
'$(LDFLAGS)' | cmp
-s
- $@ || echo
'$(LDFLAGS)' > $@
344 # We have to use static rules for the .d files for some reason
345 DEPS
= $(patsubst %.o
,%.d
,$(OBJ
))
348 # Empty rule to force recompilation if the .d file is missing
352 $(foreach OUTPUT
,$(OUTPUTS
),$(eval
$(call GEN_OBJRULE
,$(OUTPUT
))))
354 # Create preprocessed source for use in sending a bug report.
356 $(CC
) -E
-mmcu
=$(MCU
) $(CFLAGS
) $< -o
$@
358 # Target: clean project.
360 $(foreach OUTPUT
,$(OUTPUTS
), $(REMOVE
) -r
$(OUTPUT
) 2>/dev
/null
)
361 $(REMOVE
) $(BUILD_DIR
)/$(TARGET
).
*
368 # Create build directory
369 $(shell mkdir
-p
$(BUILD_DIR
) 2>/dev
/null
)
371 # Create object files directory
372 $(eval
$(foreach OUTPUT
,$(OUTPUTS
),$(shell mkdir
-p
$(OUTPUT
) 2>/dev
/null
)))
374 # Include the dependency files.
375 -include $(patsubst %.o
,%.d
,$(OBJ
))
378 # Listing of phony targets.
379 .PHONY
: all finish sizebefore sizeafter gccversion \
380 build elf hex eep lss sym coff extcoff \
381 clean clean_list debug gdb-config show_path \
382 program teensy dfu flip dfu-ee flip-ee dfu-start