summaryrefslogtreecommitdiff
path: root/runtime/build.rs
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/build.rs')
-rw-r--r--runtime/build.rs96
1 files changed, 18 insertions, 78 deletions
diff --git a/runtime/build.rs b/runtime/build.rs
index 3feca3e76..bab7745cc 100644
--- a/runtime/build.rs
+++ b/runtime/build.rs
@@ -9,63 +9,8 @@ use std::path::PathBuf;
mod not_docs {
use super::*;
use deno_cache::SqliteBackedCache;
+ use deno_core::snapshot_util::*;
use deno_core::Extension;
- use deno_core::JsRuntime;
- use deno_core::RuntimeOptions;
-
- // TODO(bartlomieju): this module contains a lot of duplicated
- // logic with `cli/build.rs`, factor out to `deno_core`.
- fn create_snapshot(
- mut js_runtime: JsRuntime,
- snapshot_path: &Path,
- files: Vec<PathBuf>,
- ) {
- // TODO(nayeemrmn): https://github.com/rust-lang/cargo/issues/3946 to get the
- // workspace root.
- let display_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
- for file in files {
- println!("cargo:rerun-if-changed={}", file.display());
- let display_path = file.strip_prefix(display_root).unwrap();
- let display_path_str = display_path.display().to_string();
- js_runtime
- .execute_script(
- &("deno:".to_string() + &display_path_str.replace('\\', "/")),
- &std::fs::read_to_string(&file).unwrap(),
- )
- .unwrap();
- }
-
- let snapshot = js_runtime.snapshot();
- let snapshot_slice: &[u8] = &snapshot;
- println!("Snapshot size: {}", snapshot_slice.len());
-
- let compressed_snapshot_with_size = {
- let mut vec = vec![];
-
- vec.extend_from_slice(
- &u32::try_from(snapshot.len())
- .expect("snapshot larger than 4gb")
- .to_le_bytes(),
- );
-
- lzzzz::lz4_hc::compress_to_vec(
- snapshot_slice,
- &mut vec,
- lzzzz::lz4_hc::CLEVEL_MAX,
- )
- .expect("snapshot compression failed");
-
- vec
- };
-
- println!(
- "Snapshot compressed size: {}",
- compressed_snapshot_with_size.len()
- );
-
- std::fs::write(snapshot_path, compressed_snapshot_with_size).unwrap();
- println!("Snapshot written to: {} ", snapshot_path.display());
- }
struct Permissions;
@@ -174,7 +119,7 @@ mod not_docs {
}
}
- fn create_runtime_snapshot(snapshot_path: &Path, files: Vec<PathBuf>) {
+ fn create_runtime_snapshot(snapshot_path: PathBuf, files: Vec<PathBuf>) {
let extensions: Vec<Extension> = vec![
deno_webidl::init(),
deno_console::init(),
@@ -205,31 +150,26 @@ mod not_docs {
deno_flash::init::<Permissions>(false), // No --unstable
];
- let js_runtime = JsRuntime::new(RuntimeOptions {
- will_snapshot: true,
+ create_snapshot(CreateSnapshotOptions {
+ cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"),
+ snapshot_path,
+ startup_snapshot: None,
extensions,
- ..Default::default()
+ additional_files: files,
+ compression_cb: Some(Box::new(|vec, snapshot_slice| {
+ lzzzz::lz4_hc::compress_to_vec(
+ snapshot_slice,
+ vec,
+ lzzzz::lz4_hc::CLEVEL_MAX,
+ )
+ .expect("snapshot compression failed");
+ })),
});
- create_snapshot(js_runtime, snapshot_path, files);
- }
-
- fn get_js_files(d: &str) -> Vec<PathBuf> {
- let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
- let mut js_files = std::fs::read_dir(d)
- .unwrap()
- .map(|dir_entry| {
- let file = dir_entry.unwrap();
- manifest_dir.join(file.path())
- })
- .filter(|path| path.extension().unwrap_or_default() == "js")
- .collect::<Vec<PathBuf>>();
- js_files.sort();
- js_files
}
pub fn build_snapshot(runtime_snapshot_path: PathBuf) {
- let js_files = get_js_files("js");
- create_runtime_snapshot(&runtime_snapshot_path, js_files);
+ let js_files = get_js_files(env!("CARGO_MANIFEST_DIR"), "js");
+ create_runtime_snapshot(runtime_snapshot_path, js_files);
}
}
@@ -242,7 +182,7 @@ fn main() {
let o = PathBuf::from(env::var_os("OUT_DIR").unwrap());
// Main snapshot
- let runtime_snapshot_path = o.join("CLI_SNAPSHOT.bin");
+ let runtime_snapshot_path = o.join("RUNTIME_SNAPSHOT.bin");
// If we're building on docs.rs we just create
// and empty snapshot file and return, because `rusty_v8`