From 57aea228e85b0c0d8f18f34ff0ade4a68812c391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 11 Aug 2021 18:01:30 +0200 Subject: [PATCH] build: allow deno_runtime crate to build in docs.rs (#11602) --- Cargo.lock | 2 +- runtime/Cargo.toml | 9 ++- runtime/build.rs | 193 ++++++++++++++++++++++++--------------------- 3 files changed, 113 insertions(+), 91 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 352ef1164d..3b01bf4246 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -818,7 +818,7 @@ dependencies = [ [[package]] name = "deno_runtime" -version = "0.22.0" +version = "0.22.2" dependencies = [ "atty", "deno_broadcast_channel", diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 4f1841fa20..e79b7c2931 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -2,13 +2,17 @@ [package] name = "deno_runtime" -version = "0.22.0" +version = "0.22.2" authors = ["the Deno authors"] edition = "2018" license = "MIT" repository = "https://github.com/denoland/deno" description = "Provides the deno runtime library" +[features] +# "fake" feature that allows to generate docs on docs.rs +docsrs = [] + [lib] name = "deno_runtime" path = "lib.rs" @@ -88,3 +92,6 @@ nix = "0.20.0" [dev-dependencies] # Used in benchmark test_util = { path = "../test_util" } + +[package.metadata.docs.rs] +features = ["docsrs"] diff --git a/runtime/build.rs b/runtime/build.rs index 3003fb2313..daab902154 100644 --- a/runtime/build.rs +++ b/runtime/build.rs @@ -1,90 +1,110 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -use deno_core::Extension; -use deno_core::JsRuntime; -use deno_core::RuntimeOptions; use std::env; use std::path::Path; 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, -) { - // 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(); +// 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, + ) { + // 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()); } - 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) { + let extensions: Vec = 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::( + "".to_owned(), + None, + None, + None, + None, + ), + deno_websocket::init::( + "".to_owned(), + None, + None, + ), + deno_webstorage::init(None), + deno_crypto::init(None), + deno_webgpu::init(false), + deno_timers::init::(), + deno_broadcast_channel::init( + deno_broadcast_channel::InMemoryBroadcastChannel::default(), + false, // No --unstable. + ), + deno_ffi::init::(false), + deno_net::init::( + None, false, // No --unstable. + None, + ), + deno_http::init(), + ]; -fn create_runtime_snapshot(snapshot_path: &Path, files: Vec) { - let extensions: Vec = 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::( - "".to_owned(), - None, - None, - None, - None, - ), - deno_websocket::init::( - "".to_owned(), - None, - None, - ), - deno_webstorage::init(None), - deno_crypto::init(None), - deno_webgpu::init(false), - deno_timers::init::(), - deno_broadcast_channel::init( - deno_broadcast_channel::InMemoryBroadcastChannel::default(), - false, // No --unstable. - ), - deno_ffi::init::(false), - deno_net::init::( - 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); + } - 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 { + 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::>(); + 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() { - // Skip building from docs.rs. - if env::var_os("DOCS_RS").is_some() { - return; - } - // To debug snapshot issues uncomment: // op_fetch_asset::trace_serializer(); @@ -95,20 +115,15 @@ fn main() { // Main snapshot let runtime_snapshot_path = o.join("CLI_SNAPSHOT.bin"); - let js_files = get_js_files("js"); - create_runtime_snapshot(&runtime_snapshot_path, js_files); -} + // 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; + } -fn get_js_files(d: &str) -> Vec { - 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::>(); - js_files.sort(); - js_files + #[cfg(not(feature = "docsrs"))] + not_docs::build_snapshot(runtime_snapshot_path) }