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 #![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_index;
27 extern crate rustc_driver;
28 extern crate rustc_resolve;
29 extern crate rustc_lint;
30 extern crate rustc_interface;
31 extern crate rustc_metadata;
32 extern crate rustc_parse;
33 extern crate rustc_target;
34 extern crate rustc_typeck;
35 extern crate rustc_lexer;
36 extern crate serialize;
38 extern crate syntax_expand;
39 extern crate syntax_pos;
40 extern crate test as testing;
41 #[macro_use] extern crate log;
42 extern crate rustc_errors as errors;
44 use std::default::Default;
49 use rustc::session::{early_warn, early_error};
50 use rustc::session::config::{ErrorOutputType, RustcOptGroup, make_crate_type_option};
69 crate mod static_files;
82 renderinfo: html::render::RenderInfo,
83 renderopts: config::RenderOptions,
87 let thread_stack_size: usize = if cfg!(target_os = "haiku") {
88 16_000_000 // 16MB on Haiku
90 32_000_000 // 32MB on other platforms
92 rustc_driver::set_sigpipe_handler();
93 env_logger::init_from_env("RUSTDOC_LOG");
94 let res = std::thread::Builder::new().stack_size(thread_stack_size).spawn(move || {
95 get_args().map(|args| main_args(&args)).unwrap_or(1)
96 }).unwrap().join().unwrap_or(rustc_driver::EXIT_FAILURE);
100 fn get_args() -> Option<Vec<String>> {
101 env::args_os().enumerate()
102 .map(|(i, arg)| arg.into_string().map_err(|arg| {
103 early_warn(ErrorOutputType::default(),
104 &format!("Argument {} is not valid Unicode: {:?}", i, arg));
109 fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
110 where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
112 RustcOptGroup::stable(name, f)
115 fn unstable<F>(name: &'static str, f: F) -> RustcOptGroup
116 where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
118 RustcOptGroup::unstable(name, f)
121 fn opts() -> Vec<RustcOptGroup> {
123 stable("h", |o| o.optflag("h", "help", "show this help message")),
124 stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
125 stable("v", |o| o.optflag("v", "verbose", "use verbose output")),
127 o.optopt("r", "input-format", "the input type of the specified file",
131 o.optopt("w", "output-format", "the output type to write", "[html]")
133 stable("o", |o| o.optopt("o", "output", "where to place the output", "PATH")),
134 stable("crate-name", |o| {
135 o.optopt("", "crate-name", "specify the name of this crate", "NAME")
137 make_crate_type_option(),
139 o.optmulti("L", "library-path", "directory to add to crate search path",
142 stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
143 stable("extern", |o| {
144 o.optmulti("", "extern", "pass an --extern to rustc", "NAME[=PATH]")
146 stable("extern-private", |o| {
147 o.optmulti("", "extern-private",
148 "pass an --extern to rustc (compatibility only)", "NAME=PATH")
150 unstable("extern-html-root-url", |o| {
151 o.optmulti("", "extern-html-root-url",
152 "base URL to use for dependencies", "NAME=URL")
154 stable("plugin-path", |o| {
155 o.optmulti("", "plugin-path", "removed", "DIR")
158 o.optmulti("C", "codegen", "pass a codegen option to rustc", "OPT[=VALUE]")
160 stable("passes", |o| {
161 o.optmulti("", "passes",
162 "list of passes to also run, you might want \
163 to pass it multiple times; a value of `list` \
164 will print available passes",
167 stable("plugins", |o| {
168 o.optmulti("", "plugins", "removed",
171 stable("no-default", |o| {
172 o.optflag("", "no-defaults", "don't run the default passes")
174 stable("document-private-items", |o| {
175 o.optflag("", "document-private-items", "document private items")
177 stable("test", |o| o.optflag("", "test", "run code examples as tests")),
178 stable("test-args", |o| {
179 o.optmulti("", "test-args", "arguments to pass to the test runner",
182 stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
183 stable("markdown-css", |o| {
184 o.optmulti("", "markdown-css",
185 "CSS files to include via <link> in a rendered Markdown file",
188 stable("html-in-header", |o| {
189 o.optmulti("", "html-in-header",
190 "files to include inline in the <head> section of a rendered Markdown file \
191 or generated documentation",
194 stable("html-before-content", |o| {
195 o.optmulti("", "html-before-content",
196 "files to include inline between <body> and the content of a rendered \
197 Markdown file or generated documentation",
200 stable("html-after-content", |o| {
201 o.optmulti("", "html-after-content",
202 "files to include inline between the content and </body> of a rendered \
203 Markdown file or generated documentation",
206 unstable("markdown-before-content", |o| {
207 o.optmulti("", "markdown-before-content",
208 "files to include inline between <body> and the content of a rendered \
209 Markdown file or generated documentation",
212 unstable("markdown-after-content", |o| {
213 o.optmulti("", "markdown-after-content",
214 "files to include inline between the content and </body> of a rendered \
215 Markdown file or generated documentation",
218 stable("markdown-playground-url", |o| {
219 o.optopt("", "markdown-playground-url",
220 "URL to send code snippets to", "URL")
222 stable("markdown-no-toc", |o| {
223 o.optflag("", "markdown-no-toc", "don't include table of contents")
226 o.optopt("e", "extend-css",
227 "To add some CSS rules with a given file to generate doc with your \
228 own theme. However, your theme might break if the rustdoc's generated HTML \
229 changes, so be careful!", "PATH")
233 "internal and debugging options (only on nightly build)", "FLAG")
235 stable("sysroot", |o| {
236 o.optopt("", "sysroot", "Override the system root", "PATH")
238 unstable("playground-url", |o| {
239 o.optopt("", "playground-url",
240 "URL to send code snippets to, may be reset by --markdown-playground-url \
241 or `#![doc(html_playground_url=...)]`",
244 unstable("display-warnings", |o| {
245 o.optflag("", "display-warnings", "to print code warnings when testing doc")
247 unstable("crate-version", |o| {
248 o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
250 unstable("sort-modules-by-appearance", |o| {
251 o.optflag("", "sort-modules-by-appearance", "sort modules by where they appear in the \
252 program, rather than alphabetically")
254 unstable("themes", |o| {
255 o.optmulti("", "themes",
256 "additional themes which will be added to the generated docs",
259 unstable("theme-checker", |o| {
260 o.optmulti("", "theme-checker",
261 "check if given theme is valid",
264 unstable("resource-suffix", |o| {
267 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
268 \"light-suffix.css\"",
271 stable("edition", |o| {
272 o.optopt("", "edition",
273 "edition to use when compiling rust code (default: 2015)",
276 stable("color", |o| {
279 "Configure coloring of output:
280 auto = colorize, if output goes to a tty (default);
281 always = always colorize output;
282 never = never colorize output",
285 stable("error-format", |o| {
288 "How errors and other messages are produced",
294 "Configure the structure of JSON diagnostics",
297 unstable("disable-minification", |o| {
299 "disable-minification",
300 "Disable minification applied on JS files")
303 o.optmulti("W", "warn", "Set lint warnings", "OPT")
305 stable("allow", |o| {
306 o.optmulti("A", "allow", "Set lint allowed", "OPT")
309 o.optmulti("D", "deny", "Set lint denied", "OPT")
311 stable("forbid", |o| {
312 o.optmulti("F", "forbid", "Set lint forbidden", "OPT")
314 stable("cap-lints", |o| {
318 "Set the most restrictive lint level. \
319 More restrictive lints are capped at this \
320 level. By default, it is at `forbid` level.",
324 unstable("index-page", |o| {
327 "Markdown file to be used as index page",
330 unstable("enable-index-page", |o| {
333 "To enable generation of the index page")
335 unstable("static-root-path", |o| {
338 "Path string to force loading static files from in output pages. \
339 If not set, uses combinations of '../' to reach the documentation root.",
342 unstable("disable-per-crate-search", |o| {
344 "disable-per-crate-search",
345 "disables generating the crate selector on the search box")
347 unstable("persist-doctests", |o| {
350 "Directory to persist doctest executables into",
353 unstable("generate-redirect-pages", |o| {
355 "generate-redirect-pages",
356 "Generate extra pages to support legacy URLs and tool links")
358 unstable("show-coverage", |o| {
361 "calculate percentage of public items with documentation")
363 unstable("enable-per-target-ignores", |o| {
365 "enable-per-target-ignores",
366 "parse ignore-foo for ignoring doctests on a per-target basis")
368 unstable("runtool", |o| {
372 "The tool to run tests with when building for a different target than host")
374 unstable("runtool-arg", |o| {
378 "One (of possibly many) arguments to pass to the runtool")
380 unstable("test-builder", |o| {
383 "specified the rustc-like binary to use as the test builder")
388 fn usage(argv0: &str) {
389 let mut options = getopts::Options::new();
390 for option in opts() {
391 (option.apply)(&mut options);
393 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
396 fn main_args(args: &[String]) -> i32 {
397 let mut options = getopts::Options::new();
398 for option in opts() {
399 (option.apply)(&mut options);
401 let matches = match options.parse(&args[1..]) {
404 early_error(ErrorOutputType::default(), &err.to_string());
407 let options = match config::Options::from_matches(&matches) {
409 Err(code) => return code,
411 rustc_interface::interface::default_thread_pool(options.edition, move || {
412 main_options(options)
416 fn main_options(options: config::Options) -> i32 {
417 let diag = core::new_handler(options.error_format,
419 options.debugging_options.treat_err_as_bug,
420 options.debugging_options.ui_testing);
422 match (options.should_test, options.markdown_input()) {
423 (true, true) => return markdown::test(options, &diag),
424 (true, false) => return test::run(options),
425 (false, true) => return markdown::render(options.input,
426 options.render_options,
432 // need to move these items separately because we lose them by the time the closure is called,
433 // but we can't crates the Handler ahead of time because it's not Send
434 let diag_opts = (options.error_format,
435 options.debugging_options.treat_err_as_bug,
436 options.debugging_options.ui_testing,
438 let show_coverage = options.show_coverage;
439 rust_input(options, move |out| {
441 // if we ran coverage, bail early, we don't need to also generate docs at this point
442 // (also we didn't load in any of the useful passes)
443 return rustc_driver::EXIT_SUCCESS;
446 let Output { krate, renderinfo, renderopts } = out;
447 info!("going to format");
448 let (error_format, treat_err_as_bug, ui_testing, edition) = diag_opts;
449 let diag = core::new_handler(error_format, None, treat_err_as_bug, ui_testing);
450 match html::render::run(
457 Ok(_) => rustc_driver::EXIT_SUCCESS,
459 diag.struct_err(&format!("couldn't generate documentation: {}", e.error))
460 .note(&format!("failed to create or modify \"{}\"", e.file.display()))
462 rustc_driver::EXIT_FAILURE
468 /// Interprets the input file as a rust source file, passing it through the
469 /// compiler all the way through the analysis passes. The rustdoc output is then
470 /// generated from the cleaned AST of the crate.
472 /// This form of input will run all of the plug/cleaning passes
473 fn rust_input<R, F>(options: config::Options, f: F) -> R
474 where R: 'static + Send,
475 F: 'static + Send + FnOnce(Output) -> R
477 // First, parse the crate and extract all relevant information.
478 info!("starting to run rustc");
480 let result = rustc_driver::catch_fatal_errors(move || {
481 let crate_name = options.crate_name.clone();
482 let crate_version = options.crate_version.clone();
483 let (mut krate, renderinfo, renderopts) = core::run_core(options);
485 info!("finished with rustc");
487 if let Some(name) = crate_name {
491 krate.version = crate_version;
501 Ok(output) => output,
502 Err(_) => panic::resume_unwind(Box::new(errors::FatalErrorMarker)),