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,
437 pub format: DocumentationFormat,
442 const DEFAULT: bool = true;
444 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
445 let builder = run.builder;
446 run.all_krates("test").path("library").default_condition(builder.config.docs)
449 fn make_run(run: RunConfig<'_>) {
450 run.builder.ensure(Std {
451 stage: run.builder.top_stage,
453 format: if run.builder.config.cmd.json() {
454 DocumentationFormat::JSON
456 DocumentationFormat::HTML
461 /// Compile all standard library documentation.
463 /// This will generate all documentation for the standard library and its
464 /// dependencies. This is largely just a wrapper around `cargo doc`.
465 fn run(self, builder: &Builder<'_>) {
466 let stage = self.stage;
467 let target = self.target;
468 let out = match self.format {
469 DocumentationFormat::HTML => builder.doc_out(target),
470 DocumentationFormat::JSON => builder.json_doc_out(target),
473 t!(fs::create_dir_all(&out));
475 builder.ensure(SharedAssets { target: self.target });
477 let index_page = builder.src.join("src/doc/index.md").into_os_string();
478 let mut extra_args = match self.format {
479 DocumentationFormat::HTML => vec![
480 OsStr::new("--markdown-css"),
481 OsStr::new("rust.css"),
482 OsStr::new("--markdown-no-toc"),
483 OsStr::new("--index-page"),
486 DocumentationFormat::JSON => vec![OsStr::new("--output-format"), OsStr::new("json")],
489 if !builder.config.docs_minification {
490 extra_args.push(OsStr::new("--disable-minification"));
493 let requested_crates = builder
496 .map(components_simplified)
498 if path.len() >= 2 && path.get(0) == Some(&"library") {
500 Some(path[1].to_owned())
501 } else if !path.is_empty() {
503 Some(path[0].to_owned())
505 // all library crates
509 .collect::<Vec<_>>();
511 doc_std(builder, self.format, stage, target, &out, &extra_args, &requested_crates);
513 // Don't open if the format is json
514 if let DocumentationFormat::JSON = self.format {
518 // Look for library/std, library/core etc in the `x.py doc` arguments and
519 // open the corresponding rendered docs.
520 for requested_crate in requested_crates {
521 if STD_PUBLIC_CRATES.iter().any(|k| *k == requested_crate.as_str()) {
522 let index = out.join(requested_crate).join("index.html");
523 open(builder, &index);
529 /// Name of the crates that are visible to consumers of the standard library.
530 /// Documentation for internal crates is handled by the rustc step, so internal crates will show
533 /// Order here is important!
534 /// Crates need to be processed starting from the leaves, otherwise rustdoc will not
535 /// create correct links between crates because rustdoc depends on the
536 /// existence of the output directories to know if it should be a local
538 const STD_PUBLIC_CRATES: [&str; 5] = ["core", "alloc", "std", "proc_macro", "test"];
540 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
541 pub enum DocumentationFormat {
546 impl DocumentationFormat {
547 fn as_str(&self) -> &str {
549 DocumentationFormat::HTML => "HTML",
550 DocumentationFormat::JSON => "JSON",
555 /// Build the documentation for public standard library crates.
557 /// `requested_crates` can be used to build only a subset of the crates. If empty, all crates will
560 builder: &Builder<'_>,
561 format: DocumentationFormat,
563 target: TargetSelection,
565 extra_args: &[&OsStr],
566 requested_crates: &[String],
568 builder.info(&format!(
569 "Documenting stage{} std ({}) in {} format",
574 if builder.no_std(target) == Some(true) {
576 "building std documentation for no_std target {target} is not supported\n\
577 Set `docs = false` in the config to disable documentation."
580 let compiler = builder.compiler(stage, builder.config.build);
581 // This is directory where the compiler will place the output of the command.
582 // We will then copy the files from this directory into the final `out` directory, the specified
583 // as a function parameter.
584 let out_dir = builder.stage_out(compiler, Mode::Std).join(target.triple).join("doc");
585 // `cargo` uses the same directory for both JSON docs and HTML docs.
586 // This could lead to cross-contamination when copying files into the specified `out` directory.
592 // could lead to HTML docs being copied into the JSON docs output directory.
593 // To avoid this issue, we clean the doc folder before invoking `cargo`.
594 if out_dir.exists() {
595 builder.remove_dir(&out_dir);
598 let run_cargo_rustdoc_for = |package: &str| {
599 let mut cargo = builder.cargo(compiler, Mode::Std, SourceType::InTree, target, "rustdoc");
600 compile::std_cargo(builder, target, compiler.stage, &mut cargo);
604 .arg("-Zskip-rustdoc-fingerprint")
607 .arg("unstable-options")
608 .arg("--resource-suffix")
609 .arg(&builder.version)
611 builder.run(&mut cargo.into());
614 for krate in STD_PUBLIC_CRATES {
615 run_cargo_rustdoc_for(krate);
616 if requested_crates.iter().any(|p| p == krate) {
617 // No need to document more of the libraries if we have the one we want.
622 builder.cp_r(&out_dir, &out);
625 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
628 pub target: TargetSelection,
631 impl Step for Rustc {
633 const DEFAULT: bool = true;
634 const ONLY_HOSTS: bool = true;
636 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
637 let builder = run.builder;
638 run.crate_or_deps("rustc-main")
640 .default_condition(builder.config.compiler_docs)
643 fn make_run(run: RunConfig<'_>) {
644 run.builder.ensure(Rustc { stage: run.builder.top_stage, target: run.target });
647 /// Generates compiler documentation.
649 /// This will generate all documentation for compiler and dependencies.
650 /// Compiler documentation is distributed separately, so we make sure
651 /// we do not merge it with the other documentation from std, test and
652 /// proc_macros. This is largely just a wrapper around `cargo doc`.
653 fn run(self, builder: &Builder<'_>) {
654 let stage = self.stage;
655 let target = self.target;
661 let components = components_simplified(path);
662 components.len() >= 2 && components[0] == "compiler"
664 .collect::<Vec<_>>();
666 // This is the intended out directory for compiler documentation.
667 let out = builder.compiler_doc_out(target);
668 t!(fs::create_dir_all(&out));
670 // Build the standard library, so that proc-macros can use it.
671 // (Normally, only the metadata would be necessary, but proc-macros are special since they run at compile-time.)
672 let compiler = builder.compiler(stage, builder.config.build);
673 builder.ensure(compile::Std::new(compiler, builder.config.build));
675 builder.info(&format!("Documenting stage{} compiler ({})", stage, target));
677 // This uses a shared directory so that librustdoc documentation gets
678 // correctly built and merged with the rustc documentation. This is
679 // needed because rustdoc is built in a different directory from
680 // rustc. rustdoc needs to be able to see everything, for example when
681 // merging the search index, or generating local (relative) links.
682 let out_dir = builder.stage_out(compiler, Mode::Rustc).join(target.triple).join("doc");
683 t!(symlink_dir_force(&builder.config, &out, &out_dir));
684 // Cargo puts proc macros in `target/doc` even if you pass `--target`
685 // explicitly (https://github.com/rust-lang/cargo/issues/7677).
686 let proc_macro_out_dir = builder.stage_out(compiler, Mode::Rustc).join("doc");
687 t!(symlink_dir_force(&builder.config, &out, &proc_macro_out_dir));
689 // Build cargo command.
690 let mut cargo = builder.cargo(compiler, Mode::Rustc, SourceType::InTree, target, "doc");
691 cargo.rustdocflag("--document-private-items");
692 // Since we always pass --document-private-items, there's no need to warn about linking to private items.
693 cargo.rustdocflag("-Arustdoc::private-intra-doc-links");
694 cargo.rustdocflag("--enable-index-page");
695 cargo.rustdocflag("-Zunstable-options");
696 cargo.rustdocflag("-Znormalize-docs");
697 cargo.rustdocflag("--show-type-layout");
698 cargo.rustdocflag("--generate-link-to-definition");
699 compile::rustc_cargo(builder, &mut cargo, target);
700 cargo.arg("-Zunstable-options");
701 cargo.arg("-Zskip-rustdoc-fingerprint");
703 // Only include compiler crates, no dependencies of those, such as `libc`.
704 // Do link to dependencies on `docs.rs` however using `rustdoc-map`.
705 cargo.arg("--no-deps");
706 cargo.arg("-Zrustdoc-map");
708 // FIXME: `-Zrustdoc-map` does not yet correctly work for transitive dependencies,
709 // once this is no longer an issue the special case for `ena` can be removed.
710 cargo.rustdocflag("--extern-html-root-url");
711 cargo.rustdocflag("ena=https://docs.rs/ena/latest/");
713 let root_crates = if paths.is_empty() {
715 INTERNER.intern_str("rustc_driver"),
716 INTERNER.intern_str("rustc_codegen_llvm"),
717 INTERNER.intern_str("rustc_codegen_ssa"),
720 paths.into_iter().map(|p| builder.crate_paths[p]).collect()
722 // Find dependencies for top level crates.
723 let compiler_crates = root_crates.iter().flat_map(|krate| {
724 builder.in_tree_crates(krate, Some(target)).into_iter().map(|krate| krate.name)
727 let mut to_open = None;
728 for krate in compiler_crates {
729 // Create all crate output directories first to make sure rustdoc uses
731 // FIXME: Cargo should probably do this itself.
732 t!(fs::create_dir_all(out_dir.join(krate)));
733 cargo.arg("-p").arg(krate);
734 if to_open.is_none() {
735 to_open = Some(krate);
739 builder.run(&mut cargo.into());
740 // Let's open the first crate documentation page:
741 if let Some(krate) = to_open {
742 let index = out.join(krate).join("index.html");
743 open(builder, &index);
748 macro_rules! tool_doc {
749 ($tool: ident, $should_run: literal, $path: literal, [$($krate: literal),+ $(,)?] $(,)?) => {
750 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
752 target: TargetSelection,
755 impl Step for $tool {
757 const DEFAULT: bool = true;
758 const ONLY_HOSTS: bool = true;
760 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
761 let builder = run.builder;
762 run.crate_or_deps($should_run).default_condition(builder.config.compiler_docs)
765 fn make_run(run: RunConfig<'_>) {
766 run.builder.ensure($tool { target: run.target });
769 /// Generates compiler documentation.
771 /// This will generate all documentation for compiler and dependencies.
772 /// Compiler documentation is distributed separately, so we make sure
773 /// we do not merge it with the other documentation from std, test and
774 /// proc_macros. This is largely just a wrapper around `cargo doc`.
775 fn run(self, builder: &Builder<'_>) {
776 let stage = builder.top_stage;
777 let target = self.target;
779 // This is the intended out directory for compiler documentation.
780 let out = builder.compiler_doc_out(target);
781 t!(fs::create_dir_all(&out));
783 // Build rustc docs so that we generate relative links.
784 builder.ensure(Rustc { stage, target });
785 // Rustdoc needs the rustc sysroot available to build.
786 // FIXME: is there a way to only ensure `check::Rustc` here? Last time I tried it failed
787 // with strange errors, but only on a full bors test ...
788 let compiler = builder.compiler(stage, builder.config.build);
789 builder.ensure(compile::Rustc::new(compiler, target));
793 "Documenting stage{} {} ({})",
795 stringify!($tool).to_lowercase(),
800 // Symlink compiler docs to the output directory of rustdoc documentation.
801 let out_dir = builder.stage_out(compiler, Mode::ToolRustc).join(target.triple).join("doc");
802 t!(fs::create_dir_all(&out_dir));
803 t!(symlink_dir_force(&builder.config, &out, &out_dir));
805 // Build cargo command.
806 let mut cargo = prepare_tool_cargo(
817 cargo.arg("-Zskip-rustdoc-fingerprint");
818 // Only include compiler crates, no dependencies of those, such as `libc`.
819 cargo.arg("--no-deps");
821 cargo.arg("-p").arg($krate);
824 cargo.rustdocflag("--document-private-items");
825 cargo.rustdocflag("--enable-index-page");
826 cargo.rustdocflag("--show-type-layout");
827 cargo.rustdocflag("--generate-link-to-definition");
828 cargo.rustdocflag("-Zunstable-options");
829 builder.run(&mut cargo.into());
835 tool_doc!(Rustdoc, "rustdoc-tool", "src/tools/rustdoc", ["rustdoc", "rustdoc-json-types"],);
840 ["rustfmt-nightly", "rustfmt-config_proc_macro"],
842 tool_doc!(Clippy, "clippy", "src/tools/clippy", ["clippy_utils"]);
843 tool_doc!(Miri, "miri", "src/tools/miri", ["miri"]);
845 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
846 pub struct ErrorIndex {
847 pub target: TargetSelection,
850 impl Step for ErrorIndex {
852 const DEFAULT: bool = true;
853 const ONLY_HOSTS: bool = true;
855 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
856 let builder = run.builder;
857 run.path("src/tools/error_index_generator").default_condition(builder.config.docs)
860 fn make_run(run: RunConfig<'_>) {
861 let target = run.target;
862 run.builder.ensure(ErrorIndex { target });
865 /// Generates the HTML rendered error-index by running the
866 /// `error_index_generator` tool.
867 fn run(self, builder: &Builder<'_>) {
868 builder.info(&format!("Documenting error index ({})", self.target));
869 let out = builder.doc_out(self.target);
870 t!(fs::create_dir_all(&out));
871 let mut index = tool::ErrorIndex::command(builder);
874 index.arg(&builder.version);
876 builder.run(&mut index);
880 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
881 pub struct UnstableBookGen {
882 target: TargetSelection,
885 impl Step for UnstableBookGen {
887 const DEFAULT: bool = true;
888 const ONLY_HOSTS: bool = true;
890 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
891 let builder = run.builder;
892 run.path("src/tools/unstable-book-gen").default_condition(builder.config.docs)
895 fn make_run(run: RunConfig<'_>) {
896 run.builder.ensure(UnstableBookGen { target: run.target });
899 fn run(self, builder: &Builder<'_>) {
900 let target = self.target;
902 builder.info(&format!("Generating unstable book md files ({})", target));
903 let out = builder.md_doc_out(target).join("unstable-book");
904 builder.create_dir(&out);
905 builder.remove_dir(&out);
906 let mut cmd = builder.tool_cmd(Tool::UnstableBookGen);
907 cmd.arg(builder.src.join("library"));
908 cmd.arg(builder.src.join("compiler"));
909 cmd.arg(builder.src.join("src"));
912 builder.run(&mut cmd);
916 fn symlink_dir_force(config: &Config, src: &Path, dst: &Path) -> io::Result<()> {
920 if let Ok(m) = fs::symlink_metadata(dst) {
921 if m.file_type().is_dir() {
922 fs::remove_dir_all(dst)?;
924 // handle directory junctions on windows by falling back to
926 fs::remove_file(dst).or_else(|_| fs::remove_dir(dst))?;
930 symlink_dir(config, src, dst)
933 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
934 pub struct RustcBook {
935 pub compiler: Compiler,
936 pub target: TargetSelection,
940 impl Step for RustcBook {
942 const DEFAULT: bool = true;
943 const ONLY_HOSTS: bool = true;
945 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
946 let builder = run.builder;
947 run.path("src/doc/rustc").default_condition(builder.config.docs)
950 fn make_run(run: RunConfig<'_>) {
951 run.builder.ensure(RustcBook {
952 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
958 /// Builds the rustc book.
960 /// The lints are auto-generated by a tool, and then merged into the book
961 /// in the "md-doc" directory in the build output directory. Then
962 /// "rustbook" is used to convert it to HTML.
963 fn run(self, builder: &Builder<'_>) {
964 let out_base = builder.md_doc_out(self.target).join("rustc");
965 t!(fs::create_dir_all(&out_base));
966 let out_listing = out_base.join("src/lints");
967 builder.cp_r(&builder.src.join("src/doc/rustc"), &out_base);
968 builder.info(&format!("Generating lint docs ({})", self.target));
970 let rustc = builder.rustc(self.compiler);
971 // The tool runs `rustc` for extracting output examples, so it needs a
972 // functional sysroot.
973 builder.ensure(compile::Std::new(self.compiler, self.target));
974 let mut cmd = builder.tool_cmd(Tool::LintDocs);
976 cmd.arg(builder.src.join("compiler"));
978 cmd.arg(&out_listing);
981 cmd.arg("--rustc-target").arg(&self.target.rustc_target_arg());
982 if builder.config.verbose() {
983 cmd.arg("--verbose");
986 cmd.arg("--validate");
988 if !builder.unstable_features() {
989 // We need to validate nightly features, even on the stable channel.
990 cmd.env("RUSTC_BOOTSTRAP", "1");
992 // If the lib directories are in an unusual location (changed in
993 // config.toml), then this needs to explicitly update the dylib search
995 builder.add_rustc_lib_path(self.compiler, &mut cmd);
996 builder.run(&mut cmd);
997 // Run rustbook/mdbook to generate the HTML pages.
998 builder.ensure(RustbookSrc {
1000 name: INTERNER.intern_str("rustc"),
1001 src: INTERNER.intern_path(out_base),
1003 if builder.was_invoked_explicitly::<Self>(Kind::Doc) {
1004 let out = builder.doc_out(self.target);
1005 let index = out.join("rustc").join("index.html");
1006 open(builder, &index);