Skip to content

Commit 37fa476

Browse files
authored
Allow to override rustfmt path with an environment variable. (#1602)
Also fix rustfmt installation to be via rustup, since rustfmt-nightly doesn't compile in nightly rust. Fixes #1601
2 parents a3d8cf7 + 0891848 commit 37fa476

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

ci/script.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ case "$BINDGEN_JOB" in
1313
"test")
1414
# Need rustfmt to compare the test expectations.
1515
rustup update nightly
16-
rustup run nightly cargo install -f rustfmt-nightly
17-
16+
rustup component add rustfmt
17+
export RUSTFMT="$(rustup which rustfmt)"
1818
cargo test $BINDGEN_PROFILE --features "$BINDGEN_FEATURES"
1919
./ci/assert-no-diff.sh
2020
;;

src/lib.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub use codegen::EnumVariation;
9292
use std::borrow::Cow;
9393
use std::fs::{File, OpenOptions};
9494
use std::io::{self, Write};
95-
use std::iter;
95+
use std::{env, iter};
9696
use std::path::{Path, PathBuf};
9797
use std::process::{Command, Stdio};
9898
use std::sync::Arc;
@@ -1200,7 +1200,7 @@ impl Builder {
12001200
/// Generate the Rust bindings using the options built up thus far.
12011201
pub fn generate(mut self) -> Result<Bindings, ()> {
12021202
// Add any extra arguments from the environment to the clang command line.
1203-
if let Some(extra_clang_args) = std::env::var("BINDGEN_EXTRA_CLANG_ARGS").ok() {
1203+
if let Some(extra_clang_args) = env::var("BINDGEN_EXTRA_CLANG_ARGS").ok() {
12041204
// Try to parse it with shell quoting. If we fail, make it one single big argument.
12051205
if let Some(strings) = shlex::split(&extra_clang_args) {
12061206
self.options.clang_args.extend(strings);
@@ -1899,6 +1899,21 @@ impl Bindings {
18991899
Ok(())
19001900
}
19011901

1902+
/// Gets the rustfmt path to rustfmt the generated bindings.
1903+
fn rustfmt_path<'a>(&'a self) -> io::Result<Cow<'a, PathBuf>> {
1904+
debug_assert!(self.options.rustfmt_bindings);
1905+
if let Some(ref p) = self.options.rustfmt_path {
1906+
return Ok(Cow::Borrowed(p));
1907+
}
1908+
if let Ok(rustfmt) = env::var("RUSTFMT") {
1909+
return Ok(Cow::Owned(rustfmt.into()));
1910+
}
1911+
match which::which("rustfmt") {
1912+
Ok(p) => Ok(Cow::Owned(p)),
1913+
Err(e) => Err(io::Error::new(io::ErrorKind::Other, format!("{}", e))),
1914+
}
1915+
}
1916+
19021917
/// Checks if rustfmt_bindings is set and runs rustfmt on the string
19031918
fn rustfmt_generated_string<'a>(
19041919
&self,
@@ -1911,18 +1926,7 @@ impl Bindings {
19111926
return Ok(Cow::Borrowed(source));
19121927
}
19131928

1914-
let rustfmt = match self.options.rustfmt_path {
1915-
Some(ref p) => Cow::Borrowed(p),
1916-
None => {
1917-
let path = which::which("rustfmt")
1918-
.map_err(|e| {
1919-
io::Error::new(io::ErrorKind::Other, format!("{}", e))
1920-
})?;
1921-
1922-
Cow::Owned(path)
1923-
}
1924-
};
1925-
1929+
let rustfmt = self.rustfmt_path()?;
19261930
let mut cmd = Command::new(&*rustfmt);
19271931

19281932
cmd

0 commit comments

Comments
 (0)