From 8f48240bf1e7d654e127ed1147df59be93c07db0 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 14 Jan 2019 14:06:22 -0800 Subject: [PATCH] Allow building the C code with static analysis --- lib/build.rs | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/lib/build.rs b/lib/build.rs index 7e8714ef..e4d1f91a 100644 --- a/lib/build.rs +++ b/lib/build.rs @@ -5,16 +5,7 @@ use std::path::{Path, PathBuf}; use std::fs; fn main() { - let mut config = cc::Build::new(); - config - .define("UTF8PROC_STATIC", "") - .flag_if_supported("-std=c99") - .flag_if_supported("-Wno-unused-parameter") - .include("include") - .include("utf8proc") - .file(Path::new("src").join("lib.c")) - .compile("tree-sitter"); - + println!("cargo:rerun-if-env-changed=TREE_SITTER_TEST"); if env::var("TREE_SITTER_TEST").is_ok() { let mut parser_config = cc::Build::new(); parser_config @@ -65,4 +56,34 @@ fn main() { scanner_c_config.compile("fixture-scanners-c"); scanner_cxx_config.compile("fixture-scanners-cxx"); } + + println!("cargo:rerun-if-env-changed=TREE_SITTER_STATIC_ANALYSIS"); + if env::var("TREE_SITTER_STATIC_ANALYSIS").is_ok() { + let clang_path = which("clang").unwrap(); + let clang_path = clang_path.to_str().unwrap(); + env::set_var("CC", &format!("scan-build -analyze-headers --use-analyzer={} cc", clang_path)); + } + + let mut config = cc::Build::new(); + config + .define("UTF8PROC_STATIC", "") + .flag_if_supported("-std=c99") + .flag_if_supported("-Wno-unused-parameter") + .include("include") + .include("utf8proc") + .file(Path::new("src").join("lib.c")) + .compile("tree-sitter"); +} + +fn which(exe_name: impl AsRef) -> Option { + 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 + } + }) + }) }