@@ -191,7 +191,8 @@ pub fn link_binary(sess: &Session,
191191 let mut out_filenames = Vec :: new ( ) ;
192192 for & crate_type in sess. crate_types . borrow ( ) . iter ( ) {
193193 // Ignore executable crates if we have -Z no-trans, as they will error.
194- if sess. opts . debugging_opts . no_trans &&
194+ if ( sess. opts . debugging_opts . no_trans ||
195+ !sess. opts . output_types . should_trans ( ) ) &&
195196 crate_type == config:: CrateTypeExecutable {
196197 continue ;
197198 }
@@ -200,15 +201,16 @@ pub fn link_binary(sess: &Session,
200201 bug ! ( "invalid output type `{:?}` for target os `{}`" ,
201202 crate_type, sess. opts. target_triple) ;
202203 }
203- let out_file = link_binary_output ( sess, trans, crate_type, outputs,
204- crate_name) ;
205- out_filenames. push ( out_file) ;
204+ let mut out_files = link_binary_output ( sess, trans, crate_type, outputs, crate_name) ;
205+ out_filenames. append ( & mut out_files) ;
206206 }
207207
208208 // Remove the temporary object file and metadata if we aren't saving temps
209209 if !sess. opts . cg . save_temps {
210- for obj in object_filenames ( trans, outputs) {
211- remove ( sess, & obj) ;
210+ if sess. opts . output_types . should_trans ( ) {
211+ for obj in object_filenames ( trans, outputs) {
212+ remove ( sess, & obj) ;
213+ }
212214 }
213215 remove ( sess, & outputs. with_extension ( "metadata.o" ) ) ;
214216 }
@@ -254,18 +256,25 @@ fn is_writeable(p: &Path) -> bool {
254256 }
255257}
256258
259+ fn filename_for_metadata ( sess : & Session , crate_name : & str , outputs : & OutputFilenames ) -> PathBuf {
260+ let out_filename = outputs. single_output_file . clone ( )
261+ . unwrap_or ( outputs
262+ . out_directory
263+ . join ( & format ! ( "lib{}{}.rmeta" , crate_name, sess. opts. cg. extra_filename) ) ) ;
264+ check_file_is_writeable ( & out_filename, sess) ;
265+ out_filename
266+ }
267+
257268pub fn filename_for_input ( sess : & Session ,
258269 crate_type : config:: CrateType ,
259270 crate_name : & str ,
260271 outputs : & OutputFilenames ) -> PathBuf {
261272 let libname = format ! ( "{}{}" , crate_name, sess. opts. cg. extra_filename) ;
273+
262274 match crate_type {
263275 config:: CrateTypeRlib => {
264276 outputs. out_directory . join ( & format ! ( "lib{}.rlib" , libname) )
265277 }
266- config:: CrateTypeMetadata => {
267- outputs. out_directory . join ( & format ! ( "lib{}.rmeta" , libname) )
268- }
269278 config:: CrateTypeCdylib |
270279 config:: CrateTypeProcMacro |
271280 config:: CrateTypeDylib => {
@@ -323,52 +332,75 @@ pub fn each_linked_rlib(sess: &Session,
323332 }
324333}
325334
335+ fn out_filename ( sess : & Session ,
336+ crate_type : config:: CrateType ,
337+ outputs : & OutputFilenames ,
338+ crate_name : & str )
339+ -> PathBuf {
340+ let default_filename = filename_for_input ( sess, crate_type, crate_name, outputs) ;
341+ let out_filename = outputs. outputs . get ( & OutputType :: Exe )
342+ . and_then ( |s| s. to_owned ( ) )
343+ . or_else ( || outputs. single_output_file . clone ( ) )
344+ . unwrap_or ( default_filename) ;
345+
346+ check_file_is_writeable ( & out_filename, sess) ;
347+
348+ out_filename
349+ }
350+
351+ // Make sure files are writeable. Mac, FreeBSD, and Windows system linkers
352+ // check this already -- however, the Linux linker will happily overwrite a
353+ // read-only file. We should be consistent.
354+ fn check_file_is_writeable ( file : & Path , sess : & Session ) {
355+ if !is_writeable ( file) {
356+ sess. fatal ( & format ! ( "output file {} is not writeable -- check its \
357+ permissions", file. display( ) ) ) ;
358+ }
359+ }
360+
326361fn link_binary_output ( sess : & Session ,
327362 trans : & CrateTranslation ,
328363 crate_type : config:: CrateType ,
329364 outputs : & OutputFilenames ,
330- crate_name : & str ) -> PathBuf {
365+ crate_name : & str ) -> Vec < PathBuf > {
331366 let objects = object_filenames ( trans, outputs) ;
332- let default_filename = filename_for_input ( sess, crate_type, crate_name,
333- outputs) ;
334- let out_filename = outputs. outputs . get ( & OutputType :: Exe )
335- . and_then ( |s| s. to_owned ( ) )
336- . or_else ( || outputs. single_output_file . clone ( ) )
337- . unwrap_or ( default_filename) ;
338367
339- // Make sure files are writeable. Mac, FreeBSD, and Windows system linkers
340- // check this already -- however, the Linux linker will happily overwrite a
341- // read-only file. We should be consistent.
342- for file in objects. iter ( ) . chain ( Some ( & out_filename) ) {
343- if !is_writeable ( file) {
344- sess. fatal ( & format ! ( "output file {} is not writeable -- check its \
345- permissions", file. display( ) ) ) ;
346- }
368+ for file in & objects {
369+ check_file_is_writeable ( file, sess) ;
347370 }
348371
349372 let tmpdir = match TempDir :: new ( "rustc" ) {
350373 Ok ( tmpdir) => tmpdir,
351374 Err ( err) => sess. fatal ( & format ! ( "couldn't create a temp dir: {}" , err) ) ,
352375 } ;
353376
354- match crate_type {
355- config:: CrateTypeRlib => {
356- link_rlib ( sess, Some ( trans) , & objects, & out_filename,
357- tmpdir. path ( ) ) . build ( ) ;
358- }
359- config:: CrateTypeStaticlib => {
360- link_staticlib ( sess, & objects, & out_filename, tmpdir. path ( ) ) ;
361- }
362- config:: CrateTypeMetadata => {
363- emit_metadata ( sess, trans, & out_filename) ;
364- }
365- _ => {
366- link_natively ( sess, crate_type, & objects, & out_filename, trans,
367- outputs, tmpdir. path ( ) ) ;
377+ let mut out_filenames = vec ! [ ] ;
378+
379+ if outputs. outputs . contains_key ( & OutputType :: Metadata ) {
380+ let out_filename = filename_for_metadata ( sess, crate_name, outputs) ;
381+ emit_metadata ( sess, trans, & out_filename) ;
382+ out_filenames. push ( out_filename) ;
383+ }
384+
385+ if outputs. outputs . should_trans ( ) {
386+ let out_filename = out_filename ( sess, crate_type, outputs, crate_name) ;
387+ match crate_type {
388+ config:: CrateTypeRlib => {
389+ link_rlib ( sess, Some ( trans) , & objects, & out_filename,
390+ tmpdir. path ( ) ) . build ( ) ;
391+ }
392+ config:: CrateTypeStaticlib => {
393+ link_staticlib ( sess, & objects, & out_filename, tmpdir. path ( ) ) ;
394+ }
395+ _ => {
396+ link_natively ( sess, crate_type, & objects, & out_filename, trans,
397+ outputs, tmpdir. path ( ) ) ;
398+ }
368399 }
400+ out_filenames. push ( out_filename) ;
369401 }
370402
371- out_filename
403+ out_filenames
372404}
373405
374406fn object_filenames ( trans : & CrateTranslation ,
0 commit comments