Move Rust into Cargo project

This commit is contained in:
Garen Tyler 2023-10-14 16:49:24 -06:00
parent 79ecd97c76
commit b30ea849d1
Signed by: garentyler
GPG Key ID: D7A048C454CB7054
6 changed files with 29 additions and 10 deletions

1
.gitignore vendored
View File

@ -16,3 +16,4 @@ mkfs
kernel/kernel kernel/kernel
user/usys.S user/usys.S
.gdbinit .gdbinit
target/

View File

@ -1,4 +1,5 @@
K=kernel K=kernel
R=$K/rustkernel
U=user U=user
P=programs P=programs
@ -12,7 +13,6 @@ OBJS = \
$K/spinlock.o \ $K/spinlock.o \
$K/string.o \ $K/string.o \
$K/main.o \ $K/main.o \
$K/rust.a \
$K/vm.o \ $K/vm.o \
$K/proc.o \ $K/proc.o \
$K/swtch.o \ $K/swtch.o \
@ -64,10 +64,7 @@ CFLAGS += -mcmodel=medany
CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax
CFLAGS += -I. CFLAGS += -I.
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector) CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
RUSTC = rustc
TARGET_TRIPLE = riscv64gc-unknown-none-elf TARGET_TRIPLE = riscv64gc-unknown-none-elf
RUSTFLAGS = -C soft-float=n --target $(TARGET_TRIPLE)
# Disable PIE when possible (for Ubuntu 16.10 toolchain) # Disable PIE when possible (for Ubuntu 16.10 toolchain)
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),) ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)
@ -79,8 +76,9 @@ endif
LDFLAGS = -z max-page-size=4096 LDFLAGS = -z max-page-size=4096
$K/kernel: $(OBJS) $K/kernel.ld $U/initcode $K/kernel: $(OBJS) $K/kernel.ld $U/initcode $R/src
$(LD) $(LDFLAGS) -T $K/kernel.ld -o $K/kernel $(OBJS) cargo +nightly -Z unstable-options -C $R build
$(LD) $(LDFLAGS) -T $K/kernel.ld -o $K/kernel $(OBJS) $R/target/$(TARGET_TRIPLE)/debug/librustkernel.a
$(OBJDUMP) -S $K/kernel > $K/kernel.asm $(OBJDUMP) -S $K/kernel > $K/kernel.asm
$(OBJDUMP) -t $K/kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $K/kernel.sym $(OBJDUMP) -t $K/kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $K/kernel.sym
@ -95,8 +93,8 @@ tags: $(OBJS) _init
ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o
%.a: %.rs # %.a: %.rs
$(RUSTC) $(RUSTFLAGS) --crate-type staticlib -o $@ -L $K $^ # $(RUSTC) $(RUSTFLAGS) --crate-type staticlib -o $@ -L $K $^
%.o: %.c *.h %.o: %.c *.h
$(CC) $(CFLAGS) -c $^ $(CC) $(CFLAGS) -c $^
@ -157,6 +155,7 @@ clean:
mkfs/mkfs .gdbinit \ mkfs/mkfs .gdbinit \
$U/usys.S \ $U/usys.S \
$(UPROGS) $(UPROGS)
cargo +nightly -Z unstable-options -C $R clean
# try to generate a unique GDB port # try to generate a unique GDB port
GDBPORT = $(shell expr `id -u` % 5000 + 25000) GDBPORT = $(shell expr `id -u` % 5000 + 25000)

View File

@ -0,0 +1,3 @@
[build]
target = "riscv64gc-unknown-none-elf"
rustflags = ["-Csoft-float=n"]

7
kernel/rustkernel/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "rustkernel"
version = "0.1.0"

View File

@ -0,0 +1,9 @@
[package]
name = "rustkernel"
version = "0.1.0"
edition = "2021"
[dependencies]
[lib]
crate-type = ["staticlib"]

View File

@ -4,7 +4,7 @@
use core::ffi::{c_char, CStr}; use core::ffi::{c_char, CStr};
extern "C" { extern "C" {
fn print(message: *const c_char); pub fn print(message: *const c_char);
fn panic(panic_message: *const c_char) -> !; fn panic(panic_message: *const c_char) -> !;
} }
@ -12,7 +12,7 @@ extern "C" {
pub extern "C" fn rust_main() { pub extern "C" fn rust_main() {
unsafe { unsafe {
print( print(
CStr::from_bytes_with_nul(b"Hello from Rust!\0") CStr::from_bytes_with_nul(b"Hello from Rust!\n\0")
.unwrap() .unwrap()
.as_ptr(), .as_ptr(),
); );