Add bundle-sqlcipher and bundle-ssl (#860)

Co-authored-by: Thom Chiovoloni <chiovolonit@gmail.com>
This commit is contained in:
dubiousjim
2021-06-02 15:07:56 -04:00
committed by GitHub
parent a2d48a8c15
commit 8770e7d443
14 changed files with 255193 additions and 66 deletions

View File

@@ -36,35 +36,54 @@ fn main() {
.expect("Could not copy bindings to output directory");
return;
}
if cfg!(feature = "sqlcipher") {
if cfg!(all(
feature = "sqlcipher",
not(feature = "bundled-sqlcipher")
)) {
if cfg!(feature = "bundled") || (win_target() && cfg!(feature = "bundled-windows")) {
println!(
"cargo:warning=Builds with bundled SQLCipher are not supported. Searching for SQLCipher to link against. \
This can lead to issues if your version of SQLCipher is not up to date!");
"cargo:warning=For backwards compatibility, feature 'sqlcipher' overrides
features 'bundled' and 'bundled-windows'. If you want a bundled build of
SQLCipher (available for the moment only on Unix), use feature 'bundled-sqlcipher'
or 'bundled-sqlcipher-vendored-openssl' to also bundle OpenSSL crypto."
)
}
build_linked::main(&out_dir, &out_path)
} else if cfg!(feature = "bundled") || (win_target() && cfg!(feature = "bundled-windows")) {
#[cfg(any(feature = "bundled", feature = "bundled-windows"))]
} else if cfg!(feature = "bundled")
|| (win_target() && cfg!(feature = "bundled-windows"))
|| cfg!(feature = "bundled-sqlcipher")
{
#[cfg(any(
feature = "bundled",
feature = "bundled-windows",
feature = "bundled-sqlcipher"
))]
build_bundled::main(&out_dir, &out_path);
#[cfg(not(any(feature = "bundled", feature = "bundled-windows")))]
#[cfg(not(any(
feature = "bundled",
feature = "bundled-windows",
feature = "bundled-sqlcipher"
)))]
panic!("The runtime test should not run this branch, which has not compiled any logic.")
} else {
build_linked::main(&out_dir, &out_path)
}
}
#[cfg(any(feature = "bundled", feature = "bundled-windows"))]
#[cfg(any(
feature = "bundled",
feature = "bundled-windows",
feature = "bundled-sqlcipher"
))]
mod build_bundled {
use std::env;
use std::path::Path;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use super::{is_compiler, win_target};
pub fn main(out_dir: &str, out_path: &Path) {
if cfg!(feature = "sqlcipher") {
// This is just a sanity check, the top level `main` should ensure this.
panic!("Builds with bundled SQLCipher are not supported");
}
let lib_name = super::lib_name();
if cfg!(feature = "bundled-windows") && !cfg!(feature = "bundled") && !win_target() {
// This is just a sanity check, the top level `main` should ensure this.
@@ -74,19 +93,21 @@ mod build_bundled {
#[cfg(feature = "buildtime_bindgen")]
{
use super::{bindings, HeaderLocation};
let header = HeaderLocation::FromPath("sqlite3/sqlite3.h".to_owned());
let header = HeaderLocation::FromPath(format!("{}/sqlite3.h", lib_name));
bindings::write_to_out_dir(header, out_path);
}
#[cfg(not(feature = "buildtime_bindgen"))]
{
use std::fs;
fs::copy("sqlite3/bindgen_bundled_version.rs", out_path)
fs::copy(format!("{}/bindgen_bundled_version.rs", lib_name), out_path)
.expect("Could not copy bindings to output directory");
}
println!("cargo:rerun-if-changed=sqlite3/sqlite3.c");
// println!("cargo:rerun-if-changed=sqlite3/sqlite3.c");
// println!("cargo:rerun-if-changed=sqlcipher/sqlite3.c");
println!("cargo:rerun-if-changed={}/sqlite3.c", lib_name);
println!("cargo:rerun-if-changed=sqlite3/wasm32-wasi-vfs.c");
let mut cfg = cc::Build::new();
cfg.file("sqlite3/sqlite3.c")
cfg.file(format!("{}/sqlite3.c", lib_name))
.flag("-DSQLITE_CORE")
.flag("-DSQLITE_DEFAULT_FOREIGN_KEYS=1")
.flag("-DSQLITE_ENABLE_API_ARMOR")
@@ -108,6 +129,84 @@ mod build_bundled {
.flag("-D_POSIX_THREAD_SAFE_FUNCTIONS") // cross compile with MinGW
.warnings(false);
if cfg!(feature = "bundled-sqlcipher") {
cfg.flag("-DSQLITE_HAS_CODEC").flag("-DSQLITE_TEMP_STORE=2");
let target = env::var("TARGET").unwrap();
let host = env::var("HOST").unwrap();
let is_windows = host.contains("windows") && target.contains("windows");
let is_apple = host.contains("apple") && target.contains("apple");
let lib_dir = env("OPENSSL_LIB_DIR").map(PathBuf::from);
let inc_dir = env("OPENSSL_INCLUDE_DIR").map(PathBuf::from);
let mut use_openssl = false;
let (lib_dir, inc_dir) = match (lib_dir, inc_dir) {
(Some(lib_dir), Some(inc_dir)) => {
use_openssl = true;
(lib_dir, inc_dir)
}
(lib_dir, inc_dir) => match find_openssl_dir(&host, &target) {
None => {
if is_windows && !cfg!(feature = "bundled-sqlcipher-vendored-openssl") {
panic!("Missing environment variable OPENSSL_DIR or OPENSSL_DIR is not set")
} else {
(PathBuf::new(), PathBuf::new())
}
}
Some(openssl_dir) => {
let lib_dir = lib_dir.unwrap_or_else(|| openssl_dir.join("lib"));
let inc_dir = inc_dir.unwrap_or_else(|| openssl_dir.join("include"));
if !Path::new(&lib_dir).exists() {
panic!(
"OpenSSL library directory does not exist: {}",
lib_dir.to_string_lossy()
);
}
if !Path::new(&inc_dir).exists() {
panic!(
"OpenSSL include directory does not exist: {}",
inc_dir.to_string_lossy()
)
}
use_openssl = true;
(lib_dir, inc_dir)
}
},
};
if cfg!(feature = "bundled-sqlcipher-vendored-openssl") {
cfg.include(std::env::var("DEP_OPENSSL_INCLUDE").unwrap());
println!("cargo:rustc-link-lib=static=crypto"); // cargo will resolve downstream to the static lib in openssl-sys
} else if is_windows {
// FIXME README says that bundled-sqlcipher is Unix only, and the sources are
// configured on a Unix machine. So maybe this should be made unreachable.
cfg.include(inc_dir.to_string_lossy().as_ref());
let mut lib = String::new();
lib.push_str(lib_dir.to_string_lossy().as_ref());
lib.push('\\');
lib.push_str("libeay32.lib");
cfg.flag(&lib);
} else if use_openssl {
cfg.include(inc_dir.to_string_lossy().as_ref());
println!("cargo:rustc-link-lib=dylib=crypto");
println!(
"cargo:rustc-link-search={}",
lib_dir.to_string_lossy().as_ref()
);
} else if is_apple {
cfg.flag("-DSQLCIPHER_CRYPTO_CC");
println!("cargo:rustc-link-lib=framework=SecurityFoundation");
println!("cargo:rustc-link-lib=framework=CoreFoundation");
} else {
println!("cargo:rustc-link-lib=dylib=crypto");
}
}
// on android sqlite can't figure out where to put the temp files.
// the bundled sqlite on android also uses `SQLITE_TEMP_STORE=3`.
// https://android.googlesource.com/platform/external/sqlite/+/2c8c9ae3b7e6f340a19a0001c2a889a211c9d8b2/dist/Android.mk
@@ -183,20 +282,46 @@ mod build_bundled {
}
println!("cargo:rerun-if-env-changed=LIBSQLITE3_FLAGS");
cfg.compile("libsqlite3.a");
cfg.compile(lib_name);
println!("cargo:lib_dir={}", out_dir);
}
fn env(name: &str) -> Option<OsString> {
let prefix = env::var("TARGET").unwrap().to_uppercase().replace("-", "_");
let prefixed = format!("{}_{}", prefix, name);
let var = env::var_os(&prefixed);
match var {
None => env::var_os(name),
_ => var,
}
}
fn find_openssl_dir(_host: &str, _target: &str) -> Option<PathBuf> {
let openssl_dir = env("OPENSSL_DIR");
openssl_dir.map(PathBuf::from)
}
}
fn env_prefix() -> &'static str {
if cfg!(feature = "sqlcipher") {
if cfg!(any(feature = "sqlcipher", feature = "bundled-sqlcipher")) {
"SQLCIPHER"
} else {
"SQLITE3"
}
}
fn lib_name() -> &'static str {
if cfg!(any(feature = "sqlcipher", feature = "bundled-sqlcipher")) {
"sqlcipher"
} else if cfg!(all(windows, feature = "winsqlite3")) {
"winsqlite3"
} else {
"sqlite3"
}
}
pub enum HeaderLocation {
FromEnvironment,
Wrapper,
@@ -227,24 +352,29 @@ mod build_linked {
#[cfg(feature = "vcpkg")]
extern crate vcpkg;
use super::{bindings, env_prefix, is_compiler, win_target, HeaderLocation};
use super::{bindings, env_prefix, is_compiler, lib_name, win_target, HeaderLocation};
use std::env;
use std::path::Path;
pub fn main(_out_dir: &str, out_path: &Path) {
let header = find_sqlite();
if (cfg!(any(feature = "bundled_bindings", feature = "bundled"))
|| (win_target() && cfg!(feature = "bundled-windows")))
if (cfg!(any(
feature = "bundled_bindings",
feature = "bundled",
feature = "bundled-sqlcipher"
)) || (win_target() && cfg!(feature = "bundled-windows")))
&& !cfg!(feature = "buildtime_bindgen")
{
// Generally means the `bundled_bindings` feature is enabled
// (there's also an edge case where we get here involving
// sqlcipher). In either case most users are better off with turning
// Generally means the `bundled_bindings` feature is enabled.
// Most users are better off with turning
// on buildtime_bindgen instead, but this is still supported as we
// have runtime version checks and there are good reasons to not
// want to run bindgen.
std::fs::copy("sqlite3/bindgen_bundled_version.rs", out_path)
.expect("Could not copy bindings to output directory");
std::fs::copy(
format!("{}/bindgen_bundled_version.rs", lib_name()),
out_path,
)
.expect("Could not copy bindings to output directory");
} else {
bindings::write_to_out_dir(header, out_path);
}
@@ -260,7 +390,7 @@ mod build_linked {
}
// Prints the necessary cargo link commands and returns the path to the header.
fn find_sqlite() -> HeaderLocation {
let link_lib = link_lib();
let link_lib = lib_name();
println!("cargo:rerun-if-env-changed={}_INCLUDE_DIR", env_prefix());
println!("cargo:rerun-if-env-changed={}_LIB_DIR", env_prefix());
@@ -324,7 +454,7 @@ mod build_linked {
fn try_vcpkg() -> Option<HeaderLocation> {
if cfg!(feature = "vcpkg") && is_compiler("msvc") {
// See if vcpkg can find it.
if let Ok(mut lib) = vcpkg::Config::new().probe(link_lib()) {
if let Ok(mut lib) = vcpkg::Config::new().probe(lib_name()) {
if let Some(mut header) = lib.include_paths.pop() {
header.push("sqlite3.h");
return Some(HeaderLocation::FromPath(header.to_string_lossy().into()));
@@ -335,20 +465,11 @@ mod build_linked {
None
}
}
fn link_lib() -> &'static str {
if cfg!(feature = "sqlcipher") {
"sqlcipher"
} else if win_target() && cfg!(feature = "winsqlite3") {
"winsqlite3"
} else {
"sqlite3"
}
}
}
#[cfg(not(feature = "buildtime_bindgen"))]
mod bindings {
#![allow(dead_code)]
use super::HeaderLocation;
use std::fs;
@@ -410,10 +531,14 @@ mod bindings {
let header: String = header.into();
let mut output = Vec::new();
let mut bindings = bindgen::builder()
.trust_clang_mangling(false)
.header(header.clone())
.parse_callbacks(Box::new(SqliteTypeChooser))
.rustfmt_bindings(true);
if cfg!(any(feature = "sqlcipher", feature = "bundled-sqlcipher")) {
bindings = bindings.clang_arg("-DSQLITE_HAS_CODEC");
}
if cfg!(feature = "unlock_notify") {
bindings = bindings.clang_arg("-DSQLITE_ENABLE_UNLOCK_NOTIFY");
}