Skip to content

Commit 3afbe9d

Browse files
committed
Update rustc and cargo commands to use envvars.
`rustc` and `cargo` will now use `RUSTC` and `CARGO` environment variables, respectively, if present.
1 parent 4d15abe commit 3afbe9d

File tree

7 files changed

+26
-16
lines changed

7 files changed

+26
-16
lines changed

src/cargo.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::process::{Command, ExitStatus};
44

55
use crate::cli::Args;
66
use crate::errors::*;
7-
use crate::extensions::CommandExt;
7+
use crate::extensions::{env_program, CommandExt};
88

99
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1010
pub enum Subcommand {
@@ -100,17 +100,17 @@ impl Package {
100100
}
101101
}
102102

103+
pub fn cargo_command() -> Command {
104+
Command::new(env_program("CARGO", "cargo"))
105+
}
106+
103107
/// Cargo metadata with specific invocation
104108
pub fn cargo_metadata_with_args(
105109
cd: Option<&Path>,
106110
args: Option<&Args>,
107111
verbose: bool,
108112
) -> Result<Option<CargoMetadata>> {
109-
let mut command = std::process::Command::new(
110-
std::env::var("CARGO")
111-
.ok()
112-
.unwrap_or_else(|| "cargo".to_string()),
113-
);
113+
let mut command = cargo_command();
114114
command.arg("metadata").args(&["--format-version", "1"]);
115115
if let Some(cd) = cd {
116116
command.current_dir(cd);
@@ -148,12 +148,12 @@ pub fn cargo_metadata_with_args(
148148

149149
/// Pass-through mode
150150
pub fn run(args: &[String], verbose: bool) -> Result<ExitStatus, CommandError> {
151-
Command::new("cargo")
151+
cargo_command()
152152
.args(args)
153153
.run_and_get_status(verbose, false)
154154
}
155155

156156
/// run cargo and get the output, does not check the exit status
157157
pub fn run_and_get_output(args: &[String], verbose: bool) -> Result<std::process::Output> {
158-
Command::new("cargo").args(args).run_and_get_output(verbose)
158+
cargo_command().args(args).run_and_get_output(verbose)
159159
}

src/docker/local.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub(crate) fn run(
2525
let engine = Engine::new(verbose)?;
2626
let dirs = Directories::create(&engine, metadata, cwd, sysroot, docker_in_docker, verbose)?;
2727

28-
let mut cmd = cargo_cmd(uses_xargo);
28+
let mut cmd = cargo_safe_command(uses_xargo);
2929
cmd.args(args);
3030

3131
let mut docker = subcommand(&engine, "run");

src/docker/shared.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub fn parse_docker_opts(value: &str) -> Result<Vec<String>> {
162162
shell_words::split(value).wrap_err_with(|| format!("could not parse docker opts of {}", value))
163163
}
164164

165-
pub(crate) fn cargo_cmd(uses_xargo: bool) -> SafeCommand {
165+
pub(crate) fn cargo_safe_command(uses_xargo: bool) -> SafeCommand {
166166
if uses_xargo {
167167
SafeCommand::new("xargo")
168168
} else {

src/extensions.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,9 @@ impl From<SafeCommand> for Command {
207207
cmd
208208
}
209209
}
210+
211+
pub(crate) fn env_program(envvar: &str, program: &str) -> String {
212+
std::env::var(envvar)
213+
.ok()
214+
.unwrap_or_else(|| program.to_string())
215+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use config::Config;
3939
use rustc_version::Channel;
4040
use serde::Deserialize;
4141

42-
pub use self::cargo::{cargo_metadata_with_args, CargoMetadata, Subcommand};
42+
pub use self::cargo::{cargo_command, cargo_metadata_with_args, CargoMetadata, Subcommand};
4343
use self::cross_toml::CrossToml;
4444
use self::errors::Context;
4545
use self::rustc::{TargetList, VersionMetaExt};

src/rustc.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::process::Command;
44
use rustc_version::{Version, VersionMeta};
55

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

1010
#[derive(Debug)]
@@ -33,8 +33,12 @@ impl VersionMetaExt for VersionMeta {
3333
}
3434
}
3535

36+
pub fn rustc_command() -> Command {
37+
Command::new(env_program("RUSTC", "rustc"))
38+
}
39+
3640
pub fn target_list(verbose: bool) -> Result<TargetList> {
37-
Command::new("rustc")
41+
rustc_command()
3842
.args(&["--print", "target-list"])
3943
.run_and_get_stdout(verbose)
4044
.map(|s| TargetList {
@@ -43,7 +47,7 @@ pub fn target_list(verbose: bool) -> Result<TargetList> {
4347
}
4448

4549
pub fn sysroot(host: &Host, target: &Target, verbose: bool) -> Result<PathBuf> {
46-
let mut stdout = Command::new("rustc")
50+
let mut stdout = rustc_command()
4751
.args(&["--print", "sysroot"])
4852
.run_and_get_stdout(verbose)?
4953
.trim()

xtask/src/hooks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct Test {
2626
}
2727

2828
fn has_nightly(verbose: bool) -> cross::Result<bool> {
29-
Command::new("cargo")
29+
cross::cargo_command()
3030
.arg("+nightly")
3131
.run_and_get_output(verbose)
3232
.map(|o| o.status.success())
@@ -47,7 +47,7 @@ fn get_channel_prefer_nightly(
4747
}
4848

4949
fn cargo(channel: Option<&str>) -> Command {
50-
let mut command = Command::new("cargo");
50+
let mut command = cross::cargo_command();
5151
if let Some(channel) = channel {
5252
command.arg(&format!("+{channel}"));
5353
}

0 commit comments

Comments
 (0)