-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
79 lines (67 loc) · 2.03 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
# 编译器设置
PREFIX = arm-none-eabi-
CC = $(PREFIX)gcc
OC = $(PREFIX)objcopy
CFLAGS = -mcpu=cortex-m3 -mthumb -std=c99 -Wall -Werror -fdata-sections -ffunction-sections $(addprefix -I, $(INC))
LDFLAGS = -Wl,--gc-sections -Tstm32_flash.ld -nostdlib -s
TARGET = main # 输出文件名
# 生成依赖信息
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
CXXFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
ifeq ($(DEBUG), 1)
CFLAGS += -O0 -g -gdwarf-2 -fstack-protector-all -fno-omit-frame-pointer
CXXFLAGS += -O0 -g -gdwarf-2 -fstack-protector-all -fno-omit-frame-pointer
else
CFLAGS += -Os
CXXFLAGS += -Os
LDFLAGS += -s
endif
# 包含目录
INC = sys/include sys/include/std user/include
SYS_SRCS = $(wildcard sys/src/*.c) $(wildcard sys/src/std/*.c) # 系统源文件
STARTUP_ASMS = sys/startup/startup_stm32f10x_md.s # 启动汇编
USER_SRCS = $(wildcard user/src/*.c) # 用户源文件
# 目标文件
OBJS = $(SYS_SRCS:.c=.o) $(STARTUP_ASMS:.s=.o) $(USER_SRCS:.c=.o)
DEPENDS = $(OBJS:.o=.d)
# 生成目标文件
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
%.o: %.s
$(CC) $(CFLAGS) -c $< -o $@
# 主目标
BINS += $(addsuffix .elf,$(TARGET))
BINS += $(addsuffix .bin,$(TARGET))
BINS += $(addsuffix .hex,$(TARGET))
all: $(BINS)
# 生成 ELF 文件
%.elf: $(OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) -o $@
%.hex: %.elf
$(OC) -O ihex $< $@
%.bin: %.elf
$(OC) -O binary $< $@
# 清理生成的文件
clean:
rm -f $(OBJS) $(BINS)
c: clean
# 烧录
write:
st-flash --reset write $(addsuffix .bin,$(TARGET)) 0x8000000
w: write
.SILENT: sizes
# 目标文件大小
.ONESHELL:
sizes:
arr=(`$(PREFIX)size $(addsuffix .elf,$(TARGET)) | sed -n '2p'`)
let flash=($${arr[0]}+$${arr[1]})
let mem=($${arr[1]}+$${arr[2]})
let flash_size=128*1024
let mem_size=20*1024
flash_usage=`echo "scale=2; a=$$flash*100/$$flash_size;print a "|bc`
mem_usage=`echo "scale=2; a=$$mem*100/$$mem_size;print a "|bc`
echo ""
echo "Flash: $$flash / $$flash_size bytes, $$flash_usage% Full (.text + .data)"
echo "SRAM: $$mem / $$mem_size bytes, $$mem_usage% Full (.data + .bss)"
s: sizes
-include $(DEPENDS)