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