2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-11-02 14:41:20 -05:00
|
|
|
use deno_core::error::custom_error;
|
|
|
|
use deno_core::json_op_sync;
|
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
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-11-02 14:41:20 -05:00
|
|
|
use regex::Regex;
|
|
|
|
use serde::Deserialize;
|
2021-02-13 11:56:56 -05:00
|
|
|
use serde_json::Value;
|
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-12-14 12:42:40 -05:00
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:41:20 -05:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct LoadArgs {
|
|
|
|
/// The fully qualified specifier that should be loaded.
|
|
|
|
specifier: String,
|
|
|
|
}
|
|
|
|
|
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-11-02 14:41:20 -05:00
|
|
|
// libs that are being provided by op crates.
|
|
|
|
let mut op_crate_libs = HashMap::new();
|
|
|
|
op_crate_libs.insert("deno.web", deno_web::get_declaration());
|
|
|
|
op_crate_libs.insert("deno.fetch", deno_fetch::get_declaration());
|
2021-01-06 10:57:28 -05:00
|
|
|
op_crate_libs.insert("deno.websocket", deno_websocket::get_declaration());
|
2020-11-02 14:41:20 -05:00
|
|
|
|
|
|
|
// ensure we invalidate the build properly.
|
|
|
|
for (_, path) in op_crate_libs.iter() {
|
|
|
|
println!("cargo:rerun-if-changed={}", path.display());
|
|
|
|
}
|
|
|
|
|
|
|
|
// libs that should be loaded into the isolate before snapshotting.
|
|
|
|
let libs = vec![
|
|
|
|
// Deno custom type libraries
|
|
|
|
"deno.window",
|
|
|
|
"deno.worker",
|
|
|
|
"deno.shared_globals",
|
|
|
|
"deno.ns",
|
|
|
|
"deno.unstable",
|
|
|
|
// Deno built-in type libraries
|
|
|
|
"es5",
|
|
|
|
"es2015.collection",
|
|
|
|
"es2015.core",
|
|
|
|
"es2015",
|
|
|
|
"es2015.generator",
|
|
|
|
"es2015.iterable",
|
|
|
|
"es2015.promise",
|
|
|
|
"es2015.proxy",
|
|
|
|
"es2015.reflect",
|
|
|
|
"es2015.symbol",
|
|
|
|
"es2015.symbol.wellknown",
|
|
|
|
"es2016.array.include",
|
|
|
|
"es2016",
|
|
|
|
"es2017",
|
|
|
|
"es2017.intl",
|
|
|
|
"es2017.object",
|
|
|
|
"es2017.sharedmemory",
|
|
|
|
"es2017.string",
|
|
|
|
"es2017.typedarrays",
|
|
|
|
"es2018.asyncgenerator",
|
|
|
|
"es2018.asynciterable",
|
|
|
|
"es2018",
|
|
|
|
"es2018.intl",
|
|
|
|
"es2018.promise",
|
|
|
|
"es2018.regexp",
|
|
|
|
"es2019.array",
|
|
|
|
"es2019",
|
|
|
|
"es2019.object",
|
|
|
|
"es2019.string",
|
|
|
|
"es2019.symbol",
|
|
|
|
"es2020.bigint",
|
|
|
|
"es2020",
|
|
|
|
"es2020.intl",
|
|
|
|
"es2020.promise",
|
2020-11-23 17:31:10 -05:00
|
|
|
"es2020.sharedmemory",
|
2020-11-02 14:41:20 -05:00
|
|
|
"es2020.string",
|
|
|
|
"es2020.symbol.wellknown",
|
|
|
|
"esnext",
|
|
|
|
"esnext.intl",
|
|
|
|
"esnext.promise",
|
|
|
|
"esnext.string",
|
|
|
|
"esnext.weakref",
|
|
|
|
];
|
|
|
|
|
2021-01-17 09:12:00 -05:00
|
|
|
let path_dts = cwd.join("dts");
|
|
|
|
// ensure we invalidate the build properly.
|
|
|
|
for name in libs.iter() {
|
|
|
|
println!(
|
|
|
|
"cargo:rerun-if-changed={}",
|
|
|
|
path_dts.join(format!("lib.{}.d.ts", name)).display()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:41:20 -05:00
|
|
|
// create a copy of the vector that includes any op crate libs to be passed
|
|
|
|
// to the JavaScript compiler to build into the snapshot
|
|
|
|
let mut build_libs = libs.clone();
|
|
|
|
for (op_lib, _) in op_crate_libs.iter() {
|
|
|
|
build_libs.push(op_lib.to_owned());
|
|
|
|
}
|
|
|
|
|
|
|
|
let re_asset = Regex::new(r"asset:/{3}lib\.(\S+)\.d\.ts").expect("bad regex");
|
|
|
|
let build_specifier = "asset:///bootstrap.ts";
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-10-07 11:20:20 -04:00
|
|
|
let mut js_runtime = JsRuntime::new(RuntimeOptions {
|
2020-09-11 09:18:49 -04:00
|
|
|
will_snapshot: true,
|
|
|
|
..Default::default()
|
|
|
|
});
|
2020-10-07 11:20:20 -04:00
|
|
|
js_runtime.register_op(
|
2020-11-02 14:41:20 -05:00
|
|
|
"op_build_info",
|
2021-02-13 11:56:56 -05:00
|
|
|
json_op_sync(move |_state, _args: Value, _bufs| {
|
2020-11-02 14:41:20 -05:00
|
|
|
Ok(json!({
|
|
|
|
"buildSpecifier": build_specifier,
|
|
|
|
"libs": build_libs,
|
|
|
|
}))
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
// using the same op that is used in `tsc.rs` for loading modules and reading
|
|
|
|
// files, but a slightly different implementation at build time.
|
|
|
|
js_runtime.register_op(
|
|
|
|
"op_load",
|
|
|
|
json_op_sync(move |_state, args, _bufs| {
|
|
|
|
let v: LoadArgs = serde_json::from_value(args)?;
|
|
|
|
// we need a basic file to send to tsc to warm it up.
|
|
|
|
if v.specifier == build_specifier {
|
|
|
|
Ok(json!({
|
|
|
|
"data": r#"console.log("hello deno!");"#,
|
|
|
|
"hash": "1",
|
|
|
|
// this corresponds to `ts.ScriptKind.TypeScript`
|
|
|
|
"scriptKind": 3
|
|
|
|
}))
|
|
|
|
// specifiers come across as `asset:///lib.{lib_name}.d.ts` and we need to
|
|
|
|
// parse out just the name so we can lookup the asset.
|
|
|
|
} else if let Some(caps) = re_asset.captures(&v.specifier) {
|
|
|
|
if let Some(lib) = caps.get(1).map(|m| m.as_str()) {
|
|
|
|
// if it comes from an op crate, we were supplied with the path to the
|
|
|
|
// file.
|
|
|
|
let path = if let Some(op_crate_lib) = op_crate_libs.get(lib) {
|
|
|
|
op_crate_lib.clone()
|
|
|
|
// otherwise we are will generate the path ourself
|
|
|
|
} else {
|
|
|
|
path_dts.join(format!("lib.{}.d.ts", lib))
|
|
|
|
};
|
|
|
|
let data = std::fs::read_to_string(path)?;
|
|
|
|
Ok(json!({
|
|
|
|
"data": data,
|
|
|
|
"hash": "1",
|
|
|
|
// this corresponds to `ts.ScriptKind.TypeScript`
|
|
|
|
"scriptKind": 3
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
Err(custom_error(
|
|
|
|
"InvalidSpecifier",
|
|
|
|
format!("An invalid specifier was requested: {}", v.specifier),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(custom_error(
|
|
|
|
"InvalidSpecifier",
|
|
|
|
format!("An invalid specifier was requested: {}", v.specifier),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}),
|
2020-02-25 09:14:27 -05:00
|
|
|
);
|
2020-10-07 11:20:20 -04:00
|
|
|
create_snapshot(js_runtime, 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-11-02 12:40:33 -05:00
|
|
|
fn git_commit_hash() -> String {
|
|
|
|
if let Ok(output) = std::process::Command::new("git")
|
|
|
|
.arg("rev-list")
|
|
|
|
.arg("-1")
|
|
|
|
.arg("HEAD")
|
|
|
|
.output()
|
|
|
|
{
|
|
|
|
if output.status.success() {
|
2021-01-19 07:53:23 -05:00
|
|
|
std::str::from_utf8(&output.stdout[..40])
|
2020-11-02 12:40:33 -05:00
|
|
|
.unwrap()
|
|
|
|
.to_string()
|
|
|
|
} else {
|
|
|
|
// When not in git repository
|
|
|
|
// (e.g. when the user install by `cargo install deno`)
|
|
|
|
"UNKNOWN".to_string()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// When there is no git command for some reason
|
|
|
|
"UNKNOWN".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
fn main() {
|
2020-12-14 09:15:16 -05:00
|
|
|
// Skip building from docs.rs.
|
|
|
|
if env::var_os("DOCS_RS").is_some() {
|
2020-07-19 13:49:44 -04:00
|
|
|
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-11-02 12:40:33 -05:00
|
|
|
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", git_commit_hash());
|
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()
|
|
|
|
);
|
2021-01-06 10:57:28 -05:00
|
|
|
println!(
|
|
|
|
"cargo:rustc-env=DENO_WEBSOCKET_LIB_PATH={}",
|
|
|
|
deno_websocket::get_declaration().display()
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2020-11-02 12:40:33 -05:00
|
|
|
println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
|
|
|
|
println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
|
2020-11-29 11:08:38 -05:00
|
|
|
if let Ok(c) = env::var("DENO_CANARY") {
|
|
|
|
println!("cargo:rustc-env=DENO_CANARY={}", c);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
let o = PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
|
|
|
|
|
|
|
// Main snapshot
|
|
|
|
let compiler_snapshot_path = o.join("COMPILER_SNAPSHOT.bin");
|
|
|
|
|
2020-12-14 12:42:40 -05:00
|
|
|
let js_files = get_js_files("tsc");
|
2020-07-22 12:03:46 -04:00
|
|
|
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-12-14 12:42:40 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|