1use std::io::{self, Write};
11use std::path::{Path, PathBuf};
12use std::{env, fs, mem};
13
14use crate::core::build_steps::compile;
15use crate::core::build_steps::tool::{self, SourceType, Tool, prepare_tool_cargo};
16use crate::core::builder::{
17 self, Alias, Builder, Compiler, Kind, RunConfig, ShouldRun, Step, crate_description,
18};
19use crate::core::config::{Config, TargetSelection};
20use crate::helpers::{submodule_path_of, symlink_dir, t, up_to_date};
21use crate::{FileType, Mode};
22
23macro_rules! book {
24 ($($name:ident, $path:expr, $book_name:expr, $lang:expr ;)+) => {
25 $(
26 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
27 pub struct $name {
28 target: TargetSelection,
29 }
30
31 impl Step for $name {
32 type Output = ();
33 const DEFAULT: bool = true;
34
35 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
36 let builder = run.builder;
37 run.path($path).default_condition(builder.config.docs)
38 }
39
40 fn make_run(run: RunConfig<'_>) {
41 run.builder.ensure($name {
42 target: run.target,
43 });
44 }
45
46 fn run(self, builder: &Builder<'_>) {
47 if let Some(submodule_path) = submodule_path_of(&builder, $path) {
48 builder.require_submodule(&submodule_path, None)
49 }
50
51 builder.ensure(RustbookSrc {
52 target: self.target,
53 name: $book_name.to_owned(),
54 src: builder.src.join($path),
55 parent: Some(self),
56 languages: $lang.into(),
57 rustdoc_compiler: None,
58 })
59 }
60 }
61 )+
62 }
63}
64
65book!(
69 CargoBook, "src/tools/cargo/src/doc", "cargo", &[];
70 ClippyBook, "src/tools/clippy/book", "clippy", &[];
71 EditionGuide, "src/doc/edition-guide", "edition-guide", &[];
72 EmbeddedBook, "src/doc/embedded-book", "embedded-book", &[];
73 Nomicon, "src/doc/nomicon", "nomicon", &[];
74 RustByExample, "src/doc/rust-by-example", "rust-by-example", &["ja", "zh"];
75 RustdocBook, "src/doc/rustdoc", "rustdoc", &[];
76 StyleGuide, "src/doc/style-guide", "style-guide", &[];
77);
78
79#[derive(Debug, Clone, Hash, PartialEq, Eq)]
80pub struct UnstableBook {
81 target: TargetSelection,
82}
83
84impl Step for UnstableBook {
85 type Output = ();
86 const DEFAULT: bool = true;
87
88 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
89 let builder = run.builder;
90 run.path("src/doc/unstable-book").default_condition(builder.config.docs)
91 }
92
93 fn make_run(run: RunConfig<'_>) {
94 run.builder.ensure(UnstableBook { target: run.target });
95 }
96
97 fn run(self, builder: &Builder<'_>) {
98 builder.ensure(UnstableBookGen { target: self.target });
99 builder.ensure(RustbookSrc {
100 target: self.target,
101 name: "unstable-book".to_owned(),
102 src: builder.md_doc_out(self.target).join("unstable-book"),
103 parent: Some(self),
104 languages: vec![],
105 rustdoc_compiler: None,
106 })
107 }
108}
109
110#[derive(Debug, Clone, Hash, PartialEq, Eq)]
111struct RustbookSrc<P: Step> {
112 target: TargetSelection,
113 name: String,
114 src: PathBuf,
115 parent: Option<P>,
116 languages: Vec<&'static str>,
117 rustdoc_compiler: Option<Compiler>,
118}
119
120impl<P: Step> Step for RustbookSrc<P> {
121 type Output = ();
122
123 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
124 run.never()
125 }
126
127 fn run(self, builder: &Builder<'_>) {
132 let target = self.target;
133 let name = self.name;
134 let src = self.src;
135 let out = builder.doc_out(target);
136 t!(fs::create_dir_all(&out));
137
138 let out = out.join(&name);
139 let index = out.join("index.html");
140 let rustbook = builder.tool_exe(Tool::Rustbook);
141
142 if !builder.config.dry_run()
143 && (!up_to_date(&src, &index) || !up_to_date(&rustbook, &index))
144 {
145 builder.info(&format!("Rustbook ({target}) - {name}"));
146 let _ = fs::remove_dir_all(&out);
147
148 let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
149
150 if let Some(compiler) = self.rustdoc_compiler {
151 let mut rustdoc = builder.rustdoc(compiler);
152 rustdoc.pop();
153 let old_path = env::var_os("PATH").unwrap_or_default();
154 let new_path =
155 env::join_paths(std::iter::once(rustdoc).chain(env::split_paths(&old_path)))
156 .expect("could not add rustdoc to PATH");
157
158 rustbook_cmd.env("PATH", new_path);
159 builder.add_rustc_lib_path(compiler, &mut rustbook_cmd);
160 }
161
162 rustbook_cmd
163 .arg("build")
164 .arg(&src)
165 .arg("-d")
166 .arg(&out)
167 .arg("--rust-root")
168 .arg(&builder.src)
169 .run(builder);
170
171 for lang in &self.languages {
172 let out = out.join(lang);
173
174 builder.info(&format!("Rustbook ({target}) - {name} - {lang}"));
175 let _ = fs::remove_dir_all(&out);
176
177 builder
178 .tool_cmd(Tool::Rustbook)
179 .arg("build")
180 .arg(&src)
181 .arg("-d")
182 .arg(&out)
183 .arg("-l")
184 .arg(lang)
185 .run(builder);
186 }
187 }
188
189 if self.parent.is_some() {
190 builder.maybe_open_in_browser::<P>(index)
191 }
192 }
193}
194
195#[derive(Debug, Clone, Hash, PartialEq, Eq)]
196pub struct TheBook {
197 compiler: Compiler,
198 target: TargetSelection,
199}
200
201impl Step for TheBook {
202 type Output = ();
203 const DEFAULT: bool = true;
204
205 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
206 let builder = run.builder;
207 run.path("src/doc/book").default_condition(builder.config.docs)
208 }
209
210 fn make_run(run: RunConfig<'_>) {
211 run.builder.ensure(TheBook {
212 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.host_target),
213 target: run.target,
214 });
215 }
216
217 fn run(self, builder: &Builder<'_>) {
227 builder.require_submodule("src/doc/book", None);
228
229 let compiler = self.compiler;
230 let target = self.target;
231
232 let absolute_path = builder.src.join("src/doc/book");
233 let redirect_path = absolute_path.join("redirects");
234
235 builder.ensure(RustbookSrc {
237 target,
238 name: "book".to_owned(),
239 src: absolute_path.clone(),
240 parent: Some(self),
241 languages: vec![],
242 rustdoc_compiler: None,
243 });
244
245 for edition in &["first-edition", "second-edition", "2018-edition"] {
247 builder.ensure(RustbookSrc {
248 target,
249 name: format!("book/{edition}"),
250 src: absolute_path.join(edition),
251 parent: Option::<Self>::None,
254 languages: vec![],
255 rustdoc_compiler: None,
256 });
257 }
258
259 let shared_assets = builder.ensure(SharedAssets { target });
261
262 let _guard = builder.msg_doc(compiler, "book redirect pages", target);
264 for file in t!(fs::read_dir(redirect_path)) {
265 let file = t!(file);
266 let path = file.path();
267 let path = path.to_str().unwrap();
268
269 invoke_rustdoc(builder, compiler, &shared_assets, target, path);
270 }
271 }
272}
273
274fn invoke_rustdoc(
275 builder: &Builder<'_>,
276 compiler: Compiler,
277 shared_assets: &SharedAssetsPaths,
278 target: TargetSelection,
279 markdown: &str,
280) {
281 let out = builder.doc_out(target);
282
283 let path = builder.src.join("src/doc").join(markdown);
284
285 let header = builder.src.join("src/doc/redirect.inc");
286 let footer = builder.src.join("src/doc/footer.inc");
287
288 let mut cmd = builder.rustdoc_cmd(compiler);
289
290 let out = out.join("book");
291
292 cmd.arg("--html-after-content")
293 .arg(&footer)
294 .arg("--html-before-content")
295 .arg(&shared_assets.version_info)
296 .arg("--html-in-header")
297 .arg(&header)
298 .arg("--markdown-no-toc")
299 .arg("--markdown-playground-url")
300 .arg("https://play.rust-lang.org/")
301 .arg("-o")
302 .arg(&out)
303 .arg(&path)
304 .arg("--markdown-css")
305 .arg("../rust.css")
306 .arg("-Zunstable-options");
307
308 if !builder.config.docs_minification {
309 cmd.arg("--disable-minification");
310 }
311
312 cmd.run(builder);
313}
314
315#[derive(Debug, Clone, Hash, PartialEq, Eq)]
316pub struct Standalone {
317 compiler: Compiler,
318 target: TargetSelection,
319}
320
321impl Step for Standalone {
322 type Output = ();
323 const DEFAULT: bool = true;
324
325 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
326 let builder = run.builder;
327 run.path("src/doc").alias("standalone").default_condition(builder.config.docs)
328 }
329
330 fn make_run(run: RunConfig<'_>) {
331 run.builder.ensure(Standalone {
332 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.host_target),
333 target: run.target,
334 });
335 }
336
337 fn run(self, builder: &Builder<'_>) {
346 let target = self.target;
347 let compiler = self.compiler;
348 let _guard = builder.msg_doc(compiler, "standalone", target);
349 let out = builder.doc_out(target);
350 t!(fs::create_dir_all(&out));
351
352 let version_info = builder.ensure(SharedAssets { target: self.target }).version_info;
353
354 let favicon = builder.src.join("src/doc/favicon.inc");
355 let footer = builder.src.join("src/doc/footer.inc");
356 let full_toc = builder.src.join("src/doc/full-toc.inc");
357
358 for file in t!(fs::read_dir(builder.src.join("src/doc"))) {
359 let file = t!(file);
360 let path = file.path();
361 let filename = path.file_name().unwrap().to_str().unwrap();
362 if !filename.ends_with(".md") || filename == "README.md" {
363 continue;
364 }
365
366 let html = out.join(filename).with_extension("html");
367 let rustdoc = builder.rustdoc(compiler);
368 if up_to_date(&path, &html)
369 && up_to_date(&footer, &html)
370 && up_to_date(&favicon, &html)
371 && up_to_date(&full_toc, &html)
372 && (builder.config.dry_run() || up_to_date(&version_info, &html))
373 && (builder.config.dry_run() || up_to_date(&rustdoc, &html))
374 {
375 continue;
376 }
377
378 let mut cmd = builder.rustdoc_cmd(compiler);
379
380 cmd.arg("--html-after-content")
381 .arg(&footer)
382 .arg("--html-before-content")
383 .arg(&version_info)
384 .arg("--html-in-header")
385 .arg(&favicon)
386 .arg("--markdown-no-toc")
387 .arg("-Zunstable-options")
388 .arg("--index-page")
389 .arg(builder.src.join("src/doc/index.md"))
390 .arg("--markdown-playground-url")
391 .arg("https://play.rust-lang.org/")
392 .arg("-o")
393 .arg(&out)
394 .arg(&path);
395
396 if !builder.config.docs_minification {
397 cmd.arg("--disable-minification");
398 }
399
400 if filename == "not_found.md" {
401 cmd.arg("--markdown-css").arg("https://doc.rust-lang.org/rust.css");
402 } else {
403 cmd.arg("--markdown-css").arg("rust.css");
404 }
405 cmd.run(builder);
406 }
407
408 if builder.paths.is_empty() || builder.was_invoked_explicitly::<Self>(Kind::Doc) {
411 let index = out.join("index.html");
412 builder.open_in_browser(index);
413 }
414 }
415}
416
417#[derive(Debug, Clone, Hash, PartialEq, Eq)]
418pub struct Releases {
419 compiler: Compiler,
420 target: TargetSelection,
421}
422
423impl Step for Releases {
424 type Output = ();
425 const DEFAULT: bool = true;
426
427 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
428 let builder = run.builder;
429 run.path("RELEASES.md").alias("releases").default_condition(builder.config.docs)
430 }
431
432 fn make_run(run: RunConfig<'_>) {
433 run.builder.ensure(Releases {
434 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.host_target),
435 target: run.target,
436 });
437 }
438
439 fn run(self, builder: &Builder<'_>) {
445 let target = self.target;
446 let compiler = self.compiler;
447 let _guard = builder.msg_doc(compiler, "releases", target);
448 let out = builder.doc_out(target);
449 t!(fs::create_dir_all(&out));
450
451 builder.ensure(Standalone {
452 compiler: builder.compiler(builder.top_stage, builder.config.host_target),
453 target,
454 });
455
456 let version_info = builder.ensure(SharedAssets { target: self.target }).version_info;
457
458 let favicon = builder.src.join("src/doc/favicon.inc");
459 let footer = builder.src.join("src/doc/footer.inc");
460 let full_toc = builder.src.join("src/doc/full-toc.inc");
461
462 let html = out.join("releases.html");
463 let tmppath = out.join("releases.md");
464 let inpath = builder.src.join("RELEASES.md");
465 let rustdoc = builder.rustdoc(compiler);
466 if !up_to_date(&inpath, &html)
467 || !up_to_date(&footer, &html)
468 || !up_to_date(&favicon, &html)
469 || !up_to_date(&full_toc, &html)
470 || !(builder.config.dry_run()
471 || up_to_date(&version_info, &html)
472 || up_to_date(&rustdoc, &html))
473 {
474 let mut tmpfile = t!(fs::File::create(&tmppath));
475 t!(tmpfile.write_all(b"% Rust Release Notes\n\n"));
476 t!(io::copy(&mut t!(fs::File::open(&inpath)), &mut tmpfile));
477 mem::drop(tmpfile);
478 let mut cmd = builder.rustdoc_cmd(compiler);
479
480 cmd.arg("--html-after-content")
481 .arg(&footer)
482 .arg("--html-before-content")
483 .arg(&version_info)
484 .arg("--html-in-header")
485 .arg(&favicon)
486 .arg("--markdown-no-toc")
487 .arg("--markdown-css")
488 .arg("rust.css")
489 .arg("-Zunstable-options")
490 .arg("--index-page")
491 .arg(builder.src.join("src/doc/index.md"))
492 .arg("--markdown-playground-url")
493 .arg("https://play.rust-lang.org/")
494 .arg("-o")
495 .arg(&out)
496 .arg(&tmppath);
497
498 if !builder.config.docs_minification {
499 cmd.arg("--disable-minification");
500 }
501
502 cmd.run(builder);
503 }
504
505 if builder.was_invoked_explicitly::<Self>(Kind::Doc) {
508 builder.open_in_browser(&html);
509 }
510 }
511}
512
513#[derive(Debug, Clone)]
514pub struct SharedAssetsPaths {
515 pub version_info: PathBuf,
516}
517
518#[derive(Debug, Clone, Hash, PartialEq, Eq)]
519pub struct SharedAssets {
520 target: TargetSelection,
521}
522
523impl Step for SharedAssets {
524 type Output = SharedAssetsPaths;
525 const DEFAULT: bool = false;
526
527 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
528 run.never()
530 }
531
532 fn run(self, builder: &Builder<'_>) -> Self::Output {
534 let out = builder.doc_out(self.target);
535
536 let version_input = builder.src.join("src").join("doc").join("version_info.html.template");
537 let version_info = out.join("version_info.html");
538 if !builder.config.dry_run() && !up_to_date(&version_input, &version_info) {
539 let info = t!(fs::read_to_string(&version_input))
540 .replace("VERSION", &builder.rust_release())
541 .replace("SHORT_HASH", builder.rust_info().sha_short().unwrap_or(""))
542 .replace("STAMP", builder.rust_info().sha().unwrap_or(""));
543 t!(fs::write(&version_info, info));
544 }
545
546 builder.copy_link(
547 &builder.src.join("src").join("doc").join("rust.css"),
548 &out.join("rust.css"),
549 FileType::Regular,
550 );
551
552 SharedAssetsPaths { version_info }
553 }
554}
555
556#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
557pub struct Std {
558 pub stage: u32,
559 pub target: TargetSelection,
560 pub format: DocumentationFormat,
561 crates: Vec<String>,
562}
563
564impl Std {
565 pub(crate) fn new(stage: u32, target: TargetSelection, format: DocumentationFormat) -> Self {
566 Std { stage, target, format, crates: vec![] }
567 }
568}
569
570impl Step for Std {
571 type Output = ();
572 const DEFAULT: bool = true;
573
574 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
575 let builder = run.builder;
576 run.crate_or_deps("sysroot").path("library").default_condition(builder.config.docs)
577 }
578
579 fn make_run(run: RunConfig<'_>) {
580 let crates = compile::std_crates_for_run_make(&run);
581 let target_is_no_std = run.builder.no_std(run.target).unwrap_or(false);
582 if crates.is_empty() && target_is_no_std {
583 return;
584 }
585 run.builder.ensure(Std {
586 stage: run.builder.top_stage,
587 target: run.target,
588 format: if run.builder.config.cmd.json() {
589 DocumentationFormat::Json
590 } else {
591 DocumentationFormat::Html
592 },
593 crates,
594 });
595 }
596
597 fn run(self, builder: &Builder<'_>) {
602 let stage = self.stage;
603 let target = self.target;
604 let crates = if self.crates.is_empty() {
605 builder
606 .in_tree_crates("sysroot", Some(target))
607 .iter()
608 .map(|c| c.name.to_string())
609 .collect()
610 } else {
611 self.crates
612 };
613
614 let out = match self.format {
615 DocumentationFormat::Html => builder.doc_out(target),
616 DocumentationFormat::Json => builder.json_doc_out(target),
617 };
618
619 t!(fs::create_dir_all(&out));
620
621 if self.format == DocumentationFormat::Html {
622 builder.ensure(SharedAssets { target: self.target });
623 }
624
625 let index_page = builder
626 .src
627 .join("src/doc/index.md")
628 .into_os_string()
629 .into_string()
630 .expect("non-utf8 paths are unsupported");
631 let mut extra_args = match self.format {
632 DocumentationFormat::Html => {
633 vec!["--markdown-css", "rust.css", "--markdown-no-toc", "--index-page", &index_page]
634 }
635 DocumentationFormat::Json => vec!["--output-format", "json"],
636 };
637
638 if !builder.config.docs_minification {
639 extra_args.push("--disable-minification");
640 }
641 extra_args.push("-Zunstable-options");
643
644 doc_std(builder, self.format, stage, target, &out, &extra_args, &crates);
645
646 if let DocumentationFormat::Json = self.format {
648 return;
649 }
650
651 if builder.paths.iter().any(|path| path.ends_with("library")) {
652 let index = out.join("std").join("index.html");
654 builder.open_in_browser(index);
655 } else {
656 for requested_crate in crates {
657 if STD_PUBLIC_CRATES.iter().any(|&k| k == requested_crate) {
658 let index = out.join(requested_crate).join("index.html");
659 builder.open_in_browser(index);
660 break;
661 }
662 }
663 }
664 }
665}
666
667const STD_PUBLIC_CRATES: [&str; 5] = ["core", "alloc", "std", "proc_macro", "test"];
677
678#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
679pub enum DocumentationFormat {
680 Html,
681 Json,
682}
683
684impl DocumentationFormat {
685 fn as_str(&self) -> &str {
686 match self {
687 DocumentationFormat::Html => "HTML",
688 DocumentationFormat::Json => "JSON",
689 }
690 }
691}
692
693fn doc_std(
695 builder: &Builder<'_>,
696 format: DocumentationFormat,
697 stage: u32,
698 target: TargetSelection,
699 out: &Path,
700 extra_args: &[&str],
701 requested_crates: &[String],
702) {
703 let compiler = builder.compiler(stage, builder.config.host_target);
704
705 let target_doc_dir_name = if format == DocumentationFormat::Json { "json-doc" } else { "doc" };
706 let target_dir = builder.stage_out(compiler, Mode::Std).join(target).join(target_doc_dir_name);
707
708 let out_dir = target_dir.join(target).join("doc");
712
713 let mut cargo =
714 builder::Cargo::new(builder, compiler, Mode::Std, SourceType::InTree, target, Kind::Doc);
715
716 compile::std_cargo(builder, target, compiler.stage, &mut cargo);
717 cargo
718 .arg("--no-deps")
719 .arg("--target-dir")
720 .arg(&*target_dir.to_string_lossy())
721 .arg("-Zskip-rustdoc-fingerprint")
722 .arg("-Zrustdoc-map")
723 .rustdocflag("--extern-html-root-url")
724 .rustdocflag("std_detect=https://docs.rs/std_detect/latest/")
725 .rustdocflag("--extern-html-root-takes-precedence")
726 .rustdocflag("--resource-suffix")
727 .rustdocflag(&builder.version);
728 for arg in extra_args {
729 cargo.rustdocflag(arg);
730 }
731
732 if builder.config.library_docs_private_items {
733 cargo.rustdocflag("--document-private-items").rustdocflag("--document-hidden-items");
734 }
735
736 for krate in requested_crates {
737 cargo.arg("-p").arg(krate);
738 }
739
740 let description =
741 format!("library{} in {} format", crate_description(requested_crates), format.as_str());
742 let _guard = builder.msg_doc(compiler, description, target);
743
744 cargo.into_cmd().run(builder);
745 builder.cp_link_r(&out_dir, out);
746}
747
748#[derive(Debug, Clone, Hash, PartialEq, Eq)]
749pub struct Rustc {
750 pub stage: u32,
751 pub target: TargetSelection,
752 crates: Vec<String>,
753}
754
755impl Rustc {
756 pub(crate) fn new(stage: u32, target: TargetSelection, builder: &Builder<'_>) -> Self {
757 let crates = builder
758 .in_tree_crates("rustc-main", Some(target))
759 .into_iter()
760 .map(|krate| krate.name.to_string())
761 .collect();
762 Self { stage, target, crates }
763 }
764}
765
766impl Step for Rustc {
767 type Output = ();
768 const DEFAULT: bool = true;
769 const ONLY_HOSTS: bool = true;
770
771 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
772 let builder = run.builder;
773 run.crate_or_deps("rustc-main")
774 .path("compiler")
775 .default_condition(builder.config.compiler_docs)
776 }
777
778 fn make_run(run: RunConfig<'_>) {
779 run.builder.ensure(Rustc {
780 stage: run.builder.top_stage,
781 target: run.target,
782 crates: run.make_run_crates(Alias::Compiler),
783 });
784 }
785
786 fn run(self, builder: &Builder<'_>) {
793 let stage = self.stage;
794 let target = self.target;
795
796 let out = builder.compiler_doc_out(target);
798 t!(fs::create_dir_all(&out));
799
800 let compiler = builder.compiler(stage, builder.config.host_target);
803 builder.ensure(compile::Std::new(compiler, builder.config.host_target));
804
805 let _guard = builder.msg_sysroot_tool(
806 Kind::Doc,
807 stage,
808 format!("compiler{}", crate_description(&self.crates)),
809 compiler.host,
810 target,
811 );
812
813 let mut cargo = builder::Cargo::new(
815 builder,
816 compiler,
817 Mode::Rustc,
818 SourceType::InTree,
819 target,
820 Kind::Doc,
821 );
822
823 cargo.rustdocflag("--document-private-items");
824 cargo.rustdocflag("-Arustdoc::private-intra-doc-links");
826 cargo.rustdocflag("--enable-index-page");
827 cargo.rustdocflag("-Znormalize-docs");
828 cargo.rustdocflag("--show-type-layout");
829 cargo.rustdocflag("--generate-link-to-definition");
833
834 compile::rustc_cargo(builder, &mut cargo, target, &compiler, &self.crates);
835 cargo.arg("-Zskip-rustdoc-fingerprint");
836
837 cargo.arg("--no-deps");
840 cargo.arg("-Zrustdoc-map");
841
842 cargo.rustdocflag("--extern-html-root-url");
845 cargo.rustdocflag("ena=https://docs.rs/ena/latest/");
846
847 let mut to_open = None;
848
849 let out_dir = builder.stage_out(compiler, Mode::Rustc).join(target).join("doc");
850 for krate in &*self.crates {
851 let dir_name = krate.replace('-', "_");
855 t!(fs::create_dir_all(out_dir.join(&*dir_name)));
856 cargo.arg("-p").arg(krate);
857 if to_open.is_none() {
858 to_open = Some(dir_name);
859 }
860 }
861
862 symlink_dir_force(&builder.config, &out, &out_dir);
869 let proc_macro_out_dir = builder.stage_out(compiler, Mode::Rustc).join("doc");
872 symlink_dir_force(&builder.config, &out, &proc_macro_out_dir);
873
874 cargo.into_cmd().run(builder);
875
876 if !builder.config.dry_run() {
877 for krate in &*self.crates {
879 let dir_name = krate.replace('-', "_");
880 assert!(out.join(&*dir_name).read_dir().unwrap().next().is_some());
882 }
883 }
884
885 if builder.paths.iter().any(|path| path.ends_with("compiler")) {
886 let index = out.join("rustc_middle").join("index.html");
888 builder.open_in_browser(index);
889 } else if let Some(krate) = to_open {
890 let index = out.join(krate).join("index.html");
892 builder.open_in_browser(index);
893 }
894 }
895}
896
897macro_rules! tool_doc {
898 (
899 $tool: ident,
900 $path: literal,
901 $(rustc_tool = $rustc_tool:literal, )?
902 $(is_library = $is_library:expr,)?
903 $(crates = $crates:expr)?
904 ) => {
905 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
906 pub struct $tool {
907 target: TargetSelection,
908 }
909
910 impl Step for $tool {
911 type Output = ();
912 const DEFAULT: bool = true;
913 const ONLY_HOSTS: bool = true;
914
915 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
916 let builder = run.builder;
917 run.path($path).default_condition(builder.config.compiler_docs)
918 }
919
920 fn make_run(run: RunConfig<'_>) {
921 run.builder.ensure($tool { target: run.target });
922 }
923
924 fn run(self, builder: &Builder<'_>) {
931 let mut source_type = SourceType::InTree;
932
933 if let Some(submodule_path) = submodule_path_of(&builder, $path) {
934 source_type = SourceType::Submodule;
935 builder.require_submodule(&submodule_path, None);
936 }
937
938 let stage = builder.top_stage;
939 let target = self.target;
940
941 let out = builder.compiler_doc_out(target);
943 t!(fs::create_dir_all(&out));
944
945 let compiler = builder.compiler(stage, builder.config.host_target);
946 builder.ensure(compile::Std::new(compiler, target));
947
948 if true $(&& $rustc_tool)? {
949 builder.ensure(Rustc::new(stage, target, builder));
951
952 builder.ensure(compile::Rustc::new(compiler, target));
956 }
957
958 let mut cargo = prepare_tool_cargo(
960 builder,
961 compiler,
962 Mode::ToolRustc,
963 target,
964 Kind::Doc,
965 $path,
966 source_type,
967 &[],
968 );
969
970 cargo.arg("-Zskip-rustdoc-fingerprint");
971 cargo.arg("--no-deps");
973
974 if false $(|| $is_library)? {
975 cargo.arg("--lib");
976 }
977
978 $(for krate in $crates {
979 cargo.arg("-p").arg(krate);
980 })?
981
982 cargo.rustdocflag("--document-private-items");
983 cargo.rustdocflag("-Arustdoc::private-intra-doc-links");
985 cargo.rustdocflag("--enable-index-page");
986 cargo.rustdocflag("--show-type-layout");
987 cargo.rustdocflag("--generate-link-to-definition");
988
989 let out_dir = builder.stage_out(compiler, Mode::ToolRustc).join(target).join("doc");
990 $(for krate in $crates {
991 let dir_name = krate.replace("-", "_");
992 t!(fs::create_dir_all(out_dir.join(&*dir_name)));
993 })?
994
995 symlink_dir_force(&builder.config, &out, &out_dir);
997 let proc_macro_out_dir = builder.stage_out(compiler, Mode::ToolRustc).join("doc");
998 symlink_dir_force(&builder.config, &out, &proc_macro_out_dir);
999
1000 let _guard = builder.msg_doc(compiler, stringify!($tool).to_lowercase(), target);
1001 cargo.into_cmd().run(builder);
1002
1003 if !builder.config.dry_run() {
1004 $(for krate in $crates {
1006 let dir_name = krate.replace("-", "_");
1007 assert!(out.join(&*dir_name).read_dir().unwrap().next().is_some());
1009 })?
1010 }
1011 }
1012 }
1013 }
1014}
1015
1016tool_doc!(
1018 BuildHelper,
1019 "src/build_helper",
1020 rustc_tool = false,
1021 is_library = true,
1022 crates = ["build_helper"]
1023);
1024tool_doc!(Rustdoc, "src/tools/rustdoc", crates = ["rustdoc", "rustdoc-json-types"]);
1025tool_doc!(Rustfmt, "src/tools/rustfmt", crates = ["rustfmt-nightly", "rustfmt-config_proc_macro"]);
1026tool_doc!(Clippy, "src/tools/clippy", crates = ["clippy_config", "clippy_utils"]);
1027tool_doc!(Miri, "src/tools/miri", crates = ["miri"]);
1028tool_doc!(
1029 Cargo,
1030 "src/tools/cargo",
1031 rustc_tool = false,
1032 crates = [
1033 "cargo",
1034 "cargo-credential",
1035 "cargo-platform",
1036 "cargo-test-macro",
1037 "cargo-test-support",
1038 "cargo-util",
1039 "cargo-util-schemas",
1040 "crates-io",
1041 "mdman",
1042 "rustfix",
1043 ]
1044);
1045tool_doc!(Tidy, "src/tools/tidy", rustc_tool = false, crates = ["tidy"]);
1046tool_doc!(
1047 Bootstrap,
1048 "src/bootstrap",
1049 rustc_tool = false,
1050 is_library = true,
1051 crates = ["bootstrap"]
1052);
1053tool_doc!(
1054 RunMakeSupport,
1055 "src/tools/run-make-support",
1056 rustc_tool = false,
1057 is_library = true,
1058 crates = ["run_make_support"]
1059);
1060tool_doc!(
1061 Compiletest,
1062 "src/tools/compiletest",
1063 rustc_tool = false,
1064 is_library = true,
1065 crates = ["compiletest"]
1066);
1067
1068#[derive(Ord, PartialOrd, Debug, Clone, Hash, PartialEq, Eq)]
1069pub struct ErrorIndex {
1070 pub target: TargetSelection,
1071}
1072
1073impl Step for ErrorIndex {
1074 type Output = ();
1075 const DEFAULT: bool = true;
1076 const ONLY_HOSTS: bool = true;
1077
1078 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1079 let builder = run.builder;
1080 run.path("src/tools/error_index_generator").default_condition(builder.config.docs)
1081 }
1082
1083 fn make_run(run: RunConfig<'_>) {
1084 let target = run.target;
1085 run.builder.ensure(ErrorIndex { target });
1086 }
1087
1088 fn run(self, builder: &Builder<'_>) {
1091 builder.info(&format!("Documenting error index ({})", self.target));
1092 let out = builder.doc_out(self.target);
1093 t!(fs::create_dir_all(&out));
1094 tool::ErrorIndex::command(builder).arg("html").arg(out).arg(&builder.version).run(builder);
1095 }
1096}
1097
1098#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1099pub struct UnstableBookGen {
1100 target: TargetSelection,
1101}
1102
1103impl Step for UnstableBookGen {
1104 type Output = ();
1105 const DEFAULT: bool = true;
1106 const ONLY_HOSTS: bool = true;
1107
1108 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1109 let builder = run.builder;
1110 run.path("src/tools/unstable-book-gen").default_condition(builder.config.docs)
1111 }
1112
1113 fn make_run(run: RunConfig<'_>) {
1114 run.builder.ensure(UnstableBookGen { target: run.target });
1115 }
1116
1117 fn run(self, builder: &Builder<'_>) {
1118 let target = self.target;
1119
1120 builder.info(&format!("Generating unstable book md files ({target})"));
1121 let out = builder.md_doc_out(target).join("unstable-book");
1122 builder.create_dir(&out);
1123 builder.remove_dir(&out);
1124 let mut cmd = builder.tool_cmd(Tool::UnstableBookGen);
1125 cmd.arg(builder.src.join("library"));
1126 cmd.arg(builder.src.join("compiler"));
1127 cmd.arg(builder.src.join("src"));
1128 cmd.arg(out);
1129
1130 cmd.run(builder);
1131 }
1132}
1133
1134fn symlink_dir_force(config: &Config, original: &Path, link: &Path) {
1135 if config.dry_run() {
1136 return;
1137 }
1138 if let Ok(m) = fs::symlink_metadata(link) {
1139 if m.file_type().is_dir() {
1140 t!(fs::remove_dir_all(link));
1141 } else {
1142 t!(fs::remove_file(link).or_else(|_| fs::remove_dir(link)));
1145 }
1146 }
1147
1148 t!(
1149 symlink_dir(config, original, link),
1150 format!("failed to create link from {} -> {}", link.display(), original.display())
1151 );
1152}
1153
1154#[derive(Ord, PartialOrd, Debug, Clone, Hash, PartialEq, Eq)]
1155pub struct RustcBook {
1156 pub compiler: Compiler,
1157 pub target: TargetSelection,
1158 pub validate: bool,
1159}
1160
1161impl Step for RustcBook {
1162 type Output = ();
1163 const DEFAULT: bool = true;
1164 const ONLY_HOSTS: bool = true;
1165
1166 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1167 let builder = run.builder;
1168 run.path("src/doc/rustc").default_condition(builder.config.docs)
1169 }
1170
1171 fn make_run(run: RunConfig<'_>) {
1172 run.builder.ensure(RustcBook {
1173 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.host_target),
1174 target: run.target,
1175 validate: false,
1176 });
1177 }
1178
1179 fn run(self, builder: &Builder<'_>) {
1185 let out_base = builder.md_doc_out(self.target).join("rustc");
1186 t!(fs::create_dir_all(&out_base));
1187 let out_listing = out_base.join("src/lints");
1188 builder.cp_link_r(&builder.src.join("src/doc/rustc"), &out_base);
1189 builder.info(&format!("Generating lint docs ({})", self.target));
1190
1191 let rustc = builder.rustc(self.compiler);
1192 builder.ensure(compile::Std::new(self.compiler, self.target));
1195 let mut cmd = builder.tool_cmd(Tool::LintDocs);
1196 cmd.arg("--src");
1197 cmd.arg(builder.src.join("compiler"));
1198 cmd.arg("--out");
1199 cmd.arg(&out_listing);
1200 cmd.arg("--rustc");
1201 cmd.arg(&rustc);
1202 cmd.arg("--rustc-target").arg(self.target.rustc_target_arg());
1203 if let Some(target_linker) = builder.linker(self.target) {
1204 cmd.arg("--rustc-linker").arg(target_linker);
1205 }
1206 if builder.is_verbose() {
1207 cmd.arg("--verbose");
1208 }
1209 if self.validate {
1210 cmd.arg("--validate");
1211 }
1212 cmd.env("RUSTC_BOOTSTRAP", "1");
1216
1217 builder.add_rustc_lib_path(self.compiler, &mut cmd);
1221 let doc_generator_guard = builder.msg(
1222 Kind::Run,
1223 self.compiler.stage,
1224 "lint-docs",
1225 self.compiler.host,
1226 self.target,
1227 );
1228 cmd.run(builder);
1229 drop(doc_generator_guard);
1230
1231 builder.ensure(RustbookSrc {
1233 target: self.target,
1234 name: "rustc".to_owned(),
1235 src: out_base,
1236 parent: Some(self),
1237 languages: vec![],
1238 rustdoc_compiler: None,
1239 });
1240 }
1241}
1242
1243#[derive(Ord, PartialOrd, Debug, Clone, Hash, PartialEq, Eq)]
1244pub struct Reference {
1245 pub compiler: Compiler,
1246 pub target: TargetSelection,
1247}
1248
1249impl Step for Reference {
1250 type Output = ();
1251 const DEFAULT: bool = true;
1252
1253 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1254 let builder = run.builder;
1255 run.path("src/doc/reference").default_condition(builder.config.docs)
1256 }
1257
1258 fn make_run(run: RunConfig<'_>) {
1259 run.builder.ensure(Reference {
1260 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.host_target),
1261 target: run.target,
1262 });
1263 }
1264
1265 fn run(self, builder: &Builder<'_>) {
1267 builder.require_submodule("src/doc/reference", None);
1268
1269 builder.ensure(compile::Std::new(self.compiler, builder.config.host_target));
1272
1273 builder.ensure(RustbookSrc {
1275 target: self.target,
1276 name: "reference".to_owned(),
1277 src: builder.src.join("src/doc/reference"),
1278 rustdoc_compiler: Some(self.compiler),
1279 parent: Some(self),
1280 languages: vec![],
1281 });
1282 }
1283}