-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
executable file
·397 lines (300 loc) · 13.8 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#=============================================================================#
# ARM makefile
#
# author: Freddie Chopin, http://www.freddiechopin.info/
# last change: 2012-01-08
#
# this makefile is based strongly on many examples found in the network
#=============================================================================#
#=============================================================================#
# toolchain configuration
#=============================================================================#
TOOLCHAIN = arm-none-eabi-
CC = $(TOOLCHAIN)gcc
AS = $(TOOLCHAIN)gcc -x assembler-with-cpp
OBJCOPY = $(TOOLCHAIN)objcopy
OBJDUMP = $(TOOLCHAIN)objdump
SIZE = $(TOOLCHAIN)size
RM = rm -f
#=============================================================================#
# test configuration
#=============================================================================#
UNITY_BASE=../Unity
CC_TEST = gcc
AS_TEST = gcc -x assembler-with-cpp
SIZE_TEST = size
LINT = oclint
#=============================================================================#
# project configuration
#=============================================================================#
# project name
PROJECT = rear-can-node
# core type
CORE = cortex-m0
# linker script
LD_SCRIPT = gcc.ld
# output folder (absolute or relative path, leave empty for in-tree compilation)
OUT_DIR = bin
# C definitions
C_DEFS = -DCORE_M0 -DDEBUG_ENABLE -DCAN_ARCHITECTURE_ARM
# ASM definitions
AS_DEFS = -D__STARTUP_CLEAR_BSS -D__START=main
# include directories (absolute or relative paths to additional folders with
# headers, current folder is always included)
INC_DIRS_CROSS = inc/ ../lpc11cx4-library/lpc_chip_11cxx_lib/inc ../lpc11cx4-library/evt_lib/inc/ ../MY17/lib/MY17_Can_Library
# library directories (absolute or relative paths to additional folders with
# libraries)
LIB_DIRS =
# libraries (additional libraries for linking, e.g. "-lm -lsome_name" to link
# math library libm.a and libsome_name.a)
LIBS =
# additional directories with source files (absolute or relative paths to
# folders with source files, current folder is always included)
SRCS_DIRS = ../lpc11cx4-library/lpc_chip_11cxx_lib/src ../lpc11cx4-library/evt_lib/src/ src/ ../MY17/lib/MY17_Can_Library
# extension of C files
C_EXT = c
# wildcard for C source files (all files with C_EXT extension found in current
# folder and SRCS_DIRS folders will be compiled and linked)
C_SRCS = $(wildcard $(patsubst %, %/*.$(C_EXT), . $(SRCS_DIRS)))
# extension of ASM files
AS_EXT = S
# wildcard for ASM source files (all files with AS_EXT extension found in
# current folder and SRCS_DIRS folders will be compiled and linked)
AS_SRCS = $(wildcard $(patsubst %, %/*.$(AS_EXT), . $(SRCS_DIRS)))
# optimization flags ("-O0" - no optimization, "-O1" - optimize, "-O2" -
# optimize even more, "-Os" - optimize for size or "-O3" - optimize yet more)
OPTIMIZATION = -O2
# set to 1 to optimize size by removing unused code and data during link phase
REMOVE_UNUSED = 1
# define warning options here
C_WARNINGS = -Wall -Wstrict-prototypes -Wextra
# C language standard ("c89" / "iso9899:1990", "iso9899:199409",
# "c99" / "iso9899:1999", "gnu89" - default, "gnu99")
C_STD = gnu89
#=============================================================================#
# Unit Testing Configuration
#=============================================================================#
# test out folder
OUT_DIR_TEST = testbin
# include directories for test
INC_DIRS_TEST = $(INC_DIRS_CROSS) $(SRCS_DIRS) test $(UNITY_BASE)/src $(UNITY_BASE)/extras/fixture/src
# directories for testing sources
TEST_SRCS_DIRS = test $(UNITY_BASE)/src $(UNITY_BASE)/extras/fixture/src
# c files for testing
C_SRCS_TEST = $(wildcard $(patsubst %, %/*.$(C_EXT), . $(TEST_SRCS_DIRS))) src/state.c src/transfer_functions.c
#=============================================================================#
# Write Configuration
#=============================================================================#
COMPORT = $(word 1, $(wildcard /dev/tty.usbserial-*) $(wildcard /dev/ttyUSB*))
BAUDRATE = 115200
CLOCK_OSC = 0
#=============================================================================#
# Lint Configuration
#=============================================================================#
MAX_LINE_SIZE = 140
#=============================================================================#
# set the VPATH according to SRCS_DIRS
#=============================================================================#
VPATH = $(SRCS_DIRS) test $(UNITY_BASE)/extras/fixture/src $(UNITY_BASE)/src devices
#=============================================================================#
# when using output folder, append trailing slash to its name
#=============================================================================#
ifeq ($(strip $(OUT_DIR)), )
OUT_DIR_F =
else
OUT_DIR_F = $(strip $(OUT_DIR))/
endif
#=============================================================================#
# when using output folder, append trailing slash to its name
#=============================================================================#
ifeq ($(strip $(OUT_DIR_TEST)), )
OUT_DIR_TEST_F =
else
OUT_DIR_TEST_F = $(strip $(OUT_DIR_TEST))/
endif
#=============================================================================#
# various compilation flags
#=============================================================================#
# core flags
CORE_FLAGS = -mcpu=$(CORE) -mthumb
# flags for C compiler
C_FLAGS = -fdiagnostics-color=always -std=$(C_STD) -g -ggdb3 -fverbose-asm -Wa,-ahlms=$(OUT_DIR_F)$(notdir $(<:.$(C_EXT)=.lst)) -DUART_BAUD=$(BAUDRATE)
# add diagnostic colors c standard debug(?) extra comments
# flags for assembler
AS_FLAGS = -g -ggdb3 -Wa,-amhls=$(OUT_DIR_F)$(notdir $(<:.$(AS_EXT)=.lst))
# flags for linker
LD_FLAGS = -T$(LD_SCRIPT) -g -nostartfiles -Wl,-Map=$(OUT_DIR_F)$(PROJECT).map,--cref
# flags for lint
LINT_FLAGS = -rc LONG_LINE=$(MAX_LINE_SIZE)
# process option for removing unused code
ifeq ($(REMOVE_UNUSED), 1)
# enable garbage collection of unused sections
LD_FLAGS += -Wl,--gc-sections
# put functions and data into their own sections
OPTIMIZATION += -ffunction-sections -fdata-sections
endif
#=============================================================================#
# do some formatting
#=============================================================================#
C_OBJS_TEST = $(addprefix $(OUT_DIR_TEST_F), $(notdir $(C_SRCS_TEST:.$(C_EXT)=.o)))
AS_OBJS_TEST = $(addprefix $(OUT_DIR_TEST_F), $(notdir $(AS_SRCS_TEST:.$(AS_EXT)=.o)))
TEST_OBJS = $(AS_OBJS_TEST) $(C_OBJS_TEST)
C_OBJS = $(addprefix $(OUT_DIR_F), $(notdir $(C_SRCS:.$(C_EXT)=.o)))
AS_OBJS = $(addprefix $(OUT_DIR_F), $(notdir $(AS_SRCS:.$(AS_EXT)=.o)))
OBJS = $(AS_OBJS) $(C_OBJS) $(USER_OBJS)
DEPS = $(OBJS:.o=.d)
INC_DIRS_F = -I. $(patsubst %, -I%, $(INC_DIRS_CROSS))
LIB_DIRS_F = $(patsubst %, -L%, $(LIB_DIRS))
INC_DIRS_F_TEST = -I. $(patsubst %, -I%, $(INC_DIRS_TEST))
ELF = $(OUT_DIR_F)$(PROJECT).elf
HEX = $(OUT_DIR_F)$(PROJECT).hex
BIN = $(OUT_DIR_F)$(PROJECT).bin
LSS = $(OUT_DIR_F)$(PROJECT).lss
DMP = $(OUT_DIR_F)$(PROJECT).dmp
TEST_TARGET = $(OUT_DIR_TEST_F)$(PROJECT)
# format final flags for tools, request dependancies for C and asm
C_FLAGS_F_CROSS = $(CORE_FLAGS) $(OPTIMIZATION) $(C_WARNINGS) $(C_FLAGS) $(C_DEFS) -MD -MP -MF $(OUT_DIR_F)$(@F:.o=.d) $(INC_DIRS_F)
AS_FLAGS_F_CROSS = $(CORE_FLAGS) $(AS_FLAGS) $(AS_DEFS) -MD -MP -MF $(OUT_DIR_F)$(@F:.o=.d) $(INC_DIRS_F)
LD_FLAGS_F_CROSS = $(CORE_FLAGS) $(LD_FLAGS) $(LIB_DIRS_F_CROSS)
# format final flags for tools, request dependancies for C and asm
C_FLAGS_F = $(CORE_FLAGS) $(OPTIMIZATION) $(C_WARNINGS) $(C_FLAGS) $(C_DEFS) -MD -MP -MF $(OUT_DIR_F)$(@F:.o=.d) $(INC_DIRS_F)
AS_FLAGS_F = $(CORE_FLAGS) $(AS_FLAGS) $(AS_DEFS) -MD -MP -MF $(OUT_DIR_F)$(@F:.o=.d) $(INC_DIRS_F)
LD_FLAGS_F = $(CORE_FLAGS) $(LD_FLAGS) $(LIB_DIRS_F)
C_FLAGS_F_TEST = $(OPTIMIZATION) $(C_WARNINGS) $(C_DEFS) -MD -MP -MF $(OUT_DIR_F)$(@F:.o=.d) $(INC_DIRS_F_TEST) -DTEST_HARDWARE
AS_FLAGS_F_TEST = $(AS_FLAGS) $(AS_DEFS) -MD -MP -MF $(OUT_DIR_F)$(@F:.o=.d) $(INC_DIRS_F_TEST)
# LD_FLAGS_F_TEST = $(LIB_DIRS_F_TEST)
#contents of output directory
GENERATED = $(wildcard $(patsubst %, $(OUT_DIR_F)*.%, bin d dmp elf hex lss lst map o)) $(wildcard $(OUT_DIR_TEST_F)*)
#=============================================================================#
# make all
#=============================================================================#
all : make_output_dir $(ELF) $(LSS) $(DMP) $(HEX) $(BIN) print_size
test : CC = $(CC_TEST)
test : AS = $(AS_TEST)
test : OBJCOPY = $(OBJCOPY_TEST)
test : OBJDUMP = $(OBJDUMP_TEST)
test : SIZE = $(SIZE_TEST)
test : C_FLAGS_F = $(C_FLAGS_F_TEST)
test : AS_FLAGS_F = $(AS_FLAGS_F_TEST)
test : LD_FLAGS_F = $(LD_FLAGS_F_TEST)
.PHONY: test
test : make_test_output_dir $(TEST_TARGET)
./$(TEST_TARGET)
test_writeflash: AS_DEFS = -D__STARTUP_CLEAR_BSS -D__START=hardware_test
test_writeflash: writeflash
# make object files dependent on Makefile
$(OBJS) : Makefile
$(TEST_OBJS) : Makefile
# make .elf file dependent on linker script
$(ELF) : $(LD_SCRIPT)
#-----------------------------------------------------------------------------#
# test_linking - objects -> elf
#-----------------------------------------------------------------------------#
$(TEST_TARGET) : $(TEST_OBJS)
@$(CC) $(TEST_OBJS) $(LIBS) -o $@
@echo ' '
#-----------------------------------------------------------------------------#
# linking - objects -> elf
#-----------------------------------------------------------------------------#
$(ELF) : $(OBJS)
@echo 'Linking target: $(ELF)'
$(CC) $(LD_FLAGS_F) $(OBJS) $(LIBS) -o $@
@echo ' '
#-----------------------------------------------------------------------------#
# compiling - C source -> objects
#-----------------------------------------------------------------------------#
$(OUT_DIR_F)%.o : %.$(C_EXT)
@echo 'Compiling file: $<'
$(CC) -c $(C_FLAGS_F) $< -o $@
@echo ' '
$(OUT_DIR_TEST_F)%.o : %.$(C_EXT)
@echo 'Compiling file: $<'
$(CC) -c $(C_FLAGS_F_TEST) $< -o $@
@echo ' '
#-----------------------------------------------------------------------------#
# assembling - ASM source -> objects
#-----------------------------------------------------------------------------#
$(OUT_DIR_F)%.o : %.$(AS_EXT)
@echo 'Assembling file: $<'
$(AS) -c $(AS_FLAGS_F) $< -o $@
@echo ' '
#-----------------------------------------------------------------------------#
# memory images - elf -> hex, elf -> bin
#-----------------------------------------------------------------------------#
$(HEX) : $(ELF)
@echo 'Creating IHEX image: $(HEX)'
$(OBJCOPY) -O ihex $< $@
@echo ' '
$(BIN) : $(ELF)
@echo 'Creating binary image: $(BIN)'
$(OBJCOPY) -O binary $< $@
@echo ' '
#-----------------------------------------------------------------------------#
# memory dump - elf -> dmp
#-----------------------------------------------------------------------------#
$(DMP) : $(ELF)
@echo 'Creating memory dump: $(DMP)'
$(OBJDUMP) -x --syms $< > $@
@echo ' '
#-----------------------------------------------------------------------------#
# extended listing - elf -> lss
#-----------------------------------------------------------------------------#
$(LSS) : $(ELF)
@echo 'Creating extended listing: $(LSS)'
$(OBJDUMP) -S $< > $@
@echo ' '
#-----------------------------------------------------------------------------#
# print the size of the objects and the .elf file
#-----------------------------------------------------------------------------#
print_size :
@echo 'Size of modules:'
$(SIZE) -B -t --common $(OBJS) $(USER_OBJS)
@echo ' '
@echo 'Size of target .elf file:'
$(SIZE) -B $(ELF)
@echo ' '
#-----------------------------------------------------------------------------#
# create the desired output directory
#-----------------------------------------------------------------------------#
make_output_dir :
$(shell mkdir $(OUT_DIR_F) 2>/dev/null)
make_test_output_dir :
$(shell mkdir $(OUT_DIR_TEST_F) 2>/dev/null)
#-----------------------------------------------------------------------------#
# Perform static analysis with lint
#-----------------------------------------------------------------------------#
lint: $(C_SRCS)
oclint $^ $(LINT_FLAGS) -- $(C_FLAGS_F_CROSS) -I/usr/local/Cellar/gcc-arm-none-eabi/20140805/arm-none-eabi/include/
#-----------------------------------------------------------------------------#
# Write to flash of chip
#-----------------------------------------------------------------------------#
writeflash: all
@echo "Writing to" $(COMPORT)
lpc21isp -NXPARM -control $(HEX) $(COMPORT) $(BAUDRATE) $(CLOCK_OSC)
#-----------------------------------------------------------------------------#
# Open up in picocom
#-----------------------------------------------------------------------------#
com:
@echo "Opening" $(COMPORT)
lpc21isp -NXPARM -control -termonly $(HEX) $(COMPORT) $(BAUDRATE) $(CLOCK_OSC)
#=============================================================================#
# make clean
#=============================================================================#
clean:
ifeq ($(strip $(OUT_DIR_F)), )
@echo 'Removing all generated output files'
else
@echo 'Removing all generated output files from output directory: $(OUT_DIR_F)'
endif
ifneq ($(strip $(GENERATED)), )
$(RM) $(GENERATED)
else
@echo 'Nothing to remove...'
endif
#=============================================================================#
# global exports
#=============================================================================#
.PHONY: all clean dependents
.SECONDARY:
# include dependancy files
-include $(DEPS)