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