]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/rules.mk
f97c5cb49db8f6fa304187b149a4bbe53976a751
[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
29 # Output format. (can be srec, ihex, binary)
30 FORMAT = ihex
31
32 # Optimization level, can be [0, 1, 2, 3, s].
33 #     0 = turn off optimization. s = optimize for size.
34 #     (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
35 OPT = s
36
37 COLOR ?= true
38
39 ifeq ($(COLOR),true)
40         NO_COLOR=\033[0m
41         OK_COLOR=\033[32;01m
42         ERROR_COLOR=\033[31;01m
43         WARN_COLOR=\033[33;01m
44         BLUE=\033[0;34m
45         BOLD=\033[1m
46 endif
47
48 AUTOGEN ?= false
49
50 ifneq ($(shell awk --version 2>/dev/null),)
51         AWK=awk
52 else
53         AWK=cat && test
54 endif
55
56 OK_STRING=$(OK_COLOR)[OK]$(NO_COLOR)\n
57 ERROR_STRING=$(ERROR_COLOR)[ERRORS]$(NO_COLOR)\n
58 WARN_STRING=$(WARN_COLOR)[WARNINGS]$(NO_COLOR)\n
59
60 ifndef $(SILENT)
61         SILENT = false
62 endif
63
64 TAB_LOG = printf "\n$$LOG\n\n" | $(AWK) '{ sub(/^/," | "); print }'
65 TAB_LOG_PLAIN = printf "$$LOG\n"
66 AWK_STATUS = $(AWK) '{ printf " %-10s\n", $$1; }'
67 AWK_CMD = $(AWK) '{ printf "%-99s", $$0; }'
68 PRINT_ERROR = ($(SILENT) ||printf " $(ERROR_STRING)" | $(AWK_STATUS)) && $(TAB_LOG) && exit 1
69 PRINT_WARNING = ($(SILENT) || printf " $(WARN_STRING)" | $(AWK_STATUS)) && $(TAB_LOG)
70 PRINT_ERROR_PLAIN = ($(SILENT) ||printf " $(ERROR_STRING)" | $(AWK_STATUS)) && $(TAB_LOG_PLAIN) && exit 1
71 PRINT_WARNING_PLAIN = ($(SILENT) || printf " $(WARN_STRING)" | $(AWK_STATUS)) && $(TAB_LOG_PLAIN)
72 PRINT_OK = $(SILENT) || printf " $(OK_STRING)" | $(AWK_STATUS)
73 BUILD_CMD = LOG=$$($(CMD) 2>&1) ; if [ $$? -gt 0 ]; then $(PRINT_ERROR); elif [ "$$LOG" != "" ] ; then $(PRINT_WARNING); else $(PRINT_OK); fi;
74
75 # List any extra directories to look for include files here.
76 #     Each directory must be seperated by a space.
77 #     Use forward slashes for directory separators.
78 #     For a directory that has spaces, enclose it in quotes.
79 EXTRAINCDIRS += $(subst :, ,$(VPATH_SRC))
80
81
82 # Compiler flag to set the C Standard level.
83 #     c89   = "ANSI" C
84 #     gnu89 = c89 plus GCC extensions
85 #     c99   = ISO C99 standard (not yet fully implemented)
86 #     gnu99 = c99 plus GCC extensions
87 CSTANDARD = -std=gnu99
88
89
90 # Place -D or -U options here for C sources
91 CDEFS += $(OPT_DEFS)
92
93
94 # Place -D or -U options here for ASM sources
95 ADEFS += $(OPT_DEFS)
96
97
98 # Place -D or -U options here for C++ sources
99 #CPPDEFS += -D__STDC_LIMIT_MACROS
100 #CPPDEFS += -D__STDC_CONSTANT_MACROS
101 CPPDEFS += $(OPT_DEFS)
102
103
104
105 #---------------- Compiler Options C ----------------
106 #  -g*:          generate debugging information
107 #  -O*:          optimization level
108 #  -f...:        tuning, see GCC manual and avr-libc documentation
109 #  -Wall...:     warning level
110 #  -Wa,...:      tell GCC to pass this to the assembler.
111 #    -adhlns...: create assembler listing
112 CFLAGS += -g$(DEBUG)
113 CFLAGS += $(CDEFS)
114 CFLAGS += -O$(OPT)
115 # add color
116 ifeq ($(COLOR),true)
117 ifeq ("$(shell echo "int main(){}" | $(CC) -fdiagnostics-color -x c - -o /dev/null 2>&1)", "")
118         CFLAGS+= -fdiagnostics-color
119 endif
120 endif
121 CFLAGS += -Wall
122 CFLAGS += -Wstrict-prototypes
123 #CFLAGS += -mshort-calls
124 #CFLAGS += -fno-unit-at-a-time
125 #CFLAGS += -Wundef
126 #CFLAGS += -Wunreachable-code
127 #CFLAGS += -Wsign-compare
128 CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
129 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
130 CFLAGS += $(CSTANDARD)
131 ifdef CONFIG_H
132     CFLAGS += -include $(CONFIG_H)
133 endif
134
135
136 #---------------- Compiler Options C++ ----------------
137 #  -g*:          generate debugging information
138 #  -O*:          optimization level
139 #  -f...:        tuning, see GCC manual and avr-libc documentation
140 #  -Wall...:     warning level
141 #  -Wa,...:      tell GCC to pass this to the assembler.
142 #    -adhlns...: create assembler listing
143 CPPFLAGS += -g$(DEBUG)
144 CPPFLAGS += $(CPPDEFS)
145 CPPFLAGS += -O$(OPT)
146 # to supress "warning: only initialized variables can be placed into program memory area"
147 CPPFLAGS += -w
148 CPPFLAGS += -Wall
149 CPPFLAGS += -Wundef
150 #CPPFLAGS += -mshort-calls
151 #CPPFLAGS += -fno-unit-at-a-time
152 #CPPFLAGS += -Wstrict-prototypes
153 #CPPFLAGS += -Wunreachable-code
154 #CPPFLAGS += -Wsign-compare
155 CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
156 CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
157 #CPPFLAGS += $(CSTANDARD)
158 ifdef CONFIG_H
159     CPPFLAGS += -include $(CONFIG_H)
160 endif
161
162
163 #---------------- Assembler Options ----------------
164 #  -Wa,...:   tell GCC to pass this to the assembler.
165 #  -adhlns:   create listing
166 #  -gstabs:   have the assembler create line number information; note that
167 #             for use in COFF files, additional information about filenames
168 #             and function names needs to be present in the assembler source
169 #             files -- see avr-libc docs [FIXME: not yet described there]
170 #  -listing-cont-lines: Sets the maximum number of continuation lines of hex
171 #       dump that will be displayed for a given single line of source input.
172 ASFLAGS += $(ADEFS) -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
173 ASFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
174 ifdef CONFIG_H
175     ASFLAGS += -include $(CONFIG_H)
176 endif
177
178 #---------------- Library Options ----------------
179 # Minimalistic printf version
180 PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
181
182 # Floating point printf version (requires MATH_LIB = -lm below)
183 PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
184
185 # If this is left blank, then it will use the Standard printf version.
186 PRINTF_LIB =
187 #PRINTF_LIB = $(PRINTF_LIB_MIN)
188 #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
189
190
191 # Minimalistic scanf version
192 SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
193
194 # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
195 SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
196
197 # If this is left blank, then it will use the Standard scanf version.
198 SCANF_LIB =
199 #SCANF_LIB = $(SCANF_LIB_MIN)
200 #SCANF_LIB = $(SCANF_LIB_FLOAT)
201
202
203 MATH_LIB = -lm
204
205
206 # List any extra directories to look for libraries here.
207 #     Each directory must be seperated by a space.
208 #     Use forward slashes for directory separators.
209 #     For a directory that has spaces, enclose it in quotes.
210 EXTRALIBDIRS = 
211
212
213
214 #---------------- External Memory Options ----------------
215
216 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
217 # used for variables (.data/.bss) and heap (malloc()).
218 #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
219
220 # 64 KB of external RAM, starting after internal RAM (ATmega128!),
221 # only used for heap (malloc()).
222 #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
223
224 EXTMEMOPTS =
225
226
227
228 #---------------- Linker Options ----------------
229 #  -Wl,...:     tell GCC to pass this to linker.
230 #    -Map:      create map file
231 #    --cref:    add cross reference to  map file
232 #
233 # Comennt out "--relax" option to avoid a error such:
234 #       (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
235 #
236 LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref
237 #LDFLAGS += -Wl,--relax
238 LDFLAGS += $(EXTMEMOPTS)
239 LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
240 LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
241 #LDFLAGS += -T linker_script.x
242 # You can give EXTRALDFLAGS at 'make' command line.
243 LDFLAGS += $(EXTRALDFLAGS)
244
245
246
247 #---------------- Debugging Options ----------------
248
249 # For simulavr only - target MCU frequency.
250 DEBUG_MFREQ = $(F_CPU)
251
252 # Set the DEBUG_UI to either gdb or insight.
253 # DEBUG_UI = gdb
254 DEBUG_UI = insight
255
256 # Set the debugging back-end to either avarice, simulavr.
257 DEBUG_BACKEND = avarice
258 #DEBUG_BACKEND = simulavr
259
260 # GDB Init Filename.
261 GDBINIT_FILE = __avr_gdbinit
262
263 # When using avarice settings for the JTAG
264 JTAG_DEV = /dev/com1
265
266 # Debugging port used to communicate between GDB / avarice / simulavr.
267 DEBUG_PORT = 4242
268
269 # Debugging host used to communicate between GDB / avarice / simulavr, normally
270 #     just set to localhost unless doing some sort of crazy debugging when 
271 #     avarice is running on a different computer.
272 DEBUG_HOST = localhost
273
274
275
276 #============================================================================
277
278
279 # Define programs and commands.
280 SHELL = sh
281 REMOVE = rm -f
282 REMOVEDIR = rmdir
283 COPY = cp
284 WINSHELL = cmd
285 SECHO = $(SILENT) || echo
286
287 # Define Messages
288 # English
289 MSG_ERRORS_NONE = Errors: none
290 MSG_BEGIN = -------- begin --------
291 MSG_END = --------  end  --------
292 MSG_SIZE_BEFORE = Size before:
293 MSG_SIZE_AFTER = Size after:
294 MSG_COFF = Converting to AVR COFF:
295 MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
296 MSG_FLASH = Creating load file for Flash:
297 MSG_EEPROM = Creating load file for EEPROM:
298 MSG_BIN = Creating binary load file for Flash:
299 MSG_EXTENDED_LISTING = Creating Extended Listing:
300 MSG_SYMBOL_TABLE = Creating Symbol Table:
301 MSG_LINKING = Linking:
302 MSG_COMPILING = Compiling:
303 MSG_COMPILING_CPP = Compiling:
304 MSG_ASSEMBLING = Assembling:
305 MSG_CLEANING = Cleaning project:
306 MSG_CREATING_LIBRARY = Creating library:
307 MSG_SUBMODULE_DIRTY = $(WARN_COLOR)WARNING:$(NO_COLOR)\n \
308         Some git sub-modules are out of date or modified, please consider runnning:$(BOLD)\n\
309         git submodule sync --recursive\n\
310         git submodule update --init --recursive$(NO_COLOR)\n\n\
311         You can ignore this warning if you are not compiling any ChibiOS keyboards,\n\
312         or if you have modified the ChibiOS libraries yourself. \n\n
313
314
315 # Define all object files.
316 OBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(patsubst %.cpp,$(OBJDIR)/%.o,$(patsubst %.S,$(OBJDIR)/%.o,$(SRC))))
317 # The files in the lib folder are shared between all keymaps, so generate that folder name by removing
318 # the keymap from the name
319 KBOBJDIR=$(subst _$(KEYMAP),,$(OBJDIR))
320 # And fixup the object files to match
321 LIBOBJ = $(foreach v,$(OBJ),$(if $(findstring /lib/,$v),$v))
322 NONLIBOBJ := $(filter-out $(LIBOBJ),$(OBJ))
323 LIBOBJ := $(subst _$(KEYMAP)/,/,$(LIBOBJ))
324 OBJ := $(LIBOBJ) $(NONLIBOBJ)
325
326 # Define all listing files.
327 LST = $(patsubst %.c,$(OBJDIR)/%.lst,$(patsubst %.cpp,$(OBJDIR)/%.lst,$(patsubst %.S,$(OBJDIR)/%.lst,$(SRC))))
328
329
330 # Compiler flags to generate dependency files.
331 #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
332 GENDEPFLAGS = -MMD -MP -MF $(patsubst %.o,%.td,$@)
333
334
335 # Combine all necessary flags and optional flags.
336 # Add target processor to flags.
337 # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
338 ALL_CFLAGS = $(MCUFLAGS) $(CFLAGS) $(EXTRAFLAGS)
339 ALL_CPPFLAGS = $(MCUFLAGS) -x c++ $(CPPFLAGS) $(EXTRAFLAGS)
340 ALL_ASFLAGS = $(MCUFLAGS) -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
341
342 MOVE_DEP = mv -f $(patsubst %.o,%.td,$@) $(patsubst %.o,%.d,$@)
343
344 # Default target.
345 all: build sizeafter
346
347 # Change the build target to build a HEX file or a library.
348 build: elf hex
349 #build: elf hex eep lss sym
350 #build: lib
351
352
353 elf: $(BUILD_DIR)/$(TARGET).elf
354 hex: $(BUILD_DIR)/$(TARGET).hex
355 eep: $(BUILD_DIR)/$(TARGET).eep
356 lss: $(BUILD_DIR)/$(TARGET).lss
357 sym: $(BUILD_DIR)/$(TARGET).sym
358 LIBNAME=lib$(TARGET).a
359 lib: $(LIBNAME)
360
361 check_submodule:
362         git submodule status --recursive | \
363         while IFS= read -r x; do \
364                 case "$$x" in \
365                         \ *) ;; \
366                         *) printf "$(MSG_SUBMODULE_DIRTY)";break;; \
367                 esac \
368         done
369
370 # Display size of file.
371 HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
372 #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
373 ELFSIZE = $(SIZE) $(BUILD_DIR)/$(TARGET).elf
374
375 sizebefore:
376         @if test -f $(TARGET).hex; then $(SECHO) $(MSG_SIZE_BEFORE); $(SILENT) || $(HEXSIZE); \
377         2>/dev/null; $(SECHO); fi
378
379 sizeafter: $(BUILD_DIR)/$(TARGET).hex
380         @if test -f $(TARGET).hex; then $(SECHO); $(SECHO) $(MSG_SIZE_AFTER); $(SILENT) || $(HEXSIZE); \
381         2>/dev/null; $(SECHO); fi
382         # test file sizes eventually
383         # @if [[ $($(SIZE) --target=$(FORMAT) $(TARGET).hex | $(AWK) 'NR==2 {print "0x"$5}') -gt 0x200 ]]; then $(SECHO) "File is too big!"; fi
384
385 # Display compiler version information.
386 gccversion :
387         @$(SILENT) || $(CC) --version
388
389
390
391 # Program the device.  
392 program: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
393         $(PROGRAM_CMD)
394
395 teensy: $(BUILD_DIR)/$(TARGET).hex
396         $(TEENSY_LOADER_CLI) -mmcu=$(MCU) -w -v $(BUILD_DIR)/$(TARGET).hex
397
398 flip: $(BUILD_DIR)/$(TARGET).hex
399         batchisp -hardware usb -device $(MCU) -operation erase f
400         batchisp -hardware usb -device $(MCU) -operation loadbuffer $(BUILD_DIR)/$(TARGET).hex program
401         batchisp -hardware usb -device $(MCU) -operation start reset 0
402
403 dfu: $(BUILD_DIR)/$(TARGET).hex sizeafter
404 ifneq (, $(findstring 0.7, $(shell dfu-programmer --version 2>&1)))
405         dfu-programmer $(MCU) erase --force
406 else
407         dfu-programmer $(MCU) erase
408 endif
409         dfu-programmer $(MCU) erase
410         dfu-programmer $(MCU) flash $(BUILD_DIR)/$(TARGET).hex
411         dfu-programmer $(MCU) reset
412
413 dfu-no-build:
414 ifneq (, $(findstring 0.7, $(shell dfu-programmer --version 2>&1)))
415         dfu-programmer $(MCU) erase --force
416 else
417         dfu-programmer $(MCU) erase
418 endif
419         dfu-programmer $(MCU) erase
420         dfu-programmer $(MCU) flash $(KEYMAP_PATH)/compiled.hex
421         dfu-programmer $(MCU) reset
422         
423 dfu-start:
424         dfu-programmer $(MCU) reset
425         dfu-programmer $(MCU) start
426
427 flip-ee: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
428         $(COPY) $(BUILD_DIR)/$(TARGET).eep $(BUILD_DIR)/$(TARGET)eep.hex
429         batchisp -hardware usb -device $(MCU) -operation memory EEPROM erase
430         batchisp -hardware usb -device $(MCU) -operation memory EEPROM loadbuffer $(BUILD_DIR)/$(TARGET)eep.hex program
431         batchisp -hardware usb -device $(MCU) -operation start reset 0
432         $(REMOVE) $(BUILD_DIR)/$(TARGET)eep.hex
433
434 dfu-ee: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
435 ifneq (, $(findstring 0.7, $(shell dfu-programmer --version 2>&1)))
436         dfu-programmer $(MCU) flash --eeprom $(BUILD_DIR)/$(TARGET).eep
437 else
438         dfu-programmer $(MCU) flash-eeprom $(BUILD_DIR)/$(TARGET).eep
439 endif
440         dfu-programmer $(MCU) reset
441
442
443 # Generate avr-gdb config/init file which does the following:
444 #     define the reset signal, load the target file, connect to target, and set 
445 #     a breakpoint at main().
446 gdb-config: 
447         @$(REMOVE) $(GDBINIT_FILE)
448         @echo define reset >> $(GDBINIT_FILE)
449         @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
450         @echo end >> $(GDBINIT_FILE)
451         @echo file $(BUILD_DIR)/$(TARGET).elf >> $(GDBINIT_FILE)
452         @echo target remote $(DEBUG_HOST):$(DEBUG_PORT)  >> $(GDBINIT_FILE)
453 ifeq ($(DEBUG_BACKEND),simulavr)
454         @echo load  >> $(GDBINIT_FILE)
455 endif
456         @echo break main >> $(GDBINIT_FILE)
457
458 debug: gdb-config $(BUILD_DIR)/$(TARGET).elf
459 ifeq ($(DEBUG_BACKEND), avarice)
460         @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
461         @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
462         $(BUILD_DIR)/$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
463         @$(WINSHELL) /c pause
464
465 else
466         @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
467         $(DEBUG_MFREQ) --port $(DEBUG_PORT)
468 endif
469         @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
470
471
472
473
474 # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
475 COFFCONVERT = $(OBJCOPY) --debugging
476 COFFCONVERT += --change-section-address .data-0x800000
477 COFFCONVERT += --change-section-address .bss-0x800000
478 COFFCONVERT += --change-section-address .noinit-0x800000
479 COFFCONVERT += --change-section-address .eeprom-0x810000
480
481
482
483 coff: $(BUILD_DIR)/$(TARGET).elf
484         @$(SECHO) $(MSG_COFF) $(BUILD_DIR)/$(TARGET).cof
485         $(COFFCONVERT) -O coff-avr $< $(BUILD_DIR)/$(TARGET).cof
486
487
488 extcoff: $(BUILD_DIR)/$(TARGET).elf
489         @$(SECHO) $(MSG_EXTENDED_COFF) $(BUILD_DIR)/$(TARGET).cof
490         $(COFFCONVERT) -O coff-ext-avr $< $(BUILD_DIR)/$(TARGET).cof
491
492
493
494 # Create final output files (.hex, .eep) from ELF output file.
495 %.hex: %.elf
496         @$(SILENT) || printf "$(MSG_FLASH) $@" | $(AWK_CMD)
497         $(eval CMD=$(HEX) $< $@)
498         @$(BUILD_CMD)
499         @if $(AUTOGEN); then \
500                 $(SILENT) || printf "Copying $(TARGET).hex to keymaps/$(KEYMAP)/$(KEYBOARD)_$(KEYMAP).hex\n"; \
501                 $(COPY) $@ $(KEYMAP_PATH)/$(KEYBOARD)_$(KEYMAP).hex; \
502         else \
503                 $(COPY) $@ $(TARGET).hex; \
504         fi
505
506 %.eep: %.elf
507         @$(SILENT) || printf "$(MSG_EEPROM) $@" | $(AWK_CMD)
508         $(eval CMD=$(EEP) $< $@ || exit 0)
509         @$(BUILD_CMD)
510
511 # Create extended listing file from ELF output file.
512 %.lss: %.elf
513         @$(SILENT) || printf "$(MSG_EXTENDED_LISTING) $@" | $(AWK_CMD)
514         $(eval CMD=$(OBJDUMP) -h -S -z $< > $@)
515         @$(BUILD_CMD)
516
517 # Create a symbol table from ELF output file.
518 %.sym: %.elf
519         @$(SILENT) || printf "$(MSG_SYMBOL_TABLE) $@" | $(AWK_CMD)
520         $(eval CMD=$(NM) -n $< > $@ )
521         @$(BUILD_CMD)
522
523 %.bin: %.elf
524         @$(SILENT) || printf "$(MSG_BIN) $@" | $(AWK_CMD)
525         $(eval CMD=$(BIN) $< $@ || exit 0)
526         @$(BUILD_CMD)
527
528 # Create library from object files.
529 .SECONDARY : $(BUILD_DIR)/$(TARGET).a
530 .PRECIOUS : $(OBJ)
531 %.a: $(OBJ)
532         @$(SILENT) || printf "$(MSG_CREATING_LIBRARY) $@" | $(AWK_CMD)
533         $(eval CMD=$(AR) $@ $(OBJ) )
534         @$(BUILD_CMD)
535
536 BEGIN = gccversion check_submodule sizebefore
537
538 # Link: create ELF output file from object files.
539 .SECONDARY : $(BUILD_DIR)/$(TARGET).elf
540 .PRECIOUS : $(OBJ)
541 # Note the obj.txt depeendency is there to force linking if a source file is deleted
542 %.elf: $(OBJ) $(OBJDIR)/cflags.txt $(OBJDIR)/ldflags.txt $(OBJDIR)/obj.txt | $(BEGIN)
543         @$(SILENT) || printf "$(MSG_LINKING) $@" | $(AWK_CMD)
544         $(eval CMD=$(CC) $(ALL_CFLAGS) $(filter-out %.txt,$^) --output $@ $(LDFLAGS))
545         @$(BUILD_CMD)
546
547 define GEN_OBJRULE
548 # Compile: create object files from C source files.
549 $1/%.o : %.c $1/%.d $1/cflags.txt $1/compiler.txt | $(BEGIN)
550         @mkdir -p $$(@D)
551         @$$(SILENT) || printf "$$(MSG_COMPILING) $$<" | $$(AWK_CMD)
552         $$(eval CMD=$$(CC) -c $$(ALL_CFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
553         @$$(BUILD_CMD)
554
555 # Compile: create object files from C++ source files.
556 $1/%.o : %.cpp $1/%.d $1/cppflags.txt $1/compiler.txt | $(BEGIN)
557         @mkdir -p $$(@D)
558         @$$(SILENT) || printf "$$(MSG_COMPILING_CPP) $$<" | $$(AWK_CMD)
559         $$(eval CMD=$$(CC) -c $$(ALL_CPPFLAGS) $$(GENDEPFLAGS) $$< -o $$@ && $$(MOVE_DEP))
560         @$(BUILD_CMD)
561
562 # Assemble: create object files from assembler source files.
563 $1/%.o : %.S $1/asflags.txt $1/compiler.txt | $(BEGIN)
564         @mkdir -p $$(@D)
565         @$(SILENT) || printf "$$(MSG_ASSEMBLING) $$<" | $$(AWK_CMD)
566         $$(eval CMD=$$(CC) -c $$(ALL_ASFLAGS) $$< -o $$@)
567         @$$(BUILD_CMD)
568
569 $1/force:
570
571 $1/cflags.txt: $1/force
572         echo '$$(ALL_CFLAGS)' | cmp -s - $$@ || echo '$$(ALL_CFLAGS)' > $$@
573
574 $1/cppflags.txt: $1/force
575         echo '$$(ALL_CPPFLAGS)' | cmp -s - $$@ || echo '$$(ALL_CPPFLAGS)' > $$@
576
577 $1/asflags.txt: $1/force
578         echo '$$(ALL_ASFLAGS)' | cmp -s - $$@ || echo '$$(ALL_ASFLAGS)' > $$@
579
580 $1/ldflags.txt: $1/force
581         echo '$$(LDFLAGS)' | cmp -s - $$@ || echo '$$(LDFLAGS)' > $$@
582
583 $1/obj.txt: $1/force
584         echo '$$(OBJ)' | cmp -s - $$@ || echo '$$(OBJ)' > $$@
585
586 $1/compiler.txt: $1/force
587         $$(CC) --version | cmp -s - $$@ || $$(CC) --version > $$@
588 endef
589
590 # We have to use static rules for the .d files for some reason
591 DEPS = $(patsubst %.o,%.d,$(OBJ))
592 # Keep the .d files
593 .PRECIOUS: $(DEPS)
594 # Empty rule to force recompilation if the .d file is missing
595 $(DEPS):
596
597 # Since the object files could be in two different folders, generate
598 # separate rules for them, rather than having too generic rules
599 $(eval $(call GEN_OBJRULE,$(OBJDIR)))
600 $(eval $(call GEN_OBJRULE,$(KBOBJDIR)))
601
602 # Compile: create assembler files from C source files.
603 %.s : %.c | $(BEGIN)
604         @$(SILENT) || printf "$(MSG_ASSEMBLING) $<" | $(AWK_CMD)
605         $(eval CMD=$(CC) -S $(ALL_CFLAGS) $< -o $@)
606         @$(BUILD_CMD)
607
608 # Compile: create assembler files from C++ source files.
609 %.s : %.cpp | $(BEGIN)
610         @$(SILENT) || printf "$(MSG_ASSEMBLING) $<" | $(AWK_CMD)
611         $(eval CMD=$(CC) -S $(ALL_CPPFLAGS) $< -o $@)
612         @$(BUILD_CMD)
613
614 # Create preprocessed source for use in sending a bug report.
615 %.i : %.c | $(BEGIN)
616         $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
617
618 # Target: clean project.
619 clean:
620         $(REMOVE) -r $(OBJDIR) 2>/dev/null
621         $(REMOVE) -r $(KBOBJDIR) 2>/dev/null
622         $(REMOVE) $(BUILD_DIR)/$(TARGET).*
623
624 show_path:
625         @echo VPATH=$(VPATH)
626         @echo SRC=$(SRC)
627
628 SUBDIRS := $(filter-out %/util/ %/doc/ %/keymaps/ %/old_keymap_files/,$(dir $(wildcard $(TOP_DIR)/keyboards/**/*/Makefile)))
629 SUBDIRS := $(SUBDIRS) $(dir $(wildcard $(TOP_DIR)/keyboards/*/.))
630 SUBDIRS := $(sort $(SUBDIRS))
631 # $(error $(SUBDIRS))
632 all-keyboards-defaults-%:
633         @for x in $(SUBDIRS) ; do \
634                 printf "Compiling with default: $$x" | $(AWK_CMD); \
635                 LOG=$$($(MAKE) -C $$x $(subst all-keyboards-defaults-,,$@) VERBOSE=$(VERBOSE) COLOR=$(COLOR) SILENT=true 2>&1) ; if [ $$? -gt 0 ]; then $(PRINT_ERROR_PLAIN); elif [ "$$LOG" != "" ] ; then $(PRINT_WARNING_PLAIN); else $(PRINT_OK); fi; \
636         done
637
638 all-keyboards-defaults: all-keyboards-defaults-all
639
640 KEYBOARDS := $(SUBDIRS:$(TOP_DIR)/keyboards/%/=/keyboards/%)
641 all-keyboards-all: $(addsuffix -all,$(KEYBOARDS))
642 all-keyboards-clean: $(addsuffix -clean,$(KEYBOARDS))
643 all-keyboards: all-keyboards-all
644
645 define make_keyboard
646 $(eval KEYBOARD=$(patsubst /keyboards/%,%,$1))
647 $(eval SUBPROJECT=$(lastword $(subst /, ,$(KEYBOARD))))
648 $(eval KEYBOARD=$(firstword $(subst /, ,$(KEYBOARD))))
649 $(eval KEYMAPS=$(notdir $(patsubst %/.,%,$(wildcard $(TOP_DIR)/keyboards/$(KEYBOARD)/keymaps/*/.))))
650 $(eval KEYMAPS+=$(notdir $(patsubst %/.,%,$(wildcard $(TOP_DIR)/keyboards/$(KEYBOARD)/$(SUBPROJECT)/keymaps/*/.))))
651 @for x in $(KEYMAPS) ; do \
652         printf "Compiling $(BOLD)$(KEYBOARD)/$(SUBPROJECT)$(NO_COLOR) with $(BOLD)$$x$(NO_COLOR)" | $(AWK) '{ printf "%-118s", $$0; }'; \
653         LOG=$$($(MAKE) -C $(TOP_DIR)$1 $2 keymap=$$x VERBOSE=$(VERBOSE) COLOR=$(COLOR) SILENT=true 2>&1) ; if [ $$? -gt 0 ]; then $(PRINT_ERROR_PLAIN); elif [ "$$LOG" != "" ] ; then $(PRINT_WARNING_PLAIN); else $(PRINT_OK); fi; \
654 done
655 endef
656
657 define make_keyboard_helper
658 # Just remove the -all and so on from the first argument and pass it forward
659 $(call make_keyboard,$(subst -$2,,$1),$2)
660 endef
661
662 /keyboards/%-all:
663         $(call make_keyboard_helper,$@,all)
664 /keyboards/%-clean:
665         $(call make_keyboard_helper,$@,clean)
666 /keyboards/%:
667         $(call make_keyboard_helper,$@,all)
668
669 all-keymaps-%:
670         $(eval MAKECONFIG=$(call get_target,all-keymaps,$@))
671         $(eval KEYMAPS=$(notdir $(patsubst %/.,%,$(wildcard $(TOP_DIR)/keyboards/$(KEYBOARD)/keymaps/*/.))))
672         @for x in $(KEYMAPS) ; do \
673                 printf "Compiling $(BOLD)$(KEYBOARD)$(NO_COLOR) with $(BOLD)$$x$(NO_COLOR)" | $(AWK) '{ printf "%-118s", $$0; }'; \
674                 LOG=$$($(MAKE) $(subst all-keymaps-,,$@) keyboard=$(KEYBOARD) keymap=$$x VERBOSE=$(VERBOSE) COLOR=$(COLOR) SILENT=true 2>&1) ; if [ $$? -gt 0 ]; then $(PRINT_ERROR_PLAIN); elif [ "$$LOG" != "" ] ; then $(PRINT_WARNING_PLAIN); else $(PRINT_OK); fi; \
675         done
676
677 all-keymaps: all-keymaps-all
678
679 # Create build directory
680 $(shell mkdir $(BUILD_DIR) 2>/dev/null)
681
682 # Create object files directory
683 $(shell mkdir $(OBJDIR) 2>/dev/null)
684 $(shell mkdir $(KBOBJDIR) 2>/dev/null)
685
686 # Include the dependency files.
687 -include $(patsubst %.o,%.d,$(OBJ))
688
689
690 # Listing of phony targets.
691 .PHONY : all finish sizebefore sizeafter gccversion \
692 build elf hex eep lss sym coff extcoff check_submodule \
693 clean clean_list debug gdb-config show_path \
694 program teensy dfu flip dfu-ee flip-ee dfu-start \
695 all-keyboards-defaults all-keyboards all-keymaps \
696 all-keyboards-defaults-% all-keyboards-% all-keymaps-%