1
1
use std:: fs;
2
- use std:: path:: { Path , PathBuf } ;
2
+ use std:: path:: Path ;
3
3
use std:: process:: { self , Command } ;
4
4
5
+ use super :: path:: { Dirs , RelPath } ;
5
6
use super :: rustc_info:: { get_file_name, get_rustc_version, get_wrapper_file_name} ;
6
7
use super :: utils:: { spawn_and_wait, try_hard_link, CargoProject , Compiler } ;
7
8
use super :: SysrootKind ;
8
9
10
+ static DIST_DIR : RelPath = RelPath :: DIST ;
11
+ static BIN_DIR : RelPath = RelPath :: DIST . join ( "bin" ) ;
12
+ static LIB_DIR : RelPath = RelPath :: DIST . join ( "lib" ) ;
13
+ static RUSTLIB_DIR : RelPath = LIB_DIR . join ( "rustlib" ) ;
14
+
9
15
pub ( crate ) fn build_sysroot (
16
+ dirs : & Dirs ,
10
17
channel : & str ,
11
18
sysroot_kind : SysrootKind ,
12
- dist_dir : & Path ,
13
19
cg_clif_dylib_src : & Path ,
14
20
host_triple : & str ,
15
21
target_triple : & str ,
16
22
) {
17
23
eprintln ! ( "[BUILD] sysroot {:?}" , sysroot_kind) ;
18
24
19
- if dist_dir. exists ( ) {
20
- fs:: remove_dir_all ( dist_dir) . unwrap ( ) ;
21
- }
22
- fs:: create_dir_all ( dist_dir. join ( "bin" ) ) . unwrap ( ) ;
23
- fs:: create_dir_all ( dist_dir. join ( "lib" ) ) . unwrap ( ) ;
25
+ DIST_DIR . ensure_fresh ( dirs) ;
26
+ BIN_DIR . ensure_exists ( dirs) ;
27
+ LIB_DIR . ensure_exists ( dirs) ;
24
28
25
29
// Copy the backend
26
- let cg_clif_dylib_path = dist_dir
27
- . join ( if cfg ! ( windows ) {
28
- // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
29
- // binaries.
30
- "bin"
31
- } else {
32
- "lib"
33
- } )
34
- . join ( get_file_name ( "rustc_codegen_cranelift" , "dylib" ) ) ;
30
+ let cg_clif_dylib_path = if cfg ! ( windows ) {
31
+ // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
32
+ // binaries.
33
+ BIN_DIR
34
+ } else {
35
+ LIB_DIR
36
+ }
37
+ . to_path ( dirs )
38
+ . join ( get_file_name ( "rustc_codegen_cranelift" , "dylib" ) ) ;
35
39
try_hard_link ( cg_clif_dylib_src, & cg_clif_dylib_path) ;
36
40
37
41
// Build and copy rustc and cargo wrappers
@@ -40,18 +44,17 @@ pub(crate) fn build_sysroot(
40
44
41
45
let mut build_cargo_wrapper_cmd = Command :: new ( "rustc" ) ;
42
46
build_cargo_wrapper_cmd
43
- . arg ( PathBuf :: from ( "scripts" ) . join ( format ! ( "{wrapper}.rs" ) ) )
47
+ . arg ( RelPath :: SCRIPTS . to_path ( dirs ) . join ( & format ! ( "{wrapper}.rs" ) ) )
44
48
. arg ( "-o" )
45
- . arg ( dist_dir . join ( wrapper_name) )
49
+ . arg ( DIST_DIR . to_path ( dirs ) . join ( wrapper_name) )
46
50
. arg ( "-g" ) ;
47
51
spawn_and_wait ( build_cargo_wrapper_cmd) ;
48
52
}
49
53
50
54
let default_sysroot = super :: rustc_info:: get_default_sysroot ( ) ;
51
55
52
- let rustlib = dist_dir. join ( "lib" ) . join ( "rustlib" ) ;
53
- let host_rustlib_lib = rustlib. join ( host_triple) . join ( "lib" ) ;
54
- let target_rustlib_lib = rustlib. join ( target_triple) . join ( "lib" ) ;
56
+ let host_rustlib_lib = RUSTLIB_DIR . to_path ( dirs) . join ( host_triple) . join ( "lib" ) ;
57
+ let target_rustlib_lib = RUSTLIB_DIR . to_path ( dirs) . join ( target_triple) . join ( "lib" ) ;
55
58
fs:: create_dir_all ( & host_rustlib_lib) . unwrap ( ) ;
56
59
fs:: create_dir_all ( & target_rustlib_lib) . unwrap ( ) ;
57
60
@@ -112,13 +115,7 @@ pub(crate) fn build_sysroot(
112
115
}
113
116
}
114
117
SysrootKind :: Clif => {
115
- build_clif_sysroot_for_triple (
116
- channel,
117
- dist_dir,
118
- host_triple,
119
- & cg_clif_dylib_path,
120
- None ,
121
- ) ;
118
+ build_clif_sysroot_for_triple ( dirs, channel, host_triple, & cg_clif_dylib_path, None ) ;
122
119
123
120
if host_triple != target_triple {
124
121
// When cross-compiling it is often necessary to manually pick the right linker
@@ -128,8 +125,8 @@ pub(crate) fn build_sysroot(
128
125
None
129
126
} ;
130
127
build_clif_sysroot_for_triple (
128
+ dirs,
131
129
channel,
132
- dist_dir,
133
130
target_triple,
134
131
& cg_clif_dylib_path,
135
132
linker,
@@ -142,23 +139,26 @@ pub(crate) fn build_sysroot(
142
139
let file = file. unwrap ( ) . path ( ) ;
143
140
let filename = file. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
144
141
if filename. contains ( "std-" ) && !filename. contains ( ".rlib" ) {
145
- try_hard_link ( & file, dist_dir . join ( "lib" ) . join ( file. file_name ( ) . unwrap ( ) ) ) ;
142
+ try_hard_link ( & file, LIB_DIR . to_path ( dirs ) . join ( file. file_name ( ) . unwrap ( ) ) ) ;
146
143
}
147
144
}
148
145
}
149
146
}
150
147
}
151
148
152
- static STANDARD_LIBRARY : CargoProject = CargoProject :: local ( "build_sysroot" ) ;
149
+ // FIXME move to download/ or dist/
150
+ pub ( crate ) static SYSROOT_RUSTC_VERSION : RelPath = RelPath :: BUILD_SYSROOT . join ( "rustc_version" ) ;
151
+ pub ( crate ) static SYSROOT_SRC : RelPath = RelPath :: BUILD_SYSROOT . join ( "sysroot_src" ) ;
152
+ static STANDARD_LIBRARY : CargoProject = CargoProject :: new ( & RelPath :: BUILD_SYSROOT , "build_sysroot" ) ;
153
153
154
154
fn build_clif_sysroot_for_triple (
155
+ dirs : & Dirs ,
155
156
channel : & str ,
156
- dist_dir : & Path ,
157
157
triple : & str ,
158
158
cg_clif_dylib_path : & Path ,
159
159
linker : Option < & str > ,
160
160
) {
161
- match fs:: read_to_string ( Path :: new ( "build_sysroot" ) . join ( "rustc_version" ) ) {
161
+ match fs:: read_to_string ( SYSROOT_RUSTC_VERSION . to_path ( dirs ) ) {
162
162
Err ( e) => {
163
163
eprintln ! ( "Failed to get rustc version for patched sysroot source: {}" , e) ;
164
164
eprintln ! ( "Hint: Try `./y.rs prepare` to patch the sysroot source" ) ;
@@ -176,7 +176,7 @@ fn build_clif_sysroot_for_triple(
176
176
}
177
177
}
178
178
179
- let build_dir = Path :: new ( "build_sysroot" ) . join ( "target" ) . join ( triple) . join ( channel) ;
179
+ let build_dir = STANDARD_LIBRARY . target_dir ( dirs ) . join ( triple) . join ( channel) ;
180
180
181
181
if !super :: config:: get_bool ( "keep_sysroot" ) {
182
182
// Cleanup the deps dir, but keep build scripts and the incremental cache for faster
@@ -189,7 +189,7 @@ fn build_clif_sysroot_for_triple(
189
189
// Build sysroot
190
190
let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort" . to_string ( ) ;
191
191
rustflags. push_str ( & format ! ( " -Zcodegen-backend={}" , cg_clif_dylib_path. to_str( ) . unwrap( ) ) ) ;
192
- rustflags. push_str ( & format ! ( " --sysroot={}" , dist_dir . to_str( ) . unwrap( ) ) ) ;
192
+ rustflags. push_str ( & format ! ( " --sysroot={}" , DIST_DIR . to_path ( dirs ) . to_str( ) . unwrap( ) ) ) ;
193
193
if channel == "release" {
194
194
rustflags. push_str ( " -Zmir-opt-level=3" ) ;
195
195
}
@@ -199,18 +199,15 @@ fn build_clif_sysroot_for_triple(
199
199
}
200
200
let mut compiler = Compiler :: with_triple ( triple. to_owned ( ) ) ;
201
201
compiler. rustflags = rustflags;
202
- let mut build_cmd = STANDARD_LIBRARY . build ( & compiler) ;
202
+ let mut build_cmd = STANDARD_LIBRARY . build ( & compiler, dirs ) ;
203
203
if channel == "release" {
204
204
build_cmd. arg ( "--release" ) ;
205
205
}
206
206
build_cmd. env ( "__CARGO_DEFAULT_LIB_METADATA" , "cg_clif" ) ;
207
207
spawn_and_wait ( build_cmd) ;
208
208
209
209
// Copy all relevant files to the sysroot
210
- for entry in
211
- fs:: read_dir ( Path :: new ( "build_sysroot/target" ) . join ( triple) . join ( channel) . join ( "deps" ) )
212
- . unwrap ( )
213
- {
210
+ for entry in fs:: read_dir ( build_dir. join ( "deps" ) ) . unwrap ( ) {
214
211
let entry = entry. unwrap ( ) ;
215
212
if let Some ( ext) = entry. path ( ) . extension ( ) {
216
213
if ext == "rmeta" || ext == "d" || ext == "dSYM" || ext == "clif" {
@@ -221,7 +218,7 @@ fn build_clif_sysroot_for_triple(
221
218
} ;
222
219
try_hard_link (
223
220
entry. path ( ) ,
224
- dist_dir . join ( "lib" ) . join ( "rustlib" ) . join ( triple) . join ( "lib" ) . join ( entry. file_name ( ) ) ,
221
+ RUSTLIB_DIR . to_path ( dirs ) . join ( triple) . join ( "lib" ) . join ( entry. file_name ( ) ) ,
225
222
) ;
226
223
}
227
224
}
0 commit comments