11use  crate :: config:: { Channel ,  ConfigInfo } ; 
2- use  crate :: utils:: { run_command,  run_command_with_output_and_env,  walk_dir} ; 
2+ use  crate :: utils:: { 
3+     copy_file,  create_dir,  get_sysroot_dir,  run_command,  run_command_with_output_and_env,  walk_dir, 
4+ } ; 
35use  std:: collections:: HashMap ; 
46use  std:: ffi:: OsStr ; 
57use  std:: fs; 
@@ -55,8 +57,7 @@ impl BuildArg {
5557    } 
5658} 
5759
58- pub  fn  build_sysroot ( env :  & HashMap < String ,  String > ,  config :  & ConfigInfo )  -> Result < ( ) ,  String >  { 
59-     let  start_dir = Path :: new ( "build_sysroot" ) ; 
60+ fn  cleanup_sysroot_previous_build ( start_dir :  & Path )  { 
6061    // Cleanup for previous run 
6162    // Clean target dir except for build scripts and incremental cache 
6263    let  _ = walk_dir ( 
@@ -100,6 +101,26 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
100101    let  _ = fs:: remove_file ( start_dir. join ( "Cargo.lock" ) ) ; 
101102    let  _ = fs:: remove_file ( start_dir. join ( "test_target/Cargo.lock" ) ) ; 
102103    let  _ = fs:: remove_dir_all ( start_dir. join ( "sysroot" ) ) ; 
104+ } 
105+ 
106+ pub  fn  create_build_sysroot_content ( start_dir :  & Path )  -> Result < ( ) ,  String >  { 
107+     if  !start_dir. is_dir ( )  { 
108+         create_dir ( start_dir) ?; 
109+     } 
110+     copy_file ( "build_system/build_sysroot/Cargo.toml" ,  & start_dir. join ( "Cargo.toml" ) ) ?; 
111+ 
112+     let  src_dir = start_dir. join ( "src" ) ; 
113+     if  !src_dir. is_dir ( )  { 
114+         create_dir ( & src_dir) ?; 
115+     } 
116+     copy_file ( "build_system/build_sysroot/lib.rs" ,  & start_dir. join ( "src/lib.rs" ) ) 
117+ } 
118+ 
119+ pub  fn  build_sysroot ( env :  & HashMap < String ,  String > ,  config :  & ConfigInfo )  -> Result < ( ) ,  String >  { 
120+     let  start_dir = get_sysroot_dir ( ) ; 
121+ 
122+     cleanup_sysroot_previous_build ( & start_dir) ; 
123+     create_build_sysroot_content ( & start_dir) ?; 
103124
104125    // Builds libs 
105126    let  mut  rustflags = env. get ( "RUSTFLAGS" ) . cloned ( ) . unwrap_or_default ( ) ; 
@@ -110,7 +131,6 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
110131    if  config. no_default_features  { 
111132        rustflags. push_str ( " -Csymbol-mangling-version=v0" ) ; 
112133    } 
113-     let  mut  env = env. clone ( ) ; 
114134
115135    let  mut  args:  Vec < & dyn  AsRef < OsStr > >  = vec ! [ & "cargo" ,  & "build" ,  & "--target" ,  & config. target] ; 
116136
@@ -127,18 +147,13 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
127147        "debug" 
128148    } ; 
129149
150+     let  mut  env = env. clone ( ) ; 
130151    env. insert ( "RUSTFLAGS" . to_string ( ) ,  rustflags) ; 
131-     run_command_with_output_and_env ( & args,  Some ( start_dir) ,  Some ( & env) ) ?; 
152+     run_command_with_output_and_env ( & args,  Some ( & start_dir) ,  Some ( & env) ) ?; 
132153
133154    // Copy files to sysroot 
134155    let  sysroot_path = start_dir. join ( format ! ( "sysroot/lib/rustlib/{}/lib/" ,  config. target_triple) ) ; 
135-     fs:: create_dir_all ( & sysroot_path) . map_err ( |error| { 
136-         format ! ( 
137-             "Failed to create directory `{}`: {:?}" , 
138-             sysroot_path. display( ) , 
139-             error
140-         ) 
141-     } ) ?; 
156+     create_dir ( & sysroot_path) ?; 
142157    let  copier = |dir_to_copy :  & Path | { 
143158        // FIXME: should not use shell command! 
144159        run_command ( & [ & "cp" ,  & "-r" ,  & dir_to_copy,  & sysroot_path] ,  None ) . map ( |_| ( ) ) 
@@ -151,43 +166,20 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
151166
152167    // Copy the source files to the sysroot (Rust for Linux needs this). 
153168    let  sysroot_src_path = start_dir. join ( "sysroot/lib/rustlib/src/rust" ) ; 
154-     fs:: create_dir_all ( & sysroot_src_path) . map_err ( |error| { 
155-         format ! ( 
156-             "Failed to create directory `{}`: {:?}" , 
157-             sysroot_src_path. display( ) , 
158-             error
159-         ) 
160-     } ) ?; 
161-     run_command ( 
162-         & [ 
163-             & "cp" , 
164-             & "-r" , 
165-             & start_dir. join ( "sysroot_src/library/" ) , 
166-             & sysroot_src_path, 
167-         ] , 
168-         None , 
169-     ) ?; 
169+     create_dir ( & sysroot_src_path) ?; 
170+     run_command ( & [ & "cp" ,  & "-r" ,  & start_dir. join ( "sysroot_src/library/" ) ,  & sysroot_src_path] ,  None ) ?; 
170171
171172    Ok ( ( ) ) 
172173} 
173174
174175fn  build_codegen ( args :  & mut  BuildArg )  -> Result < ( ) ,  String >  { 
175176    let  mut  env = HashMap :: new ( ) ; 
176177
177-     env. insert ( 
178-         "LD_LIBRARY_PATH" . to_string ( ) , 
179-         args. config_info . gcc_path . clone ( ) , 
180-     ) ; 
181-     env. insert ( 
182-         "LIBRARY_PATH" . to_string ( ) , 
183-         args. config_info . gcc_path . clone ( ) , 
184-     ) ; 
178+     env. insert ( "LD_LIBRARY_PATH" . to_string ( ) ,  args. config_info . gcc_path . clone ( ) ) ; 
179+     env. insert ( "LIBRARY_PATH" . to_string ( ) ,  args. config_info . gcc_path . clone ( ) ) ; 
185180
186181    if  args. config_info . no_default_features  { 
187-         env. insert ( 
188-             "RUSTFLAGS" . to_string ( ) , 
189-             "-Csymbol-mangling-version=v0" . to_string ( ) , 
190-         ) ; 
182+         env. insert ( "RUSTFLAGS" . to_string ( ) ,  "-Csymbol-mangling-version=v0" . to_string ( ) ) ; 
191183    } 
192184
193185    let  mut  command:  Vec < & dyn  AsRef < OsStr > >  = vec ! [ & "cargo" ,  & "rustc" ] ; 
@@ -212,12 +204,7 @@ fn build_codegen(args: &mut BuildArg) -> Result<(), String> {
212204    // We voluntarily ignore the error. 
213205    let  _ = fs:: remove_dir_all ( "target/out" ) ; 
214206    let  gccjit_target = "target/out/gccjit" ; 
215-     fs:: create_dir_all ( gccjit_target) . map_err ( |error| { 
216-         format ! ( 
217-             "Failed to create directory `{}`: {:?}" , 
218-             gccjit_target,  error
219-         ) 
220-     } ) ?; 
207+     create_dir ( gccjit_target) ?; 
221208
222209    println ! ( "[BUILD] sysroot" ) ; 
223210    build_sysroot ( & env,  & args. config_info ) ?; 
0 commit comments