diff --git a/librocksdb-sys/build.rs b/librocksdb-sys/build.rs index 3ac2dcc..1f19287 100644 --- a/librocksdb-sys/build.rs +++ b/librocksdb-sys/build.rs @@ -29,10 +29,7 @@ fn fail_on_empty_directory(name: &str) { } } -fn build_rocksdb() { - println!("cargo:rerun-if-changed=build.rs"); - println!("cargo:rerun-if-changed=rocksdb/"); - +fn bindgen_rocksdb() { let bindings = bindgen::Builder::default() .header("rocksdb/include/rocksdb/c.h") .hide_type("max_align_t") // https://github.com/rust-lang-nursery/rust-bindgen/issues/550 @@ -44,7 +41,9 @@ fn build_rocksdb() { bindings .write_to_file(out_path.join("bindings.rs")) .expect("unable to write rocksdb bindings"); +} +fn build_rocksdb() { let mut config = cc::Build::new(); config.include("rocksdb/include/"); config.include("rocksdb/"); @@ -148,9 +147,32 @@ fn build_snappy() { config.compile("libsnappy.a"); } +fn try_to_find_and_link_lib(lib_name: &str) -> bool { + if let Ok(lib_dir) = env::var(&format!("{}_LIB_DIR", lib_name)) { + println!("cargo:rustc-link-search=native={}", lib_dir); + let mode = match env::var_os(&format!("{}_STATIC", lib_name)) { + Some(_) => "static", + None => "dylib", + }; + println!("cargo:rustc-link-lib={}={}", mode, lib_name.to_lowercase()); + return true; + } + false +} + fn main() { + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=rocksdb/"); + println!("cargo:rerun-if-changed=snappy/"); + fail_on_empty_directory("rocksdb"); fail_on_empty_directory("snappy"); - build_rocksdb(); - build_snappy(); + bindgen_rocksdb(); + + if !try_to_find_and_link_lib("ROCKSDB") { + build_rocksdb(); + } + if !try_to_find_and_link_lib("SNAPPY") { + build_snappy(); + } }