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(ptr_offset_from)]
13 #![feature(crate_visibility_modifier)]
14 #![feature(never_type)]
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;
46 extern crate tracing as log;
48 use std::default::Default;
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 let thread_stack_size: usize = if cfg!(target_os = "haiku") {
86 16_000_000 // 16MB on Haiku
88 32_000_000 // 32MB on other platforms
90 rustc_driver::set_sigpipe_handler();
91 rustc_driver::install_ice_hook();
92 rustc_driver::init_env_logger("RUSTDOC_LOG");
94 let res = std::thread::Builder::new()
95 .stack_size(thread_stack_size)
96 .spawn(move || get_args().map(|args| main_args(&args)).unwrap_or(1))
99 .unwrap_or(rustc_driver::EXIT_FAILURE);
103 fn get_args() -> Option<Vec<String>> {
110 ErrorOutputType::default(),
111 &format!("Argument {} is not valid Unicode: {:?}", i, arg),
119 fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
121 F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static,
123 RustcOptGroup::stable(name, f)
126 fn unstable<F>(name: &'static str, f: F) -> RustcOptGroup
128 F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static,
130 RustcOptGroup::unstable(name, f)
133 fn opts() -> Vec<RustcOptGroup> {
135 stable("h", |o| o.optflag("h", "help", "show this help message")),
136 stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
137 stable("v", |o| o.optflag("v", "verbose", "use verbose output")),
139 o.optopt("r", "input-format", "the input type of the specified file", "[rust]")
141 stable("w", |o| o.optopt("w", "output-format", "the output type to write", "[html]")),
142 stable("o", |o| o.optopt("o", "output", "where to place the output", "PATH")),
143 stable("crate-name", |o| {
144 o.optopt("", "crate-name", "specify the name of this crate", "NAME")
146 make_crate_type_option(),
148 o.optmulti("L", "library-path", "directory to add to crate search path", "DIR")
150 stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
151 stable("extern", |o| o.optmulti("", "extern", "pass an --extern to rustc", "NAME[=PATH]")),
152 unstable("extern-html-root-url", |o| {
153 o.optmulti("", "extern-html-root-url", "base URL to use for dependencies", "NAME=URL")
155 stable("plugin-path", |o| o.optmulti("", "plugin-path", "removed", "DIR")),
157 o.optmulti("C", "codegen", "pass a codegen option to rustc", "OPT[=VALUE]")
159 stable("passes", |o| {
163 "list of passes to also run, you might want to pass it multiple times; a value of \
164 `list` will print available passes",
168 stable("plugins", |o| o.optmulti("", "plugins", "removed", "PLUGINS")),
169 stable("no-default", |o| o.optflag("", "no-defaults", "don't run the default passes")),
170 stable("document-private-items", |o| {
171 o.optflag("", "document-private-items", "document private items")
173 unstable("document-hidden-items", |o| {
174 o.optflag("", "document-hidden-items", "document items that have doc(hidden)")
176 stable("test", |o| o.optflag("", "test", "run code examples as tests")),
177 stable("test-args", |o| {
178 o.optmulti("", "test-args", "arguments to pass to the test runner", "ARGS")
180 stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
181 stable("markdown-css", |o| {
185 "CSS files to include via <link> in a rendered Markdown file",
189 stable("html-in-header", |o| {
193 "files to include inline in the <head> section of a rendered Markdown file \
194 or generated documentation",
198 stable("html-before-content", |o| {
201 "html-before-content",
202 "files to include inline between <body> and the content of a rendered \
203 Markdown file or generated documentation",
207 stable("html-after-content", |o| {
210 "html-after-content",
211 "files to include inline between the content and </body> of a rendered \
212 Markdown file or generated documentation",
216 unstable("markdown-before-content", |o| {
219 "markdown-before-content",
220 "files to include inline between <body> and the content of a rendered \
221 Markdown file or generated documentation",
225 unstable("markdown-after-content", |o| {
228 "markdown-after-content",
229 "files to include inline between the content and </body> of a rendered \
230 Markdown file or generated documentation",
234 stable("markdown-playground-url", |o| {
235 o.optopt("", "markdown-playground-url", "URL to send code snippets to", "URL")
237 stable("markdown-no-toc", |o| {
238 o.optflag("", "markdown-no-toc", "don't include table of contents")
244 "To add some CSS rules with a given file to generate doc with your \
245 own theme. However, your theme might break if the rustdoc's generated HTML \
246 changes, so be careful!",
251 o.optmulti("Z", "", "internal and debugging options (only on nightly build)", "FLAG")
253 stable("sysroot", |o| o.optopt("", "sysroot", "Override the system root", "PATH")),
254 unstable("playground-url", |o| {
258 "URL to send code snippets to, may be reset by --markdown-playground-url \
259 or `#![doc(html_playground_url=...)]`",
263 unstable("display-warnings", |o| {
264 o.optflag("", "display-warnings", "to print code warnings when testing doc")
266 stable("crate-version", |o| {
267 o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
269 unstable("sort-modules-by-appearance", |o| {
272 "sort-modules-by-appearance",
273 "sort modules by where they appear in the program, rather than alphabetically",
276 stable("theme", |o| {
280 "additional themes which will be added to the generated docs",
284 stable("check-theme", |o| {
285 o.optmulti("", "check-theme", "check if given theme is valid", "FILES")
287 unstable("resource-suffix", |o| {
291 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
292 \"light-suffix.css\"",
296 stable("edition", |o| {
300 "edition to use when compiling rust code (default: 2015)",
304 stable("color", |o| {
308 "Configure coloring of output:
309 auto = colorize, if output goes to a tty (default);
310 always = always colorize output;
311 never = never colorize output",
315 stable("error-format", |o| {
319 "How errors and other messages are produced",
324 o.optopt("", "json", "Configure the structure of JSON diagnostics", "CONFIG")
326 unstable("disable-minification", |o| {
327 o.optflag("", "disable-minification", "Disable minification applied on JS files")
329 stable("warn", |o| o.optmulti("W", "warn", "Set lint warnings", "OPT")),
330 stable("allow", |o| o.optmulti("A", "allow", "Set lint allowed", "OPT")),
331 stable("deny", |o| o.optmulti("D", "deny", "Set lint denied", "OPT")),
332 stable("forbid", |o| o.optmulti("F", "forbid", "Set lint forbidden", "OPT")),
333 stable("cap-lints", |o| {
337 "Set the most restrictive lint level. \
338 More restrictive lints are capped at this \
339 level. By default, it is at `forbid` level.",
343 unstable("index-page", |o| {
344 o.optopt("", "index-page", "Markdown file to be used as index page", "PATH")
346 unstable("enable-index-page", |o| {
347 o.optflag("", "enable-index-page", "To enable generation of the index page")
349 unstable("static-root-path", |o| {
353 "Path string to force loading static files from in output pages. \
354 If not set, uses combinations of '../' to reach the documentation root.",
358 unstable("disable-per-crate-search", |o| {
361 "disable-per-crate-search",
362 "disables generating the crate selector on the search box",
365 unstable("persist-doctests", |o| {
369 "Directory to persist doctest executables into",
373 unstable("show-coverage", |o| {
377 "calculate percentage of public items with documentation",
380 unstable("enable-per-target-ignores", |o| {
383 "enable-per-target-ignores",
384 "parse ignore-foo for ignoring doctests on a per-target basis",
387 unstable("runtool", |o| {
392 "The tool to run tests with when building for a different target than host",
395 unstable("runtool-arg", |o| {
400 "One (of possibly many) arguments to pass to the runtool",
403 unstable("test-builder", |o| {
407 "specified the rustc-like binary to use as the test builder",
413 fn usage(argv0: &str) {
414 let mut options = getopts::Options::new();
415 for option in opts() {
416 (option.apply)(&mut options);
418 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
421 fn main_args(args: &[String]) -> i32 {
422 let mut options = getopts::Options::new();
423 for option in opts() {
424 (option.apply)(&mut options);
426 let matches = match options.parse(&args[1..]) {
429 early_error(ErrorOutputType::default(), &err.to_string());
432 let options = match config::Options::from_matches(&matches) {
434 Err(code) => return code,
436 rustc_interface::interface::setup_callbacks_and_run_in_default_thread_pool_with_globals(
438 move || main_options(options),
442 fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> i32 {
447 diag.struct_err(&err).emit();
454 fn run_renderer<T: formats::FormatRenderer>(
456 renderopts: config::RenderOptions,
457 render_info: config::RenderInfo,
458 diag: &rustc_errors::Handler,
459 edition: rustc_span::edition::Edition,
461 match formats::run_format::<T>(krate, renderopts, render_info, &diag, edition) {
462 Ok(_) => rustc_driver::EXIT_SUCCESS,
464 let mut msg = diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
465 let file = e.file.display().to_string();
469 msg.note(&format!("failed to create or modify \"{}\"", file)).emit()
471 rustc_driver::EXIT_FAILURE
476 fn main_options(options: config::Options) -> i32 {
477 let diag = core::new_handler(options.error_format, None, &options.debugging_options);
479 match (options.should_test, options.markdown_input()) {
480 (true, true) => return wrap_return(&diag, markdown::test(options)),
481 (true, false) => return wrap_return(&diag, test::run(options)),
485 markdown::render(&options.input, options.render_options, options.edition),
491 // need to move these items separately because we lose them by the time the closure is called,
492 // but we can't crates the Handler ahead of time because it's not Send
493 let diag_opts = (options.error_format, options.edition, options.debugging_options.clone());
494 let show_coverage = options.show_coverage;
496 // First, parse the crate and extract all relevant information.
497 info!("starting to run rustc");
499 // Interpret the input file as a rust source file, passing it through the
500 // compiler all the way through the analysis passes. The rustdoc output is
501 // then generated from the cleaned AST of the crate. This runs all the
502 // plug/cleaning passes.
503 let result = rustc_driver::catch_fatal_errors(move || {
504 let crate_name = options.crate_name.clone();
505 let crate_version = options.crate_version.clone();
506 let output_format = options.output_format;
507 let (mut krate, renderinfo, renderopts) = core::run_core(options);
509 info!("finished with rustc");
511 if let Some(name) = crate_name {
515 krate.version = crate_version;
517 let out = Output { krate, renderinfo, renderopts };
520 // if we ran coverage, bail early, we don't need to also generate docs at this point
521 // (also we didn't load in any of the useful passes)
522 return rustc_driver::EXIT_SUCCESS;
525 let Output { krate, renderinfo, renderopts } = out;
526 info!("going to format");
527 let (error_format, edition, debugging_options) = diag_opts;
528 let diag = core::new_handler(error_format, None, &debugging_options);
529 match output_format {
530 None | Some(config::OutputFormat::Html) => {
531 run_renderer::<html::render::Context>(krate, renderopts, renderinfo, &diag, edition)
533 Some(config::OutputFormat::Json) => {
534 run_renderer::<json::JsonRenderer>(krate, renderopts, renderinfo, &diag, edition)
540 Ok(output) => output,
541 Err(_) => panic::resume_unwind(Box::new(rustc_errors::FatalErrorMarker)),