Skip to content

Commit 42bcccc

Browse files
committed
linker: Cleanup implementations of link_staticlib_*
1 parent e77af0f commit 42bcccc

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

Diff for: compiler/rustc_codegen_ssa/src/back/linker.rs

+24-23
Original file line numberDiff line numberDiff line change
@@ -489,19 +489,19 @@ impl<'a> Linker for GccLinker<'a> {
489489
search_paths: &SearchPaths,
490490
) {
491491
self.hint_static();
492+
let colon = if verbatim && self.is_gnu { ":" } else { "" };
492493
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 {
495-
self.linker_arg("--whole-archive");
496-
self.cmd.arg(format!("-l{}{name}", if verbatim && self.is_gnu { ":" } else { "" }));
497-
self.linker_arg("--no-whole-archive");
498-
} else {
494+
self.cmd.arg(format!("-l{colon}{name}"));
495+
} else if self.sess.target.is_like_osx {
499496
// -force_load is the macOS equivalent of --whole-archive, but it
500497
// involves passing the full path to the library to link.
501498
self.linker_arg("-force_load");
502-
let lib =
503-
find_native_static_library(name, verbatim, search_paths.get(self.sess), self.sess);
504-
self.linker_arg(&lib);
499+
let search_paths = search_paths.get(self.sess);
500+
self.linker_arg(find_native_static_library(name, verbatim, search_paths, self.sess));
501+
} else {
502+
self.linker_arg("--whole-archive");
503+
self.cmd.arg(format!("-l{colon}{name}"));
504+
self.linker_arg("--no-whole-archive");
505505
}
506506
}
507507

@@ -511,9 +511,10 @@ impl<'a> Linker for GccLinker<'a> {
511511
self.cmd.arg(path);
512512
} else if self.sess.target.is_like_osx {
513513
self.linker_arg("-force_load");
514-
self.linker_arg(&path);
514+
self.linker_arg(path);
515515
} else {
516-
self.linker_args(&[OsString::from("--whole-archive"), path.into()]);
516+
self.linker_arg("--whole-archive");
517+
self.linker_arg(path);
517518
self.linker_arg("--no-whole-archive");
518519
}
519520
}
@@ -830,11 +831,9 @@ impl<'a> Linker for MsvcLinker<'a> {
830831
whole_archive: bool,
831832
_search_paths: &SearchPaths,
832833
) {
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-
}
834+
let prefix = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
835+
let suffix = if verbatim { "" } else { ".lib" };
836+
self.cmd.arg(format!("{prefix}{name}{suffix}"));
838837
}
839838

840839
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
@@ -1066,7 +1065,7 @@ impl<'a> Linker for EmLinker<'a> {
10661065
}
10671066

10681067
fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
1069-
self.add_object(path);
1068+
self.cmd.arg(path);
10701069
}
10711070

10721071
fn include_path(&mut self, path: &Path) {
@@ -1398,8 +1397,7 @@ impl<'a> Linker for L4Bender<'a> {
13981397
if !whole_archive {
13991398
self.cmd.arg(format!("-PC{name}"));
14001399
} else {
1401-
self.cmd.arg("--whole-archive").arg(format!("-l{name}"));
1402-
self.cmd.arg("--no-whole-archive");
1400+
self.cmd.arg("--whole-archive").arg(format!("-l{name}")).arg("--no-whole-archive");
14031401
}
14041402
}
14051403

@@ -1583,9 +1581,10 @@ impl<'a> Linker for AixLinker<'a> {
15831581
if !whole_archive {
15841582
self.cmd.arg(format!("-l{name}"));
15851583
} 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()));
1584+
let mut arg = OsString::from("-bkeepfile:");
1585+
let search_path = search_paths.get(self.sess);
1586+
arg.push(find_native_static_library(name, verbatim, search_path, self.sess));
1587+
self.cmd.arg(arg);
15891588
}
15901589
}
15911590

@@ -1594,7 +1593,9 @@ impl<'a> Linker for AixLinker<'a> {
15941593
if !whole_archive {
15951594
self.cmd.arg(path);
15961595
} else {
1597-
self.cmd.arg(format!("-bkeepfile:{}", path.to_str().unwrap()));
1596+
let mut arg = OsString::from("-bkeepfile:");
1597+
arg.push(path);
1598+
self.cmd.arg(arg);
15981599
}
15991600
}
16001601

0 commit comments

Comments
 (0)