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