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