1- use std:: env;
21use std:: fs;
32use std:: path:: { Path , PathBuf } ;
43use std:: process:: { self , Command } ;
@@ -22,35 +21,28 @@ pub(crate) fn build_sysroot(
2221 fs:: create_dir_all ( target_dir. join ( "lib" ) ) . unwrap ( ) ;
2322
2423 // Copy the backend
25- for file in [ "cg_clif" , "cg_clif_build_sysroot" ] {
26- try_hard_link (
27- cg_clif_build_dir. join ( get_file_name ( file, "bin" ) ) ,
28- target_dir. join ( "bin" ) . join ( get_file_name ( file, "bin" ) ) ,
29- ) ;
30- }
31-
3224 let cg_clif_dylib = get_file_name ( "rustc_codegen_cranelift" , "dylib" ) ;
33- try_hard_link (
34- cg_clif_build_dir . join ( & cg_clif_dylib ) ,
35- target_dir
36- . join ( if cfg ! ( windows ) {
37- // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
38- // binaries.
39- "bin "
40- } else {
41- "lib"
42- } )
43- . join ( cg_clif_dylib ) ,
44- ) ;
45-
46- // Build and copy cargo wrapper
47- let mut build_cargo_wrapper_cmd = Command :: new ( "rustc" ) ;
48- build_cargo_wrapper_cmd
49- . arg ( "scripts/cargo-clif.rs " )
50- . arg ( "-o" )
51- . arg ( target_dir . join ( "cargo-clif" ) )
52- . arg ( "-g" ) ;
53- spawn_and_wait ( build_cargo_wrapper_cmd ) ;
25+ let cg_clif_dylib_path = target_dir
26+ . join ( if cfg ! ( windows ) {
27+ // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
28+ // binaries.
29+ "bin"
30+ } else {
31+ "lib "
32+ } )
33+ . join ( & cg_clif_dylib ) ;
34+ try_hard_link ( cg_clif_build_dir . join ( cg_clif_dylib ) , & cg_clif_dylib_path ) ;
35+
36+ // Build and copy rustc and cargo wrappers
37+ for wrapper in [ "rustc-clif" , "cargo-clif" ] {
38+ let mut build_cargo_wrapper_cmd = Command :: new ( "rustc" ) ;
39+ build_cargo_wrapper_cmd
40+ . arg ( PathBuf :: from ( "scripts" ) . join ( format ! ( "{wrapper}.rs" ) ) )
41+ . arg ( "-o " )
42+ . arg ( target_dir . join ( wrapper ) )
43+ . arg ( "-g" ) ;
44+ spawn_and_wait ( build_cargo_wrapper_cmd ) ;
45+ }
5446
5547 let default_sysroot = super :: rustc_info:: get_default_sysroot ( ) ;
5648
@@ -117,7 +109,13 @@ pub(crate) fn build_sysroot(
117109 }
118110 }
119111 SysrootKind :: Clif => {
120- build_clif_sysroot_for_triple ( channel, target_dir, host_triple, None ) ;
112+ build_clif_sysroot_for_triple (
113+ channel,
114+ target_dir,
115+ host_triple,
116+ & cg_clif_dylib_path,
117+ None ,
118+ ) ;
121119
122120 if host_triple != target_triple {
123121 // When cross-compiling it is often necessary to manually pick the right linker
@@ -126,14 +124,21 @@ pub(crate) fn build_sysroot(
126124 } else {
127125 None
128126 } ;
129- build_clif_sysroot_for_triple ( channel, target_dir, target_triple, linker) ;
127+ build_clif_sysroot_for_triple (
128+ channel,
129+ target_dir,
130+ target_triple,
131+ & cg_clif_dylib_path,
132+ linker,
133+ ) ;
130134 }
131135
132136 // Copy std for the host to the lib dir. This is necessary for the jit mode to find
133137 // libstd.
134138 for file in fs:: read_dir ( host_rustlib_lib) . unwrap ( ) {
135139 let file = file. unwrap ( ) . path ( ) ;
136- if file. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . contains ( "std-" ) {
140+ let filename = file. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
141+ if filename. contains ( "std-" ) && !filename. contains ( ".rlib" ) {
137142 try_hard_link ( & file, target_dir. join ( "lib" ) . join ( file. file_name ( ) . unwrap ( ) ) ) ;
138143 }
139144 }
@@ -145,6 +150,7 @@ fn build_clif_sysroot_for_triple(
145150 channel : & str ,
146151 target_dir : & Path ,
147152 triple : & str ,
153+ cg_clif_dylib_path : & Path ,
148154 linker : Option < & str > ,
149155) {
150156 match fs:: read_to_string ( Path :: new ( "build_sysroot" ) . join ( "rustc_version" ) ) {
@@ -168,18 +174,18 @@ fn build_clif_sysroot_for_triple(
168174 let build_dir = Path :: new ( "build_sysroot" ) . join ( "target" ) . join ( triple) . join ( channel) ;
169175
170176 if !super :: config:: get_bool ( "keep_sysroot" ) {
171- // Cleanup the target dir with the exception of build scripts and the incremental cache
172- for dir in [ "build" , "deps" , "examples" , "native" ] {
173- if build_dir. join ( dir) . exists ( ) {
174- fs:: remove_dir_all ( build_dir. join ( dir) ) . unwrap ( ) ;
175- }
177+ // Cleanup the deps dir, but keep build scripts and the incremental cache for faster
178+ // recompilation as they are not affected by changes in cg_clif.
179+ if build_dir. join ( "deps" ) . exists ( ) {
180+ fs:: remove_dir_all ( build_dir. join ( "deps" ) ) . unwrap ( ) ;
176181 }
177182 }
178183
179184 // Build sysroot
180185 let mut build_cmd = Command :: new ( "cargo" ) ;
181186 build_cmd. arg ( "build" ) . arg ( "--target" ) . arg ( triple) . current_dir ( "build_sysroot" ) ;
182- let mut rustflags = "--clif -Zforce-unstable-if-unmarked" . to_string ( ) ;
187+ let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort" . to_string ( ) ;
188+ rustflags. push_str ( & format ! ( " -Zcodegen-backend={}" , cg_clif_dylib_path. to_str( ) . unwrap( ) ) ) ;
183189 if channel == "release" {
184190 build_cmd. arg ( "--release" ) ;
185191 rustflags. push_str ( " -Zmir-opt-level=3" ) ;
@@ -189,10 +195,6 @@ fn build_clif_sysroot_for_triple(
189195 write ! ( rustflags, " -Clinker={}" , linker) . unwrap ( ) ;
190196 }
191197 build_cmd. env ( "RUSTFLAGS" , rustflags) ;
192- build_cmd. env (
193- "RUSTC" ,
194- env:: current_dir ( ) . unwrap ( ) . join ( target_dir) . join ( "bin" ) . join ( "cg_clif_build_sysroot" ) ,
195- ) ;
196198 build_cmd. env ( "__CARGO_DEFAULT_LIB_METADATA" , "cg_clif" ) ;
197199 spawn_and_wait ( build_cmd) ;
198200
0 commit comments