Skip to content

Commit a68f2c2

Browse files
committed
Pass --emit=invocation-specific for crate builds
This avoids copying shared files for crates other than the empty library. This a) Allows removing `copy_doc_dir`, which allows crates to add their own custom CSS (with `--theme` or otherwise), and b) Avoids unnecessary disk IO. Here are the files generated for a build of regex 1.4.1: ``` cratesfyi=# select path from files where path like '%regex/1.4.1%' and path not like '%regex/1.4.1/%/%'; path ----------------------------------------------------------------------- sources/regex/1.4.1/PERFORMANCE.md rustdoc/regex/1.4.1/.lock sources/regex/1.4.1/Cargo.toml sources/regex/1.4.1/.cargo_vcs_info.json sources/regex/1.4.1/LICENSE-APACHE rustdoc/regex/1.4.1/source-files-20210402-1.53.0-nightly-138fd56cf.js rustdoc/regex/1.4.1/settings.html rustdoc/regex/1.4.1/search-index-20210402-1.53.0-nightly-138fd56cf.js rustdoc/regex/1.4.1/crates-20210402-1.53.0-nightly-138fd56cf.js sources/regex/1.4.1/CHANGELOG.md sources/regex/1.4.1/LICENSE-MIT sources/regex/1.4.1/rustfmt.toml sources/regex/1.4.1/HACKING.md sources/regex/1.4.1/test sources/regex/1.4.1/Cargo.toml.orig sources/regex/1.4.1/UNICODE.md sources/regex/1.4.1/.gitignore sources/regex/1.4.1/Cargo.lock sources/regex/1.4.1/README.md (19 rows) ``` Note in particular that search-index.js and crates.js are included, but not shared files like `storage.js` or `rustdoc.css`.
1 parent 11dfd38 commit a68f2c2

File tree

3 files changed

+19
-57
lines changed

3 files changed

+19
-57
lines changed

src/docbuilder/rustwide_builder.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::docbuilder::{crates::crates_from_path, Limits};
77
use crate::error::Result;
88
use crate::index::api::ReleaseData;
99
use crate::storage::CompressionAlgorithms;
10-
use crate::utils::{copy_doc_dir, parse_rustc_version, CargoMetadata, GithubUpdater};
10+
use crate::utils::{copy_dir_all, parse_rustc_version, CargoMetadata, GithubUpdater};
1111
use crate::{db::blacklist::is_blacklisted, utils::MetadataPackage};
1212
use crate::{Config, Context, Index, Metrics, Storage};
1313
use docsrs_metadata::{Metadata, DEFAULT_TARGETS, HOST_TARGET};
@@ -187,17 +187,7 @@ impl RustwideBuilder {
187187
.run(|build| {
188188
let metadata = Metadata::from_crate_root(&build.host_source_dir())?;
189189

190-
let rustdoc_flags = vec![
191-
"--emit=unversioned-shared-resources,toolchain-shared-resources".to_string(),
192-
];
193-
let res = self.execute_build(
194-
HOST_TARGET,
195-
true,
196-
build,
197-
&limits,
198-
&metadata,
199-
rustdoc_flags,
200-
)?;
190+
let res = self.execute_build(HOST_TARGET, true, build, &limits, &metadata, true)?;
201191
if !res.result.successful {
202192
failure::bail!("failed to build dummy crate for {}", self.rustc_version);
203193
}
@@ -207,7 +197,7 @@ impl RustwideBuilder {
207197
let dest = tempfile::Builder::new()
208198
.prefix("essential-files")
209199
.tempdir()?;
210-
crate::utils::copy_dir_all(source, &dest, |_| true)?;
200+
copy_dir_all(source, &dest)?;
211201
add_path_into_database(&self.storage, "", &dest)?;
212202
conn.query(
213203
"INSERT INTO config (name, value) VALUES ('rustc_version', $1) \
@@ -317,7 +307,7 @@ impl RustwideBuilder {
317307

318308
// Perform an initial build
319309
let res =
320-
self.execute_build(default_target, true, &build, &limits, &metadata, vec![])?;
310+
self.execute_build(default_target, true, &build, &limits, &metadata, false)?;
321311
if res.result.successful {
322312
if let Some(name) = res.cargo_metadata.root().library_name() {
323313
let host_target = build.host_target_dir();
@@ -423,7 +413,7 @@ impl RustwideBuilder {
423413
successful_targets: &mut Vec<String>,
424414
metadata: &Metadata,
425415
) -> Result<()> {
426-
let target_res = self.execute_build(target, false, build, limits, metadata, Vec::new())?;
416+
let target_res = self.execute_build(target, false, build, limits, metadata, false)?;
427417
if target_res.result.successful {
428418
// Cargo is not giving any error and not generating documentation of some crates
429419
// when we use a target compile options. Check documentation exists before
@@ -499,11 +489,17 @@ impl RustwideBuilder {
499489
build: &Build,
500490
limits: &Limits,
501491
metadata: &Metadata,
502-
mut rustdoc_flags: Vec<String>,
492+
create_essential_files: bool,
503493
) -> Result<FullBuildResult> {
504494
let cargo_metadata =
505495
CargoMetadata::load(&self.workspace, &self.toolchain, &build.host_source_dir())?;
506496

497+
let mut rustdoc_flags = vec![if create_essential_files {
498+
"--emit=unversioned-shared-resources,toolchain-shared-resources"
499+
} else {
500+
"--emit=invocation-specific"
501+
}
502+
.to_string()];
507503
rustdoc_flags.extend(vec![
508504
"--resource-suffix".to_string(),
509505
format!("-{}", parse_rustc_version(&self.rustc_version)?),
@@ -620,7 +616,7 @@ impl RustwideBuilder {
620616
}
621617

622618
info!("{} {}", source.display(), dest.display());
623-
copy_doc_dir(source, dest)
619+
copy_dir_all(source, dest).map_err(Into::into)
624620
}
625621

626622
fn upload_docs(

src/utils/copy.rs

+5-39
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,17 @@
1-
use std::ffi::OsStr;
21
use std::fs;
32
use std::io;
43
use std::path::Path;
54

6-
use crate::error::Result;
7-
use regex::Regex;
8-
9-
/// Copies documentation from a crate's target directory to destination.
10-
///
11-
/// Target directory must have doc directory.
12-
///
13-
/// This function is designed to avoid file duplications.
14-
pub(crate) fn copy_doc_dir(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> Result<()> {
15-
// Avoid copying common files
16-
let dup_regex = Regex::new(
17-
r"(\.lock|\.txt|\.woff|\.svg|\.css|main-.*\.css|main-.*\.js|normalize-.*\.js|rustdoc-.*\.css|storage-.*\.js|theme-.*\.js)$")
18-
.unwrap();
19-
20-
copy_dir_all(source, destination, |filename| {
21-
dup_regex.is_match(filename.to_str().unwrap())
22-
})
23-
.map_err(Into::into)
24-
}
25-
26-
pub(crate) fn copy_dir_all(
27-
src: impl AsRef<Path>,
28-
dst: impl AsRef<Path>,
29-
should_copy: impl Copy + Fn(&OsStr) -> bool,
30-
) -> io::Result<()> {
5+
/// cp -r src dst
6+
pub(crate) fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
317
let dst = dst.as_ref();
328
fs::create_dir_all(dst)?;
339
for entry in fs::read_dir(src)? {
3410
let entry = entry?;
3511
let filename = entry.file_name();
3612
if entry.file_type()?.is_dir() {
37-
copy_dir_all(entry.path(), dst.join(filename), should_copy)?;
38-
} else if should_copy(&filename) {
13+
copy_dir_all(entry.path(), dst.join(filename))?;
14+
} else {
3915
fs::copy(entry.path(), dst.join(filename))?;
4016
}
4117
}
@@ -62,21 +38,11 @@ mod test {
6238
fs::create_dir(doc.join("inner")).unwrap();
6339

6440
fs::write(doc.join("index.html"), "<html>spooky</html>").unwrap();
65-
fs::write(doc.join("index.txt"), "spooky").unwrap();
6641
fs::write(doc.join("inner").join("index.html"), "<html>spooky</html>").unwrap();
67-
fs::write(doc.join("inner").join("index.txt"), "spooky").unwrap();
68-
fs::write(doc.join("inner").join("important.svg"), "<svg></svg>").unwrap();
6942

7043
// lets try to copy a src directory to tempdir
71-
copy_doc_dir(source.path().join("doc"), destination.path()).unwrap();
44+
copy_dir_all(source.path().join("doc"), destination.path()).unwrap();
7245
assert!(destination.path().join("index.html").exists());
73-
assert!(!destination.path().join("index.txt").exists());
7446
assert!(destination.path().join("inner").join("index.html").exists());
75-
assert!(!destination.path().join("inner").join("index.txt").exists());
76-
assert!(!destination
77-
.path()
78-
.join("inner")
79-
.join("important.svg")
80-
.exists());
8147
}
8248
}

src/utils/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Various utilities for docs.rs
22
33
pub(crate) use self::cargo_metadata::{CargoMetadata, Package as MetadataPackage};
4-
pub(crate) use self::copy::{copy_dir_all, copy_doc_dir};
4+
pub(crate) use self::copy::copy_dir_all;
55
pub use self::daemon::start_daemon;
66
pub use self::github_updater::GithubUpdater;
77
pub(crate) use self::html::rewrite_lol;

0 commit comments

Comments
 (0)