1 #![deny(rust_2018_idioms)]
2 #![deny(unused_lifetimes)]
4 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
5 html_playground_url = "https://play.rust-lang.org/")]
7 #![feature(bind_by_move_pattern_guards)]
8 #![feature(rustc_private)]
9 #![feature(arbitrary_self_types)]
10 #![feature(box_patterns)]
11 #![feature(box_syntax)]
12 #![feature(in_band_lifetimes)]
14 #![feature(set_stdio)]
16 #![feature(vec_remove_item)]
17 #![feature(ptr_offset_from)]
18 #![feature(crate_visibility_modifier)]
20 #![feature(drain_filter)]
21 #![feature(inner_deref)]
22 #![feature(never_type)]
24 #![feature(unicode_internals)]
26 #![recursion_limit="256"]
29 extern crate env_logger;
31 extern crate rustc_data_structures;
32 extern crate rustc_driver;
33 extern crate rustc_resolve;
34 extern crate rustc_lint;
35 extern crate rustc_interface;
36 extern crate rustc_metadata;
37 extern crate rustc_target;
38 extern crate rustc_typeck;
39 extern crate serialize;
41 extern crate syntax_pos;
42 extern crate test as testing;
43 #[macro_use] extern crate log;
44 extern crate rustc_errors as errors;
46 use std::default::Default;
50 use std::sync::mpsc::channel;
52 use rustc::session::{early_warn, early_error};
53 use rustc::session::config::{ErrorOutputType, RustcOptGroup};
72 crate mod static_files;
84 renderinfo: html::render::RenderInfo,
85 renderopts: config::RenderOptions,
90 let thread_stack_size: usize = if cfg!(target_os = "haiku") {
91 16_000_000 // 16MB on Haiku
93 32_000_000 // 32MB on other platforms
95 rustc_driver::set_sigpipe_handler();
97 let res = std::thread::Builder::new().stack_size(thread_stack_size).spawn(move || {
98 get_args().map(|args| main_args(&args)).unwrap_or(1)
99 }).unwrap().join().unwrap_or(rustc_driver::EXIT_FAILURE);
103 fn get_args() -> Option<Vec<String>> {
104 env::args_os().enumerate()
105 .map(|(i, arg)| arg.into_string().map_err(|arg| {
106 early_warn(ErrorOutputType::default(),
107 &format!("Argument {} is not valid Unicode: {:?}", i, arg));
112 fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
113 where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
115 RustcOptGroup::stable(name, f)
118 fn unstable<F>(name: &'static str, f: F) -> RustcOptGroup
119 where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
121 RustcOptGroup::unstable(name, f)
124 fn opts() -> Vec<RustcOptGroup> {
126 stable("h", |o| o.optflag("h", "help", "show this help message")),
127 stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
128 stable("v", |o| o.optflag("v", "verbose", "use verbose output")),
130 o.optopt("r", "input-format", "the input type of the specified file",
134 o.optopt("w", "output-format", "the output type to write", "[html]")
136 stable("o", |o| o.optopt("o", "output", "where to place the output", "PATH")),
137 stable("crate-name", |o| {
138 o.optopt("", "crate-name", "specify the name of this crate", "NAME")
141 o.optmulti("L", "library-path", "directory to add to crate search path",
144 stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
145 stable("extern", |o| {
146 o.optmulti("", "extern", "pass an --extern to rustc", "NAME=PATH")
148 unstable("extern-html-root-url", |o| {
149 o.optmulti("", "extern-html-root-url",
150 "base URL to use for dependencies", "NAME=URL")
152 stable("plugin-path", |o| {
153 o.optmulti("", "plugin-path", "removed", "DIR")
156 o.optmulti("C", "codegen", "pass a codegen option to rustc", "OPT[=VALUE]")
158 stable("passes", |o| {
159 o.optmulti("", "passes",
160 "list of passes to also run, you might want \
161 to pass it multiple times; a value of `list` \
162 will print available passes",
165 stable("plugins", |o| {
166 o.optmulti("", "plugins", "removed",
169 stable("no-default", |o| {
170 o.optflag("", "no-defaults", "don't run the default passes")
172 stable("document-private-items", |o| {
173 o.optflag("", "document-private-items", "document private items")
175 stable("test", |o| o.optflag("", "test", "run code examples as tests")),
176 stable("test-args", |o| {
177 o.optmulti("", "test-args", "arguments to pass to the test runner",
180 stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
181 stable("markdown-css", |o| {
182 o.optmulti("", "markdown-css",
183 "CSS files to include via <link> in a rendered Markdown file",
186 stable("html-in-header", |o| {
187 o.optmulti("", "html-in-header",
188 "files to include inline in the <head> section of a rendered Markdown file \
189 or generated documentation",
192 stable("html-before-content", |o| {
193 o.optmulti("", "html-before-content",
194 "files to include inline between <body> and the content of a rendered \
195 Markdown file or generated documentation",
198 stable("html-after-content", |o| {
199 o.optmulti("", "html-after-content",
200 "files to include inline between the content and </body> of a rendered \
201 Markdown file or generated documentation",
204 unstable("markdown-before-content", |o| {
205 o.optmulti("", "markdown-before-content",
206 "files to include inline between <body> and the content of a rendered \
207 Markdown file or generated documentation",
210 unstable("markdown-after-content", |o| {
211 o.optmulti("", "markdown-after-content",
212 "files to include inline between the content and </body> of a rendered \
213 Markdown file or generated documentation",
216 stable("markdown-playground-url", |o| {
217 o.optopt("", "markdown-playground-url",
218 "URL to send code snippets to", "URL")
220 stable("markdown-no-toc", |o| {
221 o.optflag("", "markdown-no-toc", "don't include table of contents")
224 o.optopt("e", "extend-css",
225 "To add some CSS rules with a given file to generate doc with your \
226 own theme. However, your theme might break if the rustdoc's generated HTML \
227 changes, so be careful!", "PATH")
231 "internal and debugging options (only on nightly build)", "FLAG")
233 stable("sysroot", |o| {
234 o.optopt("", "sysroot", "Override the system root", "PATH")
236 unstable("playground-url", |o| {
237 o.optopt("", "playground-url",
238 "URL to send code snippets to, may be reset by --markdown-playground-url \
239 or `#![doc(html_playground_url=...)]`",
242 unstable("display-warnings", |o| {
243 o.optflag("", "display-warnings", "to print code warnings when testing doc")
245 unstable("crate-version", |o| {
246 o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
248 unstable("linker", |o| {
249 o.optopt("", "linker", "linker used for building executable test code", "PATH")
251 unstable("sort-modules-by-appearance", |o| {
252 o.optflag("", "sort-modules-by-appearance", "sort modules by where they appear in the \
253 program, rather than alphabetically")
255 unstable("themes", |o| {
256 o.optmulti("", "themes",
257 "additional themes which will be added to the generated docs",
260 unstable("theme-checker", |o| {
261 o.optmulti("", "theme-checker",
262 "check if given theme is valid",
265 unstable("resource-suffix", |o| {
268 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
269 \"light-suffix.css\"",
272 stable("edition", |o| {
273 o.optopt("", "edition",
274 "edition to use when compiling rust code (default: 2015)",
277 stable("color", |o| {
280 "Configure coloring of output:
281 auto = colorize, if output goes to a tty (default);
282 always = always colorize output;
283 never = never colorize output",
286 stable("error-format", |o| {
289 "How errors and other messages are produced",
292 unstable("disable-minification", |o| {
294 "disable-minification",
295 "Disable minification applied on JS files")
298 o.optmulti("W", "warn", "Set lint warnings", "OPT")
300 stable("allow", |o| {
301 o.optmulti("A", "allow", "Set lint allowed", "OPT")
304 o.optmulti("D", "deny", "Set lint denied", "OPT")
306 stable("forbid", |o| {
307 o.optmulti("F", "forbid", "Set lint forbidden", "OPT")
309 stable("cap-lints", |o| {
313 "Set the most restrictive lint level. \
314 More restrictive lints are capped at this \
315 level. By default, it is at `forbid` level.",
319 unstable("index-page", |o| {
322 "Markdown file to be used as index page",
325 unstable("enable-index-page", |o| {
328 "To enable generation of the index page")
330 unstable("static-root-path", |o| {
333 "Path string to force loading static files from in output pages. \
334 If not set, uses combinations of '../' to reach the documentation root.",
337 unstable("disable-per-crate-search", |o| {
339 "disable-per-crate-search",
340 "disables generating the crate selector on the search box")
342 unstable("persist-doctests", |o| {
345 "Directory to persist doctest executables into",
348 unstable("generate-redirect-pages", |o| {
350 "generate-redirect-pages",
351 "Generate extra pages to support legacy URLs and tool links")
353 unstable("show-coverage", |o| {
356 "calculate percentage of public items with documentation")
361 fn usage(argv0: &str) {
362 let mut options = getopts::Options::new();
363 for option in opts() {
364 (option.apply)(&mut options);
366 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
369 fn main_args(args: &[String]) -> i32 {
370 let mut options = getopts::Options::new();
371 for option in opts() {
372 (option.apply)(&mut options);
374 let matches = match options.parse(&args[1..]) {
377 early_error(ErrorOutputType::default(), &err.to_string());
380 let options = match config::Options::from_matches(&matches) {
382 Err(code) => return code,
384 rustc_interface::interface::default_thread_pool(options.edition, move || {
385 main_options(options)
389 fn main_options(options: config::Options) -> i32 {
390 let diag = core::new_handler(options.error_format,
392 options.debugging_options.treat_err_as_bug,
393 options.debugging_options.ui_testing);
395 match (options.should_test, options.markdown_input()) {
396 (true, true) => return markdown::test(options, &diag),
397 (true, false) => return test::run(options),
398 (false, true) => return markdown::render(options.input,
399 options.render_options,
405 // need to move these items separately because we lose them by the time the closure is called,
406 // but we can't crates the Handler ahead of time because it's not Send
407 let diag_opts = (options.error_format,
408 options.debugging_options.treat_err_as_bug,
409 options.debugging_options.ui_testing,
411 let show_coverage = options.show_coverage;
412 rust_input(options, move |out| {
414 // if we ran coverage, bail early, we don't need to also generate docs at this point
415 // (also we didn't load in any of the useful passes)
416 return rustc_driver::EXIT_SUCCESS;
419 let Output { krate, passes, renderinfo, renderopts } = out;
420 info!("going to format");
421 let (error_format, treat_err_as_bug, ui_testing, edition) = diag_opts;
422 let diag = core::new_handler(error_format, None, treat_err_as_bug, ui_testing);
423 match html::render::run(
426 passes.into_iter().collect(),
431 Ok(_) => rustc_driver::EXIT_SUCCESS,
433 diag.struct_err(&format!("couldn't generate documentation: {}", e.error))
434 .note(&format!("failed to create or modify \"{}\"", e.file.display()))
436 rustc_driver::EXIT_FAILURE
442 /// Interprets the input file as a rust source file, passing it through the
443 /// compiler all the way through the analysis passes. The rustdoc output is then
444 /// generated from the cleaned AST of the crate.
446 /// This form of input will run all of the plug/cleaning passes
447 fn rust_input<R, F>(options: config::Options, f: F) -> R
448 where R: 'static + Send,
449 F: 'static + Send + FnOnce(Output) -> R
451 // First, parse the crate and extract all relevant information.
452 info!("starting to run rustc");
454 let (tx, rx) = channel();
456 let result = rustc_driver::report_ices_to_stderr_if_any(move || {
457 let crate_name = options.crate_name.clone();
458 let crate_version = options.crate_version.clone();
459 let (mut krate, renderinfo, renderopts, passes) = core::run_core(options);
461 info!("finished with rustc");
463 if let Some(name) = crate_name {
467 krate.version = crate_version;
471 renderinfo: renderinfo,
478 Ok(()) => rx.recv().unwrap(),
479 Err(_) => panic::resume_unwind(Box::new(errors::FatalErrorMarker)),