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