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