@@ -303,13 +303,19 @@ pub(crate) trait Linker {
303
303
crate_type : CrateType ,
304
304
out_filename : & Path ,
305
305
) ;
306
- fn link_dylib_by_name ( & mut self , _name : & str , _verbatim : bool , _as_needed : bool ) {
306
+ fn link_dylib_by_name ( & mut self , _name : & str , _verbatim : bool , _as_needed : bool , _weak : bool ) {
307
307
bug ! ( "dylib linked with unsupported linker" )
308
308
}
309
- fn link_dylib_by_path ( & mut self , _path : & Path , _as_needed : bool ) {
309
+ fn link_dylib_by_path ( & mut self , _path : & Path , _as_needed : bool , _weak : bool ) {
310
310
bug ! ( "dylib linked with unsupported linker" )
311
311
}
312
- fn link_framework_by_name ( & mut self , _name : & str , _verbatim : bool , _as_needed : bool ) {
312
+ fn link_framework_by_name (
313
+ & mut self ,
314
+ _name : & str ,
315
+ _verbatim : bool ,
316
+ _as_needed : bool ,
317
+ _weak : bool ,
318
+ ) {
313
319
bug ! ( "framework linked with unsupported linker" )
314
320
}
315
321
fn link_staticlib_by_name ( & mut self , name : & str , verbatim : bool , whole_archive : bool ) ;
@@ -569,7 +575,7 @@ impl<'a> Linker for GccLinker<'a> {
569
575
}
570
576
}
571
577
572
- fn link_dylib_by_name ( & mut self , name : & str , verbatim : bool , as_needed : bool ) {
578
+ fn link_dylib_by_name ( & mut self , name : & str , verbatim : bool , as_needed : bool , weak : bool ) {
573
579
if self . sess . target . os == "illumos" && name == "c" {
574
580
// libc will be added via late_link_args on illumos so that it will
575
581
// appear last in the library search order.
@@ -581,26 +587,39 @@ impl<'a> Linker for GccLinker<'a> {
581
587
self . hint_dynamic ( ) ;
582
588
self . with_as_needed ( as_needed, |this| {
583
589
let colon = if verbatim && this. is_gnu { ":" } else { "" } ;
584
- this. link_or_cc_arg ( format ! ( "-l{colon}{name}" ) ) ;
590
+ if weak {
591
+ this. link_or_cc_arg ( format ! ( "-weak-l{colon}{name}" ) ) ;
592
+ } else {
593
+ this. link_or_cc_arg ( format ! ( "-l{colon}{name}" ) ) ;
594
+ }
585
595
} ) ;
586
596
}
587
597
588
- fn link_dylib_by_path ( & mut self , path : & Path , as_needed : bool ) {
598
+ fn link_dylib_by_path ( & mut self , path : & Path , as_needed : bool , weak : bool ) {
589
599
self . hint_dynamic ( ) ;
590
600
self . with_as_needed ( as_needed, |this| {
591
- this. link_or_cc_arg ( path) ;
601
+ if weak {
602
+ this. link_or_cc_arg ( "-weak_library" ) ;
603
+ this. link_or_cc_arg ( path) ;
604
+ } else {
605
+ this. link_or_cc_arg ( path) ;
606
+ }
592
607
} )
593
608
}
594
609
595
- fn link_framework_by_name ( & mut self , name : & str , _verbatim : bool , as_needed : bool ) {
610
+ fn link_framework_by_name ( & mut self , name : & str , _verbatim : bool , as_needed : bool , weak : bool ) {
596
611
self . hint_dynamic ( ) ;
597
612
if !as_needed {
598
613
// FIXME(81490): ld64 as of macOS 11 supports the -needed_framework
599
614
// flag but we have no way to detect that here.
600
615
// self.link_or_cc_arg("-needed_framework").link_or_cc_arg(name);
601
616
self . sess . dcx ( ) . emit_warn ( errors:: Ld64UnimplementedModifier ) ;
602
617
}
603
- self . link_or_cc_args ( & [ "-framework" , name] ) ;
618
+ if weak {
619
+ self . link_or_cc_args ( & [ "-weak_framework" , name] ) ;
620
+ } else {
621
+ self . link_or_cc_args ( & [ "-framework" , name] ) ;
622
+ }
604
623
}
605
624
606
625
fn link_staticlib_by_name ( & mut self , name : & str , verbatim : bool , whole_archive : bool ) {
@@ -923,7 +942,7 @@ impl<'a> Linker for MsvcLinker<'a> {
923
942
}
924
943
}
925
944
926
- fn link_dylib_by_name ( & mut self , name : & str , verbatim : bool , _as_needed : bool ) {
945
+ fn link_dylib_by_name ( & mut self , name : & str , verbatim : bool , _as_needed : bool , _weak : bool ) {
927
946
// On MSVC-like targets rustc supports import libraries using alternative naming
928
947
// scheme (`libfoo.a`) unsupported by linker, search for such libraries manually.
929
948
if let Some ( path) = try_find_native_dynamic_library ( self . sess , name, verbatim) {
@@ -933,7 +952,7 @@ impl<'a> Linker for MsvcLinker<'a> {
933
952
}
934
953
}
935
954
936
- fn link_dylib_by_path ( & mut self , path : & Path , _as_needed : bool ) {
955
+ fn link_dylib_by_path ( & mut self , path : & Path , _as_needed : bool , _weak : bool ) {
937
956
// When producing a dll, MSVC linker may not emit an implib file if the dll doesn't export
938
957
// any symbols, so we skip linking if the implib file is not present.
939
958
let implib_path = path. with_extension ( "dll.lib" ) ;
@@ -1171,12 +1190,12 @@ impl<'a> Linker for EmLinker<'a> {
1171
1190
) {
1172
1191
}
1173
1192
1174
- fn link_dylib_by_name ( & mut self , name : & str , _verbatim : bool , _as_needed : bool ) {
1193
+ fn link_dylib_by_name ( & mut self , name : & str , _verbatim : bool , _as_needed : bool , _weak : bool ) {
1175
1194
// Emscripten always links statically
1176
1195
self . link_or_cc_args ( & [ "-l" , name] ) ;
1177
1196
}
1178
1197
1179
- fn link_dylib_by_path ( & mut self , path : & Path , _as_needed : bool ) {
1198
+ fn link_dylib_by_path ( & mut self , path : & Path , _as_needed : bool , _weak : bool ) {
1180
1199
self . link_or_cc_arg ( path) ;
1181
1200
}
1182
1201
@@ -1338,11 +1357,11 @@ impl<'a> Linker for WasmLd<'a> {
1338
1357
}
1339
1358
}
1340
1359
1341
- fn link_dylib_by_name ( & mut self , name : & str , _verbatim : bool , _as_needed : bool ) {
1360
+ fn link_dylib_by_name ( & mut self , name : & str , _verbatim : bool , _as_needed : bool , _weak : bool ) {
1342
1361
self . link_or_cc_args ( & [ "-l" , name] ) ;
1343
1362
}
1344
1363
1345
- fn link_dylib_by_path ( & mut self , path : & Path , _as_needed : bool ) {
1364
+ fn link_dylib_by_path ( & mut self , path : & Path , _as_needed : bool , _weak : bool ) {
1346
1365
self . link_or_cc_arg ( path) ;
1347
1366
}
1348
1367
@@ -1643,12 +1662,12 @@ impl<'a> Linker for AixLinker<'a> {
1643
1662
}
1644
1663
}
1645
1664
1646
- fn link_dylib_by_name ( & mut self , name : & str , _verbatim : bool , _as_needed : bool ) {
1665
+ fn link_dylib_by_name ( & mut self , name : & str , _verbatim : bool , _as_needed : bool , _weak : bool ) {
1647
1666
self . hint_dynamic ( ) ;
1648
1667
self . link_or_cc_arg ( format ! ( "-l{name}" ) ) ;
1649
1668
}
1650
1669
1651
- fn link_dylib_by_path ( & mut self , path : & Path , _as_needed : bool ) {
1670
+ fn link_dylib_by_path ( & mut self , path : & Path , _as_needed : bool , _weak : bool ) {
1652
1671
self . hint_dynamic ( ) ;
1653
1672
self . link_or_cc_arg ( path) ;
1654
1673
}
0 commit comments