Skip to content

Commit

Permalink
Merge pull request #1044 from aminya/windows-build-wasm
Browse files Browse the repository at this point in the history
Fix build-wasm on Windows
  • Loading branch information
maxbrunsfeld authored Apr 5, 2021
2 parents 234bd79 + 90c9a3a commit 1ede511
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ smallbitvec = "2.3.0"
tiny_http = "0.8"
walkdir = "2.3"
webbrowser = "0.5.1"
which = "4.1.0"

[dependencies.tree-sitter]
version = ">= 0.17.0"
Expand Down
33 changes: 30 additions & 3 deletions cli/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use super::generate::parse_grammar::GrammarJSON;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use which::which;

pub fn get_grammar_name(src_dir: &Path) -> Result<String> {
let grammar_json_path = src_dir.join("grammar.json");
Expand All @@ -22,9 +24,14 @@ pub fn compile_language_to_wasm(language_dir: &Path, force_docker: bool) -> Resu
let output_filename = format!("tree-sitter-{}.wasm", grammar_name);

let mut command;
if !force_docker && Command::new("emcc").output().is_ok() {
command = Command::new("emcc");
command.current_dir(&language_dir);
if !force_docker {
let emcc_path = get_emcc_path();
if emcc_path.is_ok() {
command = Command::new(emcc_path.unwrap());
command.current_dir(&language_dir);
} else {
return Err(emcc_path.unwrap_err());
}
} else if Command::new("docker").output().is_ok() {
command = Command::new("docker");
command.args(&["run", "--rm"]);
Expand Down Expand Up @@ -116,3 +123,23 @@ pub fn compile_language_to_wasm(language_dir: &Path, force_docker: bool) -> Resu

Ok(())
}

fn get_emcc_path() -> Result<PathBuf> {
let emcc_bin;
if cfg!(windows) {
emcc_bin = "emcc.bat";
} else {
emcc_bin = "emcc";
};
let emcc_which = which(emcc_bin);
let emcc_path;
if emcc_which.is_ok() {
emcc_path = emcc_which.unwrap();
} else {
return Error::err("emcc was not found on PATH".to_string());
}
if Command::new(&emcc_path).output().is_ok() {
return Ok(emcc_path);
}
return Error::err("emcc binary doesn't work properly".to_string());
}

0 comments on commit 1ede511

Please sign in to comment.