From 9f009fe1e03c7c428305e88dc5e2269c69c421cf Mon Sep 17 00:00:00 2001 From: Will Davis Date: Mon, 4 Feb 2019 22:53:57 -0600 Subject: [PATCH] Use pkg-config to generate sqlite link dependencies When built as a static library, sqlite (or sqlcipher) doesn't carry additional link dependencies with it, and the libsqlite3-sys rlib doesn't pick them up either. A dependent crate attempting to link against rusqlite then has to specify these additional link commands, and even then they need to be specified before the libsqlite3-sys rlib is specified on the command line. Fix this by attempting to use pkg-config when the (SQLITE3|SQLCIPHER)_LIB_DIR is specified, since these builds produce the pkg-config link dependency information, and the pkg_config crate can automatically generate the correct link commands using that. (Additionally, since (SQLITE3|SQLCIPHER)_STATIC is already defined, or not, the --static flag will be correctly configured for pkg_config) --- libsqlite3-sys/build.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libsqlite3-sys/build.rs b/libsqlite3-sys/build.rs index 87998ef..e91f8c6 100644 --- a/libsqlite3-sys/build.rs +++ b/libsqlite3-sys/build.rs @@ -134,8 +134,14 @@ mod build { } // Allow users to specify where to find SQLite. if let Ok(dir) = env::var(format!("{}_LIB_DIR", env_prefix())) { - println!("cargo:rustc-link-lib={}={}", find_link_mode(), link_lib); - println!("cargo:rustc-link-search={}", dir); + // Try to use pkg-config to determine link commands + let pkgconfig_path = Path::new(&dir).join("pkgconfig"); + env::set_var("PKG_CONFIG_PATH", pkgconfig_path); + if let Err(_) = pkg_config::Config::new().probe(link_lib) { + // Otherwise just emit the bare minimum link commands. + println!("cargo:rustc-link-lib={}={}", find_link_mode(), link_lib); + println!("cargo:rustc-link-search={}", dir); + } return HeaderLocation::FromEnvironment; }