Skip to content

Commit 3d5471f

Browse files
committed
Fix some remaining rpath bugs
1 parent 82ef851 commit 3d5471f

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

src/comp/back/link.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ fn link_binary(sess: session::session,
576576

577577
gcc_args += rpath::get_rpath_flags(sess, saved_out_filename);
578578

579+
log #fmt("gcc link args: %s", str::connect(gcc_args, " "));
579580
// We run 'gcc' here
580581
let err_code = run::run_program(prog, gcc_args);
581582
if 0 != err_code {

src/comp/back/rpath.rs

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,17 @@ export get_rpath_flags, test;
1313

1414
#[cfg(target_os="linux")]
1515
#[cfg(target_os="macos")]
16-
fn get_rpath_flags(_sess: session::session, _out_filename: str) -> [str] {
16+
fn get_rpath_flags(sess: session::session, out_filename: str) -> [str] {
1717
log "preparing the RPATH!";
1818

19-
// FIXME
20-
/*
2119
let cwd = os::getcwd();
2220
let sysroot = sess.filesearch().sysroot();
2321
let output = out_filename;
2422
let libs = cstore::get_used_crate_files(sess.get_cstore());
2523
let target_triple = sess.get_opts().target_triple;
2624
let rpaths = get_rpaths(cwd, sysroot, output, libs, target_triple);
27-
*/
28-
let rpaths = [];
29-
rpaths_to_flags(rpaths)
25+
rpaths_to_flags(rpaths);
26+
[] // FIXME: activate RPATH!
3027
}
3128

3229
#[cfg(target_os="win32")]
@@ -53,14 +50,27 @@ fn get_rpaths(cwd: fs::path, sysroot: fs::path,
5350
// Use relative paths to the libraries. Binaries can be moved
5451
// as long as they maintain the relative relationship to the
5552
// crates they depend on.
56-
let rpaths = get_rpaths_relative_to_output(cwd, output, libs);
53+
let rel_rpaths = get_rpaths_relative_to_output(cwd, output, libs);
5754

5855
// Make backup absolute paths to the libraries. Binaries can
5956
// be moved as long as the crates they link against don't move.
60-
rpaths += get_absolute_rpaths(cwd, libs);
57+
let abs_rpaths = get_absolute_rpaths(cwd, libs);
6158

6259
// And a final backup rpath to the global library location.
63-
rpaths += [get_install_prefix_rpath(target_triple)];
60+
let fallback_rpaths = [get_install_prefix_rpath(target_triple)];
61+
62+
fn log_rpaths(desc: str, rpaths: [str]) {
63+
log #fmt("%s rpaths:", desc);
64+
for rpath in rpaths {
65+
log #fmt(" %s", rpath);
66+
}
67+
}
68+
69+
log_rpaths("relative", rel_rpaths);
70+
log_rpaths("absolute", abs_rpaths);
71+
log_rpaths("fallback", fallback_rpaths);
72+
73+
let rpaths = rel_rpaths + abs_rpaths + fallback_rpaths;
6474

6575
// Remove duplicates
6676
let rpaths = minimize_rpaths(rpaths);
@@ -85,6 +95,8 @@ fn get_rpath_relative_to_output(cwd: fs::path,
8595
fn get_relative_to(abs1: fs::path, abs2: fs::path) -> fs::path {
8696
assert fs::path_is_absolute(abs1);
8797
assert fs::path_is_absolute(abs2);
98+
log #fmt("finding relative path from %s to %s",
99+
abs1, abs2);
88100
let normal1 = fs::normalize(abs1);
89101
let normal2 = fs::normalize(abs2);
90102
let split1 = str::split(normal1, os_fs::path_sep as u8);
@@ -109,16 +121,19 @@ fn get_relative_to(abs1: fs::path, abs2: fs::path) -> fs::path {
109121

110122
path += vec::slice(split2, start_idx, len2 - 1u);
111123

112-
check vec::is_not_empty(path);
113-
ret fs::connect_many(path);
124+
if check vec::is_not_empty(path) {
125+
ret fs::connect_many(path);
126+
} else {
127+
ret ".";
128+
}
114129
}
115130

116131
fn get_absolute_rpaths(cwd: fs::path, libs: [fs::path]) -> [str] {
117132
vec::map(bind get_absolute_rpath(cwd, _), libs)
118133
}
119134

120135
fn get_absolute_rpath(cwd: fs::path, lib: fs::path) -> str {
121-
get_absolute(cwd, lib)
136+
fs::dirname(get_absolute(cwd, lib))
122137
}
123138

124139
fn get_absolute(cwd: fs::path, lib: fs::path) -> fs::path {
@@ -144,9 +159,13 @@ fn get_install_prefix_rpath(target_triple: str) -> str {
144159

145160
fn minimize_rpaths(rpaths: [str]) -> [str] {
146161
let set = map::new_str_hash::<()>();
147-
for rpath in rpaths { set.insert(rpath, ()); }
148162
let minimized = [];
149-
for each rpath in set.keys() { minimized += [rpath]; }
163+
for rpath in rpaths {
164+
if !set.contains_key(rpath) {
165+
minimized += [rpath];
166+
set.insert(rpath, ());
167+
}
168+
}
150169
ret minimized;
151170
}
152171

@@ -182,11 +201,18 @@ mod test {
182201
}
183202

184203
#[test]
185-
fn test_minimize() {
204+
fn test_minimize1() {
186205
let res = minimize_rpaths(["rpath1", "rpath2", "rpath1"]);
187206
assert res == ["rpath1", "rpath2"];
188207
}
189208

209+
#[test]
210+
fn test_minimize2() {
211+
let res = minimize_rpaths(["1a", "2", "2", "1a", "4a",
212+
"1a", "2", "3", "4a", "3"]);
213+
assert res == ["1a", "2", "4a", "3"];
214+
}
215+
190216
#[test]
191217
fn test_relative_to1() {
192218
let p1 = "/usr/bin/rustc";
@@ -243,10 +269,26 @@ mod test {
243269
assert res == "..";
244270
}
245271

272+
#[test]
273+
fn test_relative_to8() {
274+
let p1 = "/home/brian/Dev/rust/build/"
275+
+ "stage2/lib/rustc/i686-unknown-linux-gnu/lib/librustc.so";
276+
let p2 = "/home/brian/Dev/rust/build/stage2/bin/.."
277+
+ "/lib/rustc/i686-unknown-linux-gnu/lib/libstd.so";
278+
let res = get_relative_to(p1, p2);
279+
assert res == ".";
280+
}
281+
246282
#[test]
247283
fn test_rpath_relative() {
248284
let res = get_rpath_relative_to_output(
249285
"/usr", "bin/rustc", "lib/libstd.so");
250286
assert res == "$ORIGIN/../lib";
251287
}
288+
289+
#[test]
290+
fn test_get_absolute_rpath() {
291+
let res = get_absolute_rpath("/usr", "lib/libstd.so");
292+
assert res == "/usr/lib";
293+
}
252294
}

src/comp/util/filesearch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn make_target_lib_path(sysroot: fs::path,
8888

8989
fn get_default_sysroot() -> fs::path {
9090
alt os::get_exe_path() {
91-
option::some(p) { fs::connect(p, "..") }
91+
option::some(p) { fs::normalize(fs::connect(p, "..")) }
9292
option::none. {
9393
fail "can't determine value for sysroot";
9494
}

0 commit comments

Comments
 (0)