From 8e11c8d3eafc4452938fc7a0724c5525b8d85fb4 Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Tue, 4 Jan 2022 18:13:16 -0800 Subject: [PATCH 1/5] Remove unnecessary flags The default-linker-libraries flag requires a change in the rustc target to set `no_default_libraries` to false. The muldefs flag isn't necessary anymore. I think the removal of static libs (ex. linker fix and pthread_3ds) might have fixed this issue. The __3DS__ flag doesn't seem to have been necessary at all. Similarly, `-Z unstable-options` isn't needed for cargo. --- src/main.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4f57731..1f4fd46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -109,14 +109,11 @@ fn check_rust_version() { } fn build_elf(args: std::iter::Skip) { - let rustflags = env::var("RUSTFLAGS").unwrap_or("".into()) - + "-C default-linker-libraries -Clink-arg=-z -Clink-arg=muldefs -Clink-arg=-D__3DS__"; + let rustflags = env::var("RUSTFLAGS").unwrap_or_default(); let mut process = Command::new("cargo") .arg("build") .arg("-Z") - .arg("unstable-options") - .arg("-Z") .arg("build-std") .arg("--target") .arg("armv6k-nintendo-3ds") From 5c8013d5ba7fa7167f5e1dfad79d02eccf0216ae Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Tue, 4 Jan 2022 18:13:40 -0800 Subject: [PATCH 2/5] Apply rustfmt --- src/main.rs | 74 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1f4fd46..c333b1c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use cargo_metadata::MetadataCommand; -use rustc_version::{Version, Channel}; +use rustc_version::{Channel, Version}; use std::{ - env, fs, fmt, + env, fmt, fs, process::{self, Command, Stdio}, }; @@ -38,7 +38,11 @@ impl fmt::Display for CommitDate { } } -const MINIMUM_COMMIT_DATE: CommitDate = CommitDate { year: 2021, month: 10, day: 01 }; +const MINIMUM_COMMIT_DATE: CommitDate = CommitDate { + year: 2021, + month: 10, + day: 01, +}; const MINIMUM_RUSTC_VERSION: Version = Version::new(1, 56, 0); fn main() { @@ -56,13 +60,11 @@ fn main() { let command = args.next(); let must_link = match command { None => panic!("No command specified, try with \"build\" or \"link\""), - Some(s) => { - match s.as_str() { - "build" => false, - "link" => true, - _ => panic!("Invalid command, try with \"build\" or \"link\""), - } - } + Some(s) => match s.as_str() { + "build" => false, + "link" => true, + _ => panic!("Invalid command, try with \"build\" or \"link\""), + }, }; build_elf(args); @@ -91,8 +93,10 @@ fn check_rust_version() { let old_commit = match rustc_version.commit_date { None => false, - Some(date) => MINIMUM_COMMIT_DATE > CommitDate::parse(&date) - .expect("could not parse `rustc --version` commit date"), + Some(date) => { + MINIMUM_COMMIT_DATE + > CommitDate::parse(&date).expect("could not parse `rustc --version` commit date") + } }; if old_version || old_commit { @@ -100,9 +104,7 @@ fn check_rust_version() { "cargo-3ds requires rustc nightly version >= {}", MINIMUM_COMMIT_DATE, ); - println!( - "Please run `rustup update nightly` to upgrade your nightly version" - ); + println!("Please run `rustup update nightly` to upgrade your nightly version"); process::exit(1); } @@ -139,15 +141,18 @@ fn build_elf(args: std::iter::Skip) { fn get_metadata() -> CTRConfig { let metadata = MetadataCommand::new() - .exec() - .expect("Failed to get cargo metadata"); + .exec() + .expect("Failed to get cargo metadata"); let root_crate = metadata.root_package().expect("No root crate found"); let icon = String::from("./icon.png"); let icon = if let Err(_) = fs::File::open(&icon) { - format!("{}/libctru/default_icon.png", env::var("DEVKITPRO").unwrap()) + format!( + "{}/libctru/default_icon.png", + env::var("DEVKITPRO").unwrap() + ) } else { icon }; @@ -155,7 +160,10 @@ fn get_metadata() -> CTRConfig { CTRConfig { name: root_crate.name.clone(), author: root_crate.authors[0].clone(), - description: root_crate.description.clone().unwrap_or(String::from("Homebrew Application")), + description: root_crate + .description + .clone() + .unwrap_or(String::from("Homebrew Application")), icon: icon, } } @@ -167,7 +175,10 @@ fn build_3dsx(config: &CTRConfig, opt_lvl: &str) { .arg(&config.description) .arg(&config.author) .arg(&config.icon) - .arg(format!("./target/armv6k-nintendo-3ds/{}/{}.smdh", opt_lvl, config.name)) + .arg(format!( + "./target/armv6k-nintendo-3ds/{}/{}.smdh", + opt_lvl, config.name + )) .stdin(Stdio::inherit()) .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) @@ -187,16 +198,26 @@ fn build_3dsx(config: &CTRConfig, opt_lvl: &str) { let mut command = Command::new("3dsxtool"); let mut process = command - .arg(format!("./target/armv6k-nintendo-3ds/{}/{}.elf", opt_lvl, config.name)) - .arg(format!("./target/armv6k-nintendo-3ds/{}/{}.3dsx", opt_lvl, config.name)) - .arg(format!("--smdh=./target/armv6k-nintendo-3ds/{}/{}.smdh", opt_lvl, config.name)); + .arg(format!( + "./target/armv6k-nintendo-3ds/{}/{}.elf", + opt_lvl, config.name + )) + .arg(format!( + "./target/armv6k-nintendo-3ds/{}/{}.3dsx", + opt_lvl, config.name + )) + .arg(format!( + "--smdh=./target/armv6k-nintendo-3ds/{}/{}.smdh", + opt_lvl, config.name + )); // If romfs directory exists, automatically include it if let Ok(_) = std::fs::read_dir("./romfs") { process = process.arg("--romfs=\"./romfs\""); } - let mut process = process.stdin(Stdio::inherit()) + let mut process = process + .stdin(Stdio::inherit()) .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) .spawn() @@ -216,7 +237,10 @@ fn build_3dsx(config: &CTRConfig, opt_lvl: &str) { fn link(name: &str, opt_lvl: &str) { let mut process = Command::new("3dslink") - .arg(format!("./target/armv6k-nintendo-3ds/{}/{}.3dsx", opt_lvl, name)) + .arg(format!( + "./target/armv6k-nintendo-3ds/{}/{}.3dsx", + opt_lvl, name + )) .stdin(Stdio::inherit()) .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) From 7ac7c25f98ebc62728bcfc23ae7dbf46c9724bb8 Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Tue, 4 Jan 2022 18:24:26 -0800 Subject: [PATCH 3/5] Apply clippy suggestions Also use Path instead of opening files/directories. --- src/main.rs | 48 ++++++++++++++---------------------------------- 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/src/main.rs b/src/main.rs index c333b1c..7559333 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ use cargo_metadata::MetadataCommand; use rustc_version::{Channel, Version}; +use std::path::Path; use std::{ - env, fmt, fs, + env, fmt, process::{self, Command, Stdio}, }; @@ -22,7 +23,7 @@ struct CommitDate { impl CommitDate { fn parse(date: &str) -> Option { - let mut iter = date.split("-"); + let mut iter = date.split('-'); let year = iter.next()?.parse().ok()?; let month = iter.next()?.parse().ok()?; @@ -41,15 +42,14 @@ impl fmt::Display for CommitDate { const MINIMUM_COMMIT_DATE: CommitDate = CommitDate { year: 2021, month: 10, - day: 01, + day: 1, }; const MINIMUM_RUSTC_VERSION: Version = Version::new(1, 56, 0); fn main() { check_rust_version(); - let args: Vec = env::args().collect(); - let optimization_level = match args.contains(&String::from("--release")) { + let optimization_level = match env::args().any(|arg| arg == "--release") { true => String::from("release"), false => String::from("debug"), }; @@ -89,7 +89,7 @@ fn check_rust_version() { process::exit(1); } - let old_version: bool = MINIMUM_RUSTC_VERSION > rustc_version.semver.clone(); + let old_version: bool = MINIMUM_RUSTC_VERSION > rustc_version.semver; let old_commit = match rustc_version.commit_date { None => false, @@ -130,12 +130,7 @@ fn build_elf(args: std::iter::Skip) { let status = process.wait().unwrap(); if !status.success() { - let code = match status.code() { - Some(i) => i, - None => 1, - }; - - process::exit(code); + process::exit(status.code().unwrap_or(1)); } } @@ -148,7 +143,7 @@ fn get_metadata() -> CTRConfig { let icon = String::from("./icon.png"); - let icon = if let Err(_) = fs::File::open(&icon) { + let icon = if !Path::new(&icon).exists() { format!( "{}/libctru/default_icon.png", env::var("DEVKITPRO").unwrap() @@ -163,8 +158,8 @@ fn get_metadata() -> CTRConfig { description: root_crate .description .clone() - .unwrap_or(String::from("Homebrew Application")), - icon: icon, + .unwrap_or_else(|| String::from("Homebrew Application")), + icon, } } @@ -188,12 +183,7 @@ fn build_3dsx(config: &CTRConfig, opt_lvl: &str) { let status = process.wait().unwrap(); if !status.success() { - let code = match status.code() { - Some(i) => i, - None => 1, - }; - - process::exit(code); + process::exit(status.code().unwrap_or(1)); } let mut command = Command::new("3dsxtool"); @@ -212,7 +202,7 @@ fn build_3dsx(config: &CTRConfig, opt_lvl: &str) { )); // If romfs directory exists, automatically include it - if let Ok(_) = std::fs::read_dir("./romfs") { + if Path::new("./romfs").is_dir() { process = process.arg("--romfs=\"./romfs\""); } @@ -226,12 +216,7 @@ fn build_3dsx(config: &CTRConfig, opt_lvl: &str) { let status = process.wait().unwrap(); if !status.success() { - let code = match status.code() { - Some(i) => i, - None => 1, - }; - - process::exit(code); + process::exit(status.code().unwrap_or(1)); } } @@ -250,11 +235,6 @@ fn link(name: &str, opt_lvl: &str) { let status = process.wait().unwrap(); if !status.success() { - let code = match status.code() { - Some(i) => i, - None => 1, - }; - - process::exit(code); + process::exit(status.code().unwrap_or(1)); } } From e79c62144572d1b9b8a216d13919d21c6bfe9ffe Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Tue, 4 Jan 2022 18:25:48 -0800 Subject: [PATCH 4/5] Simplify icon finding --- src/main.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7559333..a96e4f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -141,16 +141,14 @@ fn get_metadata() -> CTRConfig { let root_crate = metadata.root_package().expect("No root crate found"); - let icon = String::from("./icon.png"); + let mut icon = String::from("./icon.png"); - let icon = if !Path::new(&icon).exists() { - format!( + if !Path::new(&icon).exists() { + icon = format!( "{}/libctru/default_icon.png", env::var("DEVKITPRO").unwrap() ) - } else { - icon - }; + } CTRConfig { name: root_crate.name.clone(), From 25d4713ad221ce25c0284331ee442a3e2c744bfa Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Tue, 4 Jan 2022 21:05:32 -0800 Subject: [PATCH 5/5] Remove RUSTFLAGS handling Now that we don't add any flags, we don't need to explicitly pass it through. --- src/main.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index a96e4f5..d1a8e59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -111,8 +111,6 @@ fn check_rust_version() { } fn build_elf(args: std::iter::Skip) { - let rustflags = env::var("RUSTFLAGS").unwrap_or_default(); - let mut process = Command::new("cargo") .arg("build") .arg("-Z") @@ -120,7 +118,6 @@ fn build_elf(args: std::iter::Skip) { .arg("--target") .arg("armv6k-nintendo-3ds") .args(args) - .env("RUSTFLAGS", rustflags) .stdin(Stdio::inherit()) .stdout(Stdio::inherit()) .stderr(Stdio::inherit())