]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/doc.rs
build both editions of the book
[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 rustbuild.
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, standalone documentation, etc.
16 //!
17 //! Everything here is basically just a shim around calling either `rustbook` or
18 //! `rustdoc`.
19
20 use std::fs::{self, File};
21 use std::io::prelude::*;
22 use std::io;
23 use std::path::Path;
24 use std::process::Command;
25
26 use {Build, Compiler, Mode};
27 use util::{cp_r, symlink_dir};
28 use build_helper::up_to_date;
29
30 /// Invoke `rustbook` as compiled in `stage` for `target` for the doc book
31 /// `name` into the `out` path.
32 ///
33 /// This will not actually generate any documentation if the documentation has
34 /// already been generated.
35 pub fn rustbook(build: &Build, target: &str, name: &str) {
36     let out = build.doc_out(target);
37     t!(fs::create_dir_all(&out));
38
39     let out = out.join(name);
40     let compiler = Compiler::new(0, &build.config.build);
41     let src = build.src.join("src/doc").join(name);
42     let index = out.join("index.html");
43     let rustbook = build.tool(&compiler, "rustbook");
44     if up_to_date(&src, &index) && up_to_date(&rustbook, &index) {
45         return
46     }
47     println!("Rustbook ({}) - {}", target, name);
48     let _ = fs::remove_dir_all(&out);
49     build.run(build.tool_cmd(&compiler, "rustbook")
50                    .arg("build")
51                    .arg(&src)
52                    .arg("-d")
53                    .arg(out));
54 }
55
56 pub fn book(build: &Build, target: &str, name: &str) {
57     rustbook(build, target, &format!("{}/first-edition", name));
58     rustbook(build, target, &format!("{}/second-edition", name));
59 }
60
61 /// Generates all standalone documentation as compiled by the rustdoc in `stage`
62 /// for the `target` into `out`.
63 ///
64 /// This will list all of `src/doc` looking for markdown files and appropriately
65 /// perform transformations like substituting `VERSION`, `SHORT_HASH`, and
66 /// `STAMP` alongw ith providing the various header/footer HTML we've cutomized.
67 ///
68 /// In the end, this is just a glorified wrapper around rustdoc!
69 pub fn standalone(build: &Build, target: &str) {
70     println!("Documenting standalone ({})", target);
71     let out = build.doc_out(target);
72     t!(fs::create_dir_all(&out));
73
74     let compiler = Compiler::new(0, &build.config.build);
75
76     let favicon = build.src.join("src/doc/favicon.inc");
77     let footer = build.src.join("src/doc/footer.inc");
78     let full_toc = build.src.join("src/doc/full-toc.inc");
79     t!(fs::copy(build.src.join("src/doc/rust.css"), out.join("rust.css")));
80
81     let version_input = build.src.join("src/doc/version_info.html.template");
82     let version_info = out.join("version_info.html");
83
84     if !up_to_date(&version_input, &version_info) {
85         let mut info = String::new();
86         t!(t!(File::open(&version_input)).read_to_string(&mut info));
87         let info = info.replace("VERSION", &build.rust_release())
88                        .replace("SHORT_HASH", build.rust_info.sha_short().unwrap_or(""))
89                        .replace("STAMP", build.rust_info.sha().unwrap_or(""));
90         t!(t!(File::create(&version_info)).write_all(info.as_bytes()));
91     }
92
93     for file in t!(fs::read_dir(build.src.join("src/doc"))) {
94         let file = t!(file);
95         let path = file.path();
96         let filename = path.file_name().unwrap().to_str().unwrap();
97         if !filename.ends_with(".md") || filename == "README.md" {
98             continue
99         }
100
101         let html = out.join(filename).with_extension("html");
102         let rustdoc = build.rustdoc(&compiler);
103         if up_to_date(&path, &html) &&
104            up_to_date(&footer, &html) &&
105            up_to_date(&favicon, &html) &&
106            up_to_date(&full_toc, &html) &&
107            up_to_date(&version_info, &html) &&
108            up_to_date(&rustdoc, &html) {
109             continue
110         }
111
112         let mut cmd = Command::new(&rustdoc);
113         build.add_rustc_lib_path(&compiler, &mut cmd);
114         cmd.arg("--html-after-content").arg(&footer)
115            .arg("--html-before-content").arg(&version_info)
116            .arg("--html-in-header").arg(&favicon)
117            .arg("--markdown-playground-url")
118            .arg("https://play.rust-lang.org/")
119            .arg("-o").arg(&out)
120            .arg(&path);
121
122         if filename == "not_found.md" {
123             cmd.arg("--markdown-no-toc")
124                .arg("--markdown-css")
125                .arg("https://doc.rust-lang.org/rust.css");
126         } else {
127             cmd.arg("--markdown-css").arg("rust.css");
128         }
129         build.run(&mut cmd);
130     }
131 }
132
133 /// Compile all standard library documentation.
134 ///
135 /// This will generate all documentation for the standard library and its
136 /// dependencies. This is largely just a wrapper around `cargo doc`.
137 pub fn std(build: &Build, stage: u32, target: &str) {
138     println!("Documenting stage{} std ({})", stage, target);
139     let out = build.doc_out(target);
140     t!(fs::create_dir_all(&out));
141     let compiler = Compiler::new(stage, &build.config.build);
142     let compiler = if build.force_use_stage1(&compiler, target) {
143         Compiler::new(1, compiler.host)
144     } else {
145         compiler
146     };
147     let out_dir = build.stage_out(&compiler, Mode::Libstd)
148                        .join(target).join("doc");
149     let rustdoc = build.rustdoc(&compiler);
150
151     // Here what we're doing is creating a *symlink* (directory junction on
152     // Windows) to the final output location. This is not done as an
153     // optimization but rather for correctness. We've got three trees of
154     // documentation, one for std, one for test, and one for rustc. It's then
155     // our job to merge them all together.
156     //
157     // Unfortunately rustbuild doesn't know nearly as well how to merge doc
158     // trees as rustdoc does itself, so instead of actually having three
159     // separate trees we just have rustdoc output to the same location across
160     // all of them.
161     //
162     // This way rustdoc generates output directly into the output, and rustdoc
163     // will also directly handle merging.
164     let my_out = build.crate_doc_out(target);
165     build.clear_if_dirty(&my_out, &rustdoc);
166     t!(symlink_dir_force(&my_out, &out_dir));
167
168     let mut cargo = build.cargo(&compiler, Mode::Libstd, target, "doc");
169     cargo.arg("--manifest-path")
170          .arg(build.src.join("src/libstd/Cargo.toml"))
171          .arg("--features").arg(build.std_features());
172
173     // We don't want to build docs for internal std dependencies unless
174     // in compiler-docs mode. When not in that mode, we whitelist the crates
175     // for which docs must be built.
176     if !build.config.compiler_docs {
177         cargo.arg("--no-deps");
178         for krate in &["alloc", "collections", "core", "std", "std_unicode"] {
179             cargo.arg("-p").arg(krate);
180             // Create all crate output directories first to make sure rustdoc uses
181             // relative links.
182             // FIXME: Cargo should probably do this itself.
183             t!(fs::create_dir_all(out_dir.join(krate)));
184         }
185     }
186
187
188     build.run(&mut cargo);
189     cp_r(&my_out, &out);
190 }
191
192 /// Compile all libtest documentation.
193 ///
194 /// This will generate all documentation for libtest and its dependencies. This
195 /// is largely just a wrapper around `cargo doc`.
196 pub fn test(build: &Build, stage: u32, target: &str) {
197     println!("Documenting stage{} test ({})", stage, target);
198     let out = build.doc_out(target);
199     t!(fs::create_dir_all(&out));
200     let compiler = Compiler::new(stage, &build.config.build);
201     let compiler = if build.force_use_stage1(&compiler, target) {
202         Compiler::new(1, compiler.host)
203     } else {
204         compiler
205     };
206     let out_dir = build.stage_out(&compiler, Mode::Libtest)
207                        .join(target).join("doc");
208     let rustdoc = build.rustdoc(&compiler);
209
210     // See docs in std above for why we symlink
211     let my_out = build.crate_doc_out(target);
212     build.clear_if_dirty(&my_out, &rustdoc);
213     t!(symlink_dir_force(&my_out, &out_dir));
214
215     let mut cargo = build.cargo(&compiler, Mode::Libtest, target, "doc");
216     cargo.arg("--manifest-path")
217          .arg(build.src.join("src/libtest/Cargo.toml"));
218     build.run(&mut cargo);
219     cp_r(&my_out, &out);
220 }
221
222 /// Generate all compiler documentation.
223 ///
224 /// This will generate all documentation for the compiler libraries and their
225 /// dependencies. This is largely just a wrapper around `cargo doc`.
226 pub fn rustc(build: &Build, stage: u32, target: &str) {
227     println!("Documenting stage{} compiler ({})", stage, target);
228     let out = build.doc_out(target);
229     t!(fs::create_dir_all(&out));
230     let compiler = Compiler::new(stage, &build.config.build);
231     let compiler = if build.force_use_stage1(&compiler, target) {
232         Compiler::new(1, compiler.host)
233     } else {
234         compiler
235     };
236     let out_dir = build.stage_out(&compiler, Mode::Librustc)
237                        .join(target).join("doc");
238     let rustdoc = build.rustdoc(&compiler);
239
240     // See docs in std above for why we symlink
241     let my_out = build.crate_doc_out(target);
242     build.clear_if_dirty(&my_out, &rustdoc);
243     t!(symlink_dir_force(&my_out, &out_dir));
244
245     let mut cargo = build.cargo(&compiler, Mode::Librustc, target, "doc");
246     cargo.arg("--manifest-path")
247          .arg(build.src.join("src/rustc/Cargo.toml"))
248          .arg("--features").arg(build.rustc_features());
249
250     if build.config.compiler_docs {
251         // src/rustc/Cargo.toml contains bin crates called rustc and rustdoc
252         // which would otherwise overwrite the docs for the real rustc and
253         // rustdoc lib crates.
254         cargo.arg("-p").arg("rustc_driver")
255              .arg("-p").arg("rustdoc");
256     } else {
257         // Like with libstd above if compiler docs aren't enabled then we're not
258         // documenting internal dependencies, so we have a whitelist.
259         cargo.arg("--no-deps");
260         for krate in &["proc_macro"] {
261             cargo.arg("-p").arg(krate);
262         }
263     }
264
265     build.run(&mut cargo);
266     cp_r(&my_out, &out);
267 }
268
269 /// Generates the HTML rendered error-index by running the
270 /// `error_index_generator` tool.
271 pub fn error_index(build: &Build, target: &str) {
272     println!("Documenting error index ({})", target);
273     let out = build.doc_out(target);
274     t!(fs::create_dir_all(&out));
275     let compiler = Compiler::new(0, &build.config.build);
276     let mut index = build.tool_cmd(&compiler, "error_index_generator");
277     index.arg("html");
278     index.arg(out.join("error-index.html"));
279
280     // FIXME: shouldn't have to pass this env var
281     index.env("CFG_BUILD", &build.config.build);
282
283     build.run(&mut index);
284 }
285
286 fn symlink_dir_force(src: &Path, dst: &Path) -> io::Result<()> {
287     if let Ok(m) = fs::symlink_metadata(dst) {
288         if m.file_type().is_dir() {
289             try!(fs::remove_dir_all(dst));
290         } else {
291             // handle directory junctions on windows by falling back to
292             // `remove_dir`.
293             try!(fs::remove_file(dst).or_else(|_| {
294                 fs::remove_dir(dst)
295             }));
296         }
297     }
298
299     symlink_dir(src, dst)
300 }