Skip to content

Commit d15db6b

Browse files
committed
linker: Merge link_staticlib_* and link_whole_staticlib_*
1 parent 859f37a commit d15db6b

File tree

2 files changed

+86
-130
lines changed

2 files changed

+86
-130
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ fn link_sanitizer_runtime(
12821282
} else {
12831283
let filename = format!("librustc{channel}_rt.{name}.a");
12841284
let path = find_sanitizer_runtime(sess, &filename).join(&filename);
1285-
linker.link_whole_staticlib_by_path(&path);
1285+
linker.link_staticlib_by_path(&path, true);
12861286
}
12871287
}
12881288

@@ -2514,18 +2514,10 @@ fn add_native_libs_from_crate(
25142514
if let Some(filename) = lib.filename {
25152515
// If rlib contains native libs as archives, they are unpacked to tmpdir.
25162516
let path = tmpdir.join(filename.as_str());
2517-
if whole_archive {
2518-
cmd.link_whole_staticlib_by_path(&path);
2519-
} else {
2520-
cmd.link_staticlib_by_path(&path);
2521-
}
2517+
cmd.link_staticlib_by_path(&path, whole_archive);
25222518
}
25232519
} else {
2524-
if whole_archive {
2525-
cmd.link_whole_staticlib_by_name(name, verbatim, search_paths);
2526-
} else {
2527-
cmd.link_staticlib_by_name(name, verbatim)
2528-
}
2520+
cmd.link_staticlib_by_name(name, verbatim, whole_archive, search_paths);
25292521
}
25302522
}
25312523
}
@@ -2539,7 +2531,7 @@ fn add_native_libs_from_crate(
25392531
// link kind is unspecified.
25402532
if !link_output_kind.can_link_dylib() && !sess.target.crt_static_allows_dylibs {
25412533
if link_static {
2542-
cmd.link_staticlib_by_name(name, verbatim)
2534+
cmd.link_staticlib_by_name(name, verbatim, false, search_paths);
25432535
}
25442536
} else {
25452537
if link_dynamic {
@@ -2796,7 +2788,7 @@ fn add_static_crate<'a>(
27962788
} else {
27972789
fix_windows_verbatim_for_gcc(path)
27982790
};
2799-
cmd.link_staticlib_by_path(&rlib_path);
2791+
cmd.link_staticlib_by_path(&rlib_path, false);
28002792
};
28012793

28022794
if !are_upstream_rust_objects_already_included(sess)

compiler/rustc_codegen_ssa/src/back/linker.rs

+81-117
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,14 @@ pub trait Linker {
171171
fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
172172
bug!("framework linked with unsupported linker")
173173
}
174-
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool);
175-
fn link_whole_staticlib_by_name(
174+
fn link_staticlib_by_name(
176175
&mut self,
177176
name: &str,
178177
verbatim: bool,
178+
whole_archive: bool,
179179
search_paths: &SearchPaths,
180180
);
181-
fn link_staticlib_by_path(&mut self, path: &Path);
182-
fn link_whole_staticlib_by_path(&mut self, path: &Path);
181+
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool);
183182
fn include_path(&mut self, path: &Path);
184183
fn framework_path(&mut self, path: &Path);
185184
fn output_filename(&mut self, path: &Path);
@@ -482,26 +481,17 @@ impl<'a> Linker for GccLinker<'a> {
482481
self.cmd.arg("-framework").arg(name);
483482
}
484483

485-
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool) {
486-
self.hint_static();
487-
self.cmd.arg(format!("-l{}{name}", if verbatim && self.is_gnu { ":" } else { "" },));
488-
}
489-
490-
// Here we explicitly ask that the entire archive is included into the
491-
// result artifact. For more details see #15460, but the gist is that
492-
// the linker will strip away any unused objects in the archive if we
493-
// don't otherwise explicitly reference them. This can occur for
494-
// libraries which are just providing bindings, libraries with generic
495-
// functions, etc.
496-
fn link_whole_staticlib_by_name(
484+
fn link_staticlib_by_name(
497485
&mut self,
498486
name: &str,
499487
verbatim: bool,
488+
whole_archive: bool,
500489
search_paths: &SearchPaths,
501490
) {
502491
self.hint_static();
503-
let target = &self.sess.target;
504-
if !target.is_like_osx {
492+
if !whole_archive {
493+
self.cmd.arg(format!("-l{}{name}", if verbatim && self.is_gnu { ":" } else { "" }));
494+
} else if !self.sess.target.is_like_osx {
505495
self.linker_arg("--whole-archive");
506496
self.cmd.arg(format!("-l{}{name}", if verbatim && self.is_gnu { ":" } else { "" }));
507497
self.linker_arg("--no-whole-archive");
@@ -515,14 +505,11 @@ impl<'a> Linker for GccLinker<'a> {
515505
}
516506
}
517507

518-
fn link_staticlib_by_path(&mut self, path: &Path) {
508+
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
519509
self.hint_static();
520-
self.cmd.arg(path);
521-
}
522-
523-
fn link_whole_staticlib_by_path(&mut self, path: &Path) {
524-
self.hint_static();
525-
if self.sess.target.is_like_osx {
510+
if !whole_archive {
511+
self.cmd.arg(path);
512+
} else if self.sess.target.is_like_osx {
526513
self.linker_arg("-force_load");
527514
self.linker_arg(&path);
528515
} else {
@@ -836,27 +823,28 @@ impl<'a> Linker for MsvcLinker<'a> {
836823
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
837824
}
838825

839-
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool) {
840-
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
841-
}
842-
843-
fn link_whole_staticlib_by_name(
826+
fn link_staticlib_by_name(
844827
&mut self,
845828
name: &str,
846829
verbatim: bool,
830+
whole_archive: bool,
847831
_search_paths: &SearchPaths,
848832
) {
849-
self.cmd.arg(format!("/WHOLEARCHIVE:{}{}", name, if verbatim { "" } else { ".lib" }));
850-
}
851-
852-
fn link_staticlib_by_path(&mut self, path: &Path) {
853-
self.cmd.arg(path);
833+
if !whole_archive {
834+
self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
835+
} else {
836+
self.cmd.arg(format!("/WHOLEARCHIVE:{}{}", name, if verbatim { "" } else { ".lib" }));
837+
}
854838
}
855839

856-
fn link_whole_staticlib_by_path(&mut self, path: &Path) {
857-
let mut arg = OsString::from("/WHOLEARCHIVE:");
858-
arg.push(path);
859-
self.cmd.arg(arg);
840+
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
841+
if !whole_archive {
842+
self.cmd.arg(path);
843+
} else {
844+
let mut arg = OsString::from("/WHOLEARCHIVE:");
845+
arg.push(path);
846+
self.cmd.arg(arg);
847+
}
860848
}
861849

862850
fn add_object(&mut self, path: &Path) {
@@ -1062,34 +1050,25 @@ impl<'a> Linker for EmLinker<'a> {
10621050

10631051
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
10641052

1065-
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
1053+
fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
10661054
// Emscripten always links statically
1067-
self.link_staticlib_by_name(name, verbatim);
1068-
}
1069-
1070-
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) {
10711055
self.cmd.arg("-l").arg(name);
10721056
}
10731057

1074-
fn link_whole_staticlib_by_name(
1058+
fn link_staticlib_by_name(
10751059
&mut self,
10761060
name: &str,
1077-
verbatim: bool,
1061+
_verbatim: bool,
1062+
_whole_archive: bool,
10781063
_search_paths: &SearchPaths,
10791064
) {
1080-
// not supported?
1081-
self.link_staticlib_by_name(name, verbatim);
1065+
self.cmd.arg("-l").arg(name);
10821066
}
10831067

1084-
fn link_staticlib_by_path(&mut self, path: &Path) {
1068+
fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
10851069
self.add_object(path);
10861070
}
10871071

1088-
fn link_whole_staticlib_by_path(&mut self, path: &Path) {
1089-
// not supported?
1090-
self.link_staticlib_by_path(path);
1091-
}
1092-
10931072
fn include_path(&mut self, path: &Path) {
10941073
self.cmd.arg("-L").arg(path);
10951074
}
@@ -1255,25 +1234,26 @@ impl<'a> Linker for WasmLd<'a> {
12551234
self.cmd.arg("-l").arg(name);
12561235
}
12571236

1258-
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) {
1259-
self.cmd.arg("-l").arg(name);
1260-
}
1261-
1262-
fn link_whole_staticlib_by_name(
1237+
fn link_staticlib_by_name(
12631238
&mut self,
12641239
name: &str,
12651240
_verbatim: bool,
1241+
whole_archive: bool,
12661242
_search_paths: &SearchPaths,
12671243
) {
1268-
self.cmd.arg("--whole-archive").arg("-l").arg(name).arg("--no-whole-archive");
1269-
}
1270-
1271-
fn link_staticlib_by_path(&mut self, path: &Path) {
1272-
self.cmd.arg(path);
1244+
if !whole_archive {
1245+
self.cmd.arg("-l").arg(name);
1246+
} else {
1247+
self.cmd.arg("--whole-archive").arg("-l").arg(name).arg("--no-whole-archive");
1248+
}
12731249
}
12741250

1275-
fn link_whole_staticlib_by_path(&mut self, path: &Path) {
1276-
self.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive");
1251+
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
1252+
if !whole_archive {
1253+
self.cmd.arg(path);
1254+
} else {
1255+
self.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive");
1256+
}
12771257
}
12781258

12791259
fn include_path(&mut self, path: &Path) {
@@ -1407,30 +1387,29 @@ impl<'a> Linker for L4Bender<'a> {
14071387
bug!("dylibs are not supported on L4Re");
14081388
}
14091389

1410-
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) {
1411-
self.hint_static();
1412-
self.cmd.arg(format!("-PC{name}"));
1413-
}
1414-
1415-
fn link_whole_staticlib_by_name(
1390+
fn link_staticlib_by_name(
14161391
&mut self,
14171392
name: &str,
14181393
_verbatim: bool,
1394+
whole_archive: bool,
14191395
_search_paths: &SearchPaths,
14201396
) {
14211397
self.hint_static();
1422-
self.cmd.arg("--whole-archive").arg(format!("-l{name}"));
1423-
self.cmd.arg("--no-whole-archive");
1424-
}
1425-
1426-
fn link_staticlib_by_path(&mut self, path: &Path) {
1427-
self.hint_static();
1428-
self.cmd.arg(path);
1398+
if !whole_archive {
1399+
self.cmd.arg(format!("-PC{name}"));
1400+
} else {
1401+
self.cmd.arg("--whole-archive").arg(format!("-l{name}"));
1402+
self.cmd.arg("--no-whole-archive");
1403+
}
14291404
}
14301405

1431-
fn link_whole_staticlib_by_path(&mut self, path: &Path) {
1406+
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
14321407
self.hint_static();
1433-
self.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive");
1408+
if !whole_archive {
1409+
self.cmd.arg(path);
1410+
} else {
1411+
self.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive");
1412+
}
14341413
}
14351414

14361415
fn include_path(&mut self, path: &Path) {
@@ -1593,31 +1572,30 @@ impl<'a> Linker for AixLinker<'a> {
15931572
self.cmd.arg(format!("-l{name}"));
15941573
}
15951574

1596-
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) {
1597-
self.hint_static();
1598-
self.cmd.arg(format!("-l{name}"));
1599-
}
1600-
1601-
fn link_whole_staticlib_by_name(
1575+
fn link_staticlib_by_name(
16021576
&mut self,
16031577
name: &str,
16041578
verbatim: bool,
1579+
whole_archive: bool,
16051580
search_paths: &SearchPaths,
16061581
) {
16071582
self.hint_static();
1608-
let lib =
1609-
find_native_static_library(name, verbatim, search_paths.get(self.sess), self.sess);
1610-
self.cmd.arg(format!("-bkeepfile:{}", lib.to_str().unwrap()));
1611-
}
1612-
1613-
fn link_staticlib_by_path(&mut self, path: &Path) {
1614-
self.hint_static();
1615-
self.cmd.arg(path);
1583+
if !whole_archive {
1584+
self.cmd.arg(format!("-l{name}"));
1585+
} else {
1586+
let lib =
1587+
find_native_static_library(name, verbatim, search_paths.get(self.sess), self.sess);
1588+
self.cmd.arg(format!("-bkeepfile:{}", lib.to_str().unwrap()));
1589+
}
16161590
}
16171591

1618-
fn link_whole_staticlib_by_path(&mut self, path: &Path) {
1592+
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
16191593
self.hint_static();
1620-
self.cmd.arg(format!("-bkeepfile:{}", path.to_str().unwrap()));
1594+
if !whole_archive {
1595+
self.cmd.arg(path);
1596+
} else {
1597+
self.cmd.arg(format!("-bkeepfile:{}", path.to_str().unwrap()));
1598+
}
16211599
}
16221600

16231601
fn include_path(&mut self, path: &Path) {
@@ -1810,24 +1788,17 @@ impl<'a> Linker for PtxLinker<'a> {
18101788
panic!("external dylibs not supported")
18111789
}
18121790

1813-
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool) {
1814-
panic!("staticlibs not supported")
1815-
}
1816-
1817-
fn link_whole_staticlib_by_name(
1791+
fn link_staticlib_by_name(
18181792
&mut self,
18191793
_name: &str,
18201794
_verbatim: bool,
1795+
_whole_archive: bool,
18211796
_search_paths: &SearchPaths,
18221797
) {
18231798
panic!("staticlibs not supported")
18241799
}
18251800

1826-
fn link_staticlib_by_path(&mut self, path: &Path) {
1827-
self.cmd.arg("--rlib").arg(path);
1828-
}
1829-
1830-
fn link_whole_staticlib_by_path(&mut self, path: &Path) {
1801+
fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
18311802
self.cmd.arg("--rlib").arg(path);
18321803
}
18331804

@@ -1904,24 +1875,17 @@ impl<'a> Linker for BpfLinker<'a> {
19041875
panic!("external dylibs not supported")
19051876
}
19061877

1907-
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool) {
1908-
panic!("staticlibs not supported")
1909-
}
1910-
1911-
fn link_whole_staticlib_by_name(
1878+
fn link_staticlib_by_name(
19121879
&mut self,
19131880
_name: &str,
19141881
_verbatim: bool,
1882+
_whole_archive: bool,
19151883
_search_paths: &SearchPaths,
19161884
) {
19171885
panic!("staticlibs not supported")
19181886
}
19191887

1920-
fn link_staticlib_by_path(&mut self, path: &Path) {
1921-
self.cmd.arg(path);
1922-
}
1923-
1924-
fn link_whole_staticlib_by_path(&mut self, path: &Path) {
1888+
fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
19251889
self.cmd.arg(path);
19261890
}
19271891

0 commit comments

Comments
 (0)