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