Build some C in Cargo project

This commit is contained in:
Garen Tyler 2024-07-20 11:31:20 -06:00
parent b30ea849d1
commit 586b48f850
Signed by: garentyler
GPG Key ID: D7A048C454CB7054
7 changed files with 66 additions and 2 deletions

View File

@ -63,6 +63,8 @@ CFLAGS += -MD
CFLAGS += -mcmodel=medany
CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax
CFLAGS += -I.
CFLAGS += -march=rv64gc
CFLAGS += -mabi=lp64d
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
TARGET_TRIPLE = riscv64gc-unknown-none-elf
@ -77,7 +79,7 @@ endif
LDFLAGS = -z max-page-size=4096
$K/kernel: $(OBJS) $K/kernel.ld $U/initcode $R/src
cargo +nightly -Z unstable-options -C $R build
CC="$(CC)" CFLAGS="$(CFLAGS)" 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) -t $K/kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $K/kernel.sym

View File

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

View File

@ -2,6 +2,15 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "cc"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2aba8f4e9906c7ce3c73463f62a7f0c65183ada1a2d47e397cc8810827f9694f"
[[package]]
name = "rustkernel"
version = "0.1.0"
dependencies = [
"cc",
]

View File

@ -5,5 +5,8 @@ edition = "2021"
[dependencies]
[build-dependencies]
cc = "1.0"
[lib]
crate-type = ["staticlib"]

View File

@ -0,0 +1,45 @@
fn main() {
cc::Build::new()
.compiler("riscv64-unknown-elf-gcc")
.flag("-Wall")
.flag("-Werror")
.flag("-O")
.flag("-fno-omit-frame-pointer")
.flag("-ggdb")
.flag("-gdwarf-2")
.flag("-MD")
.flag("-mcmodel=medany")
.flag("-ffreestanding")
.flag("-fno-common")
.flag("-nostdlib")
.flag("-mno-relax")
.flag("-I.")
.flag("-march=rv64gc")
.flag("-mabi=lp64d")
.flag_if_supported("-fno-stack-protector")
.flag_if_supported("-fno-pie")
.flag_if_supported("-no-pie")
.file("src/c/layered.c")
.compile("layered");
}
/*
Wall -Werror -O -fno-omit-frame-pointer -ggdb -gdwarf-2
-MD
-mcmodel=medany
-ffreestanding -fno-common -nostdlib -mno-relax
-I.
-fno-stack-protector
-fno-pie -no-pie
TARGET_TRIPLE = riscv64gc-unknown-none-elf
# Disable PIE when possible (for Ubuntu 16.10 toolchain)
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)
endif
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]nopie'),)
CFLAGS += -fno-pie -nopie
endif
*/

View File

@ -0,0 +1,3 @@
char *test_layered() {
return "Hello from layered C!";
}

View File

@ -6,6 +6,7 @@ use core::ffi::{c_char, CStr};
extern "C" {
pub fn print(message: *const c_char);
fn panic(panic_message: *const c_char) -> !;
fn test_layered() -> *const c_char;
}
#[no_mangle]
@ -16,6 +17,7 @@ pub extern "C" fn rust_main() {
.unwrap()
.as_ptr(),
);
print(test_layered());
}
}