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