From 92a17e782f9f785193de204e3dfc3997e47c9ee0 Mon Sep 17 00:00:00 2001 From: Patrick Thomson Date: Wed, 2 Sep 2020 15:29:49 -0400 Subject: [PATCH] Conditionally compile with a cfg variable instead. --- cli/build.rs | 19 +++++++++---------- cli/src/web_ui.rs | 21 ++++++++++++++++++++- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/cli/build.rs b/cli/build.rs index 80692a9e..cb7421aa 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -1,15 +1,14 @@ use std::path::{Path, PathBuf}; use std::{env, fs}; +use std::vec::Vec; fn main() { if let Some(git_sha) = read_git_sha() { println!("cargo:rustc-env={}={}", "BUILD_SHA", git_sha); } - if let Some(missing) = wasm_files_present() { - println!("error: couldn't find required wasm binding file {}", missing.display()); - println!("Have you run `script/build-wasm?`"); - std::process::exit(1); + if wasm_files_present() { + println!("cargo:rustc-cfg={}", "TREE_SITTER_EMBED_WASM_BINDING"); } println!( @@ -19,7 +18,7 @@ fn main() { } #[cfg(unix)] -fn required_files() -> std::vec::Vec<&'static Path> { +fn required_files() -> Vec<&'static Path> { return vec![ Path::new("../cli/src/web_ui.html"), Path::new("../docs/assets/js/playground.js"), @@ -29,20 +28,20 @@ fn required_files() -> std::vec::Vec<&'static Path> { } #[cfg(windows)] -fn required_files() -> std::vec::Vec<&'static Path> { +fn required_files() -> Vec<&'static Path> { return vec![ Path::new("../cli/src/web_ui.html"), Path::new("../docs/assets/js/playground.js"), ]; } -fn wasm_files_present() -> Option<&'static Path> { +fn wasm_files_present() -> bool { for path in required_files() { - if !path.exists() { - return Some(path) + if path.exists() { + return false } } - return None + return true } fn read_git_sha() -> Option { diff --git a/cli/src/web_ui.rs b/cli/src/web_ui.rs index 7d4c7eec..0c0c161f 100644 --- a/cli/src/web_ui.rs +++ b/cli/src/web_ui.rs @@ -10,6 +10,16 @@ use webbrowser; macro_rules! resource { ($name: tt, $path: tt) => { + #[cfg(TREE_SITTER_EMBED_WASM_BINDING)] + fn $name(tree_sitter_dir: &Option) -> Vec { + if let Some(tree_sitter_dir) = tree_sitter_dir { + fs::read(tree_sitter_dir.join($path)).unwrap() + } else { + include_bytes!(concat!("../../", $path)).to_vec() + } + } + + #[cfg(not(TREE_SITTER_EMBED_WASM_BINDING))] fn $name(tree_sitter_dir: &Option) -> Vec { if let Some(tree_sitter_dir) = tree_sitter_dir { fs::read(tree_sitter_dir.join($path)).unwrap() @@ -22,7 +32,7 @@ macro_rules! resource { macro_rules! posix_resource { ($name: tt, $path: tt) => { - #[cfg(unix)] + #[cfg(all(unix, TREE_SITTER_EMBED_WASM_BINDING))] fn $name(tree_sitter_dir: &Option) -> Vec { if let Some(tree_sitter_dir) = tree_sitter_dir { fs::read(tree_sitter_dir.join($path)).unwrap() @@ -31,6 +41,15 @@ macro_rules! posix_resource { } } + #[cfg(all(unix, not(TREE_SITTER_EMBED_WASM_BINDING)))] + fn $name(tree_sitter_dir: &Option) -> Vec { + if let Some(tree_sitter_dir) = tree_sitter_dir { + fs::read(tree_sitter_dir.join($path)).unwrap() + } else { + Vec::new() + } + } + #[cfg(windows)] fn $name(_: &Option) -> Vec { Vec::new()