Skip to content

Commit

Permalink
Only apply RUSTFLAGS when TARGET != HOST
Browse files Browse the repository at this point in the history
  • Loading branch information
roblabla committed Sep 6, 2019
1 parent fb54551 commit 9142612
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/lib.rs
Expand Up @@ -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::<Vec<String>>()
});
// 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::<Vec<String>>()
});

let mut ac = AutoCfg {
out_dir: dir,
Expand Down
2 changes: 2 additions & 0 deletions tests/rustflags.rs
Expand Up @@ -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"));
}

0 comments on commit 9142612

Please sign in to comment.