From 6bf75c482d9359a505a63b6d154d3baf92761fb9 Mon Sep 17 00:00:00 2001 From: Ohad Ravid Date: Fri, 8 Mar 2019 17:42:18 +0100 Subject: [PATCH 1/3] Added failing test --- tests/testsuite/rustflags.rs | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/tests/testsuite/rustflags.rs b/tests/testsuite/rustflags.rs index da5b5eb78c2..d04d677962a 100644 --- a/tests/testsuite/rustflags.rs +++ b/tests/testsuite/rustflags.rs @@ -1359,3 +1359,68 @@ 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 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() { + 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 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( + ".cargo/config", + r#" + [build] + rustflags = ["-Ctarget-feature=+crt-static"] + "#, + ) + .file("src/lib.rs", "") + .file( + "build.rs", + r#" + fn main() { + assert!(std::env::var("CARGO_CFG_TARGET_FEATURE").unwrap().contains("crt-static")); + } + "#, + ) + .build(); + + p.cargo("build") + .env("RUSTFLAGS", "") + .run(); +} From 438b2de5d4aac8ed8f8ff9a9de7e2044a599e8e1 Mon Sep 17 00:00:00 2001 From: Ohad Ravid Date: Fri, 8 Mar 2019 18:43:52 +0100 Subject: [PATCH 2/3] Only use the environment `RUSTFLAGS` in `env_args` if it's not empty --- src/cargo/core/compiler/build_context/mod.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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(); From d6942804e524d1419c99a619f4754149ecea7fdb Mon Sep 17 00:00:00 2001 From: Ohad Ravid Date: Wed, 20 Mar 2019 11:49:17 +0100 Subject: [PATCH 3/3] Added a test for using rust flags under target.cfg --- tests/testsuite/rustflags.rs | 60 ++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/tests/testsuite/rustflags.rs b/tests/testsuite/rustflags.rs index d04d677962a..a83ace14488 100644 --- a/tests/testsuite/rustflags.rs +++ b/tests/testsuite/rustflags.rs @@ -1362,7 +1362,7 @@ fn env_rustflags_misspelled_build_script() { #[test] fn env_rustflags_with_crt_static_sets_env_var() { - // The CARGO_CFG_TARGET_FEATURE should be set when passing this flag. + // The CARGO_CFG_TARGET_FEATURE env var should be set when passing this flag. let p = project() .file( "Cargo.toml", @@ -1378,6 +1378,10 @@ fn env_rustflags_with_crt_static_sets_env_var() { "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")); } "#, @@ -1391,7 +1395,7 @@ fn env_rustflags_with_crt_static_sets_env_var() { #[test] fn config_rustflags_with_crt_static_sets_env_var() { - // The CARGO_CFG_TARGET_FEATURE should be set when passing this flag. + // The CARGO_CFG_TARGET_FEATURE env var should be set when passing this flag using a config file. let p = project() .file( "Cargo.toml", @@ -1406,7 +1410,9 @@ fn config_rustflags_with_crt_static_sets_env_var() { ".cargo/config", r#" [build] - rustflags = ["-Ctarget-feature=+crt-static"] + rustflags = [ + "-Ctarget-feature=+crt-static", + ] "#, ) .file("src/lib.rs", "") @@ -1414,6 +1420,10 @@ fn config_rustflags_with_crt_static_sets_env_var() { "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")); } "#, @@ -1424,3 +1434,47 @@ fn config_rustflags_with_crt_static_sets_env_var() { .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