]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/doc.rs
Rollup merge of #93217 - willcrichton:example-analyzer, r=GuillaumeGomez
[rust.git] / src / bootstrap / doc.rs
1 //! Documentation generation for rustbuilder.
2 //!
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.
6 //!
7 //! Everything here is basically just a shim around calling either `rustbook` or
8 //! `rustdoc`.
9
10 use std::fs;
11 use std::io;
12 use std::path::{Path, PathBuf};
13
14 use crate::builder::{Builder, Compiler, Kind, RunConfig, ShouldRun, Step};
15 use crate::cache::{Interned, INTERNER};
16 use crate::compile;
17 use crate::config::{Config, TargetSelection};
18 use crate::tool::{self, prepare_tool_cargo, SourceType, Tool};
19 use crate::util::{symlink_dir, t, up_to_date};
20 use crate::Mode;
21
22 macro_rules! submodule_helper {
23     ($path:expr, submodule) => {
24         $path
25     };
26     ($path:expr, submodule = $submodule:literal) => {
27         $submodule
28     };
29 }
30
31 macro_rules! book {
32     ($($name:ident, $path:expr, $book_name:expr $(, submodule $(= $submodule:literal)? )? ;)+) => {
33         $(
34             #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
35         pub struct $name {
36             target: TargetSelection,
37         }
38
39         impl Step for $name {
40             type Output = ();
41             const DEFAULT: bool = true;
42
43             fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
44                 let builder = run.builder;
45                 run.path($path).default_condition(builder.config.docs)
46             }
47
48             fn make_run(run: RunConfig<'_>) {
49                 run.builder.ensure($name {
50                     target: run.target,
51                 });
52             }
53
54             fn run(self, builder: &Builder<'_>) {
55                 $(
56                     let path = Path::new(submodule_helper!( $path, submodule $( = $submodule )? ));
57                     builder.update_submodule(&path);
58                 )?
59                 builder.ensure(RustbookSrc {
60                     target: self.target,
61                     name: INTERNER.intern_str($book_name),
62                     src: INTERNER.intern_path(builder.src.join($path)),
63                 })
64             }
65         }
66         )+
67     }
68 }
69
70 // NOTE: When adding a book here, make sure to ALSO build the book by
71 // adding a build step in `src/bootstrap/builder.rs`!
72 // NOTE: Make sure to add the corresponding submodule when adding a new book.
73 // FIXME: Make checking for a submodule automatic somehow (maybe by having a list of all submodules
74 // and checking against it?).
75 book!(
76     CargoBook, "src/tools/cargo/src/doc", "cargo", submodule = "src/tools/cargo";
77     EditionGuide, "src/doc/edition-guide", "edition-guide", submodule;
78     EmbeddedBook, "src/doc/embedded-book", "embedded-book", submodule;
79     Nomicon, "src/doc/nomicon", "nomicon", submodule;
80     Reference, "src/doc/reference", "reference", submodule;
81     RustByExample, "src/doc/rust-by-example", "rust-by-example", submodule;
82     RustdocBook, "src/doc/rustdoc", "rustdoc";
83 );
84
85 fn open(builder: &Builder<'_>, path: impl AsRef<Path>) {
86     if builder.config.dry_run || !builder.config.cmd.open() {
87         return;
88     }
89
90     let path = path.as_ref();
91     builder.info(&format!("Opening doc {}", path.display()));
92     if let Err(err) = opener::open(path) {
93         builder.info(&format!("{}\n", err));
94     }
95 }
96
97 // "library/std" -> ["library", "std"]
98 //
99 // Used for deciding whether a particular step is one requested by the user on
100 // the `x.py doc` command line, which determines whether `--open` will open that
101 // page.
102 pub(crate) fn components_simplified(path: &PathBuf) -> Vec<&str> {
103     path.iter().map(|component| component.to_str().unwrap_or("???")).collect()
104 }
105
106 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
107 pub struct UnstableBook {
108     target: TargetSelection,
109 }
110
111 impl Step for UnstableBook {
112     type Output = ();
113     const DEFAULT: bool = true;
114
115     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
116         let builder = run.builder;
117         run.path("src/doc/unstable-book").default_condition(builder.config.docs)
118     }
119
120     fn make_run(run: RunConfig<'_>) {
121         run.builder.ensure(UnstableBook { target: run.target });
122     }
123
124     fn run(self, builder: &Builder<'_>) {
125         builder.ensure(UnstableBookGen { target: self.target });
126         builder.ensure(RustbookSrc {
127             target: self.target,
128             name: INTERNER.intern_str("unstable-book"),
129             src: INTERNER.intern_path(builder.md_doc_out(self.target).join("unstable-book")),
130         })
131     }
132 }
133
134 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
135 struct RustbookSrc {
136     target: TargetSelection,
137     name: Interned<String>,
138     src: Interned<PathBuf>,
139 }
140
141 impl Step for RustbookSrc {
142     type Output = ();
143
144     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
145         run.never()
146     }
147
148     /// Invoke `rustbook` for `target` for the doc book `name` from the `src` path.
149     ///
150     /// This will not actually generate any documentation if the documentation has
151     /// already been generated.
152     fn run(self, builder: &Builder<'_>) {
153         let target = self.target;
154         let name = self.name;
155         let src = self.src;
156         let out = builder.doc_out(target);
157         t!(fs::create_dir_all(&out));
158
159         let out = out.join(name);
160         let index = out.join("index.html");
161         let rustbook = builder.tool_exe(Tool::Rustbook);
162         let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
163         if builder.config.dry_run || up_to_date(&src, &index) && up_to_date(&rustbook, &index) {
164             return;
165         }
166         builder.info(&format!("Rustbook ({}) - {}", target, name));
167         let _ = fs::remove_dir_all(&out);
168
169         builder.run(rustbook_cmd.arg("build").arg(&src).arg("-d").arg(out));
170     }
171 }
172
173 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
174 pub struct TheBook {
175     compiler: Compiler,
176     target: TargetSelection,
177 }
178
179 impl Step for TheBook {
180     type Output = ();
181     const DEFAULT: bool = true;
182
183     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
184         let builder = run.builder;
185         run.path("src/doc/book").default_condition(builder.config.docs)
186     }
187
188     fn make_run(run: RunConfig<'_>) {
189         run.builder.ensure(TheBook {
190             compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
191             target: run.target,
192         });
193     }
194
195     /// Builds the book and associated stuff.
196     ///
197     /// We need to build:
198     ///
199     /// * Book
200     /// * Older edition redirects
201     /// * Version info and CSS
202     /// * Index page
203     /// * Redirect pages
204     fn run(self, builder: &Builder<'_>) {
205         let relative_path = Path::new("src").join("doc").join("book");
206         builder.update_submodule(&relative_path);
207
208         let compiler = self.compiler;
209         let target = self.target;
210
211         // build book
212         builder.ensure(RustbookSrc {
213             target,
214             name: INTERNER.intern_str("book"),
215             src: INTERNER.intern_path(builder.src.join(&relative_path)),
216         });
217
218         // building older edition redirects
219         for edition in &["first-edition", "second-edition", "2018-edition"] {
220             builder.ensure(RustbookSrc {
221                 target,
222                 name: INTERNER.intern_string(format!("book/{}", edition)),
223                 src: INTERNER.intern_path(builder.src.join(&relative_path).join(edition)),
224             });
225         }
226
227         // build the version info page and CSS
228         builder.ensure(Standalone { compiler, target });
229
230         // build the redirect pages
231         builder.info(&format!("Documenting book redirect pages ({})", target));
232         for file in t!(fs::read_dir(builder.src.join(&relative_path).join("redirects"))) {
233             let file = t!(file);
234             let path = file.path();
235             let path = path.to_str().unwrap();
236
237             invoke_rustdoc(builder, compiler, target, path);
238         }
239
240         if builder.was_invoked_explicitly::<Self>(Kind::Doc) {
241             let out = builder.doc_out(target);
242             let index = out.join("book").join("index.html");
243             open(builder, &index);
244         }
245     }
246 }
247
248 fn invoke_rustdoc(
249     builder: &Builder<'_>,
250     compiler: Compiler,
251     target: TargetSelection,
252     markdown: &str,
253 ) {
254     let out = builder.doc_out(target);
255
256     let path = builder.src.join("src/doc").join(markdown);
257
258     let header = builder.src.join("src/doc/redirect.inc");
259     let footer = builder.src.join("src/doc/footer.inc");
260     let version_info = out.join("version_info.html");
261
262     let mut cmd = builder.rustdoc_cmd(compiler);
263
264     let out = out.join("book");
265
266     cmd.arg("--html-after-content")
267         .arg(&footer)
268         .arg("--html-before-content")
269         .arg(&version_info)
270         .arg("--html-in-header")
271         .arg(&header)
272         .arg("--markdown-no-toc")
273         .arg("--markdown-playground-url")
274         .arg("https://play.rust-lang.org/")
275         .arg("-o")
276         .arg(&out)
277         .arg(&path)
278         .arg("--markdown-css")
279         .arg("../rust.css");
280
281     if !builder.config.docs_minification {
282         cmd.arg("-Z").arg("unstable-options").arg("--disable-minification");
283     }
284
285     builder.run(&mut cmd);
286 }
287
288 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
289 pub struct Standalone {
290     compiler: Compiler,
291     target: TargetSelection,
292 }
293
294 impl Step for Standalone {
295     type Output = ();
296     const DEFAULT: bool = true;
297
298     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
299         let builder = run.builder;
300         run.path("src/doc").default_condition(builder.config.docs)
301     }
302
303     fn make_run(run: RunConfig<'_>) {
304         run.builder.ensure(Standalone {
305             compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
306             target: run.target,
307         });
308     }
309
310     /// Generates all standalone documentation as compiled by the rustdoc in `stage`
311     /// for the `target` into `out`.
312     ///
313     /// This will list all of `src/doc` looking for markdown files and appropriately
314     /// perform transformations like substituting `VERSION`, `SHORT_HASH`, and
315     /// `STAMP` along with providing the various header/footer HTML we've customized.
316     ///
317     /// In the end, this is just a glorified wrapper around rustdoc!
318     fn run(self, builder: &Builder<'_>) {
319         let target = self.target;
320         let compiler = self.compiler;
321         builder.info(&format!("Documenting standalone ({})", target));
322         let out = builder.doc_out(target);
323         t!(fs::create_dir_all(&out));
324
325         let favicon = builder.src.join("src/doc/favicon.inc");
326         let footer = builder.src.join("src/doc/footer.inc");
327         let full_toc = builder.src.join("src/doc/full-toc.inc");
328         t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
329
330         let version_input = builder.src.join("src/doc/version_info.html.template");
331         let version_info = out.join("version_info.html");
332
333         if !builder.config.dry_run && !up_to_date(&version_input, &version_info) {
334             let info = t!(fs::read_to_string(&version_input))
335                 .replace("VERSION", &builder.rust_release())
336                 .replace("SHORT_HASH", builder.rust_info.sha_short().unwrap_or(""))
337                 .replace("STAMP", builder.rust_info.sha().unwrap_or(""));
338             t!(fs::write(&version_info, &info));
339         }
340
341         for file in t!(fs::read_dir(builder.src.join("src/doc"))) {
342             let file = t!(file);
343             let path = file.path();
344             let filename = path.file_name().unwrap().to_str().unwrap();
345             if !filename.ends_with(".md") || filename == "README.md" {
346                 continue;
347             }
348
349             let html = out.join(filename).with_extension("html");
350             let rustdoc = builder.rustdoc(compiler);
351             if up_to_date(&path, &html)
352                 && up_to_date(&footer, &html)
353                 && up_to_date(&favicon, &html)
354                 && up_to_date(&full_toc, &html)
355                 && (builder.config.dry_run || up_to_date(&version_info, &html))
356                 && (builder.config.dry_run || up_to_date(&rustdoc, &html))
357             {
358                 continue;
359             }
360
361             let mut cmd = builder.rustdoc_cmd(compiler);
362             // Needed for --index-page flag
363             cmd.arg("-Z").arg("unstable-options");
364
365             cmd.arg("--html-after-content")
366                 .arg(&footer)
367                 .arg("--html-before-content")
368                 .arg(&version_info)
369                 .arg("--html-in-header")
370                 .arg(&favicon)
371                 .arg("--markdown-no-toc")
372                 .arg("--index-page")
373                 .arg(&builder.src.join("src/doc/index.md"))
374                 .arg("--markdown-playground-url")
375                 .arg("https://play.rust-lang.org/")
376                 .arg("-o")
377                 .arg(&out)
378                 .arg(&path);
379
380             if !builder.config.docs_minification {
381                 cmd.arg("--disable-minification");
382             }
383
384             if filename == "not_found.md" {
385                 cmd.arg("--markdown-css")
386                     .arg(format!("https://doc.rust-lang.org/rustdoc{}.css", &builder.version))
387                     .arg("--markdown-css")
388                     .arg("https://doc.rust-lang.org/rust.css");
389             } else {
390                 cmd.arg("--markdown-css")
391                     .arg(format!("rustdoc{}.css", &builder.version))
392                     .arg("--markdown-css")
393                     .arg("rust.css");
394             }
395             builder.run(&mut cmd);
396         }
397
398         // We open doc/index.html as the default if invoked as `x.py doc --open`
399         // with no particular explicit doc requested (e.g. library/core).
400         if builder.paths.is_empty() || builder.was_invoked_explicitly::<Self>(Kind::Doc) {
401             let index = out.join("index.html");
402             open(builder, &index);
403         }
404     }
405 }
406
407 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
408 pub struct Std {
409     pub stage: u32,
410     pub target: TargetSelection,
411 }
412
413 impl Step for Std {
414     type Output = ();
415     const DEFAULT: bool = true;
416
417     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
418         let builder = run.builder;
419         run.all_krates("test").default_condition(builder.config.docs)
420     }
421
422     fn make_run(run: RunConfig<'_>) {
423         run.builder.ensure(Std { stage: run.builder.top_stage, target: run.target });
424     }
425
426     /// Compile all standard library documentation.
427     ///
428     /// This will generate all documentation for the standard library and its
429     /// dependencies. This is largely just a wrapper around `cargo doc`.
430     fn run(self, builder: &Builder<'_>) {
431         let stage = self.stage;
432         let target = self.target;
433         builder.info(&format!("Documenting stage{} std ({})", stage, target));
434         if builder.no_std(target) == Some(true) {
435             panic!(
436                 "building std documentation for no_std target {target} is not supported\n\
437                  Set `docs = false` in the config to disable documentation."
438             );
439         }
440         let out = builder.doc_out(target);
441         t!(fs::create_dir_all(&out));
442         let compiler = builder.compiler(stage, builder.config.build);
443
444         builder.ensure(compile::Std { compiler, target });
445         let out_dir = builder.stage_out(compiler, Mode::Std).join(target.triple).join("doc");
446
447         t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
448
449         let run_cargo_rustdoc_for = |package: &str| {
450             let mut cargo =
451                 builder.cargo(compiler, Mode::Std, SourceType::InTree, target, "rustdoc");
452             compile::std_cargo(builder, target, compiler.stage, &mut cargo);
453
454             cargo
455                 .arg("-p")
456                 .arg(package)
457                 .arg("-Zskip-rustdoc-fingerprint")
458                 .arg("--")
459                 .arg("--markdown-css")
460                 .arg("rust.css")
461                 .arg("--markdown-no-toc")
462                 .arg("-Z")
463                 .arg("unstable-options")
464                 .arg("--resource-suffix")
465                 .arg(&builder.version)
466                 .arg("--index-page")
467                 .arg(&builder.src.join("src/doc/index.md"));
468
469             if !builder.config.docs_minification {
470                 cargo.arg("--disable-minification");
471             }
472
473             builder.run(&mut cargo.into());
474         };
475
476         let paths = builder
477             .paths
478             .iter()
479             .map(components_simplified)
480             .filter_map(|path| {
481                 if path.get(0) == Some(&"library") {
482                     Some(path[1].to_owned())
483                 } else if !path.is_empty() {
484                     Some(path[0].to_owned())
485                 } else {
486                     None
487                 }
488             })
489             .collect::<Vec<_>>();
490
491         // Only build the following crates. While we could just iterate over the
492         // folder structure, that would also build internal crates that we do
493         // not want to show in documentation. These crates will later be visited
494         // by the rustc step, so internal documentation will show them.
495         //
496         // Note that the order here is important! The crates need to be
497         // processed starting from the leaves, otherwise rustdoc will not
498         // create correct links between crates because rustdoc depends on the
499         // existence of the output directories to know if it should be a local
500         // or remote link.
501         let krates = ["core", "alloc", "std", "proc_macro", "test"];
502         for krate in &krates {
503             run_cargo_rustdoc_for(krate);
504             if paths.iter().any(|p| p == krate) {
505                 // No need to document more of the libraries if we have the one we want.
506                 break;
507             }
508         }
509         builder.cp_r(&out_dir, &out);
510
511         // Look for library/std, library/core etc in the `x.py doc` arguments and
512         // open the corresponding rendered docs.
513         for requested_crate in paths {
514             if krates.iter().any(|k| *k == requested_crate.as_str()) {
515                 let index = out.join(requested_crate).join("index.html");
516                 open(builder, &index);
517             }
518         }
519     }
520 }
521
522 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
523 pub struct Rustc {
524     pub stage: u32,
525     pub target: TargetSelection,
526 }
527
528 impl Step for Rustc {
529     type Output = ();
530     const DEFAULT: bool = true;
531     const ONLY_HOSTS: bool = true;
532
533     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
534         let builder = run.builder;
535         run.krate("rustc-main").path("compiler").default_condition(builder.config.compiler_docs)
536     }
537
538     fn make_run(run: RunConfig<'_>) {
539         run.builder.ensure(Rustc { stage: run.builder.top_stage, target: run.target });
540     }
541
542     /// Generates compiler documentation.
543     ///
544     /// This will generate all documentation for compiler and dependencies.
545     /// Compiler documentation is distributed separately, so we make sure
546     /// we do not merge it with the other documentation from std, test and
547     /// proc_macros. This is largely just a wrapper around `cargo doc`.
548     fn run(self, builder: &Builder<'_>) {
549         let stage = self.stage;
550         let target = self.target;
551         builder.info(&format!("Documenting stage{} compiler ({})", stage, target));
552
553         let paths = builder
554             .paths
555             .iter()
556             .filter(|path| {
557                 let components = components_simplified(path);
558                 components.len() >= 2 && components[0] == "compiler"
559             })
560             .collect::<Vec<_>>();
561
562         // This is the intended out directory for compiler documentation.
563         let out = builder.compiler_doc_out(target);
564         t!(fs::create_dir_all(&out));
565
566         // Build rustc.
567         let compiler = builder.compiler(stage, builder.config.build);
568         builder.ensure(compile::Rustc { compiler, target });
569
570         // This uses a shared directory so that librustdoc documentation gets
571         // correctly built and merged with the rustc documentation. This is
572         // needed because rustdoc is built in a different directory from
573         // rustc. rustdoc needs to be able to see everything, for example when
574         // merging the search index, or generating local (relative) links.
575         let out_dir = builder.stage_out(compiler, Mode::Rustc).join(target.triple).join("doc");
576         t!(symlink_dir_force(&builder.config, &out, &out_dir));
577         // Cargo puts proc macros in `target/doc` even if you pass `--target`
578         // explicitly (https://github.com/rust-lang/cargo/issues/7677).
579         let proc_macro_out_dir = builder.stage_out(compiler, Mode::Rustc).join("doc");
580         t!(symlink_dir_force(&builder.config, &out, &proc_macro_out_dir));
581
582         // Build cargo command.
583         let mut cargo = builder.cargo(compiler, Mode::Rustc, SourceType::InTree, target, "doc");
584         cargo.rustdocflag("--document-private-items");
585         // Since we always pass --document-private-items, there's no need to warn about linking to private items.
586         cargo.rustdocflag("-Arustdoc::private-intra-doc-links");
587         cargo.rustdocflag("--enable-index-page");
588         cargo.rustdocflag("-Zunstable-options");
589         cargo.rustdocflag("-Znormalize-docs");
590         cargo.rustdocflag("--show-type-layout");
591         cargo.rustdocflag("--generate-link-to-definition");
592         compile::rustc_cargo(builder, &mut cargo, target);
593         cargo.arg("-Zunstable-options");
594         cargo.arg("-Zskip-rustdoc-fingerprint");
595
596         // Only include compiler crates, no dependencies of those, such as `libc`.
597         // Do link to dependencies on `docs.rs` however using `rustdoc-map`.
598         cargo.arg("--no-deps");
599         cargo.arg("-Zrustdoc-map");
600
601         // FIXME: `-Zrustdoc-map` does not yet correctly work for transitive dependencies,
602         // once this is no longer an issue the special case for `ena` can be removed.
603         cargo.rustdocflag("--extern-html-root-url");
604         cargo.rustdocflag("ena=https://docs.rs/ena/latest/");
605
606         let root_crates = if paths.is_empty() {
607             vec![
608                 INTERNER.intern_str("rustc_driver"),
609                 INTERNER.intern_str("rustc_codegen_llvm"),
610                 INTERNER.intern_str("rustc_codegen_ssa"),
611             ]
612         } else {
613             paths.into_iter().map(|p| builder.crate_paths[p]).collect()
614         };
615         // Find dependencies for top level crates.
616         let compiler_crates = root_crates.iter().flat_map(|krate| {
617             builder.in_tree_crates(krate, Some(target)).into_iter().map(|krate| krate.name)
618         });
619
620         let mut to_open = None;
621         for krate in compiler_crates {
622             // Create all crate output directories first to make sure rustdoc uses
623             // relative links.
624             // FIXME: Cargo should probably do this itself.
625             t!(fs::create_dir_all(out_dir.join(krate)));
626             cargo.arg("-p").arg(krate);
627             if to_open.is_none() {
628                 to_open = Some(krate);
629             }
630         }
631
632         builder.run(&mut cargo.into());
633         // Let's open the first crate documentation page:
634         if let Some(krate) = to_open {
635             let index = out.join(krate).join("index.html");
636             open(builder, &index);
637         }
638     }
639 }
640
641 macro_rules! tool_doc {
642     ($tool: ident, $should_run: literal, $path: literal, [$($krate: literal),+ $(,)?] $(,)?) => {
643         #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
644         pub struct $tool {
645             stage: u32,
646             target: TargetSelection,
647         }
648
649         impl Step for $tool {
650             type Output = ();
651             const DEFAULT: bool = true;
652             const ONLY_HOSTS: bool = true;
653
654             fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
655                 let builder = run.builder;
656                 run.krate($should_run).default_condition(builder.config.compiler_docs)
657             }
658
659             fn make_run(run: RunConfig<'_>) {
660                 run.builder.ensure($tool { stage: run.builder.top_stage, target: run.target });
661             }
662
663             /// Generates compiler documentation.
664             ///
665             /// This will generate all documentation for compiler and dependencies.
666             /// Compiler documentation is distributed separately, so we make sure
667             /// we do not merge it with the other documentation from std, test and
668             /// proc_macros. This is largely just a wrapper around `cargo doc`.
669             fn run(self, builder: &Builder<'_>) {
670                 let stage = self.stage;
671                 let target = self.target;
672                 builder.info(
673                     &format!(
674                         "Documenting stage{} {} ({})",
675                         stage,
676                         stringify!($tool).to_lowercase(),
677                         target,
678                     ),
679                 );
680
681                 // This is the intended out directory for compiler documentation.
682                 let out = builder.compiler_doc_out(target);
683                 t!(fs::create_dir_all(&out));
684
685                 let compiler = builder.compiler(stage, builder.config.build);
686
687                 // Build rustc docs so that we generate relative links.
688                 builder.ensure(Rustc { stage, target });
689
690                 // Symlink compiler docs to the output directory of rustdoc documentation.
691                 let out_dir = builder.stage_out(compiler, Mode::ToolRustc).join(target.triple).join("doc");
692                 t!(fs::create_dir_all(&out_dir));
693                 t!(symlink_dir_force(&builder.config, &out, &out_dir));
694
695                 // Build cargo command.
696                 let mut cargo = prepare_tool_cargo(
697                     builder,
698                     compiler,
699                     Mode::ToolRustc,
700                     target,
701                     "doc",
702                     $path,
703                     SourceType::InTree,
704                     &[],
705                 );
706
707                 cargo.arg("-Zskip-rustdoc-fingerprint");
708                 // Only include compiler crates, no dependencies of those, such as `libc`.
709                 cargo.arg("--no-deps");
710                 $(
711                     cargo.arg("-p").arg($krate);
712                 )+
713
714                 cargo.rustdocflag("--document-private-items");
715                 cargo.rustdocflag("--enable-index-page");
716                 cargo.rustdocflag("--show-type-layout");
717                 cargo.rustdocflag("--generate-link-to-definition");
718                 cargo.rustdocflag("-Zunstable-options");
719                 builder.run(&mut cargo.into());
720             }
721         }
722     }
723 }
724
725 tool_doc!(Rustdoc, "rustdoc-tool", "src/tools/rustdoc", ["rustdoc", "rustdoc-json-types"]);
726 tool_doc!(
727     Rustfmt,
728     "rustfmt-nightly",
729     "src/tools/rustfmt",
730     ["rustfmt-nightly", "rustfmt-config_proc_macro"],
731 );
732 tool_doc!(Clippy, "clippy", "src/tools/clippy", ["clippy_utils"]);
733
734 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
735 pub struct ErrorIndex {
736     pub target: TargetSelection,
737 }
738
739 impl Step for ErrorIndex {
740     type Output = ();
741     const DEFAULT: bool = true;
742     const ONLY_HOSTS: bool = true;
743
744     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
745         let builder = run.builder;
746         run.path("src/tools/error_index_generator").default_condition(builder.config.docs)
747     }
748
749     fn make_run(run: RunConfig<'_>) {
750         let target = run.target;
751         run.builder.ensure(ErrorIndex { target });
752     }
753
754     /// Generates the HTML rendered error-index by running the
755     /// `error_index_generator` tool.
756     fn run(self, builder: &Builder<'_>) {
757         builder.info(&format!("Documenting error index ({})", self.target));
758         let out = builder.doc_out(self.target);
759         t!(fs::create_dir_all(&out));
760         let mut index = tool::ErrorIndex::command(builder);
761         index.arg("html");
762         index.arg(out.join("error-index.html"));
763         index.arg(&builder.version);
764
765         builder.run(&mut index);
766     }
767 }
768
769 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
770 pub struct UnstableBookGen {
771     target: TargetSelection,
772 }
773
774 impl Step for UnstableBookGen {
775     type Output = ();
776     const DEFAULT: bool = true;
777     const ONLY_HOSTS: bool = true;
778
779     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
780         let builder = run.builder;
781         run.path("src/tools/unstable-book-gen").default_condition(builder.config.docs)
782     }
783
784     fn make_run(run: RunConfig<'_>) {
785         run.builder.ensure(UnstableBookGen { target: run.target });
786     }
787
788     fn run(self, builder: &Builder<'_>) {
789         let target = self.target;
790
791         builder.info(&format!("Generating unstable book md files ({})", target));
792         let out = builder.md_doc_out(target).join("unstable-book");
793         builder.create_dir(&out);
794         builder.remove_dir(&out);
795         let mut cmd = builder.tool_cmd(Tool::UnstableBookGen);
796         cmd.arg(builder.src.join("library"));
797         cmd.arg(builder.src.join("compiler"));
798         cmd.arg(builder.src.join("src"));
799         cmd.arg(out);
800
801         builder.run(&mut cmd);
802     }
803 }
804
805 fn symlink_dir_force(config: &Config, src: &Path, dst: &Path) -> io::Result<()> {
806     if config.dry_run {
807         return Ok(());
808     }
809     if let Ok(m) = fs::symlink_metadata(dst) {
810         if m.file_type().is_dir() {
811             fs::remove_dir_all(dst)?;
812         } else {
813             // handle directory junctions on windows by falling back to
814             // `remove_dir`.
815             fs::remove_file(dst).or_else(|_| fs::remove_dir(dst))?;
816         }
817     }
818
819     symlink_dir(config, src, dst)
820 }
821
822 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
823 pub struct RustcBook {
824     pub compiler: Compiler,
825     pub target: TargetSelection,
826     pub validate: bool,
827 }
828
829 impl Step for RustcBook {
830     type Output = ();
831     const DEFAULT: bool = true;
832     const ONLY_HOSTS: bool = true;
833
834     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
835         let builder = run.builder;
836         run.path("src/doc/rustc").default_condition(builder.config.docs)
837     }
838
839     fn make_run(run: RunConfig<'_>) {
840         run.builder.ensure(RustcBook {
841             compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
842             target: run.target,
843             validate: false,
844         });
845     }
846
847     /// Builds the rustc book.
848     ///
849     /// The lints are auto-generated by a tool, and then merged into the book
850     /// in the "md-doc" directory in the build output directory. Then
851     /// "rustbook" is used to convert it to HTML.
852     fn run(self, builder: &Builder<'_>) {
853         let out_base = builder.md_doc_out(self.target).join("rustc");
854         t!(fs::create_dir_all(&out_base));
855         let out_listing = out_base.join("src/lints");
856         builder.cp_r(&builder.src.join("src/doc/rustc"), &out_base);
857         builder.info(&format!("Generating lint docs ({})", self.target));
858
859         let rustc = builder.rustc(self.compiler);
860         // The tool runs `rustc` for extracting output examples, so it needs a
861         // functional sysroot.
862         builder.ensure(compile::Std { compiler: self.compiler, target: self.target });
863         let mut cmd = builder.tool_cmd(Tool::LintDocs);
864         cmd.arg("--src");
865         cmd.arg(builder.src.join("compiler"));
866         cmd.arg("--out");
867         cmd.arg(&out_listing);
868         cmd.arg("--rustc");
869         cmd.arg(&rustc);
870         cmd.arg("--rustc-target").arg(&self.target.rustc_target_arg());
871         if builder.config.verbose() {
872             cmd.arg("--verbose");
873         }
874         if self.validate {
875             cmd.arg("--validate");
876         }
877         // If the lib directories are in an unusual location (changed in
878         // config.toml), then this needs to explicitly update the dylib search
879         // path.
880         builder.add_rustc_lib_path(self.compiler, &mut cmd);
881         builder.run(&mut cmd);
882         // Run rustbook/mdbook to generate the HTML pages.
883         builder.ensure(RustbookSrc {
884             target: self.target,
885             name: INTERNER.intern_str("rustc"),
886             src: INTERNER.intern_path(out_base),
887         });
888         if builder.was_invoked_explicitly::<Self>(Kind::Doc) {
889             let out = builder.doc_out(self.target);
890             let index = out.join("rustc").join("index.html");
891             open(builder, &index);
892         }
893     }
894 }