@@ -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,10 +624,10 @@ 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) ) ;
@@ -639,7 +639,7 @@ fn link_natively(sess: &Session, dylib: bool,
639
639
} else {
640
640
Box :: new ( GnuLinker { cmd : & mut cmd, sess : & sess } ) as Box < Linker >
641
641
} ;
642
- link_args ( & mut * linker, sess, dylib , tmpdir,
642
+ link_args ( & mut * linker, sess, crate_type , tmpdir,
643
643
objects, out_filename, trans, outputs) ;
644
644
if !sess. target . target . options . no_compiler_rt {
645
645
linker. link_staticlib ( "compiler-rt" ) ;
@@ -705,7 +705,7 @@ fn link_natively(sess: &Session, dylib: bool,
705
705
706
706
fn link_args ( cmd : & mut Linker ,
707
707
sess : & Session ,
708
- dylib : bool ,
708
+ crate_type : config :: CrateType ,
709
709
tmpdir : & Path ,
710
710
objects : & [ PathBuf ] ,
711
711
out_filename : & Path ,
@@ -727,26 +727,28 @@ fn link_args(cmd: &mut Linker,
727
727
728
728
// If we're building a dynamic library then some platforms need to make sure
729
729
// that all symbols are exported correctly from the dynamic library.
730
- if dylib {
731
- cmd. export_symbols ( sess, trans, tmpdir) ;
730
+ if crate_type != config :: CrateTypeExecutable {
731
+ cmd. export_symbols ( sess, trans, tmpdir, crate_type ) ;
732
732
}
733
733
734
734
// When linking a dynamic library, we put the metadata into a section of the
735
735
// executable. This metadata is in a separate object file from the main
736
736
// object file, so we link that in here.
737
- if dylib {
737
+ if crate_type == config :: CrateTypeDylib {
738
738
cmd. add_object ( & outputs. with_extension ( "metadata.o" ) ) ;
739
739
}
740
740
741
741
// Try to strip as much out of the generated object by removing unused
742
742
// sections if possible. See more comments in linker.rs
743
743
if !sess. opts . cg . link_dead_code {
744
- cmd. gc_sections ( dylib) ;
744
+ let keep_metadata = crate_type == config:: CrateTypeDylib ;
745
+ cmd. gc_sections ( keep_metadata) ;
745
746
}
746
747
747
748
let used_link_args = sess. cstore . used_link_args ( ) ;
748
749
749
- if !dylib && t. options . position_independent_executables {
750
+ if crate_type == config:: CrateTypeExecutable &&
751
+ t. options . position_independent_executables {
750
752
let empty_vec = Vec :: new ( ) ;
751
753
let empty_str = String :: new ( ) ;
752
754
let args = sess. opts . cg . link_args . as_ref ( ) . unwrap_or ( & empty_vec) ;
@@ -801,12 +803,12 @@ fn link_args(cmd: &mut Linker,
801
803
// in this DAG so far because they're only dylibs and dylibs can only depend
802
804
// on other dylibs (e.g. other native deps).
803
805
add_local_native_libraries ( cmd, sess) ;
804
- add_upstream_rust_crates ( cmd, sess, dylib , tmpdir) ;
806
+ add_upstream_rust_crates ( cmd, sess, crate_type , tmpdir) ;
805
807
add_upstream_native_libraries ( cmd, sess) ;
806
808
807
809
// # Telling the linker what we're doing
808
810
809
- if dylib {
811
+ if crate_type != config :: CrateTypeExecutable {
810
812
cmd. build_dylib ( out_filename) ;
811
813
}
812
814
@@ -904,8 +906,10 @@ fn add_local_native_libraries(cmd: &mut Linker, sess: &Session) {
904
906
// Rust crates are not considered at all when creating an rlib output. All
905
907
// dependencies will be linked when producing the final output (instead of
906
908
// the intermediate rlib version)
907
- fn add_upstream_rust_crates ( cmd : & mut Linker , sess : & Session ,
908
- dylib : bool , tmpdir : & Path ) {
909
+ fn add_upstream_rust_crates ( cmd : & mut Linker ,
910
+ sess : & Session ,
911
+ crate_type : config:: CrateType ,
912
+ tmpdir : & Path ) {
909
913
// All of the heavy lifting has previously been accomplished by the
910
914
// dependency_format module of the compiler. This is just crawling the
911
915
// output of that module, adding crates as necessary.
@@ -915,11 +919,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
915
919
// involves just passing the right -l flag.
916
920
917
921
let formats = sess. dependency_formats . borrow ( ) ;
918
- let data = if dylib {
919
- formats. get ( & config:: CrateTypeDylib ) . unwrap ( )
920
- } else {
921
- formats. get ( & config:: CrateTypeExecutable ) . unwrap ( )
922
- } ;
922
+ let data = formats. get ( & crate_type) . unwrap ( ) ;
923
923
924
924
// Invoke get_used_crates to ensure that we get a topological sorting of
925
925
// crates.
@@ -934,7 +934,8 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
934
934
Linkage :: NotLinked |
935
935
Linkage :: IncludedFromDylib => { }
936
936
Linkage :: Static => {
937
- add_static_crate ( cmd, sess, tmpdir, dylib, & src. rlib . unwrap ( ) . 0 )
937
+ add_static_crate ( cmd, sess, tmpdir, crate_type,
938
+ & src. rlib . unwrap ( ) . 0 )
938
939
}
939
940
Linkage :: Dynamic => {
940
941
add_dynamic_crate ( cmd, sess, & src. dylib . unwrap ( ) . 0 )
@@ -979,9 +980,12 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
979
980
// (aka we're making an executable), we can just pass the rlib blindly to
980
981
// the linker (fast) because it's fine if it's not actually included as
981
982
// we're at the end of the dependency chain.
982
- fn add_static_crate ( cmd : & mut Linker , sess : & Session , tmpdir : & Path ,
983
- dylib : bool , cratepath : & Path ) {
984
- if !sess. lto ( ) && !dylib {
983
+ fn add_static_crate ( cmd : & mut Linker ,
984
+ sess : & Session ,
985
+ tmpdir : & Path ,
986
+ crate_type : config:: CrateType ,
987
+ cratepath : & Path ) {
988
+ if !sess. lto ( ) && crate_type != config:: CrateTypeDylib {
985
989
cmd. link_rlib ( & fix_windows_verbatim_for_gcc ( cratepath) ) ;
986
990
return
987
991
}
@@ -1017,7 +1021,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
1017
1021
1018
1022
if any_objects {
1019
1023
archive. build ( ) ;
1020
- if dylib {
1024
+ if crate_type == config :: CrateTypeDylib {
1021
1025
cmd. link_whole_rlib ( & fix_windows_verbatim_for_gcc ( & dst) ) ;
1022
1026
} else {
1023
1027
cmd. link_rlib ( & fix_windows_verbatim_for_gcc ( & dst) ) ;
0 commit comments