Skip to content

Commit 0a603fd

Browse files
committed
Document and feature-gate host-config tables.
1 parent e51d151 commit 0a603fd

File tree

4 files changed

+86
-28
lines changed

4 files changed

+86
-28
lines changed

src/cargo/core/features.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ unstable_cli_options!(
600600
namespaced_features: bool = ("Allow features with `dep:` prefix"),
601601
no_index_update: bool = ("Do not update the registry index even if the cache is outdated"),
602602
panic_abort_tests: bool = ("Enable support to run tests with -Cpanic=abort"),
603+
host_config: bool = ("Enable the [host] section in the .cargo/config.toml file"),
603604
patch_in_config: bool = ("Allow `[patch]` sections in .cargo/config.toml files"),
604605
rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"),
605606
separate_nightlies: bool = (HIDDEN),
@@ -787,6 +788,7 @@ impl CliUnstable {
787788
"panic-abort-tests" => self.panic_abort_tests = parse_empty(k, v)?,
788789
"jobserver-per-rustc" => self.jobserver_per_rustc = parse_empty(k, v)?,
789790
"configurable-env" => self.configurable_env = parse_empty(k, v)?,
791+
"host-config" => self.host_config = parse_empty(k, v)?,
790792
"patch-in-config" => self.patch_in_config = parse_empty(k, v)?,
791793
"features" => {
792794
// For now this is still allowed (there are still some

src/cargo/util/config/target.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,21 @@ pub(super) fn load_target_cfgs(config: &Config) -> CargoResult<Vec<(String, Targ
6666

6767
/// Loads a single `[host]` table for the given triple.
6868
pub(super) fn load_host_triple(config: &Config, triple: &str) -> CargoResult<TargetConfig> {
69-
let host_triple_key = ConfigKey::from_str(&format!("host.{}", triple));
70-
let host_prefix = match config.get_cv(&host_triple_key)? {
71-
Some(_) => format!("host.{}", triple),
72-
None => "host".to_string(),
73-
};
74-
load_config_table(config, &host_prefix)
69+
if config.cli_unstable().host_config {
70+
let host_triple_key = ConfigKey::from_str(&format!("host.{}", triple));
71+
let host_prefix = match config.get_cv(&host_triple_key)? {
72+
Some(_) => format!("host.{}", triple),
73+
None => "host".to_string(),
74+
};
75+
load_config_table(config, &host_prefix)
76+
} else {
77+
Ok(TargetConfig {
78+
runner: None,
79+
rustflags: None,
80+
linker: None,
81+
links_overrides: BTreeMap::new(),
82+
})
83+
}
7584
}
7685

7786
/// Loads a single `[target]` table for the given triple.

src/doc/src/reference/unstable.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,39 @@ cargo +nightly -Zunstable-options -Zconfig-include --config somefile.toml build
641641

642642
CLI paths are relative to the current working directory.
643643

644+
### host-config
645+
* Original Pull Request: [#9322](https://github.com/rust-lang/cargo/pull/9322)
646+
* Tracking Issue: [#3349](https://github.com/rust-lang/cargo/issues/3349)
647+
648+
The `host` key in a config file can be used pass flags to host build targets
649+
such as build scripts that must run on the host system instead of the target
650+
system when cross compiling. It supports both generic and host arch specific
651+
tables. Matching host arch tables take precedence over generic host tables.
652+
653+
It requires the `-Zhost-config` command-line option.
654+
655+
```toml
656+
# .cargo/config
657+
cargo-features = ["host-config"]
658+
659+
[host]
660+
linker = "/path/to/host/linker"
661+
[host.x86_64-unknown-linux-gnu]
662+
linker = "/path/to/host/arch/linker"
663+
[target.x86_64-unknown-linux-gnu]
664+
linker = "/path/to/target/linker"
665+
```
666+
667+
The generic `host` table above will be entirely ignored when building on a
668+
`x86_64-unknown-linux-gnu` host as the `host.x86_64-unknown-linux-gnu` table
669+
takes precedence.
670+
671+
This feature requires a `--target` to be specified.
672+
673+
```console
674+
cargo +nightly -Zunstable-options -Zhost-config --config somefile.toml build --target x86_64-unknown-linux-gnu
675+
```
676+
644677
### unit-graph
645678
* Tracking Issue: [#8002](https://github.com/rust-lang/cargo/issues/8002)
646679

tests/testsuite/build_script.rs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,20 @@ fn custom_build_env_var_rustc_linker_bad_host() {
266266
.build();
267267

268268
// build.rs should fail due to bad host linker being set
269-
p.cargo("build --verbose --target")
270-
.arg(&target)
271-
.with_status(101)
272-
.with_stderr_contains(
273-
"\
269+
if cargo_test_support::is_nightly() {
270+
p.cargo("build -Z host-config --verbose --target")
271+
.arg(&target)
272+
.masquerade_as_nightly_cargo()
273+
.with_status(101)
274+
.with_stderr_contains(
275+
"\
274276
[COMPILING] foo v0.0.1 ([CWD])
275277
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin [..]-C linker=[..]/path/to/host/linker [..]`
276278
[ERROR] linker `[..]/path/to/host/linker` not found
277279
"
278-
)
279-
.run();
280+
)
281+
.run();
282+
}
280283
}
281284

282285
#[cargo_test]
@@ -311,17 +314,20 @@ fn custom_build_env_var_rustc_linker_bad_host_with_arch() {
311314
.build();
312315

313316
// build.rs should fail due to bad host linker being set
314-
p.cargo("build --verbose --target")
315-
.arg(&target)
316-
.with_status(101)
317-
.with_stderr_contains(
318-
"\
317+
if cargo_test_support::is_nightly() {
318+
p.cargo("build -Z host-config --verbose --target")
319+
.arg(&target)
320+
.masquerade_as_nightly_cargo()
321+
.with_status(101)
322+
.with_stderr_contains(
323+
"\
319324
[COMPILING] foo v0.0.1 ([CWD])
320325
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin [..]-C linker=[..]/path/to/host/arch/linker [..]`
321326
[ERROR] linker `[..]/path/to/host/arch/linker` not found
322327
"
323-
)
324-
.run();
328+
)
329+
.run();
330+
}
325331
}
326332

327333
#[cargo_test]
@@ -355,7 +361,12 @@ fn custom_build_env_var_rustc_linker_cross_arch_host() {
355361
.build();
356362

357363
// build.rs should fail due to bad host linker being set
358-
p.cargo("build --verbose --target").arg(&target).run();
364+
if cargo_test_support::is_nightly() {
365+
p.cargo("build -Z host-config --verbose --target")
366+
.arg(&target)
367+
.masquerade_as_nightly_cargo()
368+
.run();
369+
}
359370
}
360371

361372
#[cargo_test]
@@ -391,17 +402,20 @@ fn custom_build_env_var_rustc_linker_bad_cross_arch_host() {
391402
.build();
392403

393404
// build.rs should fail due to bad host linker being set
394-
p.cargo("build --verbose --target")
395-
.arg(&target)
396-
.with_status(101)
397-
.with_stderr_contains(
398-
"\
405+
if cargo_test_support::is_nightly() {
406+
p.cargo("build -Z host-config --verbose --target")
407+
.arg(&target)
408+
.masquerade_as_nightly_cargo()
409+
.with_status(101)
410+
.with_stderr_contains(
411+
"\
399412
[COMPILING] foo v0.0.1 ([CWD])
400413
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin [..]-C linker=[..]/path/to/host/linker [..]`
401414
[ERROR] linker `[..]/path/to/host/linker` not found
402415
"
403-
)
404-
.run();
416+
)
417+
.run();
418+
}
405419
}
406420

407421
#[cargo_test]

0 commit comments

Comments
 (0)