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 let shared_assets = builder.ensure(SharedAssets { 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, &shared_assets, 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 shared_assets: &SharedAssetsPaths,
255 target: TargetSelection,
258 let out = builder.doc_out(target);
260 let path = builder.src.join("src/doc").join(markdown);
262 let header = builder.src.join("src/doc/redirect.inc");
263 let footer = builder.src.join("src/doc/footer.inc");
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")
272 .arg(&shared_assets.version_info)
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").alias("standalone").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 version_info = builder.ensure(SharedAssets { target: self.target }).version_info;
330 let favicon = builder.src.join("src/doc/favicon.inc");
331 let footer = builder.src.join("src/doc/footer.inc");
332 let full_toc = builder.src.join("src/doc/full-toc.inc");
334 for file in t!(fs::read_dir(builder.src.join("src/doc"))) {
336 let path = file.path();
337 let filename = path.file_name().unwrap().to_str().unwrap();
338 if !filename.ends_with(".md") || filename == "README.md" {
342 let html = out.join(filename).with_extension("html");
343 let rustdoc = builder.rustdoc(compiler);
344 if up_to_date(&path, &html)
345 && up_to_date(&footer, &html)
346 && up_to_date(&favicon, &html)
347 && up_to_date(&full_toc, &html)
348 && (builder.config.dry_run || up_to_date(&version_info, &html))
349 && (builder.config.dry_run || up_to_date(&rustdoc, &html))
354 let mut cmd = builder.rustdoc_cmd(compiler);
355 // Needed for --index-page flag
356 cmd.arg("-Z").arg("unstable-options");
358 cmd.arg("--html-after-content")
360 .arg("--html-before-content")
362 .arg("--html-in-header")
364 .arg("--markdown-no-toc")
366 .arg(&builder.src.join("src/doc/index.md"))
367 .arg("--markdown-playground-url")
368 .arg("https://play.rust-lang.org/")
373 if !builder.config.docs_minification {
374 cmd.arg("--disable-minification");
377 if filename == "not_found.md" {
378 cmd.arg("--markdown-css").arg("https://doc.rust-lang.org/rust.css");
380 cmd.arg("--markdown-css").arg("rust.css");
382 builder.run(&mut cmd);
385 // We open doc/index.html as the default if invoked as `x.py doc --open`
386 // with no particular explicit doc requested (e.g. library/core).
387 if builder.paths.is_empty() || builder.was_invoked_explicitly::<Self>(Kind::Doc) {
388 let index = out.join("index.html");
389 open(builder, &index);
394 #[derive(Debug, Clone)]
395 pub struct SharedAssetsPaths {
396 pub version_info: PathBuf,
399 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
400 pub struct SharedAssets {
401 target: TargetSelection,
404 impl Step for SharedAssets {
405 type Output = SharedAssetsPaths;
406 const DEFAULT: bool = false;
408 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
409 // Other tasks depend on this, no need to execute it on its own
413 // Generate shared resources used by other pieces of documentation.
414 fn run(self, builder: &Builder<'_>) -> Self::Output {
415 let out = builder.doc_out(self.target);
417 let version_input = builder.src.join("src").join("doc").join("version_info.html.template");
418 let version_info = out.join("version_info.html");
419 if !builder.config.dry_run && !up_to_date(&version_input, &version_info) {
420 let info = t!(fs::read_to_string(&version_input))
421 .replace("VERSION", &builder.rust_release())
422 .replace("SHORT_HASH", builder.rust_info.sha_short().unwrap_or(""))
423 .replace("STAMP", builder.rust_info.sha().unwrap_or(""));
424 t!(fs::write(&version_info, &info));
427 builder.copy(&builder.src.join("src").join("doc").join("rust.css"), &out.join("rust.css"));
429 SharedAssetsPaths { version_info }
433 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
436 pub target: TargetSelection,
441 const DEFAULT: bool = true;
443 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
444 let builder = run.builder;
445 run.all_krates("test").path("library").default_condition(builder.config.docs)
448 fn make_run(run: RunConfig<'_>) {
449 run.builder.ensure(Std { stage: run.builder.top_stage, target: run.target });
452 /// Compile all standard library documentation.
454 /// This will generate all documentation for the standard library and its
455 /// dependencies. This is largely just a wrapper around `cargo doc`.
456 fn run(self, builder: &Builder<'_>) {
457 let stage = self.stage;
458 let target = self.target;
459 let out = builder.doc_out(target);
460 t!(fs::create_dir_all(&out));
462 builder.ensure(SharedAssets { target: self.target });
464 let index_page = builder.src.join("src/doc/index.md").into_os_string();
465 let mut extra_args = vec![
466 OsStr::new("--markdown-css"),
467 OsStr::new("rust.css"),
468 OsStr::new("--markdown-no-toc"),
469 OsStr::new("--index-page"),
473 if !builder.config.docs_minification {
474 extra_args.push(OsStr::new("--disable-minification"));
477 let requested_crates = builder
480 .map(components_simplified)
482 if path.len() >= 2 && path.get(0) == Some(&"library") {
484 Some(path[1].to_owned())
485 } else if !path.is_empty() {
487 Some(path[0].to_owned())
489 // all library crates
493 .collect::<Vec<_>>();
497 DocumentationFormat::HTML,
505 // Look for library/std, library/core etc in the `x.py doc` arguments and
506 // open the corresponding rendered docs.
507 for requested_crate in requested_crates {
508 if STD_PUBLIC_CRATES.iter().any(|k| *k == requested_crate.as_str()) {
509 let index = out.join(requested_crate).join("index.html");
510 open(builder, &index);
516 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
519 pub target: TargetSelection,
522 impl Step for JsonStd {
524 const DEFAULT: bool = false;
526 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
527 let default = run.builder.config.docs && run.builder.config.cmd.json();
528 run.all_krates("test").path("library").default_condition(default)
531 fn make_run(run: RunConfig<'_>) {
532 run.builder.ensure(Std { stage: run.builder.top_stage, target: run.target });
535 /// Build JSON documentation for the standard library crates.
537 /// This is largely just a wrapper around `cargo doc`.
538 fn run(self, builder: &Builder<'_>) {
539 let stage = self.stage;
540 let target = self.target;
541 let out = builder.json_doc_out(target);
542 t!(fs::create_dir_all(&out));
543 let extra_args = [OsStr::new("--output-format"), OsStr::new("json")];
544 doc_std(builder, DocumentationFormat::JSON, stage, target, &out, &extra_args, &[])
548 /// Name of the crates that are visible to consumers of the standard library.
549 /// Documentation for internal crates is handled by the rustc step, so internal crates will show
552 /// Order here is important!
553 /// Crates need to be processed starting from the leaves, otherwise rustdoc will not
554 /// create correct links between crates because rustdoc depends on the
555 /// existence of the output directories to know if it should be a local
557 const STD_PUBLIC_CRATES: [&str; 5] = ["core", "alloc", "std", "proc_macro", "test"];
559 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
560 enum DocumentationFormat {
565 impl DocumentationFormat {
566 fn as_str(&self) -> &str {
568 DocumentationFormat::HTML => "HTML",
569 DocumentationFormat::JSON => "JSON",
574 /// Build the documentation for public standard library crates.
576 /// `requested_crates` can be used to build only a subset of the crates. If empty, all crates will
579 builder: &Builder<'_>,
580 format: DocumentationFormat,
582 target: TargetSelection,
584 extra_args: &[&OsStr],
585 requested_crates: &[String],
587 builder.info(&format!(
588 "Documenting stage{} std ({}) in {} format",
593 if builder.no_std(target) == Some(true) {
595 "building std documentation for no_std target {target} is not supported\n\
596 Set `docs = false` in the config to disable documentation."
599 let compiler = builder.compiler(stage, builder.config.build);
600 // This is directory where the compiler will place the output of the command.
601 // We will then copy the files from this directory into the final `out` directory, the specified
602 // as a function parameter.
603 let out_dir = builder.stage_out(compiler, Mode::Std).join(target.triple).join("doc");
604 // `cargo` uses the same directory for both JSON docs and HTML docs.
605 // This could lead to cross-contamination when copying files into the specified `out` directory.
611 // could lead to HTML docs being copied into the JSON docs output directory.
612 // To avoid this issue, we clean the doc folder before invoking `cargo`.
613 if out_dir.exists() {
614 builder.remove_dir(&out_dir);
617 let run_cargo_rustdoc_for = |package: &str| {
618 let mut cargo = builder.cargo(compiler, Mode::Std, SourceType::InTree, target, "rustdoc");
619 compile::std_cargo(builder, target, compiler.stage, &mut cargo);
623 .arg("-Zskip-rustdoc-fingerprint")
626 .arg("unstable-options")
627 .arg("--resource-suffix")
628 .arg(&builder.version)
630 builder.run(&mut cargo.into());
633 for krate in STD_PUBLIC_CRATES {
634 run_cargo_rustdoc_for(krate);
635 if requested_crates.iter().any(|p| p == krate) {
636 // No need to document more of the libraries if we have the one we want.
641 builder.cp_r(&out_dir, &out);
644 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
647 pub target: TargetSelection,
650 impl Step for Rustc {
652 const DEFAULT: bool = true;
653 const ONLY_HOSTS: bool = true;
655 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
656 let builder = run.builder;
657 run.crate_or_deps("rustc-main")
659 .default_condition(builder.config.compiler_docs)
662 fn make_run(run: RunConfig<'_>) {
663 run.builder.ensure(Rustc { stage: run.builder.top_stage, target: run.target });
666 /// Generates compiler documentation.
668 /// This will generate all documentation for compiler and dependencies.
669 /// Compiler documentation is distributed separately, so we make sure
670 /// we do not merge it with the other documentation from std, test and
671 /// proc_macros. This is largely just a wrapper around `cargo doc`.
672 fn run(self, builder: &Builder<'_>) {
673 let stage = self.stage;
674 let target = self.target;
680 let components = components_simplified(path);
681 components.len() >= 2 && components[0] == "compiler"
683 .collect::<Vec<_>>();
685 // This is the intended out directory for compiler documentation.
686 let out = builder.compiler_doc_out(target);
687 t!(fs::create_dir_all(&out));
689 // Build the standard library, so that proc-macros can use it.
690 // (Normally, only the metadata would be necessary, but proc-macros are special since they run at compile-time.)
691 let compiler = builder.compiler(stage, builder.config.build);
692 builder.ensure(compile::Std::new(compiler, builder.config.build));
694 builder.info(&format!("Documenting stage{} compiler ({})", stage, target));
696 // This uses a shared directory so that librustdoc documentation gets
697 // correctly built and merged with the rustc documentation. This is
698 // needed because rustdoc is built in a different directory from
699 // rustc. rustdoc needs to be able to see everything, for example when
700 // merging the search index, or generating local (relative) links.
701 let out_dir = builder.stage_out(compiler, Mode::Rustc).join(target.triple).join("doc");
702 t!(symlink_dir_force(&builder.config, &out, &out_dir));
703 // Cargo puts proc macros in `target/doc` even if you pass `--target`
704 // explicitly (https://github.com/rust-lang/cargo/issues/7677).
705 let proc_macro_out_dir = builder.stage_out(compiler, Mode::Rustc).join("doc");
706 t!(symlink_dir_force(&builder.config, &out, &proc_macro_out_dir));
708 // Build cargo command.
709 let mut cargo = builder.cargo(compiler, Mode::Rustc, SourceType::InTree, target, "doc");
710 cargo.rustdocflag("--document-private-items");
711 // Since we always pass --document-private-items, there's no need to warn about linking to private items.
712 cargo.rustdocflag("-Arustdoc::private-intra-doc-links");
713 cargo.rustdocflag("--enable-index-page");
714 cargo.rustdocflag("-Zunstable-options");
715 cargo.rustdocflag("-Znormalize-docs");
716 cargo.rustdocflag("--show-type-layout");
717 cargo.rustdocflag("--generate-link-to-definition");
718 compile::rustc_cargo(builder, &mut cargo, target);
719 cargo.arg("-Zunstable-options");
720 cargo.arg("-Zskip-rustdoc-fingerprint");
722 // Only include compiler crates, no dependencies of those, such as `libc`.
723 // Do link to dependencies on `docs.rs` however using `rustdoc-map`.
724 cargo.arg("--no-deps");
725 cargo.arg("-Zrustdoc-map");
727 // FIXME: `-Zrustdoc-map` does not yet correctly work for transitive dependencies,
728 // once this is no longer an issue the special case for `ena` can be removed.
729 cargo.rustdocflag("--extern-html-root-url");
730 cargo.rustdocflag("ena=https://docs.rs/ena/latest/");
732 let root_crates = if paths.is_empty() {
734 INTERNER.intern_str("rustc_driver"),
735 INTERNER.intern_str("rustc_codegen_llvm"),
736 INTERNER.intern_str("rustc_codegen_ssa"),
739 paths.into_iter().map(|p| builder.crate_paths[p]).collect()
741 // Find dependencies for top level crates.
742 let compiler_crates = root_crates.iter().flat_map(|krate| {
743 builder.in_tree_crates(krate, Some(target)).into_iter().map(|krate| krate.name)
746 let mut to_open = None;
747 for krate in compiler_crates {
748 // Create all crate output directories first to make sure rustdoc uses
750 // FIXME: Cargo should probably do this itself.
751 t!(fs::create_dir_all(out_dir.join(krate)));
752 cargo.arg("-p").arg(krate);
753 if to_open.is_none() {
754 to_open = Some(krate);
758 builder.run(&mut cargo.into());
759 // Let's open the first crate documentation page:
760 if let Some(krate) = to_open {
761 let index = out.join(krate).join("index.html");
762 open(builder, &index);
767 macro_rules! tool_doc {
768 ($tool: ident, $should_run: literal, $path: literal, [$($krate: literal),+ $(,)?] $(,)?) => {
769 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
771 target: TargetSelection,
774 impl Step for $tool {
776 const DEFAULT: bool = true;
777 const ONLY_HOSTS: bool = true;
779 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
780 let builder = run.builder;
781 run.crate_or_deps($should_run).default_condition(builder.config.compiler_docs)
784 fn make_run(run: RunConfig<'_>) {
785 run.builder.ensure($tool { target: run.target });
788 /// Generates compiler documentation.
790 /// This will generate all documentation for compiler and dependencies.
791 /// Compiler documentation is distributed separately, so we make sure
792 /// we do not merge it with the other documentation from std, test and
793 /// proc_macros. This is largely just a wrapper around `cargo doc`.
794 fn run(self, builder: &Builder<'_>) {
795 let stage = builder.top_stage;
796 let target = self.target;
798 // This is the intended out directory for compiler documentation.
799 let out = builder.compiler_doc_out(target);
800 t!(fs::create_dir_all(&out));
802 // Build rustc docs so that we generate relative links.
803 builder.ensure(Rustc { stage, target });
804 // Rustdoc needs the rustc sysroot available to build.
805 // FIXME: is there a way to only ensure `check::Rustc` here? Last time I tried it failed
806 // with strange errors, but only on a full bors test ...
807 let compiler = builder.compiler(stage, builder.config.build);
808 builder.ensure(compile::Rustc::new(compiler, target));
812 "Documenting stage{} {} ({})",
814 stringify!($tool).to_lowercase(),
819 // Symlink compiler docs to the output directory of rustdoc documentation.
820 let out_dir = builder.stage_out(compiler, Mode::ToolRustc).join(target.triple).join("doc");
821 t!(fs::create_dir_all(&out_dir));
822 t!(symlink_dir_force(&builder.config, &out, &out_dir));
824 // Build cargo command.
825 let mut cargo = prepare_tool_cargo(
836 cargo.arg("-Zskip-rustdoc-fingerprint");
837 // Only include compiler crates, no dependencies of those, such as `libc`.
838 cargo.arg("--no-deps");
840 cargo.arg("-p").arg($krate);
843 cargo.rustdocflag("--document-private-items");
844 cargo.rustdocflag("--enable-index-page");
845 cargo.rustdocflag("--show-type-layout");
846 cargo.rustdocflag("--generate-link-to-definition");
847 cargo.rustdocflag("-Zunstable-options");
848 builder.run(&mut cargo.into());
854 tool_doc!(Rustdoc, "rustdoc-tool", "src/tools/rustdoc", ["rustdoc", "rustdoc-json-types"],);
859 ["rustfmt-nightly", "rustfmt-config_proc_macro"],
861 tool_doc!(Clippy, "clippy", "src/tools/clippy", ["clippy_utils"]);
862 tool_doc!(Miri, "miri", "src/tools/miri", ["miri"]);
864 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
865 pub struct ErrorIndex {
866 pub target: TargetSelection,
869 impl Step for ErrorIndex {
871 const DEFAULT: bool = true;
872 const ONLY_HOSTS: bool = true;
874 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
875 let builder = run.builder;
876 run.path("src/tools/error_index_generator").default_condition(builder.config.docs)
879 fn make_run(run: RunConfig<'_>) {
880 let target = run.target;
881 run.builder.ensure(ErrorIndex { target });
884 /// Generates the HTML rendered error-index by running the
885 /// `error_index_generator` tool.
886 fn run(self, builder: &Builder<'_>) {
887 builder.info(&format!("Documenting error index ({})", self.target));
888 let out = builder.doc_out(self.target);
889 t!(fs::create_dir_all(&out));
890 let mut index = tool::ErrorIndex::command(builder);
893 index.arg(&builder.version);
895 builder.run(&mut index);
899 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
900 pub struct UnstableBookGen {
901 target: TargetSelection,
904 impl Step for UnstableBookGen {
906 const DEFAULT: bool = true;
907 const ONLY_HOSTS: bool = true;
909 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
910 let builder = run.builder;
911 run.path("src/tools/unstable-book-gen").default_condition(builder.config.docs)
914 fn make_run(run: RunConfig<'_>) {
915 run.builder.ensure(UnstableBookGen { target: run.target });
918 fn run(self, builder: &Builder<'_>) {
919 let target = self.target;
921 builder.info(&format!("Generating unstable book md files ({})", target));
922 let out = builder.md_doc_out(target).join("unstable-book");
923 builder.create_dir(&out);
924 builder.remove_dir(&out);
925 let mut cmd = builder.tool_cmd(Tool::UnstableBookGen);
926 cmd.arg(builder.src.join("library"));
927 cmd.arg(builder.src.join("compiler"));
928 cmd.arg(builder.src.join("src"));
931 builder.run(&mut cmd);
935 fn symlink_dir_force(config: &Config, src: &Path, dst: &Path) -> io::Result<()> {
939 if let Ok(m) = fs::symlink_metadata(dst) {
940 if m.file_type().is_dir() {
941 fs::remove_dir_all(dst)?;
943 // handle directory junctions on windows by falling back to
945 fs::remove_file(dst).or_else(|_| fs::remove_dir(dst))?;
949 symlink_dir(config, src, dst)
952 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
953 pub struct RustcBook {
954 pub compiler: Compiler,
955 pub target: TargetSelection,
959 impl Step for RustcBook {
961 const DEFAULT: bool = true;
962 const ONLY_HOSTS: bool = true;
964 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
965 let builder = run.builder;
966 run.path("src/doc/rustc").default_condition(builder.config.docs)
969 fn make_run(run: RunConfig<'_>) {
970 run.builder.ensure(RustcBook {
971 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
977 /// Builds the rustc book.
979 /// The lints are auto-generated by a tool, and then merged into the book
980 /// in the "md-doc" directory in the build output directory. Then
981 /// "rustbook" is used to convert it to HTML.
982 fn run(self, builder: &Builder<'_>) {
983 let out_base = builder.md_doc_out(self.target).join("rustc");
984 t!(fs::create_dir_all(&out_base));
985 let out_listing = out_base.join("src/lints");
986 builder.cp_r(&builder.src.join("src/doc/rustc"), &out_base);
987 builder.info(&format!("Generating lint docs ({})", self.target));
989 let rustc = builder.rustc(self.compiler);
990 // The tool runs `rustc` for extracting output examples, so it needs a
991 // functional sysroot.
992 builder.ensure(compile::Std::new(self.compiler, self.target));
993 let mut cmd = builder.tool_cmd(Tool::LintDocs);
995 cmd.arg(builder.src.join("compiler"));
997 cmd.arg(&out_listing);
1000 cmd.arg("--rustc-target").arg(&self.target.rustc_target_arg());
1001 if builder.config.verbose() {
1002 cmd.arg("--verbose");
1005 cmd.arg("--validate");
1007 if !builder.unstable_features() {
1008 // We need to validate nightly features, even on the stable channel.
1009 cmd.env("RUSTC_BOOTSTRAP", "1");
1011 // If the lib directories are in an unusual location (changed in
1012 // config.toml), then this needs to explicitly update the dylib search
1014 builder.add_rustc_lib_path(self.compiler, &mut cmd);
1015 builder.run(&mut cmd);
1016 // Run rustbook/mdbook to generate the HTML pages.
1017 builder.ensure(RustbookSrc {
1018 target: self.target,
1019 name: INTERNER.intern_str("rustc"),
1020 src: INTERNER.intern_path(out_base),
1022 if builder.was_invoked_explicitly::<Self>(Kind::Doc) {
1023 let out = builder.doc_out(self.target);
1024 let index = out.join("rustc").join("index.html");
1025 open(builder, &index);