Skip to content

Commit

Permalink
Auto merge of #4950 - alexcrichton:rustflags-orderd, r=matklad
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jan 17, 2018
2 parents c7ce876 + c9803a4 commit 7d75776
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/cargo/ops/cargo_rustc/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,16 @@ fn env_args(config: &Config,
None
}
});

// Note that we may have multiple matching `[target]` sections and
// because we're passing flags to the compiler this can affect
// cargo's caching and whether it rebuilds. Ensure a deterministic
// ordering through sorting for now. We may perhaps one day wish to
// ensure a deterministic ordering via the order keys were defined
// in files perhaps.
let mut cfgs = cfgs.collect::<Vec<_>>();
cfgs.sort();

for n in cfgs {
let key = format!("target.{}.{}", n, name);
if let Some(args) = config.get_list_or_split_string(&key)? {
Expand Down
49 changes: 43 additions & 6 deletions tests/rustflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,21 +974,21 @@ fn cfg_rustflags_normal_source() {
[RUNNING] `rustc [..] --cfg bar[..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
"));

assert_that(p.cargo("build").arg("--bin=a").arg("-v"),
execs().with_status(0).with_stderr("\
[COMPILING] foo v0.0.1 ([..])
[RUNNING] `rustc [..] --cfg bar[..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
"));

assert_that(p.cargo("build").arg("--example=b").arg("-v"),
execs().with_status(0).with_stderr("\
[COMPILING] foo v0.0.1 ([..])
[RUNNING] `rustc [..] --cfg bar[..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
"));

assert_that(p.cargo("test").arg("--no-run").arg("-v"),
execs().with_status(0).with_stderr("\
[COMPILING] foo v0.0.1 ([..])
Expand All @@ -997,7 +997,7 @@ fn cfg_rustflags_normal_source() {
[RUNNING] `rustc [..] --cfg bar[..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
"));

assert_that(p.cargo("bench").arg("--no-run").arg("-v"),
execs().with_status(0).with_stderr("\
[COMPILING] foo v0.0.1 ([..])
Expand All @@ -1006,7 +1006,7 @@ fn cfg_rustflags_normal_source() {
[RUNNING] `rustc [..] --cfg bar[..]`
[FINISHED] release [optimized] target(s) in [..]
"));

}

// target.'cfg(...)'.rustflags takes precedence over build.rustflags
Expand Down Expand Up @@ -1069,7 +1069,7 @@ fn cfg_rustflags_precedence() {
[RUNNING] `rustc [..] --cfg bar[..]`
[FINISHED] release [optimized] target(s) in [..]
"));

}

#[test]
Expand Down Expand Up @@ -1157,5 +1157,42 @@ fn target_rustflags_string_and_array_form2() {
[RUNNING] `rustc [..] --cfg foo[..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
"));
}

#[test]
fn two_matching_in_config() {
let p1 = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
"#)
.file(".cargo/config", r#"
[target.'cfg(unix)']
rustflags = ["--cfg", 'foo="a"']
[target.'cfg(windows)']
rustflags = ["--cfg", 'foo="a"']
[target.'cfg(target_pointer_width = "32")']
rustflags = ["--cfg", 'foo="b"']
[target.'cfg(target_pointer_width = "64")']
rustflags = ["--cfg", 'foo="b"']
"#)
.file("src/main.rs", r#"
fn main() {
if cfg!(foo = "a") {
println!("a");
} else if cfg!(foo = "b") {
println!("b");
} else {
panic!()
}
}
"#)
.build();

assert_that(p1.cargo("run"), execs().with_status(0));
assert_that(p1.cargo("build"),
execs().with_status(0).with_stderr("\
[FINISHED] [..]
"));
}

0 comments on commit 7d75776

Please sign in to comment.