]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/doc.rs
Use markdown::render instead of using pulldown_cmark directly
[rust.git] / src / bootstrap / doc.rs
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.
4 //
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.
10
11 //! Documentation generation for rustbuilder.
12 //!
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.
16 //!
17 //! Everything here is basically just a shim around calling either `rustbook` or
18 //! `rustdoc`.
19
20 use std::collections::HashSet;
21 use std::fs::{self, File};
22 use std::io::prelude::*;
23 use std::io;
24 use std::path::{PathBuf, Path};
25
26 use Mode;
27 use build_helper::up_to_date;
28
29 use util::symlink_dir;
30 use builder::{Builder, Compiler, RunConfig, ShouldRun, Step};
31 use tool::{self, prepare_tool_cargo, Tool, SourceType};
32 use compile;
33 use cache::{INTERNER, Interned};
34 use config::Config;
35
36 macro_rules! book {
37     ($($name:ident, $path:expr, $book_name:expr;)+) => {
38         $(
39             #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
40         pub struct $name {
41             target: Interned<String>,
42         }
43
44         impl Step for $name {
45             type Output = ();
46             const DEFAULT: bool = true;
47
48             fn should_run(run: ShouldRun) -> ShouldRun {
49                 let builder = run.builder;
50                 run.path($path).default_condition(builder.config.docs)
51             }
52
53             fn make_run(run: RunConfig) {
54                 run.builder.ensure($name {
55                     target: run.target,
56                 });
57             }
58
59             fn run(self, builder: &Builder) {
60                 builder.ensure(Rustbook {
61                     target: self.target,
62                     name: INTERNER.intern_str($book_name),
63                 })
64             }
65         }
66         )+
67     }
68 }
69
70 book!(
71     Nomicon, "src/doc/nomicon", "nomicon";
72     Reference, "src/doc/reference", "reference";
73     RustdocBook, "src/doc/rustdoc", "rustdoc";
74     RustcBook, "src/doc/rustc", "rustc";
75     RustByExample, "src/doc/rust-by-example", "rust-by-example";
76 );
77
78 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
79 struct Rustbook {
80     target: Interned<String>,
81     name: Interned<String>,
82 }
83
84 impl Step for Rustbook {
85     type Output = ();
86
87     // rustbook is never directly called, and only serves as a shim for the nomicon and the
88     // reference.
89     fn should_run(run: ShouldRun) -> ShouldRun {
90         run.never()
91     }
92
93     /// Invoke `rustbook` for `target` for the doc book `name`.
94     ///
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 {
100             target: self.target,
101             name: self.name,
102             src: INTERNER.intern_path(src),
103         });
104     }
105 }
106
107 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
108 pub struct UnstableBook {
109     target: Interned<String>,
110 }
111
112 impl Step for UnstableBook {
113     type Output = ();
114     const DEFAULT: bool = true;
115
116     fn should_run(run: ShouldRun) -> ShouldRun {
117         let builder = run.builder;
118         run.path("src/doc/unstable-book").default_condition(builder.config.docs)
119     }
120
121     fn make_run(run: RunConfig) {
122         run.builder.ensure(UnstableBook {
123             target: run.target,
124         });
125     }
126
127     fn run(self, builder: &Builder) {
128         builder.ensure(UnstableBookGen {
129             target: self.target,
130         });
131         builder.ensure(RustbookSrc {
132             target: self.target,
133             name: INTERNER.intern_str("unstable-book"),
134             src: builder.md_doc_out(self.target),
135         })
136     }
137 }
138
139 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
140 pub struct CargoBook {
141     target: Interned<String>,
142     name: Interned<String>,
143 }
144
145 impl Step for CargoBook {
146     type Output = ();
147     const DEFAULT: bool = true;
148
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)
152     }
153
154     fn make_run(run: RunConfig) {
155         run.builder.ensure(CargoBook {
156             target: run.target,
157             name: INTERNER.intern_str("cargo"),
158         });
159     }
160
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");
165
166         let out = builder.doc_out(target);
167         t!(fs::create_dir_all(&out));
168
169         let out = out.join(name);
170
171         builder.info(&format!("Cargo Book ({}) - {}", target, name));
172
173         let _ = fs::remove_dir_all(&out);
174
175         builder.run(builder.tool_cmd(Tool::Rustbook)
176                        .arg("build")
177                        .arg(&src)
178                        .arg("-d")
179                        .arg(out));
180     }
181 }
182
183 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
184 struct RustbookSrc {
185     target: Interned<String>,
186     name: Interned<String>,
187     src: Interned<PathBuf>,
188 }
189
190 impl Step for RustbookSrc {
191     type Output = ();
192
193     fn should_run(run: ShouldRun) -> ShouldRun {
194         run.never()
195     }
196
197     /// Invoke `rustbook` for `target` for the doc book `name` from the `src` path.
198     ///
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;
204         let src = self.src;
205         let out = builder.doc_out(target);
206         t!(fs::create_dir_all(&out));
207
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) {
214             return
215         }
216         builder.info(&format!("Rustbook ({}) - {}", target, name));
217         let _ = fs::remove_dir_all(&out);
218         builder.run(rustbook_cmd
219                        .arg("build")
220                        .arg(&src)
221                        .arg("-d")
222                        .arg(out));
223     }
224 }
225
226 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
227 pub struct TheBook {
228     compiler: Compiler,
229     target: Interned<String>,
230     name: &'static str,
231 }
232
233 impl Step for TheBook {
234     type Output = ();
235     const DEFAULT: bool = true;
236
237     fn should_run(run: ShouldRun) -> ShouldRun {
238         let builder = run.builder;
239         run.path("src/doc/book").default_condition(builder.config.docs)
240     }
241
242     fn make_run(run: RunConfig) {
243         run.builder.ensure(TheBook {
244             compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
245             target: run.target,
246             name: "book",
247         });
248     }
249
250     /// Build the book and associated stuff.
251     ///
252     /// We need to build:
253     ///
254     /// * Book (first edition)
255     /// * Book (second edition)
256     /// * Version info and CSS
257     /// * Index page
258     /// * Redirect pages
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 {
265             target,
266             name: INTERNER.intern_string(format!("{}/first-edition", name)),
267         });
268
269         // build book second edition
270         builder.ensure(Rustbook {
271             target,
272             name: INTERNER.intern_string(format!("{}/second-edition", name)),
273         });
274
275         // build book 2018 edition
276         builder.ensure(Rustbook {
277             target,
278             name: INTERNER.intern_string(format!("{}/2018-edition", name)),
279         });
280
281         // build the version info page and CSS
282         builder.ensure(Standalone {
283             compiler,
284             target,
285         });
286
287         // build the index page
288         let index = format!("{}/index.md", name);
289         builder.info(&format!("Documenting book index ({})", target));
290         invoke_rustdoc(builder, compiler, target, &index);
291
292         // build the redirect pages
293         builder.info(&format!("Documenting book redirect pages ({})", target));
294         for file in t!(fs::read_dir(builder.src.join("src/doc/book/redirects"))) {
295             let file = t!(file);
296             let path = file.path();
297             let path = path.to_str().unwrap();
298
299             invoke_rustdoc(builder, compiler, target, path);
300         }
301     }
302 }
303
304 fn invoke_rustdoc(builder: &Builder, compiler: Compiler, target: Interned<String>, markdown: &str) {
305     let out = builder.doc_out(target);
306
307     let path = builder.src.join("src/doc").join(markdown);
308
309     let favicon = builder.src.join("src/doc/favicon.inc");
310     let footer = builder.src.join("src/doc/footer.inc");
311     let version_info = out.join("version_info.html");
312
313     let mut cmd = builder.rustdoc_cmd(compiler.host);
314
315     let out = out.join("book");
316
317     cmd.arg("--html-after-content").arg(&footer)
318         .arg("--html-before-content").arg(&version_info)
319         .arg("--html-in-header").arg(&favicon)
320         .arg("--markdown-no-toc")
321         .arg("--markdown-playground-url")
322         .arg("https://play.rust-lang.org/")
323         .arg("-o").arg(&out)
324         .arg(&path)
325         .arg("--markdown-css")
326         .arg("../rust.css");
327
328     builder.run(&mut cmd);
329 }
330
331 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
332 pub struct Standalone {
333     compiler: Compiler,
334     target: Interned<String>,
335 }
336
337 impl Step for Standalone {
338     type Output = ();
339     const DEFAULT: bool = true;
340
341     fn should_run(run: ShouldRun) -> ShouldRun {
342         let builder = run.builder;
343         run.path("src/doc").default_condition(builder.config.docs)
344     }
345
346     fn make_run(run: RunConfig) {
347         run.builder.ensure(Standalone {
348             compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
349             target: run.target,
350         });
351     }
352
353     /// Generates all standalone documentation as compiled by the rustdoc in `stage`
354     /// for the `target` into `out`.
355     ///
356     /// This will list all of `src/doc` looking for markdown files and appropriately
357     /// perform transformations like substituting `VERSION`, `SHORT_HASH`, and
358     /// `STAMP` along with providing the various header/footer HTML we've customized.
359     ///
360     /// In the end, this is just a glorified wrapper around rustdoc!
361     fn run(self, builder: &Builder) {
362         let target = self.target;
363         let compiler = self.compiler;
364         builder.info(&format!("Documenting standalone ({})", target));
365         let out = builder.doc_out(target);
366         t!(fs::create_dir_all(&out));
367
368         let favicon = builder.src.join("src/doc/favicon.inc");
369         let footer = builder.src.join("src/doc/footer.inc");
370         let full_toc = builder.src.join("src/doc/full-toc.inc");
371         t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
372
373         let version_input = builder.src.join("src/doc/version_info.html.template");
374         let version_info = out.join("version_info.html");
375
376         if !builder.config.dry_run && !up_to_date(&version_input, &version_info) {
377             let mut info = String::new();
378             t!(t!(File::open(&version_input)).read_to_string(&mut info));
379             let info = info.replace("VERSION", &builder.rust_release())
380                            .replace("SHORT_HASH", builder.rust_info.sha_short().unwrap_or(""))
381                            .replace("STAMP", builder.rust_info.sha().unwrap_or(""));
382             t!(t!(File::create(&version_info)).write_all(info.as_bytes()));
383         }
384
385         for file in t!(fs::read_dir(builder.src.join("src/doc"))) {
386             let file = t!(file);
387             let path = file.path();
388             let filename = path.file_name().unwrap().to_str().unwrap();
389             if !filename.ends_with(".md") || filename == "README.md" {
390                 continue
391             }
392
393             let html = out.join(filename).with_extension("html");
394             let rustdoc = builder.rustdoc(compiler.host);
395             if up_to_date(&path, &html) &&
396                up_to_date(&footer, &html) &&
397                up_to_date(&favicon, &html) &&
398                up_to_date(&full_toc, &html) &&
399                up_to_date(&version_info, &html) &&
400                (builder.config.dry_run || up_to_date(&rustdoc, &html)) {
401                 continue
402             }
403
404             let mut cmd = builder.rustdoc_cmd(compiler.host);
405             cmd.arg("--html-after-content").arg(&footer)
406                .arg("--html-before-content").arg(&version_info)
407                .arg("--html-in-header").arg(&favicon)
408                .arg("--markdown-no-toc")
409                .arg("--index-page").arg(&builder.src.join("src/doc/index.md"))
410                .arg("--markdown-playground-url")
411                .arg("https://play.rust-lang.org/")
412                .arg("-o").arg(&out)
413                .arg(&path);
414
415             if filename == "not_found.md" {
416                 cmd.arg("--markdown-css")
417                    .arg("https://doc.rust-lang.org/rust.css");
418             } else {
419                 cmd.arg("--markdown-css").arg("rust.css");
420             }
421             builder.run(&mut cmd);
422         }
423     }
424 }
425
426 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
427 pub struct Std {
428     pub stage: u32,
429     pub target: Interned<String>,
430 }
431
432 impl Step for Std {
433     type Output = ();
434     const DEFAULT: bool = true;
435
436     fn should_run(run: ShouldRun) -> ShouldRun {
437         let builder = run.builder;
438         run.all_krates("std").default_condition(builder.config.docs)
439     }
440
441     fn make_run(run: RunConfig) {
442         run.builder.ensure(Std {
443             stage: run.builder.top_stage,
444             target: run.target
445         });
446     }
447
448     /// Compile all standard library documentation.
449     ///
450     /// This will generate all documentation for the standard library and its
451     /// dependencies. This is largely just a wrapper around `cargo doc`.
452     fn run(self, builder: &Builder) {
453         let stage = self.stage;
454         let target = self.target;
455         builder.info(&format!("Documenting stage{} std ({})", stage, target));
456         let out = builder.doc_out(target);
457         t!(fs::create_dir_all(&out));
458         let compiler = builder.compiler(stage, builder.config.build);
459         let compiler = if builder.force_use_stage1(compiler, target) {
460             builder.compiler(1, compiler.host)
461         } else {
462             compiler
463         };
464
465         builder.ensure(compile::Std { compiler, target });
466         let out_dir = builder.stage_out(compiler, Mode::Std)
467                            .join(target).join("doc");
468
469         // Here what we're doing is creating a *symlink* (directory junction on
470         // Windows) to the final output location. This is not done as an
471         // optimization but rather for correctness. We've got three trees of
472         // documentation, one for std, one for test, and one for rustc. It's then
473         // our job to merge them all together.
474         //
475         // Unfortunately rustbuild doesn't know nearly as well how to merge doc
476         // trees as rustdoc does itself, so instead of actually having three
477         // separate trees we just have rustdoc output to the same location across
478         // all of them.
479         //
480         // This way rustdoc generates output directly into the output, and rustdoc
481         // will also directly handle merging.
482         let my_out = builder.crate_doc_out(target);
483         t!(symlink_dir_force(&builder.config, &my_out, &out_dir));
484         t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
485
486         let run_cargo_rustdoc_for = |package: &str| {
487             let mut cargo = builder.cargo(compiler, Mode::Std, target, "rustdoc");
488             compile::std_cargo(builder, &compiler, target, &mut cargo);
489
490             // Keep a whitelist so we do not build internal stdlib crates, these will be
491             // build by the rustc step later if enabled.
492             cargo.arg("-Z").arg("unstable-options")
493                  .arg("-p").arg(package);
494             // Create all crate output directories first to make sure rustdoc uses
495             // relative links.
496             // FIXME: Cargo should probably do this itself.
497             t!(fs::create_dir_all(out_dir.join(package)));
498             cargo.arg("--")
499                  .arg("--markdown-css").arg("rust.css")
500                  .arg("--markdown-no-toc")
501                  .arg("--index-page").arg(&builder.src.join("src/doc/index.md"));
502
503             builder.run(&mut cargo);
504             builder.cp_r(&my_out, &out);
505         };
506         for krate in &["alloc", "core", "std"] {
507             run_cargo_rustdoc_for(krate);
508         }
509     }
510 }
511
512 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
513 pub struct Test {
514     stage: u32,
515     target: Interned<String>,
516 }
517
518 impl Step for Test {
519     type Output = ();
520     const DEFAULT: bool = true;
521
522     fn should_run(run: ShouldRun) -> ShouldRun {
523         let builder = run.builder;
524         run.krate("test").default_condition(builder.config.docs)
525     }
526
527     fn make_run(run: RunConfig) {
528         run.builder.ensure(Test {
529             stage: run.builder.top_stage,
530             target: run.target,
531         });
532     }
533
534     /// Compile all libtest documentation.
535     ///
536     /// This will generate all documentation for libtest and its dependencies. This
537     /// is largely just a wrapper around `cargo doc`.
538     fn run(self, builder: &Builder) {
539         let stage = self.stage;
540         let target = self.target;
541         builder.info(&format!("Documenting stage{} test ({})", stage, target));
542         let out = builder.doc_out(target);
543         t!(fs::create_dir_all(&out));
544         let compiler = builder.compiler(stage, builder.config.build);
545         let compiler = if builder.force_use_stage1(compiler, target) {
546             builder.compiler(1, compiler.host)
547         } else {
548             compiler
549         };
550
551         // Build libstd docs so that we generate relative links
552         builder.ensure(Std { stage, target });
553
554         builder.ensure(compile::Test { compiler, target });
555         let out_dir = builder.stage_out(compiler, Mode::Test)
556                            .join(target).join("doc");
557
558         // See docs in std above for why we symlink
559         let my_out = builder.crate_doc_out(target);
560         t!(symlink_dir_force(&builder.config, &my_out, &out_dir));
561
562         let mut cargo = builder.cargo(compiler, Mode::Test, target, "doc");
563         compile::test_cargo(builder, &compiler, target, &mut cargo);
564
565         cargo.arg("--no-deps").arg("-p").arg("test");
566
567         builder.run(&mut cargo);
568         builder.cp_r(&my_out, &out);
569     }
570 }
571
572 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
573 pub struct WhitelistedRustc {
574     stage: u32,
575     target: Interned<String>,
576 }
577
578 impl Step for WhitelistedRustc {
579     type Output = ();
580     const DEFAULT: bool = true;
581     const ONLY_HOSTS: bool = true;
582
583     fn should_run(run: ShouldRun) -> ShouldRun {
584         let builder = run.builder;
585         run.krate("rustc-main").default_condition(builder.config.docs)
586     }
587
588     fn make_run(run: RunConfig) {
589         run.builder.ensure(WhitelistedRustc {
590             stage: run.builder.top_stage,
591             target: run.target,
592         });
593     }
594
595     /// Generate whitelisted compiler crate documentation.
596     ///
597     /// This will generate all documentation for crates that are whitelisted
598     /// to be included in the standard documentation. This documentation is
599     /// included in the standard Rust documentation, so we should always
600     /// document it and symlink to merge with the rest of the std and test
601     /// documentation. We don't build other compiler documentation
602     /// here as we want to be able to keep it separate from the standard
603     /// documentation. This is largely just a wrapper around `cargo doc`.
604     fn run(self, builder: &Builder) {
605         let stage = self.stage;
606         let target = self.target;
607         builder.info(&format!("Documenting stage{} whitelisted compiler ({})", stage, target));
608         let out = builder.doc_out(target);
609         t!(fs::create_dir_all(&out));
610         let compiler = builder.compiler(stage, builder.config.build);
611         let compiler = if builder.force_use_stage1(compiler, target) {
612             builder.compiler(1, compiler.host)
613         } else {
614             compiler
615         };
616
617         // Build libstd docs so that we generate relative links
618         builder.ensure(Std { stage, target });
619
620         builder.ensure(compile::Rustc { compiler, target });
621         let out_dir = builder.stage_out(compiler, Mode::Rustc)
622                            .join(target).join("doc");
623
624         // See docs in std above for why we symlink
625         let my_out = builder.crate_doc_out(target);
626         t!(symlink_dir_force(&builder.config, &my_out, &out_dir));
627
628         let mut cargo = builder.cargo(compiler, Mode::Rustc, target, "doc");
629         compile::rustc_cargo(builder, &mut cargo);
630
631         // We don't want to build docs for internal compiler dependencies in this
632         // step (there is another step for that). Therefore, we whitelist the crates
633         // for which docs must be built.
634         cargo.arg("--no-deps");
635         for krate in &["proc_macro"] {
636             cargo.arg("-p").arg(krate);
637         }
638
639         builder.run(&mut cargo);
640         builder.cp_r(&my_out, &out);
641     }
642 }
643
644 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
645 pub struct Rustc {
646     stage: u32,
647     target: Interned<String>,
648 }
649
650 impl Step for Rustc {
651     type Output = ();
652     const DEFAULT: bool = true;
653     const ONLY_HOSTS: bool = true;
654
655     fn should_run(run: ShouldRun) -> ShouldRun {
656         let builder = run.builder;
657         run.krate("rustc-main").default_condition(builder.config.docs)
658     }
659
660     fn make_run(run: RunConfig) {
661         run.builder.ensure(Rustc {
662             stage: run.builder.top_stage,
663             target: run.target,
664         });
665     }
666
667     /// Generate compiler documentation.
668     ///
669     /// This will generate all documentation for compiler and dependencies.
670     /// Compiler documentation is distributed separately, so we make sure
671     /// we do not merge it with the other documentation from std, test and
672     /// proc_macros. This is largely just a wrapper around `cargo doc`.
673     fn run(self, builder: &Builder) {
674         let stage = self.stage;
675         let target = self.target;
676         builder.info(&format!("Documenting stage{} compiler ({})", stage, target));
677
678         // This is the intended out directory for compiler documentation.
679         let out = builder.compiler_doc_out(target);
680         t!(fs::create_dir_all(&out));
681
682         // Get the correct compiler for this stage.
683         let compiler = builder.compiler(stage, builder.config.build);
684         let compiler = if builder.force_use_stage1(compiler, target) {
685             builder.compiler(1, compiler.host)
686         } else {
687             compiler
688         };
689
690         if !builder.config.compiler_docs {
691             builder.info("\tskipping - compiler/librustdoc docs disabled");
692             return;
693         }
694
695         // Build libstd docs so that we generate relative links.
696         builder.ensure(Std { stage, target });
697
698         // Build rustc.
699         builder.ensure(compile::Rustc { compiler, target });
700
701         // We do not symlink to the same shared folder that already contains std library
702         // documentation from previous steps as we do not want to include that.
703         let out_dir = builder.stage_out(compiler, Mode::Rustc).join(target).join("doc");
704         t!(symlink_dir_force(&builder.config, &out, &out_dir));
705
706         // Build cargo command.
707         let mut cargo = builder.cargo(compiler, Mode::Rustc, target, "doc");
708         cargo.env("RUSTDOCFLAGS", "--document-private-items");
709         compile::rustc_cargo(builder, &mut cargo);
710
711         // Only include compiler crates, no dependencies of those, such as `libc`.
712         cargo.arg("--no-deps");
713
714         // Find dependencies for top level crates.
715         let mut compiler_crates = HashSet::new();
716         for root_crate in &["rustc", "rustc_driver", "rustc_codegen_llvm"] {
717             let interned_root_crate = INTERNER.intern_str(root_crate);
718             find_compiler_crates(builder, &interned_root_crate, &mut compiler_crates);
719         }
720
721         for krate in &compiler_crates {
722             cargo.arg("-p").arg(krate);
723         }
724
725         builder.run(&mut cargo);
726     }
727 }
728
729 fn find_compiler_crates(
730     builder: &Builder,
731     name: &Interned<String>,
732     crates: &mut HashSet<Interned<String>>
733 ) {
734     // Add current crate.
735     crates.insert(*name);
736
737     // Look for dependencies.
738     for dep in builder.crates.get(name).unwrap().deps.iter() {
739         if builder.crates.get(dep).unwrap().is_local(builder) {
740             find_compiler_crates(builder, dep, crates);
741         }
742     }
743 }
744
745 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
746 pub struct Rustdoc {
747     stage: u32,
748     target: Interned<String>,
749 }
750
751 impl Step for Rustdoc {
752     type Output = ();
753     const DEFAULT: bool = true;
754     const ONLY_HOSTS: bool = true;
755
756     fn should_run(run: ShouldRun) -> ShouldRun {
757         run.krate("rustdoc-tool")
758     }
759
760     fn make_run(run: RunConfig) {
761         run.builder.ensure(Rustdoc {
762             stage: run.builder.top_stage,
763             target: run.target,
764         });
765     }
766
767     /// Generate compiler documentation.
768     ///
769     /// This will generate all documentation for compiler and dependencies.
770     /// Compiler documentation is distributed separately, so we make sure
771     /// we do not merge it with the other documentation from std, test and
772     /// proc_macros. This is largely just a wrapper around `cargo doc`.
773     fn run(self, builder: &Builder) {
774         let stage = self.stage;
775         let target = self.target;
776         builder.info(&format!("Documenting stage{} rustdoc ({})", stage, target));
777
778         // This is the intended out directory for compiler documentation.
779         let out = builder.compiler_doc_out(target);
780         t!(fs::create_dir_all(&out));
781
782         // Get the correct compiler for this stage.
783         let compiler = builder.compiler(stage, builder.config.build);
784         let compiler = if builder.force_use_stage1(compiler, target) {
785             builder.compiler(1, compiler.host)
786         } else {
787             compiler
788         };
789
790         if !builder.config.compiler_docs {
791             builder.info("\tskipping - compiler/librustdoc docs disabled");
792             return;
793         }
794
795         // Build libstd docs so that we generate relative links.
796         builder.ensure(Std { stage, target });
797
798         // Build rustdoc.
799         builder.ensure(tool::Rustdoc { host: compiler.host });
800
801         // Symlink compiler docs to the output directory of rustdoc documentation.
802         let out_dir = builder.stage_out(compiler, Mode::ToolRustc)
803             .join(target)
804             .join("doc");
805         t!(fs::create_dir_all(&out_dir));
806         t!(symlink_dir_force(&builder.config, &out, &out_dir));
807
808         // Build cargo command.
809         let mut cargo = prepare_tool_cargo(
810             builder,
811             compiler,
812             Mode::ToolRustc,
813             target,
814             "doc",
815             "src/tools/rustdoc",
816             SourceType::InTree,
817         );
818
819         cargo.env("RUSTDOCFLAGS", "--document-private-items");
820         builder.run(&mut cargo);
821     }
822 }
823
824 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
825 pub struct ErrorIndex {
826     target: Interned<String>,
827 }
828
829 impl Step for ErrorIndex {
830     type Output = ();
831     const DEFAULT: bool = true;
832     const ONLY_HOSTS: bool = true;
833
834     fn should_run(run: ShouldRun) -> ShouldRun {
835         let builder = run.builder;
836         run.path("src/tools/error_index_generator").default_condition(builder.config.docs)
837     }
838
839     fn make_run(run: RunConfig) {
840         run.builder.ensure(ErrorIndex {
841             target: run.target,
842         });
843     }
844
845     /// Generates the HTML rendered error-index by running the
846     /// `error_index_generator` tool.
847     fn run(self, builder: &Builder) {
848         let target = self.target;
849
850         builder.info(&format!("Documenting error index ({})", target));
851         let out = builder.doc_out(target);
852         t!(fs::create_dir_all(&out));
853         let mut index = builder.tool_cmd(Tool::ErrorIndex);
854         index.arg("html");
855         index.arg(out.join("error-index.html"));
856
857         // FIXME: shouldn't have to pass this env var
858         index.env("CFG_BUILD", &builder.config.build)
859              .env("RUSTC_ERROR_METADATA_DST", builder.extended_error_dir());
860
861         builder.run(&mut index);
862     }
863 }
864
865 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
866 pub struct UnstableBookGen {
867     target: Interned<String>,
868 }
869
870 impl Step for UnstableBookGen {
871     type Output = ();
872     const DEFAULT: bool = true;
873     const ONLY_HOSTS: bool = true;
874
875     fn should_run(run: ShouldRun) -> ShouldRun {
876         let builder = run.builder;
877         run.path("src/tools/unstable-book-gen").default_condition(builder.config.docs)
878     }
879
880     fn make_run(run: RunConfig) {
881         run.builder.ensure(UnstableBookGen {
882             target: run.target,
883         });
884     }
885
886     fn run(self, builder: &Builder) {
887         let target = self.target;
888
889         builder.ensure(compile::Std {
890             compiler: builder.compiler(builder.top_stage, builder.config.build),
891             target,
892         });
893
894         builder.info(&format!("Generating unstable book md files ({})", target));
895         let out = builder.md_doc_out(target).join("unstable-book");
896         builder.create_dir(&out);
897         builder.remove_dir(&out);
898         let mut cmd = builder.tool_cmd(Tool::UnstableBookGen);
899         cmd.arg(builder.src.join("src"));
900         cmd.arg(out);
901
902         builder.run(&mut cmd);
903     }
904 }
905
906 fn symlink_dir_force(config: &Config, src: &Path, dst: &Path) -> io::Result<()> {
907     if config.dry_run {
908         return Ok(());
909     }
910     if let Ok(m) = fs::symlink_metadata(dst) {
911         if m.file_type().is_dir() {
912             try!(fs::remove_dir_all(dst));
913         } else {
914             // handle directory junctions on windows by falling back to
915             // `remove_dir`.
916             try!(fs::remove_file(dst).or_else(|_| {
917                 fs::remove_dir(dst)
918             }));
919         }
920     }
921
922     symlink_dir(config, src, dst)
923 }