Skip to content

Commit babb7da

Browse files
committed
Teach rustdoc --test about --sysroot, pass it when testing rust
This permits rustdoc tests to work in stage0
1 parent 17d873c commit babb7da

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

src/bootstrap/bin/rustc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ fn main() {
6767
("RUSTC_REAL", "RUSTC_LIBDIR")
6868
};
6969
let stage = env::var("RUSTC_STAGE").expect("RUSTC_STAGE was not set");
70+
let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set");
7071

7172
let rustc = env::var_os(rustc).unwrap_or_else(|| panic!("{:?} was not set", rustc));
7273
let libdir = env::var_os(libdir).unwrap_or_else(|| panic!("{:?} was not set", libdir));
@@ -83,7 +84,7 @@ fn main() {
8384
if let Some(target) = target {
8485
// The stage0 compiler has a special sysroot distinct from what we
8586
// actually downloaded, so we just always pass the `--sysroot` option.
86-
cmd.arg("--sysroot").arg(env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set"));
87+
cmd.arg("--sysroot").arg(sysroot);
8788

8889
// When we build Rust dylibs they're all intended for intermediate
8990
// usage, so make sure we pass the -Cprefer-dynamic flag instead of

src/bootstrap/bin/rustdoc.rs

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ fn main() {
2525
let rustdoc = env::var_os("RUSTDOC_REAL").expect("RUSTDOC_REAL was not set");
2626
let libdir = env::var_os("RUSTC_LIBDIR").expect("RUSTC_LIBDIR was not set");
2727
let stage = env::var("RUSTC_STAGE").expect("RUSTC_STAGE was not set");
28+
let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set");
2829

2930
let mut dylib_path = bootstrap::util::dylib_path();
3031
dylib_path.insert(0, PathBuf::from(libdir));
@@ -35,6 +36,8 @@ fn main() {
3536
.arg(format!("stage{}", stage))
3637
.arg("--cfg")
3738
.arg("dox")
39+
.arg("--sysroot")
40+
.arg(sysroot)
3841
.env(bootstrap::util::dylib_path_var(),
3942
env::join_paths(&dylib_path).unwrap());
4043
std::process::exit(match cmd.status() {

src/librustdoc/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,14 @@ pub fn main_args(args: &[String]) -> isize {
267267
};
268268
let crate_name = matches.opt_str("crate-name");
269269
let playground_url = matches.opt_str("playground-url");
270+
let maybe_sysroot = matches.opt_str("sysroot").map(PathBuf::from);
270271

271272
match (should_test, markdown_input) {
272273
(true, true) => {
273-
return markdown::test(input, cfgs, libs, externs, test_args)
274+
return markdown::test(input, cfgs, libs, externs, test_args, maybe_sysroot)
274275
}
275276
(true, false) => {
276-
return test::run(input, cfgs, libs, externs, test_args, crate_name)
277+
return test::run(input, cfgs, libs, externs, test_args, crate_name, maybe_sysroot)
277278
}
278279
(false, true) => return markdown::render(input,
279280
output.unwrap_or(PathBuf::from("doc")),

src/librustdoc/markdown.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
144144

145145
/// Run any tests/code examples in the markdown file `input`.
146146
pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
147-
mut test_args: Vec<String>) -> isize {
147+
mut test_args: Vec<String>, maybe_sysroot: Option<PathBuf>) -> isize {
148148
let input_str = match load_string(input) {
149149
Ok(s) => s,
150150
Err(LoadStringError::ReadFail) => return 1,
@@ -154,7 +154,7 @@ pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
154154
let mut opts = TestOptions::default();
155155
opts.no_crate_inject = true;
156156
let mut collector = Collector::new(input.to_string(), cfgs, libs, externs,
157-
true, opts);
157+
true, opts, maybe_sysroot);
158158
find_testable_code(&input_str, &mut collector);
159159
test_args.insert(0, "rustdoctest".to_string());
160160
testing::test_main(&test_args, collector.tests);

src/librustdoc/test.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ pub fn run(input: &str,
5454
libs: SearchPaths,
5555
externs: Externs,
5656
mut test_args: Vec<String>,
57-
crate_name: Option<String>)
57+
crate_name: Option<String>,
58+
maybe_sysroot: Option<PathBuf>)
5859
-> isize {
5960
let input_path = PathBuf::from(input);
6061
let input = config::Input::File(input_path.clone());
6162

6263
let sessopts = config::Options {
63-
maybe_sysroot: Some(env::current_exe().unwrap().parent().unwrap()
64-
.parent().unwrap().to_path_buf()),
64+
maybe_sysroot: maybe_sysroot.clone().or_else(
65+
|| Some(env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_path_buf())),
6566
search_paths: libs.clone(),
6667
crate_types: vec![config::CrateTypeDylib],
6768
externs: externs.clone(),
@@ -99,7 +100,8 @@ pub fn run(input: &str,
99100
libs,
100101
externs,
101102
false,
102-
opts);
103+
opts,
104+
maybe_sysroot);
103105

104106
{
105107
let dep_graph = DepGraph::new(false);
@@ -157,7 +159,8 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {
157159
fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
158160
externs: Externs,
159161
should_panic: bool, no_run: bool, as_test_harness: bool,
160-
compile_fail: bool, mut error_codes: Vec<String>, opts: &TestOptions) {
162+
compile_fail: bool, mut error_codes: Vec<String>, opts: &TestOptions,
163+
maybe_sysroot: Option<PathBuf>) {
161164
// the test harness wants its own `main` & top level functions, so
162165
// never wrap the test in `fn main() { ... }`
163166
let test = maketest(test, Some(cratename), as_test_harness, opts);
@@ -168,8 +171,8 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
168171
let outputs = OutputTypes::new(&[(OutputType::Exe, None)]);
169172

170173
let sessopts = config::Options {
171-
maybe_sysroot: Some(env::current_exe().unwrap().parent().unwrap()
172-
.parent().unwrap().to_path_buf()),
174+
maybe_sysroot: maybe_sysroot.or_else(
175+
|| Some(env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_path_buf())),
173176
search_paths: libs,
174177
crate_types: vec![config::CrateTypeExecutable],
175178
output_types: outputs,
@@ -379,11 +382,12 @@ pub struct Collector {
379382
current_header: Option<String>,
380383
cratename: String,
381384
opts: TestOptions,
385+
maybe_sysroot: Option<PathBuf>,
382386
}
383387

384388
impl Collector {
385389
pub fn new(cratename: String, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
386-
use_headers: bool, opts: TestOptions) -> Collector {
390+
use_headers: bool, opts: TestOptions, maybe_sysroot: Option<PathBuf>) -> Collector {
387391
Collector {
388392
tests: Vec::new(),
389393
names: Vec::new(),
@@ -395,6 +399,7 @@ impl Collector {
395399
current_header: None,
396400
cratename: cratename,
397401
opts: opts,
402+
maybe_sysroot: maybe_sysroot,
398403
}
399404
}
400405

@@ -413,6 +418,7 @@ impl Collector {
413418
let externs = self.externs.clone();
414419
let cratename = self.cratename.to_string();
415420
let opts = self.opts.clone();
421+
let maybe_sysroot = self.maybe_sysroot.clone();
416422
debug!("Creating test {}: {}", name, test);
417423
self.tests.push(testing::TestDescAndFn {
418424
desc: testing::TestDesc {
@@ -432,7 +438,8 @@ impl Collector {
432438
as_test_harness,
433439
compile_fail,
434440
error_codes,
435-
&opts);
441+
&opts,
442+
maybe_sysroot);
436443
})
437444
});
438445
}

0 commit comments

Comments
 (0)