@@ -27,7 +27,6 @@ use std::ffi::{CStr, CString};
2727use std:: fs;
2828use std:: mem;
2929use std:: path:: Path ;
30- use std:: process:: Stdio ;
3130use std:: ptr;
3231use std:: str;
3332use std:: sync:: { Arc , Mutex } ;
@@ -619,6 +618,8 @@ pub fn run_passes(sess: &Session,
619618 let needs_crate_bitcode =
620619 sess. crate_types . borrow ( ) . contains ( & config:: CrateTypeRlib ) &&
621620 sess. opts . output_types . contains ( & config:: OutputTypeExe ) ;
621+ let needs_crate_object =
622+ sess. opts . output_types . contains ( & config:: OutputTypeExe ) ;
622623 if needs_crate_bitcode {
623624 modules_config. emit_bc = true ;
624625 }
@@ -696,7 +697,8 @@ pub fn run_passes(sess: &Session,
696697 if sess. opts . cg . codegen_units == 1 {
697698 // 1) Only one codegen unit. In this case it's no difficulty
698699 // 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) ) ;
700702 if !sess. opts . cg . save_temps && !keep_numbered {
701703 // The user just wants `foo.x`, not `foo.0.x`.
702704 remove ( sess, & crate_output. with_extension ( ext) ) ;
@@ -715,76 +717,11 @@ pub fn run_passes(sess: &Session,
715717 }
716718 } ;
717719
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-
784720 // Flag to indicate whether the user explicitly requested bitcode.
785721 // Otherwise, we produced it only as a temporary output, and will need
786722 // to get rid of it.
787723 let mut user_wants_bitcode = false ;
724+ let mut user_wants_objects = false ;
788725 for output_type in output_types {
789726 match * output_type {
790727 config:: OutputTypeBitcode => {
@@ -801,17 +738,10 @@ pub fn run_passes(sess: &Session,
801738 copy_if_one_unit ( "0.s" , config:: OutputTypeAssembly , false ) ;
802739 }
803740 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 ) ;
814743 }
744+ config:: OutputTypeExe |
815745 config:: OutputTypeDepInfo => { }
816746 }
817747 }
@@ -848,15 +778,18 @@ pub fn run_passes(sess: &Session,
848778 let keep_numbered_bitcode = needs_crate_bitcode ||
849779 ( user_wants_bitcode && sess. opts . cg . codegen_units > 1 ) ;
850780
781+ let keep_numbered_objects = needs_crate_object ||
782+ ( user_wants_objects && sess. opts . cg . codegen_units > 1 ) ;
783+
851784 for i in 0 ..trans. modules . len ( ) {
852- if modules_config. emit_obj {
785+ if modules_config. emit_obj && !keep_numbered_objects {
853786 let ext = format ! ( "{}.o" , i) ;
854- remove ( sess, & crate_output. with_extension ( & ext[ .. ] ) ) ;
787+ remove ( sess, & crate_output. with_extension ( & ext) ) ;
855788 }
856789
857790 if modules_config. emit_bc && !keep_numbered_bitcode {
858791 let ext = format ! ( "{}.bc" , i) ;
859- remove ( sess, & crate_output. with_extension ( & ext[ .. ] ) ) ;
792+ remove ( sess, & crate_output. with_extension ( & ext) ) ;
860793 }
861794 }
862795
0 commit comments