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