Skip to content

Commit

Permalink
Filter out cfgs which should not be used during build
Browse files Browse the repository at this point in the history
Fixes #7933: Filter invalid CARGO_CFG_ in build scripts
  • Loading branch information
aleksator committed Feb 27, 2020
1 parent ab2b2c0 commit a6239da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl TargetInfo {

let cfg = lines
.map(|line| Ok(Cfg::from_str(line)?))
.filter(TargetInfo::not_user_specific_cfg)
.collect::<CargoResult<Vec<_>>>()
.chain_err(|| {
format!(
Expand Down Expand Up @@ -189,6 +190,15 @@ impl TargetInfo {
})
}

fn not_user_specific_cfg(cfg: &CargoResult<Cfg>) -> 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
Expand Down
20 changes: 20 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit a6239da

Please sign in to comment.