Skip to content

Commit 98eb2e3

Browse files
Add rustdoc support for --emit=dep-info[=path]
1 parent 745297e commit 98eb2e3

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

src/librustdoc/config.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -315,23 +315,30 @@ pub(crate) enum ModuleSorting {
315315
Alphabetical,
316316
}
317317

318-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
318+
#[derive(Clone, Debug, PartialEq, Eq)]
319319
pub(crate) enum EmitType {
320320
Unversioned,
321321
Toolchain,
322322
InvocationSpecific,
323+
DepInfo(Option<PathBuf>),
323324
}
324325

325326
impl FromStr for EmitType {
326327
type Err = ();
327328

328329
fn from_str(s: &str) -> Result<Self, Self::Err> {
329-
use EmitType::*;
330330
match s {
331-
"unversioned-shared-resources" => Ok(Unversioned),
332-
"toolchain-shared-resources" => Ok(Toolchain),
333-
"invocation-specific" => Ok(InvocationSpecific),
334-
_ => Err(()),
331+
"unversioned-shared-resources" => Ok(Self::Unversioned),
332+
"toolchain-shared-resources" => Ok(Self::Toolchain),
333+
"invocation-specific" => Ok(Self::InvocationSpecific),
334+
"dep-info" => Ok(Self::DepInfo(None)),
335+
option => {
336+
if let Some(file) = option.strip_prefix("dep-info=") {
337+
Ok(Self::DepInfo(Some(Path::new(file).into())))
338+
} else {
339+
Err(())
340+
}
341+
}
335342
}
336343
}
337344
}
@@ -340,6 +347,15 @@ impl RenderOptions {
340347
pub(crate) fn should_emit_crate(&self) -> bool {
341348
self.emit.is_empty() || self.emit.contains(&EmitType::InvocationSpecific)
342349
}
350+
351+
pub(crate) fn dep_info(&self) -> Option<Option<&Path>> {
352+
for emit in &self.emit {
353+
if let EmitType::DepInfo(file) = emit {
354+
return Some(file.as_deref());
355+
}
356+
}
357+
None
358+
}
343359
}
344360

345361
/// Create the input (string or file path)

src/librustdoc/core.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ use rustc_hir::def::Res;
1515
use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LocalDefId};
1616
use rustc_hir::intravisit::{self, Visitor};
1717
use rustc_hir::{HirId, Path};
18-
use rustc_interface::interface;
1918
use rustc_lint::{MissingDoc, late_lint_mod};
2019
use rustc_middle::hir::nested_filter;
2120
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
22-
use rustc_session::config::{self, CrateType, ErrorOutputType, Input, ResolveDocLinks};
21+
use rustc_session::config::{
22+
self, CrateType, ErrorOutputType, Input, OutFileName, OutputType, OutputTypes, ResolveDocLinks,
23+
};
2324
pub(crate) use rustc_session::config::{Options, UnstableOptions};
2425
use rustc_session::{Session, lint};
2526
use rustc_span::source_map;
@@ -219,7 +220,7 @@ pub(crate) fn create_config(
219220
remap_path_prefix,
220221
..
221222
}: RustdocOptions,
222-
RenderOptions { document_private, .. }: &RenderOptions,
223+
render_options: &RenderOptions,
223224
) -> rustc_interface::Config {
224225
// Add the doc cfg into the doc build.
225226
cfgs.push("doc".to_string());
@@ -245,8 +246,11 @@ pub(crate) fn create_config(
245246

246247
let crate_types =
247248
if proc_macro_crate { vec![CrateType::ProcMacro] } else { vec![CrateType::Rlib] };
248-
let resolve_doc_links =
249-
if *document_private { ResolveDocLinks::All } else { ResolveDocLinks::Exported };
249+
let resolve_doc_links = if render_options.document_private {
250+
ResolveDocLinks::All
251+
} else {
252+
ResolveDocLinks::Exported
253+
};
250254
let test = scrape_examples_options.map(|opts| opts.scrape_tests).unwrap_or(false);
251255
// plays with error output here!
252256
let sessopts = config::Options {
@@ -269,10 +273,18 @@ pub(crate) fn create_config(
269273
crate_name,
270274
test,
271275
remap_path_prefix,
276+
output_types: if let Some(file) = render_options.dep_info() {
277+
OutputTypes::new(&[(
278+
OutputType::DepInfo,
279+
file.map(|f| OutFileName::Real(f.to_path_buf())),
280+
)])
281+
} else {
282+
OutputTypes::new(&[])
283+
},
272284
..Options::default()
273285
};
274286

275-
interface::Config {
287+
rustc_interface::Config {
276288
opts: sessopts,
277289
crate_cfg: cfgs,
278290
crate_check_cfg: check_cfgs,

src/librustdoc/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ fn opts() -> Vec<RustcOptGroup> {
561561
"",
562562
"emit",
563563
"Comma separated list of types of output for rustdoc to emit",
564-
"[unversioned-shared-resources,toolchain-shared-resources,invocation-specific]",
564+
"[unversioned-shared-resources,toolchain-shared-resources,invocation-specific,dep-info]",
565565
),
566566
opt(Unstable, FlagMulti, "", "no-run", "Compile doctests without running them", ""),
567567
opt(
@@ -890,7 +890,13 @@ fn main_args(early_dcx: &mut EarlyDiagCtxt, at_args: &[String]) {
890890
// if we ran coverage, bail early, we don't need to also generate docs at this point
891891
// (also we didn't load in any of the useful passes)
892892
return;
893-
} else if run_check {
893+
}
894+
895+
if render_opts.dep_info().is_some() {
896+
rustc_interface::passes::write_dep_info(tcx);
897+
}
898+
899+
if run_check {
894900
// Since we're in "check" mode, no need to generate anything beyond this point.
895901
return;
896902
}

0 commit comments

Comments
 (0)