From f33ef0649837cd5ddc00423dbcdf1fc559457a41 Mon Sep 17 00:00:00 2001 From: hheik <4469778+hheik@users.noreply.github.com> Date: Mon, 29 Apr 2024 22:42:59 +0300 Subject: [PATCH] Added basic rust library for testing --- .cargo/config.toml | 5 +++++ .rustfmt.toml | 1 + Makefile | 15 +++++++++++++-- rust/foo/.gitignore | 1 + rust/foo/Cargo.lock | 7 +++++++ rust/foo/Cargo.toml | 8 ++++++++ rust/foo/src/lib.rs | 5 +++++ 7 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 .rustfmt.toml create mode 100644 rust/foo/.gitignore create mode 100644 rust/foo/Cargo.lock create mode 100644 rust/foo/Cargo.toml create mode 100644 rust/foo/src/lib.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..bcbf4d3 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,5 @@ +[build] +target = "riscv64gc-unknown-none-elf" + +[target.riscv64-unknown-elf] +linker = "/opt/riscv/bin/riscv64-unknown-elf-gcc" diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..218e203 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1 @@ +hard_tabs = true diff --git a/Makefile b/Makefile index 39a99d7..7516a3c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,9 @@ K=kernel U=user +R=rust + +RTARGET = riscv64gc-unknown-none-elf +RENV = debug OBJS = \ $K/entry.o \ @@ -30,6 +34,9 @@ OBJS = \ $K/plic.o \ $K/virtio_disk.o +RLIBS = \ + $R/foo/target/$(RTARGET)/$(RENV)/libfoo.rlib + # riscv64-unknown-elf- or riscv64-linux-gnu- # perhaps in /opt/riscv/bin #TOOLPREFIX = @@ -73,8 +80,8 @@ endif LDFLAGS = -z max-page-size=4096 -$K/kernel: $(OBJS) $K/kernel.ld $U/initcode - $(LD) $(LDFLAGS) -T $K/kernel.ld -o $K/kernel $(OBJS) +$K/kernel: $(OBJS) $K/kernel.ld $U/initcode rustlibs + $(LD) $(LDFLAGS) -T $K/kernel.ld -o $K/kernel $(OBJS) $(RLIBS) $(OBJDUMP) -S $K/kernel > $K/kernel.asm $(OBJDUMP) -t $K/kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $K/kernel.sym @@ -138,6 +145,9 @@ fs.img: mkfs/mkfs README $(UPROGS) -include kernel/*.d user/*.d +rustlibs: + cargo build --manifest-path $R/foo/Cargo.toml + clean: rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \ */*.o */*.d */*.asm */*.sym \ @@ -145,6 +155,7 @@ clean: mkfs/mkfs .gdbinit \ $U/usys.S \ $(UPROGS) + cargo clean --manifest-path $R/foo/Cargo.toml # try to generate a unique GDB port GDBPORT = $(shell expr `id -u` % 5000 + 25000) diff --git a/rust/foo/.gitignore b/rust/foo/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/rust/foo/.gitignore @@ -0,0 +1 @@ +/target diff --git a/rust/foo/Cargo.lock b/rust/foo/Cargo.lock new file mode 100644 index 0000000..0e5399e --- /dev/null +++ b/rust/foo/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "foo" +version = "0.1.0" diff --git a/rust/foo/Cargo.toml b/rust/foo/Cargo.toml new file mode 100644 index 0000000..1d9cfe3 --- /dev/null +++ b/rust/foo/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "foo" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/foo/src/lib.rs b/rust/foo/src/lib.rs new file mode 100644 index 0000000..0e4f091 --- /dev/null +++ b/rust/foo/src/lib.rs @@ -0,0 +1,5 @@ +#![no_std] + +pub fn add(left: usize, right: usize) -> usize { + left + right + 5 +}