]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/rules.mk
7b2c842ed6042499264df5ced7c321d005c7d08e
[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 %.hpp $(VPATH_SRC)
25 vpath %.S $(VPATH_SRC)
26 VPATH :=
27
28 # Convert all SRC to OBJ
29 define OBJ_FROM_SRC
30 $(patsubst %.c,$1/%.o,$(patsubst %.cpp,$1/%.o,$(patsubst %.S,$1/%.o,$($1_SRC))))
31 endef
32 $(foreach OUTPUT,$(OUTPUTS),$(eval $(OUTPUT)_OBJ +=$(call OBJ_FROM_SRC,$(OUTPUT))))
33
34 # Define a list of all objects
35 OBJ := $(foreach OUTPUT,$(OUTPUTS),$($(OUTPUT)_OBJ))
36
37 MASTER_OUTPUT := $(firstword $(OUTPUTS))
38
39
40
41 # Output format. (can be srec, ihex, binary)
42 FORMAT = ihex
43
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.)
47 OPT = s
48
49 AUTOGEN ?= false
50
51
52 # Compiler flag to set the C Standard level.
53 #     c89   = "ANSI" C
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
58
59
60 # Place -D or -U options here for C sources
61 #CDEFS +=
62
63
64 # Place -D or -U options here for ASM sources
65 #ADEFS +=
66
67
68 # Place -D or -U options here for C++ sources
69 #CPPDEFS += -D__STDC_LIMIT_MACROS
70 #CPPDEFS += -D__STDC_CONSTANT_MACROS
71 #CPPDEFS +=
72
73
74
75
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
83 CFLAGS += -g$(DEBUG)
84 CFLAGS += $(CDEFS)
85 CFLAGS += -O$(OPT)
86 # add color
87 ifeq ($(COLOR),true)
88 ifeq ("$(shell echo "int main(){}" | $(CC) -fdiagnostics-color -x c - -o /dev/null 2>&1)", "")
89         CFLAGS+= -fdiagnostics-color
90 endif
91 endif
92 CFLAGS += -Wall
93 CFLAGS += -Wstrict-prototypes
94 #CFLAGS += -mshort-calls
95 #CFLAGS += -fno-unit-at-a-time
96 #CFLAGS += -Wundef
97 #CFLAGS += -Wunreachable-code
98 #CFLAGS += -Wsign-compare
99 CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
100 CFLAGS += $(CSTANDARD)
101 ifdef CONFIG_H
102     CFLAGS += -include $(CONFIG_H)
103 endif
104
105
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)
115 CPPFLAGS += -O$(OPT)
116 # to supress "warning: only initialized variables can be placed into program memory area"
117 CPPFLAGS += -w
118 CPPFLAGS += -Wall
119 CPPFLAGS += -Wundef
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)
127 ifdef CONFIG_H
128     CPPFLAGS += -include $(CONFIG_H)
129 endif
130
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 ifdef CONFIG_H
144     ASFLAGS += -include $(CONFIG_H)
145 endif
146
147 #---------------- Library Options ----------------
148 # Minimalistic printf version
149 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
150
151 # Floating point printf version (requires MATH_LIB = -lm below)
152 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
153
154 # If this is left blank, then it will use the Standard printf version.
155 PRINTF_LIB =
156 #PRINTF_LIB = $(PRINTF_LIB_MIN)
157 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
158
159
160 # Minimalistic scanf version
161 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
162
163 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
164 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
165
166 # If this is left blank, then it will use the Standard scanf version.
167 SCANF_LIB =
168 #SCANF_LIB = $(SCANF_LIB_MIN)
169 #SCANF_LIB = $(SCANF_LIB_FLOAT)
170
171
172 MATH_LIB = -lm
173
174
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
179 #
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'
182 #
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)
191
192 # Define programs and commands.
193 SHELL = sh
194 REMOVE = rm -f
195 REMOVEDIR = rmdir
196 COPY = cp
197 WINSHELL = cmd
198 SECHO = $(SILENT) || echo
199
200
201 # Compiler flags to generate dependency files.
202 #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
203 GENDEPFLAGS = -MMD -MP -MF $(patsubst %.o,%.td,$@)
204
205
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)
212
213 MOVE_DEP = mv -f $(patsubst %.o,%.td,$@) $(patsubst %.o,%.d,$@)
214
215 # Default target.
216 all: build sizeafter
217
218 # Change the build target to build a HEX file or a library.
219 build: elf hex
220 #build: elf hex eep lss sym
221 #build: lib
222
223
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
230 lib: $(LIBNAME)
231
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
236
237 sizebefore:
238         @if test -f $(TARGET).hex; then $(SECHO) $(MSG_SIZE_BEFORE); $(SILENT) || $(HEXSIZE); \
239         2>/dev/null; $(SECHO); fi
240
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
246
247 # Display compiler version information.
248 gccversion :
249         @$(SILENT) || $(CC) --version
250
251 # Create final output files (.hex, .eep) from ELF output file.
252 %.hex: %.elf
253         @$(SILENT) || printf "$(MSG_FLASH) $@" | $(AWK_CMD)
254         $(eval CMD=$(HEX) $< $@)
255         @$(BUILD_CMD)
256         @if $(AUTOGEN); then \
257                 $(SILENT) || printf "Copying $(TARGET).hex to keymaps/$(KEYMAP)/$(KEYBOARD)_$(KEYMAP).hex\n"; \
258                 $(COPY) $@ $(KEYMAP_PATH)/$(KEYBOARD)_$(KEYMAP).hex; \
259         else \
260                 $(COPY) $@ $(TARGET).hex; \
261         fi
262
263 %.eep: %.elf
264         @$(SILENT) || printf "$(MSG_EEPROM) $@" | $(AWK_CMD)
265         $(eval CMD=$(EEP) $< $@ || exit 0)
266         @$(BUILD_CMD)
267
268 # Create extended listing file from ELF output file.
269 %.lss: %.elf
270         @$(SILENT) || printf "$(MSG_EXTENDED_LISTING) $@" | $(AWK_CMD)
271         $(eval CMD=$(OBJDUMP) -h -S -z $< > $@)
272         @$(BUILD_CMD)
273
274 # Create a symbol table from ELF output file.
275 %.sym: %.elf
276         @$(SILENT) || printf "$(MSG_SYMBOL_TABLE) $@" | $(AWK_CMD)
277         $(eval CMD=$(NM) -n $< > $@ )
278         @$(BUILD_CMD)
279
280 %.bin: %.elf
281         @$(SILENT) || printf "$(MSG_BIN) $@" | $(AWK_CMD)
282         $(eval CMD=$(BIN) $< $@ || exit 0)
283         @$(BUILD_CMD)
284
285 BEGIN = gccversion sizebefore
286
287 # Link: create ELF output file from object files.
288 .SECONDARY : $(BUILD_DIR)/$(TARGET).elf
289 .PRECIOUS : $(OBJ)
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))
294         @$(BUILD_CMD)
295         
296
297 define GEN_OBJRULE
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))
303
304 # Compile: create object files from C source files.
305 $1/%.o : %.c $1/%.d $1/cflags.txt $1/compiler.txt | $(BEGIN)
306         @mkdir -p $$(@D)
307         @$$(SILENT) || printf "$$(MSG_COMPILING) $$<" | $$(AWK_CMD)
308         $$(eval CMD := $$(CC) -c $$($1_CFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
309         @$$(BUILD_CMD)
310
311 # Compile: create object files from C++ source files.
312 $1/%.o : %.cpp $1/%.d $1/cppflags.txt $1/compiler.txt | $(BEGIN)
313         @mkdir -p $$(@D)
314         @$$(SILENT) || printf "$$(MSG_COMPILING_CPP) $$<" | $$(AWK_CMD)
315         $$(eval CMD=$$(CC) -c $$($1_CPPFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
316         @$(BUILD_CMD)
317
318 # Assemble: create object files from assembler source files.
319 $1/%.o : %.S $1/asflags.txt $1/compiler.txt | $(BEGIN)
320         @mkdir -p $$(@D)
321         @$(SILENT) || printf "$$(MSG_ASSEMBLING) $$<" | $$(AWK_CMD)
322         $$(eval CMD=$$(CC) -c $$($1_ASFLAGS) $$< -o $$@)
323         @$$(BUILD_CMD)
324
325 $1/force:
326
327 $1/cflags.txt: $1/force
328         echo '$$($1_CFLAGS)' | cmp -s - $$@ || echo '$$($1_CFLAGS)' > $$@
329
330 $1/cppflags.txt: $1/force
331         echo '$$($1_CPPFLAGS)' | cmp -s - $$@ || echo '$$($1_CPPFLAGS)' > $$@
332
333 $1/asflags.txt: $1/force
334         echo '$$($1_ASFLAGS)' | cmp -s - $$@ || echo '$$($1_ASFLAGS)' > $$@
335
336 $1/compiler.txt: $1/force
337         $$(CC) --version | cmp -s - $$@ || $$(CC) --version > $$@
338 endef
339
340 $(MASTER_OUTPUT)/obj.txt: $(MASTER_OUTPUT)/force
341         echo '$(OBJ)' | cmp -s - $$@ || echo '$(OBJ)' > $$@
342
343 $(MASTER_OUTPUT)/ldflags.txt: $(MASTER_OUTPUT)/force
344         echo '$(LDFLAGS)' | cmp -s - $$@ || echo '$(LDFLAGS)' > $$@
345
346
347 # We have to use static rules for the .d files for some reason
348 DEPS = $(patsubst %.o,%.d,$(OBJ))
349 # Keep the .d files
350 .PRECIOUS: $(DEPS)
351 # Empty rule to force recompilation if the .d file is missing
352 $(DEPS):
353         
354
355 $(foreach OUTPUT,$(OUTPUTS),$(eval $(call GEN_OBJRULE,$(OUTPUT))))
356
357 # Create preprocessed source for use in sending a bug report.
358 %.i : %.c | $(BEGIN)
359         $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
360
361 # Target: clean project.
362 clean:
363         $(REMOVE) -r $(OBJDIR) 2>/dev/null
364         $(REMOVE) -r $(KBOBJDIR) 2>/dev/null
365         $(REMOVE) $(BUILD_DIR)/$(TARGET).*
366
367 show_path:
368         @echo VPATH=$(VPATH)
369         @echo SRC=$(SRC)
370         @echo OBJ=$(OBJ)
371
372 # Create build directory
373 $(shell mkdir $(BUILD_DIR) 2>/dev/null)
374
375 # Create object files directory
376 $(eval $(foreach OUTPUT,$(OUTPUTS),$(shell mkdir $(OUTPUT) 2>/dev/null)))
377
378 # Include the dependency files.
379 -include $(patsubst %.o,%.d,$(OBJ))
380
381
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