@@ -4,7 +4,7 @@ use crate::utils::{cargo_install, git_clone, run_command, run_command_with_outpu
44use std:: fs;
55use std:: path:: Path ;
66
7- fn prepare_libcore ( ) -> Result < ( ) , String > {
7+ fn prepare_libcore ( sysroot_path : & Path ) -> Result < ( ) , String > {
88 let rustc_path = match get_rustc_path ( ) {
99 Some ( path) => path,
1010 None => return Err ( "`rustc` path not found" . to_owned ( ) ) ,
@@ -15,14 +15,18 @@ fn prepare_libcore() -> Result<(), String> {
1515 None => return Err ( format ! ( "No parent for `{}`" , rustc_path. display( ) ) ) ,
1616 } ;
1717
18- let rustlib_dir = parent. join ( "../lib/rustlib/src/rust" ) ;
18+ let rustlib_dir =
19+ parent
20+ . join ( "../lib/rustlib/src/rust" )
21+ . canonicalize ( )
22+ . map_err ( |e| format ! ( "Failed to canonicalize path: {e:?}" ) ) ?;
1923 if !rustlib_dir. is_dir ( ) {
2024 return Err ( "Please install `rust-src` component" . to_owned ( ) ) ;
2125 }
2226
23- let sysroot_dir = Path :: new ( "build_sysroot/ sysroot_src") ;
27+ let sysroot_dir = sysroot_path . join ( " sysroot_src") ;
2428 if sysroot_dir. is_dir ( ) {
25- if let Err ( e) = fs:: remove_dir_all ( sysroot_dir) {
29+ if let Err ( e) = fs:: remove_dir_all ( & sysroot_dir) {
2630 return Err ( format ! ( "Failed to remove `{}`: {:?}" , sysroot_dir. display( ) , e) ) ;
2731 }
2832 }
@@ -34,24 +38,30 @@ fn prepare_libcore() -> Result<(), String> {
3438 sysroot_library_dir. display( ) ,
3539 ) ) ?;
3640
37- run_command ( & [ & "cp" , & "-r" , & rustlib_dir, & sysroot_library_dir ] , None ) ?;
41+ run_command ( & [ & "cp" , & "-r" , & rustlib_dir. join ( "library" ) , & sysroot_dir ] , None ) ?;
3842
3943 println ! ( "[GIT] init (cwd): `{}`" , sysroot_dir. display( ) ) ;
40- run_command_with_output ( & [ & "git" , & "init" ] , Some ( & sysroot_dir) ) ?;
44+ run_command ( & [ & "git" , & "init" ] , Some ( & sysroot_dir) ) ?;
4145 println ! ( "[GIT] add (cwd): `{}`" , sysroot_dir. display( ) ) ;
42- run_command_with_output ( & [ & "git" , & "add" , & "." ] , Some ( & sysroot_dir) ) ?;
46+ run_command ( & [ & "git" , & "add" , & "." ] , Some ( & sysroot_dir) ) ?;
4347 println ! ( "[GIT] commit (cwd): `{}`" , sysroot_dir. display( ) ) ;
4448
4549 // This is needed on systems where nothing is configured.
4650 // git really needs something here, or it will fail.
4751 // Even using --author is not enough.
4852 run_command ( & [ & "git" , & "config" , & "user.email" , & "none@example.com" ] , Some ( & sysroot_dir) ) ?;
4953 run_command ( & [ & "git" , & "config" , & "user.name" , & "None" ] , Some ( & sysroot_dir) ) ?;
50- run_command ( & [ & "git" , & "config" , & "core.autocrlf= false" ] , Some ( & sysroot_dir) ) ?;
51- run_command ( & [ & "git" , & "config" , & "commit.gpgSign= false" ] , Some ( & sysroot_dir) ) ?;
54+ run_command ( & [ & "git" , & "config" , & "core.autocrlf" , & " false"] , Some ( & sysroot_dir) ) ?;
55+ run_command ( & [ & "git" , & "config" , & "commit.gpgSign" , & " false"] , Some ( & sysroot_dir) ) ?;
5256 run_command ( & [ & "git" , & "commit" , & "-m" , & "Initial commit" , & "-q" ] , Some ( & sysroot_dir) ) ?;
5357
58+ let mut patches = Vec :: new ( ) ;
5459 walk_dir ( "patches" , |_| Ok ( ( ) ) , |file_path : & Path | {
60+ patches. push ( file_path. to_path_buf ( ) ) ;
61+ Ok ( ( ) )
62+ } ) ?;
63+ patches. sort ( ) ;
64+ for file_path in patches {
5565 println ! ( "[GIT] apply `{}`" , file_path. display( ) ) ;
5666 let path = Path :: new ( "../.." ) . join ( file_path) ;
5767 run_command_with_output ( & [ & "git" , & "apply" , & path] , Some ( & sysroot_dir) ) ?;
@@ -60,15 +70,19 @@ fn prepare_libcore() -> Result<(), String> {
6070 & [ & "git" , & "commit" , & "--no-gpg-sign" , & "-m" , & format ! ( "Patch {}" , path. display( ) ) ] ,
6171 Some ( & sysroot_dir) ,
6272 ) ?;
63- Ok ( ( ) )
64- } ) ?;
73+ }
6574 println ! ( "Successfully prepared libcore for building" ) ;
6675 Ok ( ( ) )
6776}
6877
6978// build with cg_llvm for perf comparison
7079fn build_raytracer ( repo_dir : & Path ) -> Result < ( ) , String > {
7180 run_command ( & [ & "cargo" , & "build" ] , Some ( repo_dir) ) ?;
81+ let mv_target = repo_dir. join ( "raytracer_cg_llvm" ) ;
82+ if mv_target. is_file ( ) {
83+ std:: fs:: remove_file ( & mv_target)
84+ . map_err ( |e| format ! ( "Failed to remove file `{}`: {e:?}" , mv_target. display( ) ) ) ?;
85+ }
7286 run_command ( & [ & "mv" , & "target/debug/main" , & "raytracer_cg_llvm" ] , Some ( repo_dir) ) ?;
7387 Ok ( ( ) )
7488}
@@ -82,18 +96,21 @@ where
8296 println ! ( "`{}` has already been cloned" , clone_result. repo_name) ;
8397 }
8498 let repo_path = Path :: new ( & clone_result. repo_name ) ;
85- run_command ( & [ & "git" , & "checkout" , & "--" , & "." ] , Some ( repo_path) ) ?;
86- run_command ( & [ & "git" , & "checkout" , & checkout_commit] , Some ( repo_path) ) ?;
99+ run_command ( & [ & "git" , & "checkout" , & "--" , & "." ] , Some ( & repo_path) ) ?;
100+ run_command ( & [ & "git" , & "checkout" , & checkout_commit] , Some ( & repo_path) ) ?;
87101 let filter = format ! ( "-{}-" , clone_result. repo_name) ;
88102 walk_dir ( "crate_patches" , |_| Ok ( ( ) ) , |file_path| {
89103 let s = file_path. as_os_str ( ) . to_str ( ) . unwrap ( ) ;
90104 if s. contains ( & filter) && s. ends_with ( ".patch" ) {
91- run_command ( & [ & "git" , & "am" , & s] , Some ( repo_path) ) ?;
105+ run_command_with_output (
106+ & [ & "git" , & "am" , & file_path. canonicalize ( ) . unwrap ( ) ] ,
107+ Some ( & repo_path) ,
108+ ) ?;
92109 }
93110 Ok ( ( ) )
94111 } ) ?;
95112 if let Some ( extra) = extra {
96- extra ( repo_path) ?;
113+ extra ( & repo_path) ?;
97114 }
98115 Ok ( ( ) )
99116}
@@ -136,7 +153,8 @@ pub fn run() -> Result<(), String> {
136153 Some ( a) => a,
137154 None => return Ok ( ( ) ) ,
138155 } ;
139- prepare_libcore ( ) ?;
156+ let sysroot_path = Path :: new ( "build_sysroot" ) ;
157+ prepare_libcore ( sysroot_path) ?;
140158
141159 if !args. only_libcore {
142160 cargo_install ( "hyperfine" ) ?;
0 commit comments