2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-07-20 19:49:57 -04:00
|
|
|
mod op_fetch_asset;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2020-09-06 15:44:29 -04:00
|
|
|
use deno_core::JsRuntime;
|
2020-09-11 09:18:49 -04:00
|
|
|
use deno_core::RuntimeOptions;
|
2020-01-22 17:58:13 -05:00
|
|
|
use std::collections::HashMap;
|
2019-09-02 17:07:11 -04:00
|
|
|
use std::env;
|
2020-07-19 13:49:44 -04:00
|
|
|
use std::path::Path;
|
2019-09-02 17:07:11 -04:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
fn create_snapshot(
|
2020-09-06 15:44:29 -04:00
|
|
|
mut isolate: JsRuntime,
|
2020-07-19 13:49:44 -04:00
|
|
|
snapshot_path: &Path,
|
2020-09-09 08:23:57 -04:00
|
|
|
files: Vec<PathBuf>,
|
2020-07-19 13:49:44 -04:00
|
|
|
) {
|
2020-09-01 16:32:07 -04:00
|
|
|
deno_web::init(&mut isolate);
|
2020-09-18 09:20:55 -04:00
|
|
|
deno_fetch::init(&mut isolate);
|
2020-09-09 08:23:57 -04:00
|
|
|
// 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();
|
2020-07-19 13:49:44 -04:00
|
|
|
for file in files {
|
2020-09-09 08:23:57 -04:00
|
|
|
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();
|
2020-09-22 17:30:03 -04:00
|
|
|
isolate
|
|
|
|
.execute(
|
|
|
|
&("deno:".to_string() + &display_path_str.replace('\\', "/")),
|
|
|
|
&std::fs::read_to_string(&file).unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-01-30 03:32:20 -05:00
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
let snapshot = isolate.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());
|
|
|
|
}
|
2020-01-22 17:58:13 -05:00
|
|
|
|
2020-09-09 08:23:57 -04:00
|
|
|
fn create_runtime_snapshot(snapshot_path: &Path, files: Vec<PathBuf>) {
|
2020-09-11 09:18:49 -04:00
|
|
|
let isolate = JsRuntime::new(RuntimeOptions {
|
|
|
|
will_snapshot: true,
|
|
|
|
..Default::default()
|
|
|
|
});
|
2020-09-05 20:34:02 -04:00
|
|
|
create_snapshot(isolate, snapshot_path, files);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2020-02-25 09:14:27 -05:00
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
fn create_compiler_snapshot(
|
|
|
|
snapshot_path: &Path,
|
2020-09-09 08:23:57 -04:00
|
|
|
files: Vec<PathBuf>,
|
2020-07-19 13:49:44 -04:00
|
|
|
cwd: &Path,
|
|
|
|
) {
|
2020-01-22 17:58:13 -05:00
|
|
|
let mut custom_libs: HashMap<String, PathBuf> = HashMap::new();
|
2020-09-01 16:32:07 -04:00
|
|
|
custom_libs
|
|
|
|
.insert("lib.deno.web.d.ts".to_string(), deno_web::get_declaration());
|
2020-09-18 09:20:55 -04:00
|
|
|
custom_libs.insert(
|
|
|
|
"lib.deno.fetch.d.ts".to_string(),
|
|
|
|
deno_fetch::get_declaration(),
|
|
|
|
);
|
2020-01-22 17:58:13 -05:00
|
|
|
custom_libs.insert(
|
2020-01-29 12:54:23 -05:00
|
|
|
"lib.deno.window.d.ts".to_string(),
|
2020-07-22 12:03:46 -04:00
|
|
|
cwd.join("dts/lib.deno.window.d.ts"),
|
2020-01-22 17:58:13 -05:00
|
|
|
);
|
2020-01-24 14:15:01 -05:00
|
|
|
custom_libs.insert(
|
2020-01-29 12:54:23 -05:00
|
|
|
"lib.deno.worker.d.ts".to_string(),
|
2020-07-22 12:03:46 -04:00
|
|
|
cwd.join("dts/lib.deno.worker.d.ts"),
|
2020-01-29 12:54:23 -05:00
|
|
|
);
|
|
|
|
custom_libs.insert(
|
|
|
|
"lib.deno.shared_globals.d.ts".to_string(),
|
2020-07-22 12:03:46 -04:00
|
|
|
cwd.join("dts/lib.deno.shared_globals.d.ts"),
|
2020-01-29 12:54:23 -05:00
|
|
|
);
|
|
|
|
custom_libs.insert(
|
|
|
|
"lib.deno.ns.d.ts".to_string(),
|
2020-07-22 12:03:46 -04:00
|
|
|
cwd.join("dts/lib.deno.ns.d.ts"),
|
2020-01-24 14:15:01 -05:00
|
|
|
);
|
2020-04-30 11:23:40 -04:00
|
|
|
custom_libs.insert(
|
|
|
|
"lib.deno.unstable.d.ts".to_string(),
|
2020-07-22 12:03:46 -04:00
|
|
|
cwd.join("dts/lib.deno.unstable.d.ts"),
|
2020-04-30 11:23:40 -04:00
|
|
|
);
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-09-11 09:18:49 -04:00
|
|
|
let mut isolate = JsRuntime::new(RuntimeOptions {
|
|
|
|
will_snapshot: true,
|
|
|
|
..Default::default()
|
|
|
|
});
|
2020-09-10 09:57:45 -04:00
|
|
|
isolate.register_op(
|
2020-02-25 09:14:27 -05:00
|
|
|
"op_fetch_asset",
|
2020-07-20 19:49:57 -04:00
|
|
|
op_fetch_asset::op_fetch_asset(custom_libs),
|
2020-02-25 09:14:27 -05:00
|
|
|
);
|
2020-09-05 20:34:02 -04:00
|
|
|
create_snapshot(isolate, snapshot_path, files);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2020-07-20 19:49:57 -04:00
|
|
|
fn ts_version() -> String {
|
2020-08-24 11:52:05 -04:00
|
|
|
std::fs::read_to_string("tsc/00_typescript.js")
|
|
|
|
.unwrap()
|
|
|
|
.lines()
|
|
|
|
.find(|l| l.contains("ts.version = "))
|
|
|
|
.expect(
|
|
|
|
"Failed to find the pattern `ts.version = ` in typescript source code",
|
|
|
|
)
|
|
|
|
.chars()
|
|
|
|
.skip_while(|c| !char::is_numeric(*c))
|
|
|
|
.take_while(|c| *c != '"')
|
|
|
|
.collect::<String>()
|
2020-07-20 19:49:57 -04:00
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
fn main() {
|
|
|
|
// Don't build V8 if "cargo doc" is being run. This is to support docs.rs.
|
|
|
|
if env::var_os("RUSTDOCFLAGS").is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// To debug snapshot issues uncomment:
|
2020-07-20 19:49:57 -04:00
|
|
|
// op_fetch_asset::trace_serializer();
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2020-07-20 19:49:57 -04:00
|
|
|
println!("cargo:rustc-env=TS_VERSION={}", ts_version());
|
2020-08-07 10:55:02 -04:00
|
|
|
println!(
|
|
|
|
"cargo:rustc-env=DENO_WEB_LIB_PATH={}",
|
2020-09-01 16:32:07 -04:00
|
|
|
deno_web::get_declaration().display()
|
2020-08-07 10:55:02 -04:00
|
|
|
);
|
2020-09-18 09:20:55 -04:00
|
|
|
println!(
|
|
|
|
"cargo:rustc-env=DENO_FETCH_LIB_PATH={}",
|
|
|
|
deno_fetch::get_declaration().display()
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
println!(
|
|
|
|
"cargo:rustc-env=TARGET={}",
|
|
|
|
std::env::var("TARGET").unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
let o = PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
|
|
|
|
|
|
|
// Main snapshot
|
|
|
|
let runtime_snapshot_path = o.join("CLI_SNAPSHOT.bin");
|
|
|
|
let compiler_snapshot_path = o.join("COMPILER_SNAPSHOT.bin");
|
|
|
|
|
2020-09-01 16:32:07 -04:00
|
|
|
let js_files = get_js_files("rt");
|
2020-07-22 12:03:46 -04:00
|
|
|
create_runtime_snapshot(&runtime_snapshot_path, js_files);
|
|
|
|
|
|
|
|
let js_files = get_js_files("tsc");
|
|
|
|
create_compiler_snapshot(&compiler_snapshot_path, js_files, &c);
|
|
|
|
|
2020-07-22 13:19:37 -04:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
{
|
|
|
|
let mut res = winres::WindowsResource::new();
|
|
|
|
res.set_icon("deno.ico");
|
|
|
|
res.set_language(winapi::um::winnt::MAKELANGID(
|
|
|
|
winapi::um::winnt::LANG_ENGLISH,
|
|
|
|
winapi::um::winnt::SUBLANG_ENGLISH_US,
|
|
|
|
));
|
|
|
|
res.compile().unwrap();
|
|
|
|
}
|
2020-07-22 12:03:46 -04:00
|
|
|
}
|
|
|
|
|
2020-09-09 08:23:57 -04:00
|
|
|
fn get_js_files(d: &str) -> Vec<PathBuf> {
|
|
|
|
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
|
2020-07-22 12:03:46 -04:00
|
|
|
let mut js_files = std::fs::read_dir(d)
|
2020-07-19 13:49:44 -04:00
|
|
|
.unwrap()
|
|
|
|
.map(|dir_entry| {
|
|
|
|
let file = dir_entry.unwrap();
|
2020-09-09 08:23:57 -04:00
|
|
|
manifest_dir.join(file.path())
|
2020-07-19 13:49:44 -04:00
|
|
|
})
|
2020-09-09 08:23:57 -04:00
|
|
|
.filter(|path| path.extension().unwrap_or_default() == "js")
|
|
|
|
.collect::<Vec<PathBuf>>();
|
2020-07-19 13:49:44 -04:00
|
|
|
js_files.sort();
|
2020-07-22 12:03:46 -04:00
|
|
|
js_files
|
2019-09-02 17:07:11 -04:00
|
|
|
}
|