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