-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
59 lines (50 loc) · 1.87 KB
/
build.rs
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
use std::{os::unix::ffi::OsStrExt, path::Path, process::Command};
fn main() {
let program_parser_dir = Path::new("tree-sitter-borlang");
let expr_parser_dir = Path::new("tree-sitter-borlang-expr");
build_parser(program_parser_dir, "program");
build_parser(expr_parser_dir, "expr");
rerun_if_js_changed_in(program_parser_dir);
rerun_if_js_changed_in(expr_parser_dir);
}
fn rerun_if_js_changed_in(dir: &Path) {
for f in std::fs::read_dir(dir).unwrap() {
let f = f.unwrap();
if f.path().extension().and_then(|x| x.to_str()) == Some("js") {
println!("cargo:rerun-if-changed={}", f.path().to_str().unwrap());
}
}
}
fn build_parser(dir: &Path, grammar_name: &str) {
tree_sitter_cli_generate(dir);
tree_sitter_build(dir, grammar_name);
}
fn tree_sitter_cli_generate(dir: &Path) {
let mut cmd = Command::new("npm");
let cmd = cmd
.current_dir(dir)
.args(["exec", "--", "tree-sitter-cli", "generate", "--no-bindings"])
.arg("grammar.js");
println!("tree-sitter-cli generate {:?}", &cmd.get_args());
let out = cmd.output().unwrap();
println!("{}", String::from_utf8_lossy(&out.stdout));
eprintln!("{}", String::from_utf8_lossy(&out.stderr));
if !out.status.success() {
panic!(
"Parser generation failed: dir={}",
String::from_utf8_lossy(dir.as_os_str().as_bytes())
);
}
}
fn tree_sitter_build(dir: &Path, grammar_name: &str) {
let src_dir = dir.join("src");
let mut c_config = cc::Build::new();
c_config.include(&src_dir);
c_config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable")
.flag_if_supported("-Wno-trigraphs");
let parser_path = src_dir.join("parser.c");
c_config.file(&parser_path);
c_config.compile(&format!("{grammar_name}_parser"));
}