Skip to content

Commit f77ebd4

Browse files
committed
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.
1 parent 7c89cc4 commit f77ebd4

File tree

7 files changed

+129
-12
lines changed

7 files changed

+129
-12
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+
CrateSpecific,
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+
"crate-specific" => Ok(CrateSpecific),
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::CrateSpecific)
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
@@ -58,10 +58,15 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
5858
) -> Result<(), Error> {
5959
let prof = &tcx.sess.prof;
6060

61+
let emit_crate = options.should_emit_crate();
6162
let (mut format_renderer, krate) = prof
6263
.extra_verbose_generic_activity("create_renderer", T::descr())
6364
.run(|| T::init(krate, options, edition, cache, tcx))?;
6465

66+
if !emit_crate {
67+
return Ok(());
68+
}
69+
6570
// Render the crate documentation
6671
let crate_name = krate.name;
6772
let mut work = vec![(format_renderer.make_child_renderer(), krate.module)];

src/librustdoc/html/render/context.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
288288
) -> Result<(Self, clean::Crate), Error> {
289289
// need to save a copy of the options for rendering the index page
290290
let md_opts = options.clone();
291+
let emit_crate = options.should_emit_crate();
291292
let RenderOptions {
292293
output,
293294
external_html,
@@ -393,7 +394,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
393394

394395
let dst = output;
395396
scx.ensure_dir(&dst)?;
396-
krate = sources::render(&dst, &mut scx, krate)?;
397+
if emit_crate {
398+
krate = sources::render(&dst, &mut scx, krate)?;
399+
}
397400

398401
// Build our search index
399402
let index = build_index(&krate, &mut cache, tcx);

src/librustdoc/html/render/write_shared.rs

+36-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use serde::Serialize;
1313

1414
use super::{collect_paths_for_type, ensure_trailing_slash, Context, BASIC_KEYWORDS};
1515
use crate::clean::Crate;
16-
use crate::config::RenderOptions;
16+
use crate::config::{EmitType, RenderOptions};
1717
use crate::docfs::PathError;
1818
use crate::error::Error;
1919
use crate::formats::FormatRenderer;
@@ -72,6 +72,18 @@ impl SharedResource<'_> {
7272
SharedResource::CrateSpecific { basename } => cx.suffix_path(basename),
7373
}
7474
}
75+
76+
fn should_emit(&self, emit: &[EmitType]) -> bool {
77+
if emit.is_empty() {
78+
return true;
79+
}
80+
let kind = match self {
81+
SharedResource::Unversioned { .. } => EmitType::Unversioned,
82+
SharedResource::ToolchainSpecific { .. } => EmitType::Toolchain,
83+
SharedResource::CrateSpecific { .. } => EmitType::CrateSpecific,
84+
};
85+
emit.contains(&kind)
86+
}
7587
}
7688

7789
impl Context<'_> {
@@ -86,16 +98,25 @@ impl Context<'_> {
8698
self.dst.join(&filename)
8799
}
88100

89-
fn write_shared<C: AsRef<[u8]>>(&self, resource: SharedResource<'_>, contents: C) -> Result<(), Error>
90-
{
91-
self.shared.fs.write(resource.path(self), contents)
101+
fn write_shared<C: AsRef<[u8]>>(
102+
&self,
103+
resource: SharedResource<'_>,
104+
contents: C,
105+
emit: &[EmitType],
106+
) -> Result<(), Error> {
107+
if resource.should_emit(emit) {
108+
self.shared.fs.write(resource.path(self), contents)
109+
} else {
110+
Ok(())
111+
}
92112
}
93113

94114
fn write_minify(
95115
&self,
96116
resource: SharedResource<'_>,
97117
contents: &str,
98118
minify: bool,
119+
emit: &[EmitType],
99120
) -> Result<(), Error> {
100121
let tmp;
101122
let contents = if minify {
@@ -111,7 +132,7 @@ impl Context<'_> {
111132
contents.as_bytes()
112133
};
113134

114-
self.write_shared(resource, contents)
135+
self.write_shared(resource, contents, emit)
115136
}
116137
}
117138

@@ -133,10 +154,14 @@ pub(super) fn write_shared(
133154
SharedResource::ToolchainSpecific { basename: p },
134155
c,
135156
options.enable_minification,
157+
&options.emit,
136158
)
137159
};
138-
let write_toolchain =
139-
|p: &_, c: &_| cx.write_shared(SharedResource::ToolchainSpecific { basename: p }, c);
160+
let write_toolchain = |p: &_, c: &_| {
161+
cx.write_shared(SharedResource::ToolchainSpecific { basename: p }, c, &options.emit)
162+
};
163+
let write_crate =
164+
|p, c: &_| cx.write_shared(SharedResource::CrateSpecific { basename: p }, c, &options.emit);
140165

141166
// Add all the static files. These may already exist, but we just
142167
// overwrite them anyway to make sure that they're fresh and up-to-date.
@@ -214,7 +239,7 @@ pub(super) fn write_shared(
214239
}
215240
write_minify("normalize.css", static_files::NORMALIZE_CSS)?;
216241
for (name, contents) in &*FILES_UNVERSIONED {
217-
cx.write_shared(SharedResource::Unversioned { name }, contents)?;
242+
cx.write_shared(SharedResource::Unversioned { name }, contents, &options.emit)?;
218243
}
219244

220245
fn collect(path: &Path, krate: &str, key: &str) -> io::Result<(Vec<String>, Vec<String>)> {
@@ -354,7 +379,7 @@ pub(super) fn write_shared(
354379
"var N = null;var sourcesIndex = {{}};\n{}\ncreateSourceSidebar();\n",
355380
all_sources.join("\n")
356381
);
357-
cx.write_shared(SharedResource::CrateSpecific { basename: "source-files.js" }, v)?;
382+
write_crate("source-files.js", &v)?;
358383
}
359384

360385
// Update the search index and crate list.
@@ -371,12 +396,12 @@ pub(super) fn write_shared(
371396
let mut v = String::from("var searchIndex = JSON.parse('{\\\n");
372397
v.push_str(&all_indexes.join(",\\\n"));
373398
v.push_str("\\\n}');\ninitSearch(searchIndex);");
374-
cx.write_shared(SharedResource::CrateSpecific { basename: "search-index.js" }, v)?;
399+
write_crate("search-index.js", &v)?;
375400
}
376401

377402
let crate_list =
378403
format!("window.ALL_CRATES = [{}];", krates.iter().map(|k| format!("\"{}\"", k)).join(","));
379-
cx.write_shared(SharedResource::CrateSpecific { basename: "crates.js" }, crate_list)?;
404+
write_crate("crates.js", &crate_list)?;
380405

381406
if options.enable_index_page {
382407
if let Some(index_page) = options.index_page.clone() {

src/librustdoc/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,14 @@ fn opts() -> Vec<RustcOptGroup> {
527527
unstable("print", |o| {
528528
o.optmulti("", "print", "Rustdoc information to print on stdout", "[unversioned-files]")
529529
}),
530+
unstable("emit", |o| {
531+
o.optmulti(
532+
"",
533+
"emit",
534+
"Comma separated list of types of output for rustdoc to emit",
535+
"[unversioned-shared-resources,toolchain-shared-resources,crate-specific]",
536+
)
537+
}),
530538
]
531539
}
532540

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
CRATE_ONLY = $(TMPDIR)/crate-only
4+
TOOLCHAIN_ONLY = $(TMPDIR)/toolchain-only
5+
ALL_SHARED = $(TMPDIR)/all-shared
6+
7+
all: crate-only toolchain-only all-shared
8+
9+
crate-only:
10+
$(RUSTDOC) -Z unstable-options --emit=crate-specific --output $(CRATE_ONLY) --resource-suffix=-xxx x.rs
11+
[ -e $(CRATE_ONLY)/search-index-xxx.js ]
12+
[ -e $(CRATE_ONLY)/settings.html ]
13+
[ -e $(CRATE_ONLY)/x/all.html ]
14+
[ -e $(CRATE_ONLY)/x/index.html ]
15+
! [ -e $(CRATE_ONLY)/storage-xxx.js ]
16+
! [ -e $(CRATE_ONLY)/SourceSerifPro-It.ttf.woff ]
17+
18+
toolchain-only:
19+
$(RUSTDOC) -Z unstable-options --emit=toolchain-shared-resources --output $(TOOLCHAIN_ONLY) --resource-suffix=-xxx x.rs
20+
[ -e $(TOOLCHAIN_ONLY)/storage-xxx.js ]
21+
! [ -e $(TOOLCHAIN_ONLY)/SourceSerifPro-It.ttf.woff ]
22+
! [ -e $(TOOLCHAIN_ONLY)/search-index-xxx.js ]
23+
! [ -e $(TOOLCHAIN_ONLY)/x/index.html ]
24+
25+
all-shared:
26+
$(RUSTDOC) -Z unstable-options --emit=toolchain-shared-resources,unversioned-shared-resources --output $(ALL_SHARED) --resource-suffix=-xxx x.rs
27+
[ -e $(ALL_SHARED)/storage-xxx.js ]
28+
[ -e $(ALL_SHARED)/SourceSerifPro-It.ttf.woff ]
29+
! [ -e $(ALL_SHARED)/search-index-xxx.js ]
30+
! [ -e $(ALL_SHARED)/settings.html ]
31+
! [ -e $(ALL_SHARED)/x ]
32+
! [ -e $(ALL_SHARED)/src ]
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// nothing to see here

0 commit comments

Comments
 (0)