-
Notifications
You must be signed in to change notification settings - Fork 1
/
Core.mk
235 lines (186 loc) · 6.57 KB
/
Core.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
# ------------------------------------------------
# Core.mk
#
# @authors:
# -JIANG Yicheng (RM2024)
# -Zou Hetai (RM2024)
#
# RM2023 Generic Makefile (based on gcc)
# ------------------------------------------------
#######################################
# binaries
#######################################
PREFIX = arm-none-eabi-
# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
# either it can be added to the PATH environment variable.
ifdef GCC_PATH
CC = $(GCC_PATH)/$(PREFIX)gcc # c compiler
CPPC = $(GCC_PATH)/$(PREFIX)g++ # CPP compiler
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp # assembler
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
else
CC = $(PREFIX)gcc # c compiler
CPPC = $(PREFIX)g++ # CPP compiler
AS = $(PREFIX)gcc -x assembler-with-cpp # assembler
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
#######################################
# CFLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m3
# fpu
# NONE for Cortex-M0/M0+/M3
FPU =
FLOAT-ABI =
# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
# compile flags
COMPILERFLAGS = $(MCU) # MCU
COMPILERFLAGS += -ffreestanding # No standard library
COMPILERFLAGS += -fdata-sections # Place each data item into its own section
COMPILERFLAGS += -ffunction-sections # Place each function into its own section
COMPILERFLAGS += -fstack-usage # Generate stack usage information
COMPILERFLAGS += -specs=nano.specs # Use newlib-nano
COMPILERFLAGS += $(OPT) # Optimization
COMPILERFLAGS += -pipe # Use pipes rather than temporary files
COMPILERFLAGS += -Wfatal-errors # Stop after the first error
COMPILERFLAGS += -Wall # Enable all warnings
COMPILERFLAGS += -Wextra # Enable extra warnings
COMPILERFLAGS += -Wpedantic # Warn about anything that does not conform to the standard
COMPILERFLAGS += -Wshadow # Warn about shadowed variables
COMPILERFLAGS += -Wdouble-promotion # Warn about implicit conversion from float to double
COMPILERFLAGS += -Wlogical-op # Warn about suspicious uses of logical operators
COMPILERFLAGS += -Wpointer-arith # Warn about pointer arithmetic
COMPILERFLAGS += -Wmissing-field-initializers # Warn about missing field initializers
COMPILERFLAGS += -Wno-unused-parameter # Disable warning about unused parameters
COMPILERFLAGS += -Wno-unused-const-variable # Disable warning about unused const variables
COMPILERFLAGS += -fdiagnostics-color=auto # Enable colored diagnostics
# Debug
ifneq ($(DEBUG), -g0)
COMPILERFLAGS += $(DEBUG) -gdwarf-4
endif
# Generate dependency information
COMPILERFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@"
ASFLAGS = $(AS_DEFS) $(AS_INCLUDES) $(COMPILERFLAGS)
CFLAGS = $(C_DEFS) $(C_INCLUDES) $(CSTD) $(COMPILERFLAGS)
CPPFLAGS = $(C_DEFS) $(C_INCLUDES) $(CPPSTD) $(COMPILERFLAGS)
CPPFLAGS += -fno-exceptions # Disable exceptions
CPPFLAGS += -fno-rtti # Disable rtti (eg: typeid, dynamic_cast)
CPPFLAGS += -fno-threadsafe-statics # Disable thread safe statics
#######################################
# LDFLAGS
#######################################
# link script
LDSCRIPT = STM32F103C8Tx_FLASH.ld
# libraries
LIBS = -lc -lm -lnosys
LIBDIR =
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
# Hide details (silent mode)
ifeq ($(VERBOSE),1)
Q:=
else
Q:=@
endif
#######################################
# FreeRTOS
FreeRTOS_PATH = FreeRTOS
include $(FreeRTOS_PATH)/FreeRTOS.mk
# SystemView
SystemView_PATH = Diagnostic/SystemView
include $(SystemView_PATH)/SystemView.mk
# Core
CORE_DIR = RM2024-RDC-Core
include $(CORE_DIR)/$(CORE_DIR).mk
C_SOURCES += $(C_SRC) $(FreeRTOS_SRC) $(SystemView_SRC)
CPP_SOURCES += $(FreeRTOS_PATH)/sources/os.cpp
ASM_SOURCES += $(AS_SRC) $(SystemView_AS_SRC)
C_DEFS += -DAPP_NAME=$(TARGET)
C_INCLUDES += $(FreeRTOS_INC) $(SystemView_INC)
#######################################
BUILD_SUCCESS = @echo -e "\033[2K\033[92mBuild success!\033[m\n"
ifndef ECHO
HIT_TOTAL != ${MAKE} ${MAKECMDGOALS} --dry-run ECHO="HIT_MARK" | grep -c "HIT_MARK"
HIT_COUNT = $(eval HIT_N != expr ${HIT_N} + 1)${HIT_N}
ECHO = @echo -e "\033[2K\033[96;49;4m[`expr ${HIT_COUNT} '*' 100 / ${HIT_TOTAL}`%]\033[m\033[93m$(notdir $<)\033[m\r\c"
endif
# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
$(ECHO) $@
$(BUILD_SUCCESS)
$(SZ) $(BUILD_DIR)/$(TARGET).elf
#######################################
# build the application
#######################################
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(C_SOURCES:.c=.o))
vpath %.c $(sort $(dir $(C_SOURCES)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(CPP_SOURCES:.cpp=.o))
vpath %.cpp $(sort $(dir $(CPP_SOURCES)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(O_SRC)))
vpath %.o $(sort $(dir $(O_SRC)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(ASM_SOURCES:.s=.o))
vpath %.s $(sort $(dir $(ASM_SOURCES)))
# Print info
$(info Target: $(TARGET))
$(info Core: $(CORE_DIR))
$(info )
$(info C sources: $(C_SOURCES))
$(info CPP sources: $(CPP_SOURCES))
$(info ASM sources: $(ASM_SOURCES))
$(info O sources: $(O_SRC))
# Build .c files
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
$(ECHO) $@
@mkdir -p $(dir $@)
$(Q)$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(<:.c=.lst) $< -o $@
# Build .cpp files
$(BUILD_DIR)/%.o: %.cpp Makefile | $(BUILD_DIR)
$(ECHO) $@
@mkdir -p $(dir $@)
$(Q)$(CPPC) -c $(CPPFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(<:.cpp=.lst) $< -o $@
# Build .s files
$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
$(ECHO) $@
@mkdir -p $(dir $@)
$(Q)$(AS) -c $(ASFLAGS) $< -o $@
$(BUILD_DIR)/%.o: %.o Makefile | $(BUILD_DIR)
$(ECHO) $@
@mkdir -p $(dir $@)
@cp $< $@
# Build .elf file
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
$(ECHO) $@
$(Q)$(CC) $(OBJECTS) $(LDFLAGS) -o $@
# Build .hex file
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(ECHO) $@
$(Q)$(HEX) $< $@
# Build .bin file
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(ECHO) $@
$(Q)$(BIN) $< $@
# mkdir
$(BUILD_DIR):
$(Q)mkdir -p $@
# Clean up
clean:
@echo -e "\033[2K\033[96mClean up...\033[m"
@-rm -fR $(BUILD_DIR)
# Rebuild
rebuild: clean all
# Show size
size: $(BUILD_DIR)/$(TARGET).elf
$(Q)$(SZ) $<
# the following filenames are not allowed
.PHONY: all clean list size rebuild
#######################################
# dependencies
#######################################
-include $(OBJECTS:%.o=%.d)