1 #![deny(rust_2018_idioms)]
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 #![recursion_limit="256"]
27 extern crate env_logger;
29 extern crate rustc_data_structures;
30 extern crate rustc_driver;
31 extern crate rustc_resolve;
32 extern crate rustc_lint;
33 extern crate rustc_interface;
34 extern crate rustc_metadata;
35 extern crate rustc_target;
36 extern crate rustc_typeck;
37 extern crate serialize;
39 extern crate syntax_pos;
40 extern crate test as testing;
41 #[macro_use] extern crate log;
42 extern crate rustc_errors as errors;
44 extern crate serialize as rustc_serialize; // used by deriving
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};
71 crate mod static_files;
83 renderinfo: html::render::RenderInfo,
84 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();
96 let res = std::thread::Builder::new().stack_size(thread_stack_size).spawn(move || {
97 rustc_interface::interface::default_thread_pool(move || {
98 get_args().map(|args| main_args(&args)).unwrap_or(1)
100 }).unwrap().join().unwrap_or(rustc_driver::EXIT_FAILURE);
104 fn get_args() -> Option<Vec<String>> {
105 env::args_os().enumerate()
106 .map(|(i, arg)| arg.into_string().map_err(|arg| {
107 early_warn(ErrorOutputType::default(),
108 &format!("Argument {} is not valid Unicode: {:?}", i, arg));
113 fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
114 where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
116 RustcOptGroup::stable(name, f)
119 fn unstable<F>(name: &'static str, f: F) -> RustcOptGroup
120 where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
122 RustcOptGroup::unstable(name, f)
125 fn opts() -> Vec<RustcOptGroup> {
127 stable("h", |o| o.optflag("h", "help", "show this help message")),
128 stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
129 stable("v", |o| o.optflag("v", "verbose", "use verbose output")),
131 o.optopt("r", "input-format", "the input type of the specified file",
135 o.optopt("w", "output-format", "the output type to write", "[html]")
137 stable("o", |o| o.optopt("o", "output", "where to place the output", "PATH")),
138 stable("crate-name", |o| {
139 o.optopt("", "crate-name", "specify the name of this crate", "NAME")
142 o.optmulti("L", "library-path", "directory to add to crate search path",
145 stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
146 stable("extern", |o| {
147 o.optmulti("", "extern", "pass an --extern to rustc", "NAME=PATH")
149 unstable("extern-html-root-url", |o| {
150 o.optmulti("", "extern-html-root-url",
151 "base URL to use for dependencies", "NAME=URL")
153 stable("plugin-path", |o| {
154 o.optmulti("", "plugin-path", "removed", "DIR")
157 o.optmulti("C", "codegen", "pass a codegen option to rustc", "OPT[=VALUE]")
159 stable("passes", |o| {
160 o.optmulti("", "passes",
161 "list of passes to also run, you might want \
162 to pass it multiple times; a value of `list` \
163 will print available passes",
166 stable("plugins", |o| {
167 o.optmulti("", "plugins", "removed",
170 stable("no-default", |o| {
171 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",
181 stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
182 stable("markdown-css", |o| {
183 o.optmulti("", "markdown-css",
184 "CSS files to include via <link> in a rendered Markdown file",
187 stable("html-in-header", |o| {
188 o.optmulti("", "html-in-header",
189 "files to include inline in the <head> section of a rendered Markdown file \
190 or generated documentation",
193 stable("html-before-content", |o| {
194 o.optmulti("", "html-before-content",
195 "files to include inline between <body> and the content of a rendered \
196 Markdown file or generated documentation",
199 stable("html-after-content", |o| {
200 o.optmulti("", "html-after-content",
201 "files to include inline between the content and </body> of a rendered \
202 Markdown file or generated documentation",
205 unstable("markdown-before-content", |o| {
206 o.optmulti("", "markdown-before-content",
207 "files to include inline between <body> and the content of a rendered \
208 Markdown file or generated documentation",
211 unstable("markdown-after-content", |o| {
212 o.optmulti("", "markdown-after-content",
213 "files to include inline between the content and </body> of a rendered \
214 Markdown file or generated documentation",
217 stable("markdown-playground-url", |o| {
218 o.optopt("", "markdown-playground-url",
219 "URL to send code snippets to", "URL")
221 stable("markdown-no-toc", |o| {
222 o.optflag("", "markdown-no-toc", "don't include table of contents")
225 o.optopt("e", "extend-css",
226 "To add some CSS rules with a given file to generate doc with your \
227 own theme. However, your theme might break if the rustdoc's generated HTML \
228 changes, so be careful!", "PATH")
232 "internal and debugging options (only on nightly build)", "FLAG")
234 stable("sysroot", |o| {
235 o.optopt("", "sysroot", "Override the system root", "PATH")
237 unstable("playground-url", |o| {
238 o.optopt("", "playground-url",
239 "URL to send code snippets to, may be reset by --markdown-playground-url \
240 or `#![doc(html_playground_url=...)]`",
243 unstable("display-warnings", |o| {
244 o.optflag("", "display-warnings", "to print code warnings when testing doc")
246 unstable("crate-version", |o| {
247 o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
249 unstable("linker", |o| {
250 o.optopt("", "linker", "linker used for building executable test code", "PATH")
252 unstable("sort-modules-by-appearance", |o| {
253 o.optflag("", "sort-modules-by-appearance", "sort modules by where they appear in the \
254 program, rather than alphabetically")
256 unstable("themes", |o| {
257 o.optmulti("", "themes",
258 "additional themes which will be added to the generated docs",
261 unstable("theme-checker", |o| {
262 o.optmulti("", "theme-checker",
263 "check if given theme is valid",
266 unstable("resource-suffix", |o| {
269 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
270 \"light-suffix.css\"",
273 stable("edition", |o| {
274 o.optopt("", "edition",
275 "edition to use when compiling rust code (default: 2015)",
278 stable("color", |o| {
281 "Configure coloring of output:
282 auto = colorize, if output goes to a tty (default);
283 always = always colorize output;
284 never = never colorize output",
287 stable("error-format", |o| {
290 "How errors and other messages are produced",
293 unstable("disable-minification", |o| {
295 "disable-minification",
296 "Disable minification applied on JS files")
299 o.optmulti("W", "warn", "Set lint warnings", "OPT")
301 stable("allow", |o| {
302 o.optmulti("A", "allow", "Set lint allowed", "OPT")
305 o.optmulti("D", "deny", "Set lint denied", "OPT")
307 stable("forbid", |o| {
308 o.optmulti("F", "forbid", "Set lint forbidden", "OPT")
310 stable("cap-lints", |o| {
314 "Set the most restrictive lint level. \
315 More restrictive lints are capped at this \
316 level. By default, it is at `forbid` level.",
320 unstable("index-page", |o| {
323 "Markdown file to be used as index page",
326 unstable("enable-index-page", |o| {
329 "To enable generation of the index page")
331 unstable("static-root-path", |o| {
334 "Path string to force loading static files from in output pages. \
335 If not set, uses combinations of '../' to reach the documentation root.",
338 unstable("disable-per-crate-search", |o| {
340 "disable-per-crate-search",
341 "disables generating the crate selector on the search box")
343 unstable("persist-doctests", |o| {
346 "Directory to persist doctest executables into",
349 unstable("generate-redirect-pages", |o| {
351 "generate-redirect-pages",
352 "Generate extra pages to support legacy URLs and tool links")
354 unstable("show-coverage", |o| {
357 "calculate percentage of public items with documentation")
362 fn usage(argv0: &str) {
363 let mut options = getopts::Options::new();
364 for option in opts() {
365 (option.apply)(&mut options);
367 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
370 fn main_args(args: &[String]) -> i32 {
371 let mut options = getopts::Options::new();
372 for option in opts() {
373 (option.apply)(&mut options);
375 let matches = match options.parse(&args[1..]) {
378 early_error(ErrorOutputType::default(), &err.to_string());
381 let options = match config::Options::from_matches(&matches) {
383 Err(code) => return code,
386 let diag = core::new_handler(options.error_format,
388 options.debugging_options.treat_err_as_bug,
389 options.debugging_options.ui_testing);
391 match (options.should_test, options.markdown_input()) {
392 (true, true) => return markdown::test(options, &diag),
393 (true, false) => return test::run(options),
394 (false, true) => return markdown::render(options.input,
395 options.render_options,
401 // need to move these items separately because we lose them by the time the closure is called,
402 // but we can't crates the Handler ahead of time because it's not Send
403 let diag_opts = (options.error_format,
404 options.debugging_options.treat_err_as_bug,
405 options.debugging_options.ui_testing,
407 let show_coverage = options.show_coverage;
408 rust_input(options, move |out| {
410 // if we ran coverage, bail early, we don't need to also generate docs at this point
411 // (also we didn't load in any of the useful passes)
412 return rustc_driver::EXIT_SUCCESS;
415 let Output { krate, passes, renderinfo, renderopts } = out;
416 info!("going to format");
417 let (error_format, treat_err_as_bug, ui_testing, edition) = diag_opts;
418 let diag = core::new_handler(error_format, None, treat_err_as_bug, ui_testing);
419 match html::render::run(
422 passes.into_iter().collect(),
427 Ok(_) => rustc_driver::EXIT_SUCCESS,
429 diag.struct_err(&format!("couldn't generate documentation: {}", e.error))
430 .note(&format!("failed to create or modify \"{}\"", e.file.display()))
432 rustc_driver::EXIT_FAILURE
438 /// Interprets the input file as a rust source file, passing it through the
439 /// compiler all the way through the analysis passes. The rustdoc output is then
440 /// generated from the cleaned AST of the crate.
442 /// This form of input will run all of the plug/cleaning passes
443 fn rust_input<R, F>(options: config::Options, f: F) -> R
444 where R: 'static + Send,
445 F: 'static + Send + FnOnce(Output) -> R
447 // First, parse the crate and extract all relevant information.
448 info!("starting to run rustc");
450 let (tx, rx) = channel();
452 let result = rustc_driver::report_ices_to_stderr_if_any(move || {
453 let crate_name = options.crate_name.clone();
454 let crate_version = options.crate_version.clone();
455 let (mut krate, renderinfo, renderopts, passes) = core::run_core(options);
457 info!("finished with rustc");
459 if let Some(name) = crate_name {
463 krate.version = crate_version;
467 renderinfo: renderinfo,
474 Ok(()) => rx.recv().unwrap(),
475 Err(_) => panic::resume_unwind(Box::new(errors::FatalErrorMarker)),