Skip to content

Commit 5a5fe12

Browse files
committed
Auto merge of #4279 - tee-too:fix-3499, r=alexcrichton
Fix support of `[target.'cfg(...)']` syntax for rustc and rustdoc flags Support of `[target.'cfg(...)']` for rustc and rustdoc flags is buggy. This adds meaningful tests and fixes the issue. Fixes #3499
2 parents f709c35 + 828247a commit 5a5fe12

File tree

2 files changed

+104
-48
lines changed

2 files changed

+104
-48
lines changed

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,11 +1049,18 @@ fn env_args(config: &Config,
10491049
// ...including target.'cfg(...)'.rustflags
10501050
if let Some(ref target_cfg) = target_info.cfg {
10511051
if let Some(table) = config.get_table("target")? {
1052-
let cfgs = table.val.iter().map(|(t, _)| (CfgExpr::from_str(t), t))
1053-
.filter_map(|(c, n)| c.map(|c| (c, n)).ok())
1054-
.filter(|&(ref c, _)| c.matches(target_cfg));
1055-
for (_, n) in cfgs {
1056-
let key = format!("target.'{}'.{}", n, name);
1052+
let cfgs = table.val.keys().filter_map(|t| {
1053+
if t.starts_with("cfg(") && t.ends_with(")") {
1054+
let cfg = &t[4..t.len() - 1];
1055+
CfgExpr::from_str(cfg)
1056+
.ok()
1057+
.and_then(|c| if c.matches(target_cfg) { Some(t) } else { None })
1058+
} else {
1059+
None
1060+
}
1061+
});
1062+
for n in cfgs {
1063+
let key = format!("target.{}.{}", n, name);
10571064
if let Some(args) = config.get_list_or_split_string(&key)? {
10581065
let args = args.val.into_iter();
10591066
rustflags.extend(args);

tests/rustflags.rs

Lines changed: 92 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -958,33 +958,55 @@ fn cfg_rustflags_normal_source() {
958958
name = "foo"
959959
version = "0.0.1"
960960
"#)
961-
.file("src/lib.rs", "")
961+
.file("src/lib.rs", "pub fn t() {}")
962962
.file("src/bin/a.rs", "fn main() {}")
963963
.file("examples/b.rs", "fn main() {}")
964964
.file("tests/c.rs", "#[test] fn f() { }")
965-
.file("benches/d.rs", r#"
966-
#![feature(test)]
967-
extern crate test;
968-
#[bench] fn run1(_ben: &mut test::Bencher) { }"#)
969-
.file(".cargo/config", "
970-
[target.'cfg(feature=\"feat\")']
971-
rustflags = [\"-Z\", \"bogus\"]
972-
");
965+
.file(".cargo/config", &format!(r#"
966+
[target.'cfg({})']
967+
rustflags = ["--cfg", "bar"]
968+
"#, if rustc_host().contains("-windows-") {"windows"} else {"not(windows)"}));
973969
p.build();
974-
975-
assert_that(p.cargo("build").arg("--features").arg("\"feat\"")
976-
.arg("--lib"),
977-
execs().with_status(101));
978-
assert_that(p.cargo("build").arg("--features").arg("\"feat\"")
979-
.arg("--bin=a"),
980-
execs().with_status(101));
981-
assert_that(p.cargo("build").arg("--features").arg("\"feat\"")
982-
.arg("--example=b"),
983-
execs().with_status(101));
984-
assert_that(p.cargo("test").arg("--features").arg("\"feat\""),
985-
execs().with_status(101));
986-
assert_that(p.cargo("bench").arg("--features").arg("\"feat\""),
987-
execs().with_status(101));
970+
971+
assert_that(p.cargo("build").arg("--lib").arg("-v"),
972+
execs().with_status(0).with_stderr("\
973+
[COMPILING] foo v0.0.1 ([..])
974+
[RUNNING] `rustc [..] --cfg bar[..]`
975+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
976+
"));
977+
978+
assert_that(p.cargo("build").arg("--bin=a").arg("-v"),
979+
execs().with_status(0).with_stderr("\
980+
[COMPILING] foo v0.0.1 ([..])
981+
[RUNNING] `rustc [..] --cfg bar[..]`
982+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
983+
"));
984+
985+
assert_that(p.cargo("build").arg("--example=b").arg("-v"),
986+
execs().with_status(0).with_stderr("\
987+
[COMPILING] foo v0.0.1 ([..])
988+
[RUNNING] `rustc [..] --cfg bar[..]`
989+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
990+
"));
991+
992+
assert_that(p.cargo("test").arg("--no-run").arg("-v"),
993+
execs().with_status(0).with_stderr("\
994+
[COMPILING] foo v0.0.1 ([..])
995+
[RUNNING] `rustc [..] --cfg bar[..]`
996+
[RUNNING] `rustc [..] --cfg bar[..]`
997+
[RUNNING] `rustc [..] --cfg bar[..]`
998+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
999+
"));
1000+
1001+
assert_that(p.cargo("bench").arg("--no-run").arg("-v"),
1002+
execs().with_status(0).with_stderr("\
1003+
[COMPILING] foo v0.0.1 ([..])
1004+
[RUNNING] `rustc [..] --cfg bar[..]`
1005+
[RUNNING] `rustc [..] --cfg bar[..]`
1006+
[RUNNING] `rustc [..] --cfg bar[..]`
1007+
[FINISHED] release [optimized] target(s) in [..]
1008+
"));
1009+
9881010
}
9891011

9901012
// target.'cfg(...)'.rustflags takes precedence over build.rustflags
@@ -996,32 +1018,59 @@ fn cfg_rustflags_precedence() {
9961018
name = "foo"
9971019
version = "0.0.1"
9981020
"#)
999-
.file("src/lib.rs", "")
1000-
.file(".cargo/config", "
1021+
.file("src/lib.rs", "pub fn t() {}")
1022+
.file("src/bin/a.rs", "fn main() {}")
1023+
.file("examples/b.rs", "fn main() {}")
1024+
.file("tests/c.rs", "#[test] fn f() { }")
1025+
.file(".cargo/config", &format!(r#"
10011026
[build]
1002-
rustflags = [\"--cfg\", \"foo\"]
1027+
rustflags = ["--cfg", "foo"]
10031028
1004-
[target.'cfg(feature = \"feat\"')]
1005-
rustflags = [\"-Z\", \"bogus\"]
1006-
");
1029+
[target.'cfg({})']
1030+
rustflags = ["--cfg", "bar"]
1031+
"#, if rustc_host().contains("-windows-") { "windows" } else { "not(windows)" }));
10071032
p.build();
10081033

1009-
assert_that(p.cargo("build").arg("--features").arg("\"feat\"")
1010-
.arg("--lib"),
1011-
execs().with_status(101));
1012-
assert_that(p.cargo("build").arg("--features").arg("\"feat\"")
1013-
.arg("--bin=a"),
1014-
execs().with_status(101));
1015-
assert_that(p.cargo("build").arg("--features").arg("\"feat\"")
1016-
.arg("--example=b"),
1017-
execs().with_status(101));
1018-
assert_that(p.cargo("test").arg("--features").arg("\"feat\""),
1019-
execs().with_status(101));
1020-
assert_that(p.cargo("bench").arg("--features").arg("\"feat\""),
1021-
execs().with_status(101));
1022-
}
1034+
assert_that(p.cargo("build").arg("--lib").arg("-v"),
1035+
execs().with_status(0).with_stderr("\
1036+
[COMPILING] foo v0.0.1 ([..])
1037+
[RUNNING] `rustc [..] --cfg bar[..]`
1038+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1039+
"));
1040+
1041+
assert_that(p.cargo("build").arg("--bin=a").arg("-v"),
1042+
execs().with_status(0).with_stderr("\
1043+
[COMPILING] foo v0.0.1 ([..])
1044+
[RUNNING] `rustc [..] --cfg bar[..]`
1045+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1046+
"));
1047+
1048+
assert_that(p.cargo("build").arg("--example=b").arg("-v"),
1049+
execs().with_status(0).with_stderr("\
1050+
[COMPILING] foo v0.0.1 ([..])
1051+
[RUNNING] `rustc [..] --cfg bar[..]`
1052+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1053+
"));
10231054

1055+
assert_that(p.cargo("test").arg("--no-run").arg("-v"),
1056+
execs().with_status(0).with_stderr("\
1057+
[COMPILING] foo v0.0.1 ([..])
1058+
[RUNNING] `rustc [..] --cfg bar[..]`
1059+
[RUNNING] `rustc [..] --cfg bar[..]`
1060+
[RUNNING] `rustc [..] --cfg bar[..]`
1061+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1062+
"));
10241063

1064+
assert_that(p.cargo("bench").arg("--no-run").arg("-v"),
1065+
execs().with_status(0).with_stderr("\
1066+
[COMPILING] foo v0.0.1 ([..])
1067+
[RUNNING] `rustc [..] --cfg bar[..]`
1068+
[RUNNING] `rustc [..] --cfg bar[..]`
1069+
[RUNNING] `rustc [..] --cfg bar[..]`
1070+
[FINISHED] release [optimized] target(s) in [..]
1071+
"));
1072+
1073+
}
10251074

10261075
#[test]
10271076
fn target_rustflags_string_and_array_form1() {

0 commit comments

Comments
 (0)