1 #![deny(rust_2018_idioms)]
3 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
4 html_playground_url = "https://play.rust-lang.org/")]
6 #![feature(bind_by_move_pattern_guards)]
7 #![feature(rustc_private)]
8 #![feature(arbitrary_self_types)]
9 #![feature(box_patterns)]
10 #![feature(box_syntax)]
12 #![feature(set_stdio)]
14 #![feature(vec_remove_item)]
15 #![feature(ptr_offset_from)]
16 #![feature(crate_visibility_modifier)]
18 #![feature(drain_filter)]
19 #![feature(inner_deref)]
21 #![recursion_limit="256"]
24 extern crate env_logger;
26 extern crate rustc_data_structures;
27 extern crate rustc_driver;
28 extern crate rustc_resolve;
29 extern crate rustc_lint;
30 extern crate rustc_interface;
31 extern crate rustc_metadata;
32 extern crate rustc_target;
33 extern crate rustc_typeck;
34 extern crate serialize;
36 extern crate syntax_pos;
37 extern crate test as testing;
38 #[macro_use] extern crate log;
39 extern crate rustc_errors as errors;
41 extern crate serialize as rustc_serialize; // used by deriving
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};
68 crate mod static_files;
80 renderinfo: html::render::RenderInfo,
81 renderopts: config::RenderOptions,
86 let thread_stack_size: usize = if cfg!(target_os = "haiku") {
87 16_000_000 // 16MB on Haiku
89 32_000_000 // 32MB on other platforms
91 rustc_driver::set_sigpipe_handler();
93 let res = std::thread::Builder::new().stack_size(thread_stack_size).spawn(move || {
94 rustc_interface::interface::default_thread_pool(move || {
95 get_args().map(|args| main_args(&args)).unwrap_or(1)
97 }).unwrap().join().unwrap_or(rustc_driver::EXIT_FAILURE);
101 fn get_args() -> Option<Vec<String>> {
102 env::args_os().enumerate()
103 .map(|(i, arg)| arg.into_string().map_err(|arg| {
104 early_warn(ErrorOutputType::default(),
105 &format!("Argument {} is not valid Unicode: {:?}", i, arg));
110 fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
111 where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
113 RustcOptGroup::stable(name, f)
116 fn unstable<F>(name: &'static str, f: F) -> RustcOptGroup
117 where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
119 RustcOptGroup::unstable(name, f)
122 fn opts() -> Vec<RustcOptGroup> {
124 stable("h", |o| o.optflag("h", "help", "show this help message")),
125 stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
126 stable("v", |o| o.optflag("v", "verbose", "use verbose output")),
128 o.optopt("r", "input-format", "the input type of the specified file",
132 o.optopt("w", "output-format", "the output type to write", "[html]")
134 stable("o", |o| o.optopt("o", "output", "where to place the output", "PATH")),
135 stable("crate-name", |o| {
136 o.optopt("", "crate-name", "specify the name of this crate", "NAME")
139 o.optmulti("L", "library-path", "directory to add to crate search path",
142 stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
143 stable("extern", |o| {
144 o.optmulti("", "extern", "pass an --extern to rustc", "NAME=PATH")
146 unstable("extern-html-root-url", |o| {
147 o.optmulti("", "extern-html-root-url",
148 "base URL to use for dependencies", "NAME=URL")
150 stable("plugin-path", |o| {
151 o.optmulti("", "plugin-path", "removed", "DIR")
154 o.optmulti("C", "codegen", "pass a codegen option to rustc", "OPT[=VALUE]")
156 stable("passes", |o| {
157 o.optmulti("", "passes",
158 "list of passes to also run, you might want \
159 to pass it multiple times; a value of `list` \
160 will print available passes",
163 stable("plugins", |o| {
164 o.optmulti("", "plugins", "removed",
167 stable("no-default", |o| {
168 o.optflag("", "no-defaults", "don't run the default passes")
170 stable("document-private-items", |o| {
171 o.optflag("", "document-private-items", "document private items")
173 stable("test", |o| o.optflag("", "test", "run code examples as tests")),
174 stable("test-args", |o| {
175 o.optmulti("", "test-args", "arguments to pass to the test runner",
178 stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
179 stable("markdown-css", |o| {
180 o.optmulti("", "markdown-css",
181 "CSS files to include via <link> in a rendered Markdown file",
184 stable("html-in-header", |o| {
185 o.optmulti("", "html-in-header",
186 "files to include inline in the <head> section of a rendered Markdown file \
187 or generated documentation",
190 stable("html-before-content", |o| {
191 o.optmulti("", "html-before-content",
192 "files to include inline between <body> and the content of a rendered \
193 Markdown file or generated documentation",
196 stable("html-after-content", |o| {
197 o.optmulti("", "html-after-content",
198 "files to include inline between the content and </body> of a rendered \
199 Markdown file or generated documentation",
202 unstable("markdown-before-content", |o| {
203 o.optmulti("", "markdown-before-content",
204 "files to include inline between <body> and the content of a rendered \
205 Markdown file or generated documentation",
208 unstable("markdown-after-content", |o| {
209 o.optmulti("", "markdown-after-content",
210 "files to include inline between the content and </body> of a rendered \
211 Markdown file or generated documentation",
214 stable("markdown-playground-url", |o| {
215 o.optopt("", "markdown-playground-url",
216 "URL to send code snippets to", "URL")
218 stable("markdown-no-toc", |o| {
219 o.optflag("", "markdown-no-toc", "don't include table of contents")
222 o.optopt("e", "extend-css",
223 "To add some CSS rules with a given file to generate doc with your \
224 own theme. However, your theme might break if the rustdoc's generated HTML \
225 changes, so be careful!", "PATH")
229 "internal and debugging options (only on nightly build)", "FLAG")
231 stable("sysroot", |o| {
232 o.optopt("", "sysroot", "Override the system root", "PATH")
234 unstable("playground-url", |o| {
235 o.optopt("", "playground-url",
236 "URL to send code snippets to, may be reset by --markdown-playground-url \
237 or `#![doc(html_playground_url=...)]`",
240 unstable("display-warnings", |o| {
241 o.optflag("", "display-warnings", "to print code warnings when testing doc")
243 unstable("crate-version", |o| {
244 o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
246 unstable("linker", |o| {
247 o.optopt("", "linker", "linker used for building executable test code", "PATH")
249 unstable("sort-modules-by-appearance", |o| {
250 o.optflag("", "sort-modules-by-appearance", "sort modules by where they appear in the \
251 program, rather than alphabetically")
253 unstable("themes", |o| {
254 o.optmulti("", "themes",
255 "additional themes which will be added to the generated docs",
258 unstable("theme-checker", |o| {
259 o.optmulti("", "theme-checker",
260 "check if given theme is valid",
263 unstable("resource-suffix", |o| {
266 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
267 \"light-suffix.css\"",
270 stable("edition", |o| {
271 o.optopt("", "edition",
272 "edition to use when compiling rust code (default: 2015)",
275 stable("color", |o| {
278 "Configure coloring of output:
279 auto = colorize, if output goes to a tty (default);
280 always = always colorize output;
281 never = never colorize output",
284 stable("error-format", |o| {
287 "How errors and other messages are produced",
290 unstable("disable-minification", |o| {
292 "disable-minification",
293 "Disable minification applied on JS files")
296 o.optmulti("W", "warn", "Set lint warnings", "OPT")
298 stable("allow", |o| {
299 o.optmulti("A", "allow", "Set lint allowed", "OPT")
302 o.optmulti("D", "deny", "Set lint denied", "OPT")
304 stable("forbid", |o| {
305 o.optmulti("F", "forbid", "Set lint forbidden", "OPT")
307 stable("cap-lints", |o| {
311 "Set the most restrictive lint level. \
312 More restrictive lints are capped at this \
313 level. By default, it is at `forbid` level.",
317 unstable("index-page", |o| {
320 "Markdown file to be used as index page",
323 unstable("enable-index-page", |o| {
326 "To enable generation of the index page")
328 unstable("static-root-path", |o| {
331 "Path string to force loading static files from in output pages. \
332 If not set, uses combinations of '../' to reach the documentation root.",
335 unstable("disable-per-crate-search", |o| {
337 "disable-per-crate-search",
338 "disables generating the crate selector on the search box")
340 unstable("persist-doctests", |o| {
343 "Directory to persist doctest executables into",
346 unstable("generate-redirect-pages", |o| {
348 "generate-redirect-pages",
349 "Generate extra pages to support legacy URLs and tool links")
351 unstable("show-coverage", |o| {
354 "calculate percentage of public items with documentation")
359 fn usage(argv0: &str) {
360 let mut options = getopts::Options::new();
361 for option in opts() {
362 (option.apply)(&mut options);
364 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
367 fn main_args(args: &[String]) -> i32 {
368 let mut options = getopts::Options::new();
369 for option in opts() {
370 (option.apply)(&mut options);
372 let matches = match options.parse(&args[1..]) {
375 early_error(ErrorOutputType::default(), &err.to_string());
378 let options = match config::Options::from_matches(&matches) {
380 Err(code) => return code,
383 let diag = core::new_handler(options.error_format,
385 options.debugging_options.treat_err_as_bug,
386 options.debugging_options.ui_testing);
388 match (options.should_test, options.markdown_input()) {
389 (true, true) => return markdown::test(options, &diag),
390 (true, false) => return test::run(options),
391 (false, true) => return markdown::render(options.input, options.render_options, &diag),
395 // need to move these items separately because we lose them by the time the closure is called,
396 // but we can't crates the Handler ahead of time because it's not Send
397 let diag_opts = (options.error_format,
398 options.debugging_options.treat_err_as_bug,
399 options.debugging_options.ui_testing);
400 let show_coverage = options.show_coverage;
401 rust_input(options, move |out| {
403 // if we ran coverage, bail early, we don't need to also generate docs at this point
404 // (also we didn't load in any of the useful passes)
405 return rustc_driver::EXIT_SUCCESS;
408 let Output { krate, passes, renderinfo, renderopts } = out;
409 info!("going to format");
410 let (error_format, treat_err_as_bug, ui_testing) = diag_opts;
411 let diag = core::new_handler(error_format, None, treat_err_as_bug, ui_testing);
412 match html::render::run(
415 passes.into_iter().collect(),
419 Ok(_) => rustc_driver::EXIT_SUCCESS,
421 diag.struct_err(&format!("couldn't generate documentation: {}", e.error))
422 .note(&format!("failed to create or modify \"{}\"", e.file.display()))
424 rustc_driver::EXIT_FAILURE
430 /// Interprets the input file as a rust source file, passing it through the
431 /// compiler all the way through the analysis passes. The rustdoc output is then
432 /// generated from the cleaned AST of the crate.
434 /// This form of input will run all of the plug/cleaning passes
435 fn rust_input<R, F>(options: config::Options, f: F) -> R
436 where R: 'static + Send,
437 F: 'static + Send + FnOnce(Output) -> R
439 // First, parse the crate and extract all relevant information.
440 info!("starting to run rustc");
442 let (tx, rx) = channel();
444 let result = rustc_driver::report_ices_to_stderr_if_any(move || syntax::with_globals(move || {
445 let crate_name = options.crate_name.clone();
446 let crate_version = options.crate_version.clone();
447 let (mut krate, renderinfo, renderopts, passes) = core::run_core(options);
449 info!("finished with rustc");
451 if let Some(name) = crate_name {
455 krate.version = crate_version;
459 renderinfo: renderinfo,
466 Ok(()) => rx.recv().unwrap(),
467 Err(_) => panic::resume_unwind(Box::new(errors::FatalErrorMarker)),