| //! Shim which is passed to Cargo as "rustdoc" when running the bootstrap. | |
| //! | |
| //! See comments in `src/bootstrap/rustc.rs` for more information. | |
| use std::env; | |
| use std::path::PathBuf; | |
| use shim_utils::{ | |
| ArgFileCommand, collect_args, dylib_path, dylib_path_var, maybe_dump, parse_rustc_stage, | |
| parse_rustc_verbose, parse_value_from_args, | |
| }; | |
| fn main() { | |
| let args = collect_args(); | |
| let stage = parse_rustc_stage(); | |
| let verbose = parse_rustc_verbose(); | |
| let rustdoc = env::var_os("RUSTDOC_REAL").expect("RUSTDOC_REAL was not set"); | |
| let libdir = env::var_os("RUSTDOC_LIBDIR").expect("RUSTDOC_LIBDIR was not set"); | |
| let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set"); | |
| // Detect whether or not we're a build script depending on whether --target | |
| // is passed (a bit janky...) | |
| let target = parse_value_from_args(&args, "--target"); | |
| let mut dylib_path = dylib_path(); | |
| dylib_path.insert(0, PathBuf::from(libdir.clone())); | |
| let mut cmd = ArgFileCommand::new(rustdoc); | |
| if target.is_some() { | |
| // The stage0 compiler has a special sysroot distinct from what we | |
| // actually downloaded, so we just always pass the `--sysroot` option, | |
| // unless one is already set. | |
| if !args.iter().any(|arg| arg == "--sysroot") { | |
| cmd.arg("--sysroot").arg(&sysroot); | |
| } | |
| } else { | |
| // Find any host flags that were passed by bootstrap. | |
| // The flags are stored in a RUSTC_HOST_FLAGS variable, separated by spaces. | |
| if let Ok(flags) = std::env::var("RUSTC_HOST_FLAGS") { | |
| cmd.args(flags.split(' ')); | |
| } | |
| } | |
| cmd.args(&args); | |
| cmd.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap()); | |
| // Force all crates compiled by this compiler to (a) be unstable and (b) | |
| // allow the `rustc_private` feature to link to other unstable crates | |
| // also in the sysroot. | |
| if env::var_os("RUSTC_FORCE_UNSTABLE").is_some() { | |
| cmd.arg("-Z").arg("force-unstable-if-unmarked"); | |
| } | |
| // Cargo doesn't pass RUSTDOCFLAGS to proc_macros: | |
| // https://github.com/rust-lang/cargo/issues/4423 | |
| // Thus, if we are on stage 0, we explicitly set `--cfg=bootstrap`. | |
| // We also declare that the flag is expected, which we need to do to not | |
| // get warnings about it being unexpected. | |
| if stage == 0 { | |
| cmd.arg("--cfg=bootstrap"); | |
| } | |
| if let Some(crate_name) = parse_value_from_args(&args, "--crate-name") { | |
| // Add rust logo and set html root for all rustc crates. | |
| if crate_name.starts_with("rustc_") { | |
| // We use `-Zcrate-attr=allow` instead of `-A` to force rustdoc to forward this flag to | |
| // the actual doctests. Otherwise those tests all receive the | |
| // `feature(rustdoc_internals)` without receiving the `-A` which leads to errors. | |
| cmd.arg("-Zcrate-attr=allow(internal_features)") | |
| .arg("-Zcrate-attr=doc(rust_logo)") | |
| .arg("-Zcrate-attr=doc(html_root_url = \"https://doc.rust-lang.org/nightly/nightly-rustc/\")"); | |
| // Some crates already have this feature enabled | |
| cmd.arg("-Zcrate-attr=feature(rustdoc_internals)"); | |
| cmd.arg("-Aduplicate-features"); | |
| } | |
| } | |
| let (mut cmd, arg_file) = cmd.build().unwrap(); | |
| maybe_dump(format!("stage{}-rustdoc", stage + 1), &cmd); | |
| if verbose > 1 { | |
| eprintln!( | |
| "rustdoc command: {:?}={:?} {:?}", | |
| dylib_path_var(), | |
| env::join_paths(&dylib_path).unwrap(), | |
| cmd, | |
| ); | |
| eprintln!("sysroot: {sysroot:?}"); | |
| eprintln!("libdir: {libdir:?}"); | |
| } | |
| let status = cmd.status(); | |
| drop(arg_file); | |
| std::process::exit(match status { | |
| Ok(s) => s.code().unwrap_or(1), | |
| Err(e) => panic!("\n\nfailed to run {cmd:?}: {e}\n\n"), | |
| }) | |
| } |