1 //! Documentation generation for rustbuilder.
3 //! This module implements generation for all bits and pieces of documentation
4 //! for the Rust project. This notably includes suites like the rust book, the
5 //! nomicon, rust by example, standalone documentation, etc.
7 //! Everything here is basically just a shim around calling either `rustbook` or
13 use std::path::{Path, PathBuf};
15 use crate::builder::{Builder, Compiler, Kind, RunConfig, ShouldRun, Step};
16 use crate::cache::{Interned, INTERNER};
18 use crate::config::{Config, TargetSelection};
19 use crate::tool::{self, prepare_tool_cargo, SourceType, Tool};
20 use crate::util::{symlink_dir, t, up_to_date};
23 macro_rules! submodule_helper {
24 ($path:expr, submodule) => {
27 ($path:expr, submodule = $submodule:literal) => {
33 ($($name:ident, $path:expr, $book_name:expr $(, submodule $(= $submodule:literal)? )? ;)+) => {
35 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
37 target: TargetSelection,
42 const DEFAULT: bool = true;
44 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
45 let builder = run.builder;
46 run.path($path).default_condition(builder.config.docs)
49 fn make_run(run: RunConfig<'_>) {
50 run.builder.ensure($name {
55 fn run(self, builder: &Builder<'_>) {
57 let path = Path::new(submodule_helper!( $path, submodule $( = $submodule )? ));
58 builder.update_submodule(&path);
60 builder.ensure(RustbookSrc {
62 name: INTERNER.intern_str($book_name),
63 src: INTERNER.intern_path(builder.src.join($path)),
71 // NOTE: When adding a book here, make sure to ALSO build the book by
72 // adding a build step in `src/bootstrap/builder.rs`!
73 // NOTE: Make sure to add the corresponding submodule when adding a new book.
74 // FIXME: Make checking for a submodule automatic somehow (maybe by having a list of all submodules
75 // and checking against it?).
77 CargoBook, "src/tools/cargo/src/doc", "cargo", submodule = "src/tools/cargo";
78 ClippyBook, "src/tools/clippy/book", "clippy";
79 EditionGuide, "src/doc/edition-guide", "edition-guide", submodule;
80 EmbeddedBook, "src/doc/embedded-book", "embedded-book", submodule;
81 Nomicon, "src/doc/nomicon", "nomicon", submodule;
82 Reference, "src/doc/reference", "reference", submodule;
83 RustByExample, "src/doc/rust-by-example", "rust-by-example", submodule;
84 RustdocBook, "src/doc/rustdoc", "rustdoc";
85 StyleGuide, "src/doc/style-guide", "style-guide";
88 // "library/std" -> ["library", "std"]
90 // Used for deciding whether a particular step is one requested by the user on
91 // the `x.py doc` command line, which determines whether `--open` will open that
93 pub(crate) fn components_simplified(path: &PathBuf) -> Vec<&str> {
94 path.iter().map(|component| component.to_str().unwrap_or("???")).collect()
97 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
98 pub struct UnstableBook {
99 target: TargetSelection,
102 impl Step for UnstableBook {
104 const DEFAULT: bool = true;
106 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
107 let builder = run.builder;
108 run.path("src/doc/unstable-book").default_condition(builder.config.docs)
111 fn make_run(run: RunConfig<'_>) {
112 run.builder.ensure(UnstableBook { target: run.target });
115 fn run(self, builder: &Builder<'_>) {
116 builder.ensure(UnstableBookGen { target: self.target });
117 builder.ensure(RustbookSrc {
119 name: INTERNER.intern_str("unstable-book"),
120 src: INTERNER.intern_path(builder.md_doc_out(self.target).join("unstable-book")),
125 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
127 target: TargetSelection,
128 name: Interned<String>,
129 src: Interned<PathBuf>,
132 impl Step for RustbookSrc {
135 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
139 /// Invoke `rustbook` for `target` for the doc book `name` from the `src` path.
141 /// This will not actually generate any documentation if the documentation has
142 /// already been generated.
143 fn run(self, builder: &Builder<'_>) {
144 let target = self.target;
145 let name = self.name;
147 let out = builder.doc_out(target);
148 t!(fs::create_dir_all(&out));
150 let out = out.join(name);
151 let index = out.join("index.html");
152 let rustbook = builder.tool_exe(Tool::Rustbook);
153 let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
154 if builder.config.dry_run || up_to_date(&src, &index) && up_to_date(&rustbook, &index) {
157 builder.info(&format!("Rustbook ({}) - {}", target, name));
158 let _ = fs::remove_dir_all(&out);
160 builder.run(rustbook_cmd.arg("build").arg(&src).arg("-d").arg(out));
164 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
167 target: TargetSelection,
170 impl Step for TheBook {
172 const DEFAULT: bool = true;
174 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
175 let builder = run.builder;
176 run.path("src/doc/book").default_condition(builder.config.docs)
179 fn make_run(run: RunConfig<'_>) {
180 run.builder.ensure(TheBook {
181 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
186 /// Builds the book and associated stuff.
188 /// We need to build:
191 /// * Older edition redirects
192 /// * Version info and CSS
195 fn run(self, builder: &Builder<'_>) {
196 let relative_path = Path::new("src").join("doc").join("book");
197 builder.update_submodule(&relative_path);
199 let compiler = self.compiler;
200 let target = self.target;
203 builder.ensure(RustbookSrc {
205 name: INTERNER.intern_str("book"),
206 src: INTERNER.intern_path(builder.src.join(&relative_path)),
209 // building older edition redirects
210 for edition in &["first-edition", "second-edition", "2018-edition"] {
211 builder.ensure(RustbookSrc {
213 name: INTERNER.intern_string(format!("book/{}", edition)),
214 src: INTERNER.intern_path(builder.src.join(&relative_path).join(edition)),
218 // build the version info page and CSS
219 let shared_assets = builder.ensure(SharedAssets { target });
221 // build the redirect pages
222 builder.info(&format!("Documenting book redirect pages ({})", target));
223 for file in t!(fs::read_dir(builder.src.join(&relative_path).join("redirects"))) {
225 let path = file.path();
226 let path = path.to_str().unwrap();
228 invoke_rustdoc(builder, compiler, &shared_assets, target, path);
231 let out = builder.doc_out(target);
232 let index = out.join("book").join("index.html");
233 builder.maybe_open_in_browser::<Self>(index);
238 builder: &Builder<'_>,
240 shared_assets: &SharedAssetsPaths,
241 target: TargetSelection,
244 let out = builder.doc_out(target);
246 let path = builder.src.join("src/doc").join(markdown);
248 let header = builder.src.join("src/doc/redirect.inc");
249 let footer = builder.src.join("src/doc/footer.inc");
251 let mut cmd = builder.rustdoc_cmd(compiler);
253 let out = out.join("book");
255 cmd.arg("--html-after-content")
257 .arg("--html-before-content")
258 .arg(&shared_assets.version_info)
259 .arg("--html-in-header")
261 .arg("--markdown-no-toc")
262 .arg("--markdown-playground-url")
263 .arg("https://play.rust-lang.org/")
267 .arg("--markdown-css")
270 if !builder.config.docs_minification {
271 cmd.arg("-Z").arg("unstable-options").arg("--disable-minification");
274 builder.run(&mut cmd);
277 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
278 pub struct Standalone {
280 target: TargetSelection,
283 impl Step for Standalone {
285 const DEFAULT: bool = true;
287 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
288 let builder = run.builder;
289 run.path("src/doc").alias("standalone").default_condition(builder.config.docs)
292 fn make_run(run: RunConfig<'_>) {
293 run.builder.ensure(Standalone {
294 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
299 /// Generates all standalone documentation as compiled by the rustdoc in `stage`
300 /// for the `target` into `out`.
302 /// This will list all of `src/doc` looking for markdown files and appropriately
303 /// perform transformations like substituting `VERSION`, `SHORT_HASH`, and
304 /// `STAMP` along with providing the various header/footer HTML we've customized.
306 /// In the end, this is just a glorified wrapper around rustdoc!
307 fn run(self, builder: &Builder<'_>) {
308 let target = self.target;
309 let compiler = self.compiler;
310 builder.info(&format!("Documenting standalone ({})", target));
311 let out = builder.doc_out(target);
312 t!(fs::create_dir_all(&out));
314 let version_info = builder.ensure(SharedAssets { target: self.target }).version_info;
316 let favicon = builder.src.join("src/doc/favicon.inc");
317 let footer = builder.src.join("src/doc/footer.inc");
318 let full_toc = builder.src.join("src/doc/full-toc.inc");
320 for file in t!(fs::read_dir(builder.src.join("src/doc"))) {
322 let path = file.path();
323 let filename = path.file_name().unwrap().to_str().unwrap();
324 if !filename.ends_with(".md") || filename == "README.md" {
328 let html = out.join(filename).with_extension("html");
329 let rustdoc = builder.rustdoc(compiler);
330 if up_to_date(&path, &html)
331 && up_to_date(&footer, &html)
332 && up_to_date(&favicon, &html)
333 && up_to_date(&full_toc, &html)
334 && (builder.config.dry_run || up_to_date(&version_info, &html))
335 && (builder.config.dry_run || up_to_date(&rustdoc, &html))
340 let mut cmd = builder.rustdoc_cmd(compiler);
341 // Needed for --index-page flag
342 cmd.arg("-Z").arg("unstable-options");
344 cmd.arg("--html-after-content")
346 .arg("--html-before-content")
348 .arg("--html-in-header")
350 .arg("--markdown-no-toc")
352 .arg(&builder.src.join("src/doc/index.md"))
353 .arg("--markdown-playground-url")
354 .arg("https://play.rust-lang.org/")
359 if !builder.config.docs_minification {
360 cmd.arg("--disable-minification");
363 if filename == "not_found.md" {
364 cmd.arg("--markdown-css").arg("https://doc.rust-lang.org/rust.css");
366 cmd.arg("--markdown-css").arg("rust.css");
368 builder.run(&mut cmd);
371 // We open doc/index.html as the default if invoked as `x.py doc --open`
372 // with no particular explicit doc requested (e.g. library/core).
373 if builder.paths.is_empty() || builder.was_invoked_explicitly::<Self>(Kind::Doc) {
374 let index = out.join("index.html");
375 builder.open_in_browser(&index);
380 #[derive(Debug, Clone)]
381 pub struct SharedAssetsPaths {
382 pub version_info: PathBuf,
385 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
386 pub struct SharedAssets {
387 target: TargetSelection,
390 impl Step for SharedAssets {
391 type Output = SharedAssetsPaths;
392 const DEFAULT: bool = false;
394 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
395 // Other tasks depend on this, no need to execute it on its own
399 // Generate shared resources used by other pieces of documentation.
400 fn run(self, builder: &Builder<'_>) -> Self::Output {
401 let out = builder.doc_out(self.target);
403 let version_input = builder.src.join("src").join("doc").join("version_info.html.template");
404 let version_info = out.join("version_info.html");
405 if !builder.config.dry_run && !up_to_date(&version_input, &version_info) {
406 let info = t!(fs::read_to_string(&version_input))
407 .replace("VERSION", &builder.rust_release())
408 .replace("SHORT_HASH", builder.rust_info.sha_short().unwrap_or(""))
409 .replace("STAMP", builder.rust_info.sha().unwrap_or(""));
410 t!(fs::write(&version_info, &info));
413 builder.copy(&builder.src.join("src").join("doc").join("rust.css"), &out.join("rust.css"));
415 SharedAssetsPaths { version_info }
419 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
422 pub target: TargetSelection,
427 const DEFAULT: bool = true;
429 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
430 let builder = run.builder;
431 run.all_krates("test").path("library").default_condition(builder.config.docs)
434 fn make_run(run: RunConfig<'_>) {
435 run.builder.ensure(Std { stage: run.builder.top_stage, target: run.target });
438 /// Compile all standard library documentation.
440 /// This will generate all documentation for the standard library and its
441 /// dependencies. This is largely just a wrapper around `cargo doc`.
442 fn run(self, builder: &Builder<'_>) {
443 let stage = self.stage;
444 let target = self.target;
445 let out = builder.doc_out(target);
446 t!(fs::create_dir_all(&out));
448 builder.ensure(SharedAssets { target: self.target });
450 let index_page = builder.src.join("src/doc/index.md").into_os_string();
451 let mut extra_args = vec![
452 OsStr::new("--markdown-css"),
453 OsStr::new("rust.css"),
454 OsStr::new("--markdown-no-toc"),
455 OsStr::new("--index-page"),
459 if !builder.config.docs_minification {
460 extra_args.push(OsStr::new("--disable-minification"));
463 let requested_crates = builder
466 .map(components_simplified)
468 if path.len() >= 2 && path.get(0) == Some(&"library") {
470 Some(path[1].to_owned())
471 } else if !path.is_empty() {
473 Some(path[0].to_owned())
475 // all library crates
479 .collect::<Vec<_>>();
483 DocumentationFormat::HTML,
491 // Look for library/std, library/core etc in the `x.py doc` arguments and
492 // open the corresponding rendered docs.
493 for requested_crate in requested_crates {
494 if STD_PUBLIC_CRATES.iter().any(|k| *k == requested_crate.as_str()) {
495 let index = out.join(requested_crate).join("index.html");
496 builder.open_in_browser(index);
502 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
505 pub target: TargetSelection,
508 impl Step for JsonStd {
510 const DEFAULT: bool = false;
512 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
513 let default = run.builder.config.docs && run.builder.config.cmd.json();
514 run.all_krates("test").path("library").default_condition(default)
517 fn make_run(run: RunConfig<'_>) {
518 run.builder.ensure(Std { stage: run.builder.top_stage, target: run.target });
521 /// Build JSON documentation for the standard library crates.
523 /// This is largely just a wrapper around `cargo doc`.
524 fn run(self, builder: &Builder<'_>) {
525 let stage = self.stage;
526 let target = self.target;
527 let out = builder.json_doc_out(target);
528 t!(fs::create_dir_all(&out));
529 let extra_args = [OsStr::new("--output-format"), OsStr::new("json")];
530 doc_std(builder, DocumentationFormat::JSON, stage, target, &out, &extra_args, &[])
534 /// Name of the crates that are visible to consumers of the standard library.
535 /// Documentation for internal crates is handled by the rustc step, so internal crates will show
538 /// Order here is important!
539 /// Crates need to be processed starting from the leaves, otherwise rustdoc will not
540 /// create correct links between crates because rustdoc depends on the
541 /// existence of the output directories to know if it should be a local
543 const STD_PUBLIC_CRATES: [&str; 5] = ["core", "alloc", "std", "proc_macro", "test"];
545 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
546 enum DocumentationFormat {
551 impl DocumentationFormat {
552 fn as_str(&self) -> &str {
554 DocumentationFormat::HTML => "HTML",
555 DocumentationFormat::JSON => "JSON",
560 /// Build the documentation for public standard library crates.
562 /// `requested_crates` can be used to build only a subset of the crates. If empty, all crates will
565 builder: &Builder<'_>,
566 format: DocumentationFormat,
568 target: TargetSelection,
570 extra_args: &[&OsStr],
571 requested_crates: &[String],
573 builder.info(&format!(
574 "Documenting stage{} std ({}) in {} format",
579 if builder.no_std(target) == Some(true) {
581 "building std documentation for no_std target {target} is not supported\n\
582 Set `docs = false` in the config to disable documentation."
585 let compiler = builder.compiler(stage, builder.config.build);
586 // This is directory where the compiler will place the output of the command.
587 // We will then copy the files from this directory into the final `out` directory, the specified
588 // as a function parameter.
589 let out_dir = builder.stage_out(compiler, Mode::Std).join(target.triple).join("doc");
590 // `cargo` uses the same directory for both JSON docs and HTML docs.
591 // This could lead to cross-contamination when copying files into the specified `out` directory.
597 // could lead to HTML docs being copied into the JSON docs output directory.
598 // To avoid this issue, we clean the doc folder before invoking `cargo`.
599 if out_dir.exists() {
600 builder.remove_dir(&out_dir);
603 let run_cargo_rustdoc_for = |package: &str| {
604 let mut cargo = builder.cargo(compiler, Mode::Std, SourceType::InTree, target, "rustdoc");
605 compile::std_cargo(builder, target, compiler.stage, &mut cargo);
609 .arg("-Zskip-rustdoc-fingerprint")
612 .arg("unstable-options")
613 .arg("--resource-suffix")
614 .arg(&builder.version)
616 builder.run(&mut cargo.into());
619 for krate in STD_PUBLIC_CRATES {
620 run_cargo_rustdoc_for(krate);
621 if requested_crates.iter().any(|p| p == krate) {
622 // No need to document more of the libraries if we have the one we want.
627 builder.cp_r(&out_dir, &out);
630 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
633 pub target: TargetSelection,
636 impl Step for Rustc {
638 const DEFAULT: bool = true;
639 const ONLY_HOSTS: bool = true;
641 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
642 let builder = run.builder;
643 run.crate_or_deps("rustc-main")
645 .default_condition(builder.config.compiler_docs)
648 fn make_run(run: RunConfig<'_>) {
649 run.builder.ensure(Rustc { stage: run.builder.top_stage, target: run.target });
652 /// Generates compiler documentation.
654 /// This will generate all documentation for compiler and dependencies.
655 /// Compiler documentation is distributed separately, so we make sure
656 /// we do not merge it with the other documentation from std, test and
657 /// proc_macros. This is largely just a wrapper around `cargo doc`.
658 fn run(self, builder: &Builder<'_>) {
659 let stage = self.stage;
660 let target = self.target;
666 let components = components_simplified(path);
667 components.len() >= 2 && components[0] == "compiler"
669 .collect::<Vec<_>>();
671 // This is the intended out directory for compiler documentation.
672 let out = builder.compiler_doc_out(target);
673 t!(fs::create_dir_all(&out));
675 // Build the standard library, so that proc-macros can use it.
676 // (Normally, only the metadata would be necessary, but proc-macros are special since they run at compile-time.)
677 let compiler = builder.compiler(stage, builder.config.build);
678 builder.ensure(compile::Std::new(compiler, builder.config.build));
680 builder.info(&format!("Documenting stage{} compiler ({})", stage, target));
682 // This uses a shared directory so that librustdoc documentation gets
683 // correctly built and merged with the rustc documentation. This is
684 // needed because rustdoc is built in a different directory from
685 // rustc. rustdoc needs to be able to see everything, for example when
686 // merging the search index, or generating local (relative) links.
687 let out_dir = builder.stage_out(compiler, Mode::Rustc).join(target.triple).join("doc");
688 t!(symlink_dir_force(&builder.config, &out, &out_dir));
689 // Cargo puts proc macros in `target/doc` even if you pass `--target`
690 // explicitly (https://github.com/rust-lang/cargo/issues/7677).
691 let proc_macro_out_dir = builder.stage_out(compiler, Mode::Rustc).join("doc");
692 t!(symlink_dir_force(&builder.config, &out, &proc_macro_out_dir));
694 // Build cargo command.
695 let mut cargo = builder.cargo(compiler, Mode::Rustc, SourceType::InTree, target, "doc");
696 cargo.rustdocflag("--document-private-items");
697 // Since we always pass --document-private-items, there's no need to warn about linking to private items.
698 cargo.rustdocflag("-Arustdoc::private-intra-doc-links");
699 cargo.rustdocflag("--enable-index-page");
700 cargo.rustdocflag("-Zunstable-options");
701 cargo.rustdocflag("-Znormalize-docs");
702 cargo.rustdocflag("--show-type-layout");
703 cargo.rustdocflag("--generate-link-to-definition");
704 compile::rustc_cargo(builder, &mut cargo, target);
705 cargo.arg("-Zunstable-options");
706 cargo.arg("-Zskip-rustdoc-fingerprint");
708 // Only include compiler crates, no dependencies of those, such as `libc`.
709 // Do link to dependencies on `docs.rs` however using `rustdoc-map`.
710 cargo.arg("--no-deps");
711 cargo.arg("-Zrustdoc-map");
713 // FIXME: `-Zrustdoc-map` does not yet correctly work for transitive dependencies,
714 // once this is no longer an issue the special case for `ena` can be removed.
715 cargo.rustdocflag("--extern-html-root-url");
716 cargo.rustdocflag("ena=https://docs.rs/ena/latest/");
718 let root_crates = if paths.is_empty() {
720 INTERNER.intern_str("rustc_driver"),
721 INTERNER.intern_str("rustc_codegen_llvm"),
722 INTERNER.intern_str("rustc_codegen_ssa"),
725 paths.into_iter().map(|p| builder.crate_paths[p]).collect()
727 // Find dependencies for top level crates.
728 let compiler_crates = root_crates.iter().flat_map(|krate| {
729 builder.in_tree_crates(krate, Some(target)).into_iter().map(|krate| krate.name)
732 let mut to_open = None;
733 for krate in compiler_crates {
734 // Create all crate output directories first to make sure rustdoc uses
736 // FIXME: Cargo should probably do this itself.
737 t!(fs::create_dir_all(out_dir.join(krate)));
738 cargo.arg("-p").arg(krate);
739 if to_open.is_none() {
740 to_open = Some(krate);
744 builder.run(&mut cargo.into());
745 // Let's open the first crate documentation page:
746 if let Some(krate) = to_open {
747 let index = out.join(krate).join("index.html");
748 builder.open_in_browser(index);
753 macro_rules! tool_doc {
754 ($tool: ident, $should_run: literal, $path: literal, [$($krate: literal),+ $(,)?] $(,)?) => {
755 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
757 target: TargetSelection,
760 impl Step for $tool {
762 const DEFAULT: bool = true;
763 const ONLY_HOSTS: bool = true;
765 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
766 let builder = run.builder;
767 run.crate_or_deps($should_run).default_condition(builder.config.compiler_docs)
770 fn make_run(run: RunConfig<'_>) {
771 run.builder.ensure($tool { target: run.target });
774 /// Generates compiler documentation.
776 /// This will generate all documentation for compiler and dependencies.
777 /// Compiler documentation is distributed separately, so we make sure
778 /// we do not merge it with the other documentation from std, test and
779 /// proc_macros. This is largely just a wrapper around `cargo doc`.
780 fn run(self, builder: &Builder<'_>) {
781 let stage = builder.top_stage;
782 let target = self.target;
784 // This is the intended out directory for compiler documentation.
785 let out = builder.compiler_doc_out(target);
786 t!(fs::create_dir_all(&out));
788 // Build rustc docs so that we generate relative links.
789 builder.ensure(Rustc { stage, target });
790 // Rustdoc needs the rustc sysroot available to build.
791 // FIXME: is there a way to only ensure `check::Rustc` here? Last time I tried it failed
792 // with strange errors, but only on a full bors test ...
793 let compiler = builder.compiler(stage, builder.config.build);
794 builder.ensure(compile::Rustc::new(compiler, target));
798 "Documenting stage{} {} ({})",
800 stringify!($tool).to_lowercase(),
805 // Symlink compiler docs to the output directory of rustdoc documentation.
806 let out_dir = builder.stage_out(compiler, Mode::ToolRustc).join(target.triple).join("doc");
807 t!(fs::create_dir_all(&out_dir));
808 t!(symlink_dir_force(&builder.config, &out, &out_dir));
810 // Build cargo command.
811 let mut cargo = prepare_tool_cargo(
822 cargo.arg("-Zskip-rustdoc-fingerprint");
823 // Only include compiler crates, no dependencies of those, such as `libc`.
824 cargo.arg("--no-deps");
826 cargo.arg("-p").arg($krate);
829 cargo.rustdocflag("--document-private-items");
830 cargo.rustdocflag("--enable-index-page");
831 cargo.rustdocflag("--show-type-layout");
832 cargo.rustdocflag("--generate-link-to-definition");
833 cargo.rustdocflag("-Zunstable-options");
834 builder.run(&mut cargo.into());
840 tool_doc!(Rustdoc, "rustdoc-tool", "src/tools/rustdoc", ["rustdoc", "rustdoc-json-types"],);
845 ["rustfmt-nightly", "rustfmt-config_proc_macro"],
847 tool_doc!(Clippy, "clippy", "src/tools/clippy", ["clippy_utils"]);
848 tool_doc!(Miri, "miri", "src/tools/miri", ["miri"]);
850 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
851 pub struct ErrorIndex {
852 pub target: TargetSelection,
855 impl Step for ErrorIndex {
857 const DEFAULT: bool = true;
858 const ONLY_HOSTS: bool = true;
860 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
861 let builder = run.builder;
862 run.path("src/tools/error_index_generator").default_condition(builder.config.docs)
865 fn make_run(run: RunConfig<'_>) {
866 let target = run.target;
867 run.builder.ensure(ErrorIndex { target });
870 /// Generates the HTML rendered error-index by running the
871 /// `error_index_generator` tool.
872 fn run(self, builder: &Builder<'_>) {
873 builder.info(&format!("Documenting error index ({})", self.target));
874 let out = builder.doc_out(self.target);
875 t!(fs::create_dir_all(&out));
876 let mut index = tool::ErrorIndex::command(builder);
879 index.arg(&builder.version);
881 builder.run(&mut index);
885 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
886 pub struct UnstableBookGen {
887 target: TargetSelection,
890 impl Step for UnstableBookGen {
892 const DEFAULT: bool = true;
893 const ONLY_HOSTS: bool = true;
895 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
896 let builder = run.builder;
897 run.path("src/tools/unstable-book-gen").default_condition(builder.config.docs)
900 fn make_run(run: RunConfig<'_>) {
901 run.builder.ensure(UnstableBookGen { target: run.target });
904 fn run(self, builder: &Builder<'_>) {
905 let target = self.target;
907 builder.info(&format!("Generating unstable book md files ({})", target));
908 let out = builder.md_doc_out(target).join("unstable-book");
909 builder.create_dir(&out);
910 builder.remove_dir(&out);
911 let mut cmd = builder.tool_cmd(Tool::UnstableBookGen);
912 cmd.arg(builder.src.join("library"));
913 cmd.arg(builder.src.join("compiler"));
914 cmd.arg(builder.src.join("src"));
917 builder.run(&mut cmd);
921 fn symlink_dir_force(config: &Config, src: &Path, dst: &Path) -> io::Result<()> {
925 if let Ok(m) = fs::symlink_metadata(dst) {
926 if m.file_type().is_dir() {
927 fs::remove_dir_all(dst)?;
929 // handle directory junctions on windows by falling back to
931 fs::remove_file(dst).or_else(|_| fs::remove_dir(dst))?;
935 symlink_dir(config, src, dst)
938 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
939 pub struct RustcBook {
940 pub compiler: Compiler,
941 pub target: TargetSelection,
945 impl Step for RustcBook {
947 const DEFAULT: bool = true;
948 const ONLY_HOSTS: bool = true;
950 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
951 let builder = run.builder;
952 run.path("src/doc/rustc").default_condition(builder.config.docs)
955 fn make_run(run: RunConfig<'_>) {
956 run.builder.ensure(RustcBook {
957 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
963 /// Builds the rustc book.
965 /// The lints are auto-generated by a tool, and then merged into the book
966 /// in the "md-doc" directory in the build output directory. Then
967 /// "rustbook" is used to convert it to HTML.
968 fn run(self, builder: &Builder<'_>) {
969 let out_base = builder.md_doc_out(self.target).join("rustc");
970 t!(fs::create_dir_all(&out_base));
971 let out_listing = out_base.join("src/lints");
972 builder.cp_r(&builder.src.join("src/doc/rustc"), &out_base);
973 builder.info(&format!("Generating lint docs ({})", self.target));
975 let rustc = builder.rustc(self.compiler);
976 // The tool runs `rustc` for extracting output examples, so it needs a
977 // functional sysroot.
978 builder.ensure(compile::Std::new(self.compiler, self.target));
979 let mut cmd = builder.tool_cmd(Tool::LintDocs);
981 cmd.arg(builder.src.join("compiler"));
983 cmd.arg(&out_listing);
986 cmd.arg("--rustc-target").arg(&self.target.rustc_target_arg());
987 if builder.config.verbose() {
988 cmd.arg("--verbose");
991 cmd.arg("--validate");
993 if !builder.unstable_features() {
994 // We need to validate nightly features, even on the stable channel.
995 cmd.env("RUSTC_BOOTSTRAP", "1");
997 // If the lib directories are in an unusual location (changed in
998 // config.toml), then this needs to explicitly update the dylib search
1000 builder.add_rustc_lib_path(self.compiler, &mut cmd);
1001 builder.run(&mut cmd);
1002 // Run rustbook/mdbook to generate the HTML pages.
1003 builder.ensure(RustbookSrc {
1004 target: self.target,
1005 name: INTERNER.intern_str("rustc"),
1006 src: INTERNER.intern_path(out_base),
1009 let out = builder.doc_out(self.target);
1010 let index = out.join("rustc").join("index.html");
1011 builder.maybe_open_in_browser::<Self>(index);