From e9a5fb14b011a0d93586d098701dfa2d493494e7 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Mon, 3 Oct 2016 20:37:25 -0500 Subject: [PATCH 1/2] add support for per-target rustflags in .cargo/config you can now specify rustflags on a per-target basis in .cargo/config: ``` toml [target.x86_64-unknown-linux-gnu] rustflags = ["x86", "specific", "flags"] [target.arm-unknown-linux-gnueabi] rustflags = ["arm", "specific", "flags"] ``` If both build.rustflags and target.*.rustflags are specified, the target.* ones will be used. As before RUSTFLAGS overrides either set. closes #3153 --- src/cargo/ops/cargo_rustc/context.rs | 10 +++- tests/rustflags.rs | 71 ++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index fe0a9424141..7cbd2149fe4 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -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::(); + // 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); diff --git a/tests/rustflags.rs b/tests/rustflags.rs index ccdc6a91dc9..cc9266ac281 100644 --- a/tests/rustflags.rs +++ b/tests/rustflags.rs @@ -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)); +} From ef727b7d895685b4debce922c7ee45796d76f0f2 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Tue, 4 Oct 2016 23:59:03 -0500 Subject: [PATCH 2/2] add documentation about target.$triple.rustflags --- src/doc/config.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/doc/config.md b/src/doc/config.md index 6a41019923a..c542af429eb 100644 --- a/src/doc/config.md +++ b/src/doc/config.md @@ -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]