@@ -2,36 +2,39 @@ use std::fs;
22use std:: path:: Path ;
33use std:: process:: { self , Command } ;
44
5+ use super :: path:: 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 (
1016 channel : & str ,
1117 sysroot_kind : SysrootKind ,
12- dist_dir : & Path ,
1318 cg_clif_dylib_src : & Path ,
1419 host_triple : & str ,
1520 target_triple : & str ,
1621) {
1722 eprintln ! ( "[BUILD] sysroot {:?}" , sysroot_kind) ;
1823
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 ( ) ;
24+ DIST_DIR . ensure_fresh ( ) ;
25+ BIN_DIR . ensure_exists ( ) ;
26+ LIB_DIR . ensure_exists ( ) ;
2427
2528 // 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" ) ) ;
29+ let cg_clif_dylib_path = if cfg ! ( windows ) {
30+ // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
31+ // binaries.
32+ BIN_DIR
33+ } else {
34+ LIB_DIR
35+ }
36+ . to_path ( )
37+ . join ( get_file_name ( "rustc_codegen_cranelift" , "dylib" ) ) ;
3538 try_hard_link ( cg_clif_dylib_src, & cg_clif_dylib_path) ;
3639
3740 // Build and copy rustc and cargo wrappers
@@ -40,18 +43,17 @@ pub(crate) fn build_sysroot(
4043
4144 let mut build_cargo_wrapper_cmd = Command :: new ( "rustc" ) ;
4245 build_cargo_wrapper_cmd
43- . arg ( Path :: new ( "scripts" ) . join ( format ! ( "{wrapper}.rs" ) ) )
46+ . arg ( RelPath :: SCRIPTS . to_path ( ) . join ( & format ! ( "{wrapper}.rs" ) ) )
4447 . arg ( "-o" )
45- . arg ( dist_dir . join ( wrapper_name) )
48+ . arg ( DIST_DIR . to_path ( ) . join ( wrapper_name) )
4649 . arg ( "-g" ) ;
4750 spawn_and_wait ( build_cargo_wrapper_cmd) ;
4851 }
4952
5053 let default_sysroot = super :: rustc_info:: get_default_sysroot ( ) ;
5154
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" ) ;
55+ let host_rustlib_lib = RUSTLIB_DIR . to_path ( ) . join ( host_triple) . join ( "lib" ) ;
56+ let target_rustlib_lib = RUSTLIB_DIR . to_path ( ) . join ( target_triple) . join ( "lib" ) ;
5557 fs:: create_dir_all ( & host_rustlib_lib) . unwrap ( ) ;
5658 fs:: create_dir_all ( & target_rustlib_lib) . unwrap ( ) ;
5759
@@ -112,13 +114,7 @@ pub(crate) fn build_sysroot(
112114 }
113115 }
114116 SysrootKind :: Clif => {
115- build_clif_sysroot_for_triple (
116- channel,
117- dist_dir,
118- host_triple,
119- & cg_clif_dylib_path,
120- None ,
121- ) ;
117+ build_clif_sysroot_for_triple ( channel, host_triple, & cg_clif_dylib_path, None ) ;
122118
123119 if host_triple != target_triple {
124120 // When cross-compiling it is often necessary to manually pick the right linker
@@ -127,13 +123,7 @@ pub(crate) fn build_sysroot(
127123 } else {
128124 None
129125 } ;
130- build_clif_sysroot_for_triple (
131- channel,
132- dist_dir,
133- target_triple,
134- & cg_clif_dylib_path,
135- linker,
136- ) ;
126+ build_clif_sysroot_for_triple ( channel, target_triple, & cg_clif_dylib_path, linker) ;
137127 }
138128
139129 // Copy std for the host to the lib dir. This is necessary for the jit mode to find
@@ -142,23 +132,25 @@ pub(crate) fn build_sysroot(
142132 let file = file. unwrap ( ) . path ( ) ;
143133 let filename = file. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
144134 if filename. contains ( "std-" ) && !filename. contains ( ".rlib" ) {
145- try_hard_link ( & file, dist_dir . join ( "lib" ) . join ( file. file_name ( ) . unwrap ( ) ) ) ;
135+ try_hard_link ( & file, LIB_DIR . to_path ( ) . join ( file. file_name ( ) . unwrap ( ) ) ) ;
146136 }
147137 }
148138 }
149139 }
150140}
151141
152- static STANDARD_LIBRARY : CargoProject = CargoProject :: local ( "build_sysroot" , "build_sysroot" ) ;
142+ // FIXME move to download/ or dist/
143+ pub ( crate ) static SYSROOT_RUSTC_VERSION : RelPath = RelPath :: BUILD_SYSROOT . join ( "rustc_version" ) ;
144+ pub ( crate ) static SYSROOT_SRC : RelPath = RelPath :: BUILD_SYSROOT . join ( "sysroot_src" ) ;
145+ static STANDARD_LIBRARY : CargoProject = CargoProject :: new ( & RelPath :: BUILD_SYSROOT , "build_sysroot" ) ;
153146
154147fn build_clif_sysroot_for_triple (
155148 channel : & str ,
156- dist_dir : & Path ,
157149 triple : & str ,
158150 cg_clif_dylib_path : & Path ,
159151 linker : Option < & str > ,
160152) {
161- match fs:: read_to_string ( Path :: new ( "build_sysroot" ) . join ( "rustc_version" ) ) {
153+ match fs:: read_to_string ( SYSROOT_RUSTC_VERSION . to_path ( ) ) {
162154 Err ( e) => {
163155 eprintln ! ( "Failed to get rustc version for patched sysroot source: {}" , e) ;
164156 eprintln ! ( "Hint: Try `./y.rs prepare` to patch the sysroot source" ) ;
@@ -189,7 +181,7 @@ fn build_clif_sysroot_for_triple(
189181 // Build sysroot
190182 let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort" . to_string ( ) ;
191183 rustflags. push_str ( & format ! ( " -Zcodegen-backend={}" , cg_clif_dylib_path. to_str( ) . unwrap( ) ) ) ;
192- rustflags. push_str ( & format ! ( " --sysroot={}" , dist_dir . to_str( ) . unwrap( ) ) ) ;
184+ rustflags. push_str ( & format ! ( " --sysroot={}" , DIST_DIR . to_path ( ) . to_str( ) . unwrap( ) ) ) ;
193185 if channel == "release" {
194186 rustflags. push_str ( " -Zmir-opt-level=3" ) ;
195187 }
@@ -218,7 +210,7 @@ fn build_clif_sysroot_for_triple(
218210 } ;
219211 try_hard_link (
220212 entry. path ( ) ,
221- dist_dir . join ( "lib" ) . join ( "rustlib" ) . join ( triple) . join ( "lib" ) . join ( entry. file_name ( ) ) ,
213+ RUSTLIB_DIR . to_path ( ) . join ( triple) . join ( "lib" ) . join ( entry. file_name ( ) ) ,
222214 ) ;
223215 }
224216}
0 commit comments