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(box_patterns)]
7 #![feature(box_syntax)]
8 #![feature(in_band_lifetimes)]
10 #![feature(or_patterns)]
12 #![feature(crate_visibility_modifier)]
13 #![feature(never_type)]
14 #![feature(once_cell)]
15 #![recursion_limit = "256"]
18 extern crate lazy_static;
19 extern crate rustc_ast;
20 extern crate rustc_ast_pretty;
21 extern crate rustc_attr;
22 extern crate rustc_data_structures;
23 extern crate rustc_driver;
24 extern crate rustc_errors;
25 extern crate rustc_expand;
26 extern crate rustc_feature;
27 extern crate rustc_hir;
28 extern crate rustc_hir_pretty;
29 extern crate rustc_index;
30 extern crate rustc_infer;
31 extern crate rustc_interface;
32 extern crate rustc_lexer;
33 extern crate rustc_lint;
34 extern crate rustc_metadata;
35 extern crate rustc_middle;
36 extern crate rustc_mir;
37 extern crate rustc_parse;
38 extern crate rustc_resolve;
39 extern crate rustc_session;
40 extern crate rustc_span as rustc_span;
41 extern crate rustc_target;
42 extern crate rustc_trait_selection;
43 extern crate rustc_typeck;
44 extern crate test as testing;
48 use std::default::Default;
52 use rustc_errors::ErrorReported;
53 use rustc_session::config::{make_crate_type_option, ErrorOutputType, RustcOptGroup};
54 use rustc_session::getopts;
55 use rustc_session::{early_error, early_warn};
80 renderinfo: config::RenderInfo,
81 renderopts: config::RenderOptions,
85 rustc_driver::set_sigpipe_handler();
86 rustc_driver::install_ice_hook();
87 rustc_driver::init_env_logger("RUSTDOC_LOG");
88 let exit_code = rustc_driver::catch_with_exit_code(|| match get_args() {
89 Some(args) => main_args(&args),
90 _ => Err(ErrorReported),
92 process::exit(exit_code);
95 fn get_args() -> Option<Vec<String>> {
102 ErrorOutputType::default(),
103 &format!("Argument {} is not valid Unicode: {:?}", i, arg),
111 fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
113 F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static,
115 RustcOptGroup::stable(name, f)
118 fn unstable<F>(name: &'static str, f: F) -> RustcOptGroup
120 F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static,
122 RustcOptGroup::unstable(name, f)
125 fn opts() -> Vec<RustcOptGroup> {
127 stable("h", |o| o.optflag("h", "help", "show this help message")),
128 stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
129 stable("v", |o| o.optflag("v", "verbose", "use verbose output")),
131 o.optopt("r", "input-format", "the input type of the specified file", "[rust]")
133 stable("w", |o| 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", "DIR")
142 stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
143 stable("extern", |o| o.optmulti("", "extern", "pass an --extern to rustc", "NAME[=PATH]")),
144 unstable("extern-html-root-url", |o| {
145 o.optmulti("", "extern-html-root-url", "base URL to use for dependencies", "NAME=URL")
147 stable("plugin-path", |o| o.optmulti("", "plugin-path", "removed", "DIR")),
149 o.optmulti("C", "codegen", "pass a codegen option to rustc", "OPT[=VALUE]")
151 stable("passes", |o| {
155 "list of passes to also run, you might want to pass it multiple times; a value of \
156 `list` will print available passes",
160 stable("plugins", |o| o.optmulti("", "plugins", "removed", "PLUGINS")),
161 stable("no-default", |o| o.optflag("", "no-defaults", "don't run the default passes")),
162 stable("document-private-items", |o| {
163 o.optflag("", "document-private-items", "document private items")
165 unstable("document-hidden-items", |o| {
166 o.optflag("", "document-hidden-items", "document items that have doc(hidden)")
168 stable("test", |o| o.optflag("", "test", "run code examples as tests")),
169 stable("test-args", |o| {
170 o.optmulti("", "test-args", "arguments to pass to the test runner", "ARGS")
172 stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
173 stable("markdown-css", |o| {
177 "CSS files to include via <link> in a rendered Markdown file",
181 stable("html-in-header", |o| {
185 "files to include inline in the <head> section of a rendered Markdown file \
186 or generated documentation",
190 stable("html-before-content", |o| {
193 "html-before-content",
194 "files to include inline between <body> and the content of a rendered \
195 Markdown file or generated documentation",
199 stable("html-after-content", |o| {
202 "html-after-content",
203 "files to include inline between the content and </body> of a rendered \
204 Markdown file or generated documentation",
208 unstable("markdown-before-content", |o| {
211 "markdown-before-content",
212 "files to include inline between <body> and the content of a rendered \
213 Markdown file or generated documentation",
217 unstable("markdown-after-content", |o| {
220 "markdown-after-content",
221 "files to include inline between the content and </body> of a rendered \
222 Markdown file or generated documentation",
226 stable("markdown-playground-url", |o| {
227 o.optopt("", "markdown-playground-url", "URL to send code snippets to", "URL")
229 stable("markdown-no-toc", |o| {
230 o.optflag("", "markdown-no-toc", "don't include table of contents")
236 "To add some CSS rules with a given file to generate doc with your \
237 own theme. However, your theme might break if the rustdoc's generated HTML \
238 changes, so be careful!",
243 o.optmulti("Z", "", "internal and debugging options (only on nightly build)", "FLAG")
245 stable("sysroot", |o| o.optopt("", "sysroot", "Override the system root", "PATH")),
246 unstable("playground-url", |o| {
250 "URL to send code snippets to, may be reset by --markdown-playground-url \
251 or `#![doc(html_playground_url=...)]`",
255 unstable("display-warnings", |o| {
256 o.optflag("", "display-warnings", "to print code warnings when testing doc")
258 stable("crate-version", |o| {
259 o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
261 unstable("sort-modules-by-appearance", |o| {
264 "sort-modules-by-appearance",
265 "sort modules by where they appear in the program, rather than alphabetically",
268 stable("theme", |o| {
272 "additional themes which will be added to the generated docs",
276 stable("check-theme", |o| {
277 o.optmulti("", "check-theme", "check if given theme is valid", "FILES")
279 unstable("resource-suffix", |o| {
283 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
284 \"light-suffix.css\"",
288 stable("edition", |o| {
292 "edition to use when compiling rust code (default: 2015)",
296 stable("color", |o| {
300 "Configure coloring of output:
301 auto = colorize, if output goes to a tty (default);
302 always = always colorize output;
303 never = never colorize output",
307 stable("error-format", |o| {
311 "How errors and other messages are produced",
316 o.optopt("", "json", "Configure the structure of JSON diagnostics", "CONFIG")
318 unstable("disable-minification", |o| {
319 o.optflag("", "disable-minification", "Disable minification applied on JS files")
321 stable("warn", |o| o.optmulti("W", "warn", "Set lint warnings", "OPT")),
322 stable("allow", |o| o.optmulti("A", "allow", "Set lint allowed", "OPT")),
323 stable("deny", |o| o.optmulti("D", "deny", "Set lint denied", "OPT")),
324 stable("forbid", |o| o.optmulti("F", "forbid", "Set lint forbidden", "OPT")),
325 stable("cap-lints", |o| {
329 "Set the most restrictive lint level. \
330 More restrictive lints are capped at this \
331 level. By default, it is at `forbid` level.",
335 unstable("index-page", |o| {
336 o.optopt("", "index-page", "Markdown file to be used as index page", "PATH")
338 unstable("enable-index-page", |o| {
339 o.optflag("", "enable-index-page", "To enable generation of the index page")
341 unstable("static-root-path", |o| {
345 "Path string to force loading static files from in output pages. \
346 If not set, uses combinations of '../' to reach the documentation root.",
350 unstable("disable-per-crate-search", |o| {
353 "disable-per-crate-search",
354 "disables generating the crate selector on the search box",
357 unstable("persist-doctests", |o| {
361 "Directory to persist doctest executables into",
365 unstable("show-coverage", |o| {
369 "calculate percentage of public items with documentation",
372 unstable("enable-per-target-ignores", |o| {
375 "enable-per-target-ignores",
376 "parse ignore-foo for ignoring doctests on a per-target basis",
379 unstable("runtool", |o| {
384 "The tool to run tests with when building for a different target than host",
387 unstable("runtool-arg", |o| {
392 "One (of possibly many) arguments to pass to the runtool",
395 unstable("test-builder", |o| {
399 "specified the rustc-like binary to use as the test builder",
405 fn usage(argv0: &str) {
406 let mut options = getopts::Options::new();
407 for option in opts() {
408 (option.apply)(&mut options);
410 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
413 /// A result type used by several functions under `main()`.
414 type MainResult = Result<(), ErrorReported>;
416 fn main_args(args: &[String]) -> MainResult {
417 let mut options = getopts::Options::new();
418 for option in opts() {
419 (option.apply)(&mut options);
421 let matches = match options.parse(&args[1..]) {
424 early_error(ErrorOutputType::default(), &err.to_string());
428 // Note that we discard any distinction between different non-zero exit
429 // codes from `from_matches` here.
430 let options = match config::Options::from_matches(&matches) {
432 Err(code) => return if code == 0 { Ok(()) } else { Err(ErrorReported) },
434 rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals(
436 1, // this runs single-threaded, even in a parallel compiler
438 move || main_options(options),
442 fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> MainResult {
446 diag.struct_err(&err).emit();
452 fn run_renderer<T: formats::FormatRenderer>(
454 renderopts: config::RenderOptions,
455 render_info: config::RenderInfo,
456 diag: &rustc_errors::Handler,
457 edition: rustc_span::edition::Edition,
459 match formats::run_format::<T>(krate, renderopts, render_info, &diag, edition) {
462 let mut msg = diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
463 let file = e.file.display().to_string();
467 msg.note(&format!("failed to create or modify \"{}\"", file)).emit()
474 fn main_options(options: config::Options) -> MainResult {
475 let diag = core::new_handler(options.error_format, None, &options.debugging_opts);
477 match (options.should_test, options.markdown_input()) {
478 (true, true) => return wrap_return(&diag, markdown::test(options)),
479 (true, false) => return test::run(options),
483 markdown::render(&options.input, options.render_options, options.edition),
489 // need to move these items separately because we lose them by the time the closure is called,
490 // but we can't crates the Handler ahead of time because it's not Send
491 let diag_opts = (options.error_format, options.edition, options.debugging_opts.clone());
492 let show_coverage = options.show_coverage;
494 // First, parse the crate and extract all relevant information.
495 info!("starting to run rustc");
497 // Interpret the input file as a rust source file, passing it through the
498 // compiler all the way through the analysis passes. The rustdoc output is
499 // then generated from the cleaned AST of the crate. This runs all the
500 // plug/cleaning passes.
501 let crate_name = options.crate_name.clone();
502 let crate_version = options.crate_version.clone();
503 let output_format = options.output_format;
504 let (mut krate, renderinfo, renderopts, sess) = core::run_core(options);
506 info!("finished with rustc");
508 if let Some(name) = crate_name {
512 krate.version = crate_version;
514 let out = Output { krate, renderinfo, renderopts };
517 // if we ran coverage, bail early, we don't need to also generate docs at this point
518 // (also we didn't load in any of the useful passes)
522 let Output { krate, renderinfo, renderopts } = out;
523 info!("going to format");
524 let (error_format, edition, debugging_options) = diag_opts;
525 let diag = core::new_handler(error_format, None, &debugging_options);
526 match output_format {
527 None | Some(config::OutputFormat::Html) => sess.time("render_html", || {
528 run_renderer::<html::render::Context>(krate, renderopts, renderinfo, &diag, edition)
530 Some(config::OutputFormat::Json) => sess.time("render_json", || {
531 run_renderer::<json::JsonRenderer>(krate, renderopts, renderinfo, &diag, edition)