-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase.mak
329 lines (267 loc) · 9.65 KB
/
base.mak
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
# Optimization level, can be [0, 1, 2, 3, s].
# 0 = turn off optimization. s = optimize for size.
#
OPT ?= -O1
# Target file name (without extension)
TARGET = build/fw
FRAMEWORK_DIR = ../../framework
INCDIRS += inc/
# SOURCES += $(wildcard src/*.c)
INCDIRS += build/gen/inc/
DEFINES += -D$(CPUDEF)
DEFINES += -DHSE_VALUE=$(XTAL_FREQ)
CPPFLAGS += $(DEFINES)
#============================================================================
LIBOBJECTS = $(subst ../../framework/libs/,,$(addprefix build/libs/,$(addsuffix .o,$(basename $(LIBSOURCES)))))
GENOBJECTS = $(addsuffix .o,$(basename $(GENSOURCES)))
OBJECTS += $(addprefix build/,$(addsuffix .o,$(basename $(notdir $(SOURCES)))))
OBJECTS += $(LIBOBJECTS) $(GENOBJECTS)
CPPFLAGS += $(addprefix -I,$(INCDIRS))
#---------------- Preprocessor Options ----------------
# -fsingle... make better use of the single-precision FPU
# -g generate debugging information
# -save-temps preserve .s and .i-files
#
CPPFLAGS += -fsingle-precision-constant
# CPPFLAGS += -save-temps=obj
#---------------- C/C++ Compiler Options ----------------
# Use a friendly C dialect
CPPFLAGS += -fno-strict-aliasing
CPPFLAGS += -fwrapv
CPPFLAGS += -ffunction-sections
CPPFLAGS += -fdata-sections
CPPFLAGS += -Wall
CPPFLAGS += -Wmaybe-uninitialized
CPPFLAGS += -Wuninitialized
CPPFLAGS += -Wdouble-promotion
CPPFLAGS += -Wundef
CPPFLAGS += -nodefaultlibs
CPPFLAGS += -nostdlib
CPPFLAGS += -nolibc
CPPFLAGS += -fno-common
CPPFLAGS += -fno-builtin
CPPFLAGS += -nostartfiles
CPPFLAGS += -fstack-usage
CPPFLAGS += -Wpadded
CPPFLAGS += -Wunreachable-code
CPPFLAGS += -Wextra
CPPFLAGS += -Wno-unused-parameter
CPPFLAGS += -Wconversion
#---------------- C Compiler Options ----------------
#
CFLAGS += $(OPT)
CFLAGS += -std=gnu11
#CFLAGS += -Wstrict-prototypes
#CFLAGS += -Wpointer-arith
#CFLAGS += -Winline
#---------------- C++ Compiler Options ----------------
#
CXXFLAGS += $(OPT)
CXXFLAGS += -nostdinc++
CXXFLAGS += -std=c++20
CXXFLAGS += -fno-rtti
CXXFLAGS += -fno-exceptions
CXXFLAGS += -Wno-volatile # allow volatile reg |= bits;
#CXXFLAGS += -lgcc
#---------------- Assembler Options ----------------
# -Wa,... tell GCC to pass this to the assembler
#
#---------------- Linker Options ----------------
# -Wl,... tell GCC to pass this to linker
# -Map create map file
# --cref add cross reference to map file
#
ifndef LDSCRIPT
LDSCRIPT = cpu.ld
endif
LDFLAGS += $(OPT)
LDFLAGS += -lm
LDFLAGS += -Wl,-Map=build/fw.map,--cref
LDFLAGS += -Wl,--gc-sections
LDFLAGS += -specs=nano.specs -specs=nosys.specs
FINAL_LDFLAGS += $(LDFLAGS)
FINAL_LDFLAGS += -Wl,--print-memory-usage
# LDFLAGS += -specs=nano.specs -u _printf_float -u _scanf_float
# LDFLAGS += -lc -specs=nosys.specs
LDFLAGS += -T$(LDSCRIPT)
#============================================================================
POSTLD = $(PYTHON) $(FRAMEWORK_DIR)/tools/add_version_info.py # -q
# Compiler flags to generate dependency files
#
GENDEPFLAGS = -MMD -MP
# Combine all necessary flags and optional flags
# Add target processor to flags.
#
ifndef CPUFLAGS
CPUFLAGS = -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16
endif
CFLAGS += $(CPUFLAGS)
CXXFLAGS += $(CPUFLAGS)
ASFLAGS += $(CPUFLAGS)
LDFLAGS += $(CPUFLAGS)
# Default target
#
all: generate build showsize
build: elf hex bin lss sym
elf: build/fw.elf
hex: build/fw.hex
bin: generate build/fw.bin
lss: build/fw.lss
sym: build/fw.sym
include ../../framework/libs/libs.mak
generate: $(GENINCS) $(GENSOURCES)
# Target: clean project
#
clean:
@echo Cleaning project:
rm -rf build
# Include the base rules
#
# Be silent per default, but 'make V=1' will show all compiler calls.
ifneq ($(V),1)
Q := @
NULL := 2>/dev/null
endif
GIT_HASH := $(shell git describe --always --dirty)
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
USR := $(shell whoami)
HOST := $(shell hostname)
CRC = 0#$(shell crc32 $(basename $@).bin)
BIN_SIZE = $(strip $(shell wc -c < $(basename $@).bin))
DEFINES += -DGIT_HASH=$(GIT_HASH)
DEFINES += -DGIT_BRANCH=$(GIT_BRANCH)
DEFINES += -DUSR=$(USR)
DEFINES += -DHOST=$(HOST)
DEFINES += -DBUILD_CC_VERSION=$(shell $(CC) -dumpversion)
DEFINES += -DBUILD_CC=$(CC)
ifdef XTAL_FREQ
DEFINES += -DHSE_VALUE=$(XTAL_FREQ)
endif
# Link: create ELF output file from object files
#
build/fw.tmp_pre2: build/fw.tmp_pre
@echo Post-processing: $@
$(Q)$(OBJCOPY) -O binary --gap-fill 0xFF $< $(basename $@).bin
build/fw.elf: build/fw.tmp_pre2
$(Q)$(CC) $(OBJECTS) $(LDFLAGS) -Xlinker --defsym=BIN_CRC=0 -Xlinker --defsym=BIN_SIZE=$(BIN_SIZE) --output $(basename $@).tmp
$(Q)$(CC) $(OBJECTS) $(LDFLAGS) -Xlinker --defsym=BIN_CRC=0x$(CRC) -Xlinker --defsym=BIN_SIZE=$(BIN_SIZE) --output $(basename $@).elf
build/fw.tmp_pre: $(OBJECTS) $(LDSCRIPT)
@echo Linking: $@
@$(MKDIR) -p $(dir $@)
$(Q)$(CC) $(OBJECTS) $(FINAL_LDFLAGS) -Xlinker --defsym=BIN_CRC=0 -Xlinker --defsym=BIN_SIZE=0 --output $(basename $@).tmp_pre
# Create extended listing file from ELF output file
#
build/fw.lss: build/fw.elf
@echo Creating Extended Listing: $@
@$(MKDIR) -p $(dir $@)
$(Q)$(OBJDUMP) -h -S -z $< > $@
# Create a symbol table from ELF output file
#
build/fw.sym: build/fw.elf
@echo Creating Symbol Table: $@
@$(MKDIR) -p $(dir $@)
$(Q)$(NM) -n $< > $@
# Create final output files from ELF output file.
#
build/fw.hex: build/fw.elf
@echo Creating hex file: $@
@$(MKDIR) -p $(dir $@)
$(Q)$(OBJCOPY) -O ihex $< $@
# Create binary output file from ELF output file.
#
build/fw.bin: build/fw.elf
@echo Creating bin file: $@
@$(MKDIR) -p $(dir $@)
$(Q)$(OBJCOPY) -O binary --gap-fill 0xFF $< $@
# Compile: create object files from C source files
build/gen/%.o : build/gen/src/%.c $(GENINCS) $(GENSOURCES)
@echo Compiling gen C: $<
@$(MKDIR) -p $(dir $@)
$(Q)$(CC) -c $(CPPFLAGS) $(CFLAGS) $(GENDEPFLAGS) $< -o $@
build/libs/%.o : ../../framework/libs/%.c $(GENINCS) $(GENSOURCES)
@echo Compiling lib C: $<
@$(MKDIR) -p $(dir $@)
$(Q)$(CC) -c $(CPPFLAGS) $(CFLAGS) $(GENDEPFLAGS) $< -o $@
build/%.o : src/%.c $(GENINCS) $(GENSOURCES)
@echo Compiling C: $<
@$(MKDIR) -p $(dir $@)
$(Q)$(CC) -c $(CPPFLAGS) $(CFLAGS) $(GENDEPFLAGS) $< -o $@
# Compile: create object files from C++ source files
build/gen/%.o : build/gen/src/%.cpp $(GENINCS) $(GENSOURCES)
@echo Compiling gen C++: $<
@$(MKDIR) -p $(dir $@)
$(Q)$(Cxx) -c $(CPPFLAGS) $(CXXFLAGS) $(GENDEPFLAGS) $< -o $@
build/libs/%.o : ../../framework/libs/%.cpp $(GENINCS) $(GENSOURCES)
@echo Compiling lib C++: $<
@$(MKDIR) -p $(dir $@)
$(Q)$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(GENDEPFLAGS) $< -o $@
build/%.o : src/%.cpp $(GENINCS) $(GENSOURCES)
@echo Compiling C++: $<
@$(MKDIR) -p $(dir $@)
$(Q)$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(GENDEPFLAGS) $< -o $@
# build/%.o : %.cpp
# @echo Compiling C++: $<
# @$(MKDIR) -p $(dir $@)
# $(Q)$(CC) -c $(CPPFLAGS) $(CXXFLAGS) $(GENDEPFLAGS) $< -o $@
# Assemble: create object files from assembler source files
#
build/%.o : %.s $(GENINCS) $(GENSOURCES)
@echo Assembling: $<
@$(MKDIR) -p $(dir $@)
$(Q)$(CC) -c $(CPPFLAGS) $(ASFLAGS) $(GENDEPFLAGS) $< -o $@
build/libs/%.o : ../../framework/libs/%.s $(GENINCS) $(GENSOURCES)
@echo Assembling: $<
@$(MKDIR) -p $(dir $@)
$(Q)$(CC) -c $(CPPFLAGS) $(ASFLAGS) $(GENDEPFLAGS) $< -o $@
%.o: @.d
$(info no match $@)
# Display compiler version information
#
gccversion:
@$(CC) --version
# Show the final program size
#
showsize: build
@echo
@$(SIZE) build/fw.elf 2>/dev/null
# Flash the device
#
btburn: $(FRAMEWORK_DIR)/tools/bootloader.py build showsize build/fw.bin
@$(PYTHON) $(FRAMEWORK_DIR)/tools/bootloader.py
@sleep 1
@$(DFU-UTIL) -d 0483:df11 -a 0 -s $(ADDRESS):leave -D build/fw.bin
flash: build/fw.bin
@$(ST-FLASH) --reset write build/fw.bin $(ADDRESS)
# Create a DFU file from bin file
%.dfu: fw.bin
@cp $< $@
@$(DFU-SUFFIX) -v 0483 -p df11 -a $@
# TODO: consolidate these two rules into a wildcard version?
build/fw.dfu: $(FRAMEWORK_DIR)/tools/dfu-convert.py build/fw.bin
$(PYTHON) $(FRAMEWORK_DIR)/tools/dfu-convert.py -b $(ADDRESS):build/fw.bin build/fw.dfu
format: $(SOURCES) $(SRC_COMPS)
@echo formating code $?
@clang-format --style=file -i $?
tidy: generate
@echo tidy checks
@$(Q)$(TIDY) $(GENSOURCES) $(SOURCES) -extra-arg=-ferror-limit=0 -extra-arg=-std=c++20 -checks=*,-readability-identifier-length,-llvmlibc-implementation-in-namespace,-llvmlibc-callee-namespace,-google-readability-casting,-google-readability-todo,-llvm-include-order,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers,-altera-unroll-loops,-performance-no-int-to-ptr,-cppcoreguidelines-pro-type-cstyle-cast,-cppcoreguidelines-avoid-c-arrays,-hicpp-avoid-c-arrays,-modernize-avoid-c-arrays,-altera-id-dependent-backward-branch,-modernize-use-trailing-return-type,-hicpp-signed-bitwise,-bugprone-reserved-identifier,-cert-dcl37-c,-cert-dcl51-cpp,-bugprone-easily-swappable-parameters,-misc-unused-parameters,-cppcoreguidelines-avoid-non-const-global-variables,-fuchsia-statically-constructed-objects,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-llvmlibc-restrict-system-libc-headers -- $(addprefix -I,$(INCDIRS)) $(DEFINES)
#format:
# find $(SOURCES) $(INC) -iname '*.h' -o -iname '*.c' | xargs clang-format -i
include ../../framework/toolchain.mak
-include ../../framework/toolchain-user.mak
# Include the dependency files
#
-include $(OBJECTS:.o=.d)
test: $(TESTSOURCES)
@$(MKDIR) -p build/
@$(HOSTCXX) -I$(FRAMEWORK_DIR)/libs/catch2/ $(addprefix -I,$(INCDIRS)) -std=c++20 -pedantic -Wall -Wfatal-errors $(TESTSOURCES) $(FRAMEWORK_DIR)/libs/catch2/main.cpp -o build/test
build/test
force_look:
@true
# Listing of phony targets
#
.PHONY: gccversion showsize all build clean \
elf hex bin lss sym generate \
format \
force_look \
test