11use std:: fs;
2- use std:: path:: { Path , PathBuf } ;
2+ use std:: path:: Path ;
33use std:: process:: { self , Command } ;
44
5+ use super :: path:: { Dirs , RelPath } ;
56use super :: rustc_info:: { get_file_name, get_rustc_version, get_wrapper_file_name} ;
67use super :: utils:: { spawn_and_wait, try_hard_link, CargoProject , Compiler } ;
78use super :: SysrootKind ;
89
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+
915pub ( crate ) fn build_sysroot (
16+ dirs : & Dirs ,
1017 channel : & str ,
1118 sysroot_kind : SysrootKind ,
12- dist_dir : & Path ,
1319 cg_clif_dylib_src : & Path ,
1420 host_triple : & str ,
1521 target_triple : & str ,
1622) {
1723 eprintln ! ( "[BUILD] sysroot {:?}" , sysroot_kind) ;
1824
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) ;
2428
2529 // 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" ) ) ;
3539 try_hard_link ( cg_clif_dylib_src, & cg_clif_dylib_path) ;
3640
3741 // Build and copy rustc and cargo wrappers
@@ -40,18 +44,17 @@ pub(crate) fn build_sysroot(
4044
4145 let mut build_cargo_wrapper_cmd = Command :: new ( "rustc" ) ;
4246 build_cargo_wrapper_cmd
43- . arg ( PathBuf :: from ( "scripts" ) . join ( format ! ( "{wrapper}.rs" ) ) )
47+ . arg ( RelPath :: SCRIPTS . to_path ( dirs ) . join ( & format ! ( "{wrapper}.rs" ) ) )
4448 . arg ( "-o" )
45- . arg ( dist_dir . join ( wrapper_name) )
49+ . arg ( DIST_DIR . to_path ( dirs ) . join ( wrapper_name) )
4650 . arg ( "-g" ) ;
4751 spawn_and_wait ( build_cargo_wrapper_cmd) ;
4852 }
4953
5054 let default_sysroot = super :: rustc_info:: get_default_sysroot ( ) ;
5155
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" ) ;
5558 fs:: create_dir_all ( & host_rustlib_lib) . unwrap ( ) ;
5659 fs:: create_dir_all ( & target_rustlib_lib) . unwrap ( ) ;
5760
@@ -112,13 +115,7 @@ pub(crate) fn build_sysroot(
112115 }
113116 }
114117 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 ) ;
122119
123120 if host_triple != target_triple {
124121 // When cross-compiling it is often necessary to manually pick the right linker
@@ -128,8 +125,8 @@ pub(crate) fn build_sysroot(
128125 None
129126 } ;
130127 build_clif_sysroot_for_triple (
128+ dirs,
131129 channel,
132- dist_dir,
133130 target_triple,
134131 & cg_clif_dylib_path,
135132 linker,
@@ -142,23 +139,26 @@ pub(crate) fn build_sysroot(
142139 let file = file. unwrap ( ) . path ( ) ;
143140 let filename = file. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
144141 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 ( ) ) ) ;
146143 }
147144 }
148145 }
149146 }
150147}
151148
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" ) ;
153153
154154fn build_clif_sysroot_for_triple (
155+ dirs : & Dirs ,
155156 channel : & str ,
156- dist_dir : & Path ,
157157 triple : & str ,
158158 cg_clif_dylib_path : & Path ,
159159 linker : Option < & str > ,
160160) {
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 ) ) {
162162 Err ( e) => {
163163 eprintln ! ( "Failed to get rustc version for patched sysroot source: {}" , e) ;
164164 eprintln ! ( "Hint: Try `./y.rs prepare` to patch the sysroot source" ) ;
@@ -176,7 +176,7 @@ fn build_clif_sysroot_for_triple(
176176 }
177177 }
178178
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) ;
180180
181181 if !super :: config:: get_bool ( "keep_sysroot" ) {
182182 // Cleanup the deps dir, but keep build scripts and the incremental cache for faster
@@ -189,7 +189,7 @@ fn build_clif_sysroot_for_triple(
189189 // Build sysroot
190190 let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort" . to_string ( ) ;
191191 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( ) ) ) ;
193193 if channel == "release" {
194194 rustflags. push_str ( " -Zmir-opt-level=3" ) ;
195195 }
@@ -199,18 +199,15 @@ fn build_clif_sysroot_for_triple(
199199 }
200200 let mut compiler = Compiler :: with_triple ( triple. to_owned ( ) ) ;
201201 compiler. rustflags = rustflags;
202- let mut build_cmd = STANDARD_LIBRARY . build ( & compiler) ;
202+ let mut build_cmd = STANDARD_LIBRARY . build ( & compiler, dirs ) ;
203203 if channel == "release" {
204204 build_cmd. arg ( "--release" ) ;
205205 }
206206 build_cmd. env ( "__CARGO_DEFAULT_LIB_METADATA" , "cg_clif" ) ;
207207 spawn_and_wait ( build_cmd) ;
208208
209209 // 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 ( ) {
214211 let entry = entry. unwrap ( ) ;
215212 if let Some ( ext) = entry. path ( ) . extension ( ) {
216213 if ext == "rmeta" || ext == "d" || ext == "dSYM" || ext == "clif" {
@@ -221,7 +218,7 @@ fn build_clif_sysroot_for_triple(
221218 } ;
222219 try_hard_link (
223220 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 ( ) ) ,
225222 ) ;
226223 }
227224}
0 commit comments