From 115b453a178dae07fd1aeffa98a7e9f720a927da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 26 Jul 2019 15:46:23 +0200 Subject: [PATCH 1/2] Allow to override rustfmt path with an environment variable. Fixes #1601 --- src/lib.rs | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b669bcb26a..39fa3182e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,7 +92,7 @@ pub use codegen::EnumVariation; use std::borrow::Cow; use std::fs::{File, OpenOptions}; use std::io::{self, Write}; -use std::iter; +use std::{env, iter}; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; use std::sync::Arc; @@ -1200,7 +1200,7 @@ impl Builder { /// Generate the Rust bindings using the options built up thus far. pub fn generate(mut self) -> Result { // Add any extra arguments from the environment to the clang command line. - if let Some(extra_clang_args) = std::env::var("BINDGEN_EXTRA_CLANG_ARGS").ok() { + if let Some(extra_clang_args) = env::var("BINDGEN_EXTRA_CLANG_ARGS").ok() { // Try to parse it with shell quoting. If we fail, make it one single big argument. if let Some(strings) = shlex::split(&extra_clang_args) { self.options.clang_args.extend(strings); @@ -1899,6 +1899,21 @@ impl Bindings { Ok(()) } + /// Gets the rustfmt path to rustfmt the generated bindings. + fn rustfmt_path<'a>(&'a self) -> io::Result> { + debug_assert!(self.options.rustfmt_bindings); + if let Some(ref p) = self.options.rustfmt_path { + return Ok(Cow::Borrowed(p)); + } + if let Ok(rustfmt) = env::var("RUSTFMT") { + return Ok(Cow::Owned(rustfmt.into())); + } + match which::which("rustfmt") { + Ok(p) => Ok(Cow::Owned(p)), + Err(e) => Err(io::Error::new(io::ErrorKind::Other, format!("{}", e))), + } + } + /// Checks if rustfmt_bindings is set and runs rustfmt on the string fn rustfmt_generated_string<'a>( &self, @@ -1911,18 +1926,7 @@ impl Bindings { return Ok(Cow::Borrowed(source)); } - let rustfmt = match self.options.rustfmt_path { - Some(ref p) => Cow::Borrowed(p), - None => { - let path = which::which("rustfmt") - .map_err(|e| { - io::Error::new(io::ErrorKind::Other, format!("{}", e)) - })?; - - Cow::Owned(path) - } - }; - + let rustfmt = self.rustfmt_path()?; let mut cmd = Command::new(&*rustfmt); cmd From 0891848b09197d624ae3fb6f8938d833c926b5f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 26 Jul 2019 16:22:56 +0200 Subject: [PATCH 2/2] Fix rustfmt installation to be via rustup. --- ci/script.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/script.sh b/ci/script.sh index 8e2c7e1fa0..880896cd72 100755 --- a/ci/script.sh +++ b/ci/script.sh @@ -13,8 +13,8 @@ case "$BINDGEN_JOB" in "test") # Need rustfmt to compare the test expectations. rustup update nightly - rustup run nightly cargo install -f rustfmt-nightly - + rustup component add rustfmt + export RUSTFMT="$(rustup which rustfmt)" cargo test $BINDGEN_PROFILE --features "$BINDGEN_FEATURES" ./ci/assert-no-diff.sh ;;