forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#131951 - notriddle:notriddle/sha256-compile-t…
…ime, r=GuillaumeGomez rustdoc: hash assets at rustdoc build time Since sha256 is slow enough to show up on small benchmarks, we can save time by embedding the hash in the executable. Addresses rust-lang#131934 (comment)
- Loading branch information
Showing
3 changed files
with
58 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
fn main() { | ||
// generate sha256 files | ||
// this avoids having to perform hashing at runtime | ||
let files = &[ | ||
"static/css/rustdoc.css", | ||
"static/css/noscript.css", | ||
"static/css/normalize.css", | ||
"static/js/main.js", | ||
"static/js/search.js", | ||
"static/js/settings.js", | ||
"static/js/src-script.js", | ||
"static/js/storage.js", | ||
"static/js/scrape-examples.js", | ||
"static/COPYRIGHT.txt", | ||
"static/LICENSE-APACHE.txt", | ||
"static/LICENSE-MIT.txt", | ||
"static/images/rust-logo.svg", | ||
"static/images/favicon.svg", | ||
"static/images/favicon-32x32.png", | ||
"static/fonts/FiraSans-Regular.woff2", | ||
"static/fonts/FiraSans-Medium.woff2", | ||
"static/fonts/FiraSans-LICENSE.txt", | ||
"static/fonts/SourceSerif4-Regular.ttf.woff2", | ||
"static/fonts/SourceSerif4-Bold.ttf.woff2", | ||
"static/fonts/SourceSerif4-It.ttf.woff2", | ||
"static/fonts/SourceSerif4-LICENSE.md", | ||
"static/fonts/SourceCodePro-Regular.ttf.woff2", | ||
"static/fonts/SourceCodePro-Semibold.ttf.woff2", | ||
"static/fonts/SourceCodePro-It.ttf.woff2", | ||
"static/fonts/SourceCodePro-LICENSE.txt", | ||
"static/fonts/NanumBarunGothic.ttf.woff2", | ||
"static/fonts/NanumBarunGothic-LICENSE.txt", | ||
]; | ||
let out_dir = std::env::var("OUT_DIR").expect("standard Cargo environment variable"); | ||
for path in files { | ||
let inpath = format!("html/{path}"); | ||
println!("cargo::rerun-if-changed={inpath}"); | ||
let bytes = std::fs::read(inpath).expect("static path exists"); | ||
use sha2::Digest; | ||
let bytes = sha2::Sha256::digest(bytes); | ||
let mut digest = format!("-{bytes:x}"); | ||
digest.truncate(9); | ||
let outpath = std::path::PathBuf::from(format!("{out_dir}/{path}.sha256")); | ||
std::fs::create_dir_all(outpath.parent().expect("all file paths are in a directory")) | ||
.expect("should be able to write to out_dir"); | ||
std::fs::write(&outpath, digest.as_bytes()).expect("write to out_dir"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters