Skip to content

Commit 7da9e87

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 7bebd22 commit 7da9e87

File tree

4 files changed

+106
-65
lines changed

4 files changed

+106
-65
lines changed

src/compiletest/procsrv.rs

Lines changed: 18 additions & 39 deletions
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

src/librustc/metadata/filesearch.rs

Lines changed: 1 addition & 1 deletion
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
}

src/librustdoc/test.rs

Lines changed: 31 additions & 2 deletions
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;
2021
use testing;
@@ -148,13 +149,41 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool,
148149
let outdir = TempDir::new("rustdoctest").expect("rustdoc needs a tempdir");
149150
let out = Some(outdir.path().clone());
150151
let cfg = driver::build_configuration(&sess);
152+
let libdir = sess.target_filesearch().get_lib_path();
151153
driver::compile_input(sess, cfg, &input, &out, &None);
152154

153155
if no_run { return }
154156

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

src/libstd/unstable/dynamic_lib.rs

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ A simple wrapper over the platform's dynamic library facilities
1616
1717
*/
1818

19+
use prelude::*;
20+
1921
use c_str::ToCStr;
2022
use cast;
21-
use ops::*;
22-
use option::*;
2323
use os;
24-
use path::GenericPath;
25-
use path;
26-
use result::*;
2724
use str;
2825

2926
pub struct DynamicLibrary { handle: *u8}
@@ -44,7 +41,7 @@ impl Drop for DynamicLibrary {
4441
impl DynamicLibrary {
4542
/// Lazily open a dynamic library. When passed None it gives a
4643
/// handle to the calling process
47-
pub fn open(filename: Option<&path::Path>) -> Result<DynamicLibrary, ~str> {
44+
pub fn open(filename: Option<&Path>) -> Result<DynamicLibrary, ~str> {
4845
unsafe {
4946
let maybe_library = dl::check_for_errors_in(|| {
5047
match filename {
@@ -63,18 +60,55 @@ impl DynamicLibrary {
6360
}
6461
}
6562

66-
/// Appends a path to the system search path for dynamic libraries
67-
pub fn add_search_path(path: &path::Path) {
68-
let (envvar, sep) = if cfg!(windows) {
69-
("PATH", ';' as u8)
63+
/// Returns the environment variable for this process's dynamic library
64+
/// search path
65+
pub fn envvar() -> &'static str {
66+
if cfg!(windows) {
67+
"PATH"
7068
} else if cfg!(target_os = "macos") {
71-
("DYLD_LIBRARY_PATH", ':' as u8)
69+
"DYLD_LIBRARY_PATH"
7270
} else {
73-
("LD_LIBRARY_PATH", ':' as u8)
74-
};
75-
let newenv = os::getenv_as_bytes(envvar).unwrap_or(box []);
76-
let newenv = newenv + &[sep] + path.as_vec();
77-
os::setenv(envvar, str::from_utf8(newenv).unwrap());
71+
"LD_LIBRARY_PATH"
72+
}
73+
}
74+
75+
fn separator() -> u8 {
76+
if cfg!(windows) {';' as u8} else {':' as u8}
77+
}
78+
79+
/// Returns the current search path for dynamic libraries being used by this
80+
/// process
81+
pub fn search_path() -> Vec<Path> {
82+
let mut ret = Vec::new();
83+
match os::getenv_as_bytes(DynamicLibrary::envvar()) {
84+
Some(env) => {
85+
for portion in env.split(|a| *a == DynamicLibrary::separator()) {
86+
ret.push(Path::new(portion));
87+
}
88+
}
89+
None => {}
90+
}
91+
return ret;
92+
}
93+
94+
/// From a slice of paths, create a new vector which is suitable to be an
95+
/// environment variable for this platforms dylib search path.
96+
pub fn create_path(path: &[Path]) -> Vec<u8> {
97+
let mut newvar = Vec::new();
98+
for (i, path) in path.iter().enumerate() {
99+
if i > 0 { newvar.push(DynamicLibrary::separator()); }
100+
newvar.push_all(path.as_vec());
101+
}
102+
return newvar;
103+
}
104+
105+
/// Prepends a path to this process's search path for dynamic libraries
106+
pub fn prepend_search_path(path: &Path) {
107+
let mut search_path = DynamicLibrary::search_path();
108+
search_path.insert(0, path.clone());
109+
let newval = DynamicLibrary::create_path(search_path.as_slice());
110+
os::setenv(DynamicLibrary::envvar(),
111+
str::from_utf8(newval.as_slice()).unwrap());
78112
}
79113

80114
/// Access the value at the symbol of the dynamic library
@@ -150,14 +184,14 @@ mod test {
150184
#[cfg(target_os = "macos")]
151185
#[cfg(target_os = "freebsd")]
152186
pub mod dl {
187+
use prelude::*;
188+
153189
use c_str::ToCStr;
154190
use libc;
155-
use path;
156191
use ptr;
157192
use str;
158-
use result::*;
159193

160-
pub unsafe fn open_external(filename: &path::Path) -> *u8 {
194+
pub unsafe fn open_external(filename: &Path) -> *u8 {
161195
filename.with_c_str(|raw_name| {
162196
dlopen(raw_name, Lazy as libc::c_int) as *u8
163197
})
@@ -214,14 +248,13 @@ pub mod dl {
214248

215249
#[cfg(target_os = "win32")]
216250
pub mod dl {
251+
use prelude::*;
252+
217253
use libc;
218254
use os;
219-
use path::GenericPath;
220-
use path;
221255
use ptr;
222-
use result::{Ok, Err, Result};
223256

224-
pub unsafe fn open_external(filename: &path::Path) -> *u8 {
257+
pub unsafe fn open_external(filename: &Path) -> *u8 {
225258
os::win32::as_utf16_p(filename.as_str().unwrap(), |raw_name| {
226259
LoadLibraryW(raw_name as *libc::c_void) as *u8
227260
})

0 commit comments

Comments
 (0)