From b66ab5706d0f179bac0881736666a3a88fe36ff4 Mon Sep 17 00:00:00 2001 From: Kosmas Raptis Date: Tue, 19 Apr 2022 18:58:53 +0300 Subject: [PATCH 1/2] Rust! (but better) --- samples/rust/.cargo/config | 2 ++ samples/rust/Cargo.toml | 15 +++++++++++++++ samples/rust/Makefile | 25 +++++++++++++++++++++++++ samples/rust/build.rs | 21 +++++++++++++++++++++ samples/rust/src/main.rs | 28 ++++++++++++++++++++++++++++ samples/rust/xbox.json | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 123 insertions(+) create mode 100644 samples/rust/.cargo/config create mode 100644 samples/rust/Cargo.toml create mode 100644 samples/rust/Makefile create mode 100644 samples/rust/build.rs create mode 100644 samples/rust/src/main.rs create mode 100644 samples/rust/xbox.json diff --git a/samples/rust/.cargo/config b/samples/rust/.cargo/config new file mode 100644 index 000000000..0ab361db3 --- /dev/null +++ b/samples/rust/.cargo/config @@ -0,0 +1,2 @@ +[build] +target = "xbox.json" \ No newline at end of file diff --git a/samples/rust/Cargo.toml b/samples/rust/Cargo.toml new file mode 100644 index 000000000..41a80f3f2 --- /dev/null +++ b/samples/rust/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "rustnxdk" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +# Default cargo profile +[profile.dev] +panic = "abort" + +[profile.release] +panic = "abort" \ No newline at end of file diff --git a/samples/rust/Makefile b/samples/rust/Makefile new file mode 100644 index 000000000..b63a34ecf --- /dev/null +++ b/samples/rust/Makefile @@ -0,0 +1,25 @@ +PROJECT_NAME = rustnxdk +XISO_NAME = rusttest +XBE_TITLE = rusttest +NXDK_DIR = /home/$(USER)/nxdk +OUTPUT_DIR = bin +RUSTFLAGS := --target xbox.json -Zbuild-std=core + +CXBE = $(NXDK_DIR)/tools/cxbe/cxbe +XISO = $(NXDK_DIR)/tools/extract-xiso/build/extract-xiso + +EXEC_DIR = target/xbox/release + +ifeq ($(DEBUG),y) + EXEC_DIR = target/xbox/debug +else + RUSTFLAGS += --release +endif + +all: + @mkdir -p $(OUTPUT_DIR) + @cargo build $(RUSTFLAGS) + @cp $(EXEC_DIR)/$(PROJECT_NAME).exe ./main.exe + $(CXBE) -OUT:default.xbe -TITLE:$(XBE_TITLE) ./main.exe + @mv default.xbe $(OUTPUT_DIR) + $(XISO) -c $(OUTPUT_DIR) $(XISO_NAME).iso \ No newline at end of file diff --git a/samples/rust/build.rs b/samples/rust/build.rs new file mode 100644 index 000000000..de0debe53 --- /dev/null +++ b/samples/rust/build.rs @@ -0,0 +1,21 @@ +extern crate core; +use std::env; + +fn main() { + let nxdk_dir = match env::var("NXDK_DIR") { + Ok(value) => value, + Err(e) => panic!("Error getting NXDK_DIR variable: {}", e) + }; + + println!("cargo:build-std=core"); + println!("cargo:rustc-link-search={}/lib/xboxkrnl", nxdk_dir); + println!("cargo:rustc-link-lib=static=libxboxkrnl"); + println!("cargo:rustc-link-search={}/lib/", nxdk_dir); + println!("cargo:rustc-link-lib=static=libpdclib"); + println!("cargo:rustc-link-lib=static=libwinapi"); + println!("cargo:rustc-link-lib=static=libxboxrt"); + println!("cargo:rustc-link-lib=static=libnxdk_hal"); + + + +} \ No newline at end of file diff --git a/samples/rust/src/main.rs b/samples/rust/src/main.rs new file mode 100644 index 000000000..c65bb2da6 --- /dev/null +++ b/samples/rust/src/main.rs @@ -0,0 +1,28 @@ +#![no_std] // don't link the Rust standard library +#![no_main] // disable all Rust-level entry points + +use core::panic::PanicInfo; + +#[link (name="libnxdk_hal")] +extern "C" { + fn XVideoSetMode(_: i32, _: i32, _: i32, _:i32); + fn debugPrint(_: &str); +} + +#[no_mangle] +fn main() -> i32 { + unsafe { + // 640x480, 32bpp, 60Hz + XVideoSetMode(640, 480, 32, 60); + debugPrint("Hello from Rust"); + } + loop { + + } +} + +// This function is called on panic. +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop {} +} \ No newline at end of file diff --git a/samples/rust/xbox.json b/samples/rust/xbox.json new file mode 100644 index 000000000..197ff063a --- /dev/null +++ b/samples/rust/xbox.json @@ -0,0 +1,32 @@ +{ + "abi-return-struct-as-int": true, + "arch": "x86", + "cpu": "pentium3", + "data-layout": "e-m:x-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32-a:0:32-S32", + "dll-prefix": "", + "dll-suffix": ".dll", + "env": "msvc", + "exe-suffix": ".exe", + "executables": true, + "is-like-msvc": false, + "is-like-windows": true, + "linker-flavor": "lld-link", + "linker-is-gnu": false, + "lld-flavor": "link", + "llvm-target": "i386-pc-win32", + "max-atomic-width": 64, + "os": "none", + "pre-link-args": { + "lld-link": [ + "-subsystem:windows", + "-fixed", + "-base:0x00010000", + "-stack:65536", + "-merge:.edata=.edataxb" + ] + }, + "requires-uwtable": true, + "target-pointer-width": "32", + "target-family": "windows", + "vendor": "pc" +} From 161f1ac00f0ce9a7ce23d7096255390428081665 Mon Sep 17 00:00:00 2001 From: Kosmas Raptis Date: Tue, 19 Apr 2022 19:07:43 +0300 Subject: [PATCH 2/2] Default to nightly Cargo --- samples/rust/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/rust/Makefile b/samples/rust/Makefile index b63a34ecf..307ce6e83 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -18,7 +18,7 @@ endif all: @mkdir -p $(OUTPUT_DIR) - @cargo build $(RUSTFLAGS) + @cargo +nightly build $(RUSTFLAGS) @cp $(EXEC_DIR)/$(PROJECT_NAME).exe ./main.exe $(CXBE) -OUT:default.xbe -TITLE:$(XBE_TITLE) ./main.exe @mv default.xbe $(OUTPUT_DIR)