Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for per-target rustflags in .cargo/config #3157

Merged
merged 2 commits into from
Oct 5, 2016
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: 9 additions & 1 deletion src/cargo/ops/cargo_rustc/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,8 +775,16 @@ fn env_args(config: &Config,
return Ok(args.collect());
}

// Then the build.rustflags value
let name = name.chars().flat_map(|c| c.to_lowercase()).collect::<String>();
// Then the target.*.rustflags value
let target = build_config.requested_target.as_ref().unwrap_or(&build_config.host_triple);
let key = format!("target.{}.{}", target, name);
if let Some(args) = try!(config.get_list(&key)) {
let args = args.val.into_iter().map(|a| a.0);
return Ok(args.collect());
}

// Then the build.rustflags value
let key = format!("build.{}", name);
if let Some(args) = try!(config.get_list(&key)) {
let args = args.val.into_iter().map(|a| a.0);
Expand Down
3 changes: 3 additions & 0 deletions src/doc/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ linker = ".."
# Similar to the above linker configuration, but this only applies to
# when the `$triple` is being compiled for.
linker = ".."
# custom flags to pass to all compiler invocations that target $triple
# this value overrides build.rustflags when both are present
rustflags = ["..", ".."]

# Configuration keys related to the registry
[registry]
Expand Down
71 changes: 71 additions & 0 deletions tests/rustflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,3 +878,74 @@ fn build_rustflags_with_home_config() {
assert_that(p.cargo("build").arg("-v"),
execs().with_status(0));
}

#[test]
fn target_rustflags_normal_source() {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
"#)
.file("src/lib.rs", "")
.file("src/bin/a.rs", "fn main() {}")
.file("examples/b.rs", "fn main() {}")
.file("tests/c.rs", "#[test] fn f() { }")
.file("benches/d.rs", r#"
#![feature(test)]
extern crate test;
#[bench] fn run1(_ben: &mut test::Bencher) { }"#)
.file(".cargo/config", &format!("
[target.{}]
rustflags = [\"-Z\", \"bogus\"]
", rustc_host()));
p.build();

assert_that(p.cargo("build")
.arg("--lib"),
execs().with_status(101));
assert_that(p.cargo("build")
.arg("--bin=a"),
execs().with_status(101));
assert_that(p.cargo("build")
.arg("--example=b"),
execs().with_status(101));
assert_that(p.cargo("test"),
execs().with_status(101));
assert_that(p.cargo("bench"),
execs().with_status(101));
}

// target.{}.rustflags takes precedence over build.rustflags
#[test]
fn target_rustflags_precedence() {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
"#)
.file("src/lib.rs", "")
.file(".cargo/config", &format!("
[build]
rustflags = [\"--cfg\", \"foo\"]

[target.{}]
rustflags = [\"-Z\", \"bogus\"]
", rustc_host()));
p.build();

assert_that(p.cargo("build")
.arg("--lib"),
execs().with_status(101));
assert_that(p.cargo("build")
.arg("--bin=a"),
execs().with_status(101));
assert_that(p.cargo("build")
.arg("--example=b"),
execs().with_status(101));
assert_that(p.cargo("test"),
execs().with_status(101));
assert_that(p.cargo("bench"),
execs().with_status(101));
}