@@ -9,7 +9,7 @@ use std::ffi::OsStr;
99use std:: path:: PathBuf ;
1010
1111fn args ( command : & str ) -> Result < Option < Vec < String > > , String > {
12- // We skip the binary and the "cargo" option.
12+ // We skip the binary and the "cargo"/"rustc" option.
1313 if let Some ( "--help" ) = std:: env:: args ( ) . skip ( 2 ) . next ( ) . as_deref ( ) {
1414 usage ( command) ;
1515 return Ok ( None ) ;
@@ -36,66 +36,90 @@ fn usage(command: &str) {
3636 )
3737}
3838
39- pub fn run_cargo ( ) -> Result < ( ) , String > {
40- let args = match args ( "cargo" ) ? {
41- Some ( a) => a,
42- None => return Ok ( ( ) ) ,
43- } ;
39+ struct RustcTools {
40+ env : HashMap < String , String > ,
41+ args : Vec < String > ,
42+ toolchain : String ,
43+ config : ConfigInfo ,
44+ }
4445
45- // We first need to go to the original location to ensure that the config setup will go as
46- // expected.
47- let current_dir = std:: env:: current_dir ( )
48- . and_then ( |path| path. canonicalize ( ) )
49- . map_err ( |error| format ! ( "Failed to get current directory path: {:?}" , error) ) ?;
50- let current_exe = std:: env:: current_exe ( )
51- . and_then ( |path| path. canonicalize ( ) )
52- . map_err ( |error| format ! ( "Failed to get current exe path: {:?}" , error) ) ?;
53- let mut parent_dir = current_exe. components ( ) . map ( |comp| comp. as_os_str ( ) ) . collect :: < Vec < _ > > ( ) ;
54- // We run this script from "build_system/target/release/y", so we need to remove these elements.
55- for to_remove in & [ "y" , "release" , "target" , "build_system" ] {
56- if parent_dir. last ( ) . map ( |part| part == to_remove) . unwrap_or ( false ) {
57- parent_dir. pop ( ) ;
58- } else {
59- return Err ( format ! (
60- "Build script not executed from `build_system/target/release/y` (in path {})" ,
61- current_exe. display( ) ,
62- ) ) ;
46+ impl RustcTools {
47+ fn new ( command : & str ) -> Result < Option < Self > , String > {
48+ let Some ( args) = args ( command) ? else { return Ok ( None ) } ;
49+
50+ // We first need to go to the original location to ensure that the config setup will go as
51+ // expected.
52+ let current_dir = std:: env:: current_dir ( )
53+ . and_then ( |path| path. canonicalize ( ) )
54+ . map_err ( |error| format ! ( "Failed to get current directory path: {:?}" , error) ) ?;
55+ let current_exe = std:: env:: current_exe ( )
56+ . and_then ( |path| path. canonicalize ( ) )
57+ . map_err ( |error| format ! ( "Failed to get current exe path: {:?}" , error) ) ?;
58+ let mut parent_dir =
59+ current_exe. components ( ) . map ( |comp| comp. as_os_str ( ) ) . collect :: < Vec < _ > > ( ) ;
60+ // We run this script from "build_system/target/release/y", so we need to remove these elements.
61+ for to_remove in & [ "y" , "release" , "target" , "build_system" ] {
62+ if parent_dir. last ( ) . map ( |part| part == to_remove) . unwrap_or ( false ) {
63+ parent_dir. pop ( ) ;
64+ } else {
65+ return Err ( format ! (
66+ "Build script not executed from `build_system/target/release/y` (in path {})" ,
67+ current_exe. display( ) ,
68+ ) ) ;
69+ }
6370 }
64- }
65- let parent_dir = PathBuf :: from ( parent_dir. join ( & OsStr :: new ( "/" ) ) ) ;
66- std:: env:: set_current_dir ( & parent_dir) . map_err ( |error| {
67- format ! ( "Failed to go to `{}` folder: {:?}" , parent_dir. display( ) , error)
68- } ) ?;
71+ let parent_dir = PathBuf :: from ( parent_dir. join ( & OsStr :: new ( "/" ) ) ) ;
72+ std:: env:: set_current_dir ( & parent_dir) . map_err ( |error| {
73+ format ! ( "Failed to go to `{}` folder: {:?}" , parent_dir. display( ) , error)
74+ } ) ?;
6975
70- let mut env: HashMap < String , String > = std:: env:: vars ( ) . collect ( ) ;
71- ConfigInfo :: default ( ) . setup ( & mut env, false ) ?;
72- let toolchain = get_toolchain ( ) ?;
76+ let mut env: HashMap < String , String > = std:: env:: vars ( ) . collect ( ) ;
77+ let mut config = ConfigInfo :: default ( ) ;
78+ config. setup ( & mut env, false ) ?;
79+ let toolchain = get_toolchain ( ) ?;
7380
74- let toolchain_version = rustc_toolchain_version_info ( & toolchain) ?;
75- let default_version = rustc_version_info ( None ) ?;
76- if toolchain_version != default_version {
77- println ! (
78- "rustc_codegen_gcc is built for {} but the default rustc version is {}." ,
79- toolchain_version. short, default_version. short,
80- ) ;
81- println ! ( "Using {}." , toolchain_version. short) ;
82- }
81+ let toolchain_version = rustc_toolchain_version_info ( & toolchain) ?;
82+ let default_version = rustc_version_info ( None ) ?;
83+ if toolchain_version != default_version {
84+ println ! (
85+ "rustc_codegen_gcc is built for {} but the default rustc version is {}." ,
86+ toolchain_version. short, default_version. short,
87+ ) ;
88+ println ! ( "Using {}." , toolchain_version. short) ;
89+ }
8390
84- // We go back to the original folder since we now have set up everything we needed.
85- std:: env:: set_current_dir ( & current_dir) . map_err ( |error| {
86- format ! ( "Failed to go back to `{}` folder: {:?}" , current_dir. display( ) , error)
87- } ) ?;
91+ // We go back to the original folder since we now have set up everything we needed.
92+ std:: env:: set_current_dir ( & current_dir) . map_err ( |error| {
93+ format ! ( "Failed to go back to `{}` folder: {:?}" , current_dir. display( ) , error)
94+ } ) ?;
95+ let toolchain = format ! ( "+{}" , toolchain) ;
96+ Ok ( Some ( Self { toolchain, args, env, config } ) )
97+ }
98+ }
8899
89- let rustflags = env. get ( "RUSTFLAGS" ) . cloned ( ) . unwrap_or_default ( ) ;
90- env. insert ( "RUSTDOCFLAGS" . to_string ( ) , rustflags) ;
91- let toolchain = format ! ( "+{}" , toolchain) ;
92- let mut command: Vec < & dyn AsRef < OsStr > > = vec ! [ & "cargo" , & toolchain] ;
93- for arg in & args {
100+ pub fn run_cargo ( ) -> Result < ( ) , String > {
101+ let Some ( mut tools) = RustcTools :: new ( "cargo" ) ? else { return Ok ( ( ) ) } ;
102+ let rustflags = tools. env . get ( "RUSTFLAGS" ) . cloned ( ) . unwrap_or_default ( ) ;
103+ tools. env . insert ( "RUSTDOCFLAGS" . to_string ( ) , rustflags) ;
104+ let mut command: Vec < & dyn AsRef < OsStr > > = vec ! [ & "cargo" , & tools. toolchain] ;
105+ for arg in & tools. args {
94106 command. push ( arg) ;
95107 }
96- if run_command_with_output_and_env_no_err ( & command, None , Some ( & env) ) . is_err ( ) {
108+ if run_command_with_output_and_env_no_err ( & command, None , Some ( & tools . env ) ) . is_err ( ) {
97109 std:: process:: exit ( 1 ) ;
98110 }
99111
100112 Ok ( ( ) )
101113}
114+
115+ pub fn run_rustc ( ) -> Result < ( ) , String > {
116+ let Some ( tools) = RustcTools :: new ( "rustc" ) ? else { return Ok ( ( ) ) } ;
117+ let mut command = tools. config . rustc_command_vec ( ) ;
118+ for arg in & tools. args {
119+ command. push ( arg) ;
120+ }
121+ if run_command_with_output_and_env_no_err ( & command, None , Some ( & tools. env ) ) . is_err ( ) {
122+ std:: process:: exit ( 1 ) ;
123+ }
124+ Ok ( ( ) )
125+ }
0 commit comments