diff --git a/src/cargo/core/compiler/build_context/mod.rs b/src/cargo/core/compiler/build_context/mod.rs index 789acc8a3ed..274ed04f3cd 100644 --- a/src/cargo/core/compiler/build_context/mod.rs +++ b/src/cargo/core/compiler/build_context/mod.rs @@ -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(); diff --git a/tests/testsuite/rustflags.rs b/tests/testsuite/rustflags.rs index da5b5eb78c2..a83ace14488 100644 --- a/tests/testsuite/rustflags.rs +++ b/tests/testsuite/rustflags.rs @@ -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(); +} \ No newline at end of file