From: Jacob Lifshay Date: Fri, 21 May 2021 02:15:15 +0000 (-0700) Subject: don't fail if rustfmt not found X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=60f7e09b17d800a3e405a91cac7be7db05cacdd6;p=vector-math.git don't fail if rustfmt not found --- diff --git a/Cargo.toml b/Cargo.toml index d709c7a..56ad7be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,6 @@ core_simd = { version = "0.1.0", git = "https://github.com/rust-lang/stdsimd", o [build-dependencies] vector-math-build-helpers = { version = "=0.1.0", path = "vector-math-build-helpers" } -which = "3" [features] default = ["fma"] diff --git a/build.rs b/build.rs index 33d01bc..d0bd64d 100644 --- a/build.rs +++ b/build.rs @@ -8,23 +8,39 @@ use std::{ use vector_math_build_helpers::make_context_types; fn format_source(source: String) -> String { - let rustfmt_path = which::which("rustfmt").unwrap(); - let mut command = Command::new(rustfmt_path) + match try_format_source(&source) { + Ok(v) => v, + Err(e) => { + eprintln!("rustfmt failed: {}", e); + source + } + } +} + +fn try_format_source(source: &str) -> io::Result { + let mut command = Command::new("rustfmt") .stdin(Stdio::piped()) .stdout(Stdio::piped()) - .spawn() - .unwrap(); + .spawn()?; let stdin = command.stdin.take().unwrap(); let reader_thread = thread::spawn(move || -> io::Result<(String, Child)> { let mut output = String::new(); command.stdout.take().unwrap().read_to_string(&mut output)?; Ok((output, command)) }); - { stdin }.write_all(source.as_bytes()).unwrap(); - let (output, mut command) = reader_thread.join().unwrap().unwrap(); - let exit_status = command.wait().unwrap(); - assert!(exit_status.success()); - output + let write_result = { stdin }.write_all(source.as_bytes()); + let join_result = reader_thread.join().unwrap(); + write_result?; + let (output, mut command) = join_result?; + let exit_status = command.wait()?; + if exit_status.success() { + Ok(output) + } else { + Err(io::Error::new( + io::ErrorKind::Other, + format!("exited with status {}", exit_status), + )) + } } fn main() {