1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Documentation generation for rustbuilder.
13 //! This module implements generation for all bits and pieces of documentation
14 //! for the Rust project. This notably includes suites like the rust book, the
15 //! nomicon, rust by example, standalone documentation, etc.
17 //! Everything here is basically just a shim around calling either `rustbook` or
20 use std::collections::HashSet;
21 use std::fs::{self, File};
22 use std::io::prelude::*;
24 use std::path::{PathBuf, Path};
27 use build_helper::up_to_date;
29 use util::symlink_dir;
30 use builder::{Builder, Compiler, RunConfig, ShouldRun, Step};
33 use cache::{INTERNER, Interned};
37 ($($name:ident, $path:expr, $book_name:expr;)+) => {
39 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
41 target: Interned<String>,
46 const DEFAULT: bool = true;
48 fn should_run(run: ShouldRun) -> ShouldRun {
49 let builder = run.builder;
50 run.path($path).default_condition(builder.config.docs)
53 fn make_run(run: RunConfig) {
54 run.builder.ensure($name {
59 fn run(self, builder: &Builder) {
60 builder.ensure(Rustbook {
62 name: INTERNER.intern_str($book_name),
71 Nomicon, "src/doc/nomicon", "nomicon";
72 Reference, "src/doc/reference", "reference";
73 Rustdoc, "src/doc/rustdoc", "rustdoc";
74 RustcBook, "src/doc/rustc", "rustc";
75 RustByExample, "src/doc/rust-by-example", "rust-by-example";
78 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
80 target: Interned<String>,
81 name: Interned<String>,
84 impl Step for Rustbook {
87 // rustbook is never directly called, and only serves as a shim for the nomicon and the
89 fn should_run(run: ShouldRun) -> ShouldRun {
93 /// Invoke `rustbook` for `target` for the doc book `name`.
95 /// This will not actually generate any documentation if the documentation has
96 /// already been generated.
97 fn run(self, builder: &Builder) {
98 let src = builder.src.join("src/doc");
99 builder.ensure(RustbookSrc {
102 src: INTERNER.intern_path(src),
107 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
108 pub struct UnstableBook {
109 target: Interned<String>,
112 impl Step for UnstableBook {
114 const DEFAULT: bool = true;
116 fn should_run(run: ShouldRun) -> ShouldRun {
117 let builder = run.builder;
118 run.path("src/doc/unstable-book").default_condition(builder.config.docs)
121 fn make_run(run: RunConfig) {
122 run.builder.ensure(UnstableBook {
127 fn run(self, builder: &Builder) {
128 builder.ensure(UnstableBookGen {
131 builder.ensure(RustbookSrc {
133 name: INTERNER.intern_str("unstable-book"),
134 src: builder.md_doc_out(self.target),
139 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
140 pub struct CargoBook {
141 target: Interned<String>,
142 name: Interned<String>,
145 impl Step for CargoBook {
147 const DEFAULT: bool = true;
149 fn should_run(run: ShouldRun) -> ShouldRun {
150 let builder = run.builder;
151 run.path("src/tools/cargo/src/doc/book").default_condition(builder.config.docs)
154 fn make_run(run: RunConfig) {
155 run.builder.ensure(CargoBook {
157 name: INTERNER.intern_str("cargo"),
161 fn run(self, builder: &Builder) {
162 let target = self.target;
163 let name = self.name;
164 let src = builder.src.join("src/tools/cargo/src/doc");
166 let out = builder.doc_out(target);
167 t!(fs::create_dir_all(&out));
169 let out = out.join(name);
171 builder.info(&format!("Cargo Book ({}) - {}", target, name));
173 let _ = fs::remove_dir_all(&out);
175 builder.run(builder.tool_cmd(Tool::Rustbook)
183 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
185 target: Interned<String>,
186 name: Interned<String>,
187 src: Interned<PathBuf>,
190 impl Step for RustbookSrc {
193 fn should_run(run: ShouldRun) -> ShouldRun {
197 /// Invoke `rustbook` for `target` for the doc book `name` from the `src` path.
199 /// This will not actually generate any documentation if the documentation has
200 /// already been generated.
201 fn run(self, builder: &Builder) {
202 let target = self.target;
203 let name = self.name;
205 let out = builder.doc_out(target);
206 t!(fs::create_dir_all(&out));
208 let out = out.join(name);
209 let src = src.join(name);
210 let index = out.join("index.html");
211 let rustbook = builder.tool_exe(Tool::Rustbook);
212 let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
213 if up_to_date(&src, &index) && up_to_date(&rustbook, &index) {
216 builder.info(&format!("Rustbook ({}) - {}", target, name));
217 let _ = fs::remove_dir_all(&out);
218 builder.run(rustbook_cmd
226 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
229 target: Interned<String>,
233 impl Step for TheBook {
235 const DEFAULT: bool = true;
237 fn should_run(run: ShouldRun) -> ShouldRun {
238 let builder = run.builder;
239 run.path("src/doc/book").default_condition(builder.config.docs)
242 fn make_run(run: RunConfig) {
243 run.builder.ensure(TheBook {
244 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
250 /// Build the book and associated stuff.
252 /// We need to build:
254 /// * Book (first edition)
255 /// * Book (second edition)
256 /// * Version info and CSS
259 fn run(self, builder: &Builder) {
260 let compiler = self.compiler;
261 let target = self.target;
262 let name = self.name;
263 // build book first edition
264 builder.ensure(Rustbook {
266 name: INTERNER.intern_string(format!("{}/first-edition", name)),
269 // build book second edition
270 builder.ensure(Rustbook {
272 name: INTERNER.intern_string(format!("{}/second-edition", name)),
275 // build the version info page and CSS
276 builder.ensure(Standalone {
281 // build the index page
282 let index = format!("{}/index.md", name);
283 builder.info(&format!("Documenting book index ({})", target));
284 invoke_rustdoc(builder, compiler, target, &index);
286 // build the redirect pages
287 builder.info(&format!("Documenting book redirect pages ({})", target));
288 for file in t!(fs::read_dir(builder.src.join("src/doc/book/redirects"))) {
290 let path = file.path();
291 let path = path.to_str().unwrap();
293 invoke_rustdoc(builder, compiler, target, path);
298 fn invoke_rustdoc(builder: &Builder, compiler: Compiler, target: Interned<String>, markdown: &str) {
299 let out = builder.doc_out(target);
301 let path = builder.src.join("src/doc").join(markdown);
303 let favicon = builder.src.join("src/doc/favicon.inc");
304 let footer = builder.src.join("src/doc/footer.inc");
305 let version_info = out.join("version_info.html");
307 let mut cmd = builder.rustdoc_cmd(compiler.host);
309 let out = out.join("book");
311 cmd.arg("--html-after-content").arg(&footer)
312 .arg("--html-before-content").arg(&version_info)
313 .arg("--html-in-header").arg(&favicon)
314 .arg("--markdown-no-toc")
315 .arg("--markdown-playground-url")
316 .arg("https://play.rust-lang.org/")
319 .arg("--markdown-css")
322 builder.run(&mut cmd);
325 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
326 pub struct Standalone {
328 target: Interned<String>,
331 impl Step for Standalone {
333 const DEFAULT: bool = true;
335 fn should_run(run: ShouldRun) -> ShouldRun {
336 let builder = run.builder;
337 run.path("src/doc").default_condition(builder.config.docs)
340 fn make_run(run: RunConfig) {
341 run.builder.ensure(Standalone {
342 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
347 /// Generates all standalone documentation as compiled by the rustdoc in `stage`
348 /// for the `target` into `out`.
350 /// This will list all of `src/doc` looking for markdown files and appropriately
351 /// perform transformations like substituting `VERSION`, `SHORT_HASH`, and
352 /// `STAMP` along with providing the various header/footer HTML we've customized.
354 /// In the end, this is just a glorified wrapper around rustdoc!
355 fn run(self, builder: &Builder) {
356 let target = self.target;
357 let compiler = self.compiler;
358 builder.info(&format!("Documenting standalone ({})", target));
359 let out = builder.doc_out(target);
360 t!(fs::create_dir_all(&out));
362 let favicon = builder.src.join("src/doc/favicon.inc");
363 let footer = builder.src.join("src/doc/footer.inc");
364 let full_toc = builder.src.join("src/doc/full-toc.inc");
365 t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
367 let version_input = builder.src.join("src/doc/version_info.html.template");
368 let version_info = out.join("version_info.html");
370 if !builder.config.dry_run && !up_to_date(&version_input, &version_info) {
371 let mut info = String::new();
372 t!(t!(File::open(&version_input)).read_to_string(&mut info));
373 let info = info.replace("VERSION", &builder.rust_release())
374 .replace("SHORT_HASH", builder.rust_info.sha_short().unwrap_or(""))
375 .replace("STAMP", builder.rust_info.sha().unwrap_or(""));
376 t!(t!(File::create(&version_info)).write_all(info.as_bytes()));
379 for file in t!(fs::read_dir(builder.src.join("src/doc"))) {
381 let path = file.path();
382 let filename = path.file_name().unwrap().to_str().unwrap();
383 if !filename.ends_with(".md") || filename == "README.md" {
387 let html = out.join(filename).with_extension("html");
388 let rustdoc = builder.rustdoc(compiler.host);
389 if up_to_date(&path, &html) &&
390 up_to_date(&footer, &html) &&
391 up_to_date(&favicon, &html) &&
392 up_to_date(&full_toc, &html) &&
393 up_to_date(&version_info, &html) &&
394 (builder.config.dry_run || up_to_date(&rustdoc, &html)) {
398 let mut cmd = builder.rustdoc_cmd(compiler.host);
399 cmd.arg("--html-after-content").arg(&footer)
400 .arg("--html-before-content").arg(&version_info)
401 .arg("--html-in-header").arg(&favicon)
402 .arg("--markdown-playground-url")
403 .arg("https://play.rust-lang.org/")
407 if filename == "not_found.md" {
408 cmd.arg("--markdown-no-toc")
409 .arg("--markdown-css")
410 .arg("https://doc.rust-lang.org/rust.css");
412 cmd.arg("--markdown-css").arg("rust.css");
414 builder.run(&mut cmd);
419 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
422 pub target: Interned<String>,
427 const DEFAULT: bool = true;
429 fn should_run(run: ShouldRun) -> ShouldRun {
430 let builder = run.builder;
431 run.all_krates("std").default_condition(builder.config.docs)
434 fn make_run(run: RunConfig) {
435 run.builder.ensure(Std {
436 stage: run.builder.top_stage,
441 /// Compile all standard library documentation.
443 /// This will generate all documentation for the standard library and its
444 /// dependencies. This is largely just a wrapper around `cargo doc`.
445 fn run(self, builder: &Builder) {
446 let stage = self.stage;
447 let target = self.target;
448 builder.info(&format!("Documenting stage{} std ({})", stage, target));
449 let out = builder.doc_out(target);
450 t!(fs::create_dir_all(&out));
451 let compiler = builder.compiler(stage, builder.config.build);
452 let rustdoc = builder.rustdoc(compiler.host);
453 let compiler = if builder.force_use_stage1(compiler, target) {
454 builder.compiler(1, compiler.host)
459 builder.ensure(compile::Std { compiler, target });
460 let out_dir = builder.stage_out(compiler, Mode::Libstd)
461 .join(target).join("doc");
463 // Here what we're doing is creating a *symlink* (directory junction on
464 // Windows) to the final output location. This is not done as an
465 // optimization but rather for correctness. We've got three trees of
466 // documentation, one for std, one for test, and one for rustc. It's then
467 // our job to merge them all together.
469 // Unfortunately rustbuild doesn't know nearly as well how to merge doc
470 // trees as rustdoc does itself, so instead of actually having three
471 // separate trees we just have rustdoc output to the same location across
474 // This way rustdoc generates output directly into the output, and rustdoc
475 // will also directly handle merging.
476 let my_out = builder.crate_doc_out(target);
477 builder.clear_if_dirty(&my_out, &rustdoc);
478 t!(symlink_dir_force(&builder.config, &my_out, &out_dir));
480 let mut cargo = builder.cargo(compiler, Mode::Libstd, target, "doc");
481 compile::std_cargo(builder, &compiler, target, &mut cargo);
483 // Keep a whitelist so we do not build internal stdlib crates, these will be
484 // build by the rustc step later if enabled.
485 cargo.arg("--no-deps");
486 for krate in &["alloc", "core", "std", "std_unicode"] {
487 cargo.arg("-p").arg(krate);
488 // Create all crate output directories first to make sure rustdoc uses
490 // FIXME: Cargo should probably do this itself.
491 t!(fs::create_dir_all(out_dir.join(krate)));
494 builder.run(&mut cargo);
495 builder.cp_r(&my_out, &out);
499 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
502 target: Interned<String>,
507 const DEFAULT: bool = true;
509 fn should_run(run: ShouldRun) -> ShouldRun {
510 let builder = run.builder;
511 run.krate("test").default_condition(builder.config.docs)
514 fn make_run(run: RunConfig) {
515 run.builder.ensure(Test {
516 stage: run.builder.top_stage,
521 /// Compile all libtest documentation.
523 /// This will generate all documentation for libtest and its dependencies. This
524 /// is largely just a wrapper around `cargo doc`.
525 fn run(self, builder: &Builder) {
526 let stage = self.stage;
527 let target = self.target;
528 builder.info(&format!("Documenting stage{} test ({})", stage, target));
529 let out = builder.doc_out(target);
530 t!(fs::create_dir_all(&out));
531 let compiler = builder.compiler(stage, builder.config.build);
532 let rustdoc = builder.rustdoc(compiler.host);
533 let compiler = if builder.force_use_stage1(compiler, target) {
534 builder.compiler(1, compiler.host)
539 // Build libstd docs so that we generate relative links
540 builder.ensure(Std { stage, target });
542 builder.ensure(compile::Test { compiler, target });
543 let out_dir = builder.stage_out(compiler, Mode::Libtest)
544 .join(target).join("doc");
546 // See docs in std above for why we symlink
547 let my_out = builder.crate_doc_out(target);
548 builder.clear_if_dirty(&my_out, &rustdoc);
549 t!(symlink_dir_force(&builder.config, &my_out, &out_dir));
551 let mut cargo = builder.cargo(compiler, Mode::Libtest, target, "doc");
552 compile::test_cargo(builder, &compiler, target, &mut cargo);
554 cargo.arg("--no-deps").arg("-p").arg("test");
556 builder.run(&mut cargo);
557 builder.cp_r(&my_out, &out);
561 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
562 pub struct WhitelistedRustc {
564 target: Interned<String>,
567 impl Step for WhitelistedRustc {
569 const DEFAULT: bool = true;
570 const ONLY_HOSTS: bool = true;
572 fn should_run(run: ShouldRun) -> ShouldRun {
573 let builder = run.builder;
574 run.krate("rustc-main").default_condition(builder.config.docs)
577 fn make_run(run: RunConfig) {
578 run.builder.ensure(WhitelistedRustc {
579 stage: run.builder.top_stage,
584 /// Generate whitelisted compiler crate documentation.
586 /// This will generate all documentation for crates that are whitelisted
587 /// to be included in the standard documentation. This documentation is
588 /// included in the standard Rust documentation, so we should always
589 /// document it and symlink to merge with the rest of the std and test
590 /// documentation. We don't build other compiler documentation
591 /// here as we want to be able to keep it separate from the standard
592 /// documentation. This is largely just a wrapper around `cargo doc`.
593 fn run(self, builder: &Builder) {
594 let stage = self.stage;
595 let target = self.target;
596 builder.info(&format!("Documenting stage{} whitelisted compiler ({})", stage, target));
597 let out = builder.doc_out(target);
598 t!(fs::create_dir_all(&out));
599 let compiler = builder.compiler(stage, builder.config.build);
600 let rustdoc = builder.rustdoc(compiler.host);
601 let compiler = if builder.force_use_stage1(compiler, target) {
602 builder.compiler(1, compiler.host)
607 // Build libstd docs so that we generate relative links
608 builder.ensure(Std { stage, target });
610 builder.ensure(compile::Rustc { compiler, target });
611 let out_dir = builder.stage_out(compiler, Mode::Librustc)
612 .join(target).join("doc");
614 // See docs in std above for why we symlink
615 let my_out = builder.crate_doc_out(target);
616 builder.clear_if_dirty(&my_out, &rustdoc);
617 t!(symlink_dir_force(&builder.config, &my_out, &out_dir));
619 let mut cargo = builder.cargo(compiler, Mode::Librustc, target, "doc");
620 compile::rustc_cargo(builder, &mut cargo);
622 // We don't want to build docs for internal compiler dependencies in this
623 // step (there is another step for that). Therefore, we whitelist the crates
624 // for which docs must be built.
625 cargo.arg("--no-deps");
626 for krate in &["proc_macro"] {
627 cargo.arg("-p").arg(krate);
630 builder.run(&mut cargo);
631 builder.cp_r(&my_out, &out);
635 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
638 target: Interned<String>,
641 impl Step for Rustc {
643 const DEFAULT: bool = true;
644 const ONLY_HOSTS: bool = true;
646 fn should_run(run: ShouldRun) -> ShouldRun {
647 let builder = run.builder;
648 run.krate("rustc-main").default_condition(builder.config.docs)
651 fn make_run(run: RunConfig) {
652 run.builder.ensure(Rustc {
653 stage: run.builder.top_stage,
658 /// Generate compiler documentation.
660 /// This will generate all documentation for compiler and dependencies.
661 /// Compiler documentation is distributed separately, so we make sure
662 /// we do not merge it with the other documentation from std, test and
663 /// proc_macros. This is largely just a wrapper around `cargo doc`.
664 fn run(self, builder: &Builder) {
665 let stage = self.stage;
666 let target = self.target;
667 builder.info(&format!("Documenting stage{} compiler ({})", stage, target));
668 let out = builder.compiler_doc_out(target);
669 t!(fs::create_dir_all(&out));
670 let compiler = builder.compiler(stage, builder.config.build);
671 let rustdoc = builder.rustdoc(compiler.host);
672 let compiler = if builder.force_use_stage1(compiler, target) {
673 builder.compiler(1, compiler.host)
678 if !builder.config.compiler_docs {
679 builder.info(&format!("\tskipping - compiler docs disabled"));
683 // Build libstd docs so that we generate relative links
684 builder.ensure(Std { stage, target });
686 builder.ensure(compile::Rustc { compiler, target });
687 let out_dir = builder.stage_out(compiler, Mode::Librustc)
688 .join(target).join("doc");
689 // We do not symlink to the same shared folder that already contains std library
690 // documentation from previous steps as we do not want to include that.
691 builder.clear_if_dirty(&out, &rustdoc);
692 t!(symlink_dir_force(&builder.config, &out, &out_dir));
694 let mut cargo = builder.cargo(compiler, Mode::Librustc, target, "doc");
695 cargo.env("RUSTDOCFLAGS", "--document-private-items");
696 compile::rustc_cargo(builder, &mut cargo);
698 // Only include compiler crates, no dependencies of those, such as `libc`.
699 cargo.arg("--no-deps");
701 // Find dependencies for top level crates.
702 let mut compiler_crates = HashSet::new();
703 for root_crate in &["rustc", "rustc_driver"] {
704 let interned_root_crate = INTERNER.intern_str(root_crate);
705 find_compiler_crates(builder, &interned_root_crate, &mut compiler_crates);
708 for krate in &compiler_crates {
709 cargo.arg("-p").arg(krate);
712 builder.run(&mut cargo);
716 fn find_compiler_crates(
718 name: &Interned<String>,
719 crates: &mut HashSet<Interned<String>>
721 // Add current crate.
722 crates.insert(*name);
724 // Look for dependencies.
725 for dep in builder.crates.get(name).unwrap().deps.iter() {
726 if builder.crates.get(dep).unwrap().is_local(builder) {
727 find_compiler_crates(builder, dep, crates);
732 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
733 pub struct ErrorIndex {
734 target: Interned<String>,
737 impl Step for ErrorIndex {
739 const DEFAULT: bool = true;
740 const ONLY_HOSTS: bool = true;
742 fn should_run(run: ShouldRun) -> ShouldRun {
743 let builder = run.builder;
744 run.path("src/tools/error_index_generator").default_condition(builder.config.docs)
747 fn make_run(run: RunConfig) {
748 run.builder.ensure(ErrorIndex {
753 /// Generates the HTML rendered error-index by running the
754 /// `error_index_generator` tool.
755 fn run(self, builder: &Builder) {
756 let target = self.target;
758 builder.info(&format!("Documenting error index ({})", target));
759 let out = builder.doc_out(target);
760 t!(fs::create_dir_all(&out));
761 let mut index = builder.tool_cmd(Tool::ErrorIndex);
763 index.arg(out.join("error-index.html"));
765 // FIXME: shouldn't have to pass this env var
766 index.env("CFG_BUILD", &builder.config.build)
767 .env("RUSTC_ERROR_METADATA_DST", builder.extended_error_dir());
769 builder.run(&mut index);
773 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
774 pub struct UnstableBookGen {
775 target: Interned<String>,
778 impl Step for UnstableBookGen {
780 const DEFAULT: bool = true;
781 const ONLY_HOSTS: bool = true;
783 fn should_run(run: ShouldRun) -> ShouldRun {
784 let builder = run.builder;
785 run.path("src/tools/unstable-book-gen").default_condition(builder.config.docs)
788 fn make_run(run: RunConfig) {
789 run.builder.ensure(UnstableBookGen {
794 fn run(self, builder: &Builder) {
795 let target = self.target;
797 builder.ensure(compile::Std {
798 compiler: builder.compiler(builder.top_stage, builder.config.build),
802 builder.info(&format!("Generating unstable book md files ({})", target));
803 let out = builder.md_doc_out(target).join("unstable-book");
804 builder.create_dir(&out);
805 builder.remove_dir(&out);
806 let mut cmd = builder.tool_cmd(Tool::UnstableBookGen);
807 cmd.arg(builder.src.join("src"));
810 builder.run(&mut cmd);
814 fn symlink_dir_force(config: &Config, src: &Path, dst: &Path) -> io::Result<()> {
818 if let Ok(m) = fs::symlink_metadata(dst) {
819 if m.file_type().is_dir() {
820 try!(fs::remove_dir_all(dst));
822 // handle directory junctions on windows by falling back to
824 try!(fs::remove_file(dst).or_else(|_| {
830 symlink_dir(config, src, dst)