forked from tree-sitter/tree-sitter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
86 lines (73 loc) · 2.53 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::path::{Path, PathBuf};
use std::{env, fs};
fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
#[cfg(feature = "bindgen")]
generate_bindings(&out_dir);
fs::copy(
"src/wasm/stdlib-symbols.txt",
out_dir.join("stdlib-symbols.txt"),
)
.unwrap();
let mut config = cc::Build::new();
println!("cargo:rerun-if-env-changed=CARGO_FEATURE_WASM");
if env::var("CARGO_FEATURE_WASM").is_ok() {
config
.define("TREE_SITTER_FEATURE_WASM", "")
.define("static_assert(...)", "")
.include(env::var("DEP_WASMTIME_C_API_INCLUDE").unwrap())
.include(env::var("DEP_WASMTIME_C_API_WASM_INCLUDE").unwrap());
}
let manifest_path = Path::new(env!("CARGO_MANIFEST_DIR"));
let include_path = manifest_path.join("include");
let src_path = manifest_path.join("src");
let wasm_path = src_path.join("wasm");
for entry in fs::read_dir(&src_path).unwrap() {
let entry = entry.unwrap();
let path = src_path.join(entry.file_name());
println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
}
config
.flag_if_supported("-std=c11")
.flag_if_supported("-fvisibility=hidden")
.flag_if_supported("-Wshadow")
.flag_if_supported("-Wno-unused-parameter")
.include(&src_path)
.include(&wasm_path)
.include(&include_path)
.file(src_path.join("lib.c"))
.compile("tree-sitter");
println!("cargo:include={}", include_path.display());
}
#[cfg(feature = "bindgen")]
fn generate_bindings(out_dir: &Path) {
const HEADER_PATH: &str = "include/tree_sitter/api.h";
println!("cargo:rerun-if-changed={HEADER_PATH}");
let no_copy = [
"TSInput",
"TSLanguage",
"TSLogger",
"TSLookaheadIterator",
"TSParser",
"TSTree",
"TSQuery",
"TSQueryCursor",
"TSQueryCapture",
"TSQueryMatch",
"TSQueryPredicateStep",
];
let bindings = bindgen::Builder::default()
.header(HEADER_PATH)
.layout_tests(false)
.allowlist_type("^TS.*")
.allowlist_function("^ts_.*")
.allowlist_var("^TREE_SITTER.*")
.no_copy(no_copy.join("|"))
.prepend_enum_name(false)
.generate()
.expect("Failed to generate bindings");
let bindings_rs = out_dir.join("bindings.rs");
bindings
.write_to_file(&bindings_rs)
.unwrap_or_else(|_| panic!("Failed to write bindings into path: {bindings_rs:?}"));
}