2 html_root_url = "https://doc.rust-lang.org/nightly/",
3 html_playground_url = "https://play.rust-lang.org/"
5 #![feature(rustc_private)]
6 #![feature(array_methods)]
7 #![feature(box_patterns)]
8 #![feature(box_syntax)]
9 #![feature(in_band_lifetimes)]
11 #![feature(or_patterns)]
12 #![feature(peekable_next_if)]
14 #![feature(crate_visibility_modifier)]
15 #![feature(never_type)]
16 #![feature(once_cell)]
17 #![recursion_limit = "256"]
20 extern crate lazy_static;
24 // N.B. these need `extern crate` even in 2018 edition
25 // because they're loaded implicitly from the sysroot.
26 // The reason they're loaded from the sysroot is because
27 // the rustdoc artifacts aren't stored in rustc's cargo target directory.
28 // So if `rustc` was specified in Cargo.toml, this would spuriously rebuild crates.
30 // Dependencies listed in Cargo.toml do not need `extern crate`.
31 extern crate rustc_ast;
32 extern crate rustc_ast_pretty;
33 extern crate rustc_attr;
34 extern crate rustc_data_structures;
35 extern crate rustc_driver;
36 extern crate rustc_errors;
37 extern crate rustc_expand;
38 extern crate rustc_feature;
39 extern crate rustc_hir;
40 extern crate rustc_hir_pretty;
41 extern crate rustc_index;
42 extern crate rustc_infer;
43 extern crate rustc_interface;
44 extern crate rustc_lexer;
45 extern crate rustc_lint;
46 extern crate rustc_metadata;
47 extern crate rustc_middle;
48 extern crate rustc_mir;
49 extern crate rustc_parse;
50 extern crate rustc_resolve;
51 extern crate rustc_session;
52 extern crate rustc_span as rustc_span;
53 extern crate rustc_target;
54 extern crate rustc_trait_selection;
55 extern crate rustc_typeck;
56 extern crate test as testing;
58 use std::default::Default;
62 use rustc_errors::ErrorReported;
63 use rustc_session::config::{make_crate_type_option, ErrorOutputType, RustcOptGroup};
64 use rustc_session::getopts;
65 use rustc_session::{early_error, early_warn};
89 rustc_driver::set_sigpipe_handler();
90 rustc_driver::install_ice_hook();
91 rustc_driver::init_env_logger("RUSTDOC_LOG");
92 let exit_code = rustc_driver::catch_with_exit_code(|| match get_args() {
93 Some(args) => main_args(&args),
94 _ => Err(ErrorReported),
96 process::exit(exit_code);
99 fn get_args() -> Option<Vec<String>> {
106 ErrorOutputType::default(),
107 &format!("Argument {} is not valid Unicode: {:?}", i, arg),
115 fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
117 F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static,
119 RustcOptGroup::stable(name, f)
122 fn unstable<F>(name: &'static str, f: F) -> RustcOptGroup
124 F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static,
126 RustcOptGroup::unstable(name, f)
129 fn opts() -> Vec<RustcOptGroup> {
131 stable("h", |o| o.optflag("h", "help", "show this help message")),
132 stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
133 stable("v", |o| o.optflag("v", "verbose", "use verbose output")),
135 o.optopt("r", "input-format", "the input type of the specified file", "[rust]")
137 stable("w", |o| o.optopt("w", "output-format", "the output type to write", "[html]")),
138 stable("o", |o| o.optopt("o", "output", "where to place the output", "PATH")),
139 stable("crate-name", |o| {
140 o.optopt("", "crate-name", "specify the name of this crate", "NAME")
142 make_crate_type_option(),
144 o.optmulti("L", "library-path", "directory to add to crate search path", "DIR")
146 stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
147 stable("extern", |o| o.optmulti("", "extern", "pass an --extern to rustc", "NAME[=PATH]")),
148 unstable("extern-html-root-url", |o| {
149 o.optmulti("", "extern-html-root-url", "base URL to use for dependencies", "NAME=URL")
151 stable("plugin-path", |o| o.optmulti("", "plugin-path", "removed", "DIR")),
153 o.optmulti("C", "codegen", "pass a codegen option to rustc", "OPT[=VALUE]")
155 stable("passes", |o| {
159 "list of passes to also run, you might want to pass it multiple times; a value of \
160 `list` will print available passes",
164 stable("plugins", |o| o.optmulti("", "plugins", "removed", "PLUGINS")),
165 stable("no-default", |o| o.optflag("", "no-defaults", "don't run the default passes")),
166 stable("document-private-items", |o| {
167 o.optflag("", "document-private-items", "document private items")
169 unstable("document-hidden-items", |o| {
170 o.optflag("", "document-hidden-items", "document items that have doc(hidden)")
172 stable("test", |o| o.optflag("", "test", "run code examples as tests")),
173 stable("test-args", |o| {
174 o.optmulti("", "test-args", "arguments to pass to the test runner", "ARGS")
176 stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
177 stable("markdown-css", |o| {
181 "CSS files to include via <link> in a rendered Markdown file",
185 stable("html-in-header", |o| {
189 "files to include inline in the <head> section of a rendered Markdown file \
190 or generated documentation",
194 stable("html-before-content", |o| {
197 "html-before-content",
198 "files to include inline between <body> and the content of a rendered \
199 Markdown file or generated documentation",
203 stable("html-after-content", |o| {
206 "html-after-content",
207 "files to include inline between the content and </body> of a rendered \
208 Markdown file or generated documentation",
212 unstable("markdown-before-content", |o| {
215 "markdown-before-content",
216 "files to include inline between <body> and the content of a rendered \
217 Markdown file or generated documentation",
221 unstable("markdown-after-content", |o| {
224 "markdown-after-content",
225 "files to include inline between the content and </body> of a rendered \
226 Markdown file or generated documentation",
230 stable("markdown-playground-url", |o| {
231 o.optopt("", "markdown-playground-url", "URL to send code snippets to", "URL")
233 stable("markdown-no-toc", |o| {
234 o.optflag("", "markdown-no-toc", "don't include table of contents")
240 "To add some CSS rules with a given file to generate doc with your \
241 own theme. However, your theme might break if the rustdoc's generated HTML \
242 changes, so be careful!",
247 o.optmulti("Z", "", "internal and debugging options (only on nightly build)", "FLAG")
249 stable("sysroot", |o| o.optopt("", "sysroot", "Override the system root", "PATH")),
250 unstable("playground-url", |o| {
254 "URL to send code snippets to, may be reset by --markdown-playground-url \
255 or `#![doc(html_playground_url=...)]`",
259 unstable("display-warnings", |o| {
260 o.optflag("", "display-warnings", "to print code warnings when testing doc")
262 stable("crate-version", |o| {
263 o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
265 unstable("sort-modules-by-appearance", |o| {
268 "sort-modules-by-appearance",
269 "sort modules by where they appear in the program, rather than alphabetically",
272 stable("theme", |o| {
276 "additional themes which will be added to the generated docs",
280 stable("check-theme", |o| {
281 o.optmulti("", "check-theme", "check if given theme is valid", "FILES")
283 unstable("resource-suffix", |o| {
287 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
288 \"light-suffix.css\"",
292 stable("edition", |o| {
296 "edition to use when compiling rust code (default: 2015)",
300 stable("color", |o| {
304 "Configure coloring of output:
305 auto = colorize, if output goes to a tty (default);
306 always = always colorize output;
307 never = never colorize output",
311 stable("error-format", |o| {
315 "How errors and other messages are produced",
320 o.optopt("", "json", "Configure the structure of JSON diagnostics", "CONFIG")
322 unstable("disable-minification", |o| {
323 o.optflag("", "disable-minification", "Disable minification applied on JS files")
325 stable("warn", |o| o.optmulti("W", "warn", "Set lint warnings", "OPT")),
326 stable("allow", |o| o.optmulti("A", "allow", "Set lint allowed", "OPT")),
327 stable("deny", |o| o.optmulti("D", "deny", "Set lint denied", "OPT")),
328 stable("forbid", |o| o.optmulti("F", "forbid", "Set lint forbidden", "OPT")),
329 stable("cap-lints", |o| {
333 "Set the most restrictive lint level. \
334 More restrictive lints are capped at this \
335 level. By default, it is at `forbid` level.",
339 unstable("index-page", |o| {
340 o.optopt("", "index-page", "Markdown file to be used as index page", "PATH")
342 unstable("enable-index-page", |o| {
343 o.optflag("", "enable-index-page", "To enable generation of the index page")
345 unstable("static-root-path", |o| {
349 "Path string to force loading static files from in output pages. \
350 If not set, uses combinations of '../' to reach the documentation root.",
354 unstable("disable-per-crate-search", |o| {
357 "disable-per-crate-search",
358 "disables generating the crate selector on the search box",
361 unstable("persist-doctests", |o| {
365 "Directory to persist doctest executables into",
369 unstable("show-coverage", |o| {
373 "calculate percentage of public items with documentation",
376 unstable("enable-per-target-ignores", |o| {
379 "enable-per-target-ignores",
380 "parse ignore-foo for ignoring doctests on a per-target basis",
383 unstable("runtool", |o| {
388 "The tool to run tests with when building for a different target than host",
391 unstable("runtool-arg", |o| {
396 "One (of possibly many) arguments to pass to the runtool",
399 unstable("test-builder", |o| {
403 "specified the rustc-like binary to use as the test builder",
409 fn usage(argv0: &str) {
410 let mut options = getopts::Options::new();
411 for option in opts() {
412 (option.apply)(&mut options);
414 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
417 /// A result type used by several functions under `main()`.
418 type MainResult = Result<(), ErrorReported>;
420 fn main_args(args: &[String]) -> MainResult {
421 let mut options = getopts::Options::new();
422 for option in opts() {
423 (option.apply)(&mut options);
425 let matches = match options.parse(&args[1..]) {
428 early_error(ErrorOutputType::default(), &err.to_string());
432 // Note that we discard any distinction between different non-zero exit
433 // codes from `from_matches` here.
434 let options = match config::Options::from_matches(&matches) {
436 Err(code) => return if code == 0 { Ok(()) } else { Err(ErrorReported) },
438 rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals(
440 1, // this runs single-threaded, even in a parallel compiler
442 move || main_options(options),
446 fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> MainResult {
450 diag.struct_err(&err).emit();
456 fn run_renderer<T: formats::FormatRenderer>(
458 renderopts: config::RenderOptions,
459 render_info: config::RenderInfo,
460 diag: &rustc_errors::Handler,
461 edition: rustc_span::edition::Edition,
463 match formats::run_format::<T>(krate, renderopts, render_info, &diag, edition) {
466 let mut msg = diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
467 let file = e.file.display().to_string();
471 msg.note(&format!("failed to create or modify \"{}\"", file)).emit()
478 fn main_options(options: config::Options) -> MainResult {
479 let diag = core::new_handler(options.error_format, None, &options.debugging_opts);
481 match (options.should_test, options.markdown_input()) {
482 (true, true) => return wrap_return(&diag, markdown::test(options)),
483 (true, false) => return doctest::run(options),
487 markdown::render(&options.input, options.render_options, options.edition),
493 // need to move these items separately because we lose them by the time the closure is called,
494 // but we can't crates the Handler ahead of time because it's not Send
495 let diag_opts = (options.error_format, options.edition, options.debugging_opts.clone());
496 let show_coverage = options.show_coverage;
498 // First, parse the crate and extract all relevant information.
499 info!("starting to run rustc");
501 // Interpret the input file as a rust source file, passing it through the
502 // compiler all the way through the analysis passes. The rustdoc output is
503 // then generated from the cleaned AST of the crate. This runs all the
504 // plug/cleaning passes.
505 let crate_name = options.crate_name.clone();
506 let crate_version = options.crate_version.clone();
507 let output_format = options.output_format;
508 let (mut krate, renderinfo, renderopts, sess) = core::run_core(options);
510 info!("finished with rustc");
512 if let Some(name) = crate_name {
516 krate.version = crate_version;
519 // if we ran coverage, bail early, we don't need to also generate docs at this point
520 // (also we didn't load in any of the useful passes)
524 info!("going to format");
525 let (error_format, edition, debugging_options) = diag_opts;
526 let diag = core::new_handler(error_format, None, &debugging_options);
527 match output_format {
528 None | Some(config::OutputFormat::Html) => sess.time("render_html", || {
529 run_renderer::<html::render::Context>(krate, renderopts, renderinfo, &diag, edition)
531 Some(config::OutputFormat::Json) => sess.time("render_json", || {
532 run_renderer::<json::JsonRenderer>(krate, renderopts, renderinfo, &diag, edition)