-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
127 lines (102 loc) · 3.22 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
# ---------------
# Toolchain stuff
# ---------------
# riscv64-unknown-elf- or riscv64-linux-gnu-
# perhaps located in /opt/riscv/bin
TOOLPREFIX := riscv64-unknown-elf-
KERNEL_SRC := crates/kernel/src
# TODO: move to rust binutils
CC := $(TOOLPREFIX)gcc
AS := $(TOOLPREFIX)as
LD := $(TOOLPREFIX)ld
OBJCOPY := $(TOOLPREFIX)objcopy
OBJDUMP := $(TOOLPREFIX)objdump
TARGET := riscv64gc-unknown-none-elf
TYPE := debug
RELEASE_FLAG :=
TARGET_PATH := ./target/$(TARGET)/$(TYPE)
KERNEL_LIBS := $(TARGET_PATH)
KERNEL_LIB_OUT := $(KERNEL_LIBS)/libkernel.a
KERNEL_LIB_ASM := kernel_lib.S
RUSTFLAGS := -C soft-float -C panic=abort
LDSCRIPT := $(KERNEL_SRC)/kernel.ld
LDFLAGS = -z max-page-size=0x1000 --gc-sections -Map map.txt
CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb -gdwarf-2
CFLAGS += -MD
CFLAGS += -Wl,--gc-sections -mcmodel=medany -march=rv64gc
CFLAGS += -Wl,--no-warn-rwx-segments
CFLAGS += -ffreestanding -nostartfiles -nostdlib -nodefaultlibs -fno-common -mno-relax
CFLAGS += -I.
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)
CFLAGS += -fno-pie -no-pie
endif
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]nopie'),)
CFLAGS += -fno-pie -nopie
endif
# ---------------
# Kernel building (TODO: move to build.rs structure)
#
# Objdumps are here only for debugging purposes and
# don't provide symbol tables for userspace libraries yet
# ---------------
$(KERNEL_LIB_OUT):
RUSTFLAGS="$(RUSTFLAGS)" cargo build --target=$(TARGET) $(RELEASE_FLAG)
$(OBJDUMP) -d $(KERNEL_LIB_OUT) > $(KERNEL_LIB_ASM)
OBJS = \
$(KERNEL_SRC)/asm/entry.o \
$(KERNEL_LIB_OUT)
kernel_img: $(OBJS) $(LDSCRIPT)
$(LD) $(LDFLAGS) -T $(LDSCRIPT) -o kernel_img $(OBJS)
$(OBJDUMP) -S kernel_img > kernel.asm
$(OBJDUMP) -t kernel_img | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > kernel.sym
# -include *.d
# ---------------
# QEMU stuff
# ---------------
QEMU := qemu-system-riscv64
QEMUOPTS = \
-machine virt \
-bios none \
-kernel kernel_img \
-m 128M \
-cpu rv64 \
-smp 1 \
-serial mon:stdio \
-device virtio-rng-device \
-device virtio-gpu-device \
-device virtio-net-device \
-device virtio-tablet-device \
-device virtio-keyboard-device
qemu: kernel_img
$(QEMU) $(QEMUOPTS)
# ---------------
# IDE-related stuff
# ---------------
tags: $(OBJS) _init
etags $(SRC)/*.s $(SRC)/*.c
# ---------------
# Debug
# ---------------
# try to generate a unique GDB port
GDBPORT = 1234
# QEMU's gdb stub command line changed in 0.11
QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
then echo "-gdb tcp::$(GDBPORT)"; \
else echo "-s -p $(GDBPORT)"; fi)
qemu-gdb: kernel_img .gdbinit.tmpl-riscv
@echo "*** Now run 'gdb' in another window." 1>&2
$(QEMU) $(QEMUOPTS) -S $(QEMUGDB)
# ---------------
# Misc
# ---------------
# Prevent deletion of intermediate files, e.g. cat.o, after first build, so
# that disk image changes after first build are persistent until clean. More
# details:
# http://www.gnu.org/software/make/manual/html_node/Chained-Rules.html
.PRECIOUS: %.o
.PHONY: clean
clean:
rm -rf *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \
*.o */*.o *.d */*.d *.asm */*.asm *.sym */*.sym map.txt *.S \
target kernel_img