From 1a4b23adac5e70aa777171d3eeeded6e2a763664 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Sat, 6 Apr 2019 19:40:22 +0200 Subject: [PATCH 1/5] Add a `build.build-target` config key This key allows overriding the default target for build operations (e.g. `cargo build`, `cargo check`, `cargo run`), but not for test operations (`cargo test`, `cargo bench`). --- src/cargo/core/compiler/build_config.rs | 27 ++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs index 70b2b355cc9..be4fff6f2d0 100644 --- a/src/cargo/core/compiler/build_config.rs +++ b/src/cargo/core/compiler/build_config.rs @@ -38,6 +38,7 @@ impl BuildConfig { /// /// * `build.jobs` /// * `build.target` + /// * `build.build-target` /// * `target.$target.ar` /// * `target.$target.linker` /// * `target.$target.libfoo.metadata` @@ -65,16 +66,24 @@ impl BuildConfig { failure::bail!("target was empty") } } - let cfg_target = match config.get_string("build.target")? { - Some(ref target) if target.val.ends_with(".json") => { - let path = target.definition.root(config).join(&target.val); - let path_string = path - .into_os_string() - .into_string() - .map_err(|_| failure::format_err!("Target path is not valid unicode")); - Some(path_string?) + let cfg_target = { + let target = config.get_string("build.target")?; + let build_target = if !mode.is_any_test() { + config.get_string("build.build-target")? + } else { + None + }; + match build_target.or(target) { + Some(ref target) if target.val.ends_with(".json") => { + let path = target.definition.root(config).join(&target.val); + let path_string = path + .into_os_string() + .into_string() + .map_err(|_| failure::format_err!("Target path is not valid unicode")); + Some(path_string?) + } + other => other.map(|t| t.val), } - other => other.map(|t| t.val), }; let target = requested_target.or(cfg_target); From 5930b4c6815b3820cc86313f7d5d7333ac2e2fdf Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Sat, 6 Apr 2019 20:28:00 +0200 Subject: [PATCH 2/5] Update documentation --- src/doc/src/reference/config.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/doc/src/reference/config.md b/src/doc/src/reference/config.md index fb2da971b27..755dab609f4 100644 --- a/src/doc/src/reference/config.md +++ b/src/doc/src/reference/config.md @@ -124,7 +124,9 @@ debug = false jobs = 1 # number of parallel jobs, defaults to # of CPUs rustc = "rustc" # the rust compiler tool rustdoc = "rustdoc" # the doc generator tool -target = "triple" # build for the target triple (ignored by `cargo install`) +target = "triple" # build and test for the target triple (ignored by `cargo install`) +build-target = "triple" # build for the target triple (ignored by `cargo install`), overrides + # `build.target` target-dir = "target" # path of where to place all generated artifacts rustflags = ["..", ".."] # custom flags to pass to all compiler invocations rustdocflags = ["..", ".."] # custom flags to pass to rustdoc From 6699efafd8f714e0f1c50bb6351012e103d1cfc9 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Sat, 6 Apr 2019 20:28:11 +0200 Subject: [PATCH 3/5] Add a test --- tests/testsuite/cross_compile.rs | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/testsuite/cross_compile.rs b/tests/testsuite/cross_compile.rs index 686b284146e..e093c853433 100644 --- a/tests/testsuite/cross_compile.rs +++ b/tests/testsuite/cross_compile.rs @@ -109,6 +109,72 @@ fn simple_cross_config() { p.process(&p.target_bin(&target, "foo")).run(); } +#[test] +fn cross_config_build_target() { + if cross_compile::disabled() { + return; + } + + let p = project() + .file( + ".cargo/config", + &format!( + r#" + [build] + build-target = "{}" + "#, + cross_compile::alternate() + ), + ) + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.0" + authors = [] + build = "build.rs" + "#, + ) + .file( + "build.rs", + r#" + fn main() {{ + println!("cargo:rustc-env=TARGET={}", std::env::var("TARGET").unwrap()); + }} + "#, + ) + .file( + "src/main.rs", + &format!( + r#" + use std::env; + fn main() {{ + assert_eq!(env::consts::ARCH, "{arch}"); + assert_eq!(env!("TARGET"), "{target}"); + }} + + #[test] + fn test_host_target() {{ + assert_eq!(env!("TARGET"), "{host}"); + }} + "#, + target = cross_compile::alternate(), + arch = cross_compile::alternate_arch(), + host = cross_compile::host(), + ), + ) + .build(); + + let target = cross_compile::alternate(); + + p.cargo("build -v").run(); + assert!(p.target_bin(&target, "foo").is_file()); + p.process(&p.target_bin(&target, "foo")).run(); + + p.cargo("test -v").run(); +} + #[test] fn simple_deps() { if cross_compile::disabled() { From a1c0f4fbdc24d770f1fa1297b69cc25d97ab5c93 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Sat, 6 Apr 2019 20:48:37 +0200 Subject: [PATCH 4/5] Reduce line length in documentation --- src/doc/src/reference/config.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/doc/src/reference/config.md b/src/doc/src/reference/config.md index 755dab609f4..5ff2881681f 100644 --- a/src/doc/src/reference/config.md +++ b/src/doc/src/reference/config.md @@ -124,9 +124,10 @@ debug = false jobs = 1 # number of parallel jobs, defaults to # of CPUs rustc = "rustc" # the rust compiler tool rustdoc = "rustdoc" # the doc generator tool -target = "triple" # build and test for the target triple (ignored by `cargo install`) -build-target = "triple" # build for the target triple (ignored by `cargo install`), overrides - # `build.target` +target = "triple" # build and test for the target triple + # (ignored by `cargo install`) +build-target = "triple" # build for the target triple, overrides `build.target` + # (ignored by `cargo install`) target-dir = "target" # path of where to place all generated artifacts rustflags = ["..", ".."] # custom flags to pass to all compiler invocations rustdocflags = ["..", ".."] # custom flags to pass to rustdoc From d6c77c55a72e5b2e8b693af634363f7ad04adee3 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Sat, 6 Apr 2019 20:55:50 +0200 Subject: [PATCH 5/5] Improve documentation --- src/doc/src/reference/config.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/doc/src/reference/config.md b/src/doc/src/reference/config.md index 5ff2881681f..cf92b8cc9a7 100644 --- a/src/doc/src/reference/config.md +++ b/src/doc/src/reference/config.md @@ -124,10 +124,13 @@ debug = false jobs = 1 # number of parallel jobs, defaults to # of CPUs rustc = "rustc" # the rust compiler tool rustdoc = "rustdoc" # the doc generator tool -target = "triple" # build and test for the target triple - # (ignored by `cargo install`) -build-target = "triple" # build for the target triple, overrides `build.target` - # (ignored by `cargo install`) +target = "triple" # the default target triple when no `--target` is + # passed (ignored by `cargo install`) +build-target = "triple" # the default target triple for build commands such + # as `build`, `check`, or `run`, but not for test + # commands such as test or bench. Ignored by + # `cargo install`. Takes precendence over + # `build.target`. target-dir = "target" # path of where to place all generated artifacts rustflags = ["..", ".."] # custom flags to pass to all compiler invocations rustdocflags = ["..", ".."] # custom flags to pass to rustdoc