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