1use std::collections::BTreeMap;
2use std::ffi::OsStr;
3use std::io::Read;
4use std::path::{Path, PathBuf};
5use std::str::FromStr;
6use std::{fmt, io};
7
8use rustc_data_structures::fx::FxIndexMap;
9use rustc_errors::DiagCtxtHandle;
10use rustc_session::config::{
11 self, CodegenOptions, CrateType, ErrorOutputType, Externs, Input, JsonUnusedExterns,
12 OptionsTargetModifiers, UnstableOptions, get_cmd_lint_options, nightly_options,
13 parse_crate_types_from_list, parse_externs, parse_target_triple,
14};
15use rustc_session::lint::Level;
16use rustc_session::search_paths::SearchPath;
17use rustc_session::{EarlyDiagCtxt, getopts};
18use rustc_span::FileName;
19use rustc_span::edition::Edition;
20use rustc_target::spec::TargetTuple;
21
22use crate::core::new_dcx;
23use crate::externalfiles::ExternalHtml;
24use crate::html::markdown::IdMap;
25use crate::html::render::StylePath;
26use crate::html::static_files;
27use crate::passes::{self, Condition};
28use crate::scrape_examples::{AllCallLocations, ScrapeExamplesOptions};
29use crate::{html, opts, theme};
30
31#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
32pub(crate) enum OutputFormat {
33 Json,
34 #[default]
35 Html,
36 Doctest,
37}
38
39impl OutputFormat {
40 pub(crate) fn is_json(&self) -> bool {
41 matches!(self, OutputFormat::Json)
42 }
43}
44
45impl TryFrom<&str> for OutputFormat {
46 type Error = String;
47
48 fn try_from(value: &str) -> Result<Self, Self::Error> {
49 match value {
50 "json" => Ok(OutputFormat::Json),
51 "html" => Ok(OutputFormat::Html),
52 "doctest" => Ok(OutputFormat::Doctest),
53 _ => Err(format!("unknown output format `{value}`")),
54 }
55 }
56}
57
58pub(crate) enum InputMode {
60 NoInputMergeFinalize,
62 HasFile(Input),
64}
65
66#[derive(Clone)]
68pub(crate) struct Options {
69 pub(crate) crate_name: Option<String>,
72 pub(crate) bin_crate: bool,
74 pub(crate) proc_macro_crate: bool,
76 pub(crate) error_format: ErrorOutputType,
78 pub(crate) diagnostic_width: Option<usize>,
80 pub(crate) libs: Vec<SearchPath>,
82 pub(crate) lib_strs: Vec<String>,
84 pub(crate) externs: Externs,
86 pub(crate) extern_strs: Vec<String>,
88 pub(crate) cfgs: Vec<String>,
90 pub(crate) check_cfgs: Vec<String>,
92 pub(crate) codegen_options: CodegenOptions,
94 pub(crate) codegen_options_strs: Vec<String>,
96 pub(crate) unstable_opts: UnstableOptions,
98 pub(crate) unstable_opts_strs: Vec<String>,
100 pub(crate) target: TargetTuple,
102 pub(crate) edition: Edition,
105 pub(crate) sysroot: PathBuf,
107 pub(crate) maybe_sysroot: Option<PathBuf>,
109 pub(crate) lint_opts: Vec<(String, Level)>,
111 pub(crate) describe_lints: bool,
113 pub(crate) lint_cap: Option<Level>,
115
116 pub(crate) should_test: bool,
119 pub(crate) test_args: Vec<String>,
121 pub(crate) test_run_directory: Option<PathBuf>,
123 pub(crate) persist_doctests: Option<PathBuf>,
126 pub(crate) test_runtool: Option<String>,
128 pub(crate) test_runtool_args: Vec<String>,
130 pub(crate) no_run: bool,
132 pub(crate) remap_path_prefix: Vec<(PathBuf, PathBuf)>,
134
135 pub(crate) test_builder: Option<PathBuf>,
138
139 pub(crate) test_builder_wrappers: Vec<PathBuf>,
141
142 pub(crate) show_coverage: bool,
146
147 pub(crate) crate_version: Option<String>,
150 pub(crate) output_format: OutputFormat,
154 pub(crate) run_check: bool,
157 pub(crate) json_unused_externs: JsonUnusedExterns,
159 pub(crate) nocapture: bool,
161
162 pub(crate) scrape_examples_options: Option<ScrapeExamplesOptions>,
165
166 pub(crate) unstable_features: rustc_feature::UnstableFeatures,
169
170 pub(crate) expanded_args: Vec<String>,
175
176 pub(crate) doctest_build_args: Vec<String>,
178}
179
180impl fmt::Debug for Options {
181 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
182 struct FmtExterns<'a>(&'a Externs);
183
184 impl fmt::Debug for FmtExterns<'_> {
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186 f.debug_map().entries(self.0.iter()).finish()
187 }
188 }
189
190 f.debug_struct("Options")
191 .field("crate_name", &self.crate_name)
192 .field("bin_crate", &self.bin_crate)
193 .field("proc_macro_crate", &self.proc_macro_crate)
194 .field("error_format", &self.error_format)
195 .field("libs", &self.libs)
196 .field("externs", &FmtExterns(&self.externs))
197 .field("cfgs", &self.cfgs)
198 .field("check-cfgs", &self.check_cfgs)
199 .field("codegen_options", &"...")
200 .field("unstable_options", &"...")
201 .field("target", &self.target)
202 .field("edition", &self.edition)
203 .field("sysroot", &self.sysroot)
204 .field("maybe_sysroot", &self.maybe_sysroot)
205 .field("lint_opts", &self.lint_opts)
206 .field("describe_lints", &self.describe_lints)
207 .field("lint_cap", &self.lint_cap)
208 .field("should_test", &self.should_test)
209 .field("test_args", &self.test_args)
210 .field("test_run_directory", &self.test_run_directory)
211 .field("persist_doctests", &self.persist_doctests)
212 .field("show_coverage", &self.show_coverage)
213 .field("crate_version", &self.crate_version)
214 .field("test_runtool", &self.test_runtool)
215 .field("test_runtool_args", &self.test_runtool_args)
216 .field("run_check", &self.run_check)
217 .field("no_run", &self.no_run)
218 .field("test_builder_wrappers", &self.test_builder_wrappers)
219 .field("remap-file-prefix", &self.remap_path_prefix)
220 .field("nocapture", &self.nocapture)
221 .field("scrape_examples_options", &self.scrape_examples_options)
222 .field("unstable_features", &self.unstable_features)
223 .finish()
224 }
225}
226
227#[derive(Clone, Debug)]
229pub(crate) struct RenderOptions {
230 pub(crate) output: PathBuf,
232 pub(crate) external_html: ExternalHtml,
234 pub(crate) id_map: IdMap,
237 pub(crate) playground_url: Option<String>,
241 pub(crate) module_sorting: ModuleSorting,
244 pub(crate) themes: Vec<StylePath>,
247 pub(crate) extension_css: Option<PathBuf>,
249 pub(crate) extern_html_root_urls: BTreeMap<String, String>,
251 pub(crate) extern_html_root_takes_precedence: bool,
253 pub(crate) default_settings: FxIndexMap<String, String>,
256 pub(crate) resource_suffix: String,
258 pub(crate) enable_index_page: bool,
261 pub(crate) index_page: Option<PathBuf>,
264 pub(crate) static_root_path: Option<String>,
267
268 pub(crate) markdown_no_toc: bool,
272 pub(crate) markdown_css: Vec<String>,
274 pub(crate) markdown_playground_url: Option<String>,
277 pub(crate) document_private: bool,
279 pub(crate) document_hidden: bool,
281 pub(crate) generate_redirect_map: bool,
283 pub(crate) show_type_layout: bool,
285 pub(crate) unstable_features: rustc_feature::UnstableFeatures,
288 pub(crate) emit: Vec<EmitType>,
289 pub(crate) generate_link_to_definition: bool,
291 pub(crate) call_locations: AllCallLocations,
293 pub(crate) no_emit_shared: bool,
295 pub(crate) html_no_source: bool,
297 pub(crate) output_to_stdout: bool,
300 pub(crate) should_merge: ShouldMerge,
302 pub(crate) include_parts_dir: Vec<PathToParts>,
304 pub(crate) parts_out_dir: Option<PathToParts>,
306 pub(crate) disable_minification: bool,
308}
309
310#[derive(Copy, Clone, Debug, PartialEq, Eq)]
311pub(crate) enum ModuleSorting {
312 DeclarationOrder,
313 Alphabetical,
314}
315
316#[derive(Clone, Debug, PartialEq, Eq)]
317pub(crate) enum EmitType {
318 Unversioned,
319 Toolchain,
320 InvocationSpecific,
321 DepInfo(Option<PathBuf>),
322}
323
324impl FromStr for EmitType {
325 type Err = ();
326
327 fn from_str(s: &str) -> Result<Self, Self::Err> {
328 match s {
329 "unversioned-shared-resources" => Ok(Self::Unversioned),
330 "toolchain-shared-resources" => Ok(Self::Toolchain),
331 "invocation-specific" => Ok(Self::InvocationSpecific),
332 "dep-info" => Ok(Self::DepInfo(None)),
333 option => {
334 if let Some(file) = option.strip_prefix("dep-info=") {
335 Ok(Self::DepInfo(Some(Path::new(file).into())))
336 } else {
337 Err(())
338 }
339 }
340 }
341 }
342}
343
344impl RenderOptions {
345 pub(crate) fn should_emit_crate(&self) -> bool {
346 self.emit.is_empty() || self.emit.contains(&EmitType::InvocationSpecific)
347 }
348
349 pub(crate) fn dep_info(&self) -> Option<Option<&Path>> {
350 for emit in &self.emit {
351 if let EmitType::DepInfo(file) = emit {
352 return Some(file.as_deref());
353 }
354 }
355 None
356 }
357}
358
359fn make_input(early_dcx: &EarlyDiagCtxt, input: &str) -> Input {
363 if input == "-" {
364 let mut src = String::new();
365 if io::stdin().read_to_string(&mut src).is_err() {
366 early_dcx.early_fatal("couldn't read from stdin, as it did not contain valid UTF-8");
369 }
370 Input::Str { name: FileName::anon_source_code(&src), input: src }
371 } else {
372 Input::File(PathBuf::from(input))
373 }
374}
375
376impl Options {
377 pub(crate) fn from_matches(
380 early_dcx: &mut EarlyDiagCtxt,
381 matches: &getopts::Matches,
382 args: Vec<String>,
383 ) -> Option<(InputMode, Options, RenderOptions)> {
384 nightly_options::check_nightly_options(early_dcx, matches, &opts());
386
387 if args.is_empty() || matches.opt_present("h") || matches.opt_present("help") {
388 crate::usage("rustdoc");
389 return None;
390 } else if matches.opt_present("version") {
391 rustc_driver::version!(&early_dcx, "rustdoc", matches);
392 return None;
393 }
394
395 if rustc_driver::describe_flag_categories(early_dcx, matches) {
396 return None;
397 }
398
399 let color = config::parse_color(early_dcx, matches);
400 let config::JsonConfig { json_rendered, json_unused_externs, json_color, .. } =
401 config::parse_json(early_dcx, matches);
402 let error_format =
403 config::parse_error_format(early_dcx, matches, color, json_color, json_rendered);
404 let diagnostic_width = matches.opt_get("diagnostic-width").unwrap_or_default();
405
406 let mut target_modifiers = BTreeMap::<OptionsTargetModifiers, String>::new();
407 let codegen_options = CodegenOptions::build(early_dcx, matches, &mut target_modifiers);
408 let unstable_opts = UnstableOptions::build(early_dcx, matches, &mut target_modifiers);
409
410 let remap_path_prefix = match parse_remap_path_prefix(matches) {
411 Ok(prefix_mappings) => prefix_mappings,
412 Err(err) => {
413 early_dcx.early_fatal(err);
414 }
415 };
416
417 let dcx = new_dcx(error_format, None, diagnostic_width, &unstable_opts);
418 let dcx = dcx.handle();
419
420 check_deprecated_options(matches, dcx);
422
423 if matches.opt_strs("passes") == ["list"] {
424 println!("Available passes for running rustdoc:");
425 for pass in passes::PASSES {
426 println!("{:>20} - {}", pass.name, pass.description);
427 }
428 println!("\nDefault passes for rustdoc:");
429 for p in passes::DEFAULT_PASSES {
430 print!("{:>20}", p.pass.name);
431 println_condition(p.condition);
432 }
433
434 if nightly_options::match_is_nightly_build(matches) {
435 println!("\nPasses run with `--show-coverage`:");
436 for p in passes::COVERAGE_PASSES {
437 print!("{:>20}", p.pass.name);
438 println_condition(p.condition);
439 }
440 }
441
442 fn println_condition(condition: Condition) {
443 use Condition::*;
444 match condition {
445 Always => println!(),
446 WhenDocumentPrivate => println!(" (when --document-private-items)"),
447 WhenNotDocumentPrivate => println!(" (when not --document-private-items)"),
448 WhenNotDocumentHidden => println!(" (when not --document-hidden-items)"),
449 }
450 }
451
452 return None;
453 }
454
455 let mut emit = Vec::new();
456 for list in matches.opt_strs("emit") {
457 for kind in list.split(',') {
458 match kind.parse() {
459 Ok(kind) => emit.push(kind),
460 Err(()) => dcx.fatal(format!("unrecognized emission type: {kind}")),
461 }
462 }
463 }
464
465 let show_coverage = matches.opt_present("show-coverage");
466 let output_format_s = matches.opt_str("output-format");
467 let output_format = match output_format_s {
468 Some(ref s) => match OutputFormat::try_from(s.as_str()) {
469 Ok(out_fmt) => out_fmt,
470 Err(e) => dcx.fatal(e),
471 },
472 None => OutputFormat::default(),
473 };
474
475 match (
477 output_format_s.as_ref().map(|_| output_format),
478 show_coverage,
479 nightly_options::is_unstable_enabled(matches),
480 ) {
481 (None | Some(OutputFormat::Json), true, _) => {}
482 (_, true, _) => {
483 dcx.fatal(format!(
484 "`--output-format={}` is not supported for the `--show-coverage` option",
485 output_format_s.unwrap_or_default(),
486 ));
487 }
488 (_, false, true) => {}
490 (None | Some(OutputFormat::Html), false, _) => {}
491 (Some(OutputFormat::Json), false, false) => {
492 dcx.fatal(
493 "the -Z unstable-options flag must be passed to enable --output-format for documentation generation (see https://github.com/rust-lang/rust/issues/76578)",
494 );
495 }
496 (Some(OutputFormat::Doctest), false, false) => {
497 dcx.fatal(
498 "the -Z unstable-options flag must be passed to enable --output-format for documentation generation (see https://github.com/rust-lang/rust/issues/134529)",
499 );
500 }
501 }
502
503 let to_check = matches.opt_strs("check-theme");
504 if !to_check.is_empty() {
505 let mut content =
506 std::str::from_utf8(static_files::STATIC_FILES.rustdoc_css.src_bytes).unwrap();
507 if let Some((_, inside)) = content.split_once("/* Begin theme: light */") {
508 content = inside;
509 }
510 if let Some((inside, _)) = content.split_once("/* End theme: light */") {
511 content = inside;
512 }
513 let paths = match theme::load_css_paths(content) {
514 Ok(p) => p,
515 Err(e) => dcx.fatal(e),
516 };
517 let mut errors = 0;
518
519 println!("rustdoc: [check-theme] Starting tests! (Ignoring all other arguments)");
520 for theme_file in to_check.iter() {
521 print!(" - Checking \"{theme_file}\"...");
522 let (success, differences) = theme::test_theme_against(theme_file, &paths, dcx);
523 if !differences.is_empty() || !success {
524 println!(" FAILED");
525 errors += 1;
526 if !differences.is_empty() {
527 println!("{}", differences.join("\n"));
528 }
529 } else {
530 println!(" OK");
531 }
532 }
533 if errors != 0 {
534 dcx.fatal("[check-theme] one or more tests failed");
535 }
536 return None;
537 }
538
539 let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(early_dcx, matches);
540
541 let input = if describe_lints {
542 InputMode::HasFile(make_input(early_dcx, ""))
543 } else {
544 match matches.free.as_slice() {
545 [] if matches.opt_str("merge").as_deref() == Some("finalize") => {
546 InputMode::NoInputMergeFinalize
547 }
548 [] => dcx.fatal("missing file operand"),
549 [input] => InputMode::HasFile(make_input(early_dcx, input)),
550 _ => dcx.fatal("too many file operands"),
551 }
552 };
553
554 let externs = parse_externs(early_dcx, matches, &unstable_opts);
555 let extern_html_root_urls = match parse_extern_html_roots(matches) {
556 Ok(ex) => ex,
557 Err(err) => dcx.fatal(err),
558 };
559
560 let parts_out_dir =
561 match matches.opt_str("parts-out-dir").map(PathToParts::from_flag).transpose() {
562 Ok(parts_out_dir) => parts_out_dir,
563 Err(e) => dcx.fatal(e),
564 };
565 let include_parts_dir = match parse_include_parts_dir(matches) {
566 Ok(include_parts_dir) => include_parts_dir,
567 Err(e) => dcx.fatal(e),
568 };
569
570 let default_settings: Vec<Vec<(String, String)>> = vec![
571 matches
572 .opt_str("default-theme")
573 .iter()
574 .flat_map(|theme| {
575 vec![
576 ("use-system-theme".to_string(), "false".to_string()),
577 ("theme".to_string(), theme.to_string()),
578 ]
579 })
580 .collect(),
581 matches
582 .opt_strs("default-setting")
583 .iter()
584 .map(|s| match s.split_once('=') {
585 None => (s.clone(), "true".to_string()),
586 Some((k, v)) => (k.to_string(), v.to_string()),
587 })
588 .collect(),
589 ];
590 let default_settings = default_settings
591 .into_iter()
592 .flatten()
593 .map(
594 |(k, v)| (k.replace('-', "_"), v),
613 )
614 .collect();
615
616 let test_args = matches.opt_strs("test-args");
617 let test_args: Vec<String> =
618 test_args.iter().flat_map(|s| s.split_whitespace()).map(|s| s.to_string()).collect();
619
620 let should_test = matches.opt_present("test");
621 let no_run = matches.opt_present("no-run");
622
623 if !should_test && no_run {
624 dcx.fatal("the `--test` flag must be passed to enable `--no-run`");
625 }
626
627 let mut output_to_stdout = false;
628 let test_builder_wrappers =
629 matches.opt_strs("test-builder-wrapper").iter().map(PathBuf::from).collect();
630 let output = match (matches.opt_str("out-dir"), matches.opt_str("output")) {
631 (Some(_), Some(_)) => {
632 dcx.fatal("cannot use both 'out-dir' and 'output' at once");
633 }
634 (Some(out_dir), None) | (None, Some(out_dir)) => {
635 output_to_stdout = out_dir == "-";
636 PathBuf::from(out_dir)
637 }
638 (None, None) => PathBuf::from("doc"),
639 };
640
641 let cfgs = matches.opt_strs("cfg");
642 let check_cfgs = matches.opt_strs("check-cfg");
643
644 let extension_css = matches.opt_str("e").map(|s| PathBuf::from(&s));
645
646 if let Some(ref p) = extension_css
647 && !p.is_file()
648 {
649 dcx.fatal("option --extend-css argument must be a file");
650 }
651
652 let mut themes = Vec::new();
653 if matches.opt_present("theme") {
654 let mut content =
655 std::str::from_utf8(static_files::STATIC_FILES.rustdoc_css.src_bytes).unwrap();
656 if let Some((_, inside)) = content.split_once("/* Begin theme: light */") {
657 content = inside;
658 }
659 if let Some((inside, _)) = content.split_once("/* End theme: light */") {
660 content = inside;
661 }
662 let paths = match theme::load_css_paths(content) {
663 Ok(p) => p,
664 Err(e) => dcx.fatal(e),
665 };
666
667 for (theme_file, theme_s) in
668 matches.opt_strs("theme").iter().map(|s| (PathBuf::from(&s), s.to_owned()))
669 {
670 if !theme_file.is_file() {
671 dcx.struct_fatal(format!("invalid argument: \"{theme_s}\""))
672 .with_help("arguments to --theme must be files")
673 .emit();
674 }
675 if theme_file.extension() != Some(OsStr::new("css")) {
676 dcx.struct_fatal(format!("invalid argument: \"{theme_s}\""))
677 .with_help("arguments to --theme must have a .css extension")
678 .emit();
679 }
680 let (success, ret) = theme::test_theme_against(&theme_file, &paths, dcx);
681 if !success {
682 dcx.fatal(format!("error loading theme file: \"{theme_s}\""));
683 } else if !ret.is_empty() {
684 dcx.struct_warn(format!(
685 "theme file \"{theme_s}\" is missing CSS rules from the default theme",
686 ))
687 .with_warn("the theme may appear incorrect when loaded")
688 .with_help(format!(
689 "to see what rules are missing, call `rustdoc --check-theme \"{theme_s}\"`",
690 ))
691 .emit();
692 }
693 themes.push(StylePath { path: theme_file });
694 }
695 }
696
697 let edition = config::parse_crate_edition(early_dcx, matches);
698
699 let mut id_map = html::markdown::IdMap::new();
700 let Some(external_html) = ExternalHtml::load(
701 &matches.opt_strs("html-in-header"),
702 &matches.opt_strs("html-before-content"),
703 &matches.opt_strs("html-after-content"),
704 &matches.opt_strs("markdown-before-content"),
705 &matches.opt_strs("markdown-after-content"),
706 nightly_options::match_is_nightly_build(matches),
707 dcx,
708 &mut id_map,
709 edition,
710 &None,
711 ) else {
712 dcx.fatal("`ExternalHtml::load` failed");
713 };
714
715 match matches.opt_str("r").as_deref() {
716 Some("rust") | None => {}
717 Some(s) => dcx.fatal(format!("unknown input format: {s}")),
718 }
719
720 let index_page = matches.opt_str("index-page").map(|s| PathBuf::from(&s));
721 if let Some(ref index_page) = index_page
722 && !index_page.is_file()
723 {
724 dcx.fatal("option `--index-page` argument must be a file");
725 }
726
727 let target = parse_target_triple(early_dcx, matches);
728 let maybe_sysroot = matches.opt_str("sysroot").map(PathBuf::from);
729
730 let sysroot = rustc_session::filesearch::materialize_sysroot(maybe_sysroot.clone());
731
732 let libs = matches
733 .opt_strs("L")
734 .iter()
735 .map(|s| {
736 SearchPath::from_cli_opt(
737 &sysroot,
738 &target,
739 early_dcx,
740 s,
741 #[allow(rustc::bad_opt_access)] unstable_opts.unstable_options,
743 )
744 })
745 .collect();
746
747 let crate_types = match parse_crate_types_from_list(matches.opt_strs("crate-type")) {
748 Ok(types) => types,
749 Err(e) => {
750 dcx.fatal(format!("unknown crate type: {e}"));
751 }
752 };
753
754 let crate_name = matches.opt_str("crate-name");
755 let bin_crate = crate_types.contains(&CrateType::Executable);
756 let proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
757 let playground_url = matches.opt_str("playground-url");
758 let module_sorting = if matches.opt_present("sort-modules-by-appearance") {
759 ModuleSorting::DeclarationOrder
760 } else {
761 ModuleSorting::Alphabetical
762 };
763 let resource_suffix = matches.opt_str("resource-suffix").unwrap_or_default();
764 let markdown_no_toc = matches.opt_present("markdown-no-toc");
765 let markdown_css = matches.opt_strs("markdown-css");
766 let markdown_playground_url = matches.opt_str("markdown-playground-url");
767 let crate_version = matches.opt_str("crate-version");
768 let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some();
769 let static_root_path = matches.opt_str("static-root-path");
770 let test_run_directory = matches.opt_str("test-run-directory").map(PathBuf::from);
771 let persist_doctests = matches.opt_str("persist-doctests").map(PathBuf::from);
772 let test_builder = matches.opt_str("test-builder").map(PathBuf::from);
773 let codegen_options_strs = matches.opt_strs("C");
774 let unstable_opts_strs = matches.opt_strs("Z");
775 let lib_strs = matches.opt_strs("L");
776 let extern_strs = matches.opt_strs("extern");
777 let test_runtool = matches.opt_str("test-runtool");
778 let test_runtool_args = matches.opt_strs("test-runtool-arg");
779 let document_private = matches.opt_present("document-private-items");
780 let document_hidden = matches.opt_present("document-hidden-items");
781 let run_check = matches.opt_present("check");
782 let generate_redirect_map = matches.opt_present("generate-redirect-map");
783 let show_type_layout = matches.opt_present("show-type-layout");
784 let nocapture = matches.opt_present("nocapture");
785 let generate_link_to_definition = matches.opt_present("generate-link-to-definition");
786 let extern_html_root_takes_precedence =
787 matches.opt_present("extern-html-root-takes-precedence");
788 let html_no_source = matches.opt_present("html-no-source");
789 let should_merge = match parse_merge(matches) {
790 Ok(result) => result,
791 Err(e) => dcx.fatal(format!("--merge option error: {e}")),
792 };
793
794 if generate_link_to_definition && (show_coverage || output_format != OutputFormat::Html) {
795 dcx.struct_warn(
796 "`--generate-link-to-definition` option can only be used with HTML output format",
797 )
798 .with_note("`--generate-link-to-definition` option will be ignored")
799 .emit();
800 }
801
802 let scrape_examples_options = ScrapeExamplesOptions::new(matches, dcx);
803 let with_examples = matches.opt_strs("with-examples");
804 let call_locations = crate::scrape_examples::load_call_locations(with_examples, dcx);
805 let doctest_build_args = matches.opt_strs("doctest-build-arg");
806
807 let unstable_features =
808 rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref());
809
810 let disable_minification = matches.opt_present("disable-minification");
811
812 let options = Options {
813 bin_crate,
814 proc_macro_crate,
815 error_format,
816 diagnostic_width,
817 libs,
818 lib_strs,
819 externs,
820 extern_strs,
821 cfgs,
822 check_cfgs,
823 codegen_options,
824 codegen_options_strs,
825 unstable_opts,
826 unstable_opts_strs,
827 target,
828 edition,
829 sysroot,
830 maybe_sysroot,
831 lint_opts,
832 describe_lints,
833 lint_cap,
834 should_test,
835 test_args,
836 show_coverage,
837 crate_version,
838 test_run_directory,
839 persist_doctests,
840 test_runtool,
841 test_runtool_args,
842 test_builder,
843 run_check,
844 no_run,
845 test_builder_wrappers,
846 remap_path_prefix,
847 nocapture,
848 crate_name,
849 output_format,
850 json_unused_externs,
851 scrape_examples_options,
852 unstable_features,
853 expanded_args: args,
854 doctest_build_args,
855 };
856 let render_options = RenderOptions {
857 output,
858 external_html,
859 id_map,
860 playground_url,
861 module_sorting,
862 themes,
863 extension_css,
864 extern_html_root_urls,
865 extern_html_root_takes_precedence,
866 default_settings,
867 resource_suffix,
868 enable_index_page,
869 index_page,
870 static_root_path,
871 markdown_no_toc,
872 markdown_css,
873 markdown_playground_url,
874 document_private,
875 document_hidden,
876 generate_redirect_map,
877 show_type_layout,
878 unstable_features,
879 emit,
880 generate_link_to_definition,
881 call_locations,
882 no_emit_shared: false,
883 html_no_source,
884 output_to_stdout,
885 should_merge,
886 include_parts_dir,
887 parts_out_dir,
888 disable_minification,
889 };
890 Some((input, options, render_options))
891 }
892}
893
894pub(crate) fn markdown_input(input: &Input) -> Option<&Path> {
896 input.opt_path().filter(|p| matches!(p.extension(), Some(e) if e == "md" || e == "markdown"))
897}
898
899fn parse_remap_path_prefix(
900 matches: &getopts::Matches,
901) -> Result<Vec<(PathBuf, PathBuf)>, &'static str> {
902 matches
903 .opt_strs("remap-path-prefix")
904 .into_iter()
905 .map(|remap| {
906 remap
907 .rsplit_once('=')
908 .ok_or("--remap-path-prefix must contain '=' between FROM and TO")
909 .map(|(from, to)| (PathBuf::from(from), PathBuf::from(to)))
910 })
911 .collect()
912}
913
914fn check_deprecated_options(matches: &getopts::Matches, dcx: DiagCtxtHandle<'_>) {
916 let deprecated_flags = [];
917
918 for &flag in deprecated_flags.iter() {
919 if matches.opt_present(flag) {
920 dcx.struct_warn(format!("the `{flag}` flag is deprecated"))
921 .with_note(
922 "see issue #44136 <https://github.com/rust-lang/rust/issues/44136> \
923 for more information",
924 )
925 .emit();
926 }
927 }
928
929 let removed_flags = ["plugins", "plugin-path", "no-defaults", "passes", "input-format"];
930
931 for &flag in removed_flags.iter() {
932 if matches.opt_present(flag) {
933 let mut err = dcx.struct_warn(format!("the `{flag}` flag no longer functions"));
934 err.note(
935 "see issue #44136 <https://github.com/rust-lang/rust/issues/44136> \
936 for more information",
937 );
938
939 if flag == "no-defaults" || flag == "passes" {
940 err.help("you may want to use --document-private-items");
941 } else if flag == "plugins" || flag == "plugin-path" {
942 err.warn("see CVE-2018-1000622");
943 }
944
945 err.emit();
946 }
947 }
948}
949
950fn parse_extern_html_roots(
954 matches: &getopts::Matches,
955) -> Result<BTreeMap<String, String>, &'static str> {
956 let mut externs = BTreeMap::new();
957 for arg in &matches.opt_strs("extern-html-root-url") {
958 let (name, url) =
959 arg.split_once('=').ok_or("--extern-html-root-url must be of the form name=url")?;
960 externs.insert(name.to_string(), url.to_string());
961 }
962 Ok(externs)
963}
964
965#[derive(Clone, Debug)]
969pub(crate) struct PathToParts(pub(crate) PathBuf);
970
971impl PathToParts {
972 fn from_flag(path: String) -> Result<PathToParts, String> {
973 let mut path = PathBuf::from(path);
974 if path.exists() && !path.is_dir() {
976 Err(format!(
977 "--parts-out-dir and --include-parts-dir expect directories, found: {}",
978 path.display(),
979 ))
980 } else {
981 path.push("crate-info");
983 Ok(PathToParts(path))
984 }
985 }
986}
987
988fn parse_include_parts_dir(m: &getopts::Matches) -> Result<Vec<PathToParts>, String> {
990 let mut ret = Vec::new();
991 for p in m.opt_strs("include-parts-dir") {
992 let p = PathToParts::from_flag(p)?;
993 if !p.0.is_file() {
995 return Err(format!("--include-parts-dir expected {} to be a file", p.0.display()));
996 }
997 ret.push(p);
998 }
999 Ok(ret)
1000}
1001
1002#[derive(Debug, Clone)]
1004pub(crate) struct ShouldMerge {
1005 pub(crate) read_rendered_cci: bool,
1007 pub(crate) write_rendered_cci: bool,
1009}
1010
1011fn parse_merge(m: &getopts::Matches) -> Result<ShouldMerge, &'static str> {
1014 match m.opt_str("merge").as_deref() {
1015 None => Ok(ShouldMerge { read_rendered_cci: true, write_rendered_cci: true }),
1017 Some("none") if m.opt_present("include-parts-dir") => {
1018 Err("--include-parts-dir not allowed if --merge=none")
1019 }
1020 Some("none") => Ok(ShouldMerge { read_rendered_cci: false, write_rendered_cci: false }),
1021 Some("shared") if m.opt_present("parts-out-dir") || m.opt_present("include-parts-dir") => {
1022 Err("--parts-out-dir and --include-parts-dir not allowed if --merge=shared")
1023 }
1024 Some("shared") => Ok(ShouldMerge { read_rendered_cci: true, write_rendered_cci: true }),
1025 Some("finalize") if m.opt_present("parts-out-dir") => {
1026 Err("--parts-out-dir not allowed if --merge=finalize")
1027 }
1028 Some("finalize") => Ok(ShouldMerge { read_rendered_cci: false, write_rendered_cci: true }),
1029 Some(_) => Err("argument to --merge must be `none`, `shared`, or `finalize`"),
1030 }
1031}