Add support for linking to SQLCipher

This commit is contained in:
Lorenzo Villani
2017-10-24 10:54:48 +02:00
parent d5bd7d9601
commit 8cbfc00ec6
4 changed files with 37 additions and 11 deletions

View File

@@ -9,6 +9,10 @@ mod build {
use std::path::Path;
pub fn main() {
if cfg!(feature = "sqlcipher") {
panic!("Builds with bundled SQLCipher are not supported");
}
let out_dir = env::var("OUT_DIR").unwrap();
let out_path = Path::new(&out_dir).join("bindgen.rs");
fs::copy("sqlite3/bindgen_bundled_version.rs", out_path)
@@ -58,8 +62,9 @@ mod build {
fn from(header: HeaderLocation) -> String {
match header {
HeaderLocation::FromEnvironment => {
let mut header = env::var("SQLITE3_INCLUDE_DIR")
.expect("SQLITE3_INCLUDE_DIR must be set if SQLITE3_LIB_DIR is set");
let prefix = env_prefix();
let mut header = env::var(format!("{}_INCLUDE_DIR", prefix))
.expect(&format!("{}_INCLUDE_DIR must be set if {}_LIB_DIR is set", prefix, prefix));
header.push_str("/sqlite3.h");
header
}
@@ -76,9 +81,11 @@ mod build {
// Prints the necessary cargo link commands and returns the path to the header.
fn find_sqlite() -> HeaderLocation {
let link_lib = link_lib();
// Allow users to specify where to find SQLite.
if let Ok(dir) = env::var("SQLITE3_LIB_DIR") {
println!("cargo:rustc-link-lib=sqlite3");
if let Ok(dir) = env::var(format!("{}_LIB_DIR", env_prefix())) {
println!("cargo:rustc-link-lib={}", link_lib);
println!("cargo:rustc-link-search={}", dir);
return HeaderLocation::FromEnvironment;
}
@@ -88,7 +95,7 @@ mod build {
}
// See if pkg-config can do everything for us.
match pkg_config::Config::new().print_system_libs(false).probe("sqlite3") {
match pkg_config::Config::new().print_system_libs(false).probe(link_lib) {
Ok(mut lib) => {
if let Some(mut header) = lib.include_paths.pop() {
header.push("sqlite3.h");
@@ -102,7 +109,7 @@ mod build {
// request and hope that the library exists on the system paths. We used to
// output /usr/lib explicitly, but that can introduce other linking problems; see
// https://github.com/jgallagher/rusqlite/issues/207.
println!("cargo:rustc-link-lib=sqlite3");
println!("cargo:rustc-link-lib={}", link_lib);
HeaderLocation::Wrapper
}
}
@@ -111,7 +118,7 @@ mod build {
#[cfg(all(feature = "vcpkg", target_env = "msvc"))]
fn try_vcpkg() -> Option<HeaderLocation> {
// See if vcpkg can find it.
if let Ok(mut lib) = vcpkg::Config::new().probe("sqlite3") {
if let Ok(mut lib) = vcpkg::Config::new().probe(link_lib()) {
if let Some(mut header) = lib.include_paths.pop() {
header.push("sqlite3.h");
return Some(HeaderLocation::FromPath(header.to_string_lossy().into()));
@@ -125,6 +132,22 @@ mod build {
None
}
fn env_prefix() -> &'static str {
if cfg!(feature = "sqlcipher") {
"SQLCIPHER"
} else {
"SQLITE3"
}
}
fn link_lib() -> &'static str {
if cfg!(feature = "sqlcipher") {
"sqlcipher"
} else {
"sqlite3"
}
}
#[cfg(not(feature = "buildtime_bindgen"))]
mod bindings {
use super::HeaderLocation;