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