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(arbitrary_self_types)]
7 #![feature(box_patterns)]
8 #![feature(box_syntax)]
9 #![feature(in_band_lifetimes)]
11 #![feature(set_stdio)]
13 #![feature(vec_remove_item)]
14 #![feature(ptr_offset_from)]
15 #![feature(crate_visibility_modifier)]
17 #![feature(drain_filter)]
18 #![feature(never_type)]
19 #![feature(unicode_internals)]
20 #![recursion_limit = "256"]
22 extern crate env_logger;
25 extern crate rustc_data_structures;
26 extern crate rustc_driver;
27 extern crate rustc_error_codes;
28 extern crate rustc_expand;
29 extern crate rustc_feature;
30 extern crate rustc_index;
31 extern crate rustc_interface;
32 extern crate rustc_lexer;
33 extern crate rustc_lint;
34 extern crate rustc_metadata;
35 extern crate rustc_parse;
36 extern crate rustc_resolve;
37 extern crate rustc_span as rustc_span;
38 extern crate rustc_target;
39 extern crate rustc_typeck;
41 extern crate test as testing;
44 extern crate rustc_errors as errors;
46 use std::default::Default;
51 use rustc::session::config::{make_crate_type_option, ErrorOutputType, RustcOptGroup};
52 use rustc::session::{early_error, early_warn};
72 crate mod static_files;
84 renderinfo: html::render::RenderInfo,
85 renderopts: config::RenderOptions,
89 let thread_stack_size: usize = if cfg!(target_os = "haiku") {
90 16_000_000 // 16MB on Haiku
92 32_000_000 // 32MB on other platforms
94 rustc_driver::set_sigpipe_handler();
95 env_logger::init_from_env("RUSTDOC_LOG");
96 let res = std::thread::Builder::new()
97 .stack_size(thread_stack_size)
98 .spawn(move || get_args().map(|args| main_args(&args)).unwrap_or(1))
101 .unwrap_or(rustc_driver::EXIT_FAILURE);
105 fn get_args() -> Option<Vec<String>> {
112 ErrorOutputType::default(),
113 &format!("Argument {} is not valid Unicode: {:?}", i, arg),
121 fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
123 F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static,
125 RustcOptGroup::stable(name, f)
128 fn unstable<F>(name: &'static str, f: F) -> RustcOptGroup
130 F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static,
132 RustcOptGroup::unstable(name, f)
135 fn opts() -> Vec<RustcOptGroup> {
137 stable("h", |o| o.optflag("h", "help", "show this help message")),
138 stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
139 stable("v", |o| o.optflag("v", "verbose", "use verbose output")),
141 o.optopt("r", "input-format", "the input type of the specified file", "[rust]")
143 stable("w", |o| o.optopt("w", "output-format", "the output type to write", "[html]")),
144 stable("o", |o| o.optopt("o", "output", "where to place the output", "PATH")),
145 stable("crate-name", |o| {
146 o.optopt("", "crate-name", "specify the name of this crate", "NAME")
148 make_crate_type_option(),
150 o.optmulti("L", "library-path", "directory to add to crate search path", "DIR")
152 stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
153 stable("extern", |o| o.optmulti("", "extern", "pass an --extern to rustc", "NAME[=PATH]")),
154 unstable("extern-html-root-url", |o| {
155 o.optmulti("", "extern-html-root-url", "base URL to use for dependencies", "NAME=URL")
157 stable("plugin-path", |o| o.optmulti("", "plugin-path", "removed", "DIR")),
159 o.optmulti("C", "codegen", "pass a codegen option to rustc", "OPT[=VALUE]")
161 stable("passes", |o| {
165 "list of passes to also run, you might want \
166 to pass it multiple times; a value of `list` \
167 will print available passes",
171 stable("plugins", |o| o.optmulti("", "plugins", "removed", "PLUGINS")),
172 stable("no-default", |o| o.optflag("", "no-defaults", "don't run the default passes")),
173 stable("document-private-items", |o| {
174 o.optflag("", "document-private-items", "document private items")
176 unstable("document-hidden-items", |o| {
177 o.optflag("", "document-hidden-items", "document items that have doc(hidden)")
179 stable("test", |o| o.optflag("", "test", "run code examples as tests")),
180 stable("test-args", |o| {
181 o.optmulti("", "test-args", "arguments to pass to the test runner", "ARGS")
183 stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
184 stable("markdown-css", |o| {
188 "CSS files to include via <link> in a rendered Markdown file",
192 stable("html-in-header", |o| {
196 "files to include inline in the <head> section of a rendered Markdown file \
197 or generated documentation",
201 stable("html-before-content", |o| {
204 "html-before-content",
205 "files to include inline between <body> and the content of a rendered \
206 Markdown file or generated documentation",
210 stable("html-after-content", |o| {
213 "html-after-content",
214 "files to include inline between the content and </body> of a rendered \
215 Markdown file or generated documentation",
219 unstable("markdown-before-content", |o| {
222 "markdown-before-content",
223 "files to include inline between <body> and the content of a rendered \
224 Markdown file or generated documentation",
228 unstable("markdown-after-content", |o| {
231 "markdown-after-content",
232 "files to include inline between the content and </body> of a rendered \
233 Markdown file or generated documentation",
237 stable("markdown-playground-url", |o| {
238 o.optopt("", "markdown-playground-url", "URL to send code snippets to", "URL")
240 stable("markdown-no-toc", |o| {
241 o.optflag("", "markdown-no-toc", "don't include table of contents")
247 "To add some CSS rules with a given file to generate doc with your \
248 own theme. However, your theme might break if the rustdoc's generated HTML \
249 changes, so be careful!",
254 o.optmulti("Z", "", "internal and debugging options (only on nightly build)", "FLAG")
256 stable("sysroot", |o| o.optopt("", "sysroot", "Override the system root", "PATH")),
257 unstable("playground-url", |o| {
261 "URL to send code snippets to, may be reset by --markdown-playground-url \
262 or `#![doc(html_playground_url=...)]`",
266 unstable("display-warnings", |o| {
267 o.optflag("", "display-warnings", "to print code warnings when testing doc")
269 unstable("crate-version", |o| {
270 o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
272 unstable("sort-modules-by-appearance", |o| {
275 "sort-modules-by-appearance",
276 "sort modules by where they appear in the \
277 program, rather than alphabetically",
280 stable("theme", |o| {
284 "additional themes which will be added to the generated docs",
288 stable("check-theme", |o| {
289 o.optmulti("", "check-theme", "check if given theme is valid", "FILES")
291 unstable("resource-suffix", |o| {
295 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
296 \"light-suffix.css\"",
300 stable("edition", |o| {
304 "edition to use when compiling rust code (default: 2015)",
308 stable("color", |o| {
312 "Configure coloring of output:
313 auto = colorize, if output goes to a tty (default);
314 always = always colorize output;
315 never = never colorize output",
319 stable("error-format", |o| {
323 "How errors and other messages are produced",
328 o.optopt("", "json", "Configure the structure of JSON diagnostics", "CONFIG")
330 unstable("disable-minification", |o| {
331 o.optflag("", "disable-minification", "Disable minification applied on JS files")
333 stable("warn", |o| o.optmulti("W", "warn", "Set lint warnings", "OPT")),
334 stable("allow", |o| o.optmulti("A", "allow", "Set lint allowed", "OPT")),
335 stable("deny", |o| o.optmulti("D", "deny", "Set lint denied", "OPT")),
336 stable("forbid", |o| o.optmulti("F", "forbid", "Set lint forbidden", "OPT")),
337 stable("cap-lints", |o| {
341 "Set the most restrictive lint level. \
342 More restrictive lints are capped at this \
343 level. By default, it is at `forbid` level.",
347 unstable("index-page", |o| {
348 o.optopt("", "index-page", "Markdown file to be used as index page", "PATH")
350 unstable("enable-index-page", |o| {
351 o.optflag("", "enable-index-page", "To enable generation of the index page")
353 unstable("static-root-path", |o| {
357 "Path string to force loading static files from in output pages. \
358 If not set, uses combinations of '../' to reach the documentation root.",
362 unstable("disable-per-crate-search", |o| {
365 "disable-per-crate-search",
366 "disables generating the crate selector on the search box",
369 unstable("persist-doctests", |o| {
373 "Directory to persist doctest executables into",
377 unstable("generate-redirect-pages", |o| {
380 "generate-redirect-pages",
381 "Generate extra pages to support legacy URLs and tool links",
384 unstable("show-coverage", |o| {
388 "calculate percentage of public items with documentation",
391 unstable("enable-per-target-ignores", |o| {
394 "enable-per-target-ignores",
395 "parse ignore-foo for ignoring doctests on a per-target basis",
398 unstable("runtool", |o| {
403 "The tool to run tests with when building for a different target than host",
406 unstable("runtool-arg", |o| {
411 "One (of possibly many) arguments to pass to the runtool",
414 unstable("test-builder", |o| {
418 "specified the rustc-like binary to use as the test builder",
424 fn usage(argv0: &str) {
425 let mut options = getopts::Options::new();
426 for option in opts() {
427 (option.apply)(&mut options);
429 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
432 fn main_args(args: &[String]) -> i32 {
433 let mut options = getopts::Options::new();
434 for option in opts() {
435 (option.apply)(&mut options);
437 let matches = match options.parse(&args[1..]) {
440 early_error(ErrorOutputType::default(), &err.to_string());
443 let options = match config::Options::from_matches(&matches) {
445 Err(code) => return code,
447 rustc_interface::interface::default_thread_pool(options.edition, move || main_options(options))
450 fn main_options(options: config::Options) -> i32 {
451 let diag = core::new_handler(
452 options.error_format,
454 options.debugging_options.treat_err_as_bug,
455 options.debugging_options.ui_testing(),
458 match (options.should_test, options.markdown_input()) {
459 (true, true) => return markdown::test(options, &diag),
460 (true, false) => return test::run(options),
462 return markdown::render(options.input, options.render_options, &diag, options.edition);
467 // need to move these items separately because we lose them by the time the closure is called,
468 // but we can't crates the Handler ahead of time because it's not Send
470 options.error_format,
471 options.debugging_options.treat_err_as_bug,
472 options.debugging_options.ui_testing(),
475 let show_coverage = options.show_coverage;
476 rust_input(options, move |out| {
478 // if we ran coverage, bail early, we don't need to also generate docs at this point
479 // (also we didn't load in any of the useful passes)
480 return rustc_driver::EXIT_SUCCESS;
483 let Output { krate, renderinfo, renderopts } = out;
484 info!("going to format");
485 let (error_format, treat_err_as_bug, ui_testing, edition) = diag_opts;
486 let diag = core::new_handler(error_format, None, treat_err_as_bug, ui_testing);
487 match html::render::run(krate, renderopts, renderinfo, &diag, edition) {
488 Ok(_) => rustc_driver::EXIT_SUCCESS,
490 diag.struct_err(&format!("couldn't generate documentation: {}", e.error))
491 .note(&format!("failed to create or modify \"{}\"", e.file.display()))
493 rustc_driver::EXIT_FAILURE
499 /// Interprets 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 then
501 /// generated from the cleaned AST of the crate.
503 /// This form of input will run all of the plug/cleaning passes
504 fn rust_input<R, F>(options: config::Options, f: F) -> R
507 F: 'static + Send + FnOnce(Output) -> R,
509 // First, parse the crate and extract all relevant information.
510 info!("starting to run rustc");
512 let result = rustc_driver::catch_fatal_errors(move || {
513 let crate_name = options.crate_name.clone();
514 let crate_version = options.crate_version.clone();
515 let (mut krate, renderinfo, renderopts) = core::run_core(options);
517 info!("finished with rustc");
519 if let Some(name) = crate_name {
523 krate.version = crate_version;
525 f(Output { krate, renderinfo, renderopts })
529 Ok(output) => output,
530 Err(_) => panic::resume_unwind(Box::new(errors::FatalErrorMarker)),