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 fn open(builder: &Builder<'_>, path: impl AsRef<Path>) {
89 if builder.config.dry_run || !builder.config.cmd.open() {
93 let path = path.as_ref();
94 builder.info(&format!("Opening doc {}", path.display()));
95 if let Err(err) = opener::open(path) {
96 builder.info(&format!("{}\n", err));
100 // "library/std" -> ["library", "std"]
102 // Used for deciding whether a particular step is one requested by the user on
103 // the `x.py doc` command line, which determines whether `--open` will open that
105 pub(crate) fn components_simplified(path: &PathBuf) -> Vec<&str> {
106 path.iter().map(|component| component.to_str().unwrap_or("???")).collect()
109 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
110 pub struct UnstableBook {
111 target: TargetSelection,
114 impl Step for UnstableBook {
116 const DEFAULT: bool = true;
118 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
119 let builder = run.builder;
120 run.path("src/doc/unstable-book").default_condition(builder.config.docs)
123 fn make_run(run: RunConfig<'_>) {
124 run.builder.ensure(UnstableBook { target: run.target });
127 fn run(self, builder: &Builder<'_>) {
128 builder.ensure(UnstableBookGen { target: self.target });
129 builder.ensure(RustbookSrc {
131 name: INTERNER.intern_str("unstable-book"),
132 src: INTERNER.intern_path(builder.md_doc_out(self.target).join("unstable-book")),
137 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
139 target: TargetSelection,
140 name: Interned<String>,
141 src: Interned<PathBuf>,
144 impl Step for RustbookSrc {
147 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
151 /// Invoke `rustbook` for `target` for the doc book `name` from the `src` path.
153 /// This will not actually generate any documentation if the documentation has
154 /// already been generated.
155 fn run(self, builder: &Builder<'_>) {
156 let target = self.target;
157 let name = self.name;
159 let out = builder.doc_out(target);
160 t!(fs::create_dir_all(&out));
162 let out = out.join(name);
163 let index = out.join("index.html");
164 let rustbook = builder.tool_exe(Tool::Rustbook);
165 let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
166 if builder.config.dry_run || up_to_date(&src, &index) && up_to_date(&rustbook, &index) {
169 builder.info(&format!("Rustbook ({}) - {}", target, name));
170 let _ = fs::remove_dir_all(&out);
172 builder.run(rustbook_cmd.arg("build").arg(&src).arg("-d").arg(out));
176 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
179 target: TargetSelection,
182 impl Step for TheBook {
184 const DEFAULT: bool = true;
186 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
187 let builder = run.builder;
188 run.path("src/doc/book").default_condition(builder.config.docs)
191 fn make_run(run: RunConfig<'_>) {
192 run.builder.ensure(TheBook {
193 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
198 /// Builds the book and associated stuff.
200 /// We need to build:
203 /// * Older edition redirects
204 /// * Version info and CSS
207 fn run(self, builder: &Builder<'_>) {
208 let relative_path = Path::new("src").join("doc").join("book");
209 builder.update_submodule(&relative_path);
211 let compiler = self.compiler;
212 let target = self.target;
215 builder.ensure(RustbookSrc {
217 name: INTERNER.intern_str("book"),
218 src: INTERNER.intern_path(builder.src.join(&relative_path)),
221 // building older edition redirects
222 for edition in &["first-edition", "second-edition", "2018-edition"] {
223 builder.ensure(RustbookSrc {
225 name: INTERNER.intern_string(format!("book/{}", edition)),
226 src: INTERNER.intern_path(builder.src.join(&relative_path).join(edition)),
230 // build the version info page and CSS
231 builder.ensure(Standalone { compiler, target });
233 // build the redirect pages
234 builder.info(&format!("Documenting book redirect pages ({})", target));
235 for file in t!(fs::read_dir(builder.src.join(&relative_path).join("redirects"))) {
237 let path = file.path();
238 let path = path.to_str().unwrap();
240 invoke_rustdoc(builder, compiler, target, path);
243 if builder.was_invoked_explicitly::<Self>(Kind::Doc) {
244 let out = builder.doc_out(target);
245 let index = out.join("book").join("index.html");
246 open(builder, &index);
252 builder: &Builder<'_>,
254 target: TargetSelection,
257 let out = builder.doc_out(target);
259 let path = builder.src.join("src/doc").join(markdown);
261 let header = builder.src.join("src/doc/redirect.inc");
262 let footer = builder.src.join("src/doc/footer.inc");
263 let version_info = out.join("version_info.html");
265 let mut cmd = builder.rustdoc_cmd(compiler);
267 let out = out.join("book");
269 cmd.arg("--html-after-content")
271 .arg("--html-before-content")
273 .arg("--html-in-header")
275 .arg("--markdown-no-toc")
276 .arg("--markdown-playground-url")
277 .arg("https://play.rust-lang.org/")
281 .arg("--markdown-css")
284 if !builder.config.docs_minification {
285 cmd.arg("-Z").arg("unstable-options").arg("--disable-minification");
288 builder.run(&mut cmd);
291 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
292 pub struct Standalone {
294 target: TargetSelection,
297 impl Step for Standalone {
299 const DEFAULT: bool = true;
301 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
302 let builder = run.builder;
303 run.path("src/doc").default_condition(builder.config.docs)
306 fn make_run(run: RunConfig<'_>) {
307 run.builder.ensure(Standalone {
308 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
313 /// Generates all standalone documentation as compiled by the rustdoc in `stage`
314 /// for the `target` into `out`.
316 /// This will list all of `src/doc` looking for markdown files and appropriately
317 /// perform transformations like substituting `VERSION`, `SHORT_HASH`, and
318 /// `STAMP` along with providing the various header/footer HTML we've customized.
320 /// In the end, this is just a glorified wrapper around rustdoc!
321 fn run(self, builder: &Builder<'_>) {
322 let target = self.target;
323 let compiler = self.compiler;
324 builder.info(&format!("Documenting standalone ({})", target));
325 let out = builder.doc_out(target);
326 t!(fs::create_dir_all(&out));
328 let favicon = builder.src.join("src/doc/favicon.inc");
329 let footer = builder.src.join("src/doc/footer.inc");
330 let full_toc = builder.src.join("src/doc/full-toc.inc");
331 t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
333 let version_input = builder.src.join("src/doc/version_info.html.template");
334 let version_info = out.join("version_info.html");
336 if !builder.config.dry_run && !up_to_date(&version_input, &version_info) {
337 let info = t!(fs::read_to_string(&version_input))
338 .replace("VERSION", &builder.rust_release())
339 .replace("SHORT_HASH", builder.rust_info.sha_short().unwrap_or(""))
340 .replace("STAMP", builder.rust_info.sha().unwrap_or(""));
341 t!(fs::write(&version_info, &info));
344 for file in t!(fs::read_dir(builder.src.join("src/doc"))) {
346 let path = file.path();
347 let filename = path.file_name().unwrap().to_str().unwrap();
348 if !filename.ends_with(".md") || filename == "README.md" {
352 let html = out.join(filename).with_extension("html");
353 let rustdoc = builder.rustdoc(compiler);
354 if up_to_date(&path, &html)
355 && up_to_date(&footer, &html)
356 && up_to_date(&favicon, &html)
357 && up_to_date(&full_toc, &html)
358 && (builder.config.dry_run || up_to_date(&version_info, &html))
359 && (builder.config.dry_run || up_to_date(&rustdoc, &html))
364 let mut cmd = builder.rustdoc_cmd(compiler);
365 // Needed for --index-page flag
366 cmd.arg("-Z").arg("unstable-options");
368 cmd.arg("--html-after-content")
370 .arg("--html-before-content")
372 .arg("--html-in-header")
374 .arg("--markdown-no-toc")
376 .arg(&builder.src.join("src/doc/index.md"))
377 .arg("--markdown-playground-url")
378 .arg("https://play.rust-lang.org/")
383 if !builder.config.docs_minification {
384 cmd.arg("--disable-minification");
387 if filename == "not_found.md" {
388 cmd.arg("--markdown-css").arg("https://doc.rust-lang.org/rust.css");
390 cmd.arg("--markdown-css").arg("rust.css");
392 builder.run(&mut cmd);
395 // We open doc/index.html as the default if invoked as `x.py doc --open`
396 // with no particular explicit doc requested (e.g. library/core).
397 if builder.paths.is_empty() || builder.was_invoked_explicitly::<Self>(Kind::Doc) {
398 let index = out.join("index.html");
399 open(builder, &index);
404 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
407 pub target: TargetSelection,
412 const DEFAULT: bool = true;
414 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
415 let builder = run.builder;
416 run.all_krates("test").path("library").default_condition(builder.config.docs)
419 fn make_run(run: RunConfig<'_>) {
420 run.builder.ensure(Std { stage: run.builder.top_stage, target: run.target });
423 /// Compile all standard library documentation.
425 /// This will generate all documentation for the standard library and its
426 /// dependencies. This is largely just a wrapper around `cargo doc`.
427 fn run(self, builder: &Builder<'_>) {
428 let stage = self.stage;
429 let target = self.target;
430 let out = builder.doc_out(target);
431 t!(fs::create_dir_all(&out));
432 t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
434 let index_page = builder.src.join("src/doc/index.md").into_os_string();
435 let mut extra_args = vec![
436 OsStr::new("--markdown-css"),
437 OsStr::new("rust.css"),
438 OsStr::new("--markdown-no-toc"),
439 OsStr::new("--index-page"),
443 if !builder.config.docs_minification {
444 extra_args.push(OsStr::new("--disable-minification"));
447 let requested_crates = builder
450 .map(components_simplified)
452 if path.len() >= 2 && path.get(0) == Some(&"library") {
454 Some(path[1].to_owned())
455 } else if !path.is_empty() {
457 Some(path[0].to_owned())
459 // all library crates
463 .collect::<Vec<_>>();
467 DocumentationFormat::HTML,
475 // Look for library/std, library/core etc in the `x.py doc` arguments and
476 // open the corresponding rendered docs.
477 for requested_crate in requested_crates {
478 if STD_PUBLIC_CRATES.iter().any(|k| *k == requested_crate.as_str()) {
479 let index = out.join(requested_crate).join("index.html");
480 open(builder, &index);
486 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
489 pub target: TargetSelection,
492 impl Step for JsonStd {
494 const DEFAULT: bool = false;
496 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
497 let default = run.builder.config.docs && run.builder.config.cmd.json();
498 run.all_krates("test").path("library").default_condition(default)
501 fn make_run(run: RunConfig<'_>) {
502 run.builder.ensure(Std { stage: run.builder.top_stage, target: run.target });
505 /// Build JSON documentation for the standard library crates.
507 /// This is largely just a wrapper around `cargo doc`.
508 fn run(self, builder: &Builder<'_>) {
509 let stage = self.stage;
510 let target = self.target;
511 let out = builder.json_doc_out(target);
512 t!(fs::create_dir_all(&out));
513 let extra_args = [OsStr::new("--output-format"), OsStr::new("json")];
514 doc_std(builder, DocumentationFormat::JSON, stage, target, &out, &extra_args, &[])
518 /// Name of the crates that are visible to consumers of the standard library.
519 /// Documentation for internal crates is handled by the rustc step, so internal crates will show
522 /// Order here is important!
523 /// Crates need to be processed starting from the leaves, otherwise rustdoc will not
524 /// create correct links between crates because rustdoc depends on the
525 /// existence of the output directories to know if it should be a local
527 const STD_PUBLIC_CRATES: [&str; 5] = ["core", "alloc", "std", "proc_macro", "test"];
529 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
530 enum DocumentationFormat {
535 impl DocumentationFormat {
536 fn as_str(&self) -> &str {
538 DocumentationFormat::HTML => "HTML",
539 DocumentationFormat::JSON => "JSON",
544 /// Build the documentation for public standard library crates.
546 /// `requested_crates` can be used to build only a subset of the crates. If empty, all crates will
549 builder: &Builder<'_>,
550 format: DocumentationFormat,
552 target: TargetSelection,
554 extra_args: &[&OsStr],
555 requested_crates: &[String],
557 builder.info(&format!(
558 "Documenting stage{} std ({}) in {} format",
563 if builder.no_std(target) == Some(true) {
565 "building std documentation for no_std target {target} is not supported\n\
566 Set `docs = false` in the config to disable documentation."
569 let compiler = builder.compiler(stage, builder.config.build);
570 // This is directory where the compiler will place the output of the command.
571 // We will then copy the files from this directory into the final `out` directory, the specified
572 // as a function parameter.
573 let out_dir = builder.stage_out(compiler, Mode::Std).join(target.triple).join("doc");
574 // `cargo` uses the same directory for both JSON docs and HTML docs.
575 // This could lead to cross-contamination when copying files into the specified `out` directory.
581 // could lead to HTML docs being copied into the JSON docs output directory.
582 // To avoid this issue, we clean the doc folder before invoking `cargo`.
583 if out_dir.exists() {
584 builder.remove_dir(&out_dir);
587 let run_cargo_rustdoc_for = |package: &str| {
588 let mut cargo = builder.cargo(compiler, Mode::Std, SourceType::InTree, target, "rustdoc");
589 compile::std_cargo(builder, target, compiler.stage, &mut cargo);
593 .arg("-Zskip-rustdoc-fingerprint")
596 .arg("unstable-options")
597 .arg("--resource-suffix")
598 .arg(&builder.version)
600 builder.run(&mut cargo.into());
603 for krate in STD_PUBLIC_CRATES {
604 run_cargo_rustdoc_for(krate);
605 if requested_crates.iter().any(|p| p == krate) {
606 // No need to document more of the libraries if we have the one we want.
611 builder.cp_r(&out_dir, &out);
614 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
617 pub target: TargetSelection,
620 impl Step for Rustc {
622 const DEFAULT: bool = true;
623 const ONLY_HOSTS: bool = true;
625 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
626 let builder = run.builder;
627 run.crate_or_deps("rustc-main")
629 .default_condition(builder.config.compiler_docs)
632 fn make_run(run: RunConfig<'_>) {
633 run.builder.ensure(Rustc { stage: run.builder.top_stage, target: run.target });
636 /// Generates compiler documentation.
638 /// This will generate all documentation for compiler and dependencies.
639 /// Compiler documentation is distributed separately, so we make sure
640 /// we do not merge it with the other documentation from std, test and
641 /// proc_macros. This is largely just a wrapper around `cargo doc`.
642 fn run(self, builder: &Builder<'_>) {
643 let stage = self.stage;
644 let target = self.target;
650 let components = components_simplified(path);
651 components.len() >= 2 && components[0] == "compiler"
653 .collect::<Vec<_>>();
655 // This is the intended out directory for compiler documentation.
656 let out = builder.compiler_doc_out(target);
657 t!(fs::create_dir_all(&out));
659 // Build the standard library, so that proc-macros can use it.
660 // (Normally, only the metadata would be necessary, but proc-macros are special since they run at compile-time.)
661 let compiler = builder.compiler(stage, builder.config.build);
662 builder.ensure(compile::Std::new(compiler, builder.config.build));
664 builder.info(&format!("Documenting stage{} compiler ({})", stage, target));
666 // This uses a shared directory so that librustdoc documentation gets
667 // correctly built and merged with the rustc documentation. This is
668 // needed because rustdoc is built in a different directory from
669 // rustc. rustdoc needs to be able to see everything, for example when
670 // merging the search index, or generating local (relative) links.
671 let out_dir = builder.stage_out(compiler, Mode::Rustc).join(target.triple).join("doc");
672 t!(symlink_dir_force(&builder.config, &out, &out_dir));
673 // Cargo puts proc macros in `target/doc` even if you pass `--target`
674 // explicitly (https://github.com/rust-lang/cargo/issues/7677).
675 let proc_macro_out_dir = builder.stage_out(compiler, Mode::Rustc).join("doc");
676 t!(symlink_dir_force(&builder.config, &out, &proc_macro_out_dir));
678 // Build cargo command.
679 let mut cargo = builder.cargo(compiler, Mode::Rustc, SourceType::InTree, target, "doc");
680 cargo.rustdocflag("--document-private-items");
681 // Since we always pass --document-private-items, there's no need to warn about linking to private items.
682 cargo.rustdocflag("-Arustdoc::private-intra-doc-links");
683 cargo.rustdocflag("--enable-index-page");
684 cargo.rustdocflag("-Zunstable-options");
685 cargo.rustdocflag("-Znormalize-docs");
686 cargo.rustdocflag("--show-type-layout");
687 cargo.rustdocflag("--generate-link-to-definition");
688 compile::rustc_cargo(builder, &mut cargo, target);
689 cargo.arg("-Zunstable-options");
690 cargo.arg("-Zskip-rustdoc-fingerprint");
692 // Only include compiler crates, no dependencies of those, such as `libc`.
693 // Do link to dependencies on `docs.rs` however using `rustdoc-map`.
694 cargo.arg("--no-deps");
695 cargo.arg("-Zrustdoc-map");
697 // FIXME: `-Zrustdoc-map` does not yet correctly work for transitive dependencies,
698 // once this is no longer an issue the special case for `ena` can be removed.
699 cargo.rustdocflag("--extern-html-root-url");
700 cargo.rustdocflag("ena=https://docs.rs/ena/latest/");
702 let root_crates = if paths.is_empty() {
704 INTERNER.intern_str("rustc_driver"),
705 INTERNER.intern_str("rustc_codegen_llvm"),
706 INTERNER.intern_str("rustc_codegen_ssa"),
709 paths.into_iter().map(|p| builder.crate_paths[p]).collect()
711 // Find dependencies for top level crates.
712 let compiler_crates = root_crates.iter().flat_map(|krate| {
713 builder.in_tree_crates(krate, Some(target)).into_iter().map(|krate| krate.name)
716 let mut to_open = None;
717 for krate in compiler_crates {
718 // Create all crate output directories first to make sure rustdoc uses
720 // FIXME: Cargo should probably do this itself.
721 t!(fs::create_dir_all(out_dir.join(krate)));
722 cargo.arg("-p").arg(krate);
723 if to_open.is_none() {
724 to_open = Some(krate);
728 builder.run(&mut cargo.into());
729 // Let's open the first crate documentation page:
730 if let Some(krate) = to_open {
731 let index = out.join(krate).join("index.html");
732 open(builder, &index);
737 macro_rules! tool_doc {
738 ($tool: ident, $should_run: literal, $path: literal, [$($krate: literal),+ $(,)?], in_tree = $in_tree:expr $(,)?) => {
739 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
741 target: TargetSelection,
744 impl Step for $tool {
746 const DEFAULT: bool = true;
747 const ONLY_HOSTS: bool = true;
749 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
750 let builder = run.builder;
751 run.crate_or_deps($should_run).default_condition(builder.config.compiler_docs)
754 fn make_run(run: RunConfig<'_>) {
755 run.builder.ensure($tool { target: run.target });
758 /// Generates compiler documentation.
760 /// This will generate all documentation for compiler and dependencies.
761 /// Compiler documentation is distributed separately, so we make sure
762 /// we do not merge it with the other documentation from std, test and
763 /// proc_macros. This is largely just a wrapper around `cargo doc`.
764 fn run(self, builder: &Builder<'_>) {
765 let stage = builder.top_stage;
766 let target = self.target;
768 // This is the intended out directory for compiler documentation.
769 let out = builder.compiler_doc_out(target);
770 t!(fs::create_dir_all(&out));
772 // Build rustc docs so that we generate relative links.
773 builder.ensure(Rustc { stage, target });
774 // Rustdoc needs the rustc sysroot available to build.
775 // FIXME: is there a way to only ensure `check::Rustc` here? Last time I tried it failed
776 // with strange errors, but only on a full bors test ...
777 let compiler = builder.compiler(stage, builder.config.build);
778 builder.ensure(compile::Rustc::new(compiler, target));
782 "Documenting stage{} {} ({})",
784 stringify!($tool).to_lowercase(),
789 // Symlink compiler docs to the output directory of rustdoc documentation.
790 let out_dir = builder.stage_out(compiler, Mode::ToolRustc).join(target.triple).join("doc");
791 t!(fs::create_dir_all(&out_dir));
792 t!(symlink_dir_force(&builder.config, &out, &out_dir));
794 let source_type = if $in_tree == true {
797 SourceType::Submodule
800 // Build cargo command.
801 let mut cargo = prepare_tool_cargo(
812 cargo.arg("-Zskip-rustdoc-fingerprint");
813 // Only include compiler crates, no dependencies of those, such as `libc`.
814 cargo.arg("--no-deps");
816 cargo.arg("-p").arg($krate);
819 cargo.rustdocflag("--document-private-items");
820 cargo.rustdocflag("--enable-index-page");
821 cargo.rustdocflag("--show-type-layout");
822 cargo.rustdocflag("--generate-link-to-definition");
823 cargo.rustdocflag("-Zunstable-options");
824 if $in_tree == true {
825 builder.run(&mut cargo.into());
827 // Allow out-of-tree docs to fail (since the tool might be in a broken state).
828 if !builder.try_run(&mut cargo.into()) {
829 builder.info(&format!(
830 "WARNING: tool {} failed to document; ignoring failure because it is an out-of-tree tool",
831 stringify!($tool).to_lowercase(),
844 ["rustdoc", "rustdoc-json-types"],
851 ["rustfmt-nightly", "rustfmt-config_proc_macro"],
854 tool_doc!(Clippy, "clippy", "src/tools/clippy", ["clippy_utils"], in_tree = true);
855 tool_doc!(Miri, "miri", "src/tools/miri", ["miri"], in_tree = false);
857 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
858 pub struct ErrorIndex {
859 pub target: TargetSelection,
862 impl Step for ErrorIndex {
864 const DEFAULT: bool = true;
865 const ONLY_HOSTS: bool = true;
867 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
868 let builder = run.builder;
869 run.path("src/tools/error_index_generator").default_condition(builder.config.docs)
872 fn make_run(run: RunConfig<'_>) {
873 let target = run.target;
874 run.builder.ensure(ErrorIndex { target });
877 /// Generates the HTML rendered error-index by running the
878 /// `error_index_generator` tool.
879 fn run(self, builder: &Builder<'_>) {
880 builder.info(&format!("Documenting error index ({})", self.target));
881 let out = builder.doc_out(self.target);
882 t!(fs::create_dir_all(&out));
883 let mut index = tool::ErrorIndex::command(builder);
886 index.arg(&builder.version);
888 builder.run(&mut index);
892 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
893 pub struct UnstableBookGen {
894 target: TargetSelection,
897 impl Step for UnstableBookGen {
899 const DEFAULT: bool = true;
900 const ONLY_HOSTS: bool = true;
902 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
903 let builder = run.builder;
904 run.path("src/tools/unstable-book-gen").default_condition(builder.config.docs)
907 fn make_run(run: RunConfig<'_>) {
908 run.builder.ensure(UnstableBookGen { target: run.target });
911 fn run(self, builder: &Builder<'_>) {
912 let target = self.target;
914 builder.info(&format!("Generating unstable book md files ({})", target));
915 let out = builder.md_doc_out(target).join("unstable-book");
916 builder.create_dir(&out);
917 builder.remove_dir(&out);
918 let mut cmd = builder.tool_cmd(Tool::UnstableBookGen);
919 cmd.arg(builder.src.join("library"));
920 cmd.arg(builder.src.join("compiler"));
921 cmd.arg(builder.src.join("src"));
924 builder.run(&mut cmd);
928 fn symlink_dir_force(config: &Config, src: &Path, dst: &Path) -> io::Result<()> {
932 if let Ok(m) = fs::symlink_metadata(dst) {
933 if m.file_type().is_dir() {
934 fs::remove_dir_all(dst)?;
936 // handle directory junctions on windows by falling back to
938 fs::remove_file(dst).or_else(|_| fs::remove_dir(dst))?;
942 symlink_dir(config, src, dst)
945 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
946 pub struct RustcBook {
947 pub compiler: Compiler,
948 pub target: TargetSelection,
952 impl Step for RustcBook {
954 const DEFAULT: bool = true;
955 const ONLY_HOSTS: bool = true;
957 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
958 let builder = run.builder;
959 run.path("src/doc/rustc").default_condition(builder.config.docs)
962 fn make_run(run: RunConfig<'_>) {
963 run.builder.ensure(RustcBook {
964 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
970 /// Builds the rustc book.
972 /// The lints are auto-generated by a tool, and then merged into the book
973 /// in the "md-doc" directory in the build output directory. Then
974 /// "rustbook" is used to convert it to HTML.
975 fn run(self, builder: &Builder<'_>) {
976 let out_base = builder.md_doc_out(self.target).join("rustc");
977 t!(fs::create_dir_all(&out_base));
978 let out_listing = out_base.join("src/lints");
979 builder.cp_r(&builder.src.join("src/doc/rustc"), &out_base);
980 builder.info(&format!("Generating lint docs ({})", self.target));
982 let rustc = builder.rustc(self.compiler);
983 // The tool runs `rustc` for extracting output examples, so it needs a
984 // functional sysroot.
985 builder.ensure(compile::Std::new(self.compiler, self.target));
986 let mut cmd = builder.tool_cmd(Tool::LintDocs);
988 cmd.arg(builder.src.join("compiler"));
990 cmd.arg(&out_listing);
993 cmd.arg("--rustc-target").arg(&self.target.rustc_target_arg());
994 if builder.config.verbose() {
995 cmd.arg("--verbose");
998 cmd.arg("--validate");
1000 if !builder.unstable_features() {
1001 // We need to validate nightly features, even on the stable channel.
1002 cmd.env("RUSTC_BOOTSTRAP", "1");
1004 // If the lib directories are in an unusual location (changed in
1005 // config.toml), then this needs to explicitly update the dylib search
1007 builder.add_rustc_lib_path(self.compiler, &mut cmd);
1008 builder.run(&mut cmd);
1009 // Run rustbook/mdbook to generate the HTML pages.
1010 builder.ensure(RustbookSrc {
1011 target: self.target,
1012 name: INTERNER.intern_str("rustc"),
1013 src: INTERNER.intern_path(out_base),
1015 if builder.was_invoked_explicitly::<Self>(Kind::Doc) {
1016 let out = builder.doc_out(self.target);
1017 let index = out.join("rustc").join("index.html");
1018 open(builder, &index);