Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] [..]
"));
}