-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommon.mk
313 lines (240 loc) · 10.8 KB
/
common.mk
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
#=============================================================================#
# common.mk - this file mostly based on the Chopin make. It is heavily
# modifed for use with fabooh and multiple chip targets
#
# author: Freddie Chopin, http://www.freddiechopin.info/
# last change: 2010-10-13
#
# this makefile is based strongly on many examples found in the network
#
# 04-17-2013 - rick@kimballsoftware use this as common.mk
# include in your own directory, see example/basic/blink
# for information how to do this
# 04-26-2013 - rick@kimballsoftware, modified for multi boards and
# different chip architectures
#=============================================================================#
#=============================================================================#
# project configuration
#=============================================================================#
# PROJECT = sample
# project name, we don't set it here, configure in Makefile of user code
# see examples for proper Makefile setup
#=============================================================================#
# fabooh TLD (Top Level Directory)
#=============================================================================#
FBD := $(dir $(lastword $(MAKEFILE_LIST)))
# which board will be used as default
include $(FBD)/common_board.mk
_3RD_PARTY_DIR = include/3rdparty
FIXMATH_FLAGS ?= -DFIXMATH_NO_CACHE -DFIXMATH_NO_64BIT -DFIXMATH_NO_ROUNDING
FABOOH_FLAGS = -DFABOOH
FABOOH_FLAGS += $(FIXMATH_FLAGS)
FABOOH_FLAGS += -fwrapv -fomit-frame-pointer
FABOOH_FLAGS += -ffunction-sections -fdata-sections
#=============================================================================#
# board specific options, cpu and toolchain selection
#=============================================================================#
include $(FBD)$(BOARDDIR)/board.mk
#=============================================================================#
# toolchain configuration
#=============================================================================#
CXX = $(TOOLCHAIN)g++
CC = $(TOOLCHAIN)gcc
AS = $(TOOLCHAIN)gcc -x assembler-with-cpp
OBJCOPY = $(TOOLCHAIN)objcopy
OBJDUMP = $(TOOLCHAIN)objdump
SIZE = $(TOOLCHAIN)size
# output folder (absolute or relative path, leave empty for in-tree compilation)
OUT_DIR = $(BOARD)_release
# include directories (absolute or relative paths to additional folders with
# headers, current folder is always included)
INC_DIRS = include/$(CORE)/core $(BOARDDIR) $(_3RD_PARTY_DIR)
# library directories (absolute or relative paths to additional folders with
# libraries)
LIB_DIRS = $(BOARDDIR)
# 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 = $(FBD)$(BOARDDIR)
# 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
# optimize for debug, "-Og" - optimize for debug)
OPTIMIZATION ?= -Os
# warning options
#CXX_WARNINGS = -Wall -Wextra
#C_WARNINGS = -Wall -Wstrict-prototypes -Wextra
CXX_WARNINGS = -Wall
C_WARNINGS = -Wall -Wstrict-prototypes
# C++ language standard ("c++98", "gnu++98" - default, "c++0x", "gnu++0x", "gnu++11", "gnu++14")
CXX_STD ?= gnu++98
# C language standard ("c89" / "iso9899:1990", "iso9899:199409",
# "c99" / "iso9899:1999", "gnu89", "gnu99", "c11")
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
#=============================================================================#
# listing flags
LISTING_FLAGS = -Wa,-ahlms=$(OUT_DIR_F)$(notdir $(<:.c=.lst))
# flags for C++ compiler
CXX_FLAGS = -std=$(CXX_STD) -ggdb -fno-rtti -fno-exceptions -fverbose-asm
# flags for C compiler
C_FLAGS = -std=$(C_STD) -g -fverbose-asm
# flags for assembler
AS_FLAGS = -x assembler-with-cpp
#=============================================================================#
# 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$(FBD) $(patsubst %, -I$(FBD)%, $(INC_DIRS))
LIB_DIRS_F = $(patsubst %, -L$(FBD)%, $(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
# format final flags for tools, request dependancies for C++, C and asm
CXX_FLAGS_F = $(CORE_FLAGS) $(OPTIMIZATION) $(OPTIMIZATION_FLAGS) $(CXX_WARNINGS) $(FABOOH_FLAGS) $(CXX_FLAGS) $(CXX_DEFS) -MD -MP -MF $(OUT_DIR_F)$(@F:.o=.d) $(INC_DIRS_F)
C_FLAGS_F = $(CORE_FLAGS) $(OPTIMIZATION) $(OPTIMIZATION_FLAGS) $(C_WARNINGS) $(FABOOH_FLAGS) $(C_FLAGS) $(C_DEFS) -MD -MP -MF $(OUT_DIR_F)$(@F:.o=.d) $(INC_DIRS_F)
AS_FLAGS_F = $(CORE_FLAGS) $(FABOOH_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)
#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
# make object files dependent on Makefile
$(OBJS) : Makefile
# make .elf file dependent on linker script
$(ELF) : $(LD_SCRIPT)
#-----------------------------------------------------------------------------#
# linking - objects -> elf
#-----------------------------------------------------------------------------#
$(ELF) : $(OBJS)
@echo 'Linking target: $(ELF)'
$(LD) $(LD_FLAGS_F) -Wl,--start-group $(OBJS) $(LIBS) -Wl,--end-group -o $@
@echo ' '
#-----------------------------------------------------------------------------#
# compiling - C++ source -> objects
#-----------------------------------------------------------------------------#
$(OUT_DIR_F)%.o : %.$(CXX_EXT)
@echo 'Compiling C++ file: $<'
$(CXX) -c $(CXX_FLAGS_F) $< -o $@
@echo ' '
#-----------------------------------------------------------------------------#
# compiling - C source -> objects
#-----------------------------------------------------------------------------#
$(OUT_DIR_F)%.o : %.$(C_EXT)
@echo 'Compiling C 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 ' '
#-----------------------------------------------------------------------------#
# 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) -CS $< > $@
@echo ' '
#-----------------------------------------------------------------------------#
# print the size of the objects
#-----------------------------------------------------------------------------#
print_size :
@echo 'Size of modules:'
$(SIZE) -B -t --common $(OBJS) $(USER_OBJS)
@echo ' '
$(SIZE) $(ELF)
@echo ' '
#-----------------------------------------------------------------------------#
# install
#-----------------------------------------------------------------------------#
install : all
$(BOOTLOADER) $(BL_ARGS)
#-----------------------------------------------------------------------------#
# create the desired output directory
#-----------------------------------------------------------------------------#
make_output_dir :
$(shell mkdir $(OUT_DIR_F) 2>/dev/null)
boards :
@echo 'valid BOARD targets are: $(BOARDS)'
@echo 'use:'
@echo '$$ make BOARD=xxxxxx clean all install'
#=============================================================================#
# 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
@$(RM) -r $(OUT_DIR_F)
#=============================================================================#
# global exports
#=============================================================================#
.PHONY: all clean dependents install boards
.SECONDARY:
# include dependancy files
-include $(DEPS)