diff --git a/src/lib.rs b/src/lib.rs index d8c9b6e..482efef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,15 +146,26 @@ impl AutoCfg { return Err(error::from_str("output path is not a writable directory")); } - // Recover the RUSTFLAGS - let rustflags = std::env::var("RUSTFLAGS").ok().map(|rustflags| { - rustflags - .split(' ') - .map(str::trim) - .filter(|s| !s.is_empty()) - .map(str::to_string) - .collect::>() - }); + // Cargo only applies RUSTFLAGS for building TARGET artifact in + // cross-compilation environment. Sadly, we don't have a way to detect + // when we're building HOST artifact in a cross-compilation environment, + // so for now we only apply RUSTFLAGS when cross-compiling an artifact. + // + // See https://github.com/cuviper/autocfg/pull/10#issuecomment-527575030. + let rustflags = env::var("RUSTFLAGS") + .ok() + .filter(|_| env::var_os("TARGET") != env::var_os("HOST")) + .map(|rustflags| { + // This is meant to match how cargo handles the RUSTFLAG environment + // variable. + // See https://github.com/rust-lang/cargo/blob/69aea5b6f69add7c51cca939a79644080c0b0ba0/src/cargo/core/compiler/build_context/target_info.rs#L434-L441 + rustflags + .split(' ') + .map(str::trim) + .filter(|s| !s.is_empty()) + .map(str::to_string) + .collect::>() + }); let mut ac = AutoCfg { out_dir: dir, diff --git a/tests/rustflags.rs b/tests/rustflags.rs index ff971af..bfa0f03 100644 --- a/tests/rustflags.rs +++ b/tests/rustflags.rs @@ -6,6 +6,8 @@ extern crate autocfg; fn test_with_sysroot() { std::env::set_var("RUSTFLAGS", "-L target/debug/deps -L target/debug"); std::env::set_var("OUT_DIR", "target"); + // Ensure HOST != TARGET. + std::env::set_var("HOST", "lol"); let ac = autocfg::AutoCfg::new().unwrap(); assert!(ac.probe_sysroot_crate("autocfg")); }