Make x use the x and x.ps1 scripts

This removes another python search from bootstrap.
This commit is contained in:
Albert Larsan 2022-12-18 18:07:48 +01:00
parent 35a99eef32
commit 8348e05644
No known key found for this signature in database
GPG Key ID: F91FB20C20E1306C

View File

@ -1,51 +1,43 @@
//! Run `x.py` from any subdirectory of a rust compiler checkout. //! Run bootstrap from any subdirectory of a rust compiler checkout.
//! //!
//! We prefer `exec`, to avoid adding an extra process in the process tree. //! We prefer `exec`, to avoid adding an extra process in the process tree.
//! However, since `exec` isn't available on Windows, we indirect through //! However, since `exec` isn't available on Windows, we indirect through
//! `exec_or_status`, which will call `exec` on unix and `status` on Windows. //! `exec_or_status`, which will call `exec` on unix and `status` on Windows.
//! //!
//! We use `python`, `python3`, or `python2` as the python interpreter to run //! We use `powershell.exe x.ps1` on Windows, and `sh -c x` on Unix, those are
//! `x.py`, in that order of preference. //! the ones that call `x.py`. We use `sh -c` on Unix, because it is a standard.
//! We also don't use `pwsh` on Windows, because it is not installed by default;
use std::{ use std::{
env::{self, consts::EXE_EXTENSION}, env, io,
io, path::Path,
process::{self, Command, ExitStatus}, process::{self, Command, ExitStatus},
}; };
const PYTHON: &str = "python"; #[cfg(windows)]
const PYTHON2: &str = "python2"; fn x_command(dir: &Path) -> Command {
const PYTHON3: &str = "python3"; let mut cmd = Command::new("powershell.exe");
cmd.args([
"-NoLogo",
"-NoProfile",
"-NonInteractive",
"-ExecutionPolicy",
"RemoteSigned",
"-Command",
"./x.ps1",
])
.current_dir(dir);
cmd
}
fn python() -> &'static str { #[cfg(unix)]
let val = match env::var_os("PATH") { fn x_command(dir: &Path) -> Command {
Some(val) => val, Command::new(dir.join("x"))
None => return PYTHON, }
};
let mut python2 = false; #[cfg(not(any(windows, unix)))]
let mut python3 = false; fn x_command(_dir: &Path) -> Command {
compile_error!("Unsupported platform");
for dir in env::split_paths(&val) {
// `python` should always take precedence over python2 / python3 if it exists
if dir.join(PYTHON).with_extension(EXE_EXTENSION).exists() {
return PYTHON;
}
python2 |= dir.join(PYTHON2).with_extension(EXE_EXTENSION).exists();
python3 |= dir.join(PYTHON3).with_extension(EXE_EXTENSION).exists();
}
// try 3 before 2
if python3 {
PYTHON3
} else if python2 {
PYTHON2
} else {
// Python was not found on path, so exit
eprintln!("Unable to find python in your PATH. Please check it is installed.");
process::exit(1);
}
} }
#[cfg(unix)] #[cfg(unix)]
@ -72,15 +64,15 @@ fn main() {
let candidate = dir.join("x.py"); let candidate = dir.join("x.py");
if candidate.exists() { if candidate.exists() {
let mut python = Command::new(python()); let mut cmd = x_command(dir);
python.arg(&candidate).args(env::args().skip(1)).current_dir(dir); cmd.args(env::args().skip(1)).current_dir(dir);
let result = exec_or_status(&mut python); let result = exec_or_status(&mut cmd);
match result { match result {
Err(error) => { Err(error) => {
eprintln!("Failed to invoke `{}`: {}", candidate.display(), error); eprintln!("Failed to invoke `{:?}`: {}", cmd, error);
} }
Ok(status) => { Ok(status) => {
process::exit(status.code().unwrap_or(1)); process::exit(status.code().unwrap_or(1));