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 unstable("default-theme", |o| {
276 "Set the default theme. THEME should be the theme name, generally lowercase. \
277 If an unknown default theme is specified, the builtin default is used. \
278 The set of themes, and the rustdoc built-in default is not stable.",
282 unstable("default-setting", |o| {
286 "Default value for a rustdoc setting (used when \"rustdoc-SETTING\" is absent \
287 from web browser Local Storage). If VALUE is not supplied, \"true\" is used. \
288 Supported SETTINGs and VALUEs are not documented and not stable.",
292 stable("theme", |o| {
296 "additional themes which will be added to the generated docs",
300 stable("check-theme", |o| {
301 o.optmulti("", "check-theme", "check if given theme is valid", "FILES")
303 unstable("resource-suffix", |o| {
307 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
308 \"light-suffix.css\"",
312 stable("edition", |o| {
316 "edition to use when compiling rust code (default: 2015)",
320 stable("color", |o| {
324 "Configure coloring of output:
325 auto = colorize, if output goes to a tty (default);
326 always = always colorize output;
327 never = never colorize output",
331 stable("error-format", |o| {
335 "How errors and other messages are produced",
340 o.optopt("", "json", "Configure the structure of JSON diagnostics", "CONFIG")
342 unstable("disable-minification", |o| {
343 o.optflag("", "disable-minification", "Disable minification applied on JS files")
345 stable("warn", |o| o.optmulti("W", "warn", "Set lint warnings", "OPT")),
346 stable("allow", |o| o.optmulti("A", "allow", "Set lint allowed", "OPT")),
347 stable("deny", |o| o.optmulti("D", "deny", "Set lint denied", "OPT")),
348 stable("forbid", |o| o.optmulti("F", "forbid", "Set lint forbidden", "OPT")),
349 stable("cap-lints", |o| {
353 "Set the most restrictive lint level. \
354 More restrictive lints are capped at this \
355 level. By default, it is at `forbid` level.",
359 unstable("index-page", |o| {
360 o.optopt("", "index-page", "Markdown file to be used as index page", "PATH")
362 unstable("enable-index-page", |o| {
363 o.optflag("", "enable-index-page", "To enable generation of the index page")
365 unstable("static-root-path", |o| {
369 "Path string to force loading static files from in output pages. \
370 If not set, uses combinations of '../' to reach the documentation root.",
374 unstable("disable-per-crate-search", |o| {
377 "disable-per-crate-search",
378 "disables generating the crate selector on the search box",
381 unstable("persist-doctests", |o| {
385 "Directory to persist doctest executables into",
389 unstable("show-coverage", |o| {
393 "calculate percentage of public items with documentation",
396 unstable("enable-per-target-ignores", |o| {
399 "enable-per-target-ignores",
400 "parse ignore-foo for ignoring doctests on a per-target basis",
403 unstable("runtool", |o| {
408 "The tool to run tests with when building for a different target than host",
411 unstable("runtool-arg", |o| {
416 "One (of possibly many) arguments to pass to the runtool",
419 unstable("test-builder", |o| {
423 "specified the rustc-like binary to use as the test builder",
429 fn usage(argv0: &str) {
430 let mut options = getopts::Options::new();
431 for option in opts() {
432 (option.apply)(&mut options);
434 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
437 /// A result type used by several functions under `main()`.
438 type MainResult = Result<(), ErrorReported>;
440 fn main_args(args: &[String]) -> MainResult {
441 let mut options = getopts::Options::new();
442 for option in opts() {
443 (option.apply)(&mut options);
445 let matches = match options.parse(&args[1..]) {
448 early_error(ErrorOutputType::default(), &err.to_string());
452 // Note that we discard any distinction between different non-zero exit
453 // codes from `from_matches` here.
454 let options = match config::Options::from_matches(&matches) {
456 Err(code) => return if code == 0 { Ok(()) } else { Err(ErrorReported) },
458 rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals(
460 1, // this runs single-threaded, even in a parallel compiler
462 move || main_options(options),
466 fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> MainResult {
470 diag.struct_err(&err).emit();
476 fn run_renderer<T: formats::FormatRenderer>(
478 renderopts: config::RenderOptions,
479 render_info: config::RenderInfo,
480 diag: &rustc_errors::Handler,
481 edition: rustc_span::edition::Edition,
483 match formats::run_format::<T>(krate, renderopts, render_info, &diag, edition) {
486 let mut msg = diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
487 let file = e.file.display().to_string();
491 msg.note(&format!("failed to create or modify \"{}\"", file)).emit()
498 fn main_options(options: config::Options) -> MainResult {
499 let diag = core::new_handler(options.error_format, None, &options.debugging_opts);
501 match (options.should_test, options.markdown_input()) {
502 (true, true) => return wrap_return(&diag, markdown::test(options)),
503 (true, false) => return doctest::run(options),
507 markdown::render(&options.input, options.render_options, options.edition),
513 // need to move these items separately because we lose them by the time the closure is called,
514 // but we can't crates the Handler ahead of time because it's not Send
515 let diag_opts = (options.error_format, options.edition, options.debugging_opts.clone());
516 let show_coverage = options.show_coverage;
518 // First, parse the crate and extract all relevant information.
519 info!("starting to run rustc");
521 // Interpret the input file as a rust source file, passing it through the
522 // compiler all the way through the analysis passes. The rustdoc output is
523 // then generated from the cleaned AST of the crate. This runs all the
524 // plug/cleaning passes.
525 let crate_name = options.crate_name.clone();
526 let crate_version = options.crate_version.clone();
527 let output_format = options.output_format;
528 let (mut krate, renderinfo, renderopts, sess) = core::run_core(options);
530 info!("finished with rustc");
532 if let Some(name) = crate_name {
536 krate.version = crate_version;
539 // if we ran coverage, bail early, we don't need to also generate docs at this point
540 // (also we didn't load in any of the useful passes)
544 info!("going to format");
545 let (error_format, edition, debugging_options) = diag_opts;
546 let diag = core::new_handler(error_format, None, &debugging_options);
547 match output_format {
548 None | Some(config::OutputFormat::Html) => sess.time("render_html", || {
549 run_renderer::<html::render::Context>(krate, renderopts, renderinfo, &diag, edition)
551 Some(config::OutputFormat::Json) => sess.time("render_json", || {
552 run_renderer::<json::JsonRenderer>(krate, renderopts, renderinfo, &diag, edition)