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
10 use std::collections::HashSet;
13 use std::path::{Path, PathBuf};
16 use build_helper::{t, up_to_date};
18 use crate::builder::{Builder, Compiler, RunConfig, ShouldRun, Step};
19 use crate::cache::{Interned, INTERNER};
21 use crate::config::{Config, TargetSelection};
22 use crate::tool::{self, prepare_tool_cargo, SourceType, Tool};
23 use crate::util::symlink_dir;
26 ($($name:ident, $path:expr, $book_name:expr;)+) => {
28 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
30 target: TargetSelection,
35 const DEFAULT: bool = true;
37 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
38 let builder = run.builder;
39 run.path($path).default_condition(builder.config.docs)
42 fn make_run(run: RunConfig<'_>) {
43 run.builder.ensure($name {
48 fn run(self, builder: &Builder<'_>) {
49 builder.ensure(RustbookSrc {
51 name: INTERNER.intern_str($book_name),
52 src: INTERNER.intern_path(builder.src.join($path)),
60 // NOTE: When adding a book here, make sure to ALSO build the book by
61 // adding a build step in `src/bootstrap/builder.rs`!
63 CargoBook, "src/tools/cargo/src/doc", "cargo";
64 EditionGuide, "src/doc/edition-guide", "edition-guide";
65 EmbeddedBook, "src/doc/embedded-book", "embedded-book";
66 Nomicon, "src/doc/nomicon", "nomicon";
67 Reference, "src/doc/reference", "reference";
68 RustByExample, "src/doc/rust-by-example", "rust-by-example";
69 RustcBook, "src/doc/rustc", "rustc";
70 RustdocBook, "src/doc/rustdoc", "rustdoc";
73 fn open(builder: &Builder<'_>, path: impl AsRef<Path>) {
74 if builder.config.dry_run || !builder.config.cmd.open() {
78 let path = path.as_ref();
79 builder.info(&format!("Opening doc {}", path.display()));
80 if let Err(err) = opener::open(path) {
81 builder.info(&format!("{}\n", err));
85 // "src/libstd" -> ["src", "libstd"]
87 // Used for deciding whether a particular step is one requested by the user on
88 // the `x.py doc` command line, which determines whether `--open` will open that
90 fn components_simplified(path: &PathBuf) -> Vec<&str> {
91 path.iter().map(|component| component.to_str().unwrap_or("???")).collect()
94 fn is_explicit_request(builder: &Builder<'_>, path: &str) -> bool {
98 .map(components_simplified)
99 .any(|requested| requested.iter().copied().eq(path.split("/")))
102 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
103 pub struct UnstableBook {
104 target: TargetSelection,
107 impl Step for UnstableBook {
109 const DEFAULT: bool = true;
111 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
112 let builder = run.builder;
113 run.path("src/doc/unstable-book").default_condition(builder.config.docs)
116 fn make_run(run: RunConfig<'_>) {
117 run.builder.ensure(UnstableBook { target: run.target });
120 fn run(self, builder: &Builder<'_>) {
121 builder.ensure(UnstableBookGen { target: self.target });
122 builder.ensure(RustbookSrc {
124 name: INTERNER.intern_str("unstable-book"),
125 src: INTERNER.intern_path(builder.md_doc_out(self.target).join("unstable-book")),
130 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
132 target: TargetSelection,
133 name: Interned<String>,
134 src: Interned<PathBuf>,
137 impl Step for RustbookSrc {
140 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
144 /// Invoke `rustbook` for `target` for the doc book `name` from the `src` path.
146 /// This will not actually generate any documentation if the documentation has
147 /// already been generated.
148 fn run(self, builder: &Builder<'_>) {
149 let target = self.target;
150 let name = self.name;
152 let out = builder.doc_out(target);
153 t!(fs::create_dir_all(&out));
155 let out = out.join(name);
156 let index = out.join("index.html");
157 let rustbook = builder.tool_exe(Tool::Rustbook);
158 let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
159 if builder.config.dry_run || up_to_date(&src, &index) && up_to_date(&rustbook, &index) {
162 builder.info(&format!("Rustbook ({}) - {}", target, name));
163 let _ = fs::remove_dir_all(&out);
165 builder.run(rustbook_cmd.arg("build").arg(&src).arg("-d").arg(out));
169 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
172 target: TargetSelection,
175 impl Step for TheBook {
177 const DEFAULT: bool = true;
179 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
180 let builder = run.builder;
181 run.path("src/doc/book").default_condition(builder.config.docs)
184 fn make_run(run: RunConfig<'_>) {
185 run.builder.ensure(TheBook {
186 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
191 /// Builds the book and associated stuff.
193 /// We need to build:
196 /// * Older edition redirects
197 /// * Version info and CSS
200 fn run(self, builder: &Builder<'_>) {
201 let compiler = self.compiler;
202 let target = self.target;
205 builder.ensure(RustbookSrc {
207 name: INTERNER.intern_str("book"),
208 src: INTERNER.intern_path(builder.src.join("src/doc/book")),
211 // building older edition redirects
212 for edition in &["first-edition", "second-edition", "2018-edition"] {
213 builder.ensure(RustbookSrc {
215 name: INTERNER.intern_string(format!("book/{}", edition)),
216 src: INTERNER.intern_path(builder.src.join("src/doc/book").join(edition)),
220 // build the version info page and CSS
221 builder.ensure(Standalone { compiler, target });
223 // build the redirect pages
224 builder.info(&format!("Documenting book redirect pages ({})", target));
225 for file in t!(fs::read_dir(builder.src.join("src/doc/book/redirects"))) {
227 let path = file.path();
228 let path = path.to_str().unwrap();
230 invoke_rustdoc(builder, compiler, target, path);
233 if is_explicit_request(builder, "src/doc/book") {
234 let out = builder.doc_out(target);
235 let index = out.join("book").join("index.html");
236 open(builder, &index);
242 builder: &Builder<'_>,
244 target: TargetSelection,
247 let out = builder.doc_out(target);
249 let path = builder.src.join("src/doc").join(markdown);
251 let header = builder.src.join("src/doc/redirect.inc");
252 let footer = builder.src.join("src/doc/footer.inc");
253 let version_info = out.join("version_info.html");
255 let mut cmd = builder.rustdoc_cmd(compiler);
257 let out = out.join("book");
259 cmd.arg("--html-after-content")
261 .arg("--html-before-content")
263 .arg("--html-in-header")
265 .arg("--markdown-no-toc")
266 .arg("--markdown-playground-url")
267 .arg("https://play.rust-lang.org/")
271 .arg("--markdown-css")
274 builder.run(&mut cmd);
277 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
278 pub struct Standalone {
280 target: TargetSelection,
283 impl Step for Standalone {
285 const DEFAULT: bool = true;
287 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
288 let builder = run.builder;
289 run.path("src/doc").default_condition(builder.config.docs)
292 fn make_run(run: RunConfig<'_>) {
293 run.builder.ensure(Standalone {
294 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
299 /// Generates all standalone documentation as compiled by the rustdoc in `stage`
300 /// for the `target` into `out`.
302 /// This will list all of `src/doc` looking for markdown files and appropriately
303 /// perform transformations like substituting `VERSION`, `SHORT_HASH`, and
304 /// `STAMP` along with providing the various header/footer HTML we've customized.
306 /// In the end, this is just a glorified wrapper around rustdoc!
307 fn run(self, builder: &Builder<'_>) {
308 let target = self.target;
309 let compiler = self.compiler;
310 builder.info(&format!("Documenting standalone ({})", target));
311 let out = builder.doc_out(target);
312 t!(fs::create_dir_all(&out));
314 let favicon = builder.src.join("src/doc/favicon.inc");
315 let footer = builder.src.join("src/doc/footer.inc");
316 let full_toc = builder.src.join("src/doc/full-toc.inc");
317 t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
319 let version_input = builder.src.join("src/doc/version_info.html.template");
320 let version_info = out.join("version_info.html");
322 if !builder.config.dry_run && !up_to_date(&version_input, &version_info) {
323 let info = t!(fs::read_to_string(&version_input))
324 .replace("VERSION", &builder.rust_release())
325 .replace("SHORT_HASH", builder.rust_info.sha_short().unwrap_or(""))
326 .replace("STAMP", builder.rust_info.sha().unwrap_or(""));
327 t!(fs::write(&version_info, &info));
330 for file in t!(fs::read_dir(builder.src.join("src/doc"))) {
332 let path = file.path();
333 let filename = path.file_name().unwrap().to_str().unwrap();
334 if !filename.ends_with(".md") || filename == "README.md" {
338 let html = out.join(filename).with_extension("html");
339 let rustdoc = builder.rustdoc(compiler);
340 if up_to_date(&path, &html)
341 && up_to_date(&footer, &html)
342 && up_to_date(&favicon, &html)
343 && up_to_date(&full_toc, &html)
344 && (builder.config.dry_run || up_to_date(&version_info, &html))
345 && (builder.config.dry_run || up_to_date(&rustdoc, &html))
350 let mut cmd = builder.rustdoc_cmd(compiler);
351 // Needed for --index-page flag
352 cmd.arg("-Z").arg("unstable-options");
354 cmd.arg("--html-after-content")
356 .arg("--html-before-content")
358 .arg("--html-in-header")
360 .arg("--markdown-no-toc")
362 .arg(&builder.src.join("src/doc/index.md"))
363 .arg("--markdown-playground-url")
364 .arg("https://play.rust-lang.org/")
369 if filename == "not_found.md" {
370 cmd.arg("--markdown-css").arg("https://doc.rust-lang.org/rust.css");
372 cmd.arg("--markdown-css").arg("rust.css");
374 builder.run(&mut cmd);
377 // We open doc/index.html as the default if invoked as `x.py doc --open`
378 // with no particular explicit doc requested (e.g. src/libcore).
379 if builder.paths.is_empty() || is_explicit_request(builder, "src/doc") {
380 let index = out.join("index.html");
381 open(builder, &index);
386 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
389 pub target: TargetSelection,
394 const DEFAULT: bool = true;
396 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
397 let builder = run.builder;
398 run.all_krates("test").default_condition(builder.config.docs)
401 fn make_run(run: RunConfig<'_>) {
402 run.builder.ensure(Std { stage: run.builder.top_stage, target: run.target });
405 /// Compile all standard library documentation.
407 /// This will generate all documentation for the standard library and its
408 /// dependencies. This is largely just a wrapper around `cargo doc`.
409 fn run(self, builder: &Builder<'_>) {
410 let stage = self.stage;
411 let target = self.target;
412 builder.info(&format!("Documenting stage{} std ({})", stage, target));
413 let out = builder.doc_out(target);
414 t!(fs::create_dir_all(&out));
415 let compiler = builder.compiler(stage, builder.config.build);
417 builder.ensure(compile::Std { compiler, target });
418 let out_dir = builder.stage_out(compiler, Mode::Std).join(target.triple).join("doc");
420 t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
422 let run_cargo_rustdoc_for = |package: &str| {
424 builder.cargo(compiler, Mode::Std, SourceType::InTree, target, "rustdoc");
425 compile::std_cargo(builder, target, compiler.stage, &mut cargo);
431 .arg("--markdown-css")
433 .arg("--markdown-no-toc")
435 .arg("unstable-options")
436 .arg("--resource-suffix")
437 .arg(crate::channel::CFG_RELEASE_NUM)
439 .arg(&builder.src.join("src/doc/index.md"));
441 builder.run(&mut cargo.into());
443 // Only build the following crates. While we could just iterate over the
444 // folder structure, that would also build internal crates that we do
445 // not want to show in documentation. These crates will later be visited
446 // by the rustc step, so internal documentation will show them.
448 // Note that the order here is important! The crates need to be
449 // processed starting from the leaves, otherwise rustdoc will not
450 // create correct links between crates because rustdoc depends on the
451 // existence of the output directories to know if it should be a local
453 let krates = ["core", "alloc", "std", "proc_macro", "test"];
454 for krate in &krates {
455 run_cargo_rustdoc_for(krate);
457 builder.cp_r(&out_dir, &out);
459 // Look for src/libstd, src/libcore etc in the `x.py doc` arguments and
460 // open the corresponding rendered docs.
461 for path in builder.paths.iter().map(components_simplified) {
462 if path.get(0) == Some(&"src")
463 && path.get(1).map_or(false, |dir| dir.starts_with("lib"))
465 let requested_crate = &path[1][3..];
466 if krates.contains(&requested_crate) {
467 let index = out.join(requested_crate).join("index.html");
468 open(builder, &index);
475 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
478 target: TargetSelection,
481 impl Step for Rustc {
483 const DEFAULT: bool = true;
484 const ONLY_HOSTS: bool = true;
486 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
487 let builder = run.builder;
488 run.krate("rustc-main").default_condition(builder.config.docs)
491 fn make_run(run: RunConfig<'_>) {
492 run.builder.ensure(Rustc { stage: run.builder.top_stage, target: run.target });
495 /// Generates compiler documentation.
497 /// This will generate all documentation for compiler and dependencies.
498 /// Compiler documentation is distributed separately, so we make sure
499 /// we do not merge it with the other documentation from std, test and
500 /// proc_macros. This is largely just a wrapper around `cargo doc`.
501 fn run(self, builder: &Builder<'_>) {
502 let stage = self.stage;
503 let target = self.target;
504 builder.info(&format!("Documenting stage{} compiler ({})", stage, target));
506 // This is the intended out directory for compiler documentation.
507 let out = builder.compiler_doc_out(target);
508 t!(fs::create_dir_all(&out));
510 let compiler = builder.compiler(stage, builder.config.build);
512 if !builder.config.compiler_docs {
513 builder.info("\tskipping - compiler/librustdoc docs disabled");
518 builder.ensure(compile::Rustc { compiler, target });
520 // This uses a shared directory so that librustdoc documentation gets
521 // correctly built and merged with the rustc documentation. This is
522 // needed because rustdoc is built in a different directory from
523 // rustc. rustdoc needs to be able to see everything, for example when
524 // merging the search index, or generating local (relative) links.
525 let out_dir = builder.stage_out(compiler, Mode::Rustc).join(target.triple).join("doc");
526 t!(symlink_dir_force(&builder.config, &out, &out_dir));
528 // Build cargo command.
529 let mut cargo = builder.cargo(compiler, Mode::Rustc, SourceType::InTree, target, "doc");
530 cargo.rustdocflag("--document-private-items");
531 cargo.rustdocflag("--enable-index-page");
532 cargo.rustdocflag("-Zunstable-options");
533 compile::rustc_cargo(builder, &mut cargo, target);
535 // Only include compiler crates, no dependencies of those, such as `libc`.
536 cargo.arg("--no-deps");
538 // Find dependencies for top level crates.
539 let mut compiler_crates = HashSet::new();
540 for root_crate in &["rustc_driver", "rustc_codegen_llvm", "rustc_codegen_ssa"] {
542 .extend(builder.in_tree_crates(root_crate).into_iter().map(|krate| krate.name));
545 for krate in &compiler_crates {
546 // Create all crate output directories first to make sure rustdoc uses
548 // FIXME: Cargo should probably do this itself.
549 t!(fs::create_dir_all(out_dir.join(krate)));
550 cargo.arg("-p").arg(krate);
553 builder.run(&mut cargo.into());
557 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
560 target: TargetSelection,
563 impl Step for Rustdoc {
565 const DEFAULT: bool = true;
566 const ONLY_HOSTS: bool = true;
568 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
569 run.krate("rustdoc-tool")
572 fn make_run(run: RunConfig<'_>) {
573 run.builder.ensure(Rustdoc { stage: run.builder.top_stage, target: run.target });
576 /// Generates compiler documentation.
578 /// This will generate all documentation for compiler and dependencies.
579 /// Compiler documentation is distributed separately, so we make sure
580 /// we do not merge it with the other documentation from std, test and
581 /// proc_macros. This is largely just a wrapper around `cargo doc`.
582 fn run(self, builder: &Builder<'_>) {
583 let stage = self.stage;
584 let target = self.target;
585 builder.info(&format!("Documenting stage{} rustdoc ({})", stage, target));
587 // This is the intended out directory for compiler documentation.
588 let out = builder.compiler_doc_out(target);
589 t!(fs::create_dir_all(&out));
591 let compiler = builder.compiler(stage, builder.config.build);
593 if !builder.config.compiler_docs {
594 builder.info("\tskipping - compiler/librustdoc docs disabled");
598 // Build rustc docs so that we generate relative links.
599 builder.ensure(Rustc { stage, target });
602 builder.ensure(tool::Rustdoc { compiler });
604 // Symlink compiler docs to the output directory of rustdoc documentation.
605 let out_dir = builder.stage_out(compiler, Mode::ToolRustc).join(target.triple).join("doc");
606 t!(fs::create_dir_all(&out_dir));
607 t!(symlink_dir_force(&builder.config, &out, &out_dir));
609 // Build cargo command.
610 let mut cargo = prepare_tool_cargo(
621 // Only include compiler crates, no dependencies of those, such as `libc`.
622 cargo.arg("--no-deps");
623 cargo.arg("-p").arg("rustdoc");
625 cargo.rustdocflag("--document-private-items");
626 builder.run(&mut cargo.into());
630 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
631 pub struct ErrorIndex {
632 pub compiler: Compiler,
633 pub target: TargetSelection,
636 impl Step for ErrorIndex {
638 const DEFAULT: bool = true;
639 const ONLY_HOSTS: bool = true;
641 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
642 let builder = run.builder;
643 run.path("src/tools/error_index_generator").default_condition(builder.config.docs)
646 fn make_run(run: RunConfig<'_>) {
647 let target = run.target;
648 // error_index_generator depends on librustdoc. Use the compiler that
649 // is normally used to build rustdoc for other documentation so that
650 // it shares the same artifacts.
652 run.builder.compiler_for(run.builder.top_stage, run.builder.config.build, target);
653 run.builder.ensure(ErrorIndex { compiler, target });
656 /// Generates the HTML rendered error-index by running the
657 /// `error_index_generator` tool.
658 fn run(self, builder: &Builder<'_>) {
659 builder.info(&format!("Documenting error index ({})", self.target));
660 let out = builder.doc_out(self.target);
661 t!(fs::create_dir_all(&out));
662 let mut index = tool::ErrorIndex::command(builder, self.compiler);
664 index.arg(out.join("error-index.html"));
665 index.arg(crate::channel::CFG_RELEASE_NUM);
667 builder.run(&mut index);
671 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
672 pub struct UnstableBookGen {
673 target: TargetSelection,
676 impl Step for UnstableBookGen {
678 const DEFAULT: bool = true;
679 const ONLY_HOSTS: bool = true;
681 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
682 let builder = run.builder;
683 run.path("src/tools/unstable-book-gen").default_condition(builder.config.docs)
686 fn make_run(run: RunConfig<'_>) {
687 run.builder.ensure(UnstableBookGen { target: run.target });
690 fn run(self, builder: &Builder<'_>) {
691 let target = self.target;
693 builder.info(&format!("Generating unstable book md files ({})", target));
694 let out = builder.md_doc_out(target).join("unstable-book");
695 builder.create_dir(&out);
696 builder.remove_dir(&out);
697 let mut cmd = builder.tool_cmd(Tool::UnstableBookGen);
698 cmd.arg(builder.src.join("src"));
701 builder.run(&mut cmd);
705 fn symlink_dir_force(config: &Config, src: &Path, dst: &Path) -> io::Result<()> {
709 if let Ok(m) = fs::symlink_metadata(dst) {
710 if m.file_type().is_dir() {
711 fs::remove_dir_all(dst)?;
713 // handle directory junctions on windows by falling back to
715 fs::remove_file(dst).or_else(|_| fs::remove_dir(dst))?;
719 symlink_dir(config, src, dst)