]> git.proxmox.com Git - rustc.git/blob - src/bootstrap/doc.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.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 builder.run(&mut cmd);
274 }
275
276 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
277 pub struct Standalone {
278 compiler: Compiler,
279 target: TargetSelection,
280 }
281
282 impl Step for Standalone {
283 type Output = ();
284 const DEFAULT: bool = true;
285
286 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
287 let builder = run.builder;
288 run.path("src/doc").default_condition(builder.config.docs)
289 }
290
291 fn make_run(run: RunConfig<'_>) {
292 run.builder.ensure(Standalone {
293 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
294 target: run.target,
295 });
296 }
297
298 /// Generates all standalone documentation as compiled by the rustdoc in `stage`
299 /// for the `target` into `out`.
300 ///
301 /// This will list all of `src/doc` looking for markdown files and appropriately
302 /// perform transformations like substituting `VERSION`, `SHORT_HASH`, and
303 /// `STAMP` along with providing the various header/footer HTML we've customized.
304 ///
305 /// In the end, this is just a glorified wrapper around rustdoc!
306 fn run(self, builder: &Builder<'_>) {
307 let target = self.target;
308 let compiler = self.compiler;
309 builder.info(&format!("Documenting standalone ({})", target));
310 let out = builder.doc_out(target);
311 t!(fs::create_dir_all(&out));
312
313 let favicon = builder.src.join("src/doc/favicon.inc");
314 let footer = builder.src.join("src/doc/footer.inc");
315 let full_toc = builder.src.join("src/doc/full-toc.inc");
316 t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
317
318 let version_input = builder.src.join("src/doc/version_info.html.template");
319 let version_info = out.join("version_info.html");
320
321 if !builder.config.dry_run && !up_to_date(&version_input, &version_info) {
322 let info = t!(fs::read_to_string(&version_input))
323 .replace("VERSION", &builder.rust_release())
324 .replace("SHORT_HASH", builder.rust_info.sha_short().unwrap_or(""))
325 .replace("STAMP", builder.rust_info.sha().unwrap_or(""));
326 t!(fs::write(&version_info, &info));
327 }
328
329 for file in t!(fs::read_dir(builder.src.join("src/doc"))) {
330 let file = t!(file);
331 let path = file.path();
332 let filename = path.file_name().unwrap().to_str().unwrap();
333 if !filename.ends_with(".md") || filename == "README.md" {
334 continue;
335 }
336
337 let html = out.join(filename).with_extension("html");
338 let rustdoc = builder.rustdoc(compiler);
339 if up_to_date(&path, &html)
340 && up_to_date(&footer, &html)
341 && up_to_date(&favicon, &html)
342 && up_to_date(&full_toc, &html)
343 && (builder.config.dry_run || up_to_date(&version_info, &html))
344 && (builder.config.dry_run || up_to_date(&rustdoc, &html))
345 {
346 continue;
347 }
348
349 let mut cmd = builder.rustdoc_cmd(compiler);
350 // Needed for --index-page flag
351 cmd.arg("-Z").arg("unstable-options");
352
353 cmd.arg("--html-after-content")
354 .arg(&footer)
355 .arg("--html-before-content")
356 .arg(&version_info)
357 .arg("--html-in-header")
358 .arg(&favicon)
359 .arg("--markdown-no-toc")
360 .arg("--index-page")
361 .arg(&builder.src.join("src/doc/index.md"))
362 .arg("--markdown-playground-url")
363 .arg("https://play.rust-lang.org/")
364 .arg("-o")
365 .arg(&out)
366 .arg(&path);
367
368 if filename == "not_found.md" {
369 cmd.arg("--markdown-css").arg("https://doc.rust-lang.org/rust.css");
370 } else {
371 cmd.arg("--markdown-css").arg("rust.css");
372 }
373 builder.run(&mut cmd);
374 }
375
376 // We open doc/index.html as the default if invoked as `x.py doc --open`
377 // with no particular explicit doc requested (e.g. library/core).
378 if builder.paths.is_empty() || is_explicit_request(builder, "src/doc") {
379 let index = out.join("index.html");
380 open(builder, &index);
381 }
382 }
383 }
384
385 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
386 pub struct Std {
387 pub stage: u32,
388 pub target: TargetSelection,
389 }
390
391 impl Step for Std {
392 type Output = ();
393 const DEFAULT: bool = true;
394
395 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
396 let builder = run.builder;
397 run.all_krates("test").default_condition(builder.config.docs)
398 }
399
400 fn make_run(run: RunConfig<'_>) {
401 run.builder.ensure(Std { stage: run.builder.top_stage, target: run.target });
402 }
403
404 /// Compile all standard library documentation.
405 ///
406 /// This will generate all documentation for the standard library and its
407 /// dependencies. This is largely just a wrapper around `cargo doc`.
408 fn run(self, builder: &Builder<'_>) {
409 let stage = self.stage;
410 let target = self.target;
411 builder.info(&format!("Documenting stage{} std ({})", stage, target));
412 let out = builder.doc_out(target);
413 t!(fs::create_dir_all(&out));
414 let compiler = builder.compiler(stage, builder.config.build);
415
416 builder.ensure(compile::Std { compiler, target });
417 let out_dir = builder.stage_out(compiler, Mode::Std).join(target.triple).join("doc");
418
419 t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
420
421 let run_cargo_rustdoc_for = |package: &str| {
422 let mut cargo =
423 builder.cargo(compiler, Mode::Std, SourceType::InTree, target, "rustdoc");
424 compile::std_cargo(builder, target, compiler.stage, &mut cargo);
425
426 cargo
427 .arg("-p")
428 .arg(package)
429 .arg("--")
430 .arg("--markdown-css")
431 .arg("rust.css")
432 .arg("--markdown-no-toc")
433 .arg("-Z")
434 .arg("unstable-options")
435 .arg("--resource-suffix")
436 .arg(&builder.version)
437 .arg("--index-page")
438 .arg(&builder.src.join("src/doc/index.md"));
439
440 builder.run(&mut cargo.into());
441 };
442 // Only build the following crates. While we could just iterate over the
443 // folder structure, that would also build internal crates that we do
444 // not want to show in documentation. These crates will later be visited
445 // by the rustc step, so internal documentation will show them.
446 //
447 // Note that the order here is important! The crates need to be
448 // processed starting from the leaves, otherwise rustdoc will not
449 // create correct links between crates because rustdoc depends on the
450 // existence of the output directories to know if it should be a local
451 // or remote link.
452 let krates = ["core", "alloc", "std", "proc_macro", "test"];
453 for krate in &krates {
454 run_cargo_rustdoc_for(krate);
455 }
456 builder.cp_r(&out_dir, &out);
457
458 // Look for library/std, library/core etc in the `x.py doc` arguments and
459 // open the corresponding rendered docs.
460 for path in builder.paths.iter().map(components_simplified) {
461 if path.get(0) == Some(&"library") {
462 let requested_crate = &path[1];
463 if krates.contains(&requested_crate) {
464 let index = out.join(requested_crate).join("index.html");
465 open(builder, &index);
466 }
467 }
468 }
469 }
470 }
471
472 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
473 pub struct Rustc {
474 stage: u32,
475 target: TargetSelection,
476 }
477
478 impl Step for Rustc {
479 type Output = ();
480 const DEFAULT: bool = true;
481 const ONLY_HOSTS: bool = true;
482
483 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
484 let builder = run.builder;
485 run.krate("rustc-main").default_condition(builder.config.docs)
486 }
487
488 fn make_run(run: RunConfig<'_>) {
489 run.builder.ensure(Rustc { stage: run.builder.top_stage, target: run.target });
490 }
491
492 /// Generates compiler documentation.
493 ///
494 /// This will generate all documentation for compiler and dependencies.
495 /// Compiler documentation is distributed separately, so we make sure
496 /// we do not merge it with the other documentation from std, test and
497 /// proc_macros. This is largely just a wrapper around `cargo doc`.
498 fn run(self, builder: &Builder<'_>) {
499 let stage = self.stage;
500 let target = self.target;
501 builder.info(&format!("Documenting stage{} compiler ({})", stage, target));
502
503 // This is the intended out directory for compiler documentation.
504 let out = builder.compiler_doc_out(target);
505 t!(fs::create_dir_all(&out));
506
507 let compiler = builder.compiler(stage, builder.config.build);
508
509 if !builder.config.compiler_docs {
510 builder.info("\tskipping - compiler/librustdoc docs disabled");
511 return;
512 }
513
514 // Build rustc.
515 builder.ensure(compile::Rustc { compiler, target });
516
517 // This uses a shared directory so that librustdoc documentation gets
518 // correctly built and merged with the rustc documentation. This is
519 // needed because rustdoc is built in a different directory from
520 // rustc. rustdoc needs to be able to see everything, for example when
521 // merging the search index, or generating local (relative) links.
522 let out_dir = builder.stage_out(compiler, Mode::Rustc).join(target.triple).join("doc");
523 t!(symlink_dir_force(&builder.config, &out, &out_dir));
524
525 // Build cargo command.
526 let mut cargo = builder.cargo(compiler, Mode::Rustc, SourceType::InTree, target, "doc");
527 cargo.rustdocflag("--document-private-items");
528 cargo.rustdocflag("--enable-index-page");
529 cargo.rustdocflag("-Zunstable-options");
530 compile::rustc_cargo(builder, &mut cargo, target);
531
532 // Only include compiler crates, no dependencies of those, such as `libc`.
533 cargo.arg("--no-deps");
534
535 // Find dependencies for top level crates.
536 let mut compiler_crates = HashSet::new();
537 for root_crate in &["rustc_driver", "rustc_codegen_llvm", "rustc_codegen_ssa"] {
538 compiler_crates
539 .extend(builder.in_tree_crates(root_crate).into_iter().map(|krate| krate.name));
540 }
541
542 for krate in &compiler_crates {
543 // Create all crate output directories first to make sure rustdoc uses
544 // relative links.
545 // FIXME: Cargo should probably do this itself.
546 t!(fs::create_dir_all(out_dir.join(krate)));
547 cargo.arg("-p").arg(krate);
548 }
549
550 builder.run(&mut cargo.into());
551 }
552 }
553
554 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
555 pub struct Rustdoc {
556 stage: u32,
557 target: TargetSelection,
558 }
559
560 impl Step for Rustdoc {
561 type Output = ();
562 const DEFAULT: bool = true;
563 const ONLY_HOSTS: bool = true;
564
565 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
566 run.krate("rustdoc-tool")
567 }
568
569 fn make_run(run: RunConfig<'_>) {
570 run.builder.ensure(Rustdoc { stage: run.builder.top_stage, target: run.target });
571 }
572
573 /// Generates compiler documentation.
574 ///
575 /// This will generate all documentation for compiler and dependencies.
576 /// Compiler documentation is distributed separately, so we make sure
577 /// we do not merge it with the other documentation from std, test and
578 /// proc_macros. This is largely just a wrapper around `cargo doc`.
579 fn run(self, builder: &Builder<'_>) {
580 let stage = self.stage;
581 let target = self.target;
582 builder.info(&format!("Documenting stage{} rustdoc ({})", stage, target));
583
584 // This is the intended out directory for compiler documentation.
585 let out = builder.compiler_doc_out(target);
586 t!(fs::create_dir_all(&out));
587
588 let compiler = builder.compiler(stage, builder.config.build);
589
590 if !builder.config.compiler_docs {
591 builder.info("\tskipping - compiler/librustdoc docs disabled");
592 return;
593 }
594
595 // Build rustc docs so that we generate relative links.
596 builder.ensure(Rustc { stage, target });
597
598 // Build rustdoc.
599 builder.ensure(tool::Rustdoc { compiler });
600
601 // Symlink compiler docs to the output directory of rustdoc documentation.
602 let out_dir = builder.stage_out(compiler, Mode::ToolRustc).join(target.triple).join("doc");
603 t!(fs::create_dir_all(&out_dir));
604 t!(symlink_dir_force(&builder.config, &out, &out_dir));
605
606 // Build cargo command.
607 let mut cargo = prepare_tool_cargo(
608 builder,
609 compiler,
610 Mode::ToolRustc,
611 target,
612 "doc",
613 "src/tools/rustdoc",
614 SourceType::InTree,
615 &[],
616 );
617
618 // Only include compiler crates, no dependencies of those, such as `libc`.
619 cargo.arg("--no-deps");
620 cargo.arg("-p").arg("rustdoc");
621
622 cargo.rustdocflag("--document-private-items");
623 builder.run(&mut cargo.into());
624 }
625 }
626
627 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
628 pub struct ErrorIndex {
629 pub compiler: Compiler,
630 pub target: TargetSelection,
631 }
632
633 impl Step for ErrorIndex {
634 type Output = ();
635 const DEFAULT: bool = true;
636 const ONLY_HOSTS: bool = true;
637
638 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
639 let builder = run.builder;
640 run.path("src/tools/error_index_generator").default_condition(builder.config.docs)
641 }
642
643 fn make_run(run: RunConfig<'_>) {
644 let target = run.target;
645 // error_index_generator depends on librustdoc. Use the compiler that
646 // is normally used to build rustdoc for other documentation so that
647 // it shares the same artifacts.
648 let compiler =
649 run.builder.compiler_for(run.builder.top_stage, run.builder.config.build, target);
650 run.builder.ensure(ErrorIndex { compiler, target });
651 }
652
653 /// Generates the HTML rendered error-index by running the
654 /// `error_index_generator` tool.
655 fn run(self, builder: &Builder<'_>) {
656 builder.info(&format!("Documenting error index ({})", self.target));
657 let out = builder.doc_out(self.target);
658 t!(fs::create_dir_all(&out));
659 let mut index = tool::ErrorIndex::command(builder, self.compiler);
660 index.arg("html");
661 index.arg(out.join("error-index.html"));
662 index.arg(&builder.version);
663
664 builder.run(&mut index);
665 }
666 }
667
668 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
669 pub struct UnstableBookGen {
670 target: TargetSelection,
671 }
672
673 impl Step for UnstableBookGen {
674 type Output = ();
675 const DEFAULT: bool = true;
676 const ONLY_HOSTS: bool = true;
677
678 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
679 let builder = run.builder;
680 run.path("src/tools/unstable-book-gen").default_condition(builder.config.docs)
681 }
682
683 fn make_run(run: RunConfig<'_>) {
684 run.builder.ensure(UnstableBookGen { target: run.target });
685 }
686
687 fn run(self, builder: &Builder<'_>) {
688 let target = self.target;
689
690 builder.info(&format!("Generating unstable book md files ({})", target));
691 let out = builder.md_doc_out(target).join("unstable-book");
692 builder.create_dir(&out);
693 builder.remove_dir(&out);
694 let mut cmd = builder.tool_cmd(Tool::UnstableBookGen);
695 cmd.arg(builder.src.join("library"));
696 cmd.arg(builder.src.join("compiler"));
697 cmd.arg(builder.src.join("src"));
698 cmd.arg(out);
699
700 builder.run(&mut cmd);
701 }
702 }
703
704 fn symlink_dir_force(config: &Config, src: &Path, dst: &Path) -> io::Result<()> {
705 if config.dry_run {
706 return Ok(());
707 }
708 if let Ok(m) = fs::symlink_metadata(dst) {
709 if m.file_type().is_dir() {
710 fs::remove_dir_all(dst)?;
711 } else {
712 // handle directory junctions on windows by falling back to
713 // `remove_dir`.
714 fs::remove_file(dst).or_else(|_| fs::remove_dir(dst))?;
715 }
716 }
717
718 symlink_dir(config, src, dst)
719 }
720
721 #[derive(Ord, PartialOrd, Debug, Copy, Clone, Hash, PartialEq, Eq)]
722 pub struct RustcBook {
723 pub compiler: Compiler,
724 pub target: TargetSelection,
725 }
726
727 impl Step for RustcBook {
728 type Output = ();
729 const DEFAULT: bool = true;
730 const ONLY_HOSTS: bool = true;
731
732 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
733 let builder = run.builder;
734 run.path("src/doc/rustc").default_condition(builder.config.docs)
735 }
736
737 fn make_run(run: RunConfig<'_>) {
738 run.builder.ensure(RustcBook {
739 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
740 target: run.target,
741 });
742 }
743
744 /// Builds the rustc book.
745 ///
746 /// The lints are auto-generated by a tool, and then merged into the book
747 /// in the "md-doc" directory in the build output directory. Then
748 /// "rustbook" is used to convert it to HTML.
749 fn run(self, builder: &Builder<'_>) {
750 let out_base = builder.md_doc_out(self.target).join("rustc");
751 t!(fs::create_dir_all(&out_base));
752 let out_listing = out_base.join("src/lints");
753 builder.cp_r(&builder.src.join("src/doc/rustc"), &out_base);
754 builder.info(&format!("Generating lint docs ({})", self.target));
755
756 let rustc = builder.rustc(self.compiler);
757 // The tool runs `rustc` for extracting output examples, so it needs a
758 // functional sysroot.
759 builder.ensure(compile::Std { compiler: self.compiler, target: self.target });
760 let mut cmd = builder.tool_cmd(Tool::LintDocs);
761 cmd.arg("--src");
762 cmd.arg(builder.src.join("compiler"));
763 cmd.arg("--out");
764 cmd.arg(&out_listing);
765 cmd.arg("--rustc");
766 cmd.arg(&rustc);
767 cmd.arg("--rustc-target").arg(&self.target.rustc_target_arg());
768 if builder.config.verbose() {
769 cmd.arg("--verbose");
770 }
771 // If the lib directories are in an unusual location (changed in
772 // config.toml), then this needs to explicitly update the dylib search
773 // path.
774 builder.add_rustc_lib_path(self.compiler, &mut cmd);
775 builder.run(&mut cmd);
776 // Run rustbook/mdbook to generate the HTML pages.
777 builder.ensure(RustbookSrc {
778 target: self.target,
779 name: INTERNER.intern_str("rustc"),
780 src: INTERNER.intern_path(out_base),
781 });
782 if is_explicit_request(builder, "src/doc/rustc") {
783 let out = builder.doc_out(self.target);
784 let index = out.join("rustc").join("index.html");
785 open(builder, &index);
786 }
787 }
788 }