Skip to content

Commit 8b4601e

Browse files
committed
Figure out the relative path from output to each crate
1 parent 19ba9b4 commit 8b4601e

File tree

1 file changed

+62
-15
lines changed

1 file changed

+62
-15
lines changed

src/comp/back/rpath.rs

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import std::os;
22
import std::fs;
3+
import std::os_fs;
34
import std::vec;
5+
import std::map;
6+
import std::str;
7+
import std::uint;
48
import metadata::cstore;
59
import driver::session;
610
import util::filesearch;
7-
import std::map;
811

912
export get_rpath_flags, test;
1013

@@ -70,18 +73,44 @@ fn get_rpaths_relative_to_output(cwd: fs::path,
7073
vec::map(bind get_rpath_relative_to_output(cwd, output, _), libs)
7174
}
7275

73-
fn get_rpath_relative_to_output(_cwd: fs::path,
74-
_output: fs::path,
75-
_lib: fs::path) -> str {
76-
fail;
77-
/*get_relative_to(
76+
fn get_rpath_relative_to_output(cwd: fs::path,
77+
output: fs::path,
78+
lib: fs::path) -> str {
79+
"$ORIGIN" + fs::path_sep() + get_relative_to(
7880
get_absolute(cwd, output),
79-
get_absolute(cwd, lib))*/
81+
get_absolute(cwd, lib))
8082
}
8183

8284
// Find the relative path from one file to another
83-
fn get_relative_to(_abs1: fs::path, _abs2: fs::path) -> fs::path {
84-
fail;
85+
fn get_relative_to(abs1: fs::path, abs2: fs::path) -> fs::path {
86+
assert fs::path_is_absolute(abs1);
87+
assert fs::path_is_absolute(abs2);
88+
let normal1 = fs::normalize(abs1);
89+
let normal2 = fs::normalize(abs2);
90+
let split1 = str::split(normal1, os_fs::path_sep as u8);
91+
let split2 = str::split(normal2, os_fs::path_sep as u8);
92+
let len1 = vec::len(split1);
93+
let len2 = vec::len(split2);
94+
assert len1 > 0u;
95+
assert len2 > 0u;
96+
97+
let max_common_path = uint::min(len1, len2) - 1u;
98+
let start_idx = 0u;
99+
while start_idx < max_common_path
100+
&& split1[start_idx] == split2[start_idx] {
101+
start_idx += 1u;
102+
}
103+
104+
let path = [];
105+
106+
for each _ in uint::range(start_idx, len1 - 1u) {
107+
path += [".."];
108+
}
109+
110+
path += vec::slice(split2, start_idx, len2 - 1u);
111+
112+
check vec::is_not_empty(path);
113+
ret fs::connect_many(path);
85114
}
86115

87116
fn get_absolute_rpaths(cwd: fs::path, libs: [fs::path]) -> [str] {
@@ -159,7 +188,6 @@ mod test {
159188
}
160189

161190
#[test]
162-
#[ignore]
163191
fn test_relative_to1() {
164192
let p1 = "/usr/bin/rustc";
165193
let p2 = "/usr/lib/mylib";
@@ -168,7 +196,6 @@ mod test {
168196
}
169197

170198
#[test]
171-
#[ignore]
172199
fn test_relative_to2() {
173200
let p1 = "/usr/bin/rustc";
174201
let p2 = "/usr/bin/../lib/mylib";
@@ -177,7 +204,6 @@ mod test {
177204
}
178205

179206
#[test]
180-
#[ignore]
181207
fn test_relative_to3() {
182208
let p1 = "/usr/bin/whatever/rustc";
183209
let p2 = "/usr/lib/whatever/mylib";
@@ -186,7 +212,6 @@ mod test {
186212
}
187213

188214
#[test]
189-
#[ignore]
190215
fn test_relative_to4() {
191216
let p1 = "/usr/bin/whatever/../rustc";
192217
let p2 = "/usr/lib/whatever/mylib";
@@ -195,11 +220,33 @@ mod test {
195220
}
196221

197222
#[test]
198-
#[ignore]
199223
fn test_relative_to5() {
200224
let p1 = "/usr/bin/whatever/../rustc";
201225
let p2 = "/usr/lib/whatever/../mylib";
202226
let res = get_relative_to(p1, p2);
203-
assert res == "../lib/whatever";
227+
assert res == "../lib";
228+
}
229+
230+
#[test]
231+
fn test_relative_to6() {
232+
let p1 = "/1";
233+
let p2 = "/2/3";
234+
let res = get_relative_to(p1, p2);
235+
assert res == "2";
236+
}
237+
238+
#[test]
239+
fn test_relative_to7() {
240+
let p1 = "/1/2";
241+
let p2 = "/3";
242+
let res = get_relative_to(p1, p2);
243+
assert res == "..";
244+
}
245+
246+
#[test]
247+
fn test_rpath_relative() {
248+
let res = get_rpath_relative_to_output(
249+
"/usr", "bin/rustc", "lib/libstd.so");
250+
assert res == "$ORIGIN/../lib";
204251
}
205252
}

0 commit comments

Comments
 (0)