Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check CARGO_ENCODED_RUSTFLAGS of cargo 1.55+, prefer over RUSTFLAGS #38

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,20 @@ impl AutoCfg {
let rustflags = if target != env::var_os("HOST")
|| dir_contains_target(&target, &dir, env::var_os("CARGO_TARGET_DIR"))
{
env::var("RUSTFLAGS").ok().map(|rustflags| {
// This is meant to match how cargo handles the RUSTFLAG environment
let (maybe_flags, sep) = if let Ok(flags) = env::var("CARGO_ENCODED_RUSTFLAGS") {
// For cargo >= 1.55
(Some(flags), '\x1f')
} else {
// For cargo < 1.55
(env::var("RUSTFLAGS").ok(), ' ')
};

maybe_flags.map(|flags| {
// This is meant to match how cargo handles the RUSTFLAGS environment
// variable.
// See https://github.com/rust-lang/cargo/blob/69aea5b6f69add7c51cca939a79644080c0b0ba0/src/cargo/core/compiler/build_context/target_info.rs#L434-L441
rustflags
.split(' ')
flags
.split(sep)
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_string)
Expand Down
15 changes: 13 additions & 2 deletions tests/rustflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ use std::env;
/// Tests that autocfg uses the RUSTFLAGS environment variable when running
/// rustc.
#[test]
fn test_with_sysroot() {
fn test_with_sysroot_rustflags() {
test_with_sysroot("RUSTFLAGS")
}

/// Tests that autocfg uses the CARGO_ENCODED_RUSTFLAGS environment
/// variable when running rustc.
#[test]
fn test_with_sysroot_cargo_encoded_rustflags() {
test_with_sysroot("CARGO_ENCODED_RUSTFLAGS")
}

fn test_with_sysroot(var_name: &str) {
// Use the same path as this test binary.
let dir = env::current_exe().unwrap().parent().unwrap().to_path_buf();
env::set_var("RUSTFLAGS", &format!("-L {}", dir.display()));
env::set_var(var_name, &format!("-L {}", dir.display()));
env::set_var("OUT_DIR", &format!("{}", dir.display()));

// Ensure HOST != TARGET.
Expand Down