@@ -22,7 +22,7 @@ use mem;
22
22
use ops:: * ;
23
23
use option:: * ;
24
24
use os;
25
- use path:: GenericPath ;
25
+ use path:: { Path , GenericPath } ;
26
26
use path;
27
27
use result:: * ;
28
28
use slice:: { Vector , OwnedVector } ;
@@ -47,7 +47,7 @@ impl Drop for DynamicLibrary {
47
47
impl DynamicLibrary {
48
48
/// Lazily open a dynamic library. When passed None it gives a
49
49
/// handle to the calling process
50
- pub fn open ( filename : Option < & path :: Path > ) -> Result < DynamicLibrary , ~str > {
50
+ pub fn open ( filename : Option < & Path > ) -> Result < DynamicLibrary , ~str > {
51
51
unsafe {
52
52
let maybe_library = dl:: check_for_errors_in ( || {
53
53
match filename {
@@ -66,20 +66,95 @@ impl DynamicLibrary {
66
66
}
67
67
}
68
68
69
- /// Appends a path to the system search path for dynamic libraries
70
- pub fn add_search_path ( path : & path:: Path ) {
71
- let ( envvar, sep) = if cfg ! ( windows) {
72
- ( "PATH" , ';' as u8 )
69
+ /// Prepends a path to this process's search path for dynamic libraries
70
+ pub fn prepend_search_path ( path : & Path ) {
71
+ let mut search_path = DynamicLibrary :: search_path ( ) ;
72
+ search_path. insert ( 0 , path. clone ( ) ) ;
73
+ let newval = DynamicLibrary :: create_path ( search_path. as_slice ( ) ) ;
74
+ os:: setenv ( DynamicLibrary :: envvar ( ) ,
75
+ str:: from_utf8 ( newval. as_slice ( ) ) . unwrap ( ) ) ;
76
+ }
77
+
78
+ /// From a slice of paths, create a new vector which is suitable to be an
79
+ /// environment variable for this platforms dylib search path.
80
+ pub fn create_path ( path : & [ Path ] ) -> Vec < u8 > {
81
+ let mut newvar = Vec :: new ( ) ;
82
+ for ( i, path) in path. iter ( ) . enumerate ( ) {
83
+ if i > 0 { newvar. push ( DynamicLibrary :: separator ( ) ) ; }
84
+ newvar. push_all ( path. as_vec ( ) ) ;
85
+ }
86
+ return newvar;
87
+ }
88
+
89
+ /// Returns the environment variable for this process's dynamic library
90
+ /// search path
91
+ pub fn envvar ( ) -> & ' static str {
92
+ if cfg ! ( windows) {
93
+ "PATH"
73
94
} else if cfg ! ( target_os = "macos" ) {
74
- ( "DYLD_LIBRARY_PATH" , ':' as u8 )
95
+ "DYLD_LIBRARY_PATH"
75
96
} else {
97
+ "LD_LIBRARY_PATH"
98
+ }
99
+ }
100
+
101
+ <<<<<<< HEAD
76
102
( "LD_LIBRARY_PATH" , ':' as u8 )
77
103
} ;
78
104
let newenv = os:: getenv_as_bytes ( envvar ) . unwrap_or ( box [ ] ) ;
79
105
let mut newenv = newenv. move_iter( ) . collect :: < Vec < _ > > ( ) ;
80
106
newenv. push_all( & [ sep] ) ;
81
107
newenv. push_all( path. as_vec( ) ) ;
82
108
os:: setenv( envvar, str:: from_utf8( newenv. as_slice( ) ) . unwrap( ) ) ;
109
+ ||||||| parent of 943 b4dc... Fixing rustdoc stage1.
110
+ ( "LD_LIBRARY_PATH" , ':' as u8 )
111
+ } ;
112
+ let newenv = os:: getenv_as_bytes( envvar) . unwrap_or( box [ ] ) ;
113
+ let newenv = newenv + & [ sep] + path. as_vec( ) ;
114
+ os:: setenv( envvar, str:: from_utf8( newenv) . unwrap( ) ) ;
115
+ =======
116
+ "LD_LIBRARY_PATH"
117
+ }
118
+ }
119
+
120
+ fn separator( ) -> u8 {
121
+ if cfg ! ( windows) { ';' as u8} else { ':' as u8}
122
+ }
123
+
124
+ /// Returns the current search path for dynamic libraries being used by this
125
+ /// process
126
+ pub fn search_path( ) -> Vec < Path > {
127
+ let mut ret = Vec :: new( ) ;
128
+ match os:: getenv_as_bytes( DynamicLibrary :: envvar( ) ) {
129
+ Some ( env) => {
130
+ for portion in env. split( |a| * a == DynamicLibrary :: separator( ) ) {
131
+ ret. push( Path :: new( portion) ) ;
132
+ }
133
+ }
134
+ None => { }
135
+ }
136
+ return ret;
137
+ }
138
+
139
+ /// From a slice of paths, create a new vector which is suitable to be an
140
+ /// environment variable for this platforms dylib search path.
141
+ pub fn create_path( path: & [ Path ] ) -> Vec < u8 > {
142
+ let mut newvar = Vec :: new( ) ;
143
+ for ( i, path) in path. iter( ) . enumerate( ) {
144
+ if i > 0 { newvar. push( DynamicLibrary :: separator( ) ) ; }
145
+ newvar. push_all( path. as_vec( ) ) ;
146
+ }
147
+ return newvar;
148
+ }
149
+
150
+ /// Prepends a path to this process's search path for dynamic libraries
151
+ pub fn prepend_search_path( path: & Path ) {
152
+ let mut search_path = DynamicLibrary :: search_path( ) ;
153
+ search_path. insert( 0 , path. clone( ) ) ;
154
+ let newval = DynamicLibrary :: create_path( search_path. as_slice( ) ) ;
155
+ os:: setenv( DynamicLibrary :: envvar( ) ,
156
+ str:: from_utf8( newval. as_slice( ) ) . unwrap( ) ) ;
157
+ >>>>>>> 943 b4dc... Fixing rustdoc stage1.
83
158
}
84
159
85
160
/// Access the value at the symbol of the dynamic library
@@ -155,14 +230,14 @@ mod test {
155
230
#[ cfg( target_os = "macos" ) ]
156
231
#[ cfg( target_os = "freebsd" ) ]
157
232
pub mod dl {
233
+ use prelude:: * ;
234
+
158
235
use c_str:: ToCStr ;
159
236
use libc;
160
- use path;
161
237
use ptr;
162
238
use str ;
163
- use result:: * ;
164
239
165
- pub unsafe fn open_external ( filename : & path :: Path ) -> * u8 {
240
+ pub unsafe fn open_external ( filename : & Path ) -> * u8 {
166
241
filename. with_c_str ( |raw_name| {
167
242
dlopen( raw_name, Lazy as libc:: c_int) as * u8
168
243
} )
@@ -219,14 +294,13 @@ pub mod dl {
219
294
220
295
#[ cfg( target_os = "win32") ]
221
296
pub mod dl {
297
+ use prelude:: * ;
298
+
222
299
use libc;
223
300
use os;
224
- use path:: GenericPath ;
225
- use path;
226
301
use ptr;
227
- use result:: { Ok , Err , Result } ;
228
302
229
- pub unsafe fn open_external ( filename : & path :: Path ) -> * u8 {
303
+ pub unsafe fn open_external( filename: & Path ) -> * u8 {
230
304
os:: win32:: as_utf16_p( filename. as_str( ) . unwrap( ) , |raw_name| {
231
305
LoadLibraryW ( raw_name as * libc:: c_void ) as * u8
232
306
} )
0 commit comments