summaryrefslogtreecommitdiff
path: root/runtime/build.rs
blob: 52933f19b9b74cbcf637305f02c1e3b75a7d628f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

use std::env;
use std::path::Path;
use std::path::PathBuf;

// This is a shim that allows to generate documentation on docs.rs
#[cfg(not(feature = "docsrs"))]
mod not_docs {
  use super::*;
  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());
    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 extensions: Vec<Extension> = vec![
      deno_webidl::init(),
      deno_console::init(),
      deno_url::init(),
      deno_tls::init(),
      deno_web::init(deno_web::BlobStore::default(), Default::default()),
      deno_fetch::init::<deno_fetch::NoFetchPermissions>(
        "".to_owned(),
        None,
        None,
        None,
        None,
        None,
      ),
      deno_websocket::init::<deno_websocket::NoWebSocketPermissions>(
        "".to_owned(),
        None,
        None,
      ),
      deno_webstorage::init(None),
      deno_crypto::init(None),
      deno_webgpu::init(false),
      deno_timers::init::<deno_timers::NoTimersPermission>(),
      deno_broadcast_channel::init(
        deno_broadcast_channel::InMemoryBroadcastChannel::default(),
        false, // No --unstable.
      ),
      deno_ffi::init::<deno_ffi::NoFfiPermissions>(false),
      deno_net::init::<deno_net::NoNetPermissions>(
        None, false, // No --unstable.
        None,
      ),
      deno_http::init(),
    ];

    let js_runtime = JsRuntime::new(RuntimeOptions {
      will_snapshot: true,
      extensions,
      ..Default::default()
    });
    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);
  }
}

fn main() {
  // To debug snapshot issues uncomment:
  // op_fetch_asset::trace_serializer();

  println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
  println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
  let o = PathBuf::from(env::var_os("OUT_DIR").unwrap());

  // Main snapshot
  let runtime_snapshot_path = o.join("CLI_SNAPSHOT.bin");

  // If we're building on docs.rs we just create
  // and empty snapshot file and return, because `rusty_v8`
  // doesn't actually compile on docs.rs
  if env::var_os("DOCS_RS").is_some() {
    let snapshot_slice = &[];
    std::fs::write(&runtime_snapshot_path, snapshot_slice).unwrap();
    return;
  }

  #[cfg(not(feature = "docsrs"))]
  not_docs::build_snapshot(runtime_snapshot_path)
}