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, OutFileName, Sysroot, UnstableOptions, get_cmd_lint_options,
13 nightly_options, 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::edition::Edition;
19use rustc_span::{FileName, RemapPathScopeComponents};
20use rustc_target::spec::TargetTuple;
21use smallvec::SmallVec;
22
23use crate::core::new_dcx;
24use crate::externalfiles::ExternalHtml;
25use crate::html::markdown::IdMap;
26use crate::html::render::StylePath;
27use crate::html::static_files;
28use crate::passes::{self, Condition};
29use crate::scrape_examples::{AllCallLocations, ScrapeExamplesOptions};
30use crate::{html, opts, theme};
31
32#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
33pub(crate) enum OutputFormat {
34 Json,
35 #[default]
36 Html,
37 Doctest,
38}
39
40impl OutputFormat {
41 pub(crate) fn is_json(&self) -> bool {
42 matches!(self, OutputFormat::Json)
43 }
44}
45
46impl TryFrom<&str> for OutputFormat {
47 type Error = String;
48
49 fn try_from(value: &str) -> Result<Self, Self::Error> {
50 match value {
51 "json" => Ok(OutputFormat::Json),
52 "html" => Ok(OutputFormat::Html),
53 "doctest" => Ok(OutputFormat::Doctest),
54 _ => Err(format!("unknown output format `{value}`")),
55 }
56 }
57}
58
59pub(crate) enum InputMode {
61 NoInputMergeFinalize,
63 HasFile(Input),
65}
66
67#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
69pub(crate) enum MergeDoctests {
70 #[default]
71 Never,
72 Always,
73 Auto,
74}
75
76#[derive(Clone)]
78pub(crate) struct Options {
79 pub(crate) crate_name: Option<String>,
82 pub(crate) bin_crate: bool,
84 pub(crate) proc_macro_crate: bool,
86 pub(crate) error_format: ErrorOutputType,
88 pub(crate) diagnostic_width: Option<usize>,
90 pub(crate) libs: Vec<SearchPath>,
92 pub(crate) lib_strs: Vec<String>,
94 pub(crate) externs: Externs,
96 pub(crate) extern_strs: Vec<String>,
98 pub(crate) cfgs: Vec<String>,
100 pub(crate) check_cfgs: Vec<String>,
102 pub(crate) codegen_options: CodegenOptions,
104 pub(crate) codegen_options_strs: Vec<String>,
106 pub(crate) unstable_opts: UnstableOptions,
108 pub(crate) unstable_opts_strs: Vec<String>,
110 pub(crate) target: TargetTuple,
112 pub(crate) edition: Edition,
115 pub(crate) sysroot: Sysroot,
117 pub(crate) lint_opts: Vec<(String, Level)>,
119 pub(crate) describe_lints: bool,
121 pub(crate) lint_cap: Option<Level>,
123
124 pub(crate) should_test: bool,
127 pub(crate) test_args: Vec<String>,
129 pub(crate) test_run_directory: Option<PathBuf>,
131 pub(crate) persist_doctests: Option<PathBuf>,
134 pub(crate) merge_doctests: MergeDoctests,
136 pub(crate) test_runtool: Option<String>,
138 pub(crate) test_runtool_args: Vec<String>,
140 pub(crate) no_run: bool,
142 pub(crate) remap_path_prefix: Vec<(PathBuf, PathBuf)>,
144 pub(crate) remap_path_scope: RemapPathScopeComponents,
146
147 pub(crate) test_builder: Option<PathBuf>,
150
151 pub(crate) test_builder_wrappers: Vec<PathBuf>,
153
154 pub(crate) show_coverage: bool,
158
159 pub(crate) crate_version: Option<String>,
162 pub(crate) output_format: OutputFormat,
166 pub(crate) run_check: bool,
169 pub(crate) json_unused_externs: JsonUnusedExterns,
171 pub(crate) no_capture: bool,
173
174 pub(crate) scrape_examples_options: Option<ScrapeExamplesOptions>,
177
178 pub(crate) unstable_features: rustc_feature::UnstableFeatures,
181
182 pub(crate) doctest_build_args: Vec<String>,
184
185 pub(crate) target_modifiers: BTreeMap<OptionsTargetModifiers, String>,
187}
188
189impl fmt::Debug for Options {
190 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191 struct FmtExterns<'a>(&'a Externs);
192
193 impl fmt::Debug for FmtExterns<'_> {
194 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
195 f.debug_map().entries(self.0.iter()).finish()
196 }
197 }
198
199 f.debug_struct("Options")
200 .field("crate_name", &self.crate_name)
201 .field("bin_crate", &self.bin_crate)
202 .field("proc_macro_crate", &self.proc_macro_crate)
203 .field("error_format", &self.error_format)
204 .field("libs", &self.libs)
205 .field("externs", &FmtExterns(&self.externs))
206 .field("cfgs", &self.cfgs)
207 .field("check-cfgs", &self.check_cfgs)
208 .field("codegen_options", &"...")
209 .field("unstable_options", &"...")
210 .field("target", &self.target)
211 .field("edition", &self.edition)
212 .field("sysroot", &self.sysroot)
213 .field("lint_opts", &self.lint_opts)
214 .field("describe_lints", &self.describe_lints)
215 .field("lint_cap", &self.lint_cap)
216 .field("should_test", &self.should_test)
217 .field("test_args", &self.test_args)
218 .field("test_run_directory", &self.test_run_directory)
219 .field("persist_doctests", &self.persist_doctests)
220 .field("show_coverage", &self.show_coverage)
221 .field("crate_version", &self.crate_version)
222 .field("test_runtool", &self.test_runtool)
223 .field("test_runtool_args", &self.test_runtool_args)
224 .field("run_check", &self.run_check)
225 .field("no_run", &self.no_run)
226 .field("test_builder_wrappers", &self.test_builder_wrappers)
227 .field("remap-file-prefix", &self.remap_path_prefix)
228 .field("remap-file-scope", &self.remap_path_scope)
229 .field("no_capture", &self.no_capture)
230 .field("scrape_examples_options", &self.scrape_examples_options)
231 .field("unstable_features", &self.unstable_features)
232 .finish()
233 }
234}
235
236#[derive(Clone, Debug)]
238pub(crate) struct RenderOptions {
239 pub(crate) output: PathBuf,
241 pub(crate) external_html: ExternalHtml,
243 pub(crate) id_map: IdMap,
246 pub(crate) playground_url: Option<String>,
250 pub(crate) module_sorting: ModuleSorting,
253 pub(crate) themes: Vec<StylePath>,
256 pub(crate) extension_css: Option<PathBuf>,
258 pub(crate) extern_html_root_urls: BTreeMap<String, String>,
260 pub(crate) extern_html_root_takes_precedence: bool,
262 pub(crate) default_settings: FxIndexMap<String, String>,
265 pub(crate) resource_suffix: String,
267 pub(crate) enable_index_page: bool,
270 pub(crate) index_page: Option<PathBuf>,
273 pub(crate) static_root_path: Option<String>,
276
277 pub(crate) markdown_no_toc: bool,
281 pub(crate) markdown_css: Vec<String>,
283 pub(crate) markdown_playground_url: Option<String>,
286 pub(crate) document_private: bool,
288 pub(crate) document_hidden: bool,
290 pub(crate) generate_redirect_map: bool,
292 pub(crate) show_type_layout: bool,
294 pub(crate) unstable_features: rustc_feature::UnstableFeatures,
297 pub(crate) emit: SmallVec<[EmitType; 2]>,
298 pub(crate) generate_link_to_definition: bool,
300 pub(crate) call_locations: AllCallLocations,
302 pub(crate) no_emit_shared: bool,
304 pub(crate) html_no_source: bool,
306 pub(crate) output_to_stdout: bool,
309 pub(crate) should_merge: ShouldMerge,
311 pub(crate) include_parts_dir: Vec<PathToParts>,
313 pub(crate) parts_out_dir: Option<PathToParts>,
315 pub(crate) disable_minification: bool,
317 pub(crate) generate_macro_expansion: bool,
319}
320
321#[derive(Copy, Clone, Debug, PartialEq, Eq)]
322pub(crate) enum ModuleSorting {
323 DeclarationOrder,
324 Alphabetical,
325}
326
327#[derive(Clone, Debug, PartialEq, Eq)]
328pub(crate) enum EmitType {
329 HtmlStaticFiles,
330 HtmlNonStaticFiles,
331 JsonFiles,
333 DepInfo(Option<OutFileName>),
334}
335
336impl fmt::Display for EmitType {
337 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
338 f.write_str(match self {
339 Self::HtmlStaticFiles => "html-static-files",
340 Self::HtmlNonStaticFiles => "html-non-static-files",
341 Self::JsonFiles => "json-files",
342 Self::DepInfo(_) => "dep-info",
343 })
344 }
345}
346
347impl FromStr for EmitType {
348 type Err = ();
349
350 fn from_str(s: &str) -> Result<Self, Self::Err> {
351 match s {
352 "toolchain-shared-resources" => Ok(Self::HtmlStaticFiles),
354 "invocation-specific" => Ok(Self::HtmlNonStaticFiles),
355 "html-static-files" => Ok(Self::HtmlStaticFiles),
357 "html-non-static-files" => Ok(Self::HtmlNonStaticFiles),
358 "dep-info" => Ok(Self::DepInfo(None)),
359 option => match option.strip_prefix("dep-info=") {
360 Some("-") => Ok(Self::DepInfo(Some(OutFileName::Stdout))),
361 Some(f) => Ok(Self::DepInfo(Some(OutFileName::Real(f.into())))),
362 None => Err(()),
363 },
364 }
365 }
366}
367
368impl RenderOptions {
369 pub(crate) fn dep_info(&self) -> Option<Option<&OutFileName>> {
370 self.emit.iter().find_map(|emit| match emit {
371 EmitType::DepInfo(file) => Some(file.as_ref()),
372 _ => None,
373 })
374 }
375}
376
377fn make_input(early_dcx: &EarlyDiagCtxt, input: &str) -> Input {
381 if input == "-" {
382 let mut src = String::new();
383 if io::stdin().read_to_string(&mut src).is_err() {
384 early_dcx.early_fatal("couldn't read from stdin, as it did not contain valid UTF-8");
387 }
388 Input::Str { name: FileName::anon_source_code(&src), input: src }
389 } else {
390 Input::File(PathBuf::from(input))
391 }
392}
393
394impl Options {
395 pub(crate) fn from_matches(
398 early_dcx: &mut EarlyDiagCtxt,
399 matches: &getopts::Matches,
400 args: Vec<String>,
401 ) -> Option<(InputMode, Options, RenderOptions, Vec<PathBuf>)> {
402 nightly_options::check_nightly_options(early_dcx, matches, &opts());
404
405 if args.is_empty() || matches.opt_present("h") || matches.opt_present("help") {
406 crate::usage("rustdoc");
407 return None;
408 } else if matches.opt_present("version") {
409 rustc_driver::version!(&early_dcx, "rustdoc", matches);
410 return None;
411 }
412
413 if rustc_driver::describe_flag_categories(early_dcx, matches) {
414 return None;
415 }
416
417 let color = config::parse_color(early_dcx, matches);
418 let crate_name = matches.opt_str("crate-name");
419 let unstable_features =
420 rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref());
421 let config::JsonConfig { json_rendered, json_unused_externs, json_color, .. } =
422 config::parse_json(early_dcx, matches);
423 let error_format =
424 config::parse_error_format(early_dcx, matches, color, json_color, json_rendered);
425 let diagnostic_width = matches.opt_get("diagnostic-width").unwrap_or_default();
426
427 let mut collected_options = Default::default();
428 let codegen_options = CodegenOptions::build(early_dcx, matches, &mut collected_options);
429 let unstable_opts = UnstableOptions::build(early_dcx, matches, &mut collected_options);
430
431 let remap_path_prefix = match parse_remap_path_prefix(matches) {
432 Ok(prefix_mappings) => prefix_mappings,
433 Err(err) => {
434 early_dcx.early_fatal(err);
435 }
436 };
437 let remap_path_scope =
438 rustc_session::config::parse_remap_path_scope(early_dcx, matches, &unstable_opts);
439
440 let dcx = new_dcx(error_format, None, diagnostic_width, &unstable_opts);
441 let dcx = dcx.handle();
442
443 check_deprecated_options(matches, dcx);
445
446 if matches.opt_strs("passes") == ["list"] {
447 println!("Available passes for running rustdoc:");
448 for pass in passes::PASSES {
449 println!("{:>20} - {}", pass.name, pass.description);
450 }
451 println!("\nDefault passes for rustdoc:");
452 for p in passes::DEFAULT_PASSES {
453 print!("{:>20}", p.pass.name);
454 println_condition(p.condition);
455 }
456
457 if nightly_options::match_is_nightly_build(matches) {
458 println!("\nPasses run with `--show-coverage`:");
459 for p in passes::COVERAGE_PASSES {
460 print!("{:>20}", p.pass.name);
461 println_condition(p.condition);
462 }
463 }
464
465 fn println_condition(condition: Condition) {
466 use Condition::*;
467 match condition {
468 Always => println!(),
469 WhenDocumentPrivate => println!(" (when --document-private-items)"),
470 WhenNotDocumentPrivate => println!(" (when not --document-private-items)"),
471 WhenNotDocumentHidden => println!(" (when not --document-hidden-items)"),
472 }
473 }
474
475 return None;
476 }
477
478 let should_test = matches.opt_present("test");
479
480 let show_coverage = matches.opt_present("show-coverage");
481 let output_format_s = matches.opt_str("output-format");
482 let output_format = match output_format_s {
483 Some(ref s) => match OutputFormat::try_from(s.as_str()) {
484 Ok(out_fmt) => out_fmt,
485 Err(e) => dcx.fatal(e),
486 },
487 None => OutputFormat::default(),
488 };
489
490 match (
492 output_format_s.as_ref().map(|_| output_format),
493 show_coverage,
494 nightly_options::is_unstable_enabled(matches),
495 ) {
496 (None | Some(OutputFormat::Json), true, _) => {}
497 (_, true, _) => {
498 dcx.fatal(format!(
499 "`--output-format={}` is not supported for the `--show-coverage` option",
500 output_format_s.unwrap_or_default(),
501 ));
502 }
503 (_, false, true) => {}
505 (None | Some(OutputFormat::Html), false, _) => {}
506 (Some(OutputFormat::Json), false, false) => {
507 dcx.fatal(
508 "the -Z unstable-options flag must be passed to enable --output-format for documentation generation (see https://github.com/rust-lang/rust/issues/76578)",
509 );
510 }
511 (Some(OutputFormat::Doctest), false, false) => {
512 dcx.fatal(
513 "the -Z unstable-options flag must be passed to enable --output-format for documentation generation (see https://github.com/rust-lang/rust/issues/134529)",
514 );
515 }
516 }
517
518 let mut emit = FxIndexMap::default();
519 for list in matches.opt_strs("emit") {
520 if should_test {
521 dcx.fatal("the `--test` flag and the `--emit` flag are not supported together");
522 }
523 if let OutputFormat::Doctest = output_format {
524 dcx.fatal("the `--emit` flag is not supported with `--output-format=doctest`");
525 }
526
527 for typ in list.split(',') {
528 let Ok(typ) = typ.parse::<EmitType>() else {
529 dcx.fatal(format!("unrecognized emission type: {typ}"))
530 };
531
532 match typ {
533 EmitType::DepInfo(_) => match output_format {
534 OutputFormat::Json | OutputFormat::Html => {}
535 OutputFormat::Doctest => unreachable!(),
536 },
537 EmitType::HtmlStaticFiles | EmitType::HtmlNonStaticFiles => match output_format
538 {
539 OutputFormat::Html => {}
540 OutputFormat::Json => dcx.fatal(format!(
541 "the `--emit={typ}` flag is not supported with `--output-format=json`",
542 )),
543 OutputFormat::Doctest => unreachable!(),
544 },
545 EmitType::JsonFiles => unreachable!(),
546 }
547
548 emit.insert(std::mem::discriminant(&typ), typ);
553 }
554 }
555 let mut emit: SmallVec<[_; 2]> = emit.into_values().collect();
556 if emit.is_empty() {
560 match output_format {
561 OutputFormat::Json => emit.push(EmitType::JsonFiles),
562 OutputFormat::Html => {
563 emit.push(EmitType::HtmlStaticFiles);
564 emit.push(EmitType::HtmlNonStaticFiles);
565 }
566 OutputFormat::Doctest => {}
567 }
568 }
569
570 let to_check = matches.opt_strs("check-theme");
571 if !to_check.is_empty() {
572 let mut content =
573 std::str::from_utf8(static_files::STATIC_FILES.rustdoc_css.src_bytes).unwrap();
574 if let Some((_, inside)) = content.split_once("/* Begin theme: light */") {
575 content = inside;
576 }
577 if let Some((inside, _)) = content.split_once("/* End theme: light */") {
578 content = inside;
579 }
580 let paths = match theme::load_css_paths(content) {
581 Ok(p) => p,
582 Err(e) => dcx.fatal(e),
583 };
584 let mut errors = 0;
585
586 println!("rustdoc: [check-theme] Starting tests! (Ignoring all other arguments)");
587 for theme_file in to_check.iter() {
588 print!(" - Checking \"{theme_file}\"...");
589 let (success, differences) = theme::test_theme_against(theme_file, &paths, dcx);
590 if !differences.is_empty() || !success {
591 println!(" FAILED");
592 errors += 1;
593 if !differences.is_empty() {
594 println!("{}", differences.join("\n"));
595 }
596 } else {
597 println!(" OK");
598 }
599 }
600 if errors != 0 {
601 dcx.fatal("[check-theme] one or more tests failed");
602 }
603 return None;
604 }
605
606 let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(early_dcx, matches);
607
608 let input = if describe_lints {
609 InputMode::HasFile(make_input(early_dcx, ""))
610 } else {
611 match matches.free.as_slice() {
612 [] if matches.opt_str("merge").as_deref() == Some("finalize") => {
613 InputMode::NoInputMergeFinalize
614 }
615 [] => dcx.fatal("missing file operand"),
616 [input] => InputMode::HasFile(make_input(early_dcx, input)),
617 _ => dcx.fatal("too many file operands"),
618 }
619 };
620
621 let externs = parse_externs(early_dcx, matches, &unstable_opts);
622 let extern_html_root_urls = match parse_extern_html_roots(matches) {
623 Ok(ex) => ex,
624 Err(err) => dcx.fatal(err),
625 };
626
627 let parts_out_dir =
628 match matches.opt_str("parts-out-dir").map(PathToParts::from_flag).transpose() {
629 Ok(parts_out_dir) => parts_out_dir,
630 Err(e) => dcx.fatal(e),
631 };
632 let include_parts_dir = match parse_include_parts_dir(matches) {
633 Ok(include_parts_dir) => include_parts_dir,
634 Err(e) => dcx.fatal(e),
635 };
636
637 let default_settings: Vec<Vec<(String, String)>> = vec![
638 matches
639 .opt_str("default-theme")
640 .iter()
641 .flat_map(|theme| {
642 vec![
643 ("use-system-theme".to_string(), "false".to_string()),
644 ("theme".to_string(), theme.to_string()),
645 ]
646 })
647 .collect(),
648 matches
649 .opt_strs("default-setting")
650 .iter()
651 .map(|s| match s.split_once('=') {
652 None => (s.clone(), "true".to_string()),
653 Some((k, v)) => (k.to_string(), v.to_string()),
654 })
655 .collect(),
656 ];
657 let default_settings = default_settings
658 .into_iter()
659 .flatten()
660 .map(
661 |(k, v)| (k.replace('-', "_"), v),
680 )
681 .collect();
682
683 let test_args = matches.opt_strs("test-args");
684 let test_args: Vec<String> =
685 test_args.iter().flat_map(|s| s.split_whitespace()).map(|s| s.to_string()).collect();
686
687 let no_run = matches.opt_present("no-run");
688
689 if !should_test && no_run {
690 dcx.fatal("the `--test` flag must be passed to enable `--no-run`");
691 }
692
693 let mut output_to_stdout = false;
694 let test_builder_wrappers =
695 matches.opt_strs("test-builder-wrapper").iter().map(PathBuf::from).collect();
696 let output = match (matches.opt_str("out-dir"), matches.opt_str("output")) {
697 (Some(_), Some(_)) => {
698 dcx.fatal("cannot use both 'out-dir' and 'output' at once");
699 }
700 (Some(out_dir), None) | (None, Some(out_dir)) => {
701 output_to_stdout = out_dir == "-";
702 PathBuf::from(out_dir)
703 }
704 (None, None) => PathBuf::from("doc"),
705 };
706
707 let cfgs = matches.opt_strs("cfg");
708 let check_cfgs = matches.opt_strs("check-cfg");
709
710 let extension_css = matches.opt_str("e").map(|s| PathBuf::from(&s));
711
712 let mut loaded_paths = Vec::new();
713
714 if let Some(ref p) = extension_css {
715 loaded_paths.push(p.clone());
716 if !p.is_file() {
717 dcx.fatal("option --extend-css argument must be a file");
718 }
719 }
720
721 let mut themes = Vec::new();
722 if matches.opt_present("theme") {
723 let mut content =
724 std::str::from_utf8(static_files::STATIC_FILES.rustdoc_css.src_bytes).unwrap();
725 if let Some((_, inside)) = content.split_once("/* Begin theme: light */") {
726 content = inside;
727 }
728 if let Some((inside, _)) = content.split_once("/* End theme: light */") {
729 content = inside;
730 }
731 let paths = match theme::load_css_paths(content) {
732 Ok(p) => p,
733 Err(e) => dcx.fatal(e),
734 };
735
736 for (theme_file, theme_s) in
737 matches.opt_strs("theme").iter().map(|s| (PathBuf::from(&s), s.to_owned()))
738 {
739 if !theme_file.is_file() {
740 dcx.struct_fatal(format!("invalid argument: \"{theme_s}\""))
741 .with_help("arguments to --theme must be files")
742 .emit();
743 }
744 if theme_file.extension() != Some(OsStr::new("css")) {
745 dcx.struct_fatal(format!("invalid argument: \"{theme_s}\""))
746 .with_help("arguments to --theme must have a .css extension")
747 .emit();
748 }
749 let (success, ret) = theme::test_theme_against(&theme_file, &paths, dcx);
750 if !success {
751 dcx.fatal(format!("error loading theme file: \"{theme_s}\""));
752 } else if !ret.is_empty() {
753 dcx.struct_warn(format!(
754 "theme file \"{theme_s}\" is missing CSS rules from the default theme",
755 ))
756 .with_warn("the theme may appear incorrect when loaded")
757 .with_help(format!(
758 "to see what rules are missing, call `rustdoc --check-theme \"{theme_s}\"`",
759 ))
760 .emit();
761 }
762 loaded_paths.push(theme_file.clone());
763 themes.push(StylePath { path: theme_file });
764 }
765 }
766
767 let edition = config::parse_crate_edition(early_dcx, matches);
768
769 let mut id_map = html::markdown::IdMap::new();
770 let Some(external_html) = ExternalHtml::load(
771 &matches.opt_strs("html-in-header"),
772 &matches.opt_strs("html-before-content"),
773 &matches.opt_strs("html-after-content"),
774 &matches.opt_strs("markdown-before-content"),
775 &matches.opt_strs("markdown-after-content"),
776 nightly_options::match_is_nightly_build(matches),
777 dcx,
778 &mut id_map,
779 edition,
780 &None,
781 &mut loaded_paths,
782 ) else {
783 dcx.fatal("`ExternalHtml::load` failed");
784 };
785
786 match matches.opt_str("r").as_deref() {
787 Some("rust") | None => {}
788 Some(s) => dcx.fatal(format!("unknown input format: {s}")),
789 }
790
791 let index_page = matches.opt_str("index-page").map(|s| PathBuf::from(&s));
792 if let Some(ref index_page) = index_page {
793 if index_page.is_file() {
794 loaded_paths.push(index_page.clone());
795 } else {
796 dcx.fatal("option `--index-page` argument must be a file");
797 }
798 }
799
800 let target = parse_target_triple(early_dcx, matches);
801 let sysroot = Sysroot::new(matches.opt_str("sysroot").map(PathBuf::from));
802
803 let libs = matches
804 .opt_strs("L")
805 .iter()
806 .map(|s| {
807 SearchPath::from_cli_opt(
808 sysroot.path(),
809 &target,
810 early_dcx,
811 s,
812 #[allow(rustc::bad_opt_access)] unstable_opts.unstable_options,
814 )
815 })
816 .collect();
817
818 let crate_types = match parse_crate_types_from_list(matches.opt_strs("crate-type")) {
819 Ok(types) => types,
820 Err(e) => {
821 dcx.fatal(format!("unknown crate type: {e}"));
822 }
823 };
824
825 let bin_crate = crate_types.contains(&CrateType::Executable);
826 let proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
827 let playground_url = matches.opt_str("playground-url");
828 let module_sorting = if matches.opt_present("sort-modules-by-appearance") {
829 ModuleSorting::DeclarationOrder
830 } else {
831 ModuleSorting::Alphabetical
832 };
833 let resource_suffix = matches.opt_str("resource-suffix").unwrap_or_default();
834 let markdown_no_toc = matches.opt_present("markdown-no-toc");
835 let markdown_css = matches.opt_strs("markdown-css");
836 let markdown_playground_url = matches.opt_str("markdown-playground-url");
837 let crate_version = matches.opt_str("crate-version");
838 let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some();
839 let static_root_path = matches.opt_str("static-root-path");
840 let test_run_directory = matches.opt_str("test-run-directory").map(PathBuf::from);
841 let persist_doctests = matches.opt_str("persist-doctests").map(PathBuf::from);
842 let test_builder = matches.opt_str("test-builder").map(PathBuf::from);
843 let codegen_options_strs = matches.opt_strs("C");
844 let unstable_opts_strs = matches.opt_strs("Z");
845 let lib_strs = matches.opt_strs("L");
846 let extern_strs = matches.opt_strs("extern");
847 let test_runtool = matches.opt_str("test-runtool");
848 let test_runtool_args = matches.opt_strs("test-runtool-arg");
849 let document_private = matches.opt_present("document-private-items");
850 let document_hidden = matches.opt_present("document-hidden-items");
851 let run_check = matches.opt_present("check");
852 let generate_redirect_map = matches.opt_present("generate-redirect-map");
853 let show_type_layout = matches.opt_present("show-type-layout");
854 let no_capture = matches.opt_present("no-capture");
855 let generate_link_to_definition = matches.opt_present("generate-link-to-definition");
856 let generate_macro_expansion = matches.opt_present("generate-macro-expansion");
857 let extern_html_root_takes_precedence =
858 matches.opt_present("extern-html-root-takes-precedence");
859 let html_no_source = matches.opt_present("html-no-source");
860 let should_merge = match parse_merge(matches) {
861 Ok(result) => result,
862 Err(e) => dcx.fatal(format!("--merge option error: {e}")),
863 };
864 let merge_doctests = parse_merge_doctests(matches, edition, dcx);
865 tracing::debug!("merge_doctests: {merge_doctests:?}");
866
867 if generate_link_to_definition && (show_coverage || output_format != OutputFormat::Html) {
868 dcx.struct_warn(
869 "`--generate-link-to-definition` option can only be used with HTML output format",
870 )
871 .with_note("`--generate-link-to-definition` option will be ignored")
872 .emit();
873 }
874 if generate_macro_expansion && (show_coverage || output_format != OutputFormat::Html) {
875 dcx.struct_warn(
876 "`--generate-macro-expansion` option can only be used with HTML output format",
877 )
878 .with_note("`--generate-macro-expansion` option will be ignored")
879 .emit();
880 }
881
882 let scrape_examples_options = ScrapeExamplesOptions::new(matches, dcx);
883 let with_examples = matches.opt_strs("with-examples");
884 let call_locations =
885 crate::scrape_examples::load_call_locations(with_examples, dcx, &mut loaded_paths);
886 let doctest_build_args = matches.opt_strs("doctest-build-arg");
887
888 let disable_minification = matches.opt_present("disable-minification");
889
890 let options = Options {
891 bin_crate,
892 proc_macro_crate,
893 error_format,
894 diagnostic_width,
895 libs,
896 lib_strs,
897 externs,
898 extern_strs,
899 cfgs,
900 check_cfgs,
901 codegen_options,
902 codegen_options_strs,
903 unstable_opts,
904 unstable_opts_strs,
905 target,
906 edition,
907 sysroot,
908 lint_opts,
909 describe_lints,
910 lint_cap,
911 should_test,
912 test_args,
913 show_coverage,
914 crate_version,
915 test_run_directory,
916 persist_doctests,
917 merge_doctests,
918 test_runtool,
919 test_runtool_args,
920 test_builder,
921 run_check,
922 no_run,
923 test_builder_wrappers,
924 remap_path_prefix,
925 remap_path_scope,
926 no_capture,
927 crate_name,
928 output_format,
929 json_unused_externs,
930 scrape_examples_options,
931 unstable_features,
932 doctest_build_args,
933 target_modifiers: collected_options.target_modifiers,
934 };
935 let render_options = RenderOptions {
936 output,
937 external_html,
938 id_map,
939 playground_url,
940 module_sorting,
941 themes,
942 extension_css,
943 extern_html_root_urls,
944 extern_html_root_takes_precedence,
945 default_settings,
946 resource_suffix,
947 enable_index_page,
948 index_page,
949 static_root_path,
950 markdown_no_toc,
951 markdown_css,
952 markdown_playground_url,
953 document_private,
954 document_hidden,
955 generate_redirect_map,
956 show_type_layout,
957 unstable_features,
958 emit,
959 generate_link_to_definition,
960 generate_macro_expansion,
961 call_locations,
962 no_emit_shared: false,
963 html_no_source,
964 output_to_stdout,
965 should_merge,
966 include_parts_dir,
967 parts_out_dir,
968 disable_minification,
969 };
970 Some((input, options, render_options, loaded_paths))
971 }
972}
973
974pub(crate) fn markdown_input(input: &Input) -> Option<&Path> {
976 input.opt_path().filter(|p| matches!(p.extension(), Some(e) if e == "md" || e == "markdown"))
977}
978
979fn parse_remap_path_prefix(
980 matches: &getopts::Matches,
981) -> Result<Vec<(PathBuf, PathBuf)>, &'static str> {
982 matches
983 .opt_strs("remap-path-prefix")
984 .into_iter()
985 .map(|remap| {
986 remap
987 .rsplit_once('=')
988 .ok_or("--remap-path-prefix must contain '=' between FROM and TO")
989 .map(|(from, to)| (PathBuf::from(from), PathBuf::from(to)))
990 })
991 .collect()
992}
993
994fn check_deprecated_options(matches: &getopts::Matches, dcx: DiagCtxtHandle<'_>) {
996 let deprecated_flags = [];
997
998 for &flag in deprecated_flags.iter() {
999 if matches.opt_present(flag) {
1000 dcx.struct_warn(format!("the `{flag}` flag is deprecated"))
1001 .with_note(
1002 "see issue #44136 <https://github.com/rust-lang/rust/issues/44136> \
1003 for more information",
1004 )
1005 .emit();
1006 }
1007 }
1008
1009 let removed_flags = ["plugins", "plugin-path", "no-defaults", "passes", "input-format"];
1010
1011 for &flag in removed_flags.iter() {
1012 if matches.opt_present(flag) {
1013 let mut err = dcx.struct_warn(format!("the `{flag}` flag no longer functions"));
1014 err.note(
1015 "see issue #44136 <https://github.com/rust-lang/rust/issues/44136> \
1016 for more information",
1017 );
1018
1019 if flag == "no-defaults" || flag == "passes" {
1020 err.help("you may want to use --document-private-items");
1021 } else if flag == "plugins" || flag == "plugin-path" {
1022 err.warn("see CVE-2018-1000622");
1023 }
1024
1025 err.emit();
1026 }
1027 }
1028}
1029
1030fn parse_extern_html_roots(
1034 matches: &getopts::Matches,
1035) -> Result<BTreeMap<String, String>, &'static str> {
1036 let mut externs = BTreeMap::new();
1037 for arg in &matches.opt_strs("extern-html-root-url") {
1038 let (name, url) =
1039 arg.split_once('=').ok_or("--extern-html-root-url must be of the form name=url")?;
1040 externs.insert(name.to_string(), url.to_string());
1041 }
1042 Ok(externs)
1043}
1044
1045#[derive(Clone, Debug)]
1050pub(crate) struct PathToParts(pub(crate) PathBuf);
1051
1052impl PathToParts {
1053 fn from_flag(path: String) -> Result<PathToParts, String> {
1054 let path = PathBuf::from(path);
1055 if path.exists() && !path.is_dir() {
1057 Err(format!(
1058 "--parts-out-dir and --include-parts-dir expect directories, found: {}",
1059 path.display(),
1060 ))
1061 } else {
1062 Ok(PathToParts(path))
1064 }
1065 }
1066}
1067
1068fn parse_include_parts_dir(m: &getopts::Matches) -> Result<Vec<PathToParts>, String> {
1070 let mut ret = Vec::new();
1071 for p in m.opt_strs("include-parts-dir") {
1072 let p = PathToParts::from_flag(p)?;
1073 if !p.0.is_dir() {
1075 return Err(format!(
1076 "--include-parts-dir expected {} to be a directory",
1077 p.0.display()
1078 ));
1079 }
1080 ret.push(p);
1081 }
1082 Ok(ret)
1083}
1084
1085#[derive(Debug, Clone)]
1087pub(crate) struct ShouldMerge {
1088 pub(crate) read_rendered_cci: bool,
1090 pub(crate) write_rendered_cci: bool,
1092}
1093
1094fn parse_merge(m: &getopts::Matches) -> Result<ShouldMerge, &'static str> {
1097 match m.opt_str("merge").as_deref() {
1098 None => Ok(ShouldMerge { read_rendered_cci: true, write_rendered_cci: true }),
1100 Some("none") if m.opt_present("include-parts-dir") => {
1101 Err("--include-parts-dir not allowed if --merge=none")
1102 }
1103 Some("none") => Ok(ShouldMerge { read_rendered_cci: false, write_rendered_cci: false }),
1104 Some("shared") if m.opt_present("parts-out-dir") || m.opt_present("include-parts-dir") => {
1105 Err("--parts-out-dir and --include-parts-dir not allowed if --merge=shared")
1106 }
1107 Some("shared") => Ok(ShouldMerge { read_rendered_cci: true, write_rendered_cci: true }),
1108 Some("finalize") if m.opt_present("parts-out-dir") => {
1109 Err("--parts-out-dir not allowed if --merge=finalize")
1110 }
1111 Some("finalize") => Ok(ShouldMerge { read_rendered_cci: false, write_rendered_cci: true }),
1112 Some(_) => Err("argument to --merge must be `none`, `shared`, or `finalize`"),
1113 }
1114}
1115
1116fn parse_merge_doctests(
1117 m: &getopts::Matches,
1118 edition: Edition,
1119 dcx: DiagCtxtHandle<'_>,
1120) -> MergeDoctests {
1121 match m.opt_str("merge-doctests").as_deref() {
1122 Some("y") | Some("yes") | Some("on") | Some("true") => MergeDoctests::Always,
1123 Some("n") | Some("no") | Some("off") | Some("false") => MergeDoctests::Never,
1124 Some("auto") => MergeDoctests::Auto,
1125 None if edition < Edition::Edition2024 => MergeDoctests::Never,
1126 None => MergeDoctests::Auto,
1127 Some(_) => {
1128 dcx.fatal("argument to --merge-doctests must be a boolean (true/false) or 'auto'")
1129 }
1130 }
1131}