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