Skip to content
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
7 changes: 6 additions & 1 deletion src/cargo/core/compiler/build_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,12 @@ fn env_args(
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_string);
return Ok(args.collect());

let rustflags: Vec<_> = args.collect();

if rustflags.len() > 0 {
return Ok(rustflags);
}
}

let mut rustflags = Vec::new();
Expand Down
119 changes: 119 additions & 0 deletions tests/testsuite/rustflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,3 +1359,122 @@ fn env_rustflags_misspelled_build_script() {
.with_stderr_contains("[WARNING] Cargo does not read `RUST_FLAGS` environment variable. Did you mean `RUSTFLAGS`?")
.run();
}

#[test]
fn env_rustflags_with_crt_static_sets_env_var() {
// The CARGO_CFG_TARGET_FEATURE env var should be set when passing this flag.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
build = "build.rs"
"#,
)
.file("src/lib.rs", "")
.file(
"build.rs",
r#"
fn main() {
// The crt-static feature should be enabled.
#[cfg(not(target_feature = "crt-static"))]
assert!(false);

assert!(std::env::var("CARGO_CFG_TARGET_FEATURE").unwrap().contains("crt-static"));
}
"#,
)
.build();

p.cargo("build")
.env("RUSTFLAGS", "-Ctarget-feature=+crt-static")
.run();
}

#[test]
fn config_rustflags_with_crt_static_sets_env_var() {
// The CARGO_CFG_TARGET_FEATURE env var should be set when passing this flag using a config file.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
build = "build.rs"
"#,
)
.file(
".cargo/config",
r#"
[build]
rustflags = [
"-Ctarget-feature=+crt-static",
]
"#,
)
.file("src/lib.rs", "")
.file(
"build.rs",
r#"
fn main() {
// The crt-static feature should be enabled.
#[cfg(not(target_feature = "crt-static"))]
assert!(false);

assert!(std::env::var("CARGO_CFG_TARGET_FEATURE").unwrap().contains("crt-static"));
}
"#,
)
.build();

p.cargo("build")
.env("RUSTFLAGS", "")
.run();
}

#[test]
fn config_rustflags_with_target_filter_with_crt_static_sets_env_var() {
// The CARGO_CFG_TARGET_FEATURE env var should be set when passing this flag using a config file.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2018"
build = "build.rs"
"#,
)
.file(
".cargo/config",
r#"
[target.'cfg(target_arch="x86_64")']
rustflags = [
"-Ctarget-feature=+crt-static",
]
"#,
)
.file(
"src/main.rs",
r#"fn main() {}"#,
)
.file(
"build.rs",
r#"use std::env;
fn main() {
// The crt-static feature should be enabled.
#[cfg(not(target_feature = "crt-static"))]
assert!(false);

assert!(std::env::var("CARGO_CFG_TARGET_FEATURE").unwrap().contains("crt-static"));
}
"#,
)
.build();

p.cargo("build").run();
}