1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 00:59:24 -05:00

build: Factor out common code into core/build_util.rs (#8756)

This commit is contained in:
Ryan Dahl 2020-12-14 08:28:56 -05:00 committed by GitHub
parent b2bda57073
commit 502c77aad9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 100 deletions

View file

@ -1,5 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use deno_core::build_util::create_snapshot;
use deno_core::build_util::get_js_files;
use deno_core::error::custom_error; use deno_core::error::custom_error;
use deno_core::json_op_sync; use deno_core::json_op_sync;
use deno_core::serde_json; use deno_core::serde_json;
@ -13,35 +15,6 @@ use std::env;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
// TODO(bartlomieju): this module contains a lot of duplicated
// logic with `runtime/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(
&("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());
std::fs::write(&snapshot_path, snapshot_slice).unwrap();
println!("Snapshot written to: {} ", snapshot_path.display());
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct LoadArgs { struct LoadArgs {
/// The fully qualified specifier that should be loaded. /// The fully qualified specifier that should be loaded.
@ -258,7 +231,7 @@ fn main() {
// Main snapshot // Main snapshot
let compiler_snapshot_path = o.join("COMPILER_SNAPSHOT.bin"); let compiler_snapshot_path = o.join("COMPILER_SNAPSHOT.bin");
let js_files = get_js_files("tsc"); let js_files = get_js_files(&c.join("tsc"));
create_compiler_snapshot(&compiler_snapshot_path, js_files, &c); create_compiler_snapshot(&compiler_snapshot_path, js_files, &c);
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
@ -272,17 +245,3 @@ fn main() {
res.compile().unwrap(); res.compile().unwrap();
} }
} }
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
}

44
core/build_util.rs Normal file
View file

@ -0,0 +1,44 @@
//! Helper module used in cli/build.rs and runtime/build.rs
use crate::JsRuntime;
use std::path::Path;
use std::path::PathBuf;
pub 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(
&("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());
std::fs::write(&snapshot_path, snapshot_slice).unwrap();
println!("Snapshot written to: {} ", snapshot_path.display());
}
pub fn get_js_files(d: &Path) -> Vec<PathBuf> {
let mut js_files = std::fs::read_dir(d)
.unwrap()
.map(|dir_entry| {
let file = dir_entry.unwrap();
d.join(file.path())
})
.filter(|path| path.extension().unwrap_or_default() == "js")
.collect::<Vec<PathBuf>>();
js_files.sort();
js_files
}

View file

@ -8,6 +8,7 @@ extern crate log;
mod async_cancel; mod async_cancel;
mod async_cell; mod async_cell;
mod bindings; mod bindings;
pub mod build_util;
pub mod error; pub mod error;
mod flags; mod flags;
mod gotham_state; mod gotham_state;

View file

@ -1,51 +1,12 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use deno_core::build_util::create_snapshot;
use deno_core::build_util::get_js_files;
use deno_core::JsRuntime; use deno_core::JsRuntime;
use deno_core::RuntimeOptions; use deno_core::RuntimeOptions;
use std::env; use std::env;
use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
// 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>,
) {
deno_web::init(&mut js_runtime);
deno_fetch::init(&mut js_runtime);
deno_crypto::init(&mut js_runtime);
// 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(
&("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());
std::fs::write(&snapshot_path, snapshot_slice).unwrap();
println!("Snapshot written to: {} ", snapshot_path.display());
}
fn create_runtime_snapshot(snapshot_path: &Path, files: Vec<PathBuf>) {
let js_runtime = JsRuntime::new(RuntimeOptions {
will_snapshot: true,
..Default::default()
});
create_snapshot(js_runtime, snapshot_path, files);
}
fn main() { fn main() {
// Don't build V8 if "cargo doc" is being run. This is to support docs.rs. // Don't build V8 if "cargo doc" is being run. This is to support docs.rs.
if env::var_os("RUSTDOCFLAGS").is_some() { if env::var_os("RUSTDOCFLAGS").is_some() {
@ -62,20 +23,15 @@ fn main() {
// Main snapshot // Main snapshot
let runtime_snapshot_path = o.join("CLI_SNAPSHOT.bin"); let runtime_snapshot_path = o.join("CLI_SNAPSHOT.bin");
let js_files = get_js_files("rt"); let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
create_runtime_snapshot(&runtime_snapshot_path, js_files); let js_files = get_js_files(&c.join("rt"));
}
fn get_js_files(d: &str) -> Vec<PathBuf> { let mut js_runtime = JsRuntime::new(RuntimeOptions {
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); will_snapshot: true,
let mut js_files = std::fs::read_dir(d) ..Default::default()
.unwrap() });
.map(|dir_entry| { deno_web::init(&mut js_runtime);
let file = dir_entry.unwrap(); deno_fetch::init(&mut js_runtime);
manifest_dir.join(file.path()) deno_crypto::init(&mut js_runtime);
}) create_snapshot(js_runtime, &runtime_snapshot_path, js_files);
.filter(|path| path.extension().unwrap_or_default() == "js")
.collect::<Vec<PathBuf>>();
js_files.sort();
js_files
} }