Skip to content

Add a build.build-target config key #6825

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

Closed
wants to merge 5 commits into from
Closed
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
27 changes: 18 additions & 9 deletions src/cargo/core/compiler/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl BuildConfig {
///
/// * `build.jobs`
/// * `build.target`
/// * `build.build-target`
/// * `target.$target.ar`
/// * `target.$target.linker`
/// * `target.$target.libfoo.metadata`
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 7 additions & 1 deletion src/doc/src/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +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 for the target triple (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
Expand Down
66 changes: 66 additions & 0 deletions tests/testsuite/cross_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down