1
1
import std:: os;
2
2
import std:: fs;
3
+ import std:: os_fs;
3
4
import std:: vec;
5
+ import std:: map;
6
+ import std:: str;
7
+ import std:: uint;
4
8
import metadata:: cstore;
5
9
import driver:: session;
6
10
import util:: filesearch;
7
- import std:: map;
8
11
9
12
export get_rpath_flags, test;
10
13
@@ -70,18 +73,44 @@ fn get_rpaths_relative_to_output(cwd: fs::path,
70
73
vec:: map ( bind get_rpath_relative_to_output ( cwd, output, _) , libs)
71
74
}
72
75
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 (
78
80
get_absolute ( cwd, output) ,
79
- get_absolute(cwd, lib))*/
81
+ get_absolute ( cwd, lib) )
80
82
}
81
83
82
84
// 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 > 0 u;
95
+ assert len2 > 0 u;
96
+
97
+ let max_common_path = uint:: min ( len1, len2) - 1 u;
98
+ let start_idx = 0 u;
99
+ while start_idx < max_common_path
100
+ && split1[ start_idx] == split2[ start_idx] {
101
+ start_idx += 1 u;
102
+ }
103
+
104
+ let path = [ ] ;
105
+
106
+ for each _ in uint:: range ( start_idx, len1 - 1 u) {
107
+ path += [ ".." ] ;
108
+ }
109
+
110
+ path += vec:: slice ( split2, start_idx, len2 - 1 u) ;
111
+
112
+ check vec:: is_not_empty ( path) ;
113
+ ret fs:: connect_many ( path) ;
85
114
}
86
115
87
116
fn get_absolute_rpaths ( cwd : fs:: path , libs : [ fs:: path ] ) -> [ str ] {
@@ -159,7 +188,6 @@ mod test {
159
188
}
160
189
161
190
#[ test]
162
- #[ ignore]
163
191
fn test_relative_to1 ( ) {
164
192
let p1 = "/usr/bin/rustc" ;
165
193
let p2 = "/usr/lib/mylib" ;
@@ -168,7 +196,6 @@ mod test {
168
196
}
169
197
170
198
#[ test]
171
- #[ ignore]
172
199
fn test_relative_to2 ( ) {
173
200
let p1 = "/usr/bin/rustc" ;
174
201
let p2 = "/usr/bin/../lib/mylib" ;
@@ -177,7 +204,6 @@ mod test {
177
204
}
178
205
179
206
#[ test]
180
- #[ ignore]
181
207
fn test_relative_to3 ( ) {
182
208
let p1 = "/usr/bin/whatever/rustc" ;
183
209
let p2 = "/usr/lib/whatever/mylib" ;
@@ -186,7 +212,6 @@ mod test {
186
212
}
187
213
188
214
#[ test]
189
- #[ ignore]
190
215
fn test_relative_to4 ( ) {
191
216
let p1 = "/usr/bin/whatever/../rustc" ;
192
217
let p2 = "/usr/lib/whatever/mylib" ;
@@ -195,11 +220,33 @@ mod test {
195
220
}
196
221
197
222
#[ test]
198
- #[ ignore]
199
223
fn test_relative_to5 ( ) {
200
224
let p1 = "/usr/bin/whatever/../rustc" ;
201
225
let p2 = "/usr/lib/whatever/../mylib" ;
202
226
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" ;
204
251
}
205
252
}
0 commit comments