1
- use std:: env;
2
1
use std:: fs;
3
2
use std:: path:: { Path , PathBuf } ;
4
3
use std:: process:: { self , Command } ;
@@ -22,35 +21,28 @@ pub(crate) fn build_sysroot(
22
21
fs:: create_dir_all ( target_dir. join ( "lib" ) ) . unwrap ( ) ;
23
22
24
23
// 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
-
32
24
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
+ }
54
46
55
47
let default_sysroot = super :: rustc_info:: get_default_sysroot ( ) ;
56
48
@@ -117,7 +109,13 @@ pub(crate) fn build_sysroot(
117
109
}
118
110
}
119
111
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
+ ) ;
121
119
122
120
if host_triple != target_triple {
123
121
// When cross-compiling it is often necessary to manually pick the right linker
@@ -126,14 +124,21 @@ pub(crate) fn build_sysroot(
126
124
} else {
127
125
None
128
126
} ;
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
+ ) ;
130
134
}
131
135
132
136
// Copy std for the host to the lib dir. This is necessary for the jit mode to find
133
137
// libstd.
134
138
for file in fs:: read_dir ( host_rustlib_lib) . unwrap ( ) {
135
139
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" ) {
137
142
try_hard_link ( & file, target_dir. join ( "lib" ) . join ( file. file_name ( ) . unwrap ( ) ) ) ;
138
143
}
139
144
}
@@ -145,6 +150,7 @@ fn build_clif_sysroot_for_triple(
145
150
channel : & str ,
146
151
target_dir : & Path ,
147
152
triple : & str ,
153
+ cg_clif_dylib_path : & Path ,
148
154
linker : Option < & str > ,
149
155
) {
150
156
match fs:: read_to_string ( Path :: new ( "build_sysroot" ) . join ( "rustc_version" ) ) {
@@ -168,18 +174,18 @@ fn build_clif_sysroot_for_triple(
168
174
let build_dir = Path :: new ( "build_sysroot" ) . join ( "target" ) . join ( triple) . join ( channel) ;
169
175
170
176
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 ( ) ;
176
181
}
177
182
}
178
183
179
184
// Build sysroot
180
185
let mut build_cmd = Command :: new ( "cargo" ) ;
181
186
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( ) ) ) ;
183
189
if channel == "release" {
184
190
build_cmd. arg ( "--release" ) ;
185
191
rustflags. push_str ( " -Zmir-opt-level=3" ) ;
@@ -189,10 +195,6 @@ fn build_clif_sysroot_for_triple(
189
195
write ! ( rustflags, " -Clinker={}" , linker) . unwrap ( ) ;
190
196
}
191
197
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
- ) ;
196
198
build_cmd. env ( "__CARGO_DEFAULT_LIB_METADATA" , "cg_clif" ) ;
197
199
spawn_and_wait ( build_cmd) ;
198
200
0 commit comments