Skip to content

Commit ce25dab

Browse files
committed
linker: Make argument building interface in trait Linker richer
by redirecting everything to `Command`
1 parent 6dee5f1 commit ce25dab

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

src/librustc_codegen_ssa/back/link.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ fn link_sanitizer_runtime(sess: &Session, crate_type: config::CrateType, linker:
787787
// PR #41352 for details).
788788
let libname = format!("rustc{}_rt.{}", channel, name);
789789
let rpath = default_tlib.to_str().expect("non-utf8 component in path");
790-
linker.args(&["-Wl,-rpath".into(), "-Xlinker".into(), rpath.into()]);
790+
linker.args(&["-Wl,-rpath", "-Xlinker", rpath]);
791791
linker.link_dylib(Symbol::intern(&libname));
792792
}
793793
"x86_64-unknown-linux-gnu" | "x86_64-fuchsia" | "aarch64-fuchsia" => {
@@ -1300,16 +1300,15 @@ fn link_args<'a, B: ArchiveBuilder<'a>>(
13001300
}
13011301

13021302
let attr_link_args = codegen_results.crate_info.link_args.iter();
1303-
let user_link_args: Vec<_> =
1304-
sess.opts.cg.link_args.iter().chain(attr_link_args).cloned().collect();
1303+
let user_link_args = sess.opts.cg.link_args.iter().chain(attr_link_args);
13051304

13061305
if crate_type == config::CrateType::Executable {
13071306
let mut position_independent_executable = false;
13081307

13091308
if t.options.position_independent_executables {
13101309
if is_pic(sess)
13111310
&& !sess.crt_static(Some(crate_type))
1312-
&& !user_link_args.iter().any(|x| x == "-static")
1311+
&& !user_link_args.clone().any(|x| x == "-static")
13131312
{
13141313
position_independent_executable = true;
13151314
}
@@ -1440,7 +1439,7 @@ fn link_args<'a, B: ArchiveBuilder<'a>>(
14401439

14411440
// Finally add all the linker arguments provided on the command line along
14421441
// with any #[link_args] attributes found inside the crate
1443-
cmd.args(&user_link_args);
1442+
cmd.args(user_link_args);
14441443
}
14451444

14461445
// # Native library linking

src/librustc_codegen_ssa/back/linker.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl LinkerInfo {
8787
/// used to dispatch on whether a GNU-like linker (generally `ld.exe`) or an
8888
/// MSVC linker (e.g., `link.exe`) is being used.
8989
pub trait Linker {
90+
fn cmd(&mut self) -> &mut Command;
9091
fn link_dylib(&mut self, lib: Symbol);
9192
fn link_rust_dylib(&mut self, lib: Symbol, path: &Path);
9293
fn link_framework(&mut self, framework: Symbol);
@@ -111,7 +112,6 @@ pub trait Linker {
111112
fn no_default_libraries(&mut self);
112113
fn build_dylib(&mut self, out_filename: &Path);
113114
fn build_static_executable(&mut self);
114-
fn args(&mut self, args: &[String]);
115115
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType);
116116
fn subsystem(&mut self, subsystem: &str);
117117
fn group_start(&mut self);
@@ -121,6 +121,16 @@ pub trait Linker {
121121
fn finalize(&mut self) -> Command;
122122
}
123123

124+
impl dyn Linker + '_ {
125+
pub fn arg(&mut self, arg: impl AsRef<OsStr>) {
126+
self.cmd().arg(arg);
127+
}
128+
129+
pub fn args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) {
130+
self.cmd().args(args);
131+
}
132+
}
133+
124134
pub struct GccLinker<'a> {
125135
cmd: Command,
126136
sess: &'a Session,
@@ -208,6 +218,9 @@ impl<'a> GccLinker<'a> {
208218
}
209219

210220
impl<'a> Linker for GccLinker<'a> {
221+
fn cmd(&mut self) -> &mut Command {
222+
&mut self.cmd
223+
}
211224
fn link_dylib(&mut self, lib: Symbol) {
212225
self.hint_dynamic();
213226
self.cmd.arg(format!("-l{}", lib));
@@ -251,9 +264,6 @@ impl<'a> Linker for GccLinker<'a> {
251264
fn build_static_executable(&mut self) {
252265
self.cmd.arg("-static");
253266
}
254-
fn args(&mut self, args: &[String]) {
255-
self.cmd.args(args);
256-
}
257267

258268
fn link_rust_dylib(&mut self, lib: Symbol, _path: &Path) {
259269
self.hint_dynamic();
@@ -545,15 +555,15 @@ pub struct MsvcLinker<'a> {
545555
}
546556

547557
impl<'a> Linker for MsvcLinker<'a> {
558+
fn cmd(&mut self) -> &mut Command {
559+
&mut self.cmd
560+
}
548561
fn link_rlib(&mut self, lib: &Path) {
549562
self.cmd.arg(lib);
550563
}
551564
fn add_object(&mut self, path: &Path) {
552565
self.cmd.arg(path);
553566
}
554-
fn args(&mut self, args: &[String]) {
555-
self.cmd.args(args);
556-
}
557567

558568
fn build_dylib(&mut self, out_filename: &Path) {
559569
self.cmd.arg("/DLL");
@@ -778,6 +788,9 @@ pub struct EmLinker<'a> {
778788
}
779789

780790
impl<'a> Linker for EmLinker<'a> {
791+
fn cmd(&mut self) -> &mut Command {
792+
&mut self.cmd
793+
}
781794
fn include_path(&mut self, path: &Path) {
782795
self.cmd.arg("-L").arg(path);
783796
}
@@ -837,10 +850,6 @@ impl<'a> Linker for EmLinker<'a> {
837850
// noop
838851
}
839852

840-
fn args(&mut self, args: &[String]) {
841-
self.cmd.args(args);
842-
}
843-
844853
fn framework_path(&mut self, _path: &Path) {
845854
bug!("frameworks are not supported on Emscripten")
846855
}
@@ -992,6 +1001,10 @@ impl<'a> WasmLd<'a> {
9921001
}
9931002

9941003
impl<'a> Linker for WasmLd<'a> {
1004+
fn cmd(&mut self) -> &mut Command {
1005+
&mut self.cmd
1006+
}
1007+
9951008
fn link_dylib(&mut self, lib: Symbol) {
9961009
self.cmd.arg("-l").sym_arg(lib);
9971010
}
@@ -1030,10 +1043,6 @@ impl<'a> Linker for WasmLd<'a> {
10301043

10311044
fn build_static_executable(&mut self) {}
10321045

1033-
fn args(&mut self, args: &[String]) {
1034-
self.cmd.args(args);
1035-
}
1036-
10371046
fn link_rust_dylib(&mut self, lib: Symbol, _path: &Path) {
10381047
self.cmd.arg("-l").sym_arg(lib);
10391048
}
@@ -1162,6 +1171,10 @@ pub struct PtxLinker<'a> {
11621171
}
11631172

11641173
impl<'a> Linker for PtxLinker<'a> {
1174+
fn cmd(&mut self) -> &mut Command {
1175+
&mut self.cmd
1176+
}
1177+
11651178
fn link_rlib(&mut self, path: &Path) {
11661179
self.cmd.arg("--rlib").arg(path);
11671180
}
@@ -1182,10 +1195,6 @@ impl<'a> Linker for PtxLinker<'a> {
11821195
self.cmd.arg("--bitcode").arg(path);
11831196
}
11841197

1185-
fn args(&mut self, args: &[String]) {
1186-
self.cmd.args(args);
1187-
}
1188-
11891198
fn optimize(&mut self) {
11901199
match self.sess.lto() {
11911200
Lto::Thin | Lto::Fat | Lto::ThinLocal => {

0 commit comments

Comments
 (0)