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 rustbuild.
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.
17 //! Everything here is basically just a shim around calling either `rustbook` or
20 use std::fs::{self, File};
21 use std::io::prelude::*;
24 use std::process::Command;
26 use {Build, Compiler, Mode};
27 use util::{cp_r, symlink_dir};
28 use build_helper::up_to_date;
30 /// Invoke `rustbook` as compiled in `stage` for `target` for the doc book
31 /// `name` into the `out` path.
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));
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) {
47 println!("Rustbook ({}) - {}", target, name);
48 let _ = fs::remove_dir_all(&out);
49 build.run(build.tool_cmd(&compiler, "rustbook")
56 /// Build the book and associated stuff.
60 /// * Book (first edition)
61 /// * Book (second edition)
64 pub fn book(build: &Build, target: &str, name: &str) {
65 // build book first edition
66 rustbook(build, target, &format!("{}/first-edition", name));
68 // build book second edition
69 rustbook(build, target, &format!("{}/second-edition", name));
71 // build the index page
72 let index = format!("{}/index.md", name);
73 println!("Documenting book index ({})", target);
74 invoke_rustdoc(build, target, &index);
76 // build the redirect pages
77 println!("Documenting book redirect pages ({})", target);
78 for file in t!(fs::read_dir(build.src.join("src/doc/book/redirects"))) {
80 let path = file.path();
81 let path = path.to_str().unwrap();
83 invoke_rustdoc(build, target, path);
87 fn invoke_rustdoc(build: &Build, target: &str, markdown: &str) {
88 let out = build.doc_out(target);
90 let compiler = Compiler::new(0, &build.config.build);
92 let path = build.src.join("src/doc").join(markdown);
94 let rustdoc = build.rustdoc(&compiler);
96 let favicon = build.src.join("src/doc/favicon.inc");
97 let footer = build.src.join("src/doc/footer.inc");
99 let version_input = build.src.join("src/doc/version_info.html.template");
100 let version_info = out.join("version_info.html");
102 if !up_to_date(&version_input, &version_info) {
103 let mut info = String::new();
104 t!(t!(File::open(&version_input)).read_to_string(&mut info));
105 let info = info.replace("VERSION", &build.rust_release())
106 .replace("SHORT_HASH", build.rust_info.sha_short().unwrap_or(""))
107 .replace("STAMP", build.rust_info.sha().unwrap_or(""));
108 t!(t!(File::create(&version_info)).write_all(info.as_bytes()));
111 let mut cmd = Command::new(&rustdoc);
113 build.add_rustc_lib_path(&compiler, &mut cmd);
115 let out = out.join("book");
117 t!(fs::copy(build.src.join("src/doc/rust.css"), out.join("rust.css")));
119 cmd.arg("--html-after-content").arg(&footer)
120 .arg("--html-before-content").arg(&version_info)
121 .arg("--html-in-header").arg(&favicon)
122 .arg("--markdown-playground-url")
123 .arg("https://play.rust-lang.org/")
126 .arg("--markdown-css")
132 /// Generates all standalone documentation as compiled by the rustdoc in `stage`
133 /// for the `target` into `out`.
135 /// This will list all of `src/doc` looking for markdown files and appropriately
136 /// perform transformations like substituting `VERSION`, `SHORT_HASH`, and
137 /// `STAMP` alongw ith providing the various header/footer HTML we've cutomized.
139 /// In the end, this is just a glorified wrapper around rustdoc!
140 pub fn standalone(build: &Build, target: &str) {
141 println!("Documenting standalone ({})", target);
142 let out = build.doc_out(target);
143 t!(fs::create_dir_all(&out));
145 let compiler = Compiler::new(0, &build.config.build);
147 let favicon = build.src.join("src/doc/favicon.inc");
148 let footer = build.src.join("src/doc/footer.inc");
149 let full_toc = build.src.join("src/doc/full-toc.inc");
150 t!(fs::copy(build.src.join("src/doc/rust.css"), out.join("rust.css")));
152 let version_input = build.src.join("src/doc/version_info.html.template");
153 let version_info = out.join("version_info.html");
155 if !up_to_date(&version_input, &version_info) {
156 let mut info = String::new();
157 t!(t!(File::open(&version_input)).read_to_string(&mut info));
158 let info = info.replace("VERSION", &build.rust_release())
159 .replace("SHORT_HASH", build.rust_info.sha_short().unwrap_or(""))
160 .replace("STAMP", build.rust_info.sha().unwrap_or(""));
161 t!(t!(File::create(&version_info)).write_all(info.as_bytes()));
164 for file in t!(fs::read_dir(build.src.join("src/doc"))) {
166 let path = file.path();
167 let filename = path.file_name().unwrap().to_str().unwrap();
168 if !filename.ends_with(".md") || filename == "README.md" {
172 let html = out.join(filename).with_extension("html");
173 let rustdoc = build.rustdoc(&compiler);
174 if up_to_date(&path, &html) &&
175 up_to_date(&footer, &html) &&
176 up_to_date(&favicon, &html) &&
177 up_to_date(&full_toc, &html) &&
178 up_to_date(&version_info, &html) &&
179 up_to_date(&rustdoc, &html) {
183 let mut cmd = Command::new(&rustdoc);
184 build.add_rustc_lib_path(&compiler, &mut cmd);
185 cmd.arg("--html-after-content").arg(&footer)
186 .arg("--html-before-content").arg(&version_info)
187 .arg("--html-in-header").arg(&favicon)
188 .arg("--markdown-playground-url")
189 .arg("https://play.rust-lang.org/")
193 if filename == "not_found.md" {
194 cmd.arg("--markdown-no-toc")
195 .arg("--markdown-css")
196 .arg("https://doc.rust-lang.org/rust.css");
198 cmd.arg("--markdown-css").arg("rust.css");
204 /// Compile all standard library documentation.
206 /// This will generate all documentation for the standard library and its
207 /// dependencies. This is largely just a wrapper around `cargo doc`.
208 pub fn std(build: &Build, stage: u32, target: &str) {
209 println!("Documenting stage{} std ({})", stage, target);
210 let out = build.doc_out(target);
211 t!(fs::create_dir_all(&out));
212 let compiler = Compiler::new(stage, &build.config.build);
213 let compiler = if build.force_use_stage1(&compiler, target) {
214 Compiler::new(1, compiler.host)
218 let out_dir = build.stage_out(&compiler, Mode::Libstd)
219 .join(target).join("doc");
220 let rustdoc = build.rustdoc(&compiler);
222 // Here what we're doing is creating a *symlink* (directory junction on
223 // Windows) to the final output location. This is not done as an
224 // optimization but rather for correctness. We've got three trees of
225 // documentation, one for std, one for test, and one for rustc. It's then
226 // our job to merge them all together.
228 // Unfortunately rustbuild doesn't know nearly as well how to merge doc
229 // trees as rustdoc does itself, so instead of actually having three
230 // separate trees we just have rustdoc output to the same location across
233 // This way rustdoc generates output directly into the output, and rustdoc
234 // will also directly handle merging.
235 let my_out = build.crate_doc_out(target);
236 build.clear_if_dirty(&my_out, &rustdoc);
237 t!(symlink_dir_force(&my_out, &out_dir));
239 let mut cargo = build.cargo(&compiler, Mode::Libstd, target, "doc");
240 cargo.arg("--manifest-path")
241 .arg(build.src.join("src/libstd/Cargo.toml"))
242 .arg("--features").arg(build.std_features());
244 // We don't want to build docs for internal std dependencies unless
245 // in compiler-docs mode. When not in that mode, we whitelist the crates
246 // for which docs must be built.
247 if !build.config.compiler_docs {
248 cargo.arg("--no-deps");
249 for krate in &["alloc", "core", "std", "std_unicode"] {
250 cargo.arg("-p").arg(krate);
251 // Create all crate output directories first to make sure rustdoc uses
253 // FIXME: Cargo should probably do this itself.
254 t!(fs::create_dir_all(out_dir.join(krate)));
259 build.run(&mut cargo);
263 /// Compile all libtest documentation.
265 /// This will generate all documentation for libtest and its dependencies. This
266 /// is largely just a wrapper around `cargo doc`.
267 pub fn test(build: &Build, stage: u32, target: &str) {
268 println!("Documenting stage{} test ({})", stage, target);
269 let out = build.doc_out(target);
270 t!(fs::create_dir_all(&out));
271 let compiler = Compiler::new(stage, &build.config.build);
272 let compiler = if build.force_use_stage1(&compiler, target) {
273 Compiler::new(1, compiler.host)
277 let out_dir = build.stage_out(&compiler, Mode::Libtest)
278 .join(target).join("doc");
279 let rustdoc = build.rustdoc(&compiler);
281 // See docs in std above for why we symlink
282 let my_out = build.crate_doc_out(target);
283 build.clear_if_dirty(&my_out, &rustdoc);
284 t!(symlink_dir_force(&my_out, &out_dir));
286 let mut cargo = build.cargo(&compiler, Mode::Libtest, target, "doc");
287 cargo.arg("--manifest-path")
288 .arg(build.src.join("src/libtest/Cargo.toml"));
289 build.run(&mut cargo);
293 /// Generate all compiler documentation.
295 /// This will generate all documentation for the compiler libraries and their
296 /// dependencies. This is largely just a wrapper around `cargo doc`.
297 pub fn rustc(build: &Build, stage: u32, target: &str) {
298 println!("Documenting stage{} compiler ({})", stage, target);
299 let out = build.doc_out(target);
300 t!(fs::create_dir_all(&out));
301 let compiler = Compiler::new(stage, &build.config.build);
302 let compiler = if build.force_use_stage1(&compiler, target) {
303 Compiler::new(1, compiler.host)
307 let out_dir = build.stage_out(&compiler, Mode::Librustc)
308 .join(target).join("doc");
309 let rustdoc = build.rustdoc(&compiler);
311 // See docs in std above for why we symlink
312 let my_out = build.crate_doc_out(target);
313 build.clear_if_dirty(&my_out, &rustdoc);
314 t!(symlink_dir_force(&my_out, &out_dir));
316 let mut cargo = build.cargo(&compiler, Mode::Librustc, target, "doc");
317 cargo.arg("--manifest-path")
318 .arg(build.src.join("src/rustc/Cargo.toml"))
319 .arg("--features").arg(build.rustc_features());
321 if build.config.compiler_docs {
322 // src/rustc/Cargo.toml contains bin crates called rustc and rustdoc
323 // which would otherwise overwrite the docs for the real rustc and
324 // rustdoc lib crates.
325 cargo.arg("-p").arg("rustc_driver")
326 .arg("-p").arg("rustdoc");
328 // Like with libstd above if compiler docs aren't enabled then we're not
329 // documenting internal dependencies, so we have a whitelist.
330 cargo.arg("--no-deps");
331 for krate in &["proc_macro"] {
332 cargo.arg("-p").arg(krate);
336 build.run(&mut cargo);
340 /// Generates the HTML rendered error-index by running the
341 /// `error_index_generator` tool.
342 pub fn error_index(build: &Build, target: &str) {
343 println!("Documenting error index ({})", target);
344 let out = build.doc_out(target);
345 t!(fs::create_dir_all(&out));
346 let compiler = Compiler::new(0, &build.config.build);
347 let mut index = build.tool_cmd(&compiler, "error_index_generator");
349 index.arg(out.join("error-index.html"));
351 // FIXME: shouldn't have to pass this env var
352 index.env("CFG_BUILD", &build.config.build);
354 build.run(&mut index);
357 fn symlink_dir_force(src: &Path, dst: &Path) -> io::Result<()> {
358 if let Ok(m) = fs::symlink_metadata(dst) {
359 if m.file_type().is_dir() {
360 try!(fs::remove_dir_all(dst));
362 // handle directory junctions on windows by falling back to
364 try!(fs::remove_file(dst).or_else(|_| {
370 symlink_dir(src, dst)