Skip to content

Commit cb6f8fa

Browse files
committed
Support rpath with -Clinker-flavor=ld
Using `cc_args` panics when using `-Clinker-flavor=ld`, because the arguments are in a form tailored for `-Clinker-flavor=gcc`. So instead, we use `link_args` and let that wrap the arguments with the appropriate `-Wl` or `-Xlinker` when needed.
1 parent 45791dd commit cb6f8fa

File tree

4 files changed

+21
-47
lines changed

4 files changed

+21
-47
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,7 @@ fn link_sanitizer_runtime(
13841384
let filename = format!("rustc{channel}_rt.{name}");
13851385
let path = find_sanitizer_runtime(sess, &filename);
13861386
let rpath = path.to_str().expect("non-utf8 component in path");
1387-
linker.cc_args(&["-Wl,-rpath", "-Xlinker", rpath]);
1387+
linker.link_args(&["-rpath", rpath]);
13881388
linker.link_dylib_by_name(&filename, false, true);
13891389
} else if sess.target.is_like_msvc && flavor == LinkerFlavor::Msvc(Lld::No) && name == "asan" {
13901390
// MSVC provides the `/INFERASANLIBS` argument to automatically find the
@@ -2208,7 +2208,7 @@ fn add_rpath_args(
22082208
is_like_osx: sess.target.is_like_osx,
22092209
linker_is_gnu: sess.target.linker_flavor.is_gnu(),
22102210
};
2211-
cmd.cc_args(&rpath::get_rpath_flags(&rpath_config));
2211+
cmd.link_args(&rpath::get_rpath_linker_args(&rpath_config));
22122212
}
22132213
}
22142214

compiler/rustc_codegen_ssa/src/back/linker/tests.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
use super::*;
22

3+
#[test]
4+
fn test_rpaths_to_args() {
5+
let mut cmd = Command::new("foo");
6+
convert_link_args_to_cc_args(&mut cmd, &["-rpath", "path1", "-rpath", "path2"]);
7+
assert_eq!(cmd.get_args(), [OsStr::new("-Wl,-rpath,path1,-rpath,path2")]);
8+
}
9+
310
#[test]
411
fn test_xlinker() {
512
let mut cmd = Command::new("foo");

compiler/rustc_codegen_ssa/src/back/rpath.rs

+11-23
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,27 @@ pub(super) struct RPathConfig<'a> {
1313
pub linker_is_gnu: bool,
1414
}
1515

16-
pub(super) fn get_rpath_flags(config: &RPathConfig<'_>) -> Vec<OsString> {
16+
pub(super) fn get_rpath_linker_args(config: &RPathConfig<'_>) -> Vec<OsString> {
1717
debug!("preparing the RPATH!");
1818

1919
let rpaths = get_rpaths(config);
20-
let mut flags = rpaths_to_flags(rpaths);
20+
let mut args = Vec::with_capacity(rpaths.len() * 2); // the minimum needed capacity
21+
22+
for rpath in rpaths {
23+
args.push("-rpath".into());
24+
args.push(rpath);
25+
}
2126

2227
if config.linker_is_gnu {
2328
// Use DT_RUNPATH instead of DT_RPATH if available
24-
flags.push("-Wl,--enable-new-dtags".into());
29+
args.push("--enable-new-dtags".into());
2530

2631
// Set DF_ORIGIN for substitute $ORIGIN
27-
flags.push("-Wl,-z,origin".into());
28-
}
29-
30-
flags
31-
}
32-
33-
fn rpaths_to_flags(rpaths: Vec<OsString>) -> Vec<OsString> {
34-
let mut ret = Vec::with_capacity(rpaths.len()); // the minimum needed capacity
35-
36-
for rpath in rpaths {
37-
if rpath.to_string_lossy().contains(',') {
38-
ret.push("-Wl,-rpath".into());
39-
ret.push("-Xlinker".into());
40-
ret.push(rpath);
41-
} else {
42-
let mut single_arg = OsString::from("-Wl,-rpath,");
43-
single_arg.push(rpath);
44-
ret.push(single_arg);
45-
}
32+
args.push("-z".into());
33+
args.push("origin".into());
4634
}
4735

48-
ret
36+
args
4937
}
5038

5139
fn get_rpaths(config: &RPathConfig<'_>) -> Vec<OsString> {

compiler/rustc_codegen_ssa/src/back/rpath/tests.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
use std::ffi::OsString;
2-
use std::path::{Path, PathBuf};
3-
4-
use super::{RPathConfig, get_rpath_relative_to_output, minimize_rpaths, rpaths_to_flags};
5-
6-
#[test]
7-
fn test_rpaths_to_flags() {
8-
let flags = rpaths_to_flags(vec!["path1".into(), "path2".into()]);
9-
assert_eq!(flags, ["-Wl,-rpath,path1", "-Wl,-rpath,path2"]);
10-
}
1+
use super::*;
112

123
#[test]
134
fn test_minimize1() {
@@ -69,15 +60,3 @@ fn test_rpath_relative_issue_119571() {
6960
// Should not panic when lib only contains filename.
7061
let _ = get_rpath_relative_to_output(config, Path::new("libstd.so"));
7162
}
72-
73-
#[test]
74-
fn test_xlinker() {
75-
let args = rpaths_to_flags(vec!["a/normal/path".into(), "a,comma,path".into()]);
76-
77-
assert_eq!(args, vec![
78-
OsString::from("-Wl,-rpath,a/normal/path"),
79-
OsString::from("-Wl,-rpath"),
80-
OsString::from("-Xlinker"),
81-
OsString::from("a,comma,path")
82-
]);
83-
}

0 commit comments

Comments
 (0)