]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/config.rs
remove pat2021
[rust.git] / src / librustdoc / config.rs
1 use std::collections::BTreeMap;
2 use std::convert::TryFrom;
3 use std::ffi::OsStr;
4 use std::fmt;
5 use std::path::PathBuf;
6 use std::str::FromStr;
7
8 use rustc_data_structures::fx::FxHashMap;
9 use rustc_session::config::{self, parse_crate_types_from_list, parse_externs, CrateType};
10 use rustc_session::config::{
11     build_codegen_options, build_debugging_options, get_cmd_lint_options, host_triple,
12     nightly_options,
13 };
14 use rustc_session::config::{CodegenOptions, DebuggingOptions, ErrorOutputType, Externs};
15 use rustc_session::getopts;
16 use rustc_session::lint::Level;
17 use rustc_session::search_paths::SearchPath;
18 use rustc_span::edition::Edition;
19 use rustc_target::spec::TargetTriple;
20
21 use crate::core::new_handler;
22 use crate::externalfiles::ExternalHtml;
23 use crate::html;
24 use crate::html::markdown::IdMap;
25 use crate::html::render::StylePath;
26 use crate::html::static_files;
27 use crate::opts;
28 use crate::passes::{self, Condition, DefaultPassOption};
29 use crate::theme;
30
31 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
32 crate enum OutputFormat {
33     Json,
34     Html,
35 }
36
37 impl Default for OutputFormat {
38     fn default() -> OutputFormat {
39         OutputFormat::Html
40     }
41 }
42
43 impl OutputFormat {
44     crate fn is_json(&self) -> bool {
45         matches!(self, OutputFormat::Json)
46     }
47 }
48
49 impl TryFrom<&str> for OutputFormat {
50     type Error = String;
51
52     fn try_from(value: &str) -> Result<Self, Self::Error> {
53         match value {
54             "json" => Ok(OutputFormat::Json),
55             "html" => Ok(OutputFormat::Html),
56             _ => Err(format!("unknown output format `{}`", value)),
57         }
58     }
59 }
60
61 /// Configuration options for rustdoc.
62 #[derive(Clone)]
63 crate struct Options {
64     // Basic options / Options passed directly to rustc
65     /// The crate root or Markdown file to load.
66     crate input: PathBuf,
67     /// The name of the crate being documented.
68     crate crate_name: Option<String>,
69     /// Whether or not this is a proc-macro crate
70     crate proc_macro_crate: bool,
71     /// How to format errors and warnings.
72     crate error_format: ErrorOutputType,
73     /// Library search paths to hand to the compiler.
74     crate libs: Vec<SearchPath>,
75     /// Library search paths strings to hand to the compiler.
76     crate lib_strs: Vec<String>,
77     /// The list of external crates to link against.
78     crate externs: Externs,
79     /// The list of external crates strings to link against.
80     crate extern_strs: Vec<String>,
81     /// List of `cfg` flags to hand to the compiler. Always includes `rustdoc`.
82     crate cfgs: Vec<String>,
83     /// Codegen options to hand to the compiler.
84     crate codegen_options: CodegenOptions,
85     /// Codegen options strings to hand to the compiler.
86     crate codegen_options_strs: Vec<String>,
87     /// Debugging (`-Z`) options to pass to the compiler.
88     crate debugging_opts: DebuggingOptions,
89     /// Debugging (`-Z`) options strings to pass to the compiler.
90     crate debugging_opts_strs: Vec<String>,
91     /// The target used to compile the crate against.
92     crate target: TargetTriple,
93     /// Edition used when reading the crate. Defaults to "2015". Also used by default when
94     /// compiling doctests from the crate.
95     crate edition: Edition,
96     /// The path to the sysroot. Used during the compilation process.
97     crate maybe_sysroot: Option<PathBuf>,
98     /// Lint information passed over the command-line.
99     crate lint_opts: Vec<(String, Level)>,
100     /// Whether to ask rustc to describe the lints it knows.
101     crate describe_lints: bool,
102     /// What level to cap lints at.
103     crate lint_cap: Option<Level>,
104
105     // Options specific to running doctests
106     /// Whether we should run doctests instead of generating docs.
107     crate should_test: bool,
108     /// List of arguments to pass to the test harness, if running tests.
109     crate test_args: Vec<String>,
110     /// The working directory in which to run tests.
111     crate test_run_directory: Option<PathBuf>,
112     /// Optional path to persist the doctest executables to, defaults to a
113     /// temporary directory if not set.
114     crate persist_doctests: Option<PathBuf>,
115     /// Runtool to run doctests with
116     crate runtool: Option<String>,
117     /// Arguments to pass to the runtool
118     crate runtool_args: Vec<String>,
119     /// Whether to allow ignoring doctests on a per-target basis
120     /// For example, using ignore-foo to ignore running the doctest on any target that
121     /// contains "foo" as a substring
122     crate enable_per_target_ignores: bool,
123
124     /// The path to a rustc-like binary to build tests with. If not set, we
125     /// default to loading from `$sysroot/bin/rustc`.
126     crate test_builder: Option<PathBuf>,
127
128     // Options that affect the documentation process
129     /// The selected default set of passes to use.
130     ///
131     /// Be aware: This option can come both from the CLI and from crate attributes!
132     crate default_passes: DefaultPassOption,
133     /// Any passes manually selected by the user.
134     ///
135     /// Be aware: This option can come both from the CLI and from crate attributes!
136     crate manual_passes: Vec<String>,
137     /// Whether to display warnings during doc generation or while gathering doctests. By default,
138     /// all non-rustdoc-specific lints are allowed when generating docs.
139     crate display_warnings: bool,
140     /// Whether to run the `calculate-doc-coverage` pass, which counts the number of public items
141     /// with and without documentation.
142     crate show_coverage: bool,
143
144     // Options that alter generated documentation pages
145     /// Crate version to note on the sidebar of generated docs.
146     crate crate_version: Option<String>,
147     /// Collected options specific to outputting final pages.
148     crate render_options: RenderOptions,
149     /// The format that we output when rendering.
150     ///
151     /// Currently used only for the `--show-coverage` option.
152     crate output_format: OutputFormat,
153     /// If this option is set to `true`, rustdoc will only run checks and not generate
154     /// documentation.
155     crate run_check: bool,
156     /// Whether doctests should emit unused externs
157     crate json_unused_externs: bool,
158 }
159
160 impl fmt::Debug for Options {
161     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
162         struct FmtExterns<'a>(&'a Externs);
163
164         impl<'a> fmt::Debug for FmtExterns<'a> {
165             fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
166                 f.debug_map().entries(self.0.iter()).finish()
167             }
168         }
169
170         f.debug_struct("Options")
171             .field("input", &self.input)
172             .field("crate_name", &self.crate_name)
173             .field("proc_macro_crate", &self.proc_macro_crate)
174             .field("error_format", &self.error_format)
175             .field("libs", &self.libs)
176             .field("externs", &FmtExterns(&self.externs))
177             .field("cfgs", &self.cfgs)
178             .field("codegen_options", &"...")
179             .field("debugging_options", &"...")
180             .field("target", &self.target)
181             .field("edition", &self.edition)
182             .field("maybe_sysroot", &self.maybe_sysroot)
183             .field("lint_opts", &self.lint_opts)
184             .field("describe_lints", &self.describe_lints)
185             .field("lint_cap", &self.lint_cap)
186             .field("should_test", &self.should_test)
187             .field("test_args", &self.test_args)
188             .field("test_run_directory", &self.test_run_directory)
189             .field("persist_doctests", &self.persist_doctests)
190             .field("default_passes", &self.default_passes)
191             .field("manual_passes", &self.manual_passes)
192             .field("display_warnings", &self.display_warnings)
193             .field("show_coverage", &self.show_coverage)
194             .field("crate_version", &self.crate_version)
195             .field("render_options", &self.render_options)
196             .field("runtool", &self.runtool)
197             .field("runtool_args", &self.runtool_args)
198             .field("enable-per-target-ignores", &self.enable_per_target_ignores)
199             .field("run_check", &self.run_check)
200             .finish()
201     }
202 }
203
204 /// Configuration options for the HTML page-creation process.
205 #[derive(Clone, Debug)]
206 crate struct RenderOptions {
207     /// Output directory to generate docs into. Defaults to `doc`.
208     crate output: PathBuf,
209     /// External files to insert into generated pages.
210     crate external_html: ExternalHtml,
211     /// A pre-populated `IdMap` with the default headings and any headings added by Markdown files
212     /// processed by `external_html`.
213     crate id_map: IdMap,
214     /// If present, playground URL to use in the "Run" button added to code samples.
215     ///
216     /// Be aware: This option can come both from the CLI and from crate attributes!
217     crate playground_url: Option<String>,
218     /// Whether to sort modules alphabetically on a module page instead of using declaration order.
219     /// `true` by default.
220     //
221     // FIXME(misdreavus): the flag name is `--sort-modules-by-appearance` but the meaning is
222     // inverted once read.
223     crate sort_modules_alphabetically: bool,
224     /// List of themes to extend the docs with. Original argument name is included to assist in
225     /// displaying errors if it fails a theme check.
226     crate themes: Vec<StylePath>,
227     /// If present, CSS file that contains rules to add to the default CSS.
228     crate extension_css: Option<PathBuf>,
229     /// A map of crate names to the URL to use instead of querying the crate's `html_root_url`.
230     crate extern_html_root_urls: BTreeMap<String, String>,
231     /// A map of the default settings (values are as for DOM storage API). Keys should lack the
232     /// `rustdoc-` prefix.
233     crate default_settings: FxHashMap<String, String>,
234     /// If present, suffix added to CSS/JavaScript files when referencing them in generated pages.
235     crate resource_suffix: String,
236     /// Whether to run the static CSS/JavaScript through a minifier when outputting them. `true` by
237     /// default.
238     //
239     // FIXME(misdreavus): the flag name is `--disable-minification` but the meaning is inverted
240     // once read.
241     crate enable_minification: bool,
242     /// Whether to create an index page in the root of the output directory. If this is true but
243     /// `enable_index_page` is None, generate a static listing of crates instead.
244     crate enable_index_page: bool,
245     /// A file to use as the index page at the root of the output directory. Overrides
246     /// `enable_index_page` to be true if set.
247     crate index_page: Option<PathBuf>,
248     /// An optional path to use as the location of static files. If not set, uses combinations of
249     /// `../` to reach the documentation root.
250     crate static_root_path: Option<String>,
251
252     // Options specific to reading standalone Markdown files
253     /// Whether to generate a table of contents on the output file when reading a standalone
254     /// Markdown file.
255     crate markdown_no_toc: bool,
256     /// Additional CSS files to link in pages generated from standalone Markdown files.
257     crate markdown_css: Vec<String>,
258     /// If present, playground URL to use in the "Run" button added to code samples generated from
259     /// standalone Markdown files. If not present, `playground_url` is used.
260     crate markdown_playground_url: Option<String>,
261     /// If false, the `select` element to have search filtering by crates on rendered docs
262     /// won't be generated.
263     crate generate_search_filter: bool,
264     /// Document items that have lower than `pub` visibility.
265     crate document_private: bool,
266     /// Document items that have `doc(hidden)`.
267     crate document_hidden: bool,
268     /// If `true`, generate a JSON file in the crate folder instead of HTML redirection files.
269     crate generate_redirect_map: bool,
270     crate unstable_features: rustc_feature::UnstableFeatures,
271     crate emit: Vec<EmitType>,
272 }
273
274 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
275 crate enum EmitType {
276     Unversioned,
277     Toolchain,
278     InvocationSpecific,
279 }
280
281 impl FromStr for EmitType {
282     type Err = ();
283
284     fn from_str(s: &str) -> Result<Self, Self::Err> {
285         use EmitType::*;
286         match s {
287             "unversioned-shared-resources" => Ok(Unversioned),
288             "toolchain-shared-resources" => Ok(Toolchain),
289             "invocation-specific" => Ok(InvocationSpecific),
290             _ => Err(()),
291         }
292     }
293 }
294
295 impl RenderOptions {
296     crate fn should_emit_crate(&self) -> bool {
297         self.emit.is_empty() || self.emit.contains(&EmitType::InvocationSpecific)
298     }
299 }
300
301 impl Options {
302     /// Parses the given command-line for options. If an error message or other early-return has
303     /// been printed, returns `Err` with the exit code.
304     crate fn from_matches(matches: &getopts::Matches) -> Result<Options, i32> {
305         // Check for unstable options.
306         nightly_options::check_nightly_options(&matches, &opts());
307
308         if matches.opt_present("h") || matches.opt_present("help") {
309             crate::usage("rustdoc");
310             return Err(0);
311         } else if matches.opt_present("version") {
312             rustc_driver::version("rustdoc", &matches);
313             return Err(0);
314         }
315
316         if matches.opt_strs("passes") == ["list"] {
317             println!("Available passes for running rustdoc:");
318             for pass in passes::PASSES {
319                 println!("{:>20} - {}", pass.name, pass.description);
320             }
321             println!("\nDefault passes for rustdoc:");
322             for p in passes::DEFAULT_PASSES {
323                 print!("{:>20}", p.pass.name);
324                 println_condition(p.condition);
325             }
326
327             if nightly_options::match_is_nightly_build(matches) {
328                 println!("\nPasses run with `--show-coverage`:");
329                 for p in passes::COVERAGE_PASSES {
330                     print!("{:>20}", p.pass.name);
331                     println_condition(p.condition);
332                 }
333             }
334
335             fn println_condition(condition: Condition) {
336                 use Condition::*;
337                 match condition {
338                     Always => println!(),
339                     WhenDocumentPrivate => println!("  (when --document-private-items)"),
340                     WhenNotDocumentPrivate => println!("  (when not --document-private-items)"),
341                     WhenNotDocumentHidden => println!("  (when not --document-hidden-items)"),
342                 }
343             }
344
345             return Err(0);
346         }
347
348         if matches.opt_strs("print").iter().any(|opt| opt == "unversioned-files") {
349             for file in crate::html::render::FILES_UNVERSIONED.keys() {
350                 println!("{}", file);
351             }
352             return Err(0);
353         }
354
355         let color = config::parse_color(&matches);
356         let config::JsonConfig { json_rendered, json_unused_externs, .. } =
357             config::parse_json(&matches);
358         let error_format = config::parse_error_format(&matches, color, json_rendered);
359
360         let codegen_options = build_codegen_options(matches, error_format);
361         let debugging_opts = build_debugging_options(matches, error_format);
362
363         let diag = new_handler(error_format, None, &debugging_opts);
364
365         // check for deprecated options
366         check_deprecated_options(&matches, &diag);
367
368         let mut emit = Vec::new();
369         for list in matches.opt_strs("emit") {
370             for kind in list.split(',') {
371                 match kind.parse() {
372                     Ok(kind) => emit.push(kind),
373                     Err(()) => {
374                         diag.err(&format!("unrecognized emission type: {}", kind));
375                         return Err(1);
376                     }
377                 }
378             }
379         }
380
381         // check for `--output-format=json`
382         if !matches!(matches.opt_str("output-format").as_deref(), None | Some("html"))
383             && !matches.opt_present("show-coverage")
384             && !nightly_options::is_unstable_enabled(matches)
385         {
386             rustc_session::early_error(
387                 error_format,
388                 "the -Z unstable-options flag must be passed to enable --output-format for documentation generation (see https://github.com/rust-lang/rust/issues/76578)",
389             );
390         }
391
392         let to_check = matches.opt_strs("check-theme");
393         if !to_check.is_empty() {
394             let paths = theme::load_css_paths(static_files::themes::LIGHT.as_bytes());
395             let mut errors = 0;
396
397             println!("rustdoc: [check-theme] Starting tests! (Ignoring all other arguments)");
398             for theme_file in to_check.iter() {
399                 print!(" - Checking \"{}\"...", theme_file);
400                 let (success, differences) = theme::test_theme_against(theme_file, &paths, &diag);
401                 if !differences.is_empty() || !success {
402                     println!(" FAILED");
403                     errors += 1;
404                     if !differences.is_empty() {
405                         println!("{}", differences.join("\n"));
406                     }
407                 } else {
408                     println!(" OK");
409                 }
410             }
411             if errors != 0 {
412                 return Err(1);
413             }
414             return Err(0);
415         }
416
417         if matches.free.is_empty() {
418             diag.struct_err("missing file operand").emit();
419             return Err(1);
420         }
421         if matches.free.len() > 1 {
422             diag.struct_err("too many file operands").emit();
423             return Err(1);
424         }
425         let input = PathBuf::from(&matches.free[0]);
426
427         let libs = matches
428             .opt_strs("L")
429             .iter()
430             .map(|s| SearchPath::from_cli_opt(s, error_format))
431             .collect();
432         let externs = parse_externs(&matches, &debugging_opts, error_format);
433         let extern_html_root_urls = match parse_extern_html_roots(&matches) {
434             Ok(ex) => ex,
435             Err(err) => {
436                 diag.struct_err(err).emit();
437                 return Err(1);
438             }
439         };
440
441         let default_settings: Vec<Vec<(String, String)>> = vec![
442             matches
443                 .opt_str("default-theme")
444                 .iter()
445                 .map(|theme| {
446                     vec![
447                         ("use-system-theme".to_string(), "false".to_string()),
448                         ("theme".to_string(), theme.to_string()),
449                     ]
450                 })
451                 .flatten()
452                 .collect(),
453             matches
454                 .opt_strs("default-setting")
455                 .iter()
456                 .map(|s| match s.split_once('=') {
457                     None => (s.clone(), "true".to_string()),
458                     Some((k, v)) => (k.to_string(), v.to_string()),
459                 })
460                 .collect(),
461         ];
462         let default_settings = default_settings.into_iter().flatten().collect();
463
464         let test_args = matches.opt_strs("test-args");
465         let test_args: Vec<String> =
466             test_args.iter().flat_map(|s| s.split_whitespace()).map(|s| s.to_string()).collect();
467
468         let should_test = matches.opt_present("test");
469
470         let output =
471             matches.opt_str("o").map(|s| PathBuf::from(&s)).unwrap_or_else(|| PathBuf::from("doc"));
472         let cfgs = matches.opt_strs("cfg");
473
474         let extension_css = matches.opt_str("e").map(|s| PathBuf::from(&s));
475
476         if let Some(ref p) = extension_css {
477             if !p.is_file() {
478                 diag.struct_err("option --extend-css argument must be a file").emit();
479                 return Err(1);
480             }
481         }
482
483         let mut themes = Vec::new();
484         if matches.opt_present("theme") {
485             let paths = theme::load_css_paths(static_files::themes::LIGHT.as_bytes());
486
487             for (theme_file, theme_s) in
488                 matches.opt_strs("theme").iter().map(|s| (PathBuf::from(&s), s.to_owned()))
489             {
490                 if !theme_file.is_file() {
491                     diag.struct_err(&format!("invalid argument: \"{}\"", theme_s))
492                         .help("arguments to --theme must be files")
493                         .emit();
494                     return Err(1);
495                 }
496                 if theme_file.extension() != Some(OsStr::new("css")) {
497                     diag.struct_err(&format!("invalid argument: \"{}\"", theme_s))
498                         .help("arguments to --theme must have a .css extension")
499                         .emit();
500                     return Err(1);
501                 }
502                 let (success, ret) = theme::test_theme_against(&theme_file, &paths, &diag);
503                 if !success {
504                     diag.struct_err(&format!("error loading theme file: \"{}\"", theme_s)).emit();
505                     return Err(1);
506                 } else if !ret.is_empty() {
507                     diag.struct_warn(&format!(
508                         "theme file \"{}\" is missing CSS rules from the default theme",
509                         theme_s
510                     ))
511                     .warn("the theme may appear incorrect when loaded")
512                     .help(&format!(
513                         "to see what rules are missing, call `rustdoc  --check-theme \"{}\"`",
514                         theme_s
515                     ))
516                     .emit();
517                 }
518                 themes.push(StylePath { path: theme_file, disabled: true });
519             }
520         }
521
522         let edition = config::parse_crate_edition(&matches);
523
524         let mut id_map = html::markdown::IdMap::new();
525         let external_html = match ExternalHtml::load(
526             &matches.opt_strs("html-in-header"),
527             &matches.opt_strs("html-before-content"),
528             &matches.opt_strs("html-after-content"),
529             &matches.opt_strs("markdown-before-content"),
530             &matches.opt_strs("markdown-after-content"),
531             nightly_options::match_is_nightly_build(&matches),
532             &diag,
533             &mut id_map,
534             edition,
535             &None,
536         ) {
537             Some(eh) => eh,
538             None => return Err(3),
539         };
540
541         match matches.opt_str("r").as_deref() {
542             Some("rust") | None => {}
543             Some(s) => {
544                 diag.struct_err(&format!("unknown input format: {}", s)).emit();
545                 return Err(1);
546             }
547         }
548
549         let index_page = matches.opt_str("index-page").map(|s| PathBuf::from(&s));
550         if let Some(ref index_page) = index_page {
551             if !index_page.is_file() {
552                 diag.struct_err("option `--index-page` argument must be a file").emit();
553                 return Err(1);
554             }
555         }
556
557         let target =
558             matches.opt_str("target").map_or(TargetTriple::from_triple(host_triple()), |target| {
559                 if target.ends_with(".json") {
560                     TargetTriple::TargetPath(PathBuf::from(target))
561                 } else {
562                     TargetTriple::TargetTriple(target)
563                 }
564             });
565
566         let show_coverage = matches.opt_present("show-coverage");
567
568         let default_passes = if matches.opt_present("no-defaults") {
569             passes::DefaultPassOption::None
570         } else if show_coverage {
571             passes::DefaultPassOption::Coverage
572         } else {
573             passes::DefaultPassOption::Default
574         };
575         let manual_passes = matches.opt_strs("passes");
576
577         let crate_types = match parse_crate_types_from_list(matches.opt_strs("crate-type")) {
578             Ok(types) => types,
579             Err(e) => {
580                 diag.struct_err(&format!("unknown crate type: {}", e)).emit();
581                 return Err(1);
582             }
583         };
584
585         let output_format = match matches.opt_str("output-format") {
586             Some(s) => match OutputFormat::try_from(s.as_str()) {
587                 Ok(out_fmt) => {
588                     if !out_fmt.is_json() && show_coverage {
589                         diag.struct_err(
590                             "html output format isn't supported for the --show-coverage option",
591                         )
592                         .emit();
593                         return Err(1);
594                     }
595                     out_fmt
596                 }
597                 Err(e) => {
598                     diag.struct_err(&e).emit();
599                     return Err(1);
600                 }
601             },
602             None => OutputFormat::default(),
603         };
604         let crate_name = matches.opt_str("crate-name");
605         let proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
606         let playground_url = matches.opt_str("playground-url");
607         let maybe_sysroot = matches.opt_str("sysroot").map(PathBuf::from);
608         let display_warnings = matches.opt_present("display-warnings");
609         let sort_modules_alphabetically = !matches.opt_present("sort-modules-by-appearance");
610         let resource_suffix = matches.opt_str("resource-suffix").unwrap_or_default();
611         let enable_minification = !matches.opt_present("disable-minification");
612         let markdown_no_toc = matches.opt_present("markdown-no-toc");
613         let markdown_css = matches.opt_strs("markdown-css");
614         let markdown_playground_url = matches.opt_str("markdown-playground-url");
615         let crate_version = matches.opt_str("crate-version");
616         let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some();
617         let static_root_path = matches.opt_str("static-root-path");
618         let generate_search_filter = !matches.opt_present("disable-per-crate-search");
619         let test_run_directory = matches.opt_str("test-run-directory").map(PathBuf::from);
620         let persist_doctests = matches.opt_str("persist-doctests").map(PathBuf::from);
621         let test_builder = matches.opt_str("test-builder").map(PathBuf::from);
622         let codegen_options_strs = matches.opt_strs("C");
623         let debugging_opts_strs = matches.opt_strs("Z");
624         let lib_strs = matches.opt_strs("L");
625         let extern_strs = matches.opt_strs("extern");
626         let runtool = matches.opt_str("runtool");
627         let runtool_args = matches.opt_strs("runtool-arg");
628         let enable_per_target_ignores = matches.opt_present("enable-per-target-ignores");
629         let document_private = matches.opt_present("document-private-items");
630         let document_hidden = matches.opt_present("document-hidden-items");
631         let run_check = matches.opt_present("check");
632         let generate_redirect_map = matches.opt_present("generate-redirect-map");
633
634         let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
635
636         Ok(Options {
637             input,
638             proc_macro_crate,
639             error_format,
640             libs,
641             lib_strs,
642             externs,
643             extern_strs,
644             cfgs,
645             codegen_options,
646             codegen_options_strs,
647             debugging_opts,
648             debugging_opts_strs,
649             target,
650             edition,
651             maybe_sysroot,
652             lint_opts,
653             describe_lints,
654             lint_cap,
655             should_test,
656             test_args,
657             default_passes,
658             manual_passes,
659             display_warnings,
660             show_coverage,
661             crate_version,
662             test_run_directory,
663             persist_doctests,
664             runtool,
665             runtool_args,
666             enable_per_target_ignores,
667             test_builder,
668             run_check,
669             render_options: RenderOptions {
670                 output,
671                 external_html,
672                 id_map,
673                 playground_url,
674                 sort_modules_alphabetically,
675                 themes,
676                 extension_css,
677                 extern_html_root_urls,
678                 default_settings,
679                 resource_suffix,
680                 enable_minification,
681                 enable_index_page,
682                 index_page,
683                 static_root_path,
684                 markdown_no_toc,
685                 markdown_css,
686                 markdown_playground_url,
687                 generate_search_filter,
688                 document_private,
689                 document_hidden,
690                 generate_redirect_map,
691                 unstable_features: rustc_feature::UnstableFeatures::from_environment(
692                     crate_name.as_deref(),
693                 ),
694                 emit,
695             },
696             crate_name,
697             output_format,
698             json_unused_externs,
699         })
700     }
701
702     /// Returns `true` if the file given as `self.input` is a Markdown file.
703     crate fn markdown_input(&self) -> bool {
704         self.input.extension().map_or(false, |e| e == "md" || e == "markdown")
705     }
706 }
707
708 /// Prints deprecation warnings for deprecated options
709 fn check_deprecated_options(matches: &getopts::Matches, diag: &rustc_errors::Handler) {
710     let deprecated_flags = ["input-format", "no-defaults", "passes"];
711
712     for flag in deprecated_flags.iter() {
713         if matches.opt_present(flag) {
714             let mut err = diag.struct_warn(&format!("the `{}` flag is deprecated", flag));
715             err.note(
716                 "see issue #44136 <https://github.com/rust-lang/rust/issues/44136> \
717                  for more information",
718             );
719
720             if *flag == "no-defaults" {
721                 err.help("you may want to use --document-private-items");
722             }
723
724             err.emit();
725         }
726     }
727
728     let removed_flags = ["plugins", "plugin-path"];
729
730     for &flag in removed_flags.iter() {
731         if matches.opt_present(flag) {
732             diag.struct_warn(&format!("the '{}' flag no longer functions", flag))
733                 .warn("see CVE-2018-1000622")
734                 .emit();
735         }
736     }
737 }
738
739 /// Extracts `--extern-html-root-url` arguments from `matches` and returns a map of crate names to
740 /// the given URLs. If an `--extern-html-root-url` argument was ill-formed, returns an error
741 /// describing the issue.
742 fn parse_extern_html_roots(
743     matches: &getopts::Matches,
744 ) -> Result<BTreeMap<String, String>, &'static str> {
745     let mut externs = BTreeMap::new();
746     for arg in &matches.opt_strs("extern-html-root-url") {
747         let (name, url) =
748             arg.split_once('=').ok_or("--extern-html-root-url must be of the form name=url")?;
749         externs.insert(name.to_string(), url.to_string());
750     }
751     Ok(externs)
752 }