From babb7daf354d0c7224fa7fdb7638e520008f1815 Mon Sep 17 00:00:00 2001 From: Aidan Hobson Sayers Date: Sat, 24 Dec 2016 16:04:48 +0000 Subject: [PATCH] Teach `rustdoc --test` about `--sysroot`, pass it when testing rust This permits rustdoc tests to work in stage0 --- src/bootstrap/bin/rustc.rs | 3 ++- src/bootstrap/bin/rustdoc.rs | 3 +++ src/librustdoc/lib.rs | 5 +++-- src/librustdoc/markdown.rs | 4 ++-- src/librustdoc/test.rs | 25 ++++++++++++++++--------- 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/bootstrap/bin/rustc.rs b/src/bootstrap/bin/rustc.rs index 9cab6c423f5f9..129798836e03b 100644 --- a/src/bootstrap/bin/rustc.rs +++ b/src/bootstrap/bin/rustc.rs @@ -67,6 +67,7 @@ fn main() { ("RUSTC_REAL", "RUSTC_LIBDIR") }; let stage = env::var("RUSTC_STAGE").expect("RUSTC_STAGE was not set"); + let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set"); let rustc = env::var_os(rustc).unwrap_or_else(|| panic!("{:?} was not set", rustc)); let libdir = env::var_os(libdir).unwrap_or_else(|| panic!("{:?} was not set", libdir)); @@ -83,7 +84,7 @@ fn main() { if let Some(target) = target { // The stage0 compiler has a special sysroot distinct from what we // actually downloaded, so we just always pass the `--sysroot` option. - cmd.arg("--sysroot").arg(env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set")); + cmd.arg("--sysroot").arg(sysroot); // When we build Rust dylibs they're all intended for intermediate // usage, so make sure we pass the -Cprefer-dynamic flag instead of diff --git a/src/bootstrap/bin/rustdoc.rs b/src/bootstrap/bin/rustdoc.rs index a53bbe22eb94c..e9ca430f1582b 100644 --- a/src/bootstrap/bin/rustdoc.rs +++ b/src/bootstrap/bin/rustdoc.rs @@ -25,6 +25,7 @@ fn main() { let rustdoc = env::var_os("RUSTDOC_REAL").expect("RUSTDOC_REAL was not set"); let libdir = env::var_os("RUSTC_LIBDIR").expect("RUSTC_LIBDIR was not set"); let stage = env::var("RUSTC_STAGE").expect("RUSTC_STAGE was not set"); + let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set"); let mut dylib_path = bootstrap::util::dylib_path(); dylib_path.insert(0, PathBuf::from(libdir)); @@ -35,6 +36,8 @@ fn main() { .arg(format!("stage{}", stage)) .arg("--cfg") .arg("dox") + .arg("--sysroot") + .arg(sysroot) .env(bootstrap::util::dylib_path_var(), env::join_paths(&dylib_path).unwrap()); std::process::exit(match cmd.status() { diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 835825d31eec9..47616044879cd 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -267,13 +267,14 @@ pub fn main_args(args: &[String]) -> isize { }; let crate_name = matches.opt_str("crate-name"); let playground_url = matches.opt_str("playground-url"); + let maybe_sysroot = matches.opt_str("sysroot").map(PathBuf::from); match (should_test, markdown_input) { (true, true) => { - return markdown::test(input, cfgs, libs, externs, test_args) + return markdown::test(input, cfgs, libs, externs, test_args, maybe_sysroot) } (true, false) => { - return test::run(input, cfgs, libs, externs, test_args, crate_name) + return test::run(input, cfgs, libs, externs, test_args, crate_name, maybe_sysroot) } (false, true) => return markdown::render(input, output.unwrap_or(PathBuf::from("doc")), diff --git a/src/librustdoc/markdown.rs b/src/librustdoc/markdown.rs index 9dbc9d30e606b..369e18948ad5b 100644 --- a/src/librustdoc/markdown.rs +++ b/src/librustdoc/markdown.rs @@ -144,7 +144,7 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches, /// Run any tests/code examples in the markdown file `input`. pub fn test(input: &str, cfgs: Vec, libs: SearchPaths, externs: Externs, - mut test_args: Vec) -> isize { + mut test_args: Vec, maybe_sysroot: Option) -> isize { let input_str = match load_string(input) { Ok(s) => s, Err(LoadStringError::ReadFail) => return 1, @@ -154,7 +154,7 @@ pub fn test(input: &str, cfgs: Vec, libs: SearchPaths, externs: Externs, let mut opts = TestOptions::default(); opts.no_crate_inject = true; let mut collector = Collector::new(input.to_string(), cfgs, libs, externs, - true, opts); + true, opts, maybe_sysroot); find_testable_code(&input_str, &mut collector); test_args.insert(0, "rustdoctest".to_string()); testing::test_main(&test_args, collector.tests); diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index b96a737ed0007..d81165ba00f0f 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -54,14 +54,15 @@ pub fn run(input: &str, libs: SearchPaths, externs: Externs, mut test_args: Vec, - crate_name: Option) + crate_name: Option, + maybe_sysroot: Option) -> isize { let input_path = PathBuf::from(input); let input = config::Input::File(input_path.clone()); let sessopts = config::Options { - maybe_sysroot: Some(env::current_exe().unwrap().parent().unwrap() - .parent().unwrap().to_path_buf()), + maybe_sysroot: maybe_sysroot.clone().or_else( + || Some(env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_path_buf())), search_paths: libs.clone(), crate_types: vec![config::CrateTypeDylib], externs: externs.clone(), @@ -99,7 +100,8 @@ pub fn run(input: &str, libs, externs, false, - opts); + opts, + maybe_sysroot); { let dep_graph = DepGraph::new(false); @@ -157,7 +159,8 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions { fn runtest(test: &str, cratename: &str, cfgs: Vec, libs: SearchPaths, externs: Externs, should_panic: bool, no_run: bool, as_test_harness: bool, - compile_fail: bool, mut error_codes: Vec, opts: &TestOptions) { + compile_fail: bool, mut error_codes: Vec, opts: &TestOptions, + maybe_sysroot: Option) { // the test harness wants its own `main` & top level functions, so // never wrap the test in `fn main() { ... }` let test = maketest(test, Some(cratename), as_test_harness, opts); @@ -168,8 +171,8 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec, libs: SearchPaths, let outputs = OutputTypes::new(&[(OutputType::Exe, None)]); let sessopts = config::Options { - maybe_sysroot: Some(env::current_exe().unwrap().parent().unwrap() - .parent().unwrap().to_path_buf()), + maybe_sysroot: maybe_sysroot.or_else( + || Some(env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_path_buf())), search_paths: libs, crate_types: vec![config::CrateTypeExecutable], output_types: outputs, @@ -379,11 +382,12 @@ pub struct Collector { current_header: Option, cratename: String, opts: TestOptions, + maybe_sysroot: Option, } impl Collector { pub fn new(cratename: String, cfgs: Vec, libs: SearchPaths, externs: Externs, - use_headers: bool, opts: TestOptions) -> Collector { + use_headers: bool, opts: TestOptions, maybe_sysroot: Option) -> Collector { Collector { tests: Vec::new(), names: Vec::new(), @@ -395,6 +399,7 @@ impl Collector { current_header: None, cratename: cratename, opts: opts, + maybe_sysroot: maybe_sysroot, } } @@ -413,6 +418,7 @@ impl Collector { let externs = self.externs.clone(); let cratename = self.cratename.to_string(); let opts = self.opts.clone(); + let maybe_sysroot = self.maybe_sysroot.clone(); debug!("Creating test {}: {}", name, test); self.tests.push(testing::TestDescAndFn { desc: testing::TestDesc { @@ -432,7 +438,8 @@ impl Collector { as_test_harness, compile_fail, error_codes, - &opts); + &opts, + maybe_sysroot); }) }); }