Skip to content

Commit 7d75776

Browse files
committed
Auto merge of #4950 - alexcrichton:rustflags-orderd, r=matklad
Ensure `[target]` rustflags are deterministically passed The usage of `HashMap` in the `Config` tables introduced some nondeterminism, so reverse that with a sort right before we pass it down to rustc. One day we'll probably want to sort by the position where these keys were defined, but for now a blanket sort should do the trick. Closes #4935
2 parents c7ce876 + c9803a4 commit 7d75776

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,16 @@ fn env_args(config: &Config,
12211221
None
12221222
}
12231223
});
1224+
1225+
// Note that we may have multiple matching `[target]` sections and
1226+
// because we're passing flags to the compiler this can affect
1227+
// cargo's caching and whether it rebuilds. Ensure a deterministic
1228+
// ordering through sorting for now. We may perhaps one day wish to
1229+
// ensure a deterministic ordering via the order keys were defined
1230+
// in files perhaps.
1231+
let mut cfgs = cfgs.collect::<Vec<_>>();
1232+
cfgs.sort();
1233+
12241234
for n in cfgs {
12251235
let key = format!("target.{}.{}", n, name);
12261236
if let Some(args) = config.get_list_or_split_string(&key)? {

tests/rustflags.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -974,21 +974,21 @@ fn cfg_rustflags_normal_source() {
974974
[RUNNING] `rustc [..] --cfg bar[..]`
975975
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
976976
"));
977-
977+
978978
assert_that(p.cargo("build").arg("--bin=a").arg("-v"),
979979
execs().with_status(0).with_stderr("\
980980
[COMPILING] foo v0.0.1 ([..])
981981
[RUNNING] `rustc [..] --cfg bar[..]`
982982
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
983983
"));
984-
984+
985985
assert_that(p.cargo("build").arg("--example=b").arg("-v"),
986986
execs().with_status(0).with_stderr("\
987987
[COMPILING] foo v0.0.1 ([..])
988988
[RUNNING] `rustc [..] --cfg bar[..]`
989989
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
990990
"));
991-
991+
992992
assert_that(p.cargo("test").arg("--no-run").arg("-v"),
993993
execs().with_status(0).with_stderr("\
994994
[COMPILING] foo v0.0.1 ([..])
@@ -997,7 +997,7 @@ fn cfg_rustflags_normal_source() {
997997
[RUNNING] `rustc [..] --cfg bar[..]`
998998
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
999999
"));
1000-
1000+
10011001
assert_that(p.cargo("bench").arg("--no-run").arg("-v"),
10021002
execs().with_status(0).with_stderr("\
10031003
[COMPILING] foo v0.0.1 ([..])
@@ -1006,7 +1006,7 @@ fn cfg_rustflags_normal_source() {
10061006
[RUNNING] `rustc [..] --cfg bar[..]`
10071007
[FINISHED] release [optimized] target(s) in [..]
10081008
"));
1009-
1009+
10101010
}
10111011

10121012
// target.'cfg(...)'.rustflags takes precedence over build.rustflags
@@ -1069,7 +1069,7 @@ fn cfg_rustflags_precedence() {
10691069
[RUNNING] `rustc [..] --cfg bar[..]`
10701070
[FINISHED] release [optimized] target(s) in [..]
10711071
"));
1072-
1072+
10731073
}
10741074

10751075
#[test]
@@ -1157,5 +1157,42 @@ fn target_rustflags_string_and_array_form2() {
11571157
[RUNNING] `rustc [..] --cfg foo[..]`
11581158
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
11591159
"));
1160+
}
1161+
1162+
#[test]
1163+
fn two_matching_in_config() {
1164+
let p1 = project("foo")
1165+
.file("Cargo.toml", r#"
1166+
[package]
1167+
name = "foo"
1168+
version = "0.0.1"
1169+
"#)
1170+
.file(".cargo/config", r#"
1171+
[target.'cfg(unix)']
1172+
rustflags = ["--cfg", 'foo="a"']
1173+
[target.'cfg(windows)']
1174+
rustflags = ["--cfg", 'foo="a"']
1175+
[target.'cfg(target_pointer_width = "32")']
1176+
rustflags = ["--cfg", 'foo="b"']
1177+
[target.'cfg(target_pointer_width = "64")']
1178+
rustflags = ["--cfg", 'foo="b"']
1179+
"#)
1180+
.file("src/main.rs", r#"
1181+
fn main() {
1182+
if cfg!(foo = "a") {
1183+
println!("a");
1184+
} else if cfg!(foo = "b") {
1185+
println!("b");
1186+
} else {
1187+
panic!()
1188+
}
1189+
}
1190+
"#)
1191+
.build();
11601192

1193+
assert_that(p1.cargo("run"), execs().with_status(0));
1194+
assert_that(p1.cargo("build"),
1195+
execs().with_status(0).with_stderr("\
1196+
[FINISHED] [..]
1197+
"));
11611198
}

0 commit comments

Comments
 (0)