1 extern crate cc;
2 
3 use std::path::{Path, PathBuf};
4 use std::{env, fs};
5 
main()6 fn main() {
7     println!("cargo:rerun-if-env-changed=TREE_SITTER_STATIC_ANALYSIS");
8     if env::var("TREE_SITTER_STATIC_ANALYSIS").is_ok() {
9         if let (Some(clang_path), Some(scan_build_path)) = (which("clang"), which("scan-build")) {
10             let clang_path = clang_path.to_str().unwrap();
11             let scan_build_path = scan_build_path.to_str().unwrap();
12             env::set_var(
13                 "CC",
14                 &format!(
15                     "{} -analyze-headers --use-analyzer={} cc",
16                     scan_build_path, clang_path
17                 ),
18             );
19         }
20     }
21 
22     let mut config = cc::Build::new();
23 
24     println!("cargo:rerun-if-env-changed=CARGO_FEATURE_ALLOCATION_TRACKING");
25     if env::var("CARGO_FEATURE_ALLOCATION_TRACKING").is_ok() {
26         config.define("TREE_SITTER_ALLOCATION_TRACKING", "");
27     }
28 
29     let src_path = Path::new("src");
30     for entry in fs::read_dir(&src_path).unwrap() {
31         let entry = entry.unwrap();
32         let path = src_path.join(entry.file_name());
33         println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
34     }
35 
36     config
37         .flag_if_supported("-std=c99")
38         .flag_if_supported("-Wno-unused-parameter")
39         .include(src_path)
40         .include("include")
41         .file(src_path.join("lib.c"))
42         .compile("tree-sitter");
43 }
44 
which(exe_name: impl AsRef<Path>) -> Option<PathBuf>45 fn which(exe_name: impl AsRef<Path>) -> Option<PathBuf> {
46     env::var_os("PATH").and_then(|paths| {
47         env::split_paths(&paths).find_map(|dir| {
48             let full_path = dir.join(&exe_name);
49             if full_path.is_file() {
50                 Some(full_path)
51             } else {
52                 None
53             }
54         })
55     })
56 }
57