diff --git a/cranelift/codegen/build.rs b/cranelift/codegen/build.rs index 5932030fc3..e39feed8af 100644 --- a/cranelift/codegen/build.rs +++ b/cranelift/codegen/build.rs @@ -218,9 +218,44 @@ fn rebuild_isle(crate_dir: &std::path::Path) -> Result<(), Box std::io::Result { + use std::io::Write; + + let mut rustfmt = std::process::Command::new("rustfmt") + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .spawn()?; + + let mut stdin = rustfmt.stdin.take().unwrap(); + stdin.write_all(code.as_bytes())?; + drop(stdin); + + let mut stdout = rustfmt.stdout.take().unwrap(); + let mut data = vec![]; + stdout.read_to_end(&mut data)?; + + let status = rustfmt.wait()?; + if !status.success() { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + format!("`rustfmt` exited with status {}", status), + )); + } + + Ok(String::from_utf8(data).expect("rustfmt always writs utf-8 to stdout")) + } }