-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
220 lines (180 loc) · 5.54 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
# http://stackoverflow.com/questions/7004702/how-can-i-create-a-makefile-for-c-projects-with-src-obj-and-bin-subdirectories
# output name
TARGET ?= speki
TOOLCHAIN_PATH ?= /gcc-arm-none-eabi/bin/
# type of build "debug", "release-debug" or "release"
BUILD_TYPE = release-debug
# toolchain
CC = $(TOOLCHAIN_PATH)arm-none-eabi-gcc
OC = $(TOOLCHAIN_PATH)arm-none-eabi-objcopy
OD = $(TOOLCHAIN_PATH)arm-none-eabi-objdump
SZ = $(TOOLCHAIN_PATH)arm-none-eabi-size
# static check tools
CPPCHECK = cppcheck
CPPCHECKFLAGS = -i./lib -i./src/system
CPPCHECKFLAGS += --enable=all --inconclusive --std=c11 --template=gcc
CPPCHECKFLAGS += --suppress=unusedFunction --suppress=missingInclude --suppress=unmatchedSuppression
DOXYGEN = doxygen
DOXYFILE = doc/Doxyfile
# output structure
OBJDIR ?= obj
DEPDIR ?= dep
BINDIR ?= bin
DOCDIR ?= doc/doxygen
# processor type
PTYPE = STM32F40_41xxx
# don't change anything under this line if you don't know what you're doing!
# ==========================================================================
# preparation for cross platform compatibility
RM := rm -rf
MKDIR := mkdir -p
# populate variables for includes
CFLAGS_INC :=
CFLAGS_DEF :=
CFLAGS_PROC :=
CFLAGS_BASE :=
LDFLAGS :=
SRCS :=
SRCS_ASM :=
# where are the libraries located?
LIBDIR := lib
SYSDIR ?= sys
BSPDIR ?= BSP
SGUIDIR ?= sGUI
PERIPHDIR ?= STM32F4xx_StdPeriph_Driver
CMSISDIR ?= CMSIS
# include libraries
include $(LIBDIR)/$(SYSDIR)/build.mk
include $(LIBDIR)/$(BSPDIR)/build.mk
include $(LIBDIR)/$(SGUIDIR)/build.mk
include $(LIBDIR)/$(PERIPHDIR)/build.mk
include $(LIBDIR)/$(CMSISDIR)/build.mk
# compiler flags
ifeq ($(BUILD_TYPE), debug)
CFLAGS_BASE += -ggdb -O0 -DUSE_FULL_ASSERT
else ifeq ($(BUILD_TYPE), release-debug)
CFLAGS_BASE += -O4 -g3
else ifeq ($(BUILD_TYPE), release)
CFLAGS_BASE += -O4
else
$(error BUILD_TYPE needs to be set to either debug, release-debug or release)
endif
CFLAGS_BASE += -Wall -std=gnu11 -fdata-sections -ffunction-sections
CFLAGS_BASE += -specs=nano.specs
CFLAGS_BASE += -Wno-format-truncation -Wdouble-promotion -Wstrict-prototypes
CFLAGS_PROC += -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16
CFLAGS_DEF += -D$(PTYPE) -DUSE_STDPERIPH_DRIVER
CFLAGS_DEF += -DHSE_VALUE=25000000 -DSYSCALL_USART=USART1
CFLAGS_DEF += -D_VOLATILE=volatile
CFLAGS_INC += -Iinc
CFLAGS += $(CFLAGS_BASE) $(CFLAGS_PROC) $(CFLAGS_DEF) $(CFLAGS_INC)
# linker flags
LDFLAGS += -Tstm32f4_flash.ld
LDFLAGS += -Wl,-Map,"$(BINDIR)/$(TARGET).map"
LDFLAGS += -Wl,--start-group -lc -lm -Wl,--end-group
LDFLAGS += --specs=nosys.specs
LDFLAGS += -Wl,-cref -Wl,-static
LDFLAGS += -Wl,--gc-sections -Wl,--defsym=malloc_getpagesize_P=0x80
SRCS += $(wildcard src/*.c)
SRCS_ASM += $(wildcard src/*.s)
SRCS_ASM_S += $(wildcard src/*.S)
OBJS := $(SRCS:%.c=$(OBJDIR)/%.o) $(SRCS_ASM:%.s=$(OBJDIR)/%.o) $(SRCS_ASM_S:%.S=$(OBJDIR)/%.o)
# list of target files
TARGETFILES := $(BINDIR)/$(TARGET).elf $(BINDIR)/$(TARGET).bin $(BINDIR)/$(TARGET).lst $(BINDIR)/compile_commands.json
# TARGETS
# =======
# these are not real targets
.PHONY: all clean dirs size test cppcheck doccheck doc
# default target
all: $(TARGETFILES) | dirs
# link and create elf
$(BINDIR)/$(TARGET).elf: $(OBJS) | dirs
@$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS)
@echo "[LD] $@"
%.hex: %.elf
@$(OC) -O ihex $< $@
@echo "[OC] $@"
%.bin: %.elf
@$(OC) -O binary $< $@
@echo "[OC] $@"
%.lst: %.elf
@$(OD) -x -S $< > $@
@echo "[OD] $@"
# Include auto generated dependency files
-include $(wildcard $(DEPDIR)/*.d)
# compile and generate dependency files
$(OBJDIR)/%.o: %.s | dirs
@$(CC) $(CFLAGS) -c -o $@ $< -MMD -MF $(DEPDIR)/$(*F).d
@echo "[AS] $<"
# compile and generate dependency files
$(OBJDIR)/%.o: %.S | dirs
@$(CC) $(CFLAGS) -c -o $@ $< -MMD -MF $(DEPDIR)/$(*F).d
@echo "[AS] $<"
# compile and generate dependency files
$(OBJDIR)/%.o: %.c | dirs
@$(CC) $(CFLAGS) -c -o $@ $< -MMD -MF $(DEPDIR)/$(*F).d
@echo "[CC] $<"
# output size information
size: $(BINDIR)/$(TARGET).elf
@$(SZ) $(BINDIR)/$(TARGET).elf
flash: $(BINDIR)/$(TARGET).elf
openocd -f openocd.cfg
# create directories
dirs:
@$(MKDIR) $(dir $(OBJS))
@$(MKDIR) $(DEPDIR)
@$(MKDIR) $(BINDIR)
# remove output files
clean:
@echo "[RM] $(OBJDIR)"
@$(RM) $(OBJDIR)
@echo "[RM] $(DEPDIR)"
@$(RM) $(DEPDIR)
@echo "[RM] $(BINDIR)"
@$(RM) $(BINDIR)
@echo "[RM] $(DOCDIR)"
@$(RM) $(DOCDIR)
# run all static checks
test: cppcheck -doccheck
# static check with cppcheck.
# in CI export errors in junit-xml format
cppcheck: $(BINDIR)/compile_commands.json
@$(CPPCHECK) $(CPPCHECKFLAGS) .
ifdef CI
@$(CPPCHECK) $(CPPCHECKFLAGS) . \
--xml-version=2 2> cppcheck_result.xml 1>/dev/null
@cppcheck_junit cppcheck_result.xml cppcheck_junit.xml
endif
# check for undocumented files
# in CI export errors in junit-xml format
doccheck: -doc
ifdef CI
@doxygen_junit --input doxy_result.log --output doxy_junit.xml
@test ! -s doxy_result.log
endif
# generate doxygen documentation
# in CI enable logging of errors
doc:
ifdef CI
@echo "WARN_LOGFILE = doxy_result.log" >> $(DOXYFILE)
endif
@$(DOXYGEN) $(DOXYFILE)
# update target but ignore errors
-%:
-@$(MAKE) --no-print-directory $*
# rules to generate a compile_commands.json
%.compdb_entry: %.c
@echo " {" > $@
@echo " \"command\": \"$(CC) $(CFLAGS) -c $<\"," >> $@
@echo " \"directory\": \"$(CURDIR)\"," >> $@
@echo " \"file\": \"$<\"" >> $@
@echo " }," >> $@
COMPDB_ENTRIES = $(addsuffix .compdb_entry, $(basename $(SRCS)))
$(BINDIR)/compile_commands.json: $(COMPDB_ENTRIES)
@echo "[" > [email protected]
@cat $^ >> [email protected]
@sed '$$d' < [email protected] > $@
@echo " }" >> $@
@echo "]" >> $@
@rm $^