1 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
2 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
3 html_root_url = "https://doc.rust-lang.org/nightly/",
4 html_playground_url = "https://play.rust-lang.org/")]
6 #![feature(bind_by_move_pattern_guards)]
7 #![feature(rustc_private)]
8 #![feature(box_patterns)]
9 #![feature(box_syntax)]
11 #![feature(set_stdio)]
12 #![feature(slice_sort_by_cached_key)]
14 #![feature(vec_remove_item)]
15 #![feature(ptr_offset_from)]
16 #![feature(crate_visibility_modifier)]
18 #![feature(drain_filter)]
19 #![feature(inner_deref)]
21 #![recursion_limit="256"]
25 extern crate env_logger;
27 extern crate rustc_data_structures;
28 extern crate rustc_codegen_utils;
29 extern crate rustc_driver;
30 extern crate rustc_resolve;
31 extern crate rustc_lint;
32 extern crate rustc_metadata;
33 extern crate rustc_target;
34 extern crate rustc_typeck;
35 extern crate serialize;
37 extern crate syntax_pos;
38 extern crate test as testing;
39 #[macro_use] extern crate log;
40 extern crate rustc_errors as errors;
41 extern crate pulldown_cmark;
42 extern crate tempfile;
43 extern crate minifier;
44 extern crate parking_lot;
46 extern crate serialize as rustc_serialize; // used by deriving
48 use std::default::Default;
52 use std::sync::mpsc::channel;
54 use rustc::session::{early_warn, early_error};
55 use rustc::session::config::{ErrorOutputType, RustcOptGroup};
73 crate mod static_files;
85 renderinfo: html::render::RenderInfo,
86 renderopts: config::RenderOptions,
91 let thread_stack_size: usize = if cfg!(target_os = "haiku") {
92 16_000_000 // 16MB on Haiku
94 32_000_000 // 32MB on other platforms
96 rustc_driver::set_sigpipe_handler();
98 let res = std::thread::Builder::new().stack_size(thread_stack_size).spawn(move || {
99 syntax::with_globals(move || {
100 get_args().map(|args| main_args(&args)).unwrap_or(1)
102 }).unwrap().join().unwrap_or(rustc_driver::EXIT_FAILURE);
103 process::exit(res as i32);
106 fn get_args() -> Option<Vec<String>> {
107 env::args_os().enumerate()
108 .map(|(i, arg)| arg.into_string().map_err(|arg| {
109 early_warn(ErrorOutputType::default(),
110 &format!("Argument {} is not valid Unicode: {:?}", i, arg));
115 fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
116 where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
118 RustcOptGroup::stable(name, f)
121 fn unstable<F>(name: &'static str, f: F) -> RustcOptGroup
122 where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
124 RustcOptGroup::unstable(name, f)
127 fn opts() -> Vec<RustcOptGroup> {
129 stable("h", |o| o.optflag("h", "help", "show this help message")),
130 stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
131 stable("v", |o| o.optflag("v", "verbose", "use verbose output")),
133 o.optopt("r", "input-format", "the input type of the specified file",
137 o.optopt("w", "output-format", "the output type to write", "[html]")
139 stable("o", |o| o.optopt("o", "output", "where to place the output", "PATH")),
140 stable("crate-name", |o| {
141 o.optopt("", "crate-name", "specify the name of this crate", "NAME")
144 o.optmulti("L", "library-path", "directory to add to crate search path",
147 stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
148 stable("extern", |o| {
149 o.optmulti("", "extern", "pass an --extern to rustc", "NAME=PATH")
151 unstable("extern-html-root-url", |o| {
152 o.optmulti("", "extern-html-root-url",
153 "base URL to use for dependencies", "NAME=URL")
155 stable("plugin-path", |o| {
156 o.optmulti("", "plugin-path", "removed", "DIR")
159 o.optmulti("C", "codegen", "pass a codegen option to rustc", "OPT[=VALUE]")
161 stable("passes", |o| {
162 o.optmulti("", "passes",
163 "list of passes to also run, you might want \
164 to pass it multiple times; a value of `list` \
165 will print available passes",
168 stable("plugins", |o| {
169 o.optmulti("", "plugins", "removed",
172 stable("no-default", |o| {
173 o.optflag("", "no-defaults", "don't run the default passes")
175 stable("document-private-items", |o| {
176 o.optflag("", "document-private-items", "document private items")
178 stable("test", |o| o.optflag("", "test", "run code examples as tests")),
179 stable("test-args", |o| {
180 o.optmulti("", "test-args", "arguments to pass to the test runner",
183 stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
184 stable("markdown-css", |o| {
185 o.optmulti("", "markdown-css",
186 "CSS files to include via <link> in a rendered Markdown file",
189 stable("html-in-header", |o| {
190 o.optmulti("", "html-in-header",
191 "files to include inline in the <head> section of a rendered Markdown file \
192 or generated documentation",
195 stable("html-before-content", |o| {
196 o.optmulti("", "html-before-content",
197 "files to include inline between <body> and the content of a rendered \
198 Markdown file or generated documentation",
201 stable("html-after-content", |o| {
202 o.optmulti("", "html-after-content",
203 "files to include inline between the content and </body> of a rendered \
204 Markdown file or generated documentation",
207 unstable("markdown-before-content", |o| {
208 o.optmulti("", "markdown-before-content",
209 "files to include inline between <body> and the content of a rendered \
210 Markdown file or generated documentation",
213 unstable("markdown-after-content", |o| {
214 o.optmulti("", "markdown-after-content",
215 "files to include inline between the content and </body> of a rendered \
216 Markdown file or generated documentation",
219 stable("markdown-playground-url", |o| {
220 o.optopt("", "markdown-playground-url",
221 "URL to send code snippets to", "URL")
223 stable("markdown-no-toc", |o| {
224 o.optflag("", "markdown-no-toc", "don't include table of contents")
227 o.optopt("e", "extend-css",
228 "To add some CSS rules with a given file to generate doc with your \
229 own theme. However, your theme might break if the rustdoc's generated HTML \
230 changes, so be careful!", "PATH")
234 "internal and debugging options (only on nightly build)", "FLAG")
236 stable("sysroot", |o| {
237 o.optopt("", "sysroot", "Override the system root", "PATH")
239 unstable("playground-url", |o| {
240 o.optopt("", "playground-url",
241 "URL to send code snippets to, may be reset by --markdown-playground-url \
242 or `#![doc(html_playground_url=...)]`",
245 unstable("display-warnings", |o| {
246 o.optflag("", "display-warnings", "to print code warnings when testing doc")
248 unstable("crate-version", |o| {
249 o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
251 unstable("linker", |o| {
252 o.optopt("", "linker", "linker used for building executable test code", "PATH")
254 unstable("sort-modules-by-appearance", |o| {
255 o.optflag("", "sort-modules-by-appearance", "sort modules by where they appear in the \
256 program, rather than alphabetically")
258 unstable("themes", |o| {
259 o.optmulti("", "themes",
260 "additional themes which will be added to the generated docs",
263 unstable("theme-checker", |o| {
264 o.optmulti("", "theme-checker",
265 "check if given theme is valid",
268 unstable("resource-suffix", |o| {
271 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
272 \"light-suffix.css\"",
275 stable("edition", |o| {
276 o.optopt("", "edition",
277 "edition to use when compiling rust code (default: 2015)",
280 stable("color", |o| {
283 "Configure coloring of output:
284 auto = colorize, if output goes to a tty (default);
285 always = always colorize output;
286 never = never colorize output",
289 stable("error-format", |o| {
292 "How errors and other messages are produced",
295 unstable("disable-minification", |o| {
297 "disable-minification",
298 "Disable minification applied on JS files")
301 o.optmulti("W", "warn", "Set lint warnings", "OPT")
303 stable("allow", |o| {
304 o.optmulti("A", "allow", "Set lint allowed", "OPT")
307 o.optmulti("D", "deny", "Set lint denied", "OPT")
309 stable("forbid", |o| {
310 o.optmulti("F", "forbid", "Set lint forbidden", "OPT")
312 stable("cap-lints", |o| {
316 "Set the most restrictive lint level. \
317 More restrictive lints are capped at this \
318 level. By default, it is at `forbid` level.",
322 unstable("index-page", |o| {
325 "Markdown file to be used as index page",
328 unstable("enable-index-page", |o| {
331 "To enable generation of the index page")
333 unstable("static-root-path", |o| {
336 "Path string to force loading static files from in output pages. \
337 If not set, uses combinations of '../' to reach the documentation root.",
340 unstable("disable-per-crate-search", |o| {
342 "disable-per-crate-search",
343 "disables generating the crate selector on the search box")
345 unstable("persist-doctests", |o| {
348 "Directory to persist doctest executables into",
354 fn usage(argv0: &str) {
355 let mut options = getopts::Options::new();
356 for option in opts() {
357 (option.apply)(&mut options);
359 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
362 fn main_args(args: &[String]) -> isize {
363 let mut options = getopts::Options::new();
364 for option in opts() {
365 (option.apply)(&mut options);
367 let matches = match options.parse(&args[1..]) {
370 early_error(ErrorOutputType::default(), &err.to_string());
373 let options = match config::Options::from_matches(&matches) {
375 Err(code) => return code,
378 let diag = core::new_handler(options.error_format,
380 options.debugging_options.treat_err_as_bug,
381 options.debugging_options.ui_testing);
383 match (options.should_test, options.markdown_input()) {
384 (true, true) => return markdown::test(options, &diag),
385 (true, false) => return test::run(options),
386 (false, true) => return markdown::render(options.input, options.render_options, &diag),
390 // need to move these items separately because we lose them by the time the closure is called,
391 // but we can't crates the Handler ahead of time because it's not Send
392 let diag_opts = (options.error_format,
393 options.debugging_options.treat_err_as_bug,
394 options.debugging_options.ui_testing);
395 rust_input(options, move |out| {
396 let Output { krate, passes, renderinfo, renderopts } = out;
397 info!("going to format");
398 let (error_format, treat_err_as_bug, ui_testing) = diag_opts;
399 let diag = core::new_handler(error_format, None, treat_err_as_bug, ui_testing);
400 match html::render::run(
403 passes.into_iter().collect(),
407 Ok(_) => rustc_driver::EXIT_SUCCESS,
409 diag.struct_err(&format!("couldn't generate documentation: {}", e.error))
410 .note(&format!("failed to create or modify \"{}\"", e.file.display()))
412 rustc_driver::EXIT_FAILURE
418 /// Interprets the input file as a rust source file, passing it through the
419 /// compiler all the way through the analysis passes. The rustdoc output is then
420 /// generated from the cleaned AST of the crate.
422 /// This form of input will run all of the plug/cleaning passes
423 fn rust_input<R, F>(options: config::Options, f: F) -> R
424 where R: 'static + Send,
425 F: 'static + Send + FnOnce(Output) -> R
427 // First, parse the crate and extract all relevant information.
428 info!("starting to run rustc");
430 let (tx, rx) = channel();
432 let result = rustc_driver::monitor(move || syntax::with_globals(move || {
433 let crate_name = options.crate_name.clone();
434 let crate_version = options.crate_version.clone();
435 let (mut krate, renderinfo, renderopts, passes) = core::run_core(options);
437 info!("finished with rustc");
439 if let Some(name) = crate_name {
443 krate.version = crate_version;
445 info!("Executing passes");
447 for pass in &passes {
448 // determine if we know about this pass
449 let pass = match passes::find_pass(pass) {
450 Some(pass) => if let Some(pass) = pass.late_fn() {
453 // not a late pass, but still valid so don't report the error
457 error!("unknown pass {}, skipping", *pass);
469 renderinfo: renderinfo,
476 Ok(()) => rx.recv().unwrap(),
477 Err(_) => panic::resume_unwind(Box::new(errors::FatalErrorMarker)),