From 54099ec70c34a6efe2081961cc851e29e0529317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Wed, 30 Oct 2024 15:02:23 +0100 Subject: [PATCH] non_windows port --- src/lib.rs | 2 +- src/main.rs | 1 - src/non_windows.rs | 36 +++++++++++++++++++++--------------- src/windows_msvc.rs | 2 +- src/windows_not_msvc.rs | 2 +- 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ade0053..eabf8a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -353,7 +353,7 @@ pub fn compile_for_everything, Ms: AsRef, Mi: IntoIterator } fn compile_impl, Mi: IntoIterator>(resource_file: &Path, macros: Mi) -> Result<(&str, String, String), CompilationResult> { - let comp = ResourceCompiler::new(); + let mut comp = ResourceCompiler::new(); if let Some(missing) = comp.is_supported() { if missing.is_empty() { Err(CompilationResult::NotWindows) diff --git a/src/main.rs b/src/main.rs index 0742478..d1cab14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,6 @@ use std::env; fn main() { - #[cfg(target_os = "windows")] env::set_var("TARGET", if cfg!(target_arch = "x86_64") { "x86_64" diff --git a/src/non_windows.rs b/src/non_windows.rs index 38b0573..7997e8f 100644 --- a/src/non_windows.rs +++ b/src/non_windows.rs @@ -1,10 +1,10 @@ use std::process::{Command, Stdio}; use std::path::{PathBuf, Path}; use self::super::apply_macros; +use std::{env, fs, mem}; use std::borrow::Cow; use std::ffi::OsStr; use memchr::memmem; -use std::{env, fs}; #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] @@ -18,8 +18,14 @@ impl ResourceCompiler { } #[inline] - pub fn is_supported(&self) -> Option> { - self.compiler.err() + pub fn is_supported(&mut self) -> Option> { + match mem::replace(&mut self.compiler, Err("".into())) { + Ok(c) => { + self.compiler = Ok(c); + None + } + Err(e) => Some(e), + } } pub fn compile_resource, Mi: IntoIterator>(&self, out_dir: &str, prefix: &str, resource: &str, macros: Mi) @@ -47,7 +53,7 @@ struct Compiler { impl Compiler { pub fn probe() -> Result> { - let target = env::var("TARGET").map_err(|_| "no $TARGET".into())?; + let target = env::var("TARGET").map_err(|_| Cow::from("no $TARGET"))?; if let Some(rc) = env::var(&format!("RC_{}", target)) .or_else(|_| env::var(&format!("RC_{}", target.replace('-', "_")))) @@ -68,7 +74,7 @@ impl Compiler { } } else if target.ends_with("-pc-windows-msvc") { if is_runnable("llvm-rc") { - return Some(Compiler { + return Ok(Compiler { tp: CompilerType::LlvmRc { has_no_preprocess: Command::new("llvm-rc") .arg("/?") @@ -87,7 +93,8 @@ impl Compiler { Err("".into()) } - pub fn compile, Mi: IntoIterator>(&self, out_dir: &str, prefix: &str, resource: &str, macros: Mi) -> String { + pub fn compile, Mi: IntoIterator>(&self, out_dir: &str, prefix: &str, resource: &str, macros: Mi) + -> Result> { let out_file = format!("{}/{}.lib", out_dir, prefix); match self.tp { CompilerType::LlvmRc { has_no_preprocess } => { @@ -97,8 +104,7 @@ impl Compiler { .file(resource) .cargo_metadata(false) .include(out_dir) - .expand()) - .unwrap(); + .expand()).map_err(|e| e.to_string())?; try_command(Command::new(&self.executable[..]) .args(&["/fo", &out_file]) @@ -116,7 +122,7 @@ impl Compiler { Path::new(&self.executable[..]), "compile", &preprocessed_path, - &out_file); + &out_file)?; } CompilerType::WindRes => { try_command(apply_macros(Command::new(&self.executable[..]) @@ -126,10 +132,10 @@ impl Compiler { Path::new(&self.executable[..]), "compile", resource, - &out_file); + &out_file)?; } } - out_file + Ok(out_file) } } @@ -150,11 +156,11 @@ fn cc_xc(to: &mut cc::Build) -> &mut cc::Build { to } -fn try_command(cmd: &mut Command, exec: &Path, action: &str, whom: &str, whre: &str) { +fn try_command(cmd: &mut Command, exec: &Path, action: &str, whom: &str, whre: &str) -> Result<(), Cow<'static, str>> { match cmd.status() { - Ok(stat) if stat.success() => {} - Ok(stat) => panic!("{} failed to {} \"{}\" into \"{}\" with {}", exec.display(), action, whom, whre, stat), - Err(e) => panic!("Couldn't execute {} to {} \"{}\" into \"{}\": {}", exec.display(), action, whom, whre, e), + Ok(stat) if stat.success() => Ok(()), + Ok(stat) => Err(format!("{} failed to {} \"{}\" into \"{}\" with {}", exec.display(), action, whom, whre, stat).into()), + Err(e) => Err(format!("Couldn't execute {} to {} \"{}\" into \"{}\": {}", exec.display(), action, whom, whre, e).into()), } } diff --git a/src/windows_msvc.rs b/src/windows_msvc.rs index 2670e7b..b0d9dd1 100644 --- a/src/windows_msvc.rs +++ b/src/windows_msvc.rs @@ -22,7 +22,7 @@ impl ResourceCompiler { } #[inline(always)] - pub fn is_supported(&self) -> Option> { + pub fn is_supported(&mut self) -> Option> { None } diff --git a/src/windows_not_msvc.rs b/src/windows_not_msvc.rs index 7891fab..84ed570 100644 --- a/src/windows_not_msvc.rs +++ b/src/windows_not_msvc.rs @@ -17,7 +17,7 @@ impl ResourceCompiler { } #[inline(always)] - pub fn is_supported(&self) -> Option> { + pub fn is_supported(&mut self) -> Option> { None }