1 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
2 html_playground_url = "https://play.rust-lang.org/")]
4 #![feature(rustc_private)]
5 #![feature(arbitrary_self_types)]
6 #![feature(box_patterns)]
7 #![feature(box_syntax)]
8 #![feature(in_band_lifetimes)]
10 #![feature(set_stdio)]
12 #![feature(vec_remove_item)]
13 #![feature(ptr_offset_from)]
14 #![feature(crate_visibility_modifier)]
16 #![feature(drain_filter)]
17 #![cfg_attr(bootstrap, feature(never_type))]
18 #![feature(unicode_internals)]
20 #![recursion_limit="256"]
23 extern crate env_logger;
25 extern crate rustc_data_structures;
26 extern crate rustc_driver;
27 extern crate rustc_error_codes;
28 extern crate rustc_index;
29 extern crate rustc_resolve;
30 extern crate rustc_lint;
31 extern crate rustc_interface;
32 extern crate rustc_metadata;
33 extern crate rustc_parse;
34 extern crate rustc_target;
35 extern crate rustc_typeck;
36 extern crate rustc_lexer;
37 extern crate serialize;
39 extern crate syntax_expand;
40 extern crate syntax_pos;
41 extern crate test as testing;
42 #[macro_use] extern crate log;
43 extern crate rustc_errors as errors;
45 use std::default::Default;
50 use rustc::session::{early_warn, early_error};
51 use rustc::session::config::{ErrorOutputType, RustcOptGroup, make_crate_type_option};
70 crate mod static_files;
83 renderinfo: html::render::RenderInfo,
84 renderopts: config::RenderOptions,
88 let thread_stack_size: usize = if cfg!(target_os = "haiku") {
89 16_000_000 // 16MB on Haiku
91 32_000_000 // 32MB on other platforms
93 rustc_driver::set_sigpipe_handler();
94 env_logger::init_from_env("RUSTDOC_LOG");
95 let res = std::thread::Builder::new().stack_size(thread_stack_size).spawn(move || {
96 get_args().map(|args| main_args(&args)).unwrap_or(1)
97 }).unwrap().join().unwrap_or(rustc_driver::EXIT_FAILURE);
101 fn get_args() -> Option<Vec<String>> {
102 env::args_os().enumerate()
103 .map(|(i, arg)| arg.into_string().map_err(|arg| {
104 early_warn(ErrorOutputType::default(),
105 &format!("Argument {} is not valid Unicode: {:?}", i, arg));
110 fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
111 where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
113 RustcOptGroup::stable(name, f)
116 fn unstable<F>(name: &'static str, f: F) -> RustcOptGroup
117 where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
119 RustcOptGroup::unstable(name, f)
122 fn opts() -> Vec<RustcOptGroup> {
124 stable("h", |o| o.optflag("h", "help", "show this help message")),
125 stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
126 stable("v", |o| o.optflag("v", "verbose", "use verbose output")),
128 o.optopt("r", "input-format", "the input type of the specified file",
132 o.optopt("w", "output-format", "the output type to write", "[html]")
134 stable("o", |o| o.optopt("o", "output", "where to place the output", "PATH")),
135 stable("crate-name", |o| {
136 o.optopt("", "crate-name", "specify the name of this crate", "NAME")
138 make_crate_type_option(),
140 o.optmulti("L", "library-path", "directory to add to crate search path",
143 stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
144 stable("extern", |o| {
145 o.optmulti("", "extern", "pass an --extern to rustc", "NAME[=PATH]")
147 unstable("extern-private", |o| {
148 o.optmulti("", "extern-private",
149 "pass an --extern to rustc (compatibility only)", "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("sort-modules-by-appearance", |o| {
252 o.optflag("", "sort-modules-by-appearance", "sort modules by where they appear in the \
253 program, rather than alphabetically")
255 stable("theme", |o| {
256 o.optmulti("", "theme",
257 "additional themes which will be added to the generated docs",
260 stable("check-theme", |o| {
261 o.optmulti("", "check-theme",
262 "check if given theme is valid",
265 unstable("resource-suffix", |o| {
268 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
269 \"light-suffix.css\"",
272 stable("edition", |o| {
273 o.optopt("", "edition",
274 "edition to use when compiling rust code (default: 2015)",
277 stable("color", |o| {
280 "Configure coloring of output:
281 auto = colorize, if output goes to a tty (default);
282 always = always colorize output;
283 never = never colorize output",
286 stable("error-format", |o| {
289 "How errors and other messages are produced",
295 "Configure the structure of JSON diagnostics",
298 unstable("disable-minification", |o| {
300 "disable-minification",
301 "Disable minification applied on JS files")
304 o.optmulti("W", "warn", "Set lint warnings", "OPT")
306 stable("allow", |o| {
307 o.optmulti("A", "allow", "Set lint allowed", "OPT")
310 o.optmulti("D", "deny", "Set lint denied", "OPT")
312 stable("forbid", |o| {
313 o.optmulti("F", "forbid", "Set lint forbidden", "OPT")
315 stable("cap-lints", |o| {
319 "Set the most restrictive lint level. \
320 More restrictive lints are capped at this \
321 level. By default, it is at `forbid` level.",
325 unstable("index-page", |o| {
328 "Markdown file to be used as index page",
331 unstable("enable-index-page", |o| {
334 "To enable generation of the index page")
336 unstable("static-root-path", |o| {
339 "Path string to force loading static files from in output pages. \
340 If not set, uses combinations of '../' to reach the documentation root.",
343 unstable("disable-per-crate-search", |o| {
345 "disable-per-crate-search",
346 "disables generating the crate selector on the search box")
348 unstable("persist-doctests", |o| {
351 "Directory to persist doctest executables into",
354 unstable("generate-redirect-pages", |o| {
356 "generate-redirect-pages",
357 "Generate extra pages to support legacy URLs and tool links")
359 unstable("show-coverage", |o| {
362 "calculate percentage of public items with documentation")
364 unstable("enable-per-target-ignores", |o| {
366 "enable-per-target-ignores",
367 "parse ignore-foo for ignoring doctests on a per-target basis")
369 unstable("runtool", |o| {
373 "The tool to run tests with when building for a different target than host")
375 unstable("runtool-arg", |o| {
379 "One (of possibly many) arguments to pass to the runtool")
381 unstable("test-builder", |o| {
384 "specified the rustc-like binary to use as the test builder")
389 fn usage(argv0: &str) {
390 let mut options = getopts::Options::new();
391 for option in opts() {
392 (option.apply)(&mut options);
394 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
397 fn main_args(args: &[String]) -> i32 {
398 let mut options = getopts::Options::new();
399 for option in opts() {
400 (option.apply)(&mut options);
402 let matches = match options.parse(&args[1..]) {
405 early_error(ErrorOutputType::default(), &err.to_string());
408 let options = match config::Options::from_matches(&matches) {
410 Err(code) => return code,
412 rustc_interface::interface::default_thread_pool(options.edition, move || {
413 main_options(options)
417 fn main_options(options: config::Options) -> i32 {
418 let diag = core::new_handler(options.error_format,
420 options.debugging_options.treat_err_as_bug,
421 options.debugging_options.ui_testing);
423 match (options.should_test, options.markdown_input()) {
424 (true, true) => return markdown::test(options, &diag),
425 (true, false) => return test::run(options),
426 (false, true) => return markdown::render(options.input,
427 options.render_options,
433 // need to move these items separately because we lose them by the time the closure is called,
434 // but we can't crates the Handler ahead of time because it's not Send
435 let diag_opts = (options.error_format,
436 options.debugging_options.treat_err_as_bug,
437 options.debugging_options.ui_testing,
439 let show_coverage = options.show_coverage;
440 rust_input(options, move |out| {
442 // if we ran coverage, bail early, we don't need to also generate docs at this point
443 // (also we didn't load in any of the useful passes)
444 return rustc_driver::EXIT_SUCCESS;
447 let Output { krate, renderinfo, renderopts } = out;
448 info!("going to format");
449 let (error_format, treat_err_as_bug, ui_testing, edition) = diag_opts;
450 let diag = core::new_handler(error_format, None, treat_err_as_bug, ui_testing);
451 match html::render::run(
458 Ok(_) => rustc_driver::EXIT_SUCCESS,
460 diag.struct_err(&format!("couldn't generate documentation: {}", e.error))
461 .note(&format!("failed to create or modify \"{}\"", e.file.display()))
463 rustc_driver::EXIT_FAILURE
469 /// Interprets the input file as a rust source file, passing it through the
470 /// compiler all the way through the analysis passes. The rustdoc output is then
471 /// generated from the cleaned AST of the crate.
473 /// This form of input will run all of the plug/cleaning passes
474 fn rust_input<R, F>(options: config::Options, f: F) -> R
475 where R: 'static + Send,
476 F: 'static + Send + FnOnce(Output) -> R
478 // First, parse the crate and extract all relevant information.
479 info!("starting to run rustc");
481 let result = rustc_driver::catch_fatal_errors(move || {
482 let crate_name = options.crate_name.clone();
483 let crate_version = options.crate_version.clone();
484 let (mut krate, renderinfo, renderopts) = core::run_core(options);
486 info!("finished with rustc");
488 if let Some(name) = crate_name {
492 krate.version = crate_version;
502 Ok(output) => output,
503 Err(_) => panic::resume_unwind(Box::new(errors::FatalErrorMarker)),