From a6239da8b2cf6f1d7626b9e72a162b649eb9ef7c Mon Sep 17 00:00:00 2001 From: Alex Tokarev Date: Tue, 25 Feb 2020 21:28:36 +0300 Subject: [PATCH] Filter out cfgs which should not be used during build Fixes #7933: Filter invalid CARGO_CFG_ in build scripts --- .../compiler/build_context/target_info.rs | 10 ++++++++++ tests/testsuite/build.rs | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 4bcf2b229d0..2f7cafedaea 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -153,6 +153,7 @@ impl TargetInfo { let cfg = lines .map(|line| Ok(Cfg::from_str(line)?)) + .filter(TargetInfo::not_user_specific_cfg) .collect::>>() .chain_err(|| { format!( @@ -189,6 +190,15 @@ impl TargetInfo { }) } + fn not_user_specific_cfg(cfg: &CargoResult) -> bool { + if let Ok(Cfg::Name(cfg_name)) = cfg { + if cfg_name == "debug_assertions" || cfg_name == "proc_macro" { + return false; + } + } + true + } + /// All the target `cfg` settings. pub fn cfg(&self) -> &[Cfg] { &self.cfg diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 50b49ea7ea4..5558360b5e8 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -4739,3 +4739,23 @@ fn build_with_relative_cargo_home_path() { p.cargo("build").env("CARGO_HOME", "./cargo_home/").run(); } + +#[cargo_test] +fn user_specific_cfgs_are_filtered_out() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/main.rs", r#"fn main() {}"#) + .file( + "build.rs", + r#" + fn main() { + assert!(std::env::var_os("CARGO_CFG_PROC_MACRO").is_none()); + assert!(std::env::var_os("CARGO_CFG_DEBUG_ASSERTIONS").is_none()); + }"#, + ) + .build(); + + p.cargo("rustc -- --cfg debug_assertions --cfg proc_macro") + .run(); + p.process(&p.bin("foo")).run(); +}