Browse Source

Merge pull request #4 from AzureMarker/master

Remove unnecessary flags and apply suggestions from rustfmt & clippy
pull/5/head
Meziu 3 years ago committed by GitHub
parent
commit
f462e13059
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 128
      src/main.rs

128
src/main.rs

@ -1,7 +1,8 @@
use cargo_metadata::MetadataCommand; use cargo_metadata::MetadataCommand;
use rustc_version::{Version, Channel}; use rustc_version::{Channel, Version};
use std::path::Path;
use std::{ use std::{
env, fs, fmt, env, fmt,
process::{self, Command, Stdio}, process::{self, Command, Stdio},
}; };
@ -22,7 +23,7 @@ struct CommitDate {
impl CommitDate { impl CommitDate {
fn parse(date: &str) -> Option<Self> { fn parse(date: &str) -> Option<Self> {
let mut iter = date.split("-"); let mut iter = date.split('-');
let year = iter.next()?.parse().ok()?; let year = iter.next()?.parse().ok()?;
let month = iter.next()?.parse().ok()?; let month = iter.next()?.parse().ok()?;
@ -38,14 +39,17 @@ 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: 1,
};
const MINIMUM_RUSTC_VERSION: Version = Version::new(1, 56, 0); const MINIMUM_RUSTC_VERSION: Version = Version::new(1, 56, 0);
fn main() { fn main() {
check_rust_version(); check_rust_version();
let args: Vec<String> = env::args().collect(); let optimization_level = match env::args().any(|arg| arg == "--release") {
let optimization_level = match args.contains(&String::from("--release")) {
true => String::from("release"), true => String::from("release"),
false => String::from("debug"), false => String::from("debug"),
}; };
@ -56,13 +60,11 @@ fn main() {
let command = args.next(); let command = args.next();
let must_link = match command { let must_link = match command {
None => panic!("No command specified, try with \"build\" or \"link\""), None => panic!("No command specified, try with \"build\" or \"link\""),
Some(s) => { Some(s) => match s.as_str() {
match s.as_str() { "build" => false,
"build" => false, "link" => true,
"link" => true, _ => panic!("Invalid command, try with \"build\" or \"link\""),
_ => panic!("Invalid command, try with \"build\" or \"link\""), },
}
}
}; };
build_elf(args); build_elf(args);
@ -87,12 +89,14 @@ fn check_rust_version() {
process::exit(1); 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 { let old_commit = match rustc_version.commit_date {
None => false, None => false,
Some(date) => MINIMUM_COMMIT_DATE > CommitDate::parse(&date) Some(date) => {
.expect("could not parse `rustc --version` commit date"), MINIMUM_COMMIT_DATE
> CommitDate::parse(&date).expect("could not parse `rustc --version` commit date")
}
}; };
if old_version || old_commit { if old_version || old_commit {
@ -100,28 +104,20 @@ fn check_rust_version() {
"cargo-3ds requires rustc nightly version >= {}", "cargo-3ds requires rustc nightly version >= {}",
MINIMUM_COMMIT_DATE, MINIMUM_COMMIT_DATE,
); );
println!( println!("Please run `rustup update nightly` to upgrade your nightly version");
"Please run `rustup update nightly` to upgrade your nightly version"
);
process::exit(1); process::exit(1);
} }
} }
fn build_elf(args: std::iter::Skip<env::Args>) { fn build_elf(args: std::iter::Skip<env::Args>) {
let rustflags = env::var("RUSTFLAGS").unwrap_or("".into())
+ "-C default-linker-libraries -Clink-arg=-z -Clink-arg=muldefs -Clink-arg=-D__3DS__";
let mut process = Command::new("cargo") let mut process = Command::new("cargo")
.arg("build") .arg("build")
.arg("-Z") .arg("-Z")
.arg("unstable-options")
.arg("-Z")
.arg("build-std") .arg("build-std")
.arg("--target") .arg("--target")
.arg("armv6k-nintendo-3ds") .arg("armv6k-nintendo-3ds")
.args(args) .args(args)
.env("RUSTFLAGS", rustflags)
.stdin(Stdio::inherit()) .stdin(Stdio::inherit())
.stdout(Stdio::inherit()) .stdout(Stdio::inherit())
.stderr(Stdio::inherit()) .stderr(Stdio::inherit())
@ -131,35 +127,34 @@ fn build_elf(args: std::iter::Skip<env::Args>) {
let status = process.wait().unwrap(); let status = process.wait().unwrap();
if !status.success() { if !status.success() {
let code = match status.code() { process::exit(status.code().unwrap_or(1));
Some(i) => i,
None => 1,
};
process::exit(code);
} }
} }
fn get_metadata() -> CTRConfig { fn get_metadata() -> CTRConfig {
let metadata = MetadataCommand::new() let metadata = MetadataCommand::new()
.exec() .exec()
.expect("Failed to get cargo metadata"); .expect("Failed to get cargo metadata");
let root_crate = metadata.root_package().expect("No root crate found"); 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 let Err(_) = fs::File::open(&icon) { if !Path::new(&icon).exists() {
format!("{}/libctru/default_icon.png", env::var("DEVKITPRO").unwrap()) icon = format!(
} else { "{}/libctru/default_icon.png",
icon env::var("DEVKITPRO").unwrap()
}; )
}
CTRConfig { CTRConfig {
name: root_crate.name.clone(), name: root_crate.name.clone(),
author: root_crate.authors[0].clone(), author: root_crate.authors[0].clone(),
description: root_crate.description.clone().unwrap_or(String::from("Homebrew Application")), description: root_crate
icon: icon, .description
.clone()
.unwrap_or_else(|| String::from("Homebrew Application")),
icon,
} }
} }
@ -170,7 +165,10 @@ fn build_3dsx(config: &CTRConfig, opt_lvl: &str) {
.arg(&config.description) .arg(&config.description)
.arg(&config.author) .arg(&config.author)
.arg(&config.icon) .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()) .stdin(Stdio::inherit())
.stdout(Stdio::inherit()) .stdout(Stdio::inherit())
.stderr(Stdio::inherit()) .stderr(Stdio::inherit())
@ -180,26 +178,31 @@ fn build_3dsx(config: &CTRConfig, opt_lvl: &str) {
let status = process.wait().unwrap(); let status = process.wait().unwrap();
if !status.success() { if !status.success() {
let code = match status.code() { process::exit(status.code().unwrap_or(1));
Some(i) => i,
None => 1,
};
process::exit(code);
} }
let mut command = Command::new("3dsxtool"); let mut command = Command::new("3dsxtool");
let mut process = command let mut process = command
.arg(format!("./target/armv6k-nintendo-3ds/{}/{}.elf", opt_lvl, config.name)) .arg(format!(
.arg(format!("./target/armv6k-nintendo-3ds/{}/{}.3dsx", opt_lvl, config.name)) "./target/armv6k-nintendo-3ds/{}/{}.elf",
.arg(format!("--smdh=./target/armv6k-nintendo-3ds/{}/{}.smdh", opt_lvl, config.name)); 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 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\""); process = process.arg("--romfs=\"./romfs\"");
} }
let mut process = process.stdin(Stdio::inherit()) let mut process = process
.stdin(Stdio::inherit())
.stdout(Stdio::inherit()) .stdout(Stdio::inherit())
.stderr(Stdio::inherit()) .stderr(Stdio::inherit())
.spawn() .spawn()
@ -208,18 +211,16 @@ fn build_3dsx(config: &CTRConfig, opt_lvl: &str) {
let status = process.wait().unwrap(); let status = process.wait().unwrap();
if !status.success() { if !status.success() {
let code = match status.code() { process::exit(status.code().unwrap_or(1));
Some(i) => i,
None => 1,
};
process::exit(code);
} }
} }
fn link(name: &str, opt_lvl: &str) { fn link(name: &str, opt_lvl: &str) {
let mut process = Command::new("3dslink") 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()) .stdin(Stdio::inherit())
.stdout(Stdio::inherit()) .stdout(Stdio::inherit())
.stderr(Stdio::inherit()) .stderr(Stdio::inherit())
@ -229,11 +230,6 @@ fn link(name: &str, opt_lvl: &str) {
let status = process.wait().unwrap(); let status = process.wait().unwrap();
if !status.success() { if !status.success() {
let code = match status.code() { process::exit(status.code().unwrap_or(1));
Some(i) => i,
None => 1,
};
process::exit(code);
} }
} }

Loading…
Cancel
Save