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(ptr_offset_from)]
14 #![feature(crate_visibility_modifier)]
16 #![feature(drain_filter)]
17 #![feature(never_type)]
18 #![feature(unicode_internals)]
19 #![recursion_limit = "256"]
21 extern crate env_logger;
24 extern crate rustc_data_structures;
25 extern crate rustc_driver;
26 extern crate rustc_error_codes;
27 extern crate rustc_expand;
28 extern crate rustc_feature;
29 extern crate rustc_hir;
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 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 unstable("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 \
274 program, rather than alphabetically",
277 stable("theme", |o| {
281 "additional themes which will be added to the generated docs",
285 stable("check-theme", |o| {
286 o.optmulti("", "check-theme", "check if given theme is valid", "FILES")
288 unstable("resource-suffix", |o| {
292 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
293 \"light-suffix.css\"",
297 stable("edition", |o| {
301 "edition to use when compiling rust code (default: 2015)",
305 stable("color", |o| {
309 "Configure coloring of output:
310 auto = colorize, if output goes to a tty (default);
311 always = always colorize output;
312 never = never colorize output",
316 stable("error-format", |o| {
320 "How errors and other messages are produced",
325 o.optopt("", "json", "Configure the structure of JSON diagnostics", "CONFIG")
327 unstable("disable-minification", |o| {
328 o.optflag("", "disable-minification", "Disable minification applied on JS files")
330 stable("warn", |o| o.optmulti("W", "warn", "Set lint warnings", "OPT")),
331 stable("allow", |o| o.optmulti("A", "allow", "Set lint allowed", "OPT")),
332 stable("deny", |o| o.optmulti("D", "deny", "Set lint denied", "OPT")),
333 stable("forbid", |o| o.optmulti("F", "forbid", "Set lint forbidden", "OPT")),
334 stable("cap-lints", |o| {
338 "Set the most restrictive lint level. \
339 More restrictive lints are capped at this \
340 level. By default, it is at `forbid` level.",
344 unstable("index-page", |o| {
345 o.optopt("", "index-page", "Markdown file to be used as index page", "PATH")
347 unstable("enable-index-page", |o| {
348 o.optflag("", "enable-index-page", "To enable generation of the index page")
350 unstable("static-root-path", |o| {
354 "Path string to force loading static files from in output pages. \
355 If not set, uses combinations of '../' to reach the documentation root.",
359 unstable("disable-per-crate-search", |o| {
362 "disable-per-crate-search",
363 "disables generating the crate selector on the search box",
366 unstable("persist-doctests", |o| {
370 "Directory to persist doctest executables into",
374 unstable("generate-redirect-pages", |o| {
377 "generate-redirect-pages",
378 "Generate extra pages to support legacy URLs and tool links",
381 unstable("show-coverage", |o| {
385 "calculate percentage of public items with documentation",
388 unstable("enable-per-target-ignores", |o| {
391 "enable-per-target-ignores",
392 "parse ignore-foo for ignoring doctests on a per-target basis",
395 unstable("runtool", |o| {
400 "The tool to run tests with when building for a different target than host",
403 unstable("runtool-arg", |o| {
408 "One (of possibly many) arguments to pass to the runtool",
411 unstable("test-builder", |o| {
415 "specified the rustc-like binary to use as the test builder",
421 fn usage(argv0: &str) {
422 let mut options = getopts::Options::new();
423 for option in opts() {
424 (option.apply)(&mut options);
426 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
429 fn main_args(args: &[String]) -> i32 {
430 let mut options = getopts::Options::new();
431 for option in opts() {
432 (option.apply)(&mut options);
434 let matches = match options.parse(&args[1..]) {
437 early_error(ErrorOutputType::default(), &err.to_string());
440 let options = match config::Options::from_matches(&matches) {
442 Err(code) => return code,
444 rustc_interface::interface::default_thread_pool(options.edition, move || main_options(options))
447 fn main_options(options: config::Options) -> i32 {
448 let diag = core::new_handler(options.error_format, None, &options.debugging_options);
450 match (options.should_test, options.markdown_input()) {
451 (true, true) => return markdown::test(options, &diag),
452 (true, false) => return test::run(options),
454 return markdown::render(options.input, options.render_options, &diag, options.edition);
459 // need to move these items separately because we lose them by the time the closure is called,
460 // but we can't crates the Handler ahead of time because it's not Send
461 let diag_opts = (options.error_format, options.edition, options.debugging_options.clone());
462 let show_coverage = options.show_coverage;
463 rust_input(options, move |out| {
465 // if we ran coverage, bail early, we don't need to also generate docs at this point
466 // (also we didn't load in any of the useful passes)
467 return rustc_driver::EXIT_SUCCESS;
470 let Output { krate, renderinfo, renderopts } = out;
471 info!("going to format");
472 let (error_format, edition, debugging_options) = diag_opts;
473 let diag = core::new_handler(error_format, None, &debugging_options);
474 match html::render::run(krate, renderopts, renderinfo, &diag, edition) {
475 Ok(_) => rustc_driver::EXIT_SUCCESS,
477 diag.struct_err(&format!("couldn't generate documentation: {}", e.error))
478 .note(&format!("failed to create or modify \"{}\"", e.file.display()))
480 rustc_driver::EXIT_FAILURE
486 /// Interprets the input file as a rust source file, passing it through the
487 /// compiler all the way through the analysis passes. The rustdoc output is then
488 /// generated from the cleaned AST of the crate.
490 /// This form of input will run all of the plug/cleaning passes
491 fn rust_input<R, F>(options: config::Options, f: F) -> R
494 F: 'static + Send + FnOnce(Output) -> R,
496 // First, parse the crate and extract all relevant information.
497 info!("starting to run rustc");
499 let result = rustc_driver::catch_fatal_errors(move || {
500 let crate_name = options.crate_name.clone();
501 let crate_version = options.crate_version.clone();
502 let (mut krate, renderinfo, renderopts) = core::run_core(options);
504 info!("finished with rustc");
506 if let Some(name) = crate_name {
510 krate.version = crate_version;
512 f(Output { krate, renderinfo, renderopts })
516 Ok(output) => output,
517 Err(_) => panic::resume_unwind(Box::new(errors::FatalErrorMarker)),