-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
147 lines (119 loc) · 2.62 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
include ./common.mk
K=kernel
U=user
# 汇编文件
SRCS_ASM = \
$K/entry.S \
$K/trampoline.S \
$K/kernelvec.S \
$K/swtch.S
# C 语言文件
SRCS_C = \
$K/main.c \
$K/start.c \
$K/main.c \
$K/proc.c \
$K/console.c \
$K/spinlock.c \
$K/uart.c \
$K/file.c \
$K/printf.c \
$K/kalloc.c \
$K/string.c \
$K/vm.c \
$K/trap.c \
$K/plic.c \
$K/bio.c \
$K/sleeplock.c \
$K/fs.c \
$K/virtio_disk.c \
$K/log.c \
$K/syscall.c \
$K/sysproc.c \
$K/exec.c \
$K/sysfile.c \
$K/pipe.c
# 用户态APP
SRC = \
$U/init.c \
$U/sh.c \
$U/helloworld.c \
$U/ls.c \
$U/cat.c \
$U/echo.c \
$U/forktest.c \
$U/grep.c \
$U/kill.c \
$U/ln.c \
$U/mkdir.c \
$U/rm.c \
$U/wc.c \
$U/zombie.c
# 建立目标文件
OBJS = ${SRCS_ASM:.S=.o}
OBJS += ${SRCS_C:.c=.o}
USER_APPS = ${SRC:.c=.paddle}
USER_OBJS = ${SRC:.c=.o}
# 默认启动命令
.DEFAULT_GOAL := all
# 生成目标文件
all: kernel user disk.img
# 伪目标文件
kernel: kernel.elf
# 生成用户态APP
user: ${USER_APPS}
# 生成磁盘镜像
disk.img: mkfs/mkfs README ${USER_APPS}
mkfs/mkfs disk.img README ${USER_APPS}
# 生成内核
kernel.elf: ${OBJS}
${LD} ${LDFLAGS} -T $K/kernel.ld -o kernel.elf $^
# 启动虚拟机
run: all
${QEMU} ${QFLAGS} -kernel kernel.elf
# 生成格式化程序
mkfs/mkfs: mkfs/mkfs.c
gcc -Werror -I. -Wall -o mkfs/mkfs mkfs/mkfs.c
# 生成汇编
code : ${OBJS}
${OBJDUMP} -S ${OBJS} | less
# 调试
debug: all
${QEMU} ${QFLAGS} -kernel kernel.elf -s -S &
${GDB} kernel.elf -q -x ./gdbinit
# 供给vscode用
qemu-debug: all
@echo "start qemu debug"
${QEMU} ${QFLAGS} -kernel kernel.elf -s -S
# 清理文件
clean:
rm -f ${OBJS} kernel.elf disk.img mkfs/mkfs user/*.o user/usys.S \
${USER_APPS} $U/initcode $U/initcode.out
# 内核C到目标文件
${K}/%.o : ${K}/%.c
${CC} ${CFLAGS} -c -o $@ $<
# 内核汇编到目标文件
${K}/%.o : ${K}/%.S
${CC} ${CFLAGS} -I. -c -o $@ $<
# 用户态C到目标文件
${U}/%.o : ${U}/%.c
${CC} ${CFLAGS} -c -o $@ $<
# 用户态汇编到目标文件
${U}/%.o : ${U}/%.S
${CC} ${CFLAGS} -I. -c -o $@ $<
# initcode.S
$U/initcode: $U/initcode.S
$(CC) $(CFLAGS) -march=rv64g -nostdinc -I. -Ikernel -c $U/initcode.S -o $U/initcode.o
$(LD) ${LDFLAGS} -N -e start -Ttext 0 -o $U/initcode.out $U/initcode.o
$(OBJCOPY) -S -O binary $U/initcode.out $U/initcode
@od -t xC ./user/initcode
# 用户态的静态链接库
ULIB = $U/usys.o $U/ulib.o $U/printf.o $U/umalloc.o
# 生成系统调用相关文件
$U/usys.S: $U/usys.py
$(PYTHON) $U/usys.py > $U/usys.S
# 从.o 链接静态链接库 生成app文件
%.paddle: %.o ${ULIB}
${LD} ${LDFLAGS} -T $U/user.ld -o $@ $^
.PRECIOUS: user/%.o
.PRECIOUS: kernel/%.o