Skip to content

Commit 56e9f36

Browse files
committed
Fixing rustdoc stage1.
See rust-lang#13983 and rust-lang#14000. Fix was originally authored by alexcrichton and then rebased a couple times by pnkfelix, most recently atop PR 13954.
1 parent d9b4bcc commit 56e9f36

File tree

4 files changed

+99
-58
lines changed

4 files changed

+99
-58
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, Command, Process, 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: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::io::{Command, TempDir};
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,12 +151,37 @@ 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!
158-
match Command::new(outdir.path().join("rust_out")).output() {
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.
165+
let exe = outdir.path().join("rust_out");
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+
match Command::new(exe).env(env.as_slice()).output() {
159185
Err(e) => fail!("couldn't run the test: {}{}", e,
160186
if e.kind == io::PermissionDenied {
161187
" - maybe your tempdir is mounted with noexec?"

src/libstd/unstable/dynamic_lib.rs

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

19-
19+
use clone::Clone;
2020
use c_str::ToCStr;
21+
use iter::Iterator;
2122
use mem;
2223
use ops::*;
2324
use option::*;
2425
use os;
25-
use path::GenericPath;
26-
use path;
26+
use path::{Path,GenericPath};
2727
use result::*;
28-
use slice::Vector;
28+
use slice::{Vector,ImmutableVector};
2929
use str;
3030
use vec::Vec;
3131

@@ -76,22 +76,55 @@ impl DynamicLibrary {
7676
}
7777
}
7878

79-
/// Appends a path to the system search path for dynamic libraries
80-
pub fn add_search_path(path: &path::Path) {
81-
let (envvar, sep) = if cfg!(windows) {
82-
("PATH", ';' as u8)
79+
/// Prepends a path to this process's search path for dynamic libraries
80+
pub fn prepend_search_path(path: &Path) {
81+
let mut search_path = DynamicLibrary::search_path();
82+
search_path.insert(0, path.clone());
83+
let newval = DynamicLibrary::create_path(search_path.as_slice());
84+
os::setenv(DynamicLibrary::envvar(),
85+
str::from_utf8(newval.as_slice()).unwrap());
86+
}
87+
88+
/// From a slice of paths, create a new vector which is suitable to be an
89+
/// environment variable for this platforms dylib search path.
90+
pub fn create_path(path: &[Path]) -> Vec<u8> {
91+
let mut newvar = Vec::new();
92+
for (i, path) in path.iter().enumerate() {
93+
if i > 0 { newvar.push(DynamicLibrary::separator()); }
94+
newvar.push_all(path.as_vec());
95+
}
96+
return newvar;
97+
}
98+
99+
/// Returns the environment variable for this process's dynamic library
100+
/// search path
101+
pub fn envvar() -> &'static str {
102+
if cfg!(windows) {
103+
"PATH"
83104
} else if cfg!(target_os = "macos") {
84-
("DYLD_LIBRARY_PATH", ':' as u8)
105+
"DYLD_LIBRARY_PATH"
85106
} else {
86-
("LD_LIBRARY_PATH", ':' as u8)
87-
};
88-
let mut newenv = Vec::from_slice(path.as_vec());
89-
newenv.push(sep);
90-
match os::getenv_as_bytes(envvar) {
91-
Some(bytes) => newenv.push_all(bytes),
107+
"LD_LIBRARY_PATH"
108+
}
109+
}
110+
111+
fn separator() -> u8 {
112+
if cfg!(windows) {';' as u8} else {':' as u8}
113+
}
114+
115+
/// Returns the current search path for dynamic libraries being used by this
116+
/// process
117+
pub fn search_path() -> Vec<Path> {
118+
let mut ret = Vec::new();
119+
match os::getenv_as_bytes(DynamicLibrary::envvar()) {
120+
Some(env) => {
121+
for portion in env.split(|a| *a == DynamicLibrary::separator()) {
122+
ret.push(Path::new(portion));
123+
}
124+
}
92125
None => {}
93126
}
94-
os::setenv(envvar, str::from_utf8(newenv.as_slice()).unwrap());
127+
return ret;
95128
}
96129

97130
/// Access the value at the symbol of the dynamic library
@@ -168,11 +201,12 @@ mod test {
168201
#[cfg(target_os = "macos")]
169202
#[cfg(target_os = "freebsd")]
170203
pub mod dl {
204+
use prelude::*;
205+
171206
use c_str::ToCStr;
172207
use libc;
173208
use ptr;
174209
use str;
175-
use result::*;
176210

177211
pub unsafe fn open_external<T: ToCStr>(filename: T) -> *u8 {
178212
filename.with_c_str(|raw_name| {
@@ -231,6 +265,8 @@ pub mod dl {
231265

232266
#[cfg(target_os = "win32")]
233267
pub mod dl {
268+
use prelude::*;
269+
234270
use libc;
235271
use os;
236272
use ptr;

0 commit comments

Comments
 (0)