]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/rules.mk
add version to make output
[qmk_firmware.git] / tmk_core / rules.mk
1 # Hey Emacs, this is a -*- makefile -*-
2 #----------------------------------------------------------------------------
3 # WinAVR Makefile Template written by Eric B. Weddington, Jg Wunsch, et al.
4 #
5 # Released to the Public Domain
6 #
7 # Additional material for this makefile was written by:
8 # Peter Fleury
9 # Tim Henigan
10 # Colin O'Flynn
11 # Reiner Patommel
12 # Markus Pfaff
13 # Sander Pool
14 # Frederik Rouleau
15 # Carlos Lamas
16 #
17
18 # Enable vpath seraching for source files only
19 # Without this, output files, could be read from the wrong .build directories
20 VPATH_SRC := $(VPATH)
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)
27 VPATH :=
28
29 # Convert all SRC to OBJ
30 define OBJ_FROM_SRC
31 $(patsubst %.c,$1/%.o,$(patsubst %.cpp,$1/%.o,$(patsubst %.cc,$1/%.o,$(patsubst %.S,$1/%.o,$($1_SRC)))))
32 endef
33 $(foreach OUTPUT,$(OUTPUTS),$(eval $(OUTPUT)_OBJ +=$(call OBJ_FROM_SRC,$(OUTPUT))))
34
35 # Define a list of all objects
36 OBJ := $(foreach OUTPUT,$(OUTPUTS),$($(OUTPUT)_OBJ))
37
38 MASTER_OUTPUT := $(firstword $(OUTPUTS))
39
40
41
42 # Output format. (can be srec, ihex, binary)
43 FORMAT = ihex
44
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.)
48 OPT = s
49
50 AUTOGEN ?= false
51
52
53 # Compiler flag to set the C Standard level.
54 #     c89   = "ANSI" C
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
59
60
61 # Place -D or -U options here for C sources
62 #CDEFS +=
63
64
65 # Place -D or -U options here for ASM sources
66 #ADEFS +=
67
68
69 # Place -D or -U options here for C++ sources
70 #CPPDEFS += -D__STDC_LIMIT_MACROS
71 #CPPDEFS += -D__STDC_CONSTANT_MACROS
72 #CPPDEFS +=
73
74
75
76
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
84 CFLAGS += -g$(DEBUG)
85 CFLAGS += $(CDEFS)
86 CFLAGS += -O$(OPT)
87 # add color
88 ifeq ($(COLOR),true)
89 ifeq ("$(shell echo "int main(){}" | $(CC) -fdiagnostics-color -x c - -o /dev/null 2>&1)", "")
90         CFLAGS+= -fdiagnostics-color
91 endif
92 endif
93 CFLAGS += -Wall
94 CFLAGS += -Wstrict-prototypes
95 ifneq ($(strip $(ALLOW_WARNINGS)), yes)
96     CFLAGS += -Werror
97 endif
98 #CFLAGS += -mshort-calls
99 #CFLAGS += -fno-unit-at-a-time
100 #CFLAGS += -Wundef
101 #CFLAGS += -Wunreachable-code
102 #CFLAGS += -Wsign-compare
103 CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
104 CFLAGS += $(CSTANDARD)
105
106
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)
116 CPPFLAGS += -O$(OPT)
117 # to supress "warning: only initialized variables can be placed into program memory area"
118 CPPFLAGS += -w
119 CPPFLAGS += -Wall
120 CPPFLAGS += -Wundef
121 ifneq ($(strip $(ALLOW_WARNINGS)), yes)
122     CPPFLAGS += -Werror
123 endif
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)
131
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.
141 ASFLAGS += $(ADEFS) 
142 ASFLAGS += -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
143
144 #---------------- Library Options ----------------
145 # Minimalistic printf version
146 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
147
148 # Floating point printf version (requires MATH_LIB = -lm below)
149 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
150
151 # If this is left blank, then it will use the Standard printf version.
152 PRINTF_LIB =
153 #PRINTF_LIB = $(PRINTF_LIB_MIN)
154 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
155
156
157 # Minimalistic scanf version
158 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
159
160 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
161 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
162
163 # If this is left blank, then it will use the Standard scanf version.
164 SCANF_LIB =
165 #SCANF_LIB = $(SCANF_LIB_MIN)
166 #SCANF_LIB = $(SCANF_LIB_FLOAT)
167
168
169 MATH_LIB = -lm
170 CREATE_MAP ?= yes
171
172
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
177 #
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'
180 #
181
182 ifeq ($(CREATE_MAP),yes)
183         LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref
184 endif
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)
192
193 # Define programs and commands.
194 SHELL = sh
195 REMOVE = rm -f
196 REMOVEDIR = rmdir
197 COPY = cp
198 WINSHELL = cmd
199 SECHO = $(SILENT) || echo
200
201
202 # Compiler flags to generate dependency files.
203 #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
204 GENDEPFLAGS = -MMD -MP -MF $(patsubst %.o,%.td,$@)
205
206
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)
213
214 MOVE_DEP = mv -f $(patsubst %.o,%.td,$@) $(patsubst %.o,%.d,$@)
215
216
217 elf: $(BUILD_DIR)/$(TARGET).elf
218 hex: $(BUILD_DIR)/$(TARGET).hex
219 eep: $(BUILD_DIR)/$(TARGET).eep
220 lss: $(BUILD_DIR)/$(TARGET).lss
221 sym: $(BUILD_DIR)/$(TARGET).sym
222 LIBNAME=lib$(TARGET).a
223 lib: $(LIBNAME)
224
225 # Display size of file.
226 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
227 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
228 ELFSIZE = $(SIZE) $(BUILD_DIR)/$(TARGET).elf
229
230 sizebefore:
231         @if test -f $(TARGET).hex; then $(SECHO) $(MSG_SIZE_BEFORE); $(SILENT) || $(HEXSIZE); \
232         2>/dev/null; $(SECHO); fi
233
234 sizeafter: $(BUILD_DIR)/$(TARGET).hex
235         @if test -f $(TARGET).hex; then $(SECHO); $(SECHO) $(MSG_SIZE_AFTER); $(SILENT) || $(HEXSIZE); \
236         2>/dev/null; $(SECHO); fi
237         # test file sizes eventually
238         # @if [[ $($(SIZE) --target=$(FORMAT) $(TARGET).hex | $(AWK) 'NR==2 {print "0x"$5}') -gt 0x200 ]]; then $(SECHO) "File is too big!"; fi
239
240 # Display qmk version information.
241 qmkversion :
242         @$(SILENT) || printf "QMK Firmware v$(shell git describe --abbrev=0 --tags 2>/dev/null)\n\n"
243
244 # Display compiler version information.
245 gccversion :
246         @$(SILENT) || $(CC) --version
247
248 # Create final output files (.hex, .eep) from ELF output file.
249 %.hex: %.elf
250         @$(SILENT) || printf "$(MSG_FLASH) $@" | $(AWK_CMD)
251         $(eval CMD=$(HEX) $< $@)
252         @$(BUILD_CMD)
253         @if $(AUTOGEN); then \
254                 $(SILENT) || printf "Copying $(TARGET).hex to keymaps/$(KEYMAP)/$(KEYBOARD)_$(KEYMAP).hex\n"; \
255                 $(COPY) $@ $(KEYMAP_PATH)/$(KEYBOARD)_$(KEYMAP).hex; \
256         else \
257                 $(COPY) $@ $(TARGET).hex; \
258         fi
259
260 %.eep: %.elf
261         @$(SILENT) || printf "$(MSG_EEPROM) $@" | $(AWK_CMD)
262         $(eval CMD=$(EEP) $< $@ || exit 0)
263         @$(BUILD_CMD)
264
265 # Create extended listing file from ELF output file.
266 %.lss: %.elf
267         @$(SILENT) || printf "$(MSG_EXTENDED_LISTING) $@" | $(AWK_CMD)
268         $(eval CMD=$(OBJDUMP) -h -S -z $< > $@)
269         @$(BUILD_CMD)
270
271 # Create a symbol table from ELF output file.
272 %.sym: %.elf
273         @$(SILENT) || printf "$(MSG_SYMBOL_TABLE) $@" | $(AWK_CMD)
274         $(eval CMD=$(NM) -n $< > $@ )
275         @$(BUILD_CMD)
276
277 %.bin: %.elf
278         @$(SILENT) || printf "$(MSG_BIN) $@" | $(AWK_CMD)
279         $(eval CMD=$(BIN) $< $@ || exit 0)
280         @$(BUILD_CMD)
281
282 BEGIN = qmkversion gccversion sizebefore
283
284 # Link: create ELF output file from object files.
285 .SECONDARY : $(BUILD_DIR)/$(TARGET).elf
286 .PRECIOUS : $(OBJ)
287 # Note the obj.txt depeendency is there to force linking if a source file is deleted
288 %.elf: $(OBJ) $(MASTER_OUTPUT)/cflags.txt $(MASTER_OUTPUT)/ldflags.txt $(MASTER_OUTPUT)/obj.txt | $(BEGIN)
289         @$(SILENT) || printf "$(MSG_LINKING) $@" | $(AWK_CMD)
290         $(eval CMD=$(CC) $(ALL_CFLAGS) $(filter-out %.txt,$^) --output $@ $(LDFLAGS))
291         @$(BUILD_CMD)
292         
293
294 define GEN_OBJRULE
295 $1_INCFLAGS := $$(patsubst %,-I%,$$($1_INC))
296 ifdef $1_CONFIG
297 $1_CONFIG_FLAGS += -include $$($1_CONFIG)
298 endif
299 $1_CFLAGS = $$(ALL_CFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
300 $1_CPPFLAGS= $$(ALL_CPPFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
301 $1_ASFLAGS= $$(ALL_ASFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
302
303 # Compile: create object files from C source files.
304 $1/%.o : %.c $1/%.d $1/cflags.txt $1/compiler.txt | $(BEGIN)
305         @mkdir -p $$(@D)
306         @$$(SILENT) || printf "$$(MSG_COMPILING) $$<" | $$(AWK_CMD)
307         $$(eval CMD := $$(CC) -c $$($1_CFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
308         @$$(BUILD_CMD)
309
310 # Compile: create object files from C++ source files.
311 $1/%.o : %.cpp $1/%.d $1/cppflags.txt $1/compiler.txt | $(BEGIN)
312         @mkdir -p $$(@D)
313         @$$(SILENT) || printf "$$(MSG_COMPILING_CPP) $$<" | $$(AWK_CMD)
314         $$(eval CMD=$$(CC) -c $$($1_CPPFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
315         @$$(BUILD_CMD)
316
317 $1/%.o : %.cc $1/%.d $1/cppflags.txt $1/compiler.txt | $(BEGIN)
318         @mkdir -p $$(@D)
319         @$$(SILENT) || printf "$$(MSG_COMPILING_CPP) $$<" | $$(AWK_CMD)
320         $$(eval CMD=$$(CC) -c $$($1_CPPFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
321         @$$(BUILD_CMD)
322
323 # Assemble: create object files from assembler source files.
324 $1/%.o : %.S $1/asflags.txt $1/compiler.txt | $(BEGIN)
325         @mkdir -p $$(@D)
326         @$(SILENT) || printf "$$(MSG_ASSEMBLING) $$<" | $$(AWK_CMD)
327         $$(eval CMD=$$(CC) -c $$($1_ASFLAGS) $$< -o $$@)
328         @$$(BUILD_CMD)
329
330 $1/force:
331
332 $1/cflags.txt: $1/force
333         echo '$$($1_CFLAGS)' | cmp -s - $$@ || echo '$$($1_CFLAGS)' > $$@
334
335 $1/cppflags.txt: $1/force
336         echo '$$($1_CPPFLAGS)' | cmp -s - $$@ || echo '$$($1_CPPFLAGS)' > $$@
337
338 $1/asflags.txt: $1/force
339         echo '$$($1_ASFLAGS)' | cmp -s - $$@ || echo '$$($1_ASFLAGS)' > $$@
340
341 $1/compiler.txt: $1/force
342         $$(CC) --version | cmp -s - $$@ || $$(CC) --version > $$@
343 endef
344
345 .PRECIOUS: $(MASTER_OUTPUT)/obj.txt
346 $(MASTER_OUTPUT)/obj.txt: $(MASTER_OUTPUT)/force
347         echo '$(OBJ)' | cmp -s - $@ || echo '$(OBJ)' > $@
348
349 .PRECIOUS: $(MASTER_OUTPUT)/ldflags.txt
350 $(MASTER_OUTPUT)/ldflags.txt: $(MASTER_OUTPUT)/force
351         echo '$(LDFLAGS)' | cmp -s - $@ || echo '$(LDFLAGS)' > $@
352
353
354 # We have to use static rules for the .d files for some reason
355 DEPS = $(patsubst %.o,%.d,$(OBJ))
356 # Keep the .d files
357 .PRECIOUS: $(DEPS)
358 # Empty rule to force recompilation if the .d file is missing
359 $(DEPS):
360         
361
362 $(foreach OUTPUT,$(OUTPUTS),$(eval $(call GEN_OBJRULE,$(OUTPUT))))
363
364 # Create preprocessed source for use in sending a bug report.
365 %.i : %.c | $(BEGIN)
366         $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
367
368 # Target: clean project.
369 clean:
370         $(foreach OUTPUT,$(OUTPUTS), $(REMOVE) -r $(OUTPUT) 2>/dev/null)
371         $(REMOVE) $(BUILD_DIR)/$(TARGET).*
372
373 show_path:
374         @echo VPATH=$(VPATH)
375         @echo SRC=$(SRC)
376         @echo OBJ=$(OBJ)
377
378 # Create build directory
379 $(shell mkdir -p $(BUILD_DIR) 2>/dev/null)
380
381 # Create object files directory
382 $(eval $(foreach OUTPUT,$(OUTPUTS),$(shell mkdir -p $(OUTPUT) 2>/dev/null)))
383
384 # Include the dependency files.
385 -include $(patsubst %.o,%.d,$(OBJ))
386
387
388 # Listing of phony targets.
389 .PHONY : all finish sizebefore sizeafter qmkversion \
390 gccversion build elf hex eep lss sym coff extcoff \
391 clean clean_list debug gdb-config show_path \
392 program teensy dfu flip dfu-ee flip-ee dfu-start