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";
87 fn open(builder: &Builder<'_>, path: impl AsRef<Path>) {
88 if builder.config.dry_run || !builder.config.cmd.open() {
92 let path = path.as_ref();
93 builder.info(&format!("Opening doc {}", path.display()));
94 if let Err(err) = opener::open(path) {
95 builder.info(&format!("{}\n", err));
99 // "library/std" -> ["library", "std"]
101 // Used for deciding whether a particular step is one requested by the user on
102 // the `x.py doc` command line, which determines whether `--open` will open that
104 pub(crate) fn components_simplified(path: &PathBuf) -> Vec<&str> {
105 path.iter().map(|component| component.to_str().unwrap_or("???")).collect()
108 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
109 pub struct UnstableBook {
110 target: TargetSelection,
113 impl Step for UnstableBook {
115 const DEFAULT: bool = true;
117 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
118 let builder = run.builder;
119 run.path("src/doc/unstable-book").default_condition(builder.config.docs)
122 fn make_run(run: RunConfig<'_>) {
123 run.builder.ensure(UnstableBook { target: run.target });
126 fn run(self, builder: &Builder<'_>) {
127 builder.ensure(UnstableBookGen { target: self.target });
128 builder.ensure(RustbookSrc {
130 name: INTERNER.intern_str("unstable-book"),
131 src: INTERNER.intern_path(builder.md_doc_out(self.target).join("unstable-book")),
136 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
138 target: TargetSelection,
139 name: Interned<String>,
140 src: Interned<PathBuf>,
143 impl Step for RustbookSrc {
146 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
150 /// Invoke `rustbook` for `target` for the doc book `name` from the `src` path.
152 /// This will not actually generate any documentation if the documentation has
153 /// already been generated.
154 fn run(self, builder: &Builder<'_>) {
155 let target = self.target;
156 let name = self.name;
158 let out = builder.doc_out(target);
159 t!(fs::create_dir_all(&out));
161 let out = out.join(name);
162 let index = out.join("index.html");
163 let rustbook = builder.tool_exe(Tool::Rustbook);
164 let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
165 if builder.config.dry_run || up_to_date(&src, &index) && up_to_date(&rustbook, &index) {
168 builder.info(&format!("Rustbook ({}) - {}", target, name));
169 let _ = fs::remove_dir_all(&out);
171 builder.run(rustbook_cmd.arg("build").arg(&src).arg("-d").arg(out));
175 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
178 target: TargetSelection,
181 impl Step for TheBook {
183 const DEFAULT: bool = true;
185 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
186 let builder = run.builder;
187 run.path("src/doc/book").default_condition(builder.config.docs)
190 fn make_run(run: RunConfig<'_>) {
191 run.builder.ensure(TheBook {
192 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
197 /// Builds the book and associated stuff.
199 /// We need to build:
202 /// * Older edition redirects
203 /// * Version info and CSS
206 fn run(self, builder: &Builder<'_>) {
207 let relative_path = Path::new("src").join("doc").join("book");
208 builder.update_submodule(&relative_path);
210 let compiler = self.compiler;
211 let target = self.target;
214 builder.ensure(RustbookSrc {
216 name: INTERNER.intern_str("book"),
217 src: INTERNER.intern_path(builder.src.join(&relative_path)),
220 // building older edition redirects
221 for edition in &["first-edition", "second-edition", "2018-edition"] {
222 builder.ensure(RustbookSrc {
224 name: INTERNER.intern_string(format!("book/{}", edition)),
225 src: INTERNER.intern_path(builder.src.join(&relative_path).join(edition)),
229 // build the version info page and CSS
230 builder.ensure(Standalone { compiler, target });
232 // build the redirect pages
233 builder.info(&format!("Documenting book redirect pages ({})", target));
234 for file in t!(fs::read_dir(builder.src.join(&relative_path).join("redirects"))) {
236 let path = file.path();
237 let path = path.to_str().unwrap();
239 invoke_rustdoc(builder, compiler, target, path);
242 if builder.was_invoked_explicitly::<Self>(Kind::Doc) {
243 let out = builder.doc_out(target);
244 let index = out.join("book").join("index.html");
245 open(builder, &index);
251 builder: &Builder<'_>,
253 target: TargetSelection,
256 let out = builder.doc_out(target);
258 let path = builder.src.join("src/doc").join(markdown);
260 let header = builder.src.join("src/doc/redirect.inc");
261 let footer = builder.src.join("src/doc/footer.inc");
262 let version_info = out.join("version_info.html");
264 let mut cmd = builder.rustdoc_cmd(compiler);
266 let out = out.join("book");
268 cmd.arg("--html-after-content")
270 .arg("--html-before-content")
272 .arg("--html-in-header")
274 .arg("--markdown-no-toc")
275 .arg("--markdown-playground-url")
276 .arg("https://play.rust-lang.org/")
280 .arg("--markdown-css")
283 if !builder.config.docs_minification {
284 cmd.arg("-Z").arg("unstable-options").arg("--disable-minification");
287 builder.run(&mut cmd);
290 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
291 pub struct Standalone {
293 target: TargetSelection,
296 impl Step for Standalone {
298 const DEFAULT: bool = true;
300 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
301 let builder = run.builder;
302 run.path("src/doc").default_condition(builder.config.docs)
305 fn make_run(run: RunConfig<'_>) {
306 run.builder.ensure(Standalone {
307 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
312 /// Generates all standalone documentation as compiled by the rustdoc in `stage`
313 /// for the `target` into `out`.
315 /// This will list all of `src/doc` looking for markdown files and appropriately
316 /// perform transformations like substituting `VERSION`, `SHORT_HASH`, and
317 /// `STAMP` along with providing the various header/footer HTML we've customized.
319 /// In the end, this is just a glorified wrapper around rustdoc!
320 fn run(self, builder: &Builder<'_>) {
321 let target = self.target;
322 let compiler = self.compiler;
323 builder.info(&format!("Documenting standalone ({})", target));
324 let out = builder.doc_out(target);
325 t!(fs::create_dir_all(&out));
327 let favicon = builder.src.join("src/doc/favicon.inc");
328 let footer = builder.src.join("src/doc/footer.inc");
329 let full_toc = builder.src.join("src/doc/full-toc.inc");
330 t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
332 let version_input = builder.src.join("src/doc/version_info.html.template");
333 let version_info = out.join("version_info.html");
335 if !builder.config.dry_run && !up_to_date(&version_input, &version_info) {
336 let info = t!(fs::read_to_string(&version_input))
337 .replace("VERSION", &builder.rust_release())
338 .replace("SHORT_HASH", builder.rust_info.sha_short().unwrap_or(""))
339 .replace("STAMP", builder.rust_info.sha().unwrap_or(""));
340 t!(fs::write(&version_info, &info));
343 for file in t!(fs::read_dir(builder.src.join("src/doc"))) {
345 let path = file.path();
346 let filename = path.file_name().unwrap().to_str().unwrap();
347 if !filename.ends_with(".md") || filename == "README.md" {
351 let html = out.join(filename).with_extension("html");
352 let rustdoc = builder.rustdoc(compiler);
353 if up_to_date(&path, &html)
354 && up_to_date(&footer, &html)
355 && up_to_date(&favicon, &html)
356 && up_to_date(&full_toc, &html)
357 && (builder.config.dry_run || up_to_date(&version_info, &html))
358 && (builder.config.dry_run || up_to_date(&rustdoc, &html))
363 let mut cmd = builder.rustdoc_cmd(compiler);
364 // Needed for --index-page flag
365 cmd.arg("-Z").arg("unstable-options");
367 cmd.arg("--html-after-content")
369 .arg("--html-before-content")
371 .arg("--html-in-header")
373 .arg("--markdown-no-toc")
375 .arg(&builder.src.join("src/doc/index.md"))
376 .arg("--markdown-playground-url")
377 .arg("https://play.rust-lang.org/")
382 if !builder.config.docs_minification {
383 cmd.arg("--disable-minification");
386 if filename == "not_found.md" {
387 cmd.arg("--markdown-css").arg("https://doc.rust-lang.org/rust.css");
389 cmd.arg("--markdown-css").arg("rust.css");
391 builder.run(&mut cmd);
394 // We open doc/index.html as the default if invoked as `x.py doc --open`
395 // with no particular explicit doc requested (e.g. library/core).
396 if builder.paths.is_empty() || builder.was_invoked_explicitly::<Self>(Kind::Doc) {
397 let index = out.join("index.html");
398 open(builder, &index);
403 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
406 pub target: TargetSelection,
411 const DEFAULT: bool = true;
413 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
414 let builder = run.builder;
415 run.all_krates("test").path("library").default_condition(builder.config.docs)
418 fn make_run(run: RunConfig<'_>) {
419 run.builder.ensure(Std { stage: run.builder.top_stage, target: run.target });
422 /// Compile all standard library documentation.
424 /// This will generate all documentation for the standard library and its
425 /// dependencies. This is largely just a wrapper around `cargo doc`.
426 fn run(self, builder: &Builder<'_>) {
427 let stage = self.stage;
428 let target = self.target;
429 let out = builder.doc_out(target);
430 t!(fs::create_dir_all(&out));
431 t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
433 let index_page = builder.src.join("src/doc/index.md").into_os_string();
434 let mut extra_args = vec![
435 OsStr::new("--markdown-css"),
436 OsStr::new("rust.css"),
437 OsStr::new("--markdown-no-toc"),
438 OsStr::new("--index-page"),
442 if !builder.config.docs_minification {
443 extra_args.push(OsStr::new("--disable-minification"));
446 let requested_crates = builder
449 .map(components_simplified)
451 if path.len() >= 2 && path.get(0) == Some(&"library") {
453 Some(path[1].to_owned())
454 } else if !path.is_empty() {
456 Some(path[0].to_owned())
458 // all library crates
462 .collect::<Vec<_>>();
466 DocumentationFormat::HTML,
474 // Look for library/std, library/core etc in the `x.py doc` arguments and
475 // open the corresponding rendered docs.
476 for requested_crate in requested_crates {
477 if STD_PUBLIC_CRATES.iter().any(|k| *k == requested_crate.as_str()) {
478 let index = out.join(requested_crate).join("index.html");
479 open(builder, &index);
485 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
488 pub target: TargetSelection,
491 impl Step for JsonStd {
493 const DEFAULT: bool = false;
495 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
496 let default = run.builder.config.docs && run.builder.config.cmd.json();
497 run.all_krates("test").path("library").default_condition(default)
500 fn make_run(run: RunConfig<'_>) {
501 run.builder.ensure(Std { stage: run.builder.top_stage, target: run.target });
504 /// Build JSON documentation for the standard library crates.
506 /// This is largely just a wrapper around `cargo doc`.
507 fn run(self, builder: &Builder<'_>) {
508 let stage = self.stage;
509 let target = self.target;
510 let out = builder.json_doc_out(target);
511 t!(fs::create_dir_all(&out));
512 let extra_args = [OsStr::new("--output-format"), OsStr::new("json")];
513 doc_std(builder, DocumentationFormat::JSON, stage, target, &out, &extra_args, &[])
517 /// Name of the crates that are visible to consumers of the standard library.
518 /// Documentation for internal crates is handled by the rustc step, so internal crates will show
521 /// Order here is important!
522 /// Crates need to be processed starting from the leaves, otherwise rustdoc will not
523 /// create correct links between crates because rustdoc depends on the
524 /// existence of the output directories to know if it should be a local
526 const STD_PUBLIC_CRATES: [&str; 5] = ["core", "alloc", "std", "proc_macro", "test"];
528 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
529 enum DocumentationFormat {
534 impl DocumentationFormat {
535 fn as_str(&self) -> &str {
537 DocumentationFormat::HTML => "HTML",
538 DocumentationFormat::JSON => "JSON",
543 /// Build the documentation for public standard library crates.
545 /// `requested_crates` can be used to build only a subset of the crates. If empty, all crates will
548 builder: &Builder<'_>,
549 format: DocumentationFormat,
551 target: TargetSelection,
553 extra_args: &[&OsStr],
554 requested_crates: &[String],
556 builder.info(&format!(
557 "Documenting stage{} std ({}) in {} format",
562 if builder.no_std(target) == Some(true) {
564 "building std documentation for no_std target {target} is not supported\n\
565 Set `docs = false` in the config to disable documentation."
568 let compiler = builder.compiler(stage, builder.config.build);
569 // This is directory where the compiler will place the output of the command.
570 // We will then copy the files from this directory into the final `out` directory, the specified
571 // as a function parameter.
572 let out_dir = builder.stage_out(compiler, Mode::Std).join(target.triple).join("doc");
573 // `cargo` uses the same directory for both JSON docs and HTML docs.
574 // This could lead to cross-contamination when copying files into the specified `out` directory.
580 // could lead to HTML docs being copied into the JSON docs output directory.
581 // To avoid this issue, we clean the doc folder before invoking `cargo`.
582 if out_dir.exists() {
583 builder.remove_dir(&out_dir);
586 let run_cargo_rustdoc_for = |package: &str| {
587 let mut cargo = builder.cargo(compiler, Mode::Std, SourceType::InTree, target, "rustdoc");
588 compile::std_cargo(builder, target, compiler.stage, &mut cargo);
592 .arg("-Zskip-rustdoc-fingerprint")
595 .arg("unstable-options")
596 .arg("--resource-suffix")
597 .arg(&builder.version)
599 builder.run(&mut cargo.into());
602 for krate in STD_PUBLIC_CRATES {
603 run_cargo_rustdoc_for(krate);
604 if requested_crates.iter().any(|p| p == krate) {
605 // No need to document more of the libraries if we have the one we want.
610 builder.cp_r(&out_dir, &out);
613 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
616 pub target: TargetSelection,
619 impl Step for Rustc {
621 const DEFAULT: bool = true;
622 const ONLY_HOSTS: bool = true;
624 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
625 let builder = run.builder;
626 run.crate_or_deps("rustc-main")
628 .default_condition(builder.config.compiler_docs)
631 fn make_run(run: RunConfig<'_>) {
632 run.builder.ensure(Rustc { stage: run.builder.top_stage, target: run.target });
635 /// Generates compiler documentation.
637 /// This will generate all documentation for compiler and dependencies.
638 /// Compiler documentation is distributed separately, so we make sure
639 /// we do not merge it with the other documentation from std, test and
640 /// proc_macros. This is largely just a wrapper around `cargo doc`.
641 fn run(self, builder: &Builder<'_>) {
642 let stage = self.stage;
643 let target = self.target;
649 let components = components_simplified(path);
650 components.len() >= 2 && components[0] == "compiler"
652 .collect::<Vec<_>>();
654 // This is the intended out directory for compiler documentation.
655 let out = builder.compiler_doc_out(target);
656 t!(fs::create_dir_all(&out));
658 // Build the standard library, so that proc-macros can use it.
659 // (Normally, only the metadata would be necessary, but proc-macros are special since they run at compile-time.)
660 let compiler = builder.compiler(stage, builder.config.build);
661 builder.ensure(compile::Std::new(compiler, builder.config.build));
663 builder.info(&format!("Documenting stage{} compiler ({})", stage, target));
665 // This uses a shared directory so that librustdoc documentation gets
666 // correctly built and merged with the rustc documentation. This is
667 // needed because rustdoc is built in a different directory from
668 // rustc. rustdoc needs to be able to see everything, for example when
669 // merging the search index, or generating local (relative) links.
670 let out_dir = builder.stage_out(compiler, Mode::Rustc).join(target.triple).join("doc");
671 t!(symlink_dir_force(&builder.config, &out, &out_dir));
672 // Cargo puts proc macros in `target/doc` even if you pass `--target`
673 // explicitly (https://github.com/rust-lang/cargo/issues/7677).
674 let proc_macro_out_dir = builder.stage_out(compiler, Mode::Rustc).join("doc");
675 t!(symlink_dir_force(&builder.config, &out, &proc_macro_out_dir));
677 // Build cargo command.
678 let mut cargo = builder.cargo(compiler, Mode::Rustc, SourceType::InTree, target, "doc");
679 cargo.rustdocflag("--document-private-items");
680 // Since we always pass --document-private-items, there's no need to warn about linking to private items.
681 cargo.rustdocflag("-Arustdoc::private-intra-doc-links");
682 cargo.rustdocflag("--enable-index-page");
683 cargo.rustdocflag("-Zunstable-options");
684 cargo.rustdocflag("-Znormalize-docs");
685 cargo.rustdocflag("--show-type-layout");
686 cargo.rustdocflag("--generate-link-to-definition");
687 compile::rustc_cargo(builder, &mut cargo, target);
688 cargo.arg("-Zunstable-options");
689 cargo.arg("-Zskip-rustdoc-fingerprint");
691 // Only include compiler crates, no dependencies of those, such as `libc`.
692 // Do link to dependencies on `docs.rs` however using `rustdoc-map`.
693 cargo.arg("--no-deps");
694 cargo.arg("-Zrustdoc-map");
696 // FIXME: `-Zrustdoc-map` does not yet correctly work for transitive dependencies,
697 // once this is no longer an issue the special case for `ena` can be removed.
698 cargo.rustdocflag("--extern-html-root-url");
699 cargo.rustdocflag("ena=https://docs.rs/ena/latest/");
701 let root_crates = if paths.is_empty() {
703 INTERNER.intern_str("rustc_driver"),
704 INTERNER.intern_str("rustc_codegen_llvm"),
705 INTERNER.intern_str("rustc_codegen_ssa"),
708 paths.into_iter().map(|p| builder.crate_paths[p]).collect()
710 // Find dependencies for top level crates.
711 let compiler_crates = root_crates.iter().flat_map(|krate| {
712 builder.in_tree_crates(krate, Some(target)).into_iter().map(|krate| krate.name)
715 let mut to_open = None;
716 for krate in compiler_crates {
717 // Create all crate output directories first to make sure rustdoc uses
719 // FIXME: Cargo should probably do this itself.
720 t!(fs::create_dir_all(out_dir.join(krate)));
721 cargo.arg("-p").arg(krate);
722 if to_open.is_none() {
723 to_open = Some(krate);
727 builder.run(&mut cargo.into());
728 // Let's open the first crate documentation page:
729 if let Some(krate) = to_open {
730 let index = out.join(krate).join("index.html");
731 open(builder, &index);
736 macro_rules! tool_doc {
737 ($tool: ident, $should_run: literal, $path: literal, [$($krate: literal),+ $(,)?], in_tree = $in_tree:expr $(,)?) => {
738 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
740 target: TargetSelection,
743 impl Step for $tool {
745 const DEFAULT: bool = true;
746 const ONLY_HOSTS: bool = true;
748 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
749 let builder = run.builder;
750 run.crate_or_deps($should_run).default_condition(builder.config.compiler_docs)
753 fn make_run(run: RunConfig<'_>) {
754 run.builder.ensure($tool { target: run.target });
757 /// Generates compiler documentation.
759 /// This will generate all documentation for compiler and dependencies.
760 /// Compiler documentation is distributed separately, so we make sure
761 /// we do not merge it with the other documentation from std, test and
762 /// proc_macros. This is largely just a wrapper around `cargo doc`.
763 fn run(self, builder: &Builder<'_>) {
764 let stage = builder.top_stage;
765 let target = self.target;
767 // This is the intended out directory for compiler documentation.
768 let out = builder.compiler_doc_out(target);
769 t!(fs::create_dir_all(&out));
771 // Build rustc docs so that we generate relative links.
772 builder.ensure(Rustc { stage, target });
773 // Rustdoc needs the rustc sysroot available to build.
774 // FIXME: is there a way to only ensure `check::Rustc` here? Last time I tried it failed
775 // with strange errors, but only on a full bors test ...
776 let compiler = builder.compiler(stage, builder.config.build);
777 builder.ensure(compile::Rustc::new(compiler, target));
781 "Documenting stage{} {} ({})",
783 stringify!($tool).to_lowercase(),
788 // Symlink compiler docs to the output directory of rustdoc documentation.
789 let out_dir = builder.stage_out(compiler, Mode::ToolRustc).join(target.triple).join("doc");
790 t!(fs::create_dir_all(&out_dir));
791 t!(symlink_dir_force(&builder.config, &out, &out_dir));
793 let source_type = if $in_tree == true {
796 SourceType::Submodule
799 // Build cargo command.
800 let mut cargo = prepare_tool_cargo(
811 cargo.arg("-Zskip-rustdoc-fingerprint");
812 // Only include compiler crates, no dependencies of those, such as `libc`.
813 cargo.arg("--no-deps");
815 cargo.arg("-p").arg($krate);
818 cargo.rustdocflag("--document-private-items");
819 cargo.rustdocflag("--enable-index-page");
820 cargo.rustdocflag("--show-type-layout");
821 cargo.rustdocflag("--generate-link-to-definition");
822 cargo.rustdocflag("-Zunstable-options");
823 if $in_tree == true {
824 builder.run(&mut cargo.into());
826 // Allow out-of-tree docs to fail (since the tool might be in a broken state).
827 if !builder.try_run(&mut cargo.into()) {
828 builder.info(&format!(
829 "WARNING: tool {} failed to document; ignoring failure because it is an out-of-tree tool",
830 stringify!($tool).to_lowercase(),
843 ["rustdoc", "rustdoc-json-types"],
850 ["rustfmt-nightly", "rustfmt-config_proc_macro"],
853 tool_doc!(Clippy, "clippy", "src/tools/clippy", ["clippy_utils"], in_tree = true);
854 tool_doc!(Miri, "miri", "src/tools/miri", ["miri"], in_tree = false);
856 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
857 pub struct ErrorIndex {
858 pub target: TargetSelection,
861 impl Step for ErrorIndex {
863 const DEFAULT: bool = true;
864 const ONLY_HOSTS: bool = true;
866 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
867 let builder = run.builder;
868 run.path("src/tools/error_index_generator").default_condition(builder.config.docs)
871 fn make_run(run: RunConfig<'_>) {
872 let target = run.target;
873 run.builder.ensure(ErrorIndex { target });
876 /// Generates the HTML rendered error-index by running the
877 /// `error_index_generator` tool.
878 fn run(self, builder: &Builder<'_>) {
879 builder.info(&format!("Documenting error index ({})", self.target));
880 let out = builder.doc_out(self.target);
881 t!(fs::create_dir_all(&out));
882 let mut index = tool::ErrorIndex::command(builder);
885 index.arg(&builder.version);
887 builder.run(&mut index);
891 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
892 pub struct UnstableBookGen {
893 target: TargetSelection,
896 impl Step for UnstableBookGen {
898 const DEFAULT: bool = true;
899 const ONLY_HOSTS: bool = true;
901 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
902 let builder = run.builder;
903 run.path("src/tools/unstable-book-gen").default_condition(builder.config.docs)
906 fn make_run(run: RunConfig<'_>) {
907 run.builder.ensure(UnstableBookGen { target: run.target });
910 fn run(self, builder: &Builder<'_>) {
911 let target = self.target;
913 builder.info(&format!("Generating unstable book md files ({})", target));
914 let out = builder.md_doc_out(target).join("unstable-book");
915 builder.create_dir(&out);
916 builder.remove_dir(&out);
917 let mut cmd = builder.tool_cmd(Tool::UnstableBookGen);
918 cmd.arg(builder.src.join("library"));
919 cmd.arg(builder.src.join("compiler"));
920 cmd.arg(builder.src.join("src"));
923 builder.run(&mut cmd);
927 fn symlink_dir_force(config: &Config, src: &Path, dst: &Path) -> io::Result<()> {
931 if let Ok(m) = fs::symlink_metadata(dst) {
932 if m.file_type().is_dir() {
933 fs::remove_dir_all(dst)?;
935 // handle directory junctions on windows by falling back to
937 fs::remove_file(dst).or_else(|_| fs::remove_dir(dst))?;
941 symlink_dir(config, src, dst)
944 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
945 pub struct RustcBook {
946 pub compiler: Compiler,
947 pub target: TargetSelection,
951 impl Step for RustcBook {
953 const DEFAULT: bool = true;
954 const ONLY_HOSTS: bool = true;
956 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
957 let builder = run.builder;
958 run.path("src/doc/rustc").default_condition(builder.config.docs)
961 fn make_run(run: RunConfig<'_>) {
962 run.builder.ensure(RustcBook {
963 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
969 /// Builds the rustc book.
971 /// The lints are auto-generated by a tool, and then merged into the book
972 /// in the "md-doc" directory in the build output directory. Then
973 /// "rustbook" is used to convert it to HTML.
974 fn run(self, builder: &Builder<'_>) {
975 let out_base = builder.md_doc_out(self.target).join("rustc");
976 t!(fs::create_dir_all(&out_base));
977 let out_listing = out_base.join("src/lints");
978 builder.cp_r(&builder.src.join("src/doc/rustc"), &out_base);
979 builder.info(&format!("Generating lint docs ({})", self.target));
981 let rustc = builder.rustc(self.compiler);
982 // The tool runs `rustc` for extracting output examples, so it needs a
983 // functional sysroot.
984 builder.ensure(compile::Std::new(self.compiler, self.target));
985 let mut cmd = builder.tool_cmd(Tool::LintDocs);
987 cmd.arg(builder.src.join("compiler"));
989 cmd.arg(&out_listing);
992 cmd.arg("--rustc-target").arg(&self.target.rustc_target_arg());
993 if builder.config.verbose() {
994 cmd.arg("--verbose");
997 cmd.arg("--validate");
999 if !builder.unstable_features() {
1000 // We need to validate nightly features, even on the stable channel.
1001 cmd.env("RUSTC_BOOTSTRAP", "1");
1003 // If the lib directories are in an unusual location (changed in
1004 // config.toml), then this needs to explicitly update the dylib search
1006 builder.add_rustc_lib_path(self.compiler, &mut cmd);
1007 builder.run(&mut cmd);
1008 // Run rustbook/mdbook to generate the HTML pages.
1009 builder.ensure(RustbookSrc {
1010 target: self.target,
1011 name: INTERNER.intern_str("rustc"),
1012 src: INTERNER.intern_path(out_base),
1014 if builder.was_invoked_explicitly::<Self>(Kind::Doc) {
1015 let out = builder.doc_out(self.target);
1016 let index = out.join("rustc").join("index.html");
1017 open(builder, &index);