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.

Conflicts:
	src/librustc/metadata/filesearch.rs
	src/libstd/unstable/dynamic_lib.rs
  • Loading branch information
alexcrichton authored and pnkfelix committed May 15, 2014
1 parent fedffa7 commit 986309f
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 56 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, Process, ProcessConfig, 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
33 changes: 31 additions & 2 deletions src/librustdoc/test.rs
Expand Up @@ -11,10 +11,11 @@
use std::cell::RefCell;
use std::char;
use std::io;
use std::io::{Process, TempDir};
use std::io::{Process, TempDir, ProcessConfig};
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,13 +151,41 @@ 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!
//
// 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 out = Process::output(exe.as_str().unwrap(), []);
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
};
let out = Process::configure(ProcessConfig {
env: Some(env.as_slice()),
program: exe.as_str().unwrap(),
..ProcessConfig::new()
}).map(|mut p| p.wait_with_output());
match out {
Err(e) => fail!("couldn't run the test: {}{}", e,
if e.kind == io::PermissionDenied {
Expand Down
102 changes: 88 additions & 14 deletions src/libstd/unstable/dynamic_lib.rs
Expand Up @@ -22,7 +22,7 @@ use mem;
use ops::*;
use option::*;
use os;
use path::GenericPath;
use path::{Path,GenericPath};
use path;
use result::*;
use slice::{Vector,OwnedVector};
Expand All @@ -47,7 +47,7 @@ impl Drop for DynamicLibrary {
impl DynamicLibrary {
/// Lazily open a dynamic library. When passed None it gives a
/// handle to the calling process
pub fn open(filename: Option<&path::Path>) -> Result<DynamicLibrary, ~str> {
pub fn open(filename: Option<&Path>) -> Result<DynamicLibrary, ~str> {
unsafe {
let maybe_library = dl::check_for_errors_in(|| {
match filename {
Expand All @@ -66,20 +66,95 @@ 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"
}
}

<<<<<<< HEAD
("LD_LIBRARY_PATH", ':' as u8)
};
let newenv = os::getenv_as_bytes(envvar).unwrap_or(box []);
let mut newenv = newenv.move_iter().collect::<Vec<_>>();
newenv.push_all(&[sep]);
newenv.push_all(path.as_vec());
os::setenv(envvar, str::from_utf8(newenv.as_slice()).unwrap());
||||||| parent of 943b4dc... Fixing rustdoc stage1.
("LD_LIBRARY_PATH", ':' as u8)
};
let newenv = os::getenv_as_bytes(envvar).unwrap_or(box []);
let newenv = newenv + &[sep] + path.as_vec();
os::setenv(envvar, str::from_utf8(newenv).unwrap());
=======
"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 => {}
}
return ret;
}

/// 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;
}

/// 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());
>>>>>>> 943b4dc... Fixing rustdoc stage1.
}

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

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

pub unsafe fn open_external(filename: &path::Path) -> *u8 {
pub unsafe fn open_external(filename: &Path) -> *u8 {
filename.with_c_str(|raw_name| {
dlopen(raw_name, Lazy as libc::c_int) as *u8
})
Expand Down Expand Up @@ -219,14 +294,13 @@ pub mod dl {

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

use libc;
use os;
use path::GenericPath;
use path;
use ptr;
use result::{Ok, Err, Result};

pub unsafe fn open_external(filename: &path::Path) -> *u8 {
pub unsafe fn open_external(filename: &Path) -> *u8 {
os::win32::as_utf16_p(filename.as_str().unwrap(), |raw_name| {
LoadLibraryW(raw_name as *libc::c_void) as *u8
})
Expand Down

0 comments on commit 986309f

Please sign in to comment.