Skip to content

Commit

Permalink
Fixing rustdoc stage1.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pnkfelix committed May 15, 2014
1 parent d9b4bcc commit 56e9f36
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 58 deletions.
57 changes: 18 additions & 39 deletions src/compiletest/procsrv.rs
Expand Up @@ -11,51 +11,30 @@
use std::os;
use std::str;
use std::io::process::{ProcessExit, Command, Process, ProcessOutput};
use std::unstable::dynamic_lib::DynamicLibrary;

#[cfg(target_os = "win32")]
fn target_env(lib_path: &str, prog: &str) -> Vec<(~str, ~str)> {
let env = os::env();

// Make sure we include the aux directory in the path
assert!(prog.ends_with(".exe"));
let aux_path = prog.slice(0u, prog.len() - 4u).to_owned() + ".libaux";

let mut new_env: Vec<_> = env.move_iter().map(|(k, v)| {
let new_v = if "PATH" == k {
format!("{};{};{}", v, lib_path, aux_path)
} else {
v
};
(k, new_v)
}).collect();
if prog.ends_with("rustc.exe") {
new_env.push(("RUST_THREADS".to_owned(), "1".to_owned()));
}
return new_env;
}

#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
fn target_env(lib_path: &str, prog: &str) -> Vec<(~str,~str)> {
// Make sure we include the aux directory in the path
let prog = if cfg!(windows) {prog.slice_to(prog.len() - 4)} else {prog};
let aux_path = prog + ".libaux";

// Need to be sure to put both the lib_path and the aux path in the dylib
// search path for the child.
let mut path = DynamicLibrary::search_path();
path.insert(0, Path::new(aux_path));
path.insert(0, Path::new(lib_path));

// Remove the previous dylib search path var
let var = DynamicLibrary::envvar();
let mut env: Vec<(~str,~str)> = os::env().move_iter().collect();
let var = if cfg!(target_os = "macos") {
"DYLD_LIBRARY_PATH"
} else {
"LD_LIBRARY_PATH"
match env.iter().position(|&(ref k, _)| k.as_slice() == var) {
Some(i) => { env.remove(i); }
None => {}
};
let prev = match env.iter().position(|&(ref k, _)| k.as_slice() == var) {
Some(i) => env.remove(i).unwrap().val1(),
None => "".to_owned(),
};
env.push((var.to_owned(), if prev.is_empty() {
lib_path + ":" + aux_path
} else {
lib_path + ":" + aux_path + ":" + prev
}));

// Add the new dylib search path var
let newpath = DynamicLibrary::create_path(path.as_slice());
env.push((var.to_owned(),
str::from_utf8(newpath.as_slice()).unwrap().to_owned()));
return env;
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/filesearch.rs
Expand Up @@ -136,7 +136,7 @@ impl<'a> FileSearch<'a> {

pub fn add_dylib_search_paths(&self) {
self.for_each_lib_search_path(|lib_search_path| {
DynamicLibrary::add_search_path(lib_search_path);
DynamicLibrary::prepend_search_path(lib_search_path);
FileDoesntMatch
})
}
Expand Down
28 changes: 27 additions & 1 deletion src/librustdoc/test.rs
Expand Up @@ -15,6 +15,7 @@ use std::io::{Command, TempDir};
use std::os;
use std::str;
use std::strbuf::StrBuf;
use std::unstable::dynamic_lib::DynamicLibrary;

use collections::{HashSet, HashMap};
use testing;
Expand Down Expand Up @@ -150,12 +151,37 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool,
let outdir = TempDir::new("rustdoctest").expect("rustdoc needs a tempdir");
let out = Some(outdir.path().clone());
let cfg = config::build_configuration(&sess);
let libdir = sess.target_filesearch().get_lib_path();
driver::compile_input(sess, cfg, &input, &out, &None);

if no_run { return }

// Run the code!
match Command::new(outdir.path().join("rust_out")).output() {
//
// We're careful to prepend the *target* dylib search path to the child's
// environment to ensure that the target loads the right libraries at
// runtime. It would be a sad day if the *host* libraries were loaded as a
// mistake.
let exe = outdir.path().join("rust_out");
let env = {
let mut path = DynamicLibrary::search_path();
path.insert(0, libdir.clone());

// Remove the previous dylib search path var
let var = DynamicLibrary::envvar();
let mut env: Vec<(~str,~str)> = os::env().move_iter().collect();
match env.iter().position(|&(ref k, _)| k.as_slice() == var) {
Some(i) => { env.remove(i); }
None => {}
};

// Add the new dylib search path var
let newpath = DynamicLibrary::create_path(path.as_slice());
env.push((var.to_owned(),
str::from_utf8(newpath.as_slice()).unwrap().to_owned()));
env
};
match Command::new(exe).env(env.as_slice()).output() {
Err(e) => fail!("couldn't run the test: {}{}", e,
if e.kind == io::PermissionDenied {
" - maybe your tempdir is mounted with noexec?"
Expand Down
70 changes: 53 additions & 17 deletions src/libstd/unstable/dynamic_lib.rs
Expand Up @@ -16,16 +16,16 @@ A simple wrapper over the platform's dynamic library facilities
*/


use clone::Clone;
use c_str::ToCStr;
use iter::Iterator;
use mem;
use ops::*;
use option::*;
use os;
use path::GenericPath;
use path;
use path::{Path,GenericPath};
use result::*;
use slice::Vector;
use slice::{Vector,ImmutableVector};
use str;
use vec::Vec;

Expand Down Expand Up @@ -76,22 +76,55 @@ impl DynamicLibrary {
}
}

/// Appends a path to the system search path for dynamic libraries
pub fn add_search_path(path: &path::Path) {
let (envvar, sep) = if cfg!(windows) {
("PATH", ';' as u8)
/// Prepends a path to this process's search path for dynamic libraries
pub fn prepend_search_path(path: &Path) {
let mut search_path = DynamicLibrary::search_path();
search_path.insert(0, path.clone());
let newval = DynamicLibrary::create_path(search_path.as_slice());
os::setenv(DynamicLibrary::envvar(),
str::from_utf8(newval.as_slice()).unwrap());
}

/// From a slice of paths, create a new vector which is suitable to be an
/// environment variable for this platforms dylib search path.
pub fn create_path(path: &[Path]) -> Vec<u8> {
let mut newvar = Vec::new();
for (i, path) in path.iter().enumerate() {
if i > 0 { newvar.push(DynamicLibrary::separator()); }
newvar.push_all(path.as_vec());
}
return newvar;
}

/// Returns the environment variable for this process's dynamic library
/// search path
pub fn envvar() -> &'static str {
if cfg!(windows) {
"PATH"
} else if cfg!(target_os = "macos") {
("DYLD_LIBRARY_PATH", ':' as u8)
"DYLD_LIBRARY_PATH"
} else {
("LD_LIBRARY_PATH", ':' as u8)
};
let mut newenv = Vec::from_slice(path.as_vec());
newenv.push(sep);
match os::getenv_as_bytes(envvar) {
Some(bytes) => newenv.push_all(bytes),
"LD_LIBRARY_PATH"
}
}

fn separator() -> u8 {
if cfg!(windows) {';' as u8} else {':' as u8}
}

/// Returns the current search path for dynamic libraries being used by this
/// process
pub fn search_path() -> Vec<Path> {
let mut ret = Vec::new();
match os::getenv_as_bytes(DynamicLibrary::envvar()) {
Some(env) => {
for portion in env.split(|a| *a == DynamicLibrary::separator()) {
ret.push(Path::new(portion));
}
}
None => {}
}
os::setenv(envvar, str::from_utf8(newenv.as_slice()).unwrap());
return ret;
}

/// Access the value at the symbol of the dynamic library
Expand Down Expand Up @@ -168,11 +201,12 @@ mod test {
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
pub mod dl {
use prelude::*;

use c_str::ToCStr;
use libc;
use ptr;
use str;
use result::*;

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

#[cfg(target_os = "win32")]
pub mod dl {
use prelude::*;

use libc;
use os;
use ptr;
Expand Down

1 comment on commit 56e9f36

@pnkfelix
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=alexcrichton

Please sign in to comment.