1
1
use std:: fs;
2
- use std:: path:: Path ;
2
+ use std:: path:: { Path , PathBuf } ;
3
3
use std:: process:: { self , Command } ;
4
4
5
5
use super :: path:: { Dirs , RelPath } ;
@@ -56,35 +56,40 @@ pub(crate) fn build_sysroot(
56
56
spawn_and_wait ( build_cargo_wrapper_cmd) ;
57
57
}
58
58
59
- let default_sysroot = super :: rustc_info:: get_default_sysroot ( & bootstrap_host_compiler. rustc ) ;
60
-
61
- let host_rustlib_lib =
62
- RUSTLIB_DIR . to_path ( dirs) . join ( & bootstrap_host_compiler. triple ) . join ( "lib" ) ;
63
- let target_rustlib_lib = RUSTLIB_DIR . to_path ( dirs) . join ( & target_triple) . join ( "lib" ) ;
64
- fs:: create_dir_all ( & host_rustlib_lib) . unwrap ( ) ;
65
- fs:: create_dir_all ( & target_rustlib_lib) . unwrap ( ) ;
66
-
67
59
if target_triple. ends_with ( "windows-gnu" ) {
68
60
eprintln ! ( "[BUILD] rtstartup for {target_triple}" ) ;
69
61
70
62
let rtstartup_src = SYSROOT_SRC . to_path ( dirs) . join ( "library" ) . join ( "rtstartup" ) ;
63
+ let mut target_libs = SysrootTarget { triple : target_triple. clone ( ) , libs : vec ! [ ] } ;
71
64
72
65
for file in [ "rsbegin" , "rsend" ] {
66
+ let obj = RelPath :: BUILD . to_path ( dirs) . join ( format ! ( "{file}.o" ) ) ;
73
67
let mut build_rtstartup_cmd = Command :: new ( & bootstrap_host_compiler. rustc ) ;
74
68
build_rtstartup_cmd
75
69
. arg ( "--target" )
76
70
. arg ( & target_triple)
77
71
. arg ( "--emit=obj" )
78
72
. arg ( "-o" )
79
- . arg ( target_rustlib_lib . join ( format ! ( "{file}.o" ) ) )
73
+ . arg ( & obj )
80
74
. arg ( rtstartup_src. join ( format ! ( "{file}.rs" ) ) ) ;
81
75
spawn_and_wait ( build_rtstartup_cmd) ;
76
+ target_libs. libs . push ( obj) ;
82
77
}
83
- }
84
78
79
+ target_libs. install_into_sysroot ( & DIST_DIR . to_path ( dirs) )
80
+ }
85
81
match sysroot_kind {
86
82
SysrootKind :: None => { } // Nothing to do
87
83
SysrootKind :: Llvm => {
84
+ let default_sysroot =
85
+ super :: rustc_info:: get_default_sysroot ( & bootstrap_host_compiler. rustc ) ;
86
+
87
+ let host_rustlib_lib =
88
+ RUSTLIB_DIR . to_path ( dirs) . join ( & bootstrap_host_compiler. triple ) . join ( "lib" ) ;
89
+ let target_rustlib_lib = RUSTLIB_DIR . to_path ( dirs) . join ( & target_triple) . join ( "lib" ) ;
90
+ fs:: create_dir_all ( & host_rustlib_lib) . unwrap ( ) ;
91
+ fs:: create_dir_all ( & target_rustlib_lib) . unwrap ( ) ;
92
+
88
93
for file in fs:: read_dir (
89
94
default_sysroot
90
95
. join ( "lib" )
@@ -122,12 +127,13 @@ pub(crate) fn build_sysroot(
122
127
}
123
128
}
124
129
SysrootKind :: Clif => {
125
- build_clif_sysroot_for_triple (
130
+ let host = build_clif_sysroot_for_triple (
126
131
dirs,
127
132
channel,
128
133
bootstrap_host_compiler. clone ( ) ,
129
134
& cg_clif_dylib_path,
130
135
) ;
136
+ host. install_into_sysroot ( & DIST_DIR . to_path ( dirs) ) ;
131
137
132
138
if !is_native {
133
139
build_clif_sysroot_for_triple (
@@ -140,16 +146,16 @@ pub(crate) fn build_sysroot(
140
146
bootstrap_target_compiler
141
147
} ,
142
148
& cg_clif_dylib_path,
143
- ) ;
149
+ )
150
+ . install_into_sysroot ( & DIST_DIR . to_path ( dirs) ) ;
144
151
}
145
152
146
153
// Copy std for the host to the lib dir. This is necessary for the jit mode to find
147
154
// libstd.
148
- for file in fs:: read_dir ( host_rustlib_lib) . unwrap ( ) {
149
- let file = file. unwrap ( ) . path ( ) ;
150
- let filename = file. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
155
+ for lib in host. libs {
156
+ let filename = lib. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
151
157
if filename. contains ( "std-" ) && !filename. contains ( ".rlib" ) {
152
- try_hard_link ( & file , LIB_DIR . to_path ( dirs) . join ( file . file_name ( ) . unwrap ( ) ) ) ;
158
+ try_hard_link ( & lib , LIB_DIR . to_path ( dirs) . join ( lib . file_name ( ) . unwrap ( ) ) ) ;
153
159
}
154
160
}
155
161
}
@@ -162,19 +168,36 @@ pub(crate) fn build_sysroot(
162
168
target_compiler
163
169
}
164
170
171
+ struct SysrootTarget {
172
+ triple : String ,
173
+ libs : Vec < PathBuf > ,
174
+ }
175
+
176
+ impl SysrootTarget {
177
+ fn install_into_sysroot ( & self , sysroot : & Path ) {
178
+ let target_rustlib_lib = sysroot. join ( "lib" ) . join ( "rustlib" ) . join ( & self . triple ) . join ( "lib" ) ;
179
+ fs:: create_dir_all ( & target_rustlib_lib) . unwrap ( ) ;
180
+
181
+ for lib in & self . libs {
182
+ try_hard_link ( lib, target_rustlib_lib. join ( lib. file_name ( ) . unwrap ( ) ) ) ;
183
+ }
184
+ }
185
+ }
186
+
165
187
pub ( crate ) static ORIG_BUILD_SYSROOT : RelPath = RelPath :: SOURCE . join ( "build_sysroot" ) ;
166
188
pub ( crate ) static BUILD_SYSROOT : RelPath = RelPath :: DOWNLOAD . join ( "sysroot" ) ;
167
189
pub ( crate ) static SYSROOT_RUSTC_VERSION : RelPath = BUILD_SYSROOT . join ( "rustc_version" ) ;
168
190
pub ( crate ) static SYSROOT_SRC : RelPath = BUILD_SYSROOT . join ( "sysroot_src" ) ;
169
191
pub ( crate ) static STANDARD_LIBRARY : CargoProject =
170
192
CargoProject :: new ( & BUILD_SYSROOT , "build_sysroot" ) ;
171
193
194
+ #[ must_use]
172
195
fn build_clif_sysroot_for_triple (
173
196
dirs : & Dirs ,
174
197
channel : & str ,
175
198
mut compiler : Compiler ,
176
199
cg_clif_dylib_path : & Path ,
177
- ) {
200
+ ) -> SysrootTarget {
178
201
match fs:: read_to_string ( SYSROOT_RUSTC_VERSION . to_path ( dirs) ) {
179
202
Err ( e) => {
180
203
eprintln ! ( "Failed to get rustc version for patched sysroot source: {}" , e) ;
@@ -219,7 +242,8 @@ fn build_clif_sysroot_for_triple(
219
242
build_cmd. env ( "__CARGO_DEFAULT_LIB_METADATA" , "cg_clif" ) ;
220
243
spawn_and_wait ( build_cmd) ;
221
244
222
- // Copy all relevant files to the sysroot
245
+ let mut target_libs = SysrootTarget { triple : compiler. triple , libs : vec ! [ ] } ;
246
+
223
247
for entry in fs:: read_dir ( build_dir. join ( "deps" ) ) . unwrap ( ) {
224
248
let entry = entry. unwrap ( ) ;
225
249
if let Some ( ext) = entry. path ( ) . extension ( ) {
@@ -229,9 +253,8 @@ fn build_clif_sysroot_for_triple(
229
253
} else {
230
254
continue ;
231
255
} ;
232
- try_hard_link (
233
- entry. path ( ) ,
234
- RUSTLIB_DIR . to_path ( dirs) . join ( & compiler. triple ) . join ( "lib" ) . join ( entry. file_name ( ) ) ,
235
- ) ;
256
+ target_libs. libs . push ( entry. path ( ) ) ;
236
257
}
258
+
259
+ target_libs
237
260
}
0 commit comments