@@ -27,7 +27,6 @@ use std::ffi::{CStr, CString};
27
27
use std:: fs;
28
28
use std:: mem;
29
29
use std:: path:: Path ;
30
- use std:: process:: Stdio ;
31
30
use std:: ptr;
32
31
use std:: str;
33
32
use std:: sync:: { Arc , Mutex } ;
@@ -619,6 +618,8 @@ pub fn run_passes(sess: &Session,
619
618
let needs_crate_bitcode =
620
619
sess. crate_types . borrow ( ) . contains ( & config:: CrateTypeRlib ) &&
621
620
sess. opts . output_types . contains ( & config:: OutputTypeExe ) ;
621
+ let needs_crate_object =
622
+ sess. opts . output_types . contains ( & config:: OutputTypeExe ) ;
622
623
if needs_crate_bitcode {
623
624
modules_config. emit_bc = true ;
624
625
}
@@ -696,7 +697,8 @@ pub fn run_passes(sess: &Session,
696
697
if sess. opts . cg . codegen_units == 1 {
697
698
// 1) Only one codegen unit. In this case it's no difficulty
698
699
// to copy `foo.0.x` to `foo.x`.
699
- copy_gracefully ( & crate_output. with_extension ( ext) , & crate_output. path ( output_type) ) ;
700
+ copy_gracefully ( & crate_output. with_extension ( ext) ,
701
+ & crate_output. path ( output_type) ) ;
700
702
if !sess. opts . cg . save_temps && !keep_numbered {
701
703
// The user just wants `foo.x`, not `foo.0.x`.
702
704
remove ( sess, & crate_output. with_extension ( ext) ) ;
@@ -715,76 +717,11 @@ pub fn run_passes(sess: &Session,
715
717
}
716
718
} ;
717
719
718
- let link_obj = |output_path : & Path | {
719
- // Running `ld -r` on a single input is kind of pointless.
720
- if sess. opts . cg . codegen_units == 1 {
721
- copy_gracefully ( & crate_output. with_extension ( "0.o" ) , output_path) ;
722
- // Leave the .0.o file around, to mimic the behavior of the normal
723
- // code path.
724
- return ;
725
- }
726
-
727
- // Some builds of MinGW GCC will pass --force-exe-suffix to ld, which
728
- // will automatically add a .exe extension if the extension is not
729
- // already .exe or .dll. To ensure consistent behavior on Windows, we
730
- // add the .exe suffix explicitly and then rename the output file to
731
- // the desired path. This will give the correct behavior whether or
732
- // not GCC adds --force-exe-suffix.
733
- let windows_output_path =
734
- if sess. target . target . options . is_like_windows {
735
- Some ( output_path. with_extension ( "o.exe" ) )
736
- } else {
737
- None
738
- } ;
739
-
740
- let ( pname, mut cmd) = get_linker ( sess) ;
741
-
742
- cmd. args ( & sess. target . target . options . pre_link_args ) ;
743
- cmd. arg ( "-nostdlib" ) ;
744
-
745
- for index in 0 ..trans. modules . len ( ) {
746
- cmd. arg ( & crate_output. with_extension ( & format ! ( "{}.o" , index) ) ) ;
747
- }
748
-
749
- cmd. arg ( "-r" ) . arg ( "-o" )
750
- . arg ( windows_output_path. as_ref ( ) . map ( |s| & * * s) . unwrap_or ( output_path) ) ;
751
-
752
- cmd. args ( & sess. target . target . options . post_link_args ) ;
753
-
754
- if sess. opts . debugging_opts . print_link_args {
755
- println ! ( "{:?}" , & cmd) ;
756
- }
757
-
758
- cmd. stdin ( Stdio :: null ( ) ) ;
759
- match cmd. status ( ) {
760
- Ok ( status) => {
761
- if !status. success ( ) {
762
- sess. err ( & format ! ( "linking of {} with `{:?}` failed" ,
763
- output_path. display( ) , cmd) ) ;
764
- sess. abort_if_errors ( ) ;
765
- }
766
- } ,
767
- Err ( e) => {
768
- sess. err ( & format ! ( "could not exec the linker `{}`: {}" ,
769
- pname, e) ) ;
770
- sess. abort_if_errors ( ) ;
771
- } ,
772
- }
773
-
774
- match windows_output_path {
775
- Some ( ref windows_path) => {
776
- fs:: rename ( windows_path, output_path) . unwrap ( ) ;
777
- } ,
778
- None => {
779
- // The file is already named according to `output_path`.
780
- }
781
- }
782
- } ;
783
-
784
720
// Flag to indicate whether the user explicitly requested bitcode.
785
721
// Otherwise, we produced it only as a temporary output, and will need
786
722
// to get rid of it.
787
723
let mut user_wants_bitcode = false ;
724
+ let mut user_wants_objects = false ;
788
725
for output_type in output_types {
789
726
match * output_type {
790
727
config:: OutputTypeBitcode => {
@@ -801,17 +738,10 @@ pub fn run_passes(sess: &Session,
801
738
copy_if_one_unit ( "0.s" , config:: OutputTypeAssembly , false ) ;
802
739
}
803
740
config:: OutputTypeObject => {
804
- link_obj ( & crate_output. path ( config:: OutputTypeObject ) ) ;
805
- }
806
- config:: OutputTypeExe => {
807
- // If config::OutputTypeObject is already in the list, then
808
- // `crate.o` will be handled by the config::OutputTypeObject case.
809
- // Otherwise, we need to create the temporary object so we
810
- // can run the linker.
811
- if !sess. opts . output_types . contains ( & config:: OutputTypeObject ) {
812
- link_obj ( & crate_output. temp_path ( config:: OutputTypeObject ) ) ;
813
- }
741
+ user_wants_objects = true ;
742
+ copy_if_one_unit ( "0.o" , config:: OutputTypeObject , true ) ;
814
743
}
744
+ config:: OutputTypeExe |
815
745
config:: OutputTypeDepInfo => { }
816
746
}
817
747
}
@@ -848,15 +778,18 @@ pub fn run_passes(sess: &Session,
848
778
let keep_numbered_bitcode = needs_crate_bitcode ||
849
779
( user_wants_bitcode && sess. opts . cg . codegen_units > 1 ) ;
850
780
781
+ let keep_numbered_objects = needs_crate_object ||
782
+ ( user_wants_objects && sess. opts . cg . codegen_units > 1 ) ;
783
+
851
784
for i in 0 ..trans. modules . len ( ) {
852
- if modules_config. emit_obj {
785
+ if modules_config. emit_obj && !keep_numbered_objects {
853
786
let ext = format ! ( "{}.o" , i) ;
854
- remove ( sess, & crate_output. with_extension ( & ext[ .. ] ) ) ;
787
+ remove ( sess, & crate_output. with_extension ( & ext) ) ;
855
788
}
856
789
857
790
if modules_config. emit_bc && !keep_numbered_bitcode {
858
791
let ext = format ! ( "{}.bc" , i) ;
859
- remove ( sess, & crate_output. with_extension ( & ext[ .. ] ) ) ;
792
+ remove ( sess, & crate_output. with_extension ( & ext) ) ;
860
793
}
861
794
}
862
795
0 commit comments