]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/lib.rs
27bef5db2d6d2740f451940d8e1adf6ca6d7ffa4
[rust.git] / src / librustdoc / lib.rs
1 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/",
2        html_playground_url = "https://play.rust-lang.org/")]
3
4 #![feature(rustc_private)]
5 #![feature(arbitrary_self_types)]
6 #![feature(box_patterns)]
7 #![feature(box_syntax)]
8 #![feature(in_band_lifetimes)]
9 #![feature(nll)]
10 #![feature(set_stdio)]
11 #![feature(test)]
12 #![feature(vec_remove_item)]
13 #![feature(ptr_offset_from)]
14 #![feature(crate_visibility_modifier)]
15 #![feature(const_fn)]
16 #![feature(drain_filter)]
17 #![feature(never_type)]
18 #![feature(unicode_internals)]
19
20 #![recursion_limit="256"]
21
22 extern crate getopts;
23 extern crate env_logger;
24 extern crate rustc;
25 extern crate rustc_data_structures;
26 extern crate rustc_index;
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_parse;
33 extern crate rustc_target;
34 extern crate rustc_typeck;
35 extern crate rustc_lexer;
36 extern crate serialize;
37 extern crate syntax;
38 extern crate syntax_expand;
39 extern crate syntax_pos;
40 extern crate test as testing;
41 #[macro_use] extern crate log;
42 extern crate rustc_errors as errors;
43
44 use std::default::Default;
45 use std::env;
46 use std::panic;
47 use std::process;
48
49 use rustc::session::{early_warn, early_error};
50 use rustc::session::config::{ErrorOutputType, RustcOptGroup, make_crate_type_option};
51
52 #[macro_use]
53 mod externalfiles;
54
55 mod clean;
56 mod config;
57 mod core;
58 mod docfs;
59 mod doctree;
60 mod fold;
61 pub mod html {
62     crate mod highlight;
63     crate mod escape;
64     crate mod item_type;
65     crate mod format;
66     crate mod layout;
67     pub mod markdown;
68     crate mod render;
69     crate mod static_files;
70     crate mod toc;
71     crate mod sources;
72 }
73 mod markdown;
74 mod passes;
75 mod visit_ast;
76 mod visit_lib;
77 mod test;
78 mod theme;
79
80 struct Output {
81     krate: clean::Crate,
82     renderinfo: html::render::RenderInfo,
83     renderopts: config::RenderOptions,
84 }
85
86 pub fn main() {
87     let thread_stack_size: usize = if cfg!(target_os = "haiku") {
88         16_000_000 // 16MB on Haiku
89     } else {
90         32_000_000 // 32MB on other platforms
91     };
92     rustc_driver::set_sigpipe_handler();
93     env_logger::init_from_env("RUSTDOC_LOG");
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);
97     process::exit(res);
98 }
99
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));
105         }).ok())
106         .collect()
107 }
108
109 fn stable<F>(name: &'static str, f: F) -> RustcOptGroup
110     where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
111 {
112     RustcOptGroup::stable(name, f)
113 }
114
115 fn unstable<F>(name: &'static str, f: F) -> RustcOptGroup
116     where F: Fn(&mut getopts::Options) -> &mut getopts::Options + 'static
117 {
118     RustcOptGroup::unstable(name, f)
119 }
120
121 fn opts() -> Vec<RustcOptGroup> {
122     vec![
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")),
126         stable("r", |o| {
127             o.optopt("r", "input-format", "the input type of the specified file",
128                      "[rust]")
129         }),
130         stable("w", |o| {
131             o.optopt("w", "output-format", "the output type to write", "[html]")
132         }),
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")
136         }),
137         make_crate_type_option(),
138         stable("L", |o| {
139             o.optmulti("L", "library-path", "directory to add to crate search path",
140                        "DIR")
141         }),
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]")
145         }),
146         stable("extern-private", |o| {
147             o.optmulti("", "extern-private",
148                        "pass an --extern to rustc (compatibility only)", "NAME=PATH")
149         }),
150         unstable("extern-html-root-url", |o| {
151             o.optmulti("", "extern-html-root-url",
152                        "base URL to use for dependencies", "NAME=URL")
153         }),
154         stable("plugin-path", |o| {
155             o.optmulti("", "plugin-path", "removed", "DIR")
156         }),
157         stable("C", |o| {
158             o.optmulti("C", "codegen", "pass a codegen option to rustc", "OPT[=VALUE]")
159         }),
160         stable("passes", |o| {
161             o.optmulti("", "passes",
162                        "list of passes to also run, you might want \
163                         to pass it multiple times; a value of `list` \
164                         will print available passes",
165                        "PASSES")
166         }),
167         stable("plugins", |o| {
168             o.optmulti("", "plugins", "removed",
169                        "PLUGINS")
170         }),
171         stable("no-default", |o| {
172             o.optflag("", "no-defaults", "don't run the default passes")
173         }),
174         stable("document-private-items", |o| {
175             o.optflag("", "document-private-items", "document private items")
176         }),
177         stable("test", |o| o.optflag("", "test", "run code examples as tests")),
178         stable("test-args", |o| {
179             o.optmulti("", "test-args", "arguments to pass to the test runner",
180                        "ARGS")
181         }),
182         stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
183         stable("markdown-css", |o| {
184             o.optmulti("", "markdown-css",
185                        "CSS files to include via <link> in a rendered Markdown file",
186                        "FILES")
187         }),
188         stable("html-in-header", |o|  {
189             o.optmulti("", "html-in-header",
190                        "files to include inline in the <head> section of a rendered Markdown file \
191                         or generated documentation",
192                        "FILES")
193         }),
194         stable("html-before-content", |o| {
195             o.optmulti("", "html-before-content",
196                        "files to include inline between <body> and the content of a rendered \
197                         Markdown file or generated documentation",
198                        "FILES")
199         }),
200         stable("html-after-content", |o| {
201             o.optmulti("", "html-after-content",
202                        "files to include inline between the content and </body> of a rendered \
203                         Markdown file or generated documentation",
204                        "FILES")
205         }),
206         unstable("markdown-before-content", |o| {
207             o.optmulti("", "markdown-before-content",
208                        "files to include inline between <body> and the content of a rendered \
209                         Markdown file or generated documentation",
210                        "FILES")
211         }),
212         unstable("markdown-after-content", |o| {
213             o.optmulti("", "markdown-after-content",
214                        "files to include inline between the content and </body> of a rendered \
215                         Markdown file or generated documentation",
216                        "FILES")
217         }),
218         stable("markdown-playground-url", |o| {
219             o.optopt("", "markdown-playground-url",
220                      "URL to send code snippets to", "URL")
221         }),
222         stable("markdown-no-toc", |o| {
223             o.optflag("", "markdown-no-toc", "don't include table of contents")
224         }),
225         stable("e", |o| {
226             o.optopt("e", "extend-css",
227                      "To add some CSS rules with a given file to generate doc with your \
228                       own theme. However, your theme might break if the rustdoc's generated HTML \
229                       changes, so be careful!", "PATH")
230         }),
231         unstable("Z", |o| {
232             o.optmulti("Z", "",
233                        "internal and debugging options (only on nightly build)", "FLAG")
234         }),
235         stable("sysroot", |o| {
236             o.optopt("", "sysroot", "Override the system root", "PATH")
237         }),
238         unstable("playground-url", |o| {
239             o.optopt("", "playground-url",
240                      "URL to send code snippets to, may be reset by --markdown-playground-url \
241                       or `#![doc(html_playground_url=...)]`",
242                      "URL")
243         }),
244         unstable("display-warnings", |o| {
245             o.optflag("", "display-warnings", "to print code warnings when testing doc")
246         }),
247         unstable("crate-version", |o| {
248             o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
249         }),
250         unstable("sort-modules-by-appearance", |o| {
251             o.optflag("", "sort-modules-by-appearance", "sort modules by where they appear in the \
252                                                          program, rather than alphabetically")
253         }),
254         stable("themes", |o| {
255             o.optmulti("", "themes",
256                        "additional themes which will be added to the generated docs",
257                        "FILES")
258         }),
259         stable("theme-checker", |o| {
260             o.optmulti("", "theme-checker",
261                        "check if given theme is valid",
262                        "FILES")
263         }),
264         unstable("resource-suffix", |o| {
265             o.optopt("",
266                      "resource-suffix",
267                      "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
268                       \"light-suffix.css\"",
269                      "PATH")
270         }),
271         stable("edition", |o| {
272             o.optopt("", "edition",
273                      "edition to use when compiling rust code (default: 2015)",
274                      "EDITION")
275         }),
276         stable("color", |o| {
277             o.optopt("",
278                      "color",
279                      "Configure coloring of output:
280                                           auto   = colorize, if output goes to a tty (default);
281                                           always = always colorize output;
282                                           never  = never colorize output",
283                      "auto|always|never")
284         }),
285         stable("error-format", |o| {
286             o.optopt("",
287                      "error-format",
288                      "How errors and other messages are produced",
289                      "human|json|short")
290         }),
291         stable("json", |o| {
292             o.optopt("",
293                      "json",
294                      "Configure the structure of JSON diagnostics",
295                      "CONFIG")
296         }),
297         unstable("disable-minification", |o| {
298              o.optflag("",
299                        "disable-minification",
300                        "Disable minification applied on JS files")
301         }),
302         stable("warn", |o| {
303             o.optmulti("W", "warn", "Set lint warnings", "OPT")
304         }),
305         stable("allow", |o| {
306             o.optmulti("A", "allow", "Set lint allowed", "OPT")
307         }),
308         stable("deny", |o| {
309             o.optmulti("D", "deny", "Set lint denied", "OPT")
310         }),
311         stable("forbid", |o| {
312             o.optmulti("F", "forbid", "Set lint forbidden", "OPT")
313         }),
314         stable("cap-lints", |o| {
315             o.optmulti(
316                 "",
317                 "cap-lints",
318                 "Set the most restrictive lint level. \
319                  More restrictive lints are capped at this \
320                  level. By default, it is at `forbid` level.",
321                 "LEVEL",
322             )
323         }),
324         unstable("index-page", |o| {
325              o.optopt("",
326                       "index-page",
327                       "Markdown file to be used as index page",
328                       "PATH")
329         }),
330         unstable("enable-index-page", |o| {
331              o.optflag("",
332                        "enable-index-page",
333                        "To enable generation of the index page")
334         }),
335         unstable("static-root-path", |o| {
336             o.optopt("",
337                      "static-root-path",
338                      "Path string to force loading static files from in output pages. \
339                       If not set, uses combinations of '../' to reach the documentation root.",
340                      "PATH")
341         }),
342         unstable("disable-per-crate-search", |o| {
343             o.optflag("",
344                       "disable-per-crate-search",
345                       "disables generating the crate selector on the search box")
346         }),
347         unstable("persist-doctests", |o| {
348              o.optopt("",
349                        "persist-doctests",
350                        "Directory to persist doctest executables into",
351                        "PATH")
352         }),
353         unstable("generate-redirect-pages", |o| {
354             o.optflag("",
355                       "generate-redirect-pages",
356                       "Generate extra pages to support legacy URLs and tool links")
357         }),
358         unstable("show-coverage", |o| {
359             o.optflag("",
360                       "show-coverage",
361                       "calculate percentage of public items with documentation")
362         }),
363         unstable("enable-per-target-ignores", |o| {
364             o.optflag("",
365                       "enable-per-target-ignores",
366                       "parse ignore-foo for ignoring doctests on a per-target basis")
367         }),
368         unstable("runtool", |o| {
369             o.optopt("",
370                      "runtool",
371                      "",
372                      "The tool to run tests with when building for a different target than host")
373         }),
374         unstable("runtool-arg", |o| {
375             o.optmulti("",
376                        "runtool-arg",
377                        "",
378                        "One (of possibly many) arguments to pass to the runtool")
379         }),
380         unstable("test-builder", |o| {
381             o.optflag("",
382                       "test-builder",
383                       "specified the rustc-like binary to use as the test builder")
384         }),
385     ]
386 }
387
388 fn usage(argv0: &str) {
389     let mut options = getopts::Options::new();
390     for option in opts() {
391         (option.apply)(&mut options);
392     }
393     println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
394 }
395
396 fn main_args(args: &[String]) -> i32 {
397     let mut options = getopts::Options::new();
398     for option in opts() {
399         (option.apply)(&mut options);
400     }
401     let matches = match options.parse(&args[1..]) {
402         Ok(m) => m,
403         Err(err) => {
404             early_error(ErrorOutputType::default(), &err.to_string());
405         }
406     };
407     let options = match config::Options::from_matches(&matches) {
408         Ok(opts) => opts,
409         Err(code) => return code,
410     };
411     rustc_interface::interface::default_thread_pool(options.edition, move || {
412         main_options(options)
413     })
414 }
415
416 fn main_options(options: config::Options) -> i32 {
417     let diag = core::new_handler(options.error_format,
418                                  None,
419                                  options.debugging_options.treat_err_as_bug,
420                                  options.debugging_options.ui_testing);
421
422     match (options.should_test, options.markdown_input()) {
423         (true, true) => return markdown::test(options, &diag),
424         (true, false) => return test::run(options),
425         (false, true) => return markdown::render(options.input,
426                                                  options.render_options,
427                                                  &diag,
428                                                  options.edition),
429         (false, false) => {}
430     }
431
432     // need to move these items separately because we lose them by the time the closure is called,
433     // but we can't crates the Handler ahead of time because it's not Send
434     let diag_opts = (options.error_format,
435                      options.debugging_options.treat_err_as_bug,
436                      options.debugging_options.ui_testing,
437                      options.edition);
438     let show_coverage = options.show_coverage;
439     rust_input(options, move |out| {
440         if show_coverage {
441             // if we ran coverage, bail early, we don't need to also generate docs at this point
442             // (also we didn't load in any of the useful passes)
443             return rustc_driver::EXIT_SUCCESS;
444         }
445
446         let Output { krate, renderinfo, renderopts } = out;
447         info!("going to format");
448         let (error_format, treat_err_as_bug, ui_testing, edition) = diag_opts;
449         let diag = core::new_handler(error_format, None, treat_err_as_bug, ui_testing);
450         match html::render::run(
451             krate,
452             renderopts,
453             renderinfo,
454             &diag,
455             edition,
456         ) {
457             Ok(_) => rustc_driver::EXIT_SUCCESS,
458             Err(e) => {
459                 diag.struct_err(&format!("couldn't generate documentation: {}", e.error))
460                     .note(&format!("failed to create or modify \"{}\"", e.file.display()))
461                     .emit();
462                 rustc_driver::EXIT_FAILURE
463             }
464         }
465     })
466 }
467
468 /// Interprets the input file as a rust source file, passing it through the
469 /// compiler all the way through the analysis passes. The rustdoc output is then
470 /// generated from the cleaned AST of the crate.
471 ///
472 /// This form of input will run all of the plug/cleaning passes
473 fn rust_input<R, F>(options: config::Options, f: F) -> R
474 where R: 'static + Send,
475       F: 'static + Send + FnOnce(Output) -> R
476 {
477     // First, parse the crate and extract all relevant information.
478     info!("starting to run rustc");
479
480     let result = rustc_driver::catch_fatal_errors(move || {
481         let crate_name = options.crate_name.clone();
482         let crate_version = options.crate_version.clone();
483         let (mut krate, renderinfo, renderopts) = core::run_core(options);
484
485         info!("finished with rustc");
486
487         if let Some(name) = crate_name {
488             krate.name = name
489         }
490
491         krate.version = crate_version;
492
493         f(Output {
494             krate,
495             renderinfo,
496             renderopts,
497         })
498     });
499
500     match result {
501         Ok(output) => output,
502         Err(_) => panic::resume_unwind(Box::new(errors::FatalErrorMarker)),
503     }
504 }