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)]
11 #![feature(vec_remove_item)]
12 #![feature(ptr_offset_from)]
13 #![feature(crate_visibility_modifier)]
14 #![feature(never_type)]
15 #![recursion_limit = "256"]
17 extern crate env_logger;
18 extern crate rustc_ast;
19 extern crate rustc_ast_pretty;
20 extern crate rustc_attr;
21 extern crate rustc_data_structures;
22 extern crate rustc_driver;
23 extern crate rustc_errors;
24 extern crate rustc_expand;
25 extern crate rustc_feature;
26 extern crate rustc_hir;
27 extern crate rustc_hir_pretty;
28 extern crate rustc_index;
29 extern crate rustc_infer;
30 extern crate rustc_interface;
31 extern crate rustc_lexer;
32 extern crate rustc_lint;
33 extern crate rustc_metadata;
34 extern crate rustc_middle;
35 extern crate rustc_mir;
36 extern crate rustc_parse;
37 extern crate rustc_resolve;
38 extern crate rustc_session;
39 extern crate rustc_span as rustc_span;
40 extern crate rustc_target;
41 extern crate rustc_trait_selection;
42 extern crate rustc_typeck;
43 extern crate test as testing;
47 use std::default::Default;
52 use rustc_session::config::{make_crate_type_option, ErrorOutputType, RustcOptGroup};
53 use rustc_session::getopts;
54 use rustc_session::{early_error, early_warn};
74 crate mod static_files;
86 renderinfo: html::render::RenderInfo,
87 renderopts: config::RenderOptions,
91 let thread_stack_size: usize = if cfg!(target_os = "haiku") {
92 16_000_000 // 16MB on Haiku
94 32_000_000 // 32MB on other platforms
96 rustc_driver::set_sigpipe_handler();
97 env_logger::init_from_env("RUSTDOC_LOG");
98 let res = std::thread::Builder::new()
99 .stack_size(thread_stack_size)
100 .spawn(move || get_args().map(|args| main_args(&args)).unwrap_or(1))
103 .unwrap_or(rustc_driver::EXIT_FAILURE);
107 fn get_args() -> Option<Vec<String>> {
114 ErrorOutputType::default(),
115 &format!("Argument {} is not valid Unicode: {:?}", i, arg),
123 fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
125 F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static,
127 RustcOptGroup::stable(name, f)
130 fn unstable<F>(name: &'static str, f: F) -> RustcOptGroup
132 F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static,
134 RustcOptGroup::unstable(name, f)
137 fn opts() -> Vec<RustcOptGroup> {
139 stable("h", |o| o.optflag("h", "help", "show this help message")),
140 stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
141 stable("v", |o| o.optflag("v", "verbose", "use verbose output")),
143 o.optopt("r", "input-format", "the input type of the specified file", "[rust]")
145 stable("w", |o| o.optopt("w", "output-format", "the output type to write", "[html]")),
146 stable("o", |o| o.optopt("o", "output", "where to place the output", "PATH")),
147 stable("crate-name", |o| {
148 o.optopt("", "crate-name", "specify the name of this crate", "NAME")
150 make_crate_type_option(),
152 o.optmulti("L", "library-path", "directory to add to crate search path", "DIR")
154 stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
155 stable("extern", |o| o.optmulti("", "extern", "pass an --extern to rustc", "NAME[=PATH]")),
156 unstable("extern-html-root-url", |o| {
157 o.optmulti("", "extern-html-root-url", "base URL to use for dependencies", "NAME=URL")
159 stable("plugin-path", |o| o.optmulti("", "plugin-path", "removed", "DIR")),
161 o.optmulti("C", "codegen", "pass a codegen option to rustc", "OPT[=VALUE]")
163 stable("passes", |o| {
167 "list of passes to also run, you might want \
168 to pass it multiple times; a value of `list` \
169 will print available passes",
173 stable("plugins", |o| o.optmulti("", "plugins", "removed", "PLUGINS")),
174 stable("no-default", |o| o.optflag("", "no-defaults", "don't run the default passes")),
175 stable("document-private-items", |o| {
176 o.optflag("", "document-private-items", "document private items")
178 unstable("document-hidden-items", |o| {
179 o.optflag("", "document-hidden-items", "document items that have doc(hidden)")
181 stable("test", |o| o.optflag("", "test", "run code examples as tests")),
182 stable("test-args", |o| {
183 o.optmulti("", "test-args", "arguments to pass to the test runner", "ARGS")
185 stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
186 stable("markdown-css", |o| {
190 "CSS files to include via <link> in a rendered Markdown file",
194 stable("html-in-header", |o| {
198 "files to include inline in the <head> section of a rendered Markdown file \
199 or generated documentation",
203 stable("html-before-content", |o| {
206 "html-before-content",
207 "files to include inline between <body> and the content of a rendered \
208 Markdown file or generated documentation",
212 stable("html-after-content", |o| {
215 "html-after-content",
216 "files to include inline between the content and </body> of a rendered \
217 Markdown file or generated documentation",
221 unstable("markdown-before-content", |o| {
224 "markdown-before-content",
225 "files to include inline between <body> and the content of a rendered \
226 Markdown file or generated documentation",
230 unstable("markdown-after-content", |o| {
233 "markdown-after-content",
234 "files to include inline between the content and </body> of a rendered \
235 Markdown file or generated documentation",
239 stable("markdown-playground-url", |o| {
240 o.optopt("", "markdown-playground-url", "URL to send code snippets to", "URL")
242 stable("markdown-no-toc", |o| {
243 o.optflag("", "markdown-no-toc", "don't include table of contents")
249 "To add some CSS rules with a given file to generate doc with your \
250 own theme. However, your theme might break if the rustdoc's generated HTML \
251 changes, so be careful!",
256 o.optmulti("Z", "", "internal and debugging options (only on nightly build)", "FLAG")
258 stable("sysroot", |o| o.optopt("", "sysroot", "Override the system root", "PATH")),
259 unstable("playground-url", |o| {
263 "URL to send code snippets to, may be reset by --markdown-playground-url \
264 or `#![doc(html_playground_url=...)]`",
268 unstable("display-warnings", |o| {
269 o.optflag("", "display-warnings", "to print code warnings when testing doc")
271 stable("crate-version", |o| {
272 o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
274 unstable("sort-modules-by-appearance", |o| {
277 "sort-modules-by-appearance",
278 "sort modules by where they appear in the \
279 program, rather than alphabetically",
282 stable("theme", |o| {
286 "additional themes which will be added to the generated docs",
290 stable("check-theme", |o| {
291 o.optmulti("", "check-theme", "check if given theme is valid", "FILES")
293 unstable("resource-suffix", |o| {
297 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
298 \"light-suffix.css\"",
302 stable("edition", |o| {
306 "edition to use when compiling rust code (default: 2015)",
310 stable("color", |o| {
314 "Configure coloring of output:
315 auto = colorize, if output goes to a tty (default);
316 always = always colorize output;
317 never = never colorize output",
321 stable("error-format", |o| {
325 "How errors and other messages are produced",
330 o.optopt("", "json", "Configure the structure of JSON diagnostics", "CONFIG")
332 unstable("disable-minification", |o| {
333 o.optflag("", "disable-minification", "Disable minification applied on JS files")
335 stable("warn", |o| o.optmulti("W", "warn", "Set lint warnings", "OPT")),
336 stable("allow", |o| o.optmulti("A", "allow", "Set lint allowed", "OPT")),
337 stable("deny", |o| o.optmulti("D", "deny", "Set lint denied", "OPT")),
338 stable("forbid", |o| o.optmulti("F", "forbid", "Set lint forbidden", "OPT")),
339 stable("cap-lints", |o| {
343 "Set the most restrictive lint level. \
344 More restrictive lints are capped at this \
345 level. By default, it is at `forbid` level.",
349 unstable("index-page", |o| {
350 o.optopt("", "index-page", "Markdown file to be used as index page", "PATH")
352 unstable("enable-index-page", |o| {
353 o.optflag("", "enable-index-page", "To enable generation of the index page")
355 unstable("static-root-path", |o| {
359 "Path string to force loading static files from in output pages. \
360 If not set, uses combinations of '../' to reach the documentation root.",
364 unstable("disable-per-crate-search", |o| {
367 "disable-per-crate-search",
368 "disables generating the crate selector on the search box",
371 unstable("persist-doctests", |o| {
375 "Directory to persist doctest executables into",
379 unstable("generate-redirect-pages", |o| {
382 "generate-redirect-pages",
383 "Generate extra pages to support legacy URLs and tool links",
386 unstable("show-coverage", |o| {
390 "calculate percentage of public items with documentation",
393 unstable("enable-per-target-ignores", |o| {
396 "enable-per-target-ignores",
397 "parse ignore-foo for ignoring doctests on a per-target basis",
400 unstable("runtool", |o| {
405 "The tool to run tests with when building for a different target than host",
408 unstable("runtool-arg", |o| {
413 "One (of possibly many) arguments to pass to the runtool",
416 unstable("test-builder", |o| {
420 "specified the rustc-like binary to use as the test builder",
426 fn usage(argv0: &str) {
427 let mut options = getopts::Options::new();
428 for option in opts() {
429 (option.apply)(&mut options);
431 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
434 fn main_args(args: &[String]) -> i32 {
435 let mut options = getopts::Options::new();
436 for option in opts() {
437 (option.apply)(&mut options);
439 let matches = match options.parse(&args[1..]) {
442 early_error(ErrorOutputType::default(), &err.to_string());
445 let options = match config::Options::from_matches(&matches) {
447 Err(code) => return code,
449 rustc_interface::interface::default_thread_pool(options.edition, move || main_options(options))
452 fn main_options(options: config::Options) -> i32 {
453 let diag = core::new_handler(options.error_format, None, &options.debugging_options);
455 match (options.should_test, options.markdown_input()) {
456 (true, true) => return markdown::test(options, &diag),
457 (true, false) => return test::run(options),
459 return markdown::render(options.input, options.render_options, &diag, options.edition);
464 // need to move these items separately because we lose them by the time the closure is called,
465 // but we can't crates the Handler ahead of time because it's not Send
466 let diag_opts = (options.error_format, options.edition, options.debugging_options.clone());
467 let show_coverage = options.show_coverage;
468 rust_input(options, move |out| {
470 // if we ran coverage, bail early, we don't need to also generate docs at this point
471 // (also we didn't load in any of the useful passes)
472 return rustc_driver::EXIT_SUCCESS;
475 let Output { krate, renderinfo, renderopts } = out;
476 info!("going to format");
477 let (error_format, edition, debugging_options) = diag_opts;
478 let diag = core::new_handler(error_format, None, &debugging_options);
479 match html::render::run(krate, renderopts, renderinfo, &diag, edition) {
480 Ok(_) => rustc_driver::EXIT_SUCCESS,
482 diag.struct_err(&format!("couldn't generate documentation: {}", e.error))
483 .note(&format!("failed to create or modify \"{}\"", e.file.display()))
485 rustc_driver::EXIT_FAILURE
491 /// Interprets the input file as a rust source file, passing it through the
492 /// compiler all the way through the analysis passes. The rustdoc output is then
493 /// generated from the cleaned AST of the crate.
495 /// This form of input will run all of the plug/cleaning passes
496 fn rust_input<R, F>(options: config::Options, f: F) -> R
499 F: 'static + Send + FnOnce(Output) -> R,
501 // First, parse the crate and extract all relevant information.
502 info!("starting to run rustc");
504 let result = rustc_driver::catch_fatal_errors(move || {
505 let crate_name = options.crate_name.clone();
506 let crate_version = options.crate_version.clone();
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 f(Output { krate, renderinfo, renderopts })
521 Ok(output) => output,
522 Err(_) => panic::resume_unwind(Box::new(rustc_errors::FatalErrorMarker)),