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