tree-sitter/lib/binding_rust/build.rs

90 lines
2.6 KiB
Rust
Raw Normal View History

use std::path::{Path, PathBuf};
use std::{env, fs};
2016-07-10 14:03:00 -07:00
fn main() {
println!("cargo:rerun-if-env-changed=TREE_SITTER_STATIC_ANALYSIS");
if env::var("TREE_SITTER_STATIC_ANALYSIS").is_ok() {
2019-01-15 19:18:33 -08:00
if let (Some(clang_path), Some(scan_build_path)) = (which("clang"), which("scan-build")) {
let clang_path = clang_path.to_str().unwrap();
let scan_build_path = scan_build_path.to_str().unwrap();
env::set_var(
"CC",
&format!(
"{} -analyze-headers --use-analyzer={} cc",
scan_build_path, clang_path
),
);
}
}
#[cfg(feature = "bindgen")]
generate_bindings();
let src_path = Path::new("src");
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());
}
cc::Build::new()
.flag_if_supported("-std=c99")
.flag_if_supported("-fvisibility=hidden")
.flag_if_supported("-Wshadow")
lib: remove utf8proc dependency (#436) * Remove dependency on utf8proc This removes the only external dependency on utf8proc for UTF-8 decoding. It does so by implementing its own UTF-8 decoder. This decoder is both faster and has a simpler API. * .gitmodules: remove utf8proc submodule * docs/section-2-using-parsers.md: remove requirement for utf8proc submodule * docs/section-6-contributing.md: likewise * lib/Cargo.toml: remove utf8proc subdirectory package include * lib/README.md: remove utf8proc subdirectory description * lib/binding_rust/build.rs: remove utf8proc compiler include directory * lib/src/lexer.c: remove utf8proc dependencies and types * lib/src/lib.c: remove utf8proc dependency * lib/src/unicode.h: define types for Unicode decoders * lib/src/utf16.{c,h}: implement more readable UTF-16 decoder * lib/src/utf8.{c,h}: implement fast UTF-8 decoder * lib/utf8proc: remove utf8proc submodule directory * script/build-lib: remove utf8proc compiler include directory * script/build-wasm: likewise * Optimize ts_lexer__get_lookahead. Try to favor non-failure code path and assign lookahead values directly to lexer * lib/src/lexer.c: optimize for non-failure code path * Fix some compiler errors * lib/src/lexer.c: cast from signed to unsigned for decode_next result * lib/src/utf16.c: fix non-constant initializers for older compilers * Remove some missed remnants of utf8proc * docs/section-2-using-parsers.md: only two include paths necessary now * lib/src/lib.c: no need to define UTF8PROC_STATIC * Use ICU's utf8 and utf16 decoding routines * Remove unnecessary casts when calling icu macros * Check buffer length before attempting to decode a unicode character * Use new unicode function when parsing Queries Co-Authored-By: Matthew Krupcale <mkrupcale@matthewkrupcale.com> * Mark libicu files as vendored for GitHub's stats
2019-10-14 14:18:39 -04:00
.include(src_path)
.include("include")
.file(src_path.join("lib.c"))
.compile("tree-sitter");
}
#[cfg(feature = "bindgen")]
fn generate_bindings() {
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("|"))
.generate()
.expect("Failed to generate bindings");
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let bindings_rs = out_dir.join("bindings.rs");
bindings.write_to_file(&bindings_rs).expect(&*format!(
"Failed to write bindings into path: {bindings_rs:?}"
));
}
fn which(exe_name: impl AsRef<Path>) -> Option<PathBuf> {
env::var_os("PATH").and_then(|paths| {
env::split_paths(&paths).find_map(|dir| {
let full_path = dir.join(&exe_name);
if full_path.is_file() {
Some(full_path)
} else {
None
}
})
})
2016-07-10 14:03:00 -07:00
}