diff --git a/.travis.yml b/.travis.yml index d8bc595d79..36e10e1a91 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,20 +14,28 @@ env: - LLVM_VERSION="3.8" BINDGEN_JOB="test" BINDGEN_PROFILE="--release" - LLVM_VERSION="3.8" BINDGEN_JOB="integration" BINDGEN_PROFILE= - LLVM_VERSION="3.8" BINDGEN_JOB="integration" BINDGEN_PROFILE="--release" + - LLVM_VERSION="3.8" BINDGEN_JOB="nofeatures" BINDGEN_PROFILE= + - LLVM_VERSION="3.8" BINDGEN_JOB="nofeatures" BINDGEN_PROFILE="--release" - LLVM_VERSION="3.9" BINDGEN_JOB="test" BINDGEN_PROFILE= - LLVM_VERSION="3.9" BINDGEN_JOB="test" BINDGEN_PROFILE="--release" - LLVM_VERSION="3.9" BINDGEN_JOB="integration" BINDGEN_PROFILE= - LLVM_VERSION="3.9" BINDGEN_JOB="integration" BINDGEN_PROFILE="--release" + - LLVM_VERSION="3.9" BINDGEN_JOB="nofeatures" BINDGEN_PROFILE= + - LLVM_VERSION="3.9" BINDGEN_JOB="nofeatures" BINDGEN_PROFILE="--release" - LLVM_VERSION="4.0" BINDGEN_JOB="test" BINDGEN_PROFILE= - LLVM_VERSION="4.0" BINDGEN_JOB="test" BINDGEN_PROFILE="--release" - LLVM_VERSION="4.0" BINDGEN_JOB="integration" BINDGEN_PROFILE= - LLVM_VERSION="4.0" BINDGEN_JOB="integration" BINDGEN_PROFILE="--release" + - LLVM_VERSION="4.0" BINDGEN_JOB="nofeatures" BINDGEN_PROFILE= + - LLVM_VERSION="4.0" BINDGEN_JOB="nofeatures" BINDGEN_PROFILE="--release" - LLVM_VERSION="5.0" BINDGEN_JOB="test" BINDGEN_PROFILE= - LLVM_VERSION="5.0" BINDGEN_JOB="test" BINDGEN_PROFILE="--release" - LLVM_VERSION="5.0" BINDGEN_JOB="test" BINDGEN_PROFILE= BINDGEN_FEATURES="testing_only_extra_assertions" - LLVM_VERSION="5.0" BINDGEN_JOB="test" BINDGEN_PROFILE="--release" BINDGEN_FEATURES="testing_only_extra_assertions" - LLVM_VERSION="5.0" BINDGEN_JOB="integration" BINDGEN_PROFILE= - LLVM_VERSION="5.0" BINDGEN_JOB="integration" BINDGEN_PROFILE="--release" + - LLVM_VERSION="5.0" BINDGEN_JOB="nofeatures" BINDGEN_PROFILE= + - LLVM_VERSION="5.0" BINDGEN_JOB="nofeatures" BINDGEN_PROFILE="--release" - LLVM_VERSION="5.0" BINDGEN_JOB="expectations" BINDGEN_PROFILE= - LLVM_VERSION="5.0" BINDGEN_JOB="expectations" BINDGEN_PROFILE="--release" - LLVM_VERSION="5.0" BINDGEN_JOB="misc" diff --git a/Cargo.toml b/Cargo.toml index 2f98a0ea99..232d9c3596 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,7 +54,7 @@ lazy_static = "1" peeking_take_while = "0.1.2" quote = { version = "1", default-features = false } regex = "1.0" -which = ">=1.0, <3.0" +which = { version = ">=1.0, <3.0", optional = true } shlex = "0.1" fxhash = "0.2" # New validation in 0.3.6 breaks bindgen-integration: @@ -70,9 +70,11 @@ optional = true version = "0.4" [features] -default = ["logging", "clap"] +default = ["logging", "clap", "which-rustfmt"] logging = ["env_logger", "log"] static = [] +# Dynamically discover a `rustfmt` binary using the `which` crate +which-rustfmt = ["which"] # These features only exist for CI testing -- don't use them if you're not hacking # on bindgen! diff --git a/ci/script.sh b/ci/script.sh index 880896cd72..7003618a56 100755 --- a/ci/script.sh +++ b/ci/script.sh @@ -14,19 +14,31 @@ case "$BINDGEN_JOB" in # Need rustfmt to compare the test expectations. rustup update nightly rustup component add rustfmt - export RUSTFMT="$(rustup which rustfmt)" - cargo test $BINDGEN_PROFILE --features "$BINDGEN_FEATURES" + rustup component add --toolchain nightly rustfmt + RUSTFMT="$(rustup which rustfmt)" + export RUSTFMT + cargo test "$BINDGEN_PROFILE" --features "$BINDGEN_FEATURES" ./ci/assert-no-diff.sh ;; "integration") cd ./bindgen-integration - cargo test $BINDGEN_PROFILE --features "$BINDGEN_FEATURES" + cargo test "$BINDGEN_PROFILE" --features "$BINDGEN_FEATURES" + ;; + + "nofeatures") + rustup update nightly + rustup component add rustfmt + rustup component add --toolchain nightly rustfmt + RUSTFMT="$(rustup which rustfmt)" + export RUSTFMT + cargo test "$BINDGEN_PROFILE" --no-default-features + ./ci/assert-no-diff.sh ;; "expectations") cd ./tests/expectations - cargo test $BINDGEN_PROFILE + cargo test "$BINDGEN_PROFILE" ;; "misc") @@ -43,6 +55,7 @@ case "$BINDGEN_JOB" in # TODO: Actually run quickchecks once `bindgen` is reliable enough. cargo test ;; + *) echo "Error! Unknown \$BINDGEN_JOB: '$BINDGEN_JOB'" exit 1 diff --git a/src/lib.rs b/src/lib.rs index b59c9271c7..722e53e9e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,6 +32,7 @@ extern crate quote; extern crate proc_macro2; extern crate regex; extern crate shlex; +#[cfg(feature = "which-rustfmt")] extern crate which; #[cfg(feature = "logging")] @@ -1908,10 +1909,13 @@ impl Bindings { if let Ok(rustfmt) = env::var("RUSTFMT") { return Ok(Cow::Owned(rustfmt.into())); } + #[cfg(feature = "which-rustfmt")] match which::which("rustfmt") { Ok(p) => Ok(Cow::Owned(p)), Err(e) => Err(io::Error::new(io::ErrorKind::Other, format!("{}", e))), } + #[cfg(not(feature = "which-rustfmt"))] + Err(io::Error::new(io::ErrorKind::Other, "which wasn't enabled, and no rustfmt binary specified")) } /// Checks if rustfmt_bindings is set and runs rustfmt on the string