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