Skip to content

Commit 3ded782

Browse files
Merge #803
803: add `CROSS_CUSTOM_TOOLCHAIN` to disable automatic target component downloading r=Alexhuszagh a=Emilgardis This adds an environment variable `CROSS_CUSTOM_TOOLCHAIN` to disable some rustup commands. The way to use this with `cargo-bisect-rustc` is `cargo bisect-rustc --script=./bisect.sh --target powerpc64le-unknown-linux-gnu` and the script is ```sh #!/usr/bin/env bash export CROSS_CUSTOM_TOOLCHAIN=1 exec cross run --target powerpc64le-unknown-linux-gnu ``` I've filed rust-lang/cargo-bisect-rustc#159 to avoid the pitfall that was discovered when not specifying `--target` resolves #699 Co-authored-by: Emil Gardström <[email protected]>
2 parents f532b20 + e84d948 commit 3ded782

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77

88
### Added
99

10+
- #803 - added `CROSS_CUSTOM_TOOLCHAIN` to disable automatic installation of components for use with tools like `cargo-bisect-rustc`
1011
- #795 - added images for additional toolchains maintained by cross-rs.
1112
- #792 - added `CROSS_CONTAINER_IN_CONTAINER` environment variable to replace `CROSS_DOCKER_IN_DOCKER`.
1213
- #782 - added `build-std` config option, which builds the rust standard library from source if enabled.

src/lib.rs

+27-25
Original file line numberDiff line numberDiff line change
@@ -416,35 +416,37 @@ pub fn run() -> Result<ExitStatus> {
416416
is_nightly = channel == Channel::Nightly;
417417
}
418418

419-
// build-std overrides xargo, but only use it if it's a built-in
420-
// tool but not an available target or doesn't have rust-std.
421-
let available_targets = rustup::available_targets(&toolchain, verbose)?;
422419
let uses_build_std = config.build_std(&target).unwrap_or(false);
423420
let uses_xargo =
424421
!uses_build_std && config.xargo(&target).unwrap_or(!target.is_builtin());
425-
if !is_nightly && uses_build_std {
426-
eyre::bail!(
427-
"no rust-std component available for {}: must use nightly",
428-
target.triple()
429-
);
430-
}
431-
432-
if !uses_xargo
433-
&& !available_targets.is_installed(&target)
434-
&& available_targets.contains(&target)
435-
{
436-
rustup::install(&target, &toolchain, verbose)?;
437-
} else if !rustup::component_is_installed("rust-src", &toolchain, verbose)? {
438-
rustup::install_component("rust-src", &toolchain, verbose)?;
439-
}
422+
if std::env::var("CROSS_CUSTOM_TOOLCHAIN").is_err() {
423+
// build-std overrides xargo, but only use it if it's a built-in
424+
// tool but not an available target or doesn't have rust-std.
425+
let available_targets = rustup::available_targets(&toolchain, verbose)?;
426+
427+
if !is_nightly && uses_build_std {
428+
eyre::bail!(
429+
"no rust-std component available for {}: must use nightly",
430+
target.triple()
431+
);
432+
}
440433

441-
if args
442-
.subcommand
443-
.map(|sc| sc == Subcommand::Clippy)
444-
.unwrap_or(false)
445-
&& !rustup::component_is_installed("clippy", &toolchain, verbose)?
446-
{
447-
rustup::install_component("clippy", &toolchain, verbose)?;
434+
if !uses_xargo
435+
&& !available_targets.is_installed(&target)
436+
&& available_targets.contains(&target)
437+
{
438+
rustup::install(&target, &toolchain, verbose)?;
439+
} else if !rustup::component_is_installed("rust-src", &toolchain, verbose)? {
440+
rustup::install_component("rust-src", &toolchain, verbose)?;
441+
}
442+
if args
443+
.subcommand
444+
.map(|sc| sc == Subcommand::Clippy)
445+
.unwrap_or(false)
446+
&& !rustup::component_is_installed("clippy", &toolchain, verbose)?
447+
{
448+
rustup::install_component("clippy", &toolchain, verbose)?;
449+
}
448450
}
449451

450452
let needs_interpreter = args

src/rustup.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::process::Command;
44
use rustc_version::{Channel, Version};
55

66
use crate::errors::*;
7-
use crate::extensions::CommandExt;
7+
pub use crate::extensions::{CommandExt, OutputExt};
88
use crate::Target;
99

1010
#[derive(Debug)]
@@ -43,10 +43,17 @@ pub fn installed_toolchains(verbose: bool) -> Result<Vec<String>> {
4343
}
4444

4545
pub fn available_targets(toolchain: &str, verbose: bool) -> Result<AvailableTargets> {
46-
let out = Command::new("rustup")
47-
.args(&["target", "list", "--toolchain", toolchain])
48-
.run_and_get_stdout(verbose)?;
46+
let mut cmd = Command::new("rustup");
47+
cmd.args(&["target", "list", "--toolchain", toolchain]);
48+
let output = cmd.run_and_get_output(verbose)?;
4949

50+
if !output.status.success() {
51+
if String::from_utf8_lossy(&output.stderr).contains("is a custom toolchain") {
52+
eyre::bail!("{toolchain} is a custom toolchain. To use it, you'll need to set the environment variable `CROSS_CUSTOM_TOOLCHAIN=1`")
53+
}
54+
return Err(cmd.status_result(output.status).unwrap_err().into());
55+
}
56+
let out = output.stdout()?;
5057
let mut default = String::new();
5158
let mut installed = vec![];
5259
let mut not_installed = vec![];

0 commit comments

Comments
 (0)