@@ -228,6 +228,7 @@ pub fn invalid_output_for_target(sess: &Session,
228
228
crate_type : config:: CrateType ) -> bool {
229
229
match ( sess. target . target . options . dynamic_linking ,
230
230
sess. target . target . options . executables , crate_type) {
231
+ ( false , _, config:: CrateTypeCdylib ) |
231
232
( false , _, config:: CrateTypeDylib ) => true ,
232
233
( _, false , config:: CrateTypeExecutable ) => true ,
233
234
_ => false
@@ -250,6 +251,7 @@ pub fn filename_for_input(sess: &Session,
250
251
config:: CrateTypeRlib => {
251
252
outputs. out_directory . join ( & format ! ( "lib{}.rlib" , libname) )
252
253
}
254
+ config:: CrateTypeCdylib |
253
255
config:: CrateTypeDylib => {
254
256
let ( prefix, suffix) = ( & sess. target . target . options . dll_prefix ,
255
257
& sess. target . target . options . dll_suffix ) ;
@@ -278,9 +280,10 @@ pub fn each_linked_rlib(sess: &Session,
278
280
f : & mut FnMut ( ast:: CrateNum , & Path ) ) {
279
281
let crates = sess. cstore . used_crates ( LinkagePreference :: RequireStatic ) . into_iter ( ) ;
280
282
let fmts = sess. dependency_formats . borrow ( ) ;
281
- let fmts = fmts. get ( & config:: CrateTypeExecutable ) . or_else ( || {
282
- fmts. get ( & config:: CrateTypeStaticlib )
283
- } ) . unwrap_or_else ( || {
283
+ let fmts = fmts. get ( & config:: CrateTypeExecutable )
284
+ . or_else ( || fmts. get ( & config:: CrateTypeStaticlib ) )
285
+ . or_else ( || fmts. get ( & config:: CrateTypeCdylib ) ) ;
286
+ let fmts = fmts. unwrap_or_else ( || {
284
287
bug ! ( "could not find formats for rlibs" )
285
288
} ) ;
286
289
for ( cnum, path) in crates {
@@ -335,13 +338,9 @@ fn link_binary_output(sess: &Session,
335
338
config:: CrateTypeStaticlib => {
336
339
link_staticlib ( sess, & objects, & out_filename, tmpdir. path ( ) ) ;
337
340
}
338
- config:: CrateTypeExecutable => {
339
- link_natively ( sess, false , & objects, & out_filename, trans, outputs,
340
- tmpdir. path ( ) ) ;
341
- }
342
- config:: CrateTypeDylib => {
343
- link_natively ( sess, true , & objects, & out_filename, trans, outputs,
344
- tmpdir. path ( ) ) ;
341
+ _ => {
342
+ link_natively ( sess, crate_type, & objects, & out_filename, trans,
343
+ outputs, tmpdir. path ( ) ) ;
345
344
}
346
345
}
347
346
@@ -609,13 +608,14 @@ fn link_staticlib(sess: &Session, objects: &[PathBuf], out_filename: &Path,
609
608
//
610
609
// This will invoke the system linker/cc to create the resulting file. This
611
610
// links to all upstream files as well.
612
- fn link_natively ( sess : & Session , dylib : bool ,
613
- objects : & [ PathBuf ] , out_filename : & Path ,
611
+ fn link_natively ( sess : & Session ,
612
+ crate_type : config:: CrateType ,
613
+ objects : & [ PathBuf ] ,
614
+ out_filename : & Path ,
614
615
trans : & CrateTranslation ,
615
616
outputs : & OutputFilenames ,
616
617
tmpdir : & Path ) {
617
- info ! ( "preparing dylib? ({}) from {:?} to {:?}" , dylib, objects,
618
- out_filename) ;
618
+ info ! ( "preparing {:?} from {:?} to {:?}" , crate_type, objects, out_filename) ;
619
619
620
620
// The invocations of cc share some flags across platforms
621
621
let ( pname, mut cmd) = get_linker ( sess) ;
@@ -624,18 +624,18 @@ fn link_natively(sess: &Session, dylib: bool,
624
624
let root = sess. target_filesearch ( PathKind :: Native ) . get_lib_path ( ) ;
625
625
cmd. args ( & sess. target . target . options . pre_link_args ) ;
626
626
627
- let pre_link_objects = if dylib {
628
- & sess. target . target . options . pre_link_objects_dll
629
- } else {
627
+ let pre_link_objects = if crate_type == config:: CrateTypeExecutable {
630
628
& sess. target . target . options . pre_link_objects_exe
629
+ } else {
630
+ & sess. target . target . options . pre_link_objects_dll
631
631
} ;
632
632
for obj in pre_link_objects {
633
633
cmd. arg ( root. join ( obj) ) ;
634
634
}
635
635
636
636
{
637
637
let mut linker = trans. linker_info . to_linker ( & mut cmd, & sess) ;
638
- link_args ( & mut * linker, sess, dylib , tmpdir,
638
+ link_args ( & mut * linker, sess, crate_type , tmpdir,
639
639
objects, out_filename, outputs) ;
640
640
if !sess. target . target . options . no_compiler_rt {
641
641
linker. link_staticlib ( "compiler-rt" ) ;
@@ -701,7 +701,7 @@ fn link_natively(sess: &Session, dylib: bool,
701
701
702
702
fn link_args ( cmd : & mut Linker ,
703
703
sess : & Session ,
704
- dylib : bool ,
704
+ crate_type : config :: CrateType ,
705
705
tmpdir : & Path ,
706
706
objects : & [ PathBuf ] ,
707
707
out_filename : & Path ,
@@ -722,26 +722,28 @@ fn link_args(cmd: &mut Linker,
722
722
723
723
// If we're building a dynamic library then some platforms need to make sure
724
724
// that all symbols are exported correctly from the dynamic library.
725
- if dylib {
726
- cmd. export_symbols ( tmpdir) ;
725
+ if crate_type != config :: CrateTypeExecutable {
726
+ cmd. export_symbols ( tmpdir, crate_type ) ;
727
727
}
728
728
729
729
// When linking a dynamic library, we put the metadata into a section of the
730
730
// executable. This metadata is in a separate object file from the main
731
731
// object file, so we link that in here.
732
- if dylib {
732
+ if crate_type == config :: CrateTypeDylib {
733
733
cmd. add_object ( & outputs. with_extension ( "metadata.o" ) ) ;
734
734
}
735
735
736
736
// Try to strip as much out of the generated object by removing unused
737
737
// sections if possible. See more comments in linker.rs
738
738
if !sess. opts . cg . link_dead_code {
739
- cmd. gc_sections ( dylib) ;
739
+ let keep_metadata = crate_type == config:: CrateTypeDylib ;
740
+ cmd. gc_sections ( keep_metadata) ;
740
741
}
741
742
742
743
let used_link_args = sess. cstore . used_link_args ( ) ;
743
744
744
- if !dylib && t. options . position_independent_executables {
745
+ if crate_type == config:: CrateTypeExecutable &&
746
+ t. options . position_independent_executables {
745
747
let empty_vec = Vec :: new ( ) ;
746
748
let empty_str = String :: new ( ) ;
747
749
let args = sess. opts . cg . link_args . as_ref ( ) . unwrap_or ( & empty_vec) ;
@@ -796,12 +798,12 @@ fn link_args(cmd: &mut Linker,
796
798
// in this DAG so far because they're only dylibs and dylibs can only depend
797
799
// on other dylibs (e.g. other native deps).
798
800
add_local_native_libraries ( cmd, sess) ;
799
- add_upstream_rust_crates ( cmd, sess, dylib , tmpdir) ;
801
+ add_upstream_rust_crates ( cmd, sess, crate_type , tmpdir) ;
800
802
add_upstream_native_libraries ( cmd, sess) ;
801
803
802
804
// # Telling the linker what we're doing
803
805
804
- if dylib {
806
+ if crate_type != config :: CrateTypeExecutable {
805
807
cmd. build_dylib ( out_filename) ;
806
808
}
807
809
@@ -899,8 +901,10 @@ fn add_local_native_libraries(cmd: &mut Linker, sess: &Session) {
899
901
// Rust crates are not considered at all when creating an rlib output. All
900
902
// dependencies will be linked when producing the final output (instead of
901
903
// the intermediate rlib version)
902
- fn add_upstream_rust_crates ( cmd : & mut Linker , sess : & Session ,
903
- dylib : bool , tmpdir : & Path ) {
904
+ fn add_upstream_rust_crates ( cmd : & mut Linker ,
905
+ sess : & Session ,
906
+ crate_type : config:: CrateType ,
907
+ tmpdir : & Path ) {
904
908
// All of the heavy lifting has previously been accomplished by the
905
909
// dependency_format module of the compiler. This is just crawling the
906
910
// output of that module, adding crates as necessary.
@@ -910,11 +914,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
910
914
// involves just passing the right -l flag.
911
915
912
916
let formats = sess. dependency_formats . borrow ( ) ;
913
- let data = if dylib {
914
- formats. get ( & config:: CrateTypeDylib ) . unwrap ( )
915
- } else {
916
- formats. get ( & config:: CrateTypeExecutable ) . unwrap ( )
917
- } ;
917
+ let data = formats. get ( & crate_type) . unwrap ( ) ;
918
918
919
919
// Invoke get_used_crates to ensure that we get a topological sorting of
920
920
// crates.
@@ -929,7 +929,8 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
929
929
Linkage :: NotLinked |
930
930
Linkage :: IncludedFromDylib => { }
931
931
Linkage :: Static => {
932
- add_static_crate ( cmd, sess, tmpdir, dylib, & src. rlib . unwrap ( ) . 0 )
932
+ add_static_crate ( cmd, sess, tmpdir, crate_type,
933
+ & src. rlib . unwrap ( ) . 0 )
933
934
}
934
935
Linkage :: Dynamic => {
935
936
add_dynamic_crate ( cmd, sess, & src. dylib . unwrap ( ) . 0 )
@@ -974,9 +975,12 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
974
975
// (aka we're making an executable), we can just pass the rlib blindly to
975
976
// the linker (fast) because it's fine if it's not actually included as
976
977
// we're at the end of the dependency chain.
977
- fn add_static_crate ( cmd : & mut Linker , sess : & Session , tmpdir : & Path ,
978
- dylib : bool , cratepath : & Path ) {
979
- if !sess. lto ( ) && !dylib {
978
+ fn add_static_crate ( cmd : & mut Linker ,
979
+ sess : & Session ,
980
+ tmpdir : & Path ,
981
+ crate_type : config:: CrateType ,
982
+ cratepath : & Path ) {
983
+ if !sess. lto ( ) && crate_type != config:: CrateTypeDylib {
980
984
cmd. link_rlib ( & fix_windows_verbatim_for_gcc ( cratepath) ) ;
981
985
return
982
986
}
@@ -1012,7 +1016,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
1012
1016
1013
1017
if any_objects {
1014
1018
archive. build ( ) ;
1015
- if dylib {
1019
+ if crate_type == config :: CrateTypeDylib {
1016
1020
cmd. link_whole_rlib ( & fix_windows_verbatim_for_gcc ( & dst) ) ;
1017
1021
} else {
1018
1022
cmd. link_rlib ( & fix_windows_verbatim_for_gcc ( & dst) ) ;
0 commit comments