]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/bin/rustdoc.rs
Rollup merge of #104081 - joshlf:patch-6, r=dtolnay
[rust.git] / src / bootstrap / bin / rustdoc.rs
1 //! Shim which is passed to Cargo as "rustdoc" when running the bootstrap.
2 //!
3 //! See comments in `src/bootstrap/rustc.rs` for more information.
4
5 use std::env;
6 use std::ffi::OsString;
7 use std::path::PathBuf;
8 use std::process::Command;
9
10 include!("../dylib_util.rs");
11
12 fn main() {
13     let args = env::args_os().skip(1).collect::<Vec<_>>();
14     let stage = env::var("RUSTC_STAGE").expect("RUSTC_STAGE was not set");
15     let rustdoc = env::var_os("RUSTDOC_REAL").expect("RUSTDOC_REAL was not set");
16     let libdir = env::var_os("RUSTDOC_LIBDIR").expect("RUSTDOC_LIBDIR was not set");
17     let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set");
18
19     // Detect whether or not we're a build script depending on whether --target
20     // is passed (a bit janky...)
21     let target = args.windows(2).find(|w| &*w[0] == "--target").and_then(|w| w[1].to_str());
22
23     use std::str::FromStr;
24
25     let verbose = match env::var("RUSTC_VERBOSE") {
26         Ok(s) => usize::from_str(&s).expect("RUSTC_VERBOSE should be an integer"),
27         Err(_) => 0,
28     };
29
30     let mut dylib_path = dylib_path();
31     dylib_path.insert(0, PathBuf::from(libdir.clone()));
32
33     let mut cmd = Command::new(rustdoc);
34
35     if target.is_some() {
36         // The stage0 compiler has a special sysroot distinct from what we
37         // actually downloaded, so we just always pass the `--sysroot` option,
38         // unless one is already set.
39         if !args.iter().any(|arg| arg == "--sysroot") {
40             cmd.arg("--sysroot").arg(&sysroot);
41         }
42     }
43
44     cmd.args(&args);
45     cmd.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
46
47     // Force all crates compiled by this compiler to (a) be unstable and (b)
48     // allow the `rustc_private` feature to link to other unstable crates
49     // also in the sysroot.
50     if env::var_os("RUSTC_FORCE_UNSTABLE").is_some() {
51         cmd.arg("-Z").arg("force-unstable-if-unmarked");
52     }
53     if let Some(linker) = env::var_os("RUSTDOC_LINKER") {
54         let mut arg = OsString::from("-Clinker=");
55         arg.push(&linker);
56         cmd.arg(arg);
57     }
58     if let Ok(no_threads) = env::var("RUSTDOC_LLD_NO_THREADS") {
59         cmd.arg("-Clink-arg=-fuse-ld=lld");
60         cmd.arg(format!("-Clink-arg=-Wl,{}", no_threads));
61     }
62     // Cargo doesn't pass RUSTDOCFLAGS to proc_macros:
63     // https://github.com/rust-lang/cargo/issues/4423
64     // Thus, if we are on stage 0, we explicitly set `--cfg=bootstrap`.
65     // We also declare that the flag is expected, which we need to do to not
66     // get warnings about it being unexpected.
67     if stage == "0" {
68         cmd.arg("--cfg=bootstrap");
69     }
70     cmd.arg("-Zunstable-options");
71     cmd.arg("--check-cfg=values(bootstrap)");
72
73     if verbose > 1 {
74         eprintln!(
75             "rustdoc command: {:?}={:?} {:?}",
76             dylib_path_var(),
77             env::join_paths(&dylib_path).unwrap(),
78             cmd,
79         );
80         eprintln!("sysroot: {:?}", sysroot);
81         eprintln!("libdir: {:?}", libdir);
82     }
83
84     std::process::exit(match cmd.status() {
85         Ok(s) => s.code().unwrap_or(1),
86         Err(e) => panic!("\n\nfailed to run {:?}: {}\n\n", cmd, e),
87     })
88 }