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::{
16 self, RustcPrivateCompilers, SourceType, Tool, prepare_tool_cargo,
17};
18use crate::core::builder::{
19 self, Builder, Compiler, Kind, RunConfig, ShouldRun, Step, StepMetadata, crate_description,
20};
21use crate::core::config::{Config, TargetSelection};
22use crate::helpers::{submodule_path_of, symlink_dir, t, up_to_date};
23use crate::{FileType, Mode};
24
25macro_rules! book {
26 ($($name:ident, $path:expr, $book_name:expr, $lang:expr ;)+) => {
27 $(
28 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
29 pub struct $name {
30 target: TargetSelection,
31 }
32
33 impl Step for $name {
34 type Output = ();
35
36 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
37 run.path($path)
38 }
39
40 fn is_default_step(builder: &Builder<'_>) -> bool {
41 builder.config.docs
42 }
43
44 fn make_run(run: RunConfig<'_>) {
45 run.builder.ensure($name {
46 target: run.target,
47 });
48 }
49
50 fn run(self, builder: &Builder<'_>) {
51 if let Some(submodule_path) = submodule_path_of(&builder, $path) {
52 builder.require_submodule(&submodule_path, None)
53 }
54
55 builder.ensure(RustbookSrc {
56 target: self.target,
57 name: $book_name.to_owned(),
58 src: builder.src.join($path),
59 parent: Some(self),
60 languages: $lang.into(),
61 build_compiler: None,
62 })
63 }
64 }
65 )+
66 }
67}
68
69book!(
73 CargoBook, "src/tools/cargo/doc/book", "cargo", &[];
74 ClippyBook, "src/tools/clippy/book", "clippy", &[];
75 EditionGuide, "src/doc/edition-guide", "edition-guide", &[];
76 EmbeddedBook, "src/doc/embedded-book", "embedded-book", &[];
77 Nomicon, "src/doc/nomicon", "nomicon", &[];
78 RustByExample, "src/doc/rust-by-example", "rust-by-example", &["es", "ja", "zh", "ko"];
79 RustdocBook, "src/doc/rustdoc", "rustdoc", &[];
80 StyleGuide, "src/doc/style-guide", "style-guide", &[];
81);
82
83#[derive(Debug, Clone, Hash, PartialEq, Eq)]
84pub struct UnstableBook {
85 build_compiler: Compiler,
86 target: TargetSelection,
87}
88
89impl Step for UnstableBook {
90 type Output = ();
91
92 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
93 run.path("src/doc/unstable-book")
94 }
95
96 fn is_default_step(builder: &Builder<'_>) -> bool {
97 builder.config.docs
98 }
99
100 fn make_run(run: RunConfig<'_>) {
101 let stage = if run.builder.config.is_explicit_stage() || run.builder.top_stage >= 2 {
105 run.builder.top_stage
106 } else {
107 2
108 };
109
110 run.builder.ensure(UnstableBook {
111 build_compiler: prepare_doc_compiler(run.builder, run.target, stage),
112 target: run.target,
113 });
114 }
115
116 fn run(self, builder: &Builder<'_>) {
117 builder
118 .ensure(UnstableBookGen { build_compiler: self.build_compiler, target: self.target });
119 builder.ensure(RustbookSrc {
120 target: self.target,
121 name: "unstable-book".to_owned(),
122 src: builder.md_doc_out(self.target).join("unstable-book"),
123 parent: Some(self),
124 languages: vec![],
125 build_compiler: None,
126 })
127 }
128}
129
130#[derive(Debug, Clone, Hash, PartialEq, Eq)]
131struct RustbookSrc<P: Step> {
132 target: TargetSelection,
133 name: String,
134 src: PathBuf,
135 parent: Option<P>,
136 languages: Vec<&'static str>,
137 build_compiler: Option<Compiler>,
139}
140
141impl<P: Step> Step for RustbookSrc<P> {
142 type Output = ();
143
144 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
145 run.never()
146 }
147
148 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
163 if !builder.config.dry_run()
164 && (!up_to_date(&src, &index) || !up_to_date(&rustbook, &index))
165 {
166 builder.info(&format!("Rustbook ({target}) - {name}"));
167 let _ = fs::remove_dir_all(&out);
168
169 let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
170
171 if let Some(compiler) = self.build_compiler {
172 let mut rustdoc = builder.rustdoc_for_compiler(compiler);
173 rustdoc.pop();
174 let old_path = env::var_os("PATH").unwrap_or_default();
175 let new_path =
176 env::join_paths(std::iter::once(rustdoc).chain(env::split_paths(&old_path)))
177 .expect("could not add rustdoc to PATH");
178
179 rustbook_cmd.env("PATH", new_path);
180 builder.add_rustc_lib_path(compiler, &mut rustbook_cmd);
181 }
182
183 rustbook_cmd
184 .arg("build")
185 .arg(&src)
186 .arg("-d")
187 .arg(&out)
188 .arg("--rust-root")
189 .arg(&builder.src)
190 .run(builder);
191
192 for lang in &self.languages {
193 let out = out.join(lang);
194
195 builder.info(&format!("Rustbook ({target}) - {name} - {lang}"));
196 let _ = fs::remove_dir_all(&out);
197
198 builder
199 .tool_cmd(Tool::Rustbook)
200 .arg("build")
201 .arg(&src)
202 .arg("-d")
203 .arg(&out)
204 .arg("-l")
205 .arg(lang)
206 .run(builder);
207 }
208 }
209
210 if self.parent.is_some() {
211 builder.maybe_open_in_browser::<P>(index)
212 }
213 }
214
215 fn metadata(&self) -> Option<StepMetadata> {
216 let mut metadata = StepMetadata::doc(&format!("{} (book)", self.name), self.target);
217 if let Some(compiler) = self.build_compiler {
218 metadata = metadata.built_by(compiler);
219 }
220
221 Some(metadata)
222 }
223}
224
225#[derive(Debug, Clone, Hash, PartialEq, Eq)]
226pub struct TheBook {
227 build_compiler: Compiler,
229 target: TargetSelection,
230}
231
232impl Step for TheBook {
233 type Output = ();
234
235 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
236 run.path("src/doc/book")
237 }
238
239 fn is_default_step(builder: &Builder<'_>) -> bool {
240 builder.config.docs
241 }
242
243 fn make_run(run: RunConfig<'_>) {
244 run.builder.ensure(TheBook {
245 build_compiler: prepare_doc_compiler(run.builder, run.target, run.builder.top_stage),
246 target: run.target,
247 });
248 }
249
250 fn run(self, builder: &Builder<'_>) {
260 builder.require_submodule("src/doc/book", None);
261
262 let build_compiler = self.build_compiler;
263 let target = self.target;
264
265 let absolute_path = builder.src.join("src/doc/book");
266 let redirect_path = absolute_path.join("redirects");
267
268 builder.ensure(RustbookSrc {
270 target,
271 name: "book".to_owned(),
272 src: absolute_path.clone(),
273 parent: Some(self),
274 languages: vec![],
275 build_compiler: None,
276 });
277
278 for edition in &["first-edition", "second-edition", "2018-edition"] {
280 builder.ensure(RustbookSrc {
281 target,
282 name: format!("book/{edition}"),
283 src: absolute_path.join(edition),
284 parent: Option::<Self>::None,
287 languages: vec![],
288 build_compiler: None,
289 });
290 }
291
292 let shared_assets = builder.ensure(SharedAssets { target });
294
295 let _guard = builder.msg(Kind::Doc, "book redirect pages", None, build_compiler, target);
297 if builder.config.dry_run() {
298 return;
299 }
300
301 for file in t!(fs::read_dir(redirect_path)) {
302 let file = t!(file);
303 let path = file.path();
304 let path = path.to_str().unwrap();
305
306 invoke_rustdoc(builder, build_compiler, &shared_assets, target, path);
307 }
308 }
309}
310
311fn invoke_rustdoc(
312 builder: &Builder<'_>,
313 build_compiler: Compiler,
314 shared_assets: &SharedAssetsPaths,
315 target: TargetSelection,
316 markdown: &str,
317) {
318 let out = builder.doc_out(target);
319
320 let path = builder.src.join("src/doc").join(markdown);
321
322 let header = builder.src.join("src/doc/redirect.inc");
323 let footer = builder.src.join("src/doc/footer.inc");
324
325 let mut cmd = builder.rustdoc_cmd(build_compiler);
326
327 let out = out.join("book");
328
329 cmd.arg("--html-after-content")
330 .arg(&footer)
331 .arg("--html-before-content")
332 .arg(&shared_assets.version_info)
333 .arg("--html-in-header")
334 .arg(&header)
335 .arg("--markdown-no-toc")
336 .arg("--markdown-playground-url")
337 .arg("https://play.rust-lang.org/")
338 .arg("-o")
339 .arg(&out)
340 .arg(&path)
341 .arg("--markdown-css")
342 .arg("../rust.css")
343 .arg("-Zunstable-options");
344
345 if !builder.config.docs_minification {
346 cmd.arg("--disable-minification");
347 }
348
349 cmd.run(builder);
350}
351
352#[derive(Debug, Clone, Hash, PartialEq, Eq)]
353pub struct Standalone {
354 build_compiler: Compiler,
355 target: TargetSelection,
356}
357
358impl Step for Standalone {
359 type Output = ();
360
361 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
362 run.path("src/doc").alias("standalone")
363 }
364
365 fn is_default_step(builder: &Builder<'_>) -> bool {
366 builder.config.docs
367 }
368
369 fn make_run(run: RunConfig<'_>) {
370 run.builder.ensure(Standalone {
371 build_compiler: prepare_doc_compiler(
372 run.builder,
373 run.builder.host_target,
374 run.builder.top_stage,
375 ),
376 target: run.target,
377 });
378 }
379
380 fn run(self, builder: &Builder<'_>) {
389 let target = self.target;
390 let build_compiler = self.build_compiler;
391 let _guard = builder.msg(Kind::Doc, "standalone", None, build_compiler, target);
392 let out = builder.doc_out(target);
393 t!(fs::create_dir_all(&out));
394
395 let version_info = builder.ensure(SharedAssets { target: self.target }).version_info;
396
397 let favicon = builder.src.join("src/doc/favicon.inc");
398 let footer = builder.src.join("src/doc/footer.inc");
399 let full_toc = builder.src.join("src/doc/full-toc.inc");
400
401 for file in t!(fs::read_dir(builder.src.join("src/doc"))) {
402 let file = t!(file);
403 let path = file.path();
404 let filename = path.file_name().unwrap().to_str().unwrap();
405 if !filename.ends_with(".md") || filename == "README.md" {
406 continue;
407 }
408
409 let html = out.join(filename).with_extension("html");
410 let rustdoc = builder.rustdoc_for_compiler(build_compiler);
411 if up_to_date(&path, &html)
412 && up_to_date(&footer, &html)
413 && up_to_date(&favicon, &html)
414 && up_to_date(&full_toc, &html)
415 && (builder.config.dry_run() || up_to_date(&version_info, &html))
416 && (builder.config.dry_run() || up_to_date(&rustdoc, &html))
417 {
418 continue;
419 }
420
421 let mut cmd = builder.rustdoc_cmd(build_compiler);
422
423 cmd.arg("--html-after-content")
424 .arg(&footer)
425 .arg("--html-before-content")
426 .arg(&version_info)
427 .arg("--html-in-header")
428 .arg(&favicon)
429 .arg("--markdown-no-toc")
430 .arg("-Zunstable-options")
431 .arg("--index-page")
432 .arg(builder.src.join("src/doc/index.md"))
433 .arg("--markdown-playground-url")
434 .arg("https://play.rust-lang.org/")
435 .arg("-o")
436 .arg(&out)
437 .arg(&path);
438
439 if !builder.config.docs_minification {
440 cmd.arg("--disable-minification");
441 }
442
443 if filename == "not_found.md" {
444 cmd.arg("--markdown-css").arg("https://doc.rust-lang.org/rust.css");
445 } else {
446 cmd.arg("--markdown-css").arg("rust.css");
447 }
448 cmd.run(builder);
449 }
450
451 if builder.paths.is_empty() || builder.was_invoked_explicitly::<Self>(Kind::Doc) {
454 let index = out.join("index.html");
455 builder.open_in_browser(index);
456 }
457 }
458
459 fn metadata(&self) -> Option<StepMetadata> {
460 Some(StepMetadata::doc("standalone", self.target).built_by(self.build_compiler))
461 }
462}
463
464#[derive(Debug, Clone, Hash, PartialEq, Eq)]
465pub struct Releases {
466 build_compiler: Compiler,
467 target: TargetSelection,
468}
469
470impl Step for Releases {
471 type Output = ();
472
473 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
474 run.path("RELEASES.md").alias("releases")
475 }
476
477 fn is_default_step(builder: &Builder<'_>) -> bool {
478 builder.config.docs
479 }
480
481 fn make_run(run: RunConfig<'_>) {
482 run.builder.ensure(Releases {
483 build_compiler: prepare_doc_compiler(
484 run.builder,
485 run.builder.host_target,
486 run.builder.top_stage,
487 ),
488 target: run.target,
489 });
490 }
491
492 fn run(self, builder: &Builder<'_>) {
498 let target = self.target;
499 let build_compiler = self.build_compiler;
500 let _guard = builder.msg(Kind::Doc, "releases", None, build_compiler, target);
501 let out = builder.doc_out(target);
502 t!(fs::create_dir_all(&out));
503
504 builder.ensure(Standalone { build_compiler, target });
505
506 let version_info = builder.ensure(SharedAssets { target: self.target }).version_info;
507
508 let favicon = builder.src.join("src/doc/favicon.inc");
509 let footer = builder.src.join("src/doc/footer.inc");
510 let full_toc = builder.src.join("src/doc/full-toc.inc");
511
512 let html = out.join("releases.html");
513 let tmppath = out.join("releases.md");
514 let inpath = builder.src.join("RELEASES.md");
515 let rustdoc = builder.rustdoc_for_compiler(build_compiler);
516 if !up_to_date(&inpath, &html)
517 || !up_to_date(&footer, &html)
518 || !up_to_date(&favicon, &html)
519 || !up_to_date(&full_toc, &html)
520 || !(builder.config.dry_run()
521 || up_to_date(&version_info, &html)
522 || up_to_date(&rustdoc, &html))
523 {
524 let mut tmpfile = t!(fs::File::create(&tmppath));
525 t!(tmpfile.write_all(b"% Rust Release Notes\n\n"));
526 t!(io::copy(&mut t!(fs::File::open(&inpath)), &mut tmpfile));
527 mem::drop(tmpfile);
528 let mut cmd = builder.rustdoc_cmd(build_compiler);
529
530 cmd.arg("--html-after-content")
531 .arg(&footer)
532 .arg("--html-before-content")
533 .arg(&version_info)
534 .arg("--html-in-header")
535 .arg(&favicon)
536 .arg("--markdown-no-toc")
537 .arg("--markdown-css")
538 .arg("rust.css")
539 .arg("-Zunstable-options")
540 .arg("--index-page")
541 .arg(builder.src.join("src/doc/index.md"))
542 .arg("--markdown-playground-url")
543 .arg("https://play.rust-lang.org/")
544 .arg("-o")
545 .arg(&out)
546 .arg(&tmppath);
547
548 if !builder.config.docs_minification {
549 cmd.arg("--disable-minification");
550 }
551
552 cmd.run(builder);
553 }
554
555 if builder.was_invoked_explicitly::<Self>(Kind::Doc) {
558 builder.open_in_browser(&html);
559 }
560 }
561
562 fn metadata(&self) -> Option<StepMetadata> {
563 Some(StepMetadata::doc("releases", self.target).built_by(self.build_compiler))
564 }
565}
566
567#[derive(Debug, Clone)]
568pub struct SharedAssetsPaths {
569 pub version_info: PathBuf,
570}
571
572#[derive(Debug, Clone, Hash, PartialEq, Eq)]
573pub struct SharedAssets {
574 target: TargetSelection,
575}
576
577impl Step for SharedAssets {
578 type Output = SharedAssetsPaths;
579
580 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
581 run.never()
583 }
584
585 fn is_default_step(_builder: &Builder<'_>) -> bool {
586 false
587 }
588
589 fn run(self, builder: &Builder<'_>) -> Self::Output {
591 let out = builder.doc_out(self.target);
592
593 let version_input = builder.src.join("src").join("doc").join("version_info.html.template");
594 let version_info = out.join("version_info.html");
595 if !builder.config.dry_run() && !up_to_date(&version_input, &version_info) {
596 let info = t!(fs::read_to_string(&version_input))
597 .replace("VERSION", &builder.rust_release())
598 .replace("SHORT_HASH", builder.rust_info().sha_short().unwrap_or(""))
599 .replace("STAMP", builder.rust_info().sha().unwrap_or(""));
600 t!(fs::write(&version_info, info));
601 }
602
603 builder.copy_link(
604 &builder.src.join("src").join("doc").join("rust.css"),
605 &out.join("rust.css"),
606 FileType::Regular,
607 );
608
609 builder.copy_link(
610 &builder
611 .src
612 .join("src")
613 .join("librustdoc")
614 .join("html")
615 .join("static")
616 .join("images")
617 .join("favicon.svg"),
618 &out.join("favicon.svg"),
619 FileType::Regular,
620 );
621 builder.copy_link(
622 &builder
623 .src
624 .join("src")
625 .join("librustdoc")
626 .join("html")
627 .join("static")
628 .join("images")
629 .join("favicon-32x32.png"),
630 &out.join("favicon-32x32.png"),
631 FileType::Regular,
632 );
633
634 SharedAssetsPaths { version_info }
635 }
636}
637
638#[derive(Debug, Clone, Hash, PartialEq, Eq)]
640pub struct Std {
641 build_compiler: Compiler,
642 target: TargetSelection,
643 format: DocumentationFormat,
644 crates: Vec<String>,
645}
646
647impl Std {
648 pub(crate) fn from_build_compiler(
649 build_compiler: Compiler,
650 target: TargetSelection,
651 format: DocumentationFormat,
652 ) -> Self {
653 Std { build_compiler, target, format, crates: vec![] }
654 }
655}
656
657impl Step for Std {
658 type Output = PathBuf;
660
661 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
662 run.crate_or_deps("sysroot").path("library")
663 }
664
665 fn is_default_step(builder: &Builder<'_>) -> bool {
666 builder.config.docs
667 }
668
669 fn make_run(run: RunConfig<'_>) {
670 let crates = compile::std_crates_for_make_run(&run);
671 let target_is_no_std = run.builder.no_std(run.target).unwrap_or(false);
672 if crates.is_empty() && target_is_no_std {
673 return;
674 }
675 run.builder.ensure(Std {
676 build_compiler: run.builder.compiler_for_std(run.builder.top_stage),
677 target: run.target,
678 format: if run.builder.config.cmd.json() {
679 DocumentationFormat::Json
680 } else {
681 DocumentationFormat::Html
682 },
683 crates,
684 });
685 }
686
687 fn run(self, builder: &Builder<'_>) -> Self::Output {
692 let target = self.target;
693 let crates = if self.crates.is_empty() {
694 builder
695 .in_tree_crates("sysroot", Some(target))
696 .iter()
697 .map(|c| c.name.to_string())
698 .collect()
699 } else {
700 self.crates
701 };
702
703 let out = match self.format {
704 DocumentationFormat::Html => builder.doc_out(target),
705 DocumentationFormat::Json => builder.json_doc_out(target),
706 };
707
708 t!(fs::create_dir_all(&out));
709
710 if self.format == DocumentationFormat::Html {
711 builder.ensure(SharedAssets { target: self.target });
712 }
713
714 let index_page = builder
715 .src
716 .join("src/doc/index.md")
717 .into_os_string()
718 .into_string()
719 .expect("non-utf8 paths are unsupported");
720 let mut extra_args = match self.format {
721 DocumentationFormat::Html => {
722 vec!["--markdown-css", "rust.css", "--markdown-no-toc", "--index-page", &index_page]
723 }
724 DocumentationFormat::Json => vec!["--output-format", "json"],
725 };
726
727 if !builder.config.docs_minification {
728 extra_args.push("--disable-minification");
729 }
730 extra_args.push("-Zunstable-options");
732
733 doc_std(builder, self.format, self.build_compiler, target, &out, &extra_args, &crates);
734
735 if let DocumentationFormat::Html = self.format {
737 if builder.paths.iter().any(|path| path.ends_with("library")) {
738 let index = out.join("std").join("index.html");
740 builder.maybe_open_in_browser::<Self>(index);
741 } else {
742 for requested_crate in crates {
743 if STD_PUBLIC_CRATES.iter().any(|&k| k == requested_crate) {
744 let index = out.join(requested_crate).join("index.html");
745 builder.maybe_open_in_browser::<Self>(index);
746 break;
747 }
748 }
749 }
750 }
751
752 out
753 }
754
755 fn metadata(&self) -> Option<StepMetadata> {
756 Some(
757 StepMetadata::doc("std", self.target)
758 .built_by(self.build_compiler)
759 .with_metadata(format!("crates=[{}]", self.crates.join(","))),
760 )
761 }
762}
763
764const STD_PUBLIC_CRATES: [&str; 5] = ["core", "alloc", "std", "proc_macro", "test"];
774
775#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
776pub enum DocumentationFormat {
777 Html,
778 Json,
779}
780
781impl DocumentationFormat {
782 fn as_str(&self) -> &str {
783 match self {
784 DocumentationFormat::Html => "HTML",
785 DocumentationFormat::Json => "JSON",
786 }
787 }
788}
789
790fn doc_std(
792 builder: &Builder<'_>,
793 format: DocumentationFormat,
794 build_compiler: Compiler,
795 target: TargetSelection,
796 out: &Path,
797 extra_args: &[&str],
798 requested_crates: &[String],
799) {
800 let target_doc_dir_name = if format == DocumentationFormat::Json { "json-doc" } else { "doc" };
801 let target_dir =
802 builder.stage_out(build_compiler, Mode::Std).join(target).join(target_doc_dir_name);
803
804 let out_dir = target_dir.join(target).join("doc");
808
809 let mut cargo = builder::Cargo::new(
810 builder,
811 build_compiler,
812 Mode::Std,
813 SourceType::InTree,
814 target,
815 Kind::Doc,
816 );
817
818 compile::std_cargo(builder, target, &mut cargo, requested_crates);
819 cargo
820 .arg("--no-deps")
821 .arg("--target-dir")
822 .arg(&*target_dir.to_string_lossy())
823 .arg("-Zskip-rustdoc-fingerprint")
824 .arg("-Zrustdoc-map")
825 .rustdocflag("--extern-html-root-url")
826 .rustdocflag("std_detect=https://docs.rs/std_detect/latest/")
827 .rustdocflag("--extern-html-root-takes-precedence")
828 .rustdocflag("--resource-suffix")
829 .rustdocflag(&builder.version);
830 for arg in extra_args {
831 cargo.rustdocflag(arg);
832 }
833
834 if format == DocumentationFormat::Json || builder.config.library_docs_private_items {
837 cargo.rustdocflag("--document-private-items").rustdocflag("--document-hidden-items");
838 }
839
840 let description =
841 format!("library{} in {} format", crate_description(requested_crates), format.as_str());
842 let _guard = builder.msg(Kind::Doc, description, Mode::Std, build_compiler, target);
843
844 cargo.into_cmd().run(builder);
845 builder.cp_link_r(&out_dir, out);
846}
847
848pub fn prepare_doc_compiler(
850 builder: &Builder<'_>,
851 target: TargetSelection,
852 stage: u32,
853) -> Compiler {
854 assert!(stage > 0, "Cannot document anything in stage 0");
855 let build_compiler = builder.compiler(stage - 1, builder.host_target);
856 builder.std(build_compiler, target);
857 build_compiler
858}
859
860#[derive(Debug, Clone, Hash, PartialEq, Eq)]
862pub struct Rustc {
863 build_compiler: Compiler,
864 target: TargetSelection,
865 crates: Vec<String>,
866}
867
868impl Rustc {
869 pub(crate) fn for_stage(builder: &Builder<'_>, stage: u32, target: TargetSelection) -> Self {
871 let build_compiler = prepare_doc_compiler(builder, target, stage);
872 Self::from_build_compiler(builder, build_compiler, target)
873 }
874
875 fn from_build_compiler(
876 builder: &Builder<'_>,
877 build_compiler: Compiler,
878 target: TargetSelection,
879 ) -> Self {
880 let crates = builder
881 .in_tree_crates("rustc-main", Some(target))
882 .into_iter()
883 .map(|krate| krate.name.to_string())
884 .collect();
885 Self { build_compiler, target, crates }
886 }
887}
888
889impl Step for Rustc {
890 type Output = ();
891 const IS_HOST: bool = true;
892
893 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
894 run.crate_or_deps("rustc-main").path("compiler")
895 }
896
897 fn is_default_step(builder: &Builder<'_>) -> bool {
898 builder.config.compiler_docs
899 }
900
901 fn make_run(run: RunConfig<'_>) {
902 run.builder.ensure(Rustc::for_stage(run.builder, run.builder.top_stage, run.target));
903 }
904
905 fn run(self, builder: &Builder<'_>) {
912 let target = self.target;
913
914 let out = builder.compiler_doc_out(target);
916 t!(fs::create_dir_all(&out));
917
918 let build_compiler = self.build_compiler;
921 builder.std(build_compiler, builder.config.host_target);
922
923 let _guard = builder.msg(
924 Kind::Doc,
925 format!("compiler{}", crate_description(&self.crates)),
926 Mode::Rustc,
927 build_compiler,
928 target,
929 );
930
931 let mut cargo = builder::Cargo::new(
933 builder,
934 build_compiler,
935 Mode::Rustc,
936 SourceType::InTree,
937 target,
938 Kind::Doc,
939 );
940
941 cargo.rustdocflag("--document-private-items");
942 cargo.rustdocflag("-Arustdoc::private-intra-doc-links");
944 cargo.rustdocflag("--enable-index-page");
945 cargo.rustdocflag("-Znormalize-docs");
946 cargo.rustdocflag("--show-type-layout");
947 cargo.rustdocflag("--generate-link-to-definition");
951 cargo.rustdocflag("--generate-macro-expansion");
952
953 compile::rustc_cargo(builder, &mut cargo, target, &build_compiler, &self.crates);
954 cargo.arg("-Zskip-rustdoc-fingerprint");
955
956 cargo.arg("--no-deps");
959 cargo.arg("-Zrustdoc-map");
960
961 cargo.rustdocflag("--extern-html-root-url");
964 cargo.rustdocflag("ena=https://docs.rs/ena/latest/");
965
966 let mut to_open = None;
967
968 let out_dir = builder.stage_out(build_compiler, Mode::Rustc).join(target).join("doc");
969 for krate in &*self.crates {
970 let dir_name = krate.replace('-', "_");
974 t!(fs::create_dir_all(out_dir.join(&*dir_name)));
975 cargo.arg("-p").arg(krate);
976 if to_open.is_none() {
977 to_open = Some(dir_name);
978 }
979 }
980
981 symlink_dir_force(&builder.config, &out, &out_dir);
988 let proc_macro_out_dir = builder.stage_out(build_compiler, Mode::Rustc).join("doc");
991 symlink_dir_force(&builder.config, &out, &proc_macro_out_dir);
992
993 cargo.into_cmd().run(builder);
994
995 if !builder.config.dry_run() {
996 for krate in &*self.crates {
998 let dir_name = krate.replace('-', "_");
999 assert!(out.join(&*dir_name).read_dir().unwrap().next().is_some());
1001 }
1002 }
1003
1004 if builder.paths.iter().any(|path| path.ends_with("compiler")) {
1005 let index = out.join("rustc_middle").join("index.html");
1007 builder.open_in_browser(index);
1008 } else if let Some(krate) = to_open {
1009 let index = out.join(krate).join("index.html");
1011 builder.open_in_browser(index);
1012 }
1013 }
1014
1015 fn metadata(&self) -> Option<StepMetadata> {
1016 Some(StepMetadata::doc("rustc", self.target).built_by(self.build_compiler))
1017 }
1018}
1019
1020macro_rules! tool_doc {
1021 (
1022 $tool: ident,
1023 $path: literal,
1024 mode = $mode:expr
1025 $(, is_library = $is_library:expr )?
1026 $(, crates = $crates:expr )?
1027 $(, allow_features: $allow_features:expr )?
1029 ) => {
1030 #[derive(Debug, Clone, Hash, PartialEq, Eq)]
1031 pub struct $tool {
1032 build_compiler: Compiler,
1033 mode: Mode,
1034 target: TargetSelection,
1035 }
1036
1037 impl Step for $tool {
1038 type Output = ();
1039 const IS_HOST: bool = true;
1040
1041 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1042 run.path($path)
1043 }
1044
1045 fn is_default_step(builder: &Builder<'_>) -> bool {
1046 builder.config.compiler_docs
1047 }
1048
1049 fn make_run(run: RunConfig<'_>) {
1050 let target = run.target;
1051 let build_compiler = match $mode {
1052 Mode::ToolRustcPrivate => {
1053 let compilers = RustcPrivateCompilers::new(run.builder, run.builder.top_stage, target);
1055
1056 run.builder.ensure(Rustc::from_build_compiler(run.builder, compilers.build_compiler(), target));
1058 compilers.build_compiler()
1059 }
1060 Mode::ToolTarget => {
1061 prepare_doc_compiler(run.builder, run.builder.host_target, run.builder.top_stage)
1064 }
1065 _ => {
1066 panic!("Unexpected tool mode for documenting: {:?}", $mode);
1067 }
1068 };
1069
1070 run.builder.ensure($tool { build_compiler, mode: $mode, target });
1071 }
1072
1073 fn run(self, builder: &Builder<'_>) {
1077 let mut source_type = SourceType::InTree;
1078
1079 if let Some(submodule_path) = submodule_path_of(&builder, $path) {
1080 source_type = SourceType::Submodule;
1081 builder.require_submodule(&submodule_path, None);
1082 }
1083
1084 let $tool { build_compiler, mode, target } = self;
1085
1086 let out = builder.compiler_doc_out(target);
1088 t!(fs::create_dir_all(&out));
1089
1090 let mut cargo = prepare_tool_cargo(
1092 builder,
1093 build_compiler,
1094 mode,
1095 target,
1096 Kind::Doc,
1097 $path,
1098 source_type,
1099 &[],
1100 );
1101 let allow_features = {
1102 let mut _value = "";
1103 $( _value = $allow_features; )?
1104 _value
1105 };
1106
1107 if !allow_features.is_empty() {
1108 cargo.allow_features(allow_features);
1109 }
1110
1111 cargo.arg("-Zskip-rustdoc-fingerprint");
1112 cargo.arg("--no-deps");
1114
1115 if false $(|| $is_library)? {
1116 cargo.arg("--lib");
1117 }
1118
1119 $(for krate in $crates {
1120 cargo.arg("-p").arg(krate);
1121 })?
1122
1123 cargo.rustdocflag("--document-private-items");
1124 cargo.rustdocflag("-Arustdoc::private-intra-doc-links");
1126 cargo.rustdocflag("--enable-index-page");
1127 cargo.rustdocflag("--show-type-layout");
1128 cargo.rustdocflag("--generate-link-to-definition");
1129
1130 let out_dir = builder.stage_out(build_compiler, mode).join(target).join("doc");
1131 $(for krate in $crates {
1132 let dir_name = krate.replace("-", "_");
1133 t!(fs::create_dir_all(out_dir.join(&*dir_name)));
1134 })?
1135
1136 symlink_dir_force(&builder.config, &out, &out_dir);
1138 let proc_macro_out_dir = builder.stage_out(build_compiler, mode).join("doc");
1139 symlink_dir_force(&builder.config, &out, &proc_macro_out_dir);
1140
1141 let _guard = builder.msg(Kind::Doc, stringify!($tool).to_lowercase(), None, build_compiler, target);
1142 cargo.into_cmd().run(builder);
1143
1144 if !builder.config.dry_run() {
1145 $(for krate in $crates {
1147 let dir_name = krate.replace("-", "_");
1148 assert!(out.join(&*dir_name).read_dir().unwrap().next().is_some());
1150 })?
1151 }
1152 }
1153
1154 fn metadata(&self) -> Option<StepMetadata> {
1155 Some(StepMetadata::doc(stringify!($tool), self.target).built_by(self.build_compiler))
1156 }
1157 }
1158 }
1159}
1160
1161tool_doc!(
1163 BuildHelper,
1164 "src/build_helper",
1165 mode = Mode::ToolTarget,
1170 is_library = true,
1171 crates = ["build_helper"]
1172);
1173tool_doc!(
1174 Rustdoc,
1175 "src/tools/rustdoc",
1176 mode = Mode::ToolRustcPrivate,
1177 crates = ["rustdoc", "rustdoc-json-types"]
1178);
1179tool_doc!(
1180 Rustfmt,
1181 "src/tools/rustfmt",
1182 mode = Mode::ToolRustcPrivate,
1183 crates = ["rustfmt-nightly", "rustfmt-config_proc_macro"]
1184);
1185tool_doc!(
1186 Clippy,
1187 "src/tools/clippy",
1188 mode = Mode::ToolRustcPrivate,
1189 crates = ["clippy_config", "clippy_utils"]
1190);
1191tool_doc!(Miri, "src/tools/miri", mode = Mode::ToolRustcPrivate, crates = ["miri"]);
1192tool_doc!(
1193 Cargo,
1194 "src/tools/cargo",
1195 mode = Mode::ToolTarget,
1196 crates = [
1197 "cargo",
1198 "cargo-credential",
1199 "cargo-platform",
1200 "cargo-test-macro",
1201 "cargo-test-support",
1202 "cargo-util",
1203 "cargo-util-schemas",
1204 "crates-io",
1205 "mdman",
1206 "rustfix",
1207 ],
1208 allow_features: "specialization"
1211);
1212tool_doc!(Tidy, "src/tools/tidy", mode = Mode::ToolTarget, crates = ["tidy"]);
1213tool_doc!(
1214 Bootstrap,
1215 "src/bootstrap",
1216 mode = Mode::ToolTarget,
1217 is_library = true,
1218 crates = ["bootstrap"]
1219);
1220tool_doc!(
1221 RunMakeSupport,
1222 "src/tools/run-make-support",
1223 mode = Mode::ToolTarget,
1224 is_library = true,
1225 crates = ["run_make_support"]
1226);
1227tool_doc!(
1228 Compiletest,
1229 "src/tools/compiletest",
1230 mode = Mode::ToolTarget,
1231 is_library = true,
1232 crates = ["compiletest"]
1233);
1234
1235#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1236pub struct ErrorIndex {
1237 compilers: RustcPrivateCompilers,
1238}
1239
1240impl Step for ErrorIndex {
1241 type Output = ();
1242 const IS_HOST: bool = true;
1243
1244 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1245 run.path("src/tools/error_index_generator")
1246 }
1247
1248 fn is_default_step(builder: &Builder<'_>) -> bool {
1249 builder.config.docs
1250 }
1251
1252 fn make_run(run: RunConfig<'_>) {
1253 run.builder.ensure(ErrorIndex {
1254 compilers: RustcPrivateCompilers::new(run.builder, run.builder.top_stage, run.target),
1255 });
1256 }
1257
1258 fn run(self, builder: &Builder<'_>) {
1261 builder.info(&format!("Documenting error index ({})", self.compilers.target()));
1262 let out = builder.doc_out(self.compilers.target());
1263 t!(fs::create_dir_all(&out));
1264 tool::ErrorIndex::command(builder, self.compilers)
1265 .arg("html")
1266 .arg(&out)
1267 .arg(&builder.version)
1268 .run(builder);
1269
1270 let index = out.join("error-index.html");
1271 builder.maybe_open_in_browser::<Self>(index);
1272 }
1273
1274 fn metadata(&self) -> Option<StepMetadata> {
1275 Some(
1276 StepMetadata::doc("error-index", self.compilers.target())
1277 .built_by(self.compilers.build_compiler()),
1278 )
1279 }
1280}
1281
1282#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1283pub struct UnstableBookGen {
1284 build_compiler: Compiler,
1285 target: TargetSelection,
1286}
1287
1288impl Step for UnstableBookGen {
1289 type Output = ();
1290 const IS_HOST: bool = true;
1291
1292 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1293 run.path("src/tools/unstable-book-gen")
1294 }
1295
1296 fn is_default_step(builder: &Builder<'_>) -> bool {
1297 builder.config.docs
1298 }
1299
1300 fn make_run(run: RunConfig<'_>) {
1301 run.builder.ensure(UnstableBookGen {
1302 build_compiler: prepare_doc_compiler(run.builder, run.target, run.builder.top_stage),
1303 target: run.target,
1304 });
1305 }
1306
1307 fn run(self, builder: &Builder<'_>) {
1308 let target = self.target;
1309 let rustc_path = builder.rustc(self.build_compiler);
1310
1311 builder.info(&format!("Generating unstable book md files ({target})"));
1312 let out = builder.md_doc_out(target).join("unstable-book");
1313 builder.create_dir(&out);
1314 builder.remove_dir(&out);
1315 let mut cmd = builder.tool_cmd(Tool::UnstableBookGen);
1316 cmd.arg(builder.src.join("library"));
1317 cmd.arg(builder.src.join("compiler"));
1318 cmd.arg(builder.src.join("src"));
1319 cmd.arg(rustc_path);
1320 cmd.arg(out);
1321
1322 builder.add_rustc_lib_path(self.build_compiler, &mut cmd);
1325
1326 cmd.run(builder);
1327 }
1328}
1329
1330fn symlink_dir_force(config: &Config, original: &Path, link: &Path) {
1331 if config.dry_run() {
1332 return;
1333 }
1334 if let Ok(m) = fs::symlink_metadata(link) {
1335 if m.file_type().is_dir() {
1336 t!(fs::remove_dir_all(link));
1337 } else {
1338 t!(fs::remove_file(link).or_else(|_| fs::remove_dir(link)));
1341 }
1342 }
1343
1344 t!(
1345 symlink_dir(config, original, link),
1346 format!("failed to create link from {} -> {}", link.display(), original.display())
1347 );
1348}
1349
1350#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1352pub struct RustcBook {
1353 build_compiler: Compiler,
1354 target: TargetSelection,
1355 validate: bool,
1358}
1359
1360impl RustcBook {
1361 pub fn validate(build_compiler: Compiler, target: TargetSelection) -> Self {
1362 Self { build_compiler, target, validate: true }
1363 }
1364}
1365
1366impl Step for RustcBook {
1367 type Output = ();
1368 const IS_HOST: bool = true;
1369
1370 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1371 run.path("src/doc/rustc")
1372 }
1373
1374 fn is_default_step(builder: &Builder<'_>) -> bool {
1375 builder.config.docs
1376 }
1377
1378 fn make_run(run: RunConfig<'_>) {
1379 let stage = if run.builder.config.is_explicit_stage() || run.builder.top_stage >= 2 {
1383 run.builder.top_stage
1384 } else {
1385 2
1386 };
1387
1388 run.builder.ensure(RustcBook {
1389 build_compiler: prepare_doc_compiler(run.builder, run.target, stage),
1390 target: run.target,
1391 validate: false,
1392 });
1393 }
1394
1395 fn run(self, builder: &Builder<'_>) {
1401 if cfg!(not(test)) && self.target == "i686-pc-windows-msvc" {
1404 eprintln!("WARNING: Skipping rustc book build to work around #158378");
1405 return;
1406 }
1407
1408 let out_base = builder.md_doc_out(self.target).join("rustc");
1409 t!(fs::create_dir_all(&out_base));
1410 let out_listing = out_base.join("src/lints");
1411 builder.cp_link_r(&builder.src.join("src/doc/rustc"), &out_base);
1412 builder.info(&format!("Generating lint docs ({})", self.target));
1413
1414 let rustc = builder.rustc(self.build_compiler);
1415 builder.std(self.build_compiler, self.target);
1418 let mut cmd = builder.tool_cmd(Tool::LintDocs);
1419 cmd.arg("--build-rustc-stage");
1420 cmd.arg(self.build_compiler.stage.to_string());
1421 cmd.arg("--src");
1422 cmd.arg(builder.src.join("compiler"));
1423 cmd.arg("--out");
1424 cmd.arg(&out_listing);
1425 cmd.arg("--rustc");
1426 cmd.arg(&rustc);
1427 cmd.arg("--rustc-target").arg(self.target.rustc_target_arg());
1428 if let Some(target_linker) = builder.linker(self.target) {
1429 cmd.arg("--rustc-linker").arg(target_linker);
1430 }
1431 if builder.is_verbose() {
1432 cmd.arg("--verbose");
1433 }
1434 if self.validate {
1435 cmd.arg("--validate");
1436 }
1437 cmd.env("RUSTC_BOOTSTRAP", "1");
1441
1442 builder.add_rustc_lib_path(self.build_compiler, &mut cmd);
1446 let doc_generator_guard =
1447 builder.msg(Kind::Run, "lint-docs", None, self.build_compiler, self.target);
1448 cmd.run(builder);
1449 drop(doc_generator_guard);
1450
1451 builder.ensure(RustbookSrc {
1453 target: self.target,
1454 name: "rustc".to_owned(),
1455 src: out_base,
1456 parent: Some(self),
1457 languages: vec![],
1458 build_compiler: None,
1459 });
1460 }
1461}
1462
1463#[derive(Debug, Clone, Hash, PartialEq, Eq)]
1467pub struct Reference {
1468 build_compiler: Compiler,
1469 target: TargetSelection,
1470}
1471
1472impl Step for Reference {
1473 type Output = ();
1474
1475 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1476 run.path("src/doc/reference")
1477 }
1478
1479 fn is_default_step(builder: &Builder<'_>) -> bool {
1480 builder.config.docs
1481 }
1482
1483 fn make_run(run: RunConfig<'_>) {
1484 let stage = if run.builder.config.is_explicit_stage() || run.builder.top_stage >= 2 {
1490 run.builder.top_stage
1491 } else {
1492 2
1493 };
1494
1495 run.builder.ensure(Reference {
1496 build_compiler: prepare_doc_compiler(run.builder, run.target, stage),
1497 target: run.target,
1498 });
1499 }
1500
1501 fn run(self, builder: &Builder<'_>) {
1503 builder.require_submodule("src/doc/reference", None);
1504
1505 builder.std(self.build_compiler, builder.config.host_target);
1508
1509 builder.ensure(RustbookSrc {
1511 target: self.target,
1512 name: "reference".to_owned(),
1513 src: builder.src.join("src/doc/reference"),
1514 build_compiler: Some(self.build_compiler),
1515 parent: Some(self),
1516 languages: vec![],
1517 });
1518 }
1519}