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