Skip to content

Commit 986309f

Browse files
alexcrichtonpnkfelix
authored andcommitted
Fixing rustdoc stage1.
See rust-lang#13983 and rust-lang#14000. Conflicts: src/librustc/metadata/filesearch.rs src/libstd/unstable/dynamic_lib.rs
1 parent fedffa7 commit 986309f

File tree

4 files changed

+138
-56
lines changed

4 files changed

+138
-56
lines changed

Diff for: src/compiletest/procsrv.rs

+18-39
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,30 @@
1111
use std::os;
1212
use std::str;
1313
use std::io::process::{ProcessExit, Process, ProcessConfig, ProcessOutput};
14+
use std::unstable::dynamic_lib::DynamicLibrary;
1415

15-
#[cfg(target_os = "win32")]
16-
fn target_env(lib_path: &str, prog: &str) -> Vec<(~str, ~str)> {
17-
let env = os::env();
18-
19-
// Make sure we include the aux directory in the path
20-
assert!(prog.ends_with(".exe"));
21-
let aux_path = prog.slice(0u, prog.len() - 4u).to_owned() + ".libaux";
22-
23-
let mut new_env: Vec<_> = env.move_iter().map(|(k, v)| {
24-
let new_v = if "PATH" == k {
25-
format!("{};{};{}", v, lib_path, aux_path)
26-
} else {
27-
v
28-
};
29-
(k, new_v)
30-
}).collect();
31-
if prog.ends_with("rustc.exe") {
32-
new_env.push(("RUST_THREADS".to_owned(), "1".to_owned()));
33-
}
34-
return new_env;
35-
}
36-
37-
#[cfg(target_os = "linux")]
38-
#[cfg(target_os = "macos")]
39-
#[cfg(target_os = "freebsd")]
4016
fn target_env(lib_path: &str, prog: &str) -> Vec<(~str,~str)> {
41-
// Make sure we include the aux directory in the path
17+
let prog = if cfg!(windows) {prog.slice_to(prog.len() - 4)} else {prog};
4218
let aux_path = prog + ".libaux";
4319

20+
// Need to be sure to put both the lib_path and the aux path in the dylib
21+
// search path for the child.
22+
let mut path = DynamicLibrary::search_path();
23+
path.insert(0, Path::new(aux_path));
24+
path.insert(0, Path::new(lib_path));
25+
26+
// Remove the previous dylib search path var
27+
let var = DynamicLibrary::envvar();
4428
let mut env: Vec<(~str,~str)> = os::env().move_iter().collect();
45-
let var = if cfg!(target_os = "macos") {
46-
"DYLD_LIBRARY_PATH"
47-
} else {
48-
"LD_LIBRARY_PATH"
29+
match env.iter().position(|&(ref k, _)| k.as_slice() == var) {
30+
Some(i) => { env.remove(i); }
31+
None => {}
4932
};
50-
let prev = match env.iter().position(|&(ref k, _)| k.as_slice() == var) {
51-
Some(i) => env.remove(i).unwrap().val1(),
52-
None => "".to_owned(),
53-
};
54-
env.push((var.to_owned(), if prev.is_empty() {
55-
lib_path + ":" + aux_path
56-
} else {
57-
lib_path + ":" + aux_path + ":" + prev
58-
}));
33+
34+
// Add the new dylib search path var
35+
let newpath = DynamicLibrary::create_path(path.as_slice());
36+
env.push((var.to_owned(),
37+
str::from_utf8(newpath.as_slice()).unwrap().to_owned()));
5938
return env;
6039
}
6140

Diff for: src/librustc/metadata/filesearch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<'a> FileSearch<'a> {
136136

137137
pub fn add_dylib_search_paths(&self) {
138138
self.for_each_lib_search_path(|lib_search_path| {
139-
DynamicLibrary::add_search_path(lib_search_path);
139+
DynamicLibrary::prepend_search_path(lib_search_path);
140140
FileDoesntMatch
141141
})
142142
}

Diff for: src/librustdoc/test.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
use std::cell::RefCell;
1212
use std::char;
1313
use std::io;
14-
use std::io::{Process, TempDir};
14+
use std::io::{Process, TempDir, ProcessConfig};
1515
use std::os;
1616
use std::str;
1717
use std::strbuf::StrBuf;
18+
use std::unstable::dynamic_lib::DynamicLibrary;
1819

1920
use collections::{HashSet, HashMap};
2021
use testing;
@@ -150,13 +151,41 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool,
150151
let outdir = TempDir::new("rustdoctest").expect("rustdoc needs a tempdir");
151152
let out = Some(outdir.path().clone());
152153
let cfg = config::build_configuration(&sess);
154+
let libdir = sess.target_filesearch().get_lib_path();
153155
driver::compile_input(sess, cfg, &input, &out, &None);
154156

155157
if no_run { return }
156158

157159
// Run the code!
160+
//
161+
// We're careful to prepend the *target* dylib search path to the child's
162+
// environment to ensure that the target loads the right libraries at
163+
// runtime. It would be a sad day if the *host* libraries were loaded as a
164+
// mistake.
158165
let exe = outdir.path().join("rust_out");
159-
let out = Process::output(exe.as_str().unwrap(), []);
166+
let env = {
167+
let mut path = DynamicLibrary::search_path();
168+
path.insert(0, libdir.clone());
169+
170+
// Remove the previous dylib search path var
171+
let var = DynamicLibrary::envvar();
172+
let mut env: Vec<(~str,~str)> = os::env().move_iter().collect();
173+
match env.iter().position(|&(ref k, _)| k.as_slice() == var) {
174+
Some(i) => { env.remove(i); }
175+
None => {}
176+
};
177+
178+
// Add the new dylib search path var
179+
let newpath = DynamicLibrary::create_path(path.as_slice());
180+
env.push((var.to_owned(),
181+
str::from_utf8(newpath.as_slice()).unwrap().to_owned()));
182+
env
183+
};
184+
let out = Process::configure(ProcessConfig {
185+
env: Some(env.as_slice()),
186+
program: exe.as_str().unwrap(),
187+
..ProcessConfig::new()
188+
}).map(|mut p| p.wait_with_output());
160189
match out {
161190
Err(e) => fail!("couldn't run the test: {}{}", e,
162191
if e.kind == io::PermissionDenied {

Diff for: src/libstd/unstable/dynamic_lib.rs

+88-14
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use mem;
2222
use ops::*;
2323
use option::*;
2424
use os;
25-
use path::GenericPath;
25+
use path::{Path,GenericPath};
2626
use path;
2727
use result::*;
2828
use slice::{Vector,OwnedVector};
@@ -47,7 +47,7 @@ impl Drop for DynamicLibrary {
4747
impl DynamicLibrary {
4848
/// Lazily open a dynamic library. When passed None it gives a
4949
/// 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> {
5151
unsafe {
5252
let maybe_library = dl::check_for_errors_in(|| {
5353
match filename {
@@ -66,20 +66,95 @@ impl DynamicLibrary {
6666
}
6767
}
6868

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"
7394
} else if cfg!(target_os = "macos") {
74-
("DYLD_LIBRARY_PATH", ':' as u8)
95+
"DYLD_LIBRARY_PATH"
7596
} else {
97+
"LD_LIBRARY_PATH"
98+
}
99+
}
100+
101+
<<<<<<< HEAD
76102
("LD_LIBRARY_PATH", ':' as u8)
77103
};
78104
let newenv = os::getenv_as_bytes(envvar).unwrap_or(box []);
79105
let mut newenv = newenv.move_iter().collect::<Vec<_>>();
80106
newenv.push_all(&[sep]);
81107
newenv.push_all(path.as_vec());
82108
os::setenv(envvar, str::from_utf8(newenv.as_slice()).unwrap());
109+
||||||| parent of 943b4dc... 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+
>>>>>>> 943b4dc... Fixing rustdoc stage1.
83158
}
84159

85160
/// Access the value at the symbol of the dynamic library
@@ -155,14 +230,14 @@ mod test {
155230
#[cfg(target_os = "macos")]
156231
#[cfg(target_os = "freebsd")]
157232
pub mod dl {
233+
use prelude::*;
234+
158235
use c_str::ToCStr;
159236
use libc;
160-
use path;
161237
use ptr;
162238
use str;
163-
use result::*;
164239

165-
pub unsafe fn open_external(filename: &path::Path) -> *u8 {
240+
pub unsafe fn open_external(filename: &Path) -> *u8 {
166241
filename.with_c_str(|raw_name| {
167242
dlopen(raw_name, Lazy as libc::c_int) as *u8
168243
})
@@ -219,14 +294,13 @@ pub mod dl {
219294

220295
#[cfg(target_os = "win32")]
221296
pub mod dl {
297+
use prelude::*;
298+
222299
use libc;
223300
use os;
224-
use path::GenericPath;
225-
use path;
226301
use ptr;
227-
use result::{Ok, Err, Result};
228302

229-
pub unsafe fn open_external(filename: &path::Path) -> *u8 {
303+
pub unsafe fn open_external(filename: &Path) -> *u8 {
230304
os::win32::as_utf16_p(filename.as_str().unwrap(), |raw_name| {
231305
LoadLibraryW(raw_name as *libc::c_void) as *u8
232306
})

0 commit comments

Comments
 (0)