1- //! Run `x.py` from any subdirectory of a rust compiler checkout.
1+ //! Run bootstrap from any subdirectory of a rust compiler checkout.
22//!
33//! We prefer `exec`, to avoid adding an extra process in the process tree.
44//! However, since `exec` isn't available on Windows, we indirect through
55//! `exec_or_status`, which will call `exec` on unix and `status` on Windows.
66//!
7- //! We use `python`, `python3`, or `python2` as the python interpreter to run
8- //! `x.py`, in that order of preference.
7+ //! We use `powershell.exe x.ps1` on Windows, and `sh -c x` on Unix, those are
8+ //! the ones that call `x.py`. We use `sh -c` on Unix, because it is a standard.
9+ //! We also don't use `pwsh` on Windows, because it is not installed by default;
910
1011use std:: {
11- env:: { self , consts :: EXE_EXTENSION } ,
12- io ,
12+ env, io ,
13+ path :: Path ,
1314 process:: { self , Command , ExitStatus } ,
1415} ;
1516
16- const PYTHON : & str = "python" ;
17- const PYTHON2 : & str = "python2" ;
18- const PYTHON3 : & str = "python3" ;
19-
20- fn python ( ) -> & ' static str {
21- let val = match env:: var_os ( "PATH" ) {
22- Some ( val) => val,
23- None => return PYTHON ,
24- } ;
25-
26- let mut python2 = false ;
27- let mut python3 = false ;
28-
29- for dir in env:: split_paths ( & val) {
30- // `python` should always take precedence over python2 / python3 if it exists
31- if dir. join ( PYTHON ) . with_extension ( EXE_EXTENSION ) . exists ( ) {
32- return PYTHON ;
33- }
17+ #[ cfg( windows) ]
18+ fn x_command ( dir : & Path ) -> Command {
19+ let mut cmd = Command :: new ( "powershell.exe" ) ;
20+ cmd. args ( [
21+ "-NoLogo" ,
22+ "-NoProfile" ,
23+ "-NonInteractive" ,
24+ "-ExecutionPolicy" ,
25+ "RemoteSigned" ,
26+ "-Command" ,
27+ "./x.ps1" ,
28+ ] )
29+ . current_dir ( dir) ;
30+ cmd
31+ }
3432
35- python2 |= dir. join ( PYTHON2 ) . with_extension ( EXE_EXTENSION ) . exists ( ) ;
36- python3 |= dir. join ( PYTHON3 ) . with_extension ( EXE_EXTENSION ) . exists ( ) ;
37- }
33+ #[ cfg( unix) ]
34+ fn x_command ( dir : & Path ) -> Command {
35+ Command :: new ( dir. join ( "x" ) )
36+ }
3837
39- // try 3 before 2
40- if python3 {
41- PYTHON3
42- } else if python2 {
43- PYTHON2
44- } else {
45- // Python was not found on path, so exit
46- eprintln ! ( "Unable to find python in your PATH. Please check it is installed." ) ;
47- process:: exit ( 1 ) ;
48- }
38+ #[ cfg( not( any( windows, unix) ) ) ]
39+ fn x_command ( _dir : & Path ) -> Command {
40+ compile_error ! ( "Unsupported platform" ) ;
4941}
5042
5143#[ cfg( unix) ]
@@ -72,15 +64,15 @@ fn main() {
7264 let candidate = dir. join ( "x.py" ) ;
7365
7466 if candidate. exists ( ) {
75- let mut python = Command :: new ( python ( ) ) ;
67+ let mut cmd = x_command ( dir ) ;
7668
77- python . arg ( & candidate ) . args ( env:: args ( ) . skip ( 1 ) ) . current_dir ( dir) ;
69+ cmd . args ( env:: args ( ) . skip ( 1 ) ) . current_dir ( dir) ;
7870
79- let result = exec_or_status ( & mut python ) ;
71+ let result = exec_or_status ( & mut cmd ) ;
8072
8173 match result {
8274 Err ( error) => {
83- eprintln ! ( "Failed to invoke `{}`: {}" , candidate . display ( ) , error) ;
75+ eprintln ! ( "Failed to invoke `{:? }`: {}" , cmd , error) ;
8476 }
8577 Ok ( status) => {
8678 process:: exit ( status. code ( ) . unwrap_or ( 1 ) ) ;
0 commit comments