forked from dongfang/BGC32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
362 lines (285 loc) · 12.1 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
#=============================================================================#
# ARM makefile
#
# author: Freddie Chopin, http://www.freddiechopin.info/
# last change: 2012-01-07
#
# this makefile is based strongly on many examples found in the network
#=============================================================================#
#=============================================================================#
# toolchain configuration
#=============================================================================#
TOOLCHAIN = arm-none-eabi-
CXX = $(TOOLCHAIN)g++
CC = $(TOOLCHAIN)gcc
AS = $(TOOLCHAIN)gcc -x assembler-with-cpp
OBJCOPY = $(TOOLCHAIN)objcopy
OBJDUMP = $(TOOLCHAIN)objdump
SIZE = $(TOOLCHAIN)size
RM = rm -f
#=============================================================================#
# project configuration
#=============================================================================#
# project name
PROJECT = BGC32Gimbal
PROJECT_USB = $(PROJECT).USB
# core type
CORE = cortex-m3
# linker script
LD_SCRIPT = stm32_flash.ld
LD_USB_SCRIPT = stm32_flash_usb.ld
# output folder (absolute or relative path, leave empty for in-tree compilation)
OUT_DIR = out
# C++ definitions (e.g. "-Dsymbol_with_value=0xDEAD -Dsymbol_without_value")
CXX_DEFS = -DSTM32F10X_HD
# C definitions
C_DEFS = -DSTM32F10X_HD -DUSE_STDPERIPH_DRIVER -DARM_MATH_CM3 -D_DTIMING
#C_DEFS += -DDISABLE_PA10
#C_DEFS += -DUSB_DISC_DEV=GPIOD -DUSB_DISC_PIN=GPIO_Pin_11 -DUSB_DISC_RCC=RCC_APB2Periph_GPIOA
# ASM definitions
AS_DEFS =
# include directories (absolute or relative paths to additional folders with
# headers, current folder is always included)
INC_DIRS = src Libraries \
Libraries/CMSIS/Include \
Libraries/CMSIS/Device/ST/STM32F10x/Include \
Libraries/STM32F10x_StdPeriph_Driver/inc \
Libraries/STM32_USB-FS-Device_Driver/inc \
src/vcp \
src/calibration \
src/drv \
src/cli \
src/sensors
# library directories (absolute or relative paths to additional folders with
# libraries)
LIB_DIRS = Libraries/CMSIS/Lib/GCC
# libraries (additional libraries for linking, e.g. "-lm -lsome_name" to link
# math library libm.a and libsome_name.a)
LIBS = -lm -larm_cortexM3l_math
# additional directories with source files (absolute or relative paths to
# folders with source files, current folder is always included)
SRCS_DIRS = Libraries/STM32F10x_StdPeriph_Driver/src src src/startup src/sys \
Libraries/STM32_USB-FS-Device_Driver/src \
src/vcp \
src/calibration \
src/drv \
src/cli \
src/sensors
# extension of C++ files
CXX_EXT = cpp
# wildcard for C++ source files (all files with CXX_EXT extension found in
# current folder and SRCS_DIRS folders will be compiled and linked)
CXX_SRCS = $(wildcard $(patsubst %, %/*.$(CXX_EXT), . $(SRCS_DIRS)))
# 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
# set to 1 to compile and link additional code required for C++
USES_CXX = 0
# define warning options here
CXX_WARNINGS = -Wall -Wextra
C_WARNINGS = -Wall -Wstrict-prototypes -Wextra
# C++ language standard ("c++98", "gnu++98" - default, "c++0x", "gnu++0x")
CXX_STD = gnu++98
# C language standard ("c89" / "iso9899:1990", "iso9899:199409",
# "c99" / "iso9899:1999", "gnu89" - default, "gnu99")
C_STD = gnu99
#=============================================================================#
# set the VPATH according to SRCS_DIRS
#=============================================================================#
VPATH = $(SRCS_DIRS)
#=============================================================================#
# 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
#=============================================================================#
# various compilation flags
#=============================================================================#
# core flags
CORE_FLAGS = -mcpu=$(CORE) -mthumb
# flags for C++ compiler
#CXX_FLAGS = -std=$(CXX_STD) -g -ggdb3 -fno-rtti -fno-exceptions -fverbose-asm -Wa,-ahlms=$(OUT_DIR_F)$(notdir $(<:.$(CXX_EXT)=.lst))
CXX_FLAGS = -std=$(CXX_STD) -fno-rtti -fno-exceptions -fverbose-asm -Wa,-ahlms=$(OUT_DIR_F)$(notdir $(<:.$(CXX_EXT)=.lst))
# flags for C compiler
#C_FLAGS = -std=$(C_STD) -g -ggdb3 -fverbose-asm -Wa,-ahlms=$(OUT_DIR_F)$(notdir $(<:.$(C_EXT)=.lst))
C_FLAGS = -std=$(C_STD) -fverbose-asm -Wa,-ahlms=$(OUT_DIR_F)$(notdir $(<:.$(C_EXT)=.lst))
C_FLAGS += -fsingle-precision-constant
# flags for assembler
#AS_FLAGS = -g -ggdb3 -Wa,-amhls=$(OUT_DIR_F)$(notdir $(<:.$(AS_EXT)=.lst))
AS_FLAGS = -Wa,-amhls=$(OUT_DIR_F)$(notdir $(<:.$(AS_EXT)=.lst))
# flags for linker
LD_FLAGS = -T$(LD_SCRIPT) -Wl,-Map=$(OUT_DIR_F)$(PROJECT).map,--cref,--no-warn-mismatch
LD_USB_FLAGS = -T$(LD_USB_SCRIPT) -Wl,-Map=$(OUT_DIR_F)$(PROJECT_USB).map,--cref,--no-warn-mismatch
# process option for removing unused code
ifeq ($(REMOVE_UNUSED), 1)
LD_FLAGS += -Xlinker --gc-sections
LD_USB_FLAGS += -Xlinker --gc-sections
OPTIMIZATION += -ffunction-sections -fdata-sections
endif
# if __USES_CXX is defined for ASM then code for global/static constructors /
# destructors is compiled; if -nostartfiles option for linker is added then C++
# initialization / finalization code is not linked
ifeq ($(USES_CXX), 1)
AS_DEFS += -D__USES_CXX
else
LD_FLAGS += -nostartfiles
LD_USB_FLAGS += -nostartfiles
endif
#=============================================================================#
# do some formatting
#=============================================================================#
CXX_OBJS = $(addprefix $(OUT_DIR_F), $(notdir $(CXX_SRCS:.$(CXX_EXT)=.o)))
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) $(CXX_OBJS) $(USER_OBJS)
DEPS = $(OBJS:.o=.d)
INC_DIRS_F = -I. $(patsubst %, -I%, $(INC_DIRS))
LIB_DIRS_F = $(patsubst %, -L%, $(LIB_DIRS))
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
USBELF = $(OUT_DIR_F)$(PROJECT_USB).elf
USBHEX = $(OUT_DIR_F)$(PROJECT_USB).hex
USBBIN = $(OUT_DIR_F)$(PROJECT_USB).bin
USBLSS = $(OUT_DIR_F)$(PROJECT_USB).lss
USBDMP = $(OUT_DIR_F)$(PROJECT_USB).dmp
# format final flags for tools, request dependancies for C++, C and asm
CXX_FLAGS_F = $(CORE_FLAGS) $(OPTIMIZATION) $(CXX_WARNINGS) $(CXX_FLAGS) $(CXX_DEFS) -MD -MP -MF $(OUT_DIR_F)$(@F:.o=.d) $(INC_DIRS_F)
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)
LD_USB_FLAGS_F = $(CORE_FLAGS) $(LD_USB_FLAGS) $(LIB_DIRS_F)
#contents of output directory
GENERATED = $(wildcard $(patsubst %, $(OUT_DIR_F)*.%, bin d dmp elf hex lss lst map o))
#=============================================================================#
# make all
#=============================================================================#
#all : make_output_dir $(ELF) $(LSS) $(DMP) $(HEX) $(BIN) print_size
all: make_output_dir $(ELF) $(LSS) $(DMP) $(HEX) $(BIN) $(USBELF) $(USBLSS) $(USBBIN) print_size \
# $(USBELF) $(USBLSS) $(USBBIN
# make object files dependent on Makefile
#$(OBJS) : Makefile
# make .elf file dependent on linker script
$(ELF) : $(LD_SCRIPT)
$(USBELF) : $(LD_USB_SCRIPT)
#-----------------------------------------------------------------------------#
# linking - objects -> elf
#-----------------------------------------------------------------------------#
$(ELF) : $(OBJS) $(LD_SCRIPT)
@echo 'Linking target: $(ELF)'
$(CXX) $(LD_FLAGS_F) $(OBJS) $(LIBS) -o $@
@echo ' '
#-----------------------------------------------------------------------------#
# linking - objects -> elf, USB version
#-----------------------------------------------------------------------------#
$(USBELF) : $(ELF) $(OBJS) $(LD_USB_SCRIPT)
@echo 'Linking target: $(USBELF)'
$(CXX) $(LD_USB_FLAGS_F) $(OBJS) $(LIBS) -o $@
@echo ' '
#-----------------------------------------------------------------------------#
# compiling - C++ source -> objects
#-----------------------------------------------------------------------------#
$(OUT_DIR_F)%.o : %.$(CXX_EXT)
@echo 'Compiling file: $<'
$(CXX) -c $(CXX_FLAGS_F) $< -o $@
@echo ' '
#-----------------------------------------------------------------------------#
# compiling - C source -> objects
#-----------------------------------------------------------------------------#
$(OUT_DIR_F)%.o : %.$(C_EXT)
@echo 'Compiling file: $<'
$(CC) -c $(C_FLAGS_F) $< -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 ' '
$(USBBIN) : $(USBELF)
@echo 'Creating binary image: $(USBBIN)'
$(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 ' '
$(USBLSS) : $(USBELF)
@echo 'Creating extended listing: $(USBLSS)'
$(OBJDUMP) -S $< > $@
@echo ' '
#-----------------------------------------------------------------------------#
# print the size of the objects and the .elf file
#-----------------------------------------------------------------------------#
print_size: $(OBJS) $(ELF)
-#@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 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)