-
Notifications
You must be signed in to change notification settings - Fork 7
/
Makefile
261 lines (216 loc) · 9.11 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# MCU name
MCU = atmega162
# Output format. (can be srec, ihex, binary)
FORMAT = ihex
# Target file name (without extension).
TARGET = bootloader
# List C source files here. (C dependencies are automatically generated.)
SRC = bootloader.c
# Create object files and object files directories
OBJECTDIRDEBUG = build/debug
OBJECTDIRRELEASE = build/release
OBJRELEASE = $(OBJECTDIRRELEASE)/bootloader.o
OBJDEBUG = $(OBJECTDIRDEBUG)/bootloader.o
# Optimization level, can be [0, 1, 2, 3, s].
OPT_DEBUG = 1
OPT_RELEASE = s
# Compiler flag to set the C Standard level.
CSTANDARD = -std=gnu99
CDEFS_DEBUG = -DDEBUG
CDEFS_RELEASE = -DNDEBUG
CFLAGS_DEBUG = -g2 $(CDEFS_DEBUG) -O$(OPT_DEBUG)
CFLAGS_RELEASE = $(CDEFS_RELEASE) -O$(OPT_RELEASE)
#---------------- Compiler Options C ----------------
CFLAGS = -funsigned-char
CFLAGS += -funsigned-bitfields
CFLAGS += -ffunction-sections
CFLAGS += -fdata-sections
CFLAGS += -fpack-struct
CFLAGS += -fshort-enums
CFLAGS += -Wall
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
CFLAGS += $(CSTANDARD)
# Math library
MATH_LIB = -lm
#---------------- Linker Options ----------------
# -Wl,...: tell GCC to pass this to linker.
# -Map: create map file
# --cref: add cross reference to map file
LDFLAGSDEBUG = -Wl,-Map="$(OBJECTDIRDEBUG)/$(TARGET).map" -Wl,--start-group -Wl,-lm -Wl,--end-group -Wl,--gc-sections -Wl,-section-start=.text=0x3800 -mmcu=atmega162 -B.
LDFLAGSRELEASE = -Wl,-Map="$(OBJECTDIRRELEASE)/$(TARGET).map" -Wl,--start-group -Wl,-lm -Wl,--end-group -Wl,--gc-sections -Wl,-section-start=.text=0x3800 -mmcu=atmega162 -B.
#####################################################################################################################
# Define programs and commands.
SHELL = sh
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
AR = avr-ar rcs
NM = avr-nm
REMOVE = rm -f
REMOVEDIR = rm -rf
COPY = cp
WINSHELL = cmd
# Define Messages
# English
MSG_ERRORS_NONE = Errors: none
MSG_BEGIN = -------- begin --------
MSG_END = -------- end --------
MSG_SIZE_BEFORE = Size before:
MSG_SIZE_AFTER = Size after:
MSG_FLASH = Creating load file for Flash:
MSG_EEPROM = Creating load file for EEPROM:
MSG_EXTENDED_LISTING = Creating Extended Listing:
MSG_LINKING = Linking:
MSG_COMPILING = Compiling C:
MSG_CLEANING = Cleaning project:
MSG_BINARY_FILE = Creating binary output file:
# Compiler flags to generate dependency files.
GENDEPFLAGSDEBUG = -MD -MP -MF $(OBJECTDIRDEBUG)/$(@F).d -MT.$(OBJECTDIRDEBUG)/$(@F).d -MT$(OBJECTDIRDEBUG)/$(@F)
GENDEPFLAGSRELEASE = -MD -MP -MF $(OBJECTDIRRELEASE)/$(@F).d -MT.$(OBJECTDIRRELEASE)/$(@F).d -MT$(OBJECTDIRRELEASE)/$(@F)
# Combine all necessary flags and optional flags.
# Add target processor to flags.
ALL_CFLAGS_DEBUG = -mmcu=$(MCU) -I. $(CFLAGS_DEBUG) $(CFLAGS) $(GENDEPFLAGSDEBUG)
ALL_CFLAGS_RELEASE = -mmcu=$(MCU) -I. $(CFLAGS_RELEASE) $(CFLAGS) $(GENDEPFLAGSRELEASE)
#####################################################################################################################
# Default target.
all: debug release
debug: begin gccversion sizebefore_debug build_debug sizeafter_debug end
release: begin gccversion sizebefore_release build_release sizeafter_release end
# Change the build target to build a HEX file or a library.
build_debug: elf_debug hex_debug eep_debug lss_debug sym_debug bin_debug
build_release: elf_release hex_release eep_release lss_release sym_release bin_release
elf_debug: $(OBJECTDIRDEBUG)/$(TARGET).elf
hex_debug: $(OBJECTDIRDEBUG)/$(TARGET).hex
eep_debug: $(OBJECTDIRDEBUG)/$(TARGET).eep
lss_debug: $(OBJECTDIRDEBUG)/$(TARGET).lss
sym_debug: $(OBJECTDIRDEBUG)/$(TARGET).sym
bin_debug: $(OBJECTDIRDEBUG)/$(TARGET).bin
elf_release: $(OBJECTDIRRELEASE)/$(TARGET).elf
hex_release: $(OBJECTDIRRELEASE)/$(TARGET).hex
eep_release: $(OBJECTDIRRELEASE)/$(TARGET).eep
lss_release: $(OBJECTDIRRELEASE)/$(TARGET).lss
sym_release: $(OBJECTDIRRELEASE)/$(TARGET).sym
bin_release: $(OBJECTDIRRELEASE)/$(TARGET).bin
begin:
@echo
@echo $(MSG_BEGIN)
end:
@echo $(MSG_END)
@echo
# Display size of file.
ELFSIZE_DEBUG = $(SIZE) --mcu=$(MCU) --format=avr $(OBJECTDIRDEBUG)/$(TARGET).elf
ELFSIZE_RELEASE = $(SIZE) --mcu=$(MCU) --format=avr $(OBJECTDIRRELEASE)/$(TARGET).elf
sizebefore_debug:
@if test -f $(OBJECTDIRDEBUG)/$(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE_DEBUG); \
2>/dev/null; echo; fi
sizeafter_debug:
@if test -f $(OBJECTDIRDEBUG)/$(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE_DEBUG); \
2>/dev/null; echo; fi
sizebefore_release:
@if test -f $(OBJECTDIRRELEASE)/$(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE_RELEASE); \
2>/dev/null; echo; fi
sizeafter_release:
@if test -f $(OBJECTDIRRELEASE)/$(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE_RELEASE); \
2>/dev/null; echo; fi
# Display compiler version information.
gccversion :
@$(CC) --version
flashdebug:
avrdude -c usbasp -p m162 -P usb -v -u -D -U efuse:w:0xfb:m -U hfuse:w:0xd8:m -U lfuse:w:0xfd:m -u -U flash:w:$(OBJECTDIRDEBUG)/$(TARGET).hex
flashrelease:
avrdude -c usbasp -p m162 -P usb -v -u -D -U efuse:w:0xfb:m -U hfuse:w:0xd8:m -U lfuse:w:0xfd:m -u -U flash:w:$(OBJECTDIRRELEASE)/$(TARGET).hex
erase:
avrdude -c usbasp -p m162 -P usb -v -u -e
# Create final output files (.hex, .eep) from ELF output file.
$(OBJECTDIRDEBUG)/%.hex: $(OBJECTDIRDEBUG)/%.elf
@echo
@echo $(MSG_FLASH) $@
$(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures "$(OBJECTDIRDEBUG)/$(TARGET).elf" "$(OBJECTDIRDEBUG)/$(TARGET).hex"
$(OBJECTDIRDEBUG)/%.eep: $(OBJECTDIRDEBUG)/%.elf
@echo
@echo $(MSG_EEPROM) $@
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0 --no-change-warnings -O ihex "$(OBJECTDIRDEBUG)/$(TARGET).elf" "$(OBJECTDIRDEBUG)/$(TARGET).eep" || exit 0
#-j .eeprom --set-section-flags=.eeprom="alloc,load" \
#--change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
# Create extended listing file from ELF output file.
$(OBJECTDIRDEBUG)/%.lss: $(OBJECTDIRDEBUG)/%.elf
@echo
@echo $(MSG_EXTENDED_LISTING) $@
$(OBJDUMP) -h -S -z $< > $@
# Create a symbol table from ELF output file.
$(OBJECTDIRDEBUG)/%.sym: $(OBJECTDIRDEBUG)/%.elf
@echo
@echo $(MSG_SYMBOL_TABLE) $@
$(NM) -n $< > $@
# Create a binary output file from ELF output file
$(OBJECTDIRDEBUG)/%.bin: $(OBJECTDIRDEBUG)/%.elf
@echo
@echo $(MSG_BINARY_FILE) $@
$(OBJCOPY) -O binary $< $@
# Link: create ELF output file from object files.
.SECONDARY : $(OBJECTDIRDEBUG)/$(TARGET).elf
.PRECIOUS : $(OBJDEBUG)
$(OBJECTDIRDEBUG)/%.elf: $(OBJDEBUG)
@echo
@echo $(MSG_LINKING) $<
$(CC) -o $(OBJECTDIRDEBUG)/$(TARGET).elf $(OBJDEBUG) $(LDFLAGSDEBUG)
# Compile: create object files from C source files.
$(OBJECTDIRDEBUG)/%.o : %.c
$(shell mkdir build 2>/dev/null)
$(shell mkdir $(OBJECTDIRDEBUG) 2>/dev/null)
@echo
@echo $(MSG_COMPILING) $<
$(CC) -c $(ALL_CFLAGS_DEBUG) $< -o $@
#####################################################################################################################
# Create final output files (.hex, .eep) from ELF output file :Release version.
$(OBJECTDIRRELEASE)/%.hex: $(OBJECTDIRRELEASE)/%.elf
@echo
@echo $(MSG_FLASH) $@
$(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures "$(OBJECTDIRRELEASE)/$(TARGET).elf" "$(OBJECTDIRRELEASE)/$(TARGET).hex"
$(OBJECTDIRRELEASE)/%.eep: $(OBJECTDIRRELEASE)/%.elf
@echo
@echo $(MSG_EEPROM) $@
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0 --no-change-warnings -O ihex "$(OBJECTDIRRELEASE)/$(TARGET).elf" "$(OBJECTDIRRELEASE)/$(TARGET).eep" || exit 0
#-j .eeprom --set-section-flags=.eeprom="alloc,load" \
#--change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
# Create extended listing file from ELF output file.
$(OBJECTDIRRELEASE)/%.lss: $(OBJECTDIRRELEASE)/%.elf
@echo
@echo $(MSG_EXTENDED_LISTING) $@
$(OBJDUMP) -h -S -z $< > $@
# Create a symbol table from ELF output file.
$(OBJECTDIRRELEASE)/%.sym: $(OBJECTDIRRELEASE)/%.elf
@echo
@echo $(MSG_SYMBOL_TABLE) $@
$(NM) -n $< > $@
# Create a binary output file from ELF output file
$(OBJECTDIRRELEASE)/%.bin: $(OBJECTDIRRELEASE)/%.elf
@echo
@echo $(MSG_BINARY_FILE) $@
$(OBJCOPY) -O binary $< $@
# Link: create ELF output file from object files.
.SECONDARY : $(OBJECTDIRRELEASE)/$(TARGET).elf
.PRECIOUS : $(OBJRELEASE)
$(OBJECTDIRRELEASE)/%.elf: $(OBJRELEASE)
@echo
@echo $(MSG_LINKING) $<
$(CC) -o $(OBJECTDIRRELEASE)/$(TARGET).elf $(OBJRELEASE) $(LDFLAGSRELEASE)
# Compile: create object files from C source files.
$(OBJECTDIRRELEASE)/%.o : %.c
$(shell mkdir build 2>/dev/null)
$(shell mkdir $(OBJECTDIRRELEASE) 2>/dev/null)
@echo
@echo $(MSG_COMPILING) $<
$(CC) -c $(ALL_CFLAGS_RELEASE) $< -o $@
#####################################################################################################################
# Target: clean project.
clean: begin clean_list end
clean_list :
@echo
@echo $(MSG_CLEANING)
$(REMOVEDIR) build
# Create object files directory
# Listing of phony targets.
.PHONY : all begin finish end sizebefore sizeafter gccversion debug release\
build elf hex bin eep lss clean clean_list flashdebug flashrelease erase