2020-01-02 20:13:47 +00:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-07-20 23:49:57 +00:00
|
|
|
//
|
|
|
|
mod op_fetch_asset;
|
2020-07-19 17:49:44 +00:00
|
|
|
|
|
|
|
use deno_core::js_check;
|
2020-04-23 09:51:07 +00:00
|
|
|
use deno_core::CoreIsolate;
|
2020-01-22 22:58:13 +00:00
|
|
|
use deno_core::StartupData;
|
|
|
|
use std::collections::HashMap;
|
2019-09-02 21:07:11 +00:00
|
|
|
use std::env;
|
2020-07-19 17:49:44 +00:00
|
|
|
use std::path::Path;
|
2019-09-02 21:07:11 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2020-07-15 19:54:38 +00:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
extern crate winapi;
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
extern crate winres;
|
|
|
|
|
2020-07-19 17:49:44 +00:00
|
|
|
fn create_snapshot(
|
|
|
|
mut isolate: CoreIsolate,
|
|
|
|
snapshot_path: &Path,
|
|
|
|
files: Vec<String>,
|
|
|
|
) {
|
|
|
|
for file in files {
|
|
|
|
println!("cargo:rerun-if-changed={}", file);
|
|
|
|
js_check(isolate.execute(&file, &std::fs::read_to_string(&file).unwrap()));
|
2020-01-30 08:32:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 17:49:44 +00: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 22:58:13 +00:00
|
|
|
|
2020-07-19 17:49:44 +00:00
|
|
|
fn create_runtime_snapshot(snapshot_path: &Path, files: Vec<String>) {
|
|
|
|
let runtime_isolate = CoreIsolate::new(StartupData::None, true);
|
|
|
|
create_snapshot(runtime_isolate, snapshot_path, files);
|
|
|
|
}
|
2020-02-25 14:14:27 +00:00
|
|
|
|
2020-07-19 17:49:44 +00:00
|
|
|
fn create_compiler_snapshot(
|
|
|
|
snapshot_path: &Path,
|
|
|
|
files: Vec<String>,
|
|
|
|
cwd: &Path,
|
|
|
|
) {
|
2020-04-23 09:51:07 +00:00
|
|
|
let mut runtime_isolate = CoreIsolate::new(StartupData::None, true);
|
2020-01-22 22:58:13 +00:00
|
|
|
let mut custom_libs: HashMap<String, PathBuf> = HashMap::new();
|
|
|
|
custom_libs.insert(
|
2020-01-29 17:54:23 +00:00
|
|
|
"lib.deno.window.d.ts".to_string(),
|
2020-07-22 16:03:46 +00:00
|
|
|
cwd.join("dts/lib.deno.window.d.ts"),
|
2020-01-22 22:58:13 +00:00
|
|
|
);
|
2020-01-24 19:15:01 +00:00
|
|
|
custom_libs.insert(
|
2020-01-29 17:54:23 +00:00
|
|
|
"lib.deno.worker.d.ts".to_string(),
|
2020-07-22 16:03:46 +00:00
|
|
|
cwd.join("dts/lib.deno.worker.d.ts"),
|
2020-01-29 17:54:23 +00:00
|
|
|
);
|
|
|
|
custom_libs.insert(
|
|
|
|
"lib.deno.shared_globals.d.ts".to_string(),
|
2020-07-22 16:03:46 +00:00
|
|
|
cwd.join("dts/lib.deno.shared_globals.d.ts"),
|
2020-01-29 17:54:23 +00:00
|
|
|
);
|
|
|
|
custom_libs.insert(
|
|
|
|
"lib.deno.ns.d.ts".to_string(),
|
2020-07-22 16:03:46 +00:00
|
|
|
cwd.join("dts/lib.deno.ns.d.ts"),
|
2020-01-24 19:15:01 +00:00
|
|
|
);
|
2020-04-30 15:23:40 +00:00
|
|
|
custom_libs.insert(
|
|
|
|
"lib.deno.unstable.d.ts".to_string(),
|
2020-07-22 16:03:46 +00:00
|
|
|
cwd.join("dts/lib.deno.unstable.d.ts"),
|
2020-04-30 15:23:40 +00:00
|
|
|
);
|
2020-02-25 14:14:27 +00:00
|
|
|
runtime_isolate.register_op(
|
|
|
|
"op_fetch_asset",
|
2020-07-20 23:49:57 +00:00
|
|
|
op_fetch_asset::op_fetch_asset(custom_libs),
|
2020-02-25 14:14:27 +00:00
|
|
|
);
|
2020-07-19 17:49:44 +00:00
|
|
|
create_snapshot(runtime_isolate, snapshot_path, files);
|
|
|
|
}
|
|
|
|
|
2020-07-20 23:49:57 +00:00
|
|
|
fn ts_version() -> String {
|
2020-07-22 16:03:46 +00:00
|
|
|
// TODO(ry) This should be automatically extracted from typescript.js
|
|
|
|
"3.9.2".to_string()
|
2020-07-20 23:49:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 17:49:44 +00: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 23:49:57 +00:00
|
|
|
// op_fetch_asset::trace_serializer();
|
2020-07-19 17:49:44 +00:00
|
|
|
|
2020-07-20 23:49:57 +00:00
|
|
|
println!("cargo:rustc-env=TS_VERSION={}", ts_version());
|
2020-07-19 17:49:44 +00: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-07-22 16:03:46 +00:00
|
|
|
let js_files = get_js_files("js2");
|
|
|
|
create_runtime_snapshot(&runtime_snapshot_path, js_files);
|
|
|
|
|
|
|
|
let js_files = get_js_files("tsc");
|
|
|
|
create_compiler_snapshot(&compiler_snapshot_path, js_files, &c);
|
|
|
|
|
|
|
|
set_binary_metadata();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_js_files(d: &str) -> Vec<String> {
|
|
|
|
let mut js_files = std::fs::read_dir(d)
|
2020-07-19 17:49:44 +00:00
|
|
|
.unwrap()
|
|
|
|
.map(|dir_entry| {
|
|
|
|
let file = dir_entry.unwrap();
|
|
|
|
file.path().to_string_lossy().to_string()
|
|
|
|
})
|
|
|
|
.filter(|filename| filename.ends_with(".js"))
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
js_files.sort();
|
2020-07-22 16:03:46 +00:00
|
|
|
js_files
|
2019-09-02 21:07:11 +00:00
|
|
|
}
|
2020-07-15 19:54:38 +00:00
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
fn set_binary_metadata() {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "windows"))]
|
|
|
|
fn set_binary_metadata() {}
|