-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
143 lines (110 loc) · 4.25 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
# GNU Makefile
# This Makefile is only for competition on os.educg.net
# What a pity that they don't support cmake
# This Makefile is not meant to be maintance
OS_PLATFORM:=k210
# OS_PLATFORM:=qemu
FLASH_USB_PORT?=/dev/ttyUSB0
FLASH_BAUDRATE:=3000000
ifeq ($(OS_PLATFORM), qemu)
BIN_OFFSET:=2097152
else
BIN_OFFSET:=131072
endif
TOOLCHAIN_PREFIX?=riscv64-unknown-elf-
PROJ_ROOT:=$(shell pwd)
BUILD_DIR:=${PROJ_ROOT}/build
CC:=${TOOLCHAIN_PREFIX}gcc
LD:=${TOOLCHAIN_PREFIX}ld
AR:=${TOOLCHAIN_PREFIX}ar
OBJCOPY:=${TOOLCHAIN_PREFIX}objcopy
OBJDUMP:=${TOOLCHAIN_PREFIX}objdump
KERNEL_HEADER:=${PROJ_ROOT}/kernel/header/
USER_HEADER:=${PROJ_ROOT}/userlib/header/
COMMON_HEADER:=${PROJ_ROOT}/header/
CC_FLAGS_GENERIC:= -fno-pic -nostdinc -static -fno-builtin -fno-strict-aliasing -fstack-protector-strong -g -nostdlib -mcmodel=medany -Wstack-usage=8192 -fstack-usage
CC_FLAGS_KERNEL:= ${CC_FLAGS_GENERIC} -isystem ${KERNEL_HEADER} -isystem ${COMMON_HEADER}
CC_FLAGS_USER:= -Wall -O -fno-omit-frame-pointer -ggdb -g -MD -mcmodel=medany -ffreestanding -fno-common -nostdlib -mno-relax -fstack-protector-strong -isystem ${USER_HEADER} -isystem ${COMMON_HEADER}
LD_FLAGS_KERNEL:= -N -T${PROJ_ROOT}/kernel/kernel-${OS_PLATFORM}.ld
LD_FLAGS_USER:= -z max-page-size=4096 -N -e main -Ttext 0
ifeq ($(OS_PLATFORM), qemu)
CC_FLAGS_KERNEL += -DPLATFORM_QEMU
else
CC_FLAGS_KERNEL += -DPLATFORM_K210
endif
.PHONY: all
all: prerequirement os.bin
.PHONY: prerequirement
prerequirement:
@mkdir -p ${BUILD_DIR}
.PHONY: clean
clean:
rm -rf ${BUILD_DIR}
rm -f os.bin
USERLIB_BUILD_DIR:=${BUILD_DIR}/userlib
KERNEL_BUILD_DIR:=${BUILD_DIR}/kernel
INIT_BUILD_DIR:=${BUILD_DIR}/init
# Kernel builds
KERNEL_ROOT:=${PROJ_ROOT}/kernel
KERNEL_C_SRCS:=$(shell find ${KERNEL_ROOT} -type f -name '*.c')
KERNEL_ASM_SRCS:=$(shell find ${KERNEL_ROOT} -type f -name '*.S')
KERNEL_SRCS:= ${KERNEL_ASM_SRCS} ${KERNEL_C_SRCS}
KERNEL_OBJS:=$(patsubst %, ${KERNEL_BUILD_DIR}/%.o, $(subst ${KERNEL_ROOT}/,,$(KERNEL_SRCS)))
${KERNEL_BUILD_DIR}/%.c.o: ${KERNEL_ROOT}/%.c
@mkdir -p $(dir $@)
@echo "Build $@"
${CC} ${CC_FLAGS_KERNEL} -o $@ -c $<
${KERNEL_BUILD_DIR}/%.S.o: ${KERNEL_ROOT}/%.S
@mkdir -p $(dir $@)
@echo "Build Asmssembly $@"
${CC} ${CC_FLAGS_KERNEL} -DASM_FILE -o $@ -c $<
${BUILD_DIR}/kernel.elf: ${BUILD_DIR}/init.o ${KERNEL_OBJS}
@mkdir -p $(dir $@)
@echo "Build kernel.elf"
${LD} ${LD_FLAGS_KERNEL} -o $@ $^
${OBJDUMP} -S $@ > [email protected]
# Userlib builds
USERLIB_ROOT:=${PROJ_ROOT}/userlib
USERLIB_SRCS:=$(shell find ${USERLIB_ROOT} -type f -name '*.c')
USERLIB_OBJS:=$(patsubst %, ${USERLIB_BUILD_DIR}/%.o, $(subst ${USERLIB_ROOT}/,,$(USERLIB_SRCS)))
${USERLIB_BUILD_DIR}/%.c.o: ${USERLIB_ROOT}/%.c
@mkdir -p $(dir $@)
@echo "Build $@"
${CC} ${CC_FLAGS_GENERIC} -isystem ${COMMON_HEADER} -isystem ${USER_HEADER} -o $@ -c $<
${BUILD_DIR}/libuser.a: ${USERLIB_OBJS}
@mkdir -p $(dir $@)
@echo "Build libuser.a"
${AR} -rcv $@ $^
# Init code builds
INIT_ROOT:=${PROJ_ROOT}/init
INIT_SRCS:=$(shell find ${INIT_ROOT} -type f -name '*.c')
INIT_OBJS:=$(patsubst %, ${INIT_BUILD_DIR}/%.o, $(subst ${INIT_ROOT}/,,$(INIT_SRCS)))
${INIT_BUILD_DIR}/%.c.o: ${INIT_ROOT}/%.c
@mkdir -p $(dir $@)
@echo "Build $@"
${CC} ${CC_FLAGS_USER} -o $@ -c $<
${BUILD_DIR}/init.elf: ${BUILD_DIR}/libuser.a ${INIT_OBJS}
@mkdir -p $(dir $@)
@echo "Build init.elf"
${LD} ${LD_FLAGS_USER} -o $@ ${INIT_OBJS} ${BUILD_DIR}/libuser.a
${BUILD_DIR}/init.o: ${BUILD_DIR}/init.elf ${INIT_ROOT}/package.S
@mkdir -p $(dir $@)
@echo "Build init.o"
${CC} ${CC_FLAGS_GENERIC} -DINIT_ELF=\"${BUILD_DIR}/init.elf\" -o $@ -c ${INIT_ROOT}/package.S
# SBI Build
${BUILD_DIR}/SBI.elf:
cd ${PROJ_ROOT}/SBI/${OS_PLATFORM}/rustsbi-${OS_PLATFORM}/rustsbi-${OS_PLATFORM} && \
cargo build --package rustsbi-k210 --target riscv64imac-unknown-none-elf --release && \
cp ../target/riscv64imac-unknown-none-elf/release/rustsbi-${OS_PLATFORM} \
$@
cd ${PROJ_ROOT}
${BUILD_DIR}/%.bin: ${BUILD_DIR}/%.elf
${OBJCOPY} $< --strip-all -O binary $@
os.bin: ${BUILD_DIR}/kernel.bin ${BUILD_DIR}/SBI.bin
rm -f $@
dd conv=notrunc bs=1 if=${BUILD_DIR}/SBI.bin of=$@
dd conv=notrunc bs=1 if=${BUILD_DIR}/kernel.bin of=$@ seek=${BIN_OFFSET}
ifeq (${OS_PLATFORM}, k210)
flash: os.bin
${PROJ_ROOT}/tools/ktool/ktool.py -p ${FLASH_USB_PORT} -b ${FLASH_BAUDRATE} -t os.bin
endif