From 7d201d825b55d13b4d81deaa0fa1281d55aa7da5 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Sun, 29 Oct 2023 00:47:53 -0400 Subject: [PATCH] Always link ctru, for now Also add a little extra description helper for unknown error types in ctru::error --- ctru-rs/src/error.rs | 8 +++++++- ctru-sys/Cargo.toml | 4 ++++ ctru-sys/build.rs | 25 +++++++++++++++++-------- ctru-sys/src/lib.rs | 10 ++++------ 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/ctru-rs/src/error.rs b/ctru-rs/src/error.rs index 0dd7cc5..dc5a421 100644 --- a/ctru-rs/src/error.rs +++ b/ctru-rs/src/error.rs @@ -256,7 +256,13 @@ fn result_code_description_str(result: ctru_sys::Result) -> Cow<'static, str> { RD_NOT_AUTHORIZED => "not_authorized", RD_TOO_LARGE => "too_large", RD_INVALID_SELECTION => "invalid_selection", - code => return Cow::Owned(format!("(unknown description: {code:#x})")), + code => { + let error = unsafe { CStr::from_ptr(ctru_sys::osStrError(result)) }.to_str(); + match error { + Ok(err) => err, + Err(_) => return Cow::Owned(format!("(unknown description: {code:#x})")), + } + } }) } diff --git a/ctru-sys/Cargo.toml b/ctru-sys/Cargo.toml index d324c4e..3cd651d 100644 --- a/ctru-sys/Cargo.toml +++ b/ctru-sys/Cargo.toml @@ -21,6 +21,10 @@ doxygen-rs = "0.4.2" itertools = "0.11.0" which = "4.4.0" +[dev-dependencies] +shim-3ds = { git = "https://github.com/rust3ds/shim-3ds.git" } +test-runner = { git = "https://github.com/rust3ds/test-runner.git" } + [package.metadata.docs.rs] default-target = "armv6k-nintendo-3ds" targets = [] diff --git a/ctru-sys/build.rs b/ctru-sys/build.rs index f6b295c..3189455 100644 --- a/ctru-sys/build.rs +++ b/ctru-sys/build.rs @@ -19,19 +19,28 @@ impl ParseCallbacks for CustomCallbacks { fn main() { let devkitpro = env::var("DEVKITPRO").unwrap(); let devkitarm = env::var("DEVKITARM").unwrap(); - let profile = env::var("PROFILE").unwrap(); + let debuginfo = env::var("DEBUG").unwrap(); let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-env-changed=DEVKITPRO"); println!("cargo:rustc-link-search=native={devkitpro}/libctru/lib"); - println!( - "cargo:rustc-link-lib=static={}", - match profile.as_str() { - "debug" => "ctrud", - _ => "ctru", - } - ); + + let linked_libctru = match debuginfo.as_str() { + // Normally this should just be "true" or "false", but just in case, + // we don't support all the different options documented in + // https://doc.rust-lang.org/cargo/reference/profiles.html#debug + // so just default to linking with debuginfo if it wasn't disabled + "0" | "false" | "none" => "ctru", + // TODO https://github.com/rust3ds/cargo-3ds/issues/14#issuecomment-1783991872 + // To link properly, this must be the same as the library linked by cargo-3ds + // building the standard library, which is always `ctru` in practice. + // Ideally we should link `ctrud` if debug symbols are requested though: + _ => "ctru", + // _ => "ctrud", + }; + + println!("cargo:rustc-link-lib=static={linked_libctru}"); detect_and_track_libctru(); diff --git a/ctru-sys/src/lib.rs b/ctru-sys/src/lib.rs index 32252e6..34a214d 100644 --- a/ctru-sys/src/lib.rs +++ b/ctru-sys/src/lib.rs @@ -3,6 +3,8 @@ #![allow(non_camel_case_types)] #![allow(non_snake_case)] #![allow(clippy::all)] +#![cfg_attr(test, feature(custom_test_frameworks))] +#![cfg_attr(test, test_runner(test_runner::run_gdb))] pub mod result; pub use result::*; @@ -15,10 +17,6 @@ pub unsafe fn errno() -> s32 { *__errno() } -// TODO: not sure if there's a better way to do this, but I have gotten myself -// with this a couple times so having the hint seems nice to have. +// Prevent linking errors from the standard `test` library when running `cargo 3ds test --lib`. #[cfg(test)] -compile_error!(concat!( - "ctru-sys doesn't have tests and its lib test will fail to build at link time. ", - "Try specifying `--package ctru-rs` to build those tests.", -)); +extern crate shim_3ds;