9
9
// except according to those terms.
10
10
11
11
use super :: archive:: { ArchiveBuilder , ArchiveConfig } ;
12
- use super :: bytecode:: { self , RLIB_BYTECODE_EXTENSION } ;
12
+ use super :: bytecode:: RLIB_BYTECODE_EXTENSION ;
13
13
use super :: linker:: Linker ;
14
14
use super :: command:: Command ;
15
15
use super :: rpath:: RPathConfig ;
@@ -37,7 +37,7 @@ use std::env;
37
37
use std:: ffi:: OsString ;
38
38
use std:: fmt;
39
39
use std:: fs:: { self , File } ;
40
- use std:: io:: { self , Read , Write , BufWriter } ;
40
+ use std:: io:: { self , Write , BufWriter } ;
41
41
use std:: path:: { Path , PathBuf } ;
42
42
use std:: process:: { Output , Stdio } ;
43
43
use std:: str;
@@ -126,14 +126,6 @@ fn command_path(sess: &Session) -> OsString {
126
126
env:: join_paths ( new_path) . unwrap ( )
127
127
}
128
128
129
- fn metadata_obj ( outputs : & OutputFilenames ) -> PathBuf {
130
- outputs. temp_path ( OutputType :: Object , Some ( METADATA_MODULE_NAME ) )
131
- }
132
-
133
- fn allocator_obj ( outputs : & OutputFilenames ) -> PathBuf {
134
- outputs. temp_path ( OutputType :: Object , Some ( ALLOCATOR_MODULE_NAME ) )
135
- }
136
-
137
129
pub fn remove ( sess : & Session , path : & Path ) {
138
130
match fs:: remove_file ( path) {
139
131
Ok ( ..) => { }
@@ -175,13 +167,23 @@ pub fn link_binary(sess: &Session,
175
167
// Remove the temporary object file and metadata if we aren't saving temps
176
168
if !sess. opts . cg . save_temps {
177
169
if sess. opts . output_types . should_trans ( ) {
178
- for obj in trans. modules . iter ( ) {
179
- remove ( sess, & obj. object ) ;
170
+ for obj in trans. modules . iter ( ) . filter_map ( |m| m . object . as_ref ( ) ) {
171
+ remove ( sess, obj) ;
180
172
}
181
173
}
182
- remove ( sess, & metadata_obj ( outputs) ) ;
183
- if trans. allocator_module . is_some ( ) {
184
- remove ( sess, & allocator_obj ( outputs) ) ;
174
+ for obj in trans. modules . iter ( ) . filter_map ( |m| m. bytecode_compressed . as_ref ( ) ) {
175
+ remove ( sess, obj) ;
176
+ }
177
+ if let Some ( ref obj) = trans. metadata_module . object {
178
+ remove ( sess, obj) ;
179
+ }
180
+ if let Some ( ref allocator) = trans. allocator_module {
181
+ if let Some ( ref obj) = allocator. object {
182
+ remove ( sess, obj) ;
183
+ }
184
+ if let Some ( ref bc) = allocator. bytecode_compressed {
185
+ remove ( sess, bc) ;
186
+ }
185
187
}
186
188
}
187
189
@@ -256,8 +258,8 @@ fn link_binary_output(sess: &Session,
256
258
crate_type : config:: CrateType ,
257
259
outputs : & OutputFilenames ,
258
260
crate_name : & str ) -> Vec < PathBuf > {
259
- for module in trans. modules . iter ( ) {
260
- check_file_is_writeable ( & module . object , sess) ;
261
+ for obj in trans. modules . iter ( ) . filter_map ( |m| m . object . as_ref ( ) ) {
262
+ check_file_is_writeable ( obj , sess) ;
261
263
}
262
264
263
265
let tmpdir = match TempDir :: new ( "rustc" ) {
@@ -280,20 +282,14 @@ fn link_binary_output(sess: &Session,
280
282
link_rlib ( sess,
281
283
trans,
282
284
RlibFlavor :: Normal ,
283
- outputs,
284
285
& out_filename,
285
286
tmpdir. path ( ) ) . build ( ) ;
286
287
}
287
288
config:: CrateTypeStaticlib => {
288
- link_staticlib ( sess,
289
- trans,
290
- outputs,
291
- & out_filename,
292
- tmpdir. path ( ) ) ;
289
+ link_staticlib ( sess, trans, & out_filename, tmpdir. path ( ) ) ;
293
290
}
294
291
_ => {
295
- link_natively ( sess, crate_type, & out_filename,
296
- trans, outputs, tmpdir. path ( ) ) ;
292
+ link_natively ( sess, crate_type, & out_filename, trans, tmpdir. path ( ) ) ;
297
293
}
298
294
}
299
295
out_filenames. push ( out_filename) ;
@@ -349,14 +345,13 @@ enum RlibFlavor {
349
345
fn link_rlib < ' a > ( sess : & ' a Session ,
350
346
trans : & CrateTranslation ,
351
347
flavor : RlibFlavor ,
352
- outputs : & OutputFilenames ,
353
348
out_filename : & Path ,
354
349
tmpdir : & Path ) -> ArchiveBuilder < ' a > {
355
350
info ! ( "preparing rlib to {:?}" , out_filename) ;
356
351
let mut ab = ArchiveBuilder :: new ( archive_config ( sess, out_filename, None ) ) ;
357
352
358
- for module in trans. modules . iter ( ) {
359
- ab. add_file ( & module . object ) ;
353
+ for obj in trans. modules . iter ( ) . filter_map ( |m| m . object . as_ref ( ) ) {
354
+ ab. add_file ( obj ) ;
360
355
}
361
356
362
357
// Note that in this loop we are ignoring the value of `lib.cfg`. That is,
@@ -421,56 +416,9 @@ fn link_rlib<'a>(sess: &'a Session,
421
416
ab. add_file ( & metadata) ;
422
417
423
418
// For LTO purposes, the bytecode of this library is also inserted
424
- // into the archive. If codegen_units > 1, we insert each of the
425
- // bitcode files.
426
- for module in trans. modules . iter ( ) {
427
- // Note that we make sure that the bytecode filename in the
428
- // archive is never exactly 16 bytes long by adding a 16 byte
429
- // extension to it. This is to work around a bug in LLDB that
430
- // would cause it to crash if the name of a file in an archive
431
- // was exactly 16 bytes.
432
- let bc_filename = module. object . with_extension ( "bc" ) ;
433
- let bc_encoded_filename = tmpdir. join ( {
434
- module. object . with_extension ( RLIB_BYTECODE_EXTENSION ) . file_name ( ) . unwrap ( )
435
- } ) ;
436
-
437
- let mut bc_data = Vec :: new ( ) ;
438
- match fs:: File :: open ( & bc_filename) . and_then ( |mut f| {
439
- f. read_to_end ( & mut bc_data)
440
- } ) {
441
- Ok ( ..) => { }
442
- Err ( e) => sess. fatal ( & format ! ( "failed to read bytecode: {}" ,
443
- e) )
444
- }
445
-
446
- let encoded = bytecode:: encode ( & module. llmod_id , & bc_data) ;
447
-
448
- let mut bc_file_deflated = match fs:: File :: create ( & bc_encoded_filename) {
449
- Ok ( file) => file,
450
- Err ( e) => {
451
- sess. fatal ( & format ! ( "failed to create compressed \
452
- bytecode file: {}", e) )
453
- }
454
- } ;
455
-
456
- match bc_file_deflated. write_all ( & encoded) {
457
- Ok ( ( ) ) => { }
458
- Err ( e) => {
459
- sess. fatal ( & format ! ( "failed to write compressed \
460
- bytecode: {}", e) ) ;
461
- }
462
- } ;
463
-
464
- ab. add_file ( & bc_encoded_filename) ;
465
-
466
- // See the bottom of back::write::run_passes for an explanation
467
- // of when we do and don't keep .#module-name#.bc files around.
468
- let user_wants_numbered_bitcode =
469
- sess. opts . output_types . contains_key ( & OutputType :: Bitcode ) &&
470
- sess. codegen_units ( ) > 1 ;
471
- if !sess. opts . cg . save_temps && !user_wants_numbered_bitcode {
472
- remove ( sess, & bc_filename) ;
473
- }
419
+ // into the archive.
420
+ for bytecode in trans. modules . iter ( ) . filter_map ( |m| m. bytecode_compressed . as_ref ( ) ) {
421
+ ab. add_file ( bytecode) ;
474
422
}
475
423
476
424
// After adding all files to the archive, we need to update the
@@ -482,8 +430,11 @@ fn link_rlib<'a>(sess: &'a Session,
482
430
}
483
431
484
432
RlibFlavor :: StaticlibBase => {
485
- if trans. allocator_module . is_some ( ) {
486
- ab. add_file ( & allocator_obj ( outputs) ) ;
433
+ let obj = trans. allocator_module
434
+ . as_ref ( )
435
+ . and_then ( |m| m. object . as_ref ( ) ) ;
436
+ if let Some ( obj) = obj {
437
+ ab. add_file ( obj) ;
487
438
}
488
439
}
489
440
}
@@ -505,13 +456,11 @@ fn link_rlib<'a>(sess: &'a Session,
505
456
// metadata file).
506
457
fn link_staticlib ( sess : & Session ,
507
458
trans : & CrateTranslation ,
508
- outputs : & OutputFilenames ,
509
459
out_filename : & Path ,
510
460
tempdir : & Path ) {
511
461
let mut ab = link_rlib ( sess,
512
462
trans,
513
463
RlibFlavor :: StaticlibBase ,
514
- outputs,
515
464
out_filename,
516
465
tempdir) ;
517
466
let mut all_native_libs = vec ! [ ] ;
@@ -616,7 +565,6 @@ fn link_natively(sess: &Session,
616
565
crate_type : config:: CrateType ,
617
566
out_filename : & Path ,
618
567
trans : & CrateTranslation ,
619
- outputs : & OutputFilenames ,
620
568
tmpdir : & Path ) {
621
569
info ! ( "preparing {:?} to {:?}" , crate_type, out_filename) ;
622
570
let flavor = sess. linker_flavor ( ) ;
@@ -656,7 +604,7 @@ fn link_natively(sess: &Session,
656
604
{
657
605
let mut linker = trans. linker_info . to_linker ( cmd, & sess) ;
658
606
link_args ( & mut * linker, sess, crate_type, tmpdir,
659
- out_filename, outputs , trans) ;
607
+ out_filename, trans) ;
660
608
cmd = linker. finalize ( ) ;
661
609
}
662
610
if let Some ( args) = sess. target . target . options . late_link_args . get ( & flavor) {
@@ -878,7 +826,6 @@ fn link_args(cmd: &mut Linker,
878
826
crate_type : config:: CrateType ,
879
827
tmpdir : & Path ,
880
828
out_filename : & Path ,
881
- outputs : & OutputFilenames ,
882
829
trans : & CrateTranslation ) {
883
830
884
831
// The default library location, we need this to find the runtime.
@@ -889,8 +836,8 @@ fn link_args(cmd: &mut Linker,
889
836
let t = & sess. target . target ;
890
837
891
838
cmd. include_path ( & fix_windows_verbatim_for_gcc ( & lib_path) ) ;
892
- for module in trans. modules . iter ( ) {
893
- cmd. add_object ( & module . object ) ;
839
+ for obj in trans. modules . iter ( ) . filter_map ( |m| m . object . as_ref ( ) ) {
840
+ cmd. add_object ( obj ) ;
894
841
}
895
842
cmd. output_filename ( out_filename) ;
896
843
@@ -913,11 +860,16 @@ fn link_args(cmd: &mut Linker,
913
860
// object file, so we link that in here.
914
861
if crate_type == config:: CrateTypeDylib ||
915
862
crate_type == config:: CrateTypeProcMacro {
916
- cmd. add_object ( & metadata_obj ( outputs) ) ;
863
+ if let Some ( obj) = trans. metadata_module . object . as_ref ( ) {
864
+ cmd. add_object ( obj) ;
865
+ }
917
866
}
918
867
919
- if trans. allocator_module . is_some ( ) {
920
- cmd. add_object ( & allocator_obj ( outputs) ) ;
868
+ let obj = trans. allocator_module
869
+ . as_ref ( )
870
+ . and_then ( |m| m. object . as_ref ( ) ) ;
871
+ if let Some ( obj) = obj {
872
+ cmd. add_object ( obj) ;
921
873
}
922
874
923
875
// Try to strip as much out of the generated object by removing unused
@@ -1185,9 +1137,9 @@ fn add_upstream_rust_crates(cmd: &mut Linker,
1185
1137
1186
1138
for f in archive. src_files ( ) {
1187
1139
if f. ends_with ( RLIB_BYTECODE_EXTENSION ) || f == METADATA_FILENAME {
1188
- archive. remove_file ( & f) ;
1189
- continue
1190
- }
1140
+ archive. remove_file ( & f) ;
1141
+ continue
1142
+ }
1191
1143
}
1192
1144
1193
1145
archive. build ( ) ;
0 commit comments