Skip to content

Commit

Permalink
Merge #803
Browse files Browse the repository at this point in the history
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 <emil.gardstrom@gmail.com>
  • Loading branch information
bors[bot] and Emilgardis committed Jun 16, 2022
2 parents f532b20 + e84d948 commit 3ded782
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Added

- #803 - added `CROSS_CUSTOM_TOOLCHAIN` to disable automatic installation of components for use with tools like `cargo-bisect-rustc`
- #795 - added images for additional toolchains maintained by cross-rs.
- #792 - added `CROSS_CONTAINER_IN_CONTAINER` environment variable to replace `CROSS_DOCKER_IN_DOCKER`.
- #782 - added `build-std` config option, which builds the rust standard library from source if enabled.
Expand Down
52 changes: 27 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,35 +416,37 @@ pub fn run() -> Result<ExitStatus> {
is_nightly = channel == Channel::Nightly;
}

// build-std overrides xargo, but only use it if it's a built-in
// tool but not an available target or doesn't have rust-std.
let available_targets = rustup::available_targets(&toolchain, verbose)?;
let uses_build_std = config.build_std(&target).unwrap_or(false);
let uses_xargo =
!uses_build_std && config.xargo(&target).unwrap_or(!target.is_builtin());
if !is_nightly && uses_build_std {
eyre::bail!(
"no rust-std component available for {}: must use nightly",
target.triple()
);
}

if !uses_xargo
&& !available_targets.is_installed(&target)
&& available_targets.contains(&target)
{
rustup::install(&target, &toolchain, verbose)?;
} else if !rustup::component_is_installed("rust-src", &toolchain, verbose)? {
rustup::install_component("rust-src", &toolchain, verbose)?;
}
if std::env::var("CROSS_CUSTOM_TOOLCHAIN").is_err() {
// build-std overrides xargo, but only use it if it's a built-in
// tool but not an available target or doesn't have rust-std.
let available_targets = rustup::available_targets(&toolchain, verbose)?;

if !is_nightly && uses_build_std {
eyre::bail!(
"no rust-std component available for {}: must use nightly",
target.triple()
);
}

if args
.subcommand
.map(|sc| sc == Subcommand::Clippy)
.unwrap_or(false)
&& !rustup::component_is_installed("clippy", &toolchain, verbose)?
{
rustup::install_component("clippy", &toolchain, verbose)?;
if !uses_xargo
&& !available_targets.is_installed(&target)
&& available_targets.contains(&target)
{
rustup::install(&target, &toolchain, verbose)?;
} else if !rustup::component_is_installed("rust-src", &toolchain, verbose)? {
rustup::install_component("rust-src", &toolchain, verbose)?;
}
if args
.subcommand
.map(|sc| sc == Subcommand::Clippy)
.unwrap_or(false)
&& !rustup::component_is_installed("clippy", &toolchain, verbose)?
{
rustup::install_component("clippy", &toolchain, verbose)?;
}
}

let needs_interpreter = args
Expand Down
15 changes: 11 additions & 4 deletions src/rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::process::Command;
use rustc_version::{Channel, Version};

use crate::errors::*;
use crate::extensions::CommandExt;
pub use crate::extensions::{CommandExt, OutputExt};
use crate::Target;

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

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

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

0 comments on commit 3ded782

Please sign in to comment.