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(box_patterns)]
7 #![feature(box_syntax)]
11 #![feature(vec_remove_item)]
12 #![feature(ptr_offset_from)]
13 #![feature(crate_visibility_modifier)]
15 #![feature(drain_filter)]
16 #![feature(inner_deref)]
18 #![recursion_limit="256"]
22 extern crate env_logger;
24 extern crate rustc_data_structures;
25 extern crate rustc_codegen_utils;
26 extern crate rustc_driver;
27 extern crate rustc_resolve;
28 extern crate rustc_lint;
29 extern crate rustc_metadata;
30 extern crate rustc_target;
31 extern crate rustc_typeck;
32 extern crate serialize;
34 extern crate syntax_pos;
35 extern crate test as testing;
36 #[macro_use] extern crate log;
37 extern crate rustc_errors as errors;
38 extern crate pulldown_cmark;
39 extern crate tempfile;
40 extern crate minifier;
41 extern crate parking_lot;
43 extern crate serialize as rustc_serialize; // used by deriving
45 use std::default::Default;
49 use std::sync::mpsc::channel;
51 use rustc::session::{early_warn, early_error};
52 use rustc::session::config::{ErrorOutputType, RustcOptGroup};
70 crate mod static_files;
82 renderinfo: html::render::RenderInfo,
83 renderopts: config::RenderOptions,
88 let thread_stack_size: usize = if cfg!(target_os = "haiku") {
89 16_000_000 // 16MB on Haiku
91 32_000_000 // 32MB on other platforms
93 rustc_driver::set_sigpipe_handler();
95 let res = std::thread::Builder::new().stack_size(thread_stack_size).spawn(move || {
96 syntax::with_globals(move || {
97 get_args().map(|args| main_args(&args)).unwrap_or(1)
99 }).unwrap().join().unwrap_or(rustc_driver::EXIT_FAILURE);
100 process::exit(res as i32);
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",
351 fn usage(argv0: &str) {
352 let mut options = getopts::Options::new();
353 for option in opts() {
354 (option.apply)(&mut options);
356 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
359 fn main_args(args: &[String]) -> isize {
360 let mut options = getopts::Options::new();
361 for option in opts() {
362 (option.apply)(&mut options);
364 let matches = match options.parse(&args[1..]) {
367 early_error(ErrorOutputType::default(), &err.to_string());
370 let options = match config::Options::from_matches(&matches) {
372 Err(code) => return code,
375 let diag = core::new_handler(options.error_format,
377 options.debugging_options.treat_err_as_bug,
378 options.debugging_options.ui_testing);
380 match (options.should_test, options.markdown_input()) {
381 (true, true) => return markdown::test(options, &diag),
382 (true, false) => return test::run(options),
383 (false, true) => return markdown::render(options.input, options.render_options, &diag),
387 // need to move these items separately because we lose them by the time the closure is called,
388 // but we can't crates the Handler ahead of time because it's not Send
389 let diag_opts = (options.error_format,
390 options.debugging_options.treat_err_as_bug,
391 options.debugging_options.ui_testing);
392 rust_input(options, move |out| {
393 let Output { krate, passes, renderinfo, renderopts } = out;
394 info!("going to format");
395 let (error_format, treat_err_as_bug, ui_testing) = diag_opts;
396 let diag = core::new_handler(error_format, None, treat_err_as_bug, ui_testing);
397 match html::render::run(
400 passes.into_iter().collect(),
404 Ok(_) => rustc_driver::EXIT_SUCCESS,
406 diag.struct_err(&format!("couldn't generate documentation: {}", e.error))
407 .note(&format!("failed to create or modify \"{}\"", e.file.display()))
409 rustc_driver::EXIT_FAILURE
415 /// Interprets the input file as a rust source file, passing it through the
416 /// compiler all the way through the analysis passes. The rustdoc output is then
417 /// generated from the cleaned AST of the crate.
419 /// This form of input will run all of the plug/cleaning passes
420 fn rust_input<R, F>(options: config::Options, f: F) -> R
421 where R: 'static + Send,
422 F: 'static + Send + FnOnce(Output) -> R
424 // First, parse the crate and extract all relevant information.
425 info!("starting to run rustc");
427 let (tx, rx) = channel();
429 let result = rustc_driver::monitor(move || syntax::with_globals(move || {
430 let crate_name = options.crate_name.clone();
431 let crate_version = options.crate_version.clone();
432 let (mut krate, renderinfo, renderopts, passes) = core::run_core(options);
434 info!("finished with rustc");
436 if let Some(name) = crate_name {
440 krate.version = crate_version;
442 info!("Executing passes");
444 for pass in &passes {
445 // determine if we know about this pass
446 let pass = match passes::find_pass(pass) {
447 Some(pass) => if let Some(pass) = pass.late_fn() {
450 // not a late pass, but still valid so don't report the error
454 error!("unknown pass {}, skipping", *pass);
466 renderinfo: renderinfo,
473 Ok(()) => rx.recv().unwrap(),
474 Err(_) => panic::resume_unwind(Box::new(errors::FatalErrorMarker)),