Skip to content

Commit 31f5320

Browse files
authored
Rollup merge of #83478 - jyn514:fine-grained-files, r=Mark-Simulacrum
rustdoc: Add unstable option to only emit shared/crate-specific files The intended use case is for docs.rs, which can now copy exactly the files it cares about, rather than having to guess based on whether they have a resource suffix or not. In particular, some files have a resource suffix but cannot be shared between crates: rust-lang/docs.rs#1312 (comment) The end goal is to fix rust-lang/docs.rs#1327 by reverting rust-lang/docs.rs#1324. This obsoletes `--print=unversioned-files`, which I plan to remove as soon as docs.rs stops using it. I recommend reviewing this one commit at a time. r? ``@GuillaumeGomez`` cc ``@Nemo157`` ``@pietroalbini``
2 parents 48ebad5 + 413938d commit 31f5320

File tree

9 files changed

+281
-134
lines changed

9 files changed

+281
-134
lines changed

src/librustdoc/config.rs

+43
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::convert::TryFrom;
33
use std::ffi::OsStr;
44
use std::fmt;
55
use std::path::PathBuf;
6+
use std::str::FromStr;
67

78
use rustc_data_structures::fx::FxHashMap;
89
use rustc_session::config::{self, parse_crate_types_from_list, parse_externs, CrateType};
@@ -266,6 +267,34 @@ crate struct RenderOptions {
266267
/// If `true`, generate a JSON file in the crate folder instead of HTML redirection files.
267268
crate generate_redirect_map: bool,
268269
crate unstable_features: rustc_feature::UnstableFeatures,
270+
crate emit: Vec<EmitType>,
271+
}
272+
273+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
274+
crate enum EmitType {
275+
Unversioned,
276+
Toolchain,
277+
InvocationSpecific,
278+
}
279+
280+
impl FromStr for EmitType {
281+
type Err = ();
282+
283+
fn from_str(s: &str) -> Result<Self, Self::Err> {
284+
use EmitType::*;
285+
match s {
286+
"unversioned-shared-resources" => Ok(Unversioned),
287+
"toolchain-shared-resources" => Ok(Toolchain),
288+
"invocation-specific" => Ok(InvocationSpecific),
289+
_ => Err(()),
290+
}
291+
}
292+
}
293+
294+
impl RenderOptions {
295+
crate fn should_emit_crate(&self) -> bool {
296+
self.emit.is_empty() || self.emit.contains(&EmitType::InvocationSpecific)
297+
}
269298
}
270299

271300
impl Options {
@@ -334,6 +363,19 @@ impl Options {
334363
// check for deprecated options
335364
check_deprecated_options(&matches, &diag);
336365

366+
let mut emit = Vec::new();
367+
for list in matches.opt_strs("emit") {
368+
for kind in list.split(',') {
369+
match kind.parse() {
370+
Ok(kind) => emit.push(kind),
371+
Err(()) => {
372+
diag.err(&format!("unrecognized emission type: {}", kind));
373+
return Err(1);
374+
}
375+
}
376+
}
377+
}
378+
337379
let to_check = matches.opt_strs("check-theme");
338380
if !to_check.is_empty() {
339381
let paths = theme::load_css_paths(static_files::themes::LIGHT.as_bytes());
@@ -641,6 +683,7 @@ impl Options {
641683
unstable_features: rustc_feature::UnstableFeatures::from_environment(
642684
crate_name.as_deref(),
643685
),
686+
emit,
644687
},
645688
crate_name,
646689
output_format,

src/librustdoc/formats/renderer.rs

+5
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,15 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
6363
) -> Result<(), Error> {
6464
let prof = &tcx.sess.prof;
6565

66+
let emit_crate = options.should_emit_crate();
6667
let (mut format_renderer, krate) = prof
6768
.extra_verbose_generic_activity("create_renderer", T::descr())
6869
.run(|| T::init(krate, options, edition, cache, tcx))?;
6970

71+
if !emit_crate {
72+
return Ok(());
73+
}
74+
7075
// Render the crate documentation
7176
let crate_name = krate.name;
7277
let mut work = vec![(format_renderer.make_child_renderer(), krate.module)];

src/librustdoc/html/render/context.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,6 @@ crate struct Context<'tcx> {
7979
rustc_data_structures::static_assert_size!(Context<'_>, 152);
8080

8181
impl<'tcx> Context<'tcx> {
82-
pub(super) fn path(&self, filename: &str) -> PathBuf {
83-
// We use splitn vs Path::extension here because we might get a filename
84-
// like `style.min.css` and we want to process that into
85-
// `style-suffix.min.css`. Path::extension would just return `css`
86-
// which would result in `style.min-suffix.css` which isn't what we
87-
// want.
88-
let (base, ext) = filename.split_once('.').unwrap();
89-
let filename = format!("{}{}.{}", base, self.shared.resource_suffix, ext);
90-
self.dst.join(&filename)
91-
}
92-
9382
pub(super) fn tcx(&self) -> TyCtxt<'tcx> {
9483
self.shared.tcx
9584
}
@@ -301,6 +290,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
301290
) -> Result<(Self, clean::Crate), Error> {
302291
// need to save a copy of the options for rendering the index page
303292
let md_opts = options.clone();
293+
let emit_crate = options.should_emit_crate();
304294
let RenderOptions {
305295
output,
306296
external_html,
@@ -406,7 +396,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
406396

407397
let dst = output;
408398
scx.ensure_dir(&dst)?;
409-
krate = sources::render(&dst, &mut scx, krate)?;
399+
if emit_crate {
400+
krate = sources::render(&dst, &mut scx, krate)?;
401+
}
410402

411403
// Build our search index
412404
let index = build_index(&krate, &mut cache, tcx);
@@ -489,7 +481,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
489481
|buf: &mut Buffer| all.print(buf),
490482
&self.shared.style_files,
491483
);
492-
self.shared.fs.write(&final_file, v.as_bytes())?;
484+
self.shared.fs.write(final_file, v.as_bytes())?;
493485

494486
// Generating settings page.
495487
page.title = "Rustdoc settings";

0 commit comments

Comments
 (0)