]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/rules.mk
Merge pull request #666 from fredizzimo/makefile_overhaul
[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
102
103 #---------------- Compiler Options C++ ----------------
104 #  -g*:          generate debugging information
105 #  -O*:          optimization level
106 #  -f...:        tuning, see GCC manual and avr-libc documentation
107 #  -Wall...:     warning level
108 #  -Wa,...:      tell GCC to pass this to the assembler.
109 #    -adhlns...: create assembler listing
110 CPPFLAGS += -g$(DEBUG)
111 CPPFLAGS += $(CPPDEFS)
112 CPPFLAGS += -O$(OPT)
113 # to supress "warning: only initialized variables can be placed into program memory area"
114 CPPFLAGS += -w
115 CPPFLAGS += -Wall
116 CPPFLAGS += -Wundef
117 #CPPFLAGS += -mshort-calls
118 #CPPFLAGS += -fno-unit-at-a-time
119 #CPPFLAGS += -Wstrict-prototypes
120 #CPPFLAGS += -Wunreachable-code
121 #CPPFLAGS += -Wsign-compare
122 CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
123 #CPPFLAGS += $(CSTANDARD)
124
125 #---------------- Assembler Options ----------------
126 #  -Wa,...:   tell GCC to pass this to the assembler.
127 #  -adhlns:   create listing
128 #  -gstabs:   have the assembler create line number information; note that
129 #             for use in COFF files, additional information about filenames
130 #             and function names needs to be present in the assembler source
131 #             files -- see avr-libc docs [FIXME: not yet described there]
132 #  -listing-cont-lines: Sets the maximum number of continuation lines of hex
133 #       dump that will be displayed for a given single line of source input.
134 ASFLAGS += $(ADEFS) 
135 ASFLAGS += -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
136
137 #---------------- Library Options ----------------
138 # Minimalistic printf version
139 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
140
141 # Floating point printf version (requires MATH_LIB = -lm below)
142 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
143
144 # If this is left blank, then it will use the Standard printf version.
145 PRINTF_LIB =
146 #PRINTF_LIB = $(PRINTF_LIB_MIN)
147 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
148
149
150 # Minimalistic scanf version
151 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
152
153 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
154 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
155
156 # If this is left blank, then it will use the Standard scanf version.
157 SCANF_LIB =
158 #SCANF_LIB = $(SCANF_LIB_MIN)
159 #SCANF_LIB = $(SCANF_LIB_FLOAT)
160
161
162 MATH_LIB = -lm
163
164
165 #---------------- Linker Options ----------------
166 #  -Wl,...:     tell GCC to pass this to linker.
167 #    -Map:      create map file
168 #    --cref:    add cross reference to  map file
169 #
170 # Comennt out "--relax" option to avoid a error such:
171 #       (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
172 #
173 LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref
174 #LDFLAGS += -Wl,--relax
175 LDFLAGS += $(EXTMEMOPTS)
176 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
177 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
178 #LDFLAGS += -T linker_script.x
179 # You can give EXTRALDFLAGS at 'make' command line.
180 LDFLAGS += $(EXTRALDFLAGS)
181
182 # Define programs and commands.
183 SHELL = sh
184 REMOVE = rm -f
185 REMOVEDIR = rmdir
186 COPY = cp
187 WINSHELL = cmd
188 SECHO = $(SILENT) || echo
189
190
191 # Compiler flags to generate dependency files.
192 #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
193 GENDEPFLAGS = -MMD -MP -MF $(patsubst %.o,%.td,$@)
194
195
196 # Combine all necessary flags and optional flags.
197 # Add target processor to flags.
198 # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
199 ALL_CFLAGS = $(MCUFLAGS) $(CFLAGS) $(EXTRAFLAGS)
200 ALL_CPPFLAGS = $(MCUFLAGS) -x c++ $(CPPFLAGS) $(EXTRAFLAGS)
201 ALL_ASFLAGS = $(MCUFLAGS) -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
202
203 MOVE_DEP = mv -f $(patsubst %.o,%.td,$@) $(patsubst %.o,%.d,$@)
204
205 # Default target.
206 all: build sizeafter
207
208 # Change the build target to build a HEX file or a library.
209 build: elf hex
210 #build: elf hex eep lss sym
211 #build: lib
212
213
214 elf: $(BUILD_DIR)/$(TARGET).elf
215 hex: $(BUILD_DIR)/$(TARGET).hex
216 eep: $(BUILD_DIR)/$(TARGET).eep
217 lss: $(BUILD_DIR)/$(TARGET).lss
218 sym: $(BUILD_DIR)/$(TARGET).sym
219 LIBNAME=lib$(TARGET).a
220 lib: $(LIBNAME)
221
222 # Display size of file.
223 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
224 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
225 ELFSIZE = $(SIZE) $(BUILD_DIR)/$(TARGET).elf
226
227 sizebefore:
228         @if test -f $(TARGET).hex; then $(SECHO) $(MSG_SIZE_BEFORE); $(SILENT) || $(HEXSIZE); \
229         2>/dev/null; $(SECHO); fi
230
231 sizeafter: $(BUILD_DIR)/$(TARGET).hex
232         @if test -f $(TARGET).hex; then $(SECHO); $(SECHO) $(MSG_SIZE_AFTER); $(SILENT) || $(HEXSIZE); \
233         2>/dev/null; $(SECHO); fi
234         # test file sizes eventually
235         # @if [[ $($(SIZE) --target=$(FORMAT) $(TARGET).hex | $(AWK) 'NR==2 {print "0x"$5}') -gt 0x200 ]]; then $(SECHO) "File is too big!"; fi
236
237 # Display compiler version information.
238 gccversion :
239         @$(SILENT) || $(CC) --version
240
241 # Create final output files (.hex, .eep) from ELF output file.
242 %.hex: %.elf
243         @$(SILENT) || printf "$(MSG_FLASH) $@" | $(AWK_CMD)
244         $(eval CMD=$(HEX) $< $@)
245         @$(BUILD_CMD)
246         @if $(AUTOGEN); then \
247                 $(SILENT) || printf "Copying $(TARGET).hex to keymaps/$(KEYMAP)/$(KEYBOARD)_$(KEYMAP).hex\n"; \
248                 $(COPY) $@ $(KEYMAP_PATH)/$(KEYBOARD)_$(KEYMAP).hex; \
249         else \
250                 $(COPY) $@ $(TARGET).hex; \
251         fi
252
253 %.eep: %.elf
254         @$(SILENT) || printf "$(MSG_EEPROM) $@" | $(AWK_CMD)
255         $(eval CMD=$(EEP) $< $@ || exit 0)
256         @$(BUILD_CMD)
257
258 # Create extended listing file from ELF output file.
259 %.lss: %.elf
260         @$(SILENT) || printf "$(MSG_EXTENDED_LISTING) $@" | $(AWK_CMD)
261         $(eval CMD=$(OBJDUMP) -h -S -z $< > $@)
262         @$(BUILD_CMD)
263
264 # Create a symbol table from ELF output file.
265 %.sym: %.elf
266         @$(SILENT) || printf "$(MSG_SYMBOL_TABLE) $@" | $(AWK_CMD)
267         $(eval CMD=$(NM) -n $< > $@ )
268         @$(BUILD_CMD)
269
270 %.bin: %.elf
271         @$(SILENT) || printf "$(MSG_BIN) $@" | $(AWK_CMD)
272         $(eval CMD=$(BIN) $< $@ || exit 0)
273         @$(BUILD_CMD)
274
275 BEGIN = gccversion sizebefore
276
277 # Link: create ELF output file from object files.
278 .SECONDARY : $(BUILD_DIR)/$(TARGET).elf
279 .PRECIOUS : $(OBJ)
280 # Note the obj.txt depeendency is there to force linking if a source file is deleted
281 %.elf: $(OBJ) $(MASTER_OUTPUT)/cflags.txt $(MASTER_OUTPUT)/ldflags.txt $(MASTER_OUTPUT)/obj.txt | $(BEGIN)
282         @$(SILENT) || printf "$(MSG_LINKING) $@" | $(AWK_CMD)
283         $(eval CMD=$(CC) $(ALL_CFLAGS) $(filter-out %.txt,$^) --output $@ $(LDFLAGS))
284         @$(BUILD_CMD)
285         
286
287 define GEN_OBJRULE
288 $1_INCFLAGS := $$(patsubst %,-I%,$$($1_INC))
289 ifdef $1_CONFIG
290 $1_CONFIG_FLAGS += -include $$($1_CONFIG)
291 endif
292 $1_CFLAGS = $$(ALL_CFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
293 $1_CPPFLAGS= $$(ALL_CPPFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
294 $1_ASFLAGS= $$(ALL_ASFLAGS) $$($1_DEFS) $$($1_INCFLAGS) $$($1_CONFIG_FLAGS)
295
296 # Compile: create object files from C source files.
297 $1/%.o : %.c $1/%.d $1/cflags.txt $1/compiler.txt | $(BEGIN)
298         @mkdir -p $$(@D)
299         @$$(SILENT) || printf "$$(MSG_COMPILING) $$<" | $$(AWK_CMD)
300         $$(eval CMD := $$(CC) -c $$($1_CFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
301         @$$(BUILD_CMD)
302
303 # Compile: create object files from C++ source files.
304 $1/%.o : %.cpp $1/%.d $1/cppflags.txt $1/compiler.txt | $(BEGIN)
305         @mkdir -p $$(@D)
306         @$$(SILENT) || printf "$$(MSG_COMPILING_CPP) $$<" | $$(AWK_CMD)
307         $$(eval CMD=$$(CC) -c $$($1_CPPFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
308         @$(BUILD_CMD)
309
310 # Assemble: create object files from assembler source files.
311 $1/%.o : %.S $1/asflags.txt $1/compiler.txt | $(BEGIN)
312         @mkdir -p $$(@D)
313         @$(SILENT) || printf "$$(MSG_ASSEMBLING) $$<" | $$(AWK_CMD)
314         $$(eval CMD=$$(CC) -c $$($1_ASFLAGS) $$< -o $$@)
315         @$$(BUILD_CMD)
316
317 $1/force:
318
319 $1/cflags.txt: $1/force
320         echo '$$($1_CFLAGS)' | cmp -s - $$@ || echo '$$($1_CFLAGS)' > $$@
321
322 $1/cppflags.txt: $1/force
323         echo '$$($1_CPPFLAGS)' | cmp -s - $$@ || echo '$$($1_CPPFLAGS)' > $$@
324
325 $1/asflags.txt: $1/force
326         echo '$$($1_ASFLAGS)' | cmp -s - $$@ || echo '$$($1_ASFLAGS)' > $$@
327
328 $1/compiler.txt: $1/force
329         $$(CC) --version | cmp -s - $$@ || $$(CC) --version > $$@
330 endef
331
332 $(MASTER_OUTPUT)/obj.txt: $(MASTER_OUTPUT)/force
333         echo '$(OBJ)' | cmp -s - $$@ || echo '$(OBJ)' > $$@
334
335 $(MASTER_OUTPUT)/ldflags.txt: $(MASTER_OUTPUT)/force
336         echo '$(LDFLAGS)' | cmp -s - $$@ || echo '$(LDFLAGS)' > $$@
337
338
339 # We have to use static rules for the .d files for some reason
340 DEPS = $(patsubst %.o,%.d,$(OBJ))
341 # Keep the .d files
342 .PRECIOUS: $(DEPS)
343 # Empty rule to force recompilation if the .d file is missing
344 $(DEPS):
345         
346
347 $(foreach OUTPUT,$(OUTPUTS),$(eval $(call GEN_OBJRULE,$(OUTPUT))))
348
349 # Create preprocessed source for use in sending a bug report.
350 %.i : %.c | $(BEGIN)
351         $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
352
353 # Target: clean project.
354 clean:
355         $(foreach OUTPUT,$(OUTPUTS), $(REMOVE) -r $(OUTPUT) 2>/dev/null)
356         $(REMOVE) $(BUILD_DIR)/$(TARGET).*
357
358 show_path:
359         @echo VPATH=$(VPATH)
360         @echo SRC=$(SRC)
361         @echo OBJ=$(OBJ)
362
363 # Create build directory
364 $(shell mkdir $(BUILD_DIR) 2>/dev/null)
365
366 # Create object files directory
367 $(eval $(foreach OUTPUT,$(OUTPUTS),$(shell mkdir $(OUTPUT) 2>/dev/null)))
368
369 # Include the dependency files.
370 -include $(patsubst %.o,%.d,$(OBJ))
371
372
373 # Listing of phony targets.
374 .PHONY : all finish sizebefore sizeafter gccversion \
375 build elf hex eep lss sym coff extcoff \
376 clean clean_list debug gdb-config show_path \
377 program teensy dfu flip dfu-ee flip-ee dfu-start