1 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
2 html_playground_url = "https://play.rust-lang.org/")]
4 #![feature(bind_by_move_pattern_guards)]
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(inner_deref)]
19 #![feature(never_type)]
21 #![feature(unicode_internals)]
23 #![recursion_limit="256"]
26 extern crate env_logger;
28 extern crate rustc_data_structures;
29 extern crate rustc_driver;
30 extern crate rustc_resolve;
31 extern crate rustc_lint;
32 extern crate rustc_interface;
33 extern crate rustc_metadata;
34 extern crate rustc_target;
35 extern crate rustc_typeck;
36 extern crate serialize;
38 extern crate syntax_pos;
39 extern crate test as testing;
40 #[macro_use] extern crate log;
41 extern crate rustc_errors as errors;
43 use std::default::Default;
47 use std::sync::mpsc::channel;
49 use rustc::session::{early_warn, early_error};
50 use rustc::session::config::{ErrorOutputType, RustcOptGroup};
69 crate mod static_files;
81 renderinfo: html::render::RenderInfo,
82 renderopts: config::RenderOptions,
87 let thread_stack_size: usize = if cfg!(target_os = "haiku") {
88 16_000_000 // 16MB on Haiku
90 32_000_000 // 32MB on other platforms
92 rustc_driver::set_sigpipe_handler();
94 let res = std::thread::Builder::new().stack_size(thread_stack_size).spawn(move || {
95 get_args().map(|args| main_args(&args)).unwrap_or(1)
96 }).unwrap().join().unwrap_or(rustc_driver::EXIT_FAILURE);
100 fn get_args() -> Option<Vec<String>> {
101 env::args_os().enumerate()
102 .map(|(i, arg)| arg.into_string().map_err(|arg| {
103 early_warn(ErrorOutputType::default(),
104 &format!("Argument {} is not valid Unicode: {:?}", i, arg));
109 fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
110 where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
112 RustcOptGroup::stable(name, f)
115 fn unstable<F>(name: &'static str, f: F) -> RustcOptGroup
116 where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
118 RustcOptGroup::unstable(name, f)
121 fn opts() -> Vec<RustcOptGroup> {
123 stable("h", |o| o.optflag("h", "help", "show this help message")),
124 stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
125 stable("v", |o| o.optflag("v", "verbose", "use verbose output")),
127 o.optopt("r", "input-format", "the input type of the specified file",
131 o.optopt("w", "output-format", "the output type to write", "[html]")
133 stable("o", |o| o.optopt("o", "output", "where to place the output", "PATH")),
134 stable("crate-name", |o| {
135 o.optopt("", "crate-name", "specify the name of this crate", "NAME")
138 o.optmulti("L", "library-path", "directory to add to crate search path",
141 stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
142 stable("extern", |o| {
143 o.optmulti("", "extern", "pass an --extern to rustc", "NAME=PATH")
145 unstable("extern-html-root-url", |o| {
146 o.optmulti("", "extern-html-root-url",
147 "base URL to use for dependencies", "NAME=URL")
149 stable("plugin-path", |o| {
150 o.optmulti("", "plugin-path", "removed", "DIR")
153 o.optmulti("C", "codegen", "pass a codegen option to rustc", "OPT[=VALUE]")
155 stable("passes", |o| {
156 o.optmulti("", "passes",
157 "list of passes to also run, you might want \
158 to pass it multiple times; a value of `list` \
159 will print available passes",
162 stable("plugins", |o| {
163 o.optmulti("", "plugins", "removed",
166 stable("no-default", |o| {
167 o.optflag("", "no-defaults", "don't run the default passes")
169 stable("document-private-items", |o| {
170 o.optflag("", "document-private-items", "document private items")
172 stable("test", |o| o.optflag("", "test", "run code examples as tests")),
173 stable("test-args", |o| {
174 o.optmulti("", "test-args", "arguments to pass to the test runner",
177 stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
178 stable("markdown-css", |o| {
179 o.optmulti("", "markdown-css",
180 "CSS files to include via <link> in a rendered Markdown file",
183 stable("html-in-header", |o| {
184 o.optmulti("", "html-in-header",
185 "files to include inline in the <head> section of a rendered Markdown file \
186 or generated documentation",
189 stable("html-before-content", |o| {
190 o.optmulti("", "html-before-content",
191 "files to include inline between <body> and the content of a rendered \
192 Markdown file or generated documentation",
195 stable("html-after-content", |o| {
196 o.optmulti("", "html-after-content",
197 "files to include inline between the content and </body> of a rendered \
198 Markdown file or generated documentation",
201 unstable("markdown-before-content", |o| {
202 o.optmulti("", "markdown-before-content",
203 "files to include inline between <body> and the content of a rendered \
204 Markdown file or generated documentation",
207 unstable("markdown-after-content", |o| {
208 o.optmulti("", "markdown-after-content",
209 "files to include inline between the content and </body> of a rendered \
210 Markdown file or generated documentation",
213 stable("markdown-playground-url", |o| {
214 o.optopt("", "markdown-playground-url",
215 "URL to send code snippets to", "URL")
217 stable("markdown-no-toc", |o| {
218 o.optflag("", "markdown-no-toc", "don't include table of contents")
221 o.optopt("e", "extend-css",
222 "To add some CSS rules with a given file to generate doc with your \
223 own theme. However, your theme might break if the rustdoc's generated HTML \
224 changes, so be careful!", "PATH")
228 "internal and debugging options (only on nightly build)", "FLAG")
230 stable("sysroot", |o| {
231 o.optopt("", "sysroot", "Override the system root", "PATH")
233 unstable("playground-url", |o| {
234 o.optopt("", "playground-url",
235 "URL to send code snippets to, may be reset by --markdown-playground-url \
236 or `#![doc(html_playground_url=...)]`",
239 unstable("display-warnings", |o| {
240 o.optflag("", "display-warnings", "to print code warnings when testing doc")
242 unstable("crate-version", |o| {
243 o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
245 unstable("linker", |o| {
246 o.optopt("", "linker", "linker used for building executable test code", "PATH")
248 unstable("sort-modules-by-appearance", |o| {
249 o.optflag("", "sort-modules-by-appearance", "sort modules by where they appear in the \
250 program, rather than alphabetically")
252 unstable("themes", |o| {
253 o.optmulti("", "themes",
254 "additional themes which will be added to the generated docs",
257 unstable("theme-checker", |o| {
258 o.optmulti("", "theme-checker",
259 "check if given theme is valid",
262 unstable("resource-suffix", |o| {
265 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
266 \"light-suffix.css\"",
269 stable("edition", |o| {
270 o.optopt("", "edition",
271 "edition to use when compiling rust code (default: 2015)",
274 stable("color", |o| {
277 "Configure coloring of output:
278 auto = colorize, if output goes to a tty (default);
279 always = always colorize output;
280 never = never colorize output",
283 stable("error-format", |o| {
286 "How errors and other messages are produced",
289 unstable("disable-minification", |o| {
291 "disable-minification",
292 "Disable minification applied on JS files")
295 o.optmulti("W", "warn", "Set lint warnings", "OPT")
297 stable("allow", |o| {
298 o.optmulti("A", "allow", "Set lint allowed", "OPT")
301 o.optmulti("D", "deny", "Set lint denied", "OPT")
303 stable("forbid", |o| {
304 o.optmulti("F", "forbid", "Set lint forbidden", "OPT")
306 stable("cap-lints", |o| {
310 "Set the most restrictive lint level. \
311 More restrictive lints are capped at this \
312 level. By default, it is at `forbid` level.",
316 unstable("index-page", |o| {
319 "Markdown file to be used as index page",
322 unstable("enable-index-page", |o| {
325 "To enable generation of the index page")
327 unstable("static-root-path", |o| {
330 "Path string to force loading static files from in output pages. \
331 If not set, uses combinations of '../' to reach the documentation root.",
334 unstable("disable-per-crate-search", |o| {
336 "disable-per-crate-search",
337 "disables generating the crate selector on the search box")
339 unstable("persist-doctests", |o| {
342 "Directory to persist doctest executables into",
345 unstable("generate-redirect-pages", |o| {
347 "generate-redirect-pages",
348 "Generate extra pages to support legacy URLs and tool links")
350 unstable("show-coverage", |o| {
353 "calculate percentage of public items with documentation")
358 fn usage(argv0: &str) {
359 let mut options = getopts::Options::new();
360 for option in opts() {
361 (option.apply)(&mut options);
363 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
366 fn main_args(args: &[String]) -> i32 {
367 let mut options = getopts::Options::new();
368 for option in opts() {
369 (option.apply)(&mut options);
371 let matches = match options.parse(&args[1..]) {
374 early_error(ErrorOutputType::default(), &err.to_string());
377 let options = match config::Options::from_matches(&matches) {
379 Err(code) => return code,
381 rustc_interface::interface::default_thread_pool(options.edition, move || {
382 main_options(options)
386 fn main_options(options: config::Options) -> i32 {
387 let diag = core::new_handler(options.error_format,
389 options.debugging_options.treat_err_as_bug,
390 options.debugging_options.ui_testing);
392 match (options.should_test, options.markdown_input()) {
393 (true, true) => return markdown::test(options, &diag),
394 (true, false) => return test::run(options),
395 (false, true) => return markdown::render(options.input,
396 options.render_options,
402 // need to move these items separately because we lose them by the time the closure is called,
403 // but we can't crates the Handler ahead of time because it's not Send
404 let diag_opts = (options.error_format,
405 options.debugging_options.treat_err_as_bug,
406 options.debugging_options.ui_testing,
408 let show_coverage = options.show_coverage;
409 rust_input(options, move |out| {
411 // if we ran coverage, bail early, we don't need to also generate docs at this point
412 // (also we didn't load in any of the useful passes)
413 return rustc_driver::EXIT_SUCCESS;
416 let Output { krate, passes, renderinfo, renderopts } = out;
417 info!("going to format");
418 let (error_format, treat_err_as_bug, ui_testing, edition) = diag_opts;
419 let diag = core::new_handler(error_format, None, treat_err_as_bug, ui_testing);
420 match html::render::run(
423 passes.into_iter().collect(),
428 Ok(_) => rustc_driver::EXIT_SUCCESS,
430 diag.struct_err(&format!("couldn't generate documentation: {}", e.error))
431 .note(&format!("failed to create or modify \"{}\"", e.file.display()))
433 rustc_driver::EXIT_FAILURE
439 /// Interprets the input file as a rust source file, passing it through the
440 /// compiler all the way through the analysis passes. The rustdoc output is then
441 /// generated from the cleaned AST of the crate.
443 /// This form of input will run all of the plug/cleaning passes
444 fn rust_input<R, F>(options: config::Options, f: F) -> R
445 where R: 'static + Send,
446 F: 'static + Send + FnOnce(Output) -> R
448 // First, parse the crate and extract all relevant information.
449 info!("starting to run rustc");
451 let (tx, rx) = channel();
453 let result = rustc_driver::report_ices_to_stderr_if_any(move || {
454 let crate_name = options.crate_name.clone();
455 let crate_version = options.crate_version.clone();
456 let (mut krate, renderinfo, renderopts, passes) = core::run_core(options);
458 info!("finished with rustc");
460 if let Some(name) = crate_name {
464 krate.version = crate_version;
468 renderinfo: renderinfo,
475 Ok(()) => rx.recv().unwrap(),
476 Err(_) => panic::resume_unwind(Box::new(errors::FatalErrorMarker)),