2024-05-13 16:09:58 +02:00
|
|
|
use std::{env, fs, path::PathBuf};
|
2016-07-10 14:03:00 -07:00
|
|
|
|
|
|
|
|
fn main() {
|
2024-02-01 22:18:04 -08:00
|
|
|
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
|
|
|
|
|
|
2023-08-20 16:00:33 +03:00
|
|
|
#[cfg(feature = "bindgen")]
|
2024-02-01 22:18:04 -08:00
|
|
|
generate_bindings(&out_dir);
|
|
|
|
|
|
|
|
|
|
fs::copy(
|
|
|
|
|
"src/wasm/stdlib-symbols.txt",
|
|
|
|
|
out_dir.join("stdlib-symbols.txt"),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2023-08-20 16:00:33 +03:00
|
|
|
|
2022-01-03 10:57:01 -08:00
|
|
|
let mut config = cc::Build::new();
|
|
|
|
|
|
|
|
|
|
println!("cargo:rerun-if-env-changed=CARGO_FEATURE_WASM");
|
|
|
|
|
if env::var("CARGO_FEATURE_WASM").is_ok() {
|
2024-02-26 10:48:58 -08:00
|
|
|
config
|
|
|
|
|
.define("TREE_SITTER_FEATURE_WASM", "")
|
|
|
|
|
.define("static_assert(...)", "")
|
2024-05-26 01:17:45 -04:00
|
|
|
.include(env::var("DEP_WASMTIME_C_API_INCLUDE").unwrap());
|
2022-01-03 10:57:01 -08:00
|
|
|
}
|
|
|
|
|
|
2024-05-13 16:09:58 +02:00
|
|
|
let manifest_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
|
2023-11-20 09:39:19 -05:00
|
|
|
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() {
|
2019-01-21 14:22:35 -08:00
|
|
|
let entry = entry.unwrap();
|
|
|
|
|
let path = src_path.join(entry.file_name());
|
|
|
|
|
println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 10:57:01 -08:00
|
|
|
config
|
2024-02-27 21:56:24 +02:00
|
|
|
.flag_if_supported("-std=c11")
|
2023-08-17 16:27:52 +03:00
|
|
|
.flag_if_supported("-fvisibility=hidden")
|
|
|
|
|
.flag_if_supported("-Wshadow")
|
2023-10-27 12:16:05 +01:00
|
|
|
.flag_if_supported("-Wno-unused-parameter")
|
2024-05-30 16:05:30 +08:00
|
|
|
.flag_if_supported("-Wno-incompatible-pointer-types")
|
2023-11-20 09:39:19 -05:00
|
|
|
.include(&src_path)
|
|
|
|
|
.include(&wasm_path)
|
|
|
|
|
.include(&include_path)
|
2024-10-04 23:15:17 -04:00
|
|
|
.define("_POSIX_C_SOURCE", "200112L")
|
|
|
|
|
.define("_DEFAULT_SOURCE", None)
|
2024-05-30 16:05:30 +08:00
|
|
|
.warnings(false)
|
2019-01-21 14:22:35 -08:00
|
|
|
.file(src_path.join("lib.c"))
|
2019-01-14 14:06:22 -08:00
|
|
|
.compile("tree-sitter");
|
2023-11-20 09:39:19 -05:00
|
|
|
|
|
|
|
|
println!("cargo:include={}", include_path.display());
|
2019-01-14 14:06:22 -08:00
|
|
|
}
|
|
|
|
|
|
2023-08-20 16:00:33 +03:00
|
|
|
#[cfg(feature = "bindgen")]
|
2024-05-30 16:05:30 +08:00
|
|
|
fn generate_bindings(out_dir: &std::path::Path) {
|
2025-06-15 18:22:58 -04:00
|
|
|
use std::str::FromStr;
|
2024-12-16 00:48:20 -05:00
|
|
|
|
|
|
|
|
use bindgen::RustTarget;
|
|
|
|
|
|
2023-08-20 16:00:33 +03:00
|
|
|
const HEADER_PATH: &str = "include/tree_sitter/api.h";
|
|
|
|
|
|
2024-02-06 23:18:27 +01:00
|
|
|
println!("cargo:rerun-if-changed={HEADER_PATH}");
|
2023-08-20 16:00:33 +03:00
|
|
|
|
|
|
|
|
let no_copy = [
|
|
|
|
|
"TSInput",
|
|
|
|
|
"TSLanguage",
|
|
|
|
|
"TSLogger",
|
|
|
|
|
"TSLookaheadIterator",
|
|
|
|
|
"TSParser",
|
|
|
|
|
"TSTree",
|
|
|
|
|
"TSQuery",
|
|
|
|
|
"TSQueryCursor",
|
|
|
|
|
"TSQueryCapture",
|
|
|
|
|
"TSQueryMatch",
|
|
|
|
|
"TSQueryPredicateStep",
|
|
|
|
|
];
|
|
|
|
|
|
2025-06-13 11:11:22 -07:00
|
|
|
let rust_version = env!("CARGO_PKG_RUST_VERSION");
|
|
|
|
|
|
2023-08-20 16:00:33 +03:00
|
|
|
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("|"))
|
2023-09-03 06:47:27 +03:00
|
|
|
.prepend_enum_name(false)
|
2024-05-30 16:05:30 +08:00
|
|
|
.use_core()
|
2024-12-16 00:48:20 -05:00
|
|
|
.clang_arg("-D TREE_SITTER_FEATURE_WASM")
|
|
|
|
|
.rust_target(RustTarget::from_str(rust_version).unwrap())
|
2023-08-20 16:00:33 +03:00
|
|
|
.generate()
|
|
|
|
|
.expect("Failed to generate bindings");
|
|
|
|
|
|
|
|
|
|
let bindings_rs = out_dir.join("bindings.rs");
|
2025-03-03 01:37:48 -05:00
|
|
|
bindings.write_to_file(&bindings_rs).unwrap_or_else(|_| {
|
|
|
|
|
panic!(
|
|
|
|
|
"Failed to write bindings into path: {}",
|
|
|
|
|
bindings_rs.display()
|
|
|
|
|
)
|
|
|
|
|
});
|
2023-08-20 16:00:33 +03:00
|
|
|
}
|