Conditionally compile with a cfg variable instead.

This commit is contained in:
Patrick Thomson 2020-09-02 15:29:49 -04:00
parent 6256110bd2
commit 92a17e782f
2 changed files with 29 additions and 11 deletions

View file

@ -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<String> {

View file

@ -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<PathBuf>) -> Vec<u8> {
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<PathBuf>) -> Vec<u8> {
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<PathBuf>) -> Vec<u8> {
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<PathBuf>) -> Vec<u8> {
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<PathBuf>) -> Vec<u8> {
Vec::new()