| 1 |
use std::env; |
| 2 |
use std::path::Path; |
| 3 |
|
| 4 |
/// Tells whether we're building for Windows. This is more suitable than a plain |
| 5 |
/// `cfg!(windows)`, since the latter does not properly handle cross-compilation |
| 6 |
/// |
| 7 |
/// Note that there is no way to know at compile-time which system we'll be |
| 8 |
/// targeting, and this test must be made at run-time (of the build script) See |
| 9 |
/// <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts> |
| 10 |
fn win_target() -> bool { |
| 11 |
env::var("CARGO_CFG_WINDOWS").is_ok() |
| 12 |
} |
| 13 |
|
| 14 |
/// Tells whether we're building for Android. |
| 15 |
/// See [`win_target`] |
| 16 |
#[cfg(any(feature = "bundled", feature = "bundled-windows"))] |
| 17 |
fn android_target() -> bool { |
| 18 |
env::var("CARGO_CFG_TARGET_OS").is_ok_and(|v| v == "android") |
| 19 |
} |
| 20 |
|
| 21 |
/// Tells whether a given compiler will be used `compiler_name` is compared to |
| 22 |
/// the content of `CARGO_CFG_TARGET_ENV` (and is always lowercase) |
| 23 |
/// |
| 24 |
/// See [`win_target`] |
| 25 |
fn is_compiler(compiler_name: &str) -> bool { |
| 26 |
env::var("CARGO_CFG_TARGET_ENV").is_ok_and(|v| v == compiler_name) |
| 27 |
} |
| 28 |
|
| 29 |
/// Copy bindgen file from `dir` to `out_path`. |
| 30 |
fn copy_bindings<T: AsRef<Path>>(dir: &str, bindgen_name: &str, out_path: T) { |
| 31 |
let from = if cfg!(feature = "loadable_extension") { |
| 32 |
format!("{dir}/{bindgen_name}_ext.rs") |
| 33 |
} else { |
| 34 |
format!("{dir}/{bindgen_name}.rs") |
| 35 |
}; |
| 36 |
std::fs::copy(from, out_path).expect("Could not copy bindings to output directory"); |
| 37 |
} |
| 38 |
|
| 39 |
fn main() { |
| 40 |
let out_dir = env::var("OUT_DIR").unwrap(); |
| 41 |
let out_path = Path::new(&out_dir).join("bindgen.rs"); |
| 42 |
if cfg!(feature = "in_gecko") { |
| 43 |
// When inside mozilla-central, we are included into the build with |
| 44 |
// sqlite3.o directly, so we don't want to provide any linker arguments. |
| 45 |
copy_bindings("sqlite3", "bindgen_bundled_version", out_path); |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
println!("cargo:rerun-if-env-changed=LIBSQLITE3_SYS_USE_PKG_CONFIG"); |
| 50 |
if env::var_os("LIBSQLITE3_SYS_USE_PKG_CONFIG").is_some_and(|s| s != "0") |
| 51 |
|| cfg!(feature = "loadable_extension") |
| 52 |
{ |
| 53 |
build_linked::main(&out_dir, &out_path); |
| 54 |
} else if cfg!(all( |
| 55 |
feature = "sqlcipher", |
| 56 |
not(feature = "bundled-sqlcipher") |
| 57 |
)) { |
| 58 |
if cfg!(feature = "bundled") || (win_target() && cfg!(feature = "bundled-windows")) { |
| 59 |
println!( |
| 60 |
"cargo:warning=For backwards compatibility, feature 'sqlcipher' overrides |
| 61 |
features 'bundled' and 'bundled-windows'. If you want a bundled build of |
| 62 |
SQLCipher (available for the moment only on Unix), use feature 'bundled-sqlcipher' |
| 63 |
or 'bundled-sqlcipher-vendored-openssl' to also bundle OpenSSL crypto." |
| 64 |
); |
| 65 |
} |
| 66 |
build_linked::main(&out_dir, &out_path); |
| 67 |
} else if cfg!(feature = "bundled") |
| 68 |
|| (win_target() && cfg!(feature = "bundled-windows")) |
| 69 |
|| cfg!(feature = "bundled-sqlcipher") |
| 70 |
{ |
| 71 |
#[cfg(any( |
| 72 |
feature = "bundled", |
| 73 |
feature = "bundled-windows", |
| 74 |
feature = "bundled-sqlcipher" |
| 75 |
))] |
| 76 |
build_bundled::main(&out_dir, &out_path); |
| 77 |
#[cfg(not(any( |
| 78 |
feature = "bundled", |
| 79 |
feature = "bundled-windows", |
| 80 |
feature = "bundled-sqlcipher" |
| 81 |
)))] |
| 82 |
panic!("The runtime test should not run this branch, which has not compiled any logic.") |
| 83 |
} else { |
| 84 |
build_linked::main(&out_dir, &out_path); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
#[cfg(any( |
| 89 |
feature = "bundled", |
| 90 |
feature = "bundled-windows", |
| 91 |
feature = "bundled-sqlcipher" |
| 92 |
))] |
| 93 |
mod build_bundled { |
| 94 |
use std::env; |
| 95 |
use std::ffi::OsString; |
| 96 |
use std::path::{Path, PathBuf}; |
| 97 |
|
| 98 |
use super::{is_compiler, win_target}; |
| 99 |
|
| 100 |
pub fn main(out_dir: &str, out_path: &Path) { |
| 101 |
let lib_name = super::lib_name(); |
| 102 |
|
| 103 |
// This is just a sanity check, the top level `main` should ensure this. |
| 104 |
assert!(!(cfg!(feature = "bundled-windows") && !cfg!(feature = "bundled") && !win_target()), |
| 105 |
"This module should not be used: we're not on Windows and the bundled feature has not been enabled"); |
| 106 |
|
| 107 |
#[cfg(feature = "buildtime_bindgen")] |
| 108 |
{ |
| 109 |
use super::{bindings, HeaderLocation}; |
| 110 |
let header = HeaderLocation::FromPath(lib_name.to_owned()); |
| 111 |
bindings::write_to_out_dir(header, out_path); |
| 112 |
} |
| 113 |
#[cfg(not(feature = "buildtime_bindgen"))] |
| 114 |
{ |
| 115 |
super::copy_bindings(lib_name, "bindgen_bundled_version", out_path); |
| 116 |
} |
| 117 |
println!("cargo:include={}/{lib_name}", env!("CARGO_MANIFEST_DIR")); |
| 118 |
println!("cargo:rerun-if-changed={lib_name}/sqlite3.c"); |
| 119 |
println!("cargo:rerun-if-changed=sqlite3/wasm32-wasi-vfs.c"); |
| 120 |
let mut cfg = cc::Build::new(); |
| 121 |
cfg.file(format!("{lib_name}/sqlite3.c")) |
| 122 |
.flag("-DSQLITE_CORE") |
| 123 |
.flag("-DSQLITE_DEFAULT_FOREIGN_KEYS=1") |
| 124 |
.flag("-DSQLITE_ENABLE_API_ARMOR") |
| 125 |
.flag("-DSQLITE_ENABLE_COLUMN_METADATA") |
| 126 |
.flag("-DSQLITE_ENABLE_DBSTAT_VTAB") |
| 127 |
.flag("-DSQLITE_ENABLE_FTS3") |
| 128 |
.flag("-DSQLITE_ENABLE_FTS3_PARENTHESIS") |
| 129 |
.flag("-DSQLITE_ENABLE_FTS5") |
| 130 |
.flag("-DSQLITE_ENABLE_JSON1") |
| 131 |
.flag("-DSQLITE_ENABLE_LOAD_EXTENSION=1") |
| 132 |
.flag("-DSQLITE_ENABLE_MEMORY_MANAGEMENT") |
| 133 |
.flag("-DSQLITE_ENABLE_RTREE") |
| 134 |
.flag("-DSQLITE_ENABLE_STAT4") |
| 135 |
.flag("-DSQLITE_SOUNDEX") |
| 136 |
.flag("-DSQLITE_THREADSAFE=1") |
| 137 |
.flag("-DSQLITE_USE_URI") |
| 138 |
.flag("-DHAVE_USLEEP=1") |
| 139 |
.flag("-DHAVE_ISNAN") |
| 140 |
.flag("-D_POSIX_THREAD_SAFE_FUNCTIONS") // cross compile with MinGW |
| 141 |
.warnings(false); |
| 142 |
|
| 143 |
if cfg!(feature = "bundled-sqlcipher") { |
| 144 |
cfg.flag("-DSQLITE_HAS_CODEC").flag("-DSQLITE_TEMP_STORE=2"); |
| 145 |
|
| 146 |
let target = env::var("TARGET").unwrap(); |
| 147 |
let host = env::var("HOST").unwrap(); |
| 148 |
|
| 149 |
let is_windows = host.contains("windows") && target.contains("windows"); |
| 150 |
let is_apple = host.contains("apple") && target.contains("apple"); |
| 151 |
|
| 152 |
let lib_dir = env("OPENSSL_LIB_DIR").map(PathBuf::from); |
| 153 |
let inc_dir = env("OPENSSL_INCLUDE_DIR").map(PathBuf::from); |
| 154 |
let mut use_openssl = false; |
| 155 |
|
| 156 |
let (lib_dir, inc_dir) = match (lib_dir, inc_dir) { |
| 157 |
(Some(lib_dir), Some(inc_dir)) => { |
| 158 |
use_openssl = true; |
| 159 |
(vec![lib_dir], inc_dir) |
| 160 |
} |
| 161 |
(lib_dir, inc_dir) => match find_openssl_dir(&host, &target) { |
| 162 |
None => { |
| 163 |
if is_windows && !cfg!(feature = "bundled-sqlcipher-vendored-openssl") { |
| 164 |
panic!("Missing environment variable OPENSSL_DIR or OPENSSL_DIR is not set") |
| 165 |
} else { |
| 166 |
(vec![PathBuf::new()], PathBuf::new()) |
| 167 |
} |
| 168 |
} |
| 169 |
Some(openssl_dir) => { |
| 170 |
let lib_dir = lib_dir.map(|d| vec![d]).unwrap_or_else(|| { |
| 171 |
let mut lib_dirs = vec![]; |
| 172 |
// OpenSSL 3.0 now puts its libraries in lib64/ by default, |
| 173 |
// check for both it and lib/. |
| 174 |
if openssl_dir.join("lib64").exists() { |
| 175 |
lib_dirs.push(openssl_dir.join("lib64")); |
| 176 |
} |
| 177 |
if openssl_dir.join("lib").exists() { |
| 178 |
lib_dirs.push(openssl_dir.join("lib")); |
| 179 |
} |
| 180 |
lib_dirs |
| 181 |
}); |
| 182 |
let inc_dir = inc_dir.unwrap_or_else(|| openssl_dir.join("include")); |
| 183 |
|
| 184 |
if !lib_dir.iter().all(|p| p.exists()) { |
| 185 |
panic!("OpenSSL library directory does not exist: {lib_dir:?}"); |
| 186 |
} |
| 187 |
|
| 188 |
if !Path::new(&inc_dir).exists() { |
| 189 |
panic!( |
| 190 |
"OpenSSL include directory does not exist: {}", |
| 191 |
inc_dir.to_string_lossy() |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
use_openssl = true; |
| 196 |
(lib_dir, inc_dir) |
| 197 |
} |
| 198 |
}, |
| 199 |
}; |
| 200 |
|
| 201 |
if cfg!(feature = "bundled-sqlcipher-vendored-openssl") { |
| 202 |
cfg.include(env::var("DEP_OPENSSL_INCLUDE").unwrap()); |
| 203 |
// cargo will resolve downstream to the static lib in |
| 204 |
// openssl-sys |
| 205 |
} else if use_openssl { |
| 206 |
cfg.include(inc_dir.to_string_lossy().as_ref()); |
| 207 |
let lib_name = if is_windows { "libcrypto" } else { "crypto" }; |
| 208 |
println!("cargo:rustc-link-lib=dylib={lib_name}"); |
| 209 |
for lib_dir_item in &lib_dir { |
| 210 |
println!("cargo:rustc-link-search={}", lib_dir_item.to_string_lossy()); |
| 211 |
} |
| 212 |
} else if is_apple { |
| 213 |
cfg.flag("-DSQLCIPHER_CRYPTO_CC"); |
| 214 |
println!("cargo:rustc-link-lib=framework=Security"); |
| 215 |
println!("cargo:rustc-link-lib=framework=CoreFoundation"); |
| 216 |
} else { |
| 217 |
// branch not taken on Windows, just `crypto` is fine. |
| 218 |
println!("cargo:rustc-link-lib=dylib=crypto"); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
// on android sqlite can't figure out where to put the temp files. |
| 223 |
// the bundled sqlite on android also uses `SQLITE_TEMP_STORE=3`. |
| 224 |
// https://android.googlesource.com/platform/external/sqlite/+/2c8c9ae3b7e6f340a19a0001c2a889a211c9d8b2/dist/Android.mk |
| 225 |
if super::android_target() { |
| 226 |
cfg.flag("-DSQLITE_TEMP_STORE=3"); |
| 227 |
} |
| 228 |
|
| 229 |
if cfg!(feature = "with-asan") { |
| 230 |
cfg.flag("-fsanitize=address"); |
| 231 |
} |
| 232 |
|
| 233 |
// If explicitly requested: enable static linking against the Microsoft Visual |
| 234 |
// C++ Runtime to avoid dependencies on vcruntime140.dll and similar libraries. |
| 235 |
if cfg!(target_feature = "crt-static") && is_compiler("msvc") { |
| 236 |
cfg.static_crt(true); |
| 237 |
} |
| 238 |
|
| 239 |
if !win_target() { |
| 240 |
cfg.flag("-DHAVE_LOCALTIME_R"); |
| 241 |
} |
| 242 |
if env::var("TARGET").is_ok_and(|v| v.starts_with("wasm32-wasi")) { |
| 243 |
cfg.flag("-USQLITE_THREADSAFE") |
| 244 |
.flag("-DSQLITE_THREADSAFE=0") |
| 245 |
// https://github.com/rust-lang/rust/issues/74393 |
| 246 |
.flag("-DLONGDOUBLE_TYPE=double") |
| 247 |
.flag("-D_WASI_EMULATED_MMAN") |
| 248 |
.flag("-D_WASI_EMULATED_GETPID") |
| 249 |
.flag("-D_WASI_EMULATED_SIGNAL") |
| 250 |
.flag("-D_WASI_EMULATED_PROCESS_CLOCKS"); |
| 251 |
|
| 252 |
if cfg!(feature = "wasm32-wasi-vfs") { |
| 253 |
cfg.file("sqlite3/wasm32-wasi-vfs.c"); |
| 254 |
} |
| 255 |
} |
| 256 |
if cfg!(feature = "unlock_notify") { |
| 257 |
cfg.flag("-DSQLITE_ENABLE_UNLOCK_NOTIFY"); |
| 258 |
} |
| 259 |
if cfg!(feature = "preupdate_hook") { |
| 260 |
cfg.flag("-DSQLITE_ENABLE_PREUPDATE_HOOK"); |
| 261 |
} |
| 262 |
if cfg!(feature = "session") { |
| 263 |
cfg.flag("-DSQLITE_ENABLE_SESSION"); |
| 264 |
} |
| 265 |
|
| 266 |
if let Ok(limit) = env::var("SQLITE_MAX_VARIABLE_NUMBER") { |
| 267 |
cfg.flag(format!("-DSQLITE_MAX_VARIABLE_NUMBER={limit}")); |
| 268 |
} |
| 269 |
println!("cargo:rerun-if-env-changed=SQLITE_MAX_VARIABLE_NUMBER"); |
| 270 |
|
| 271 |
if let Ok(limit) = env::var("SQLITE_MAX_EXPR_DEPTH") { |
| 272 |
cfg.flag(format!("-DSQLITE_MAX_EXPR_DEPTH={limit}")); |
| 273 |
} |
| 274 |
println!("cargo:rerun-if-env-changed=SQLITE_MAX_EXPR_DEPTH"); |
| 275 |
|
| 276 |
if let Ok(limit) = env::var("SQLITE_MAX_COLUMN") { |
| 277 |
cfg.flag(format!("-DSQLITE_MAX_COLUMN={limit}")); |
| 278 |
} |
| 279 |
println!("cargo:rerun-if-env-changed=SQLITE_MAX_COLUMN"); |
| 280 |
|
| 281 |
if let Ok(extras) = env::var("LIBSQLITE3_FLAGS") { |
| 282 |
for extra in extras.split_whitespace() { |
| 283 |
if extra.starts_with("-D") || extra.starts_with("-U") { |
| 284 |
cfg.flag(extra); |
| 285 |
} else if extra.starts_with("SQLITE_") { |
| 286 |
cfg.flag(format!("-D{extra}")); |
| 287 |
} else { |
| 288 |
panic!("Don't understand {extra} in LIBSQLITE3_FLAGS"); |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
println!("cargo:rerun-if-env-changed=LIBSQLITE3_FLAGS"); |
| 293 |
|
| 294 |
cfg.compile(lib_name); |
| 295 |
|
| 296 |
println!("cargo:lib_dir={out_dir}"); |
| 297 |
} |
| 298 |
|
| 299 |
fn env(name: &str) -> Option<OsString> { |
| 300 |
let prefix = env::var("TARGET").unwrap().to_uppercase().replace('-', "_"); |
| 301 |
let prefixed = format!("{prefix}_{name}"); |
| 302 |
let var = env::var_os(prefixed); |
| 303 |
|
| 304 |
match var { |
| 305 |
None => env::var_os(name), |
| 306 |
_ => var, |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
fn find_openssl_dir(_host: &str, _target: &str) -> Option<PathBuf> { |
| 311 |
let openssl_dir = env("OPENSSL_DIR"); |
| 312 |
openssl_dir.map(PathBuf::from) |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
fn env_prefix() -> &'static str { |
| 317 |
if cfg!(any(feature = "sqlcipher", feature = "bundled-sqlcipher")) { |
| 318 |
"SQLCIPHER" |
| 319 |
} else { |
| 320 |
"SQLITE3" |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
fn lib_name() -> &'static str { |
| 325 |
if cfg!(any(feature = "sqlcipher", feature = "bundled-sqlcipher")) { |
| 326 |
"sqlcipher" |
| 327 |
} else { |
| 328 |
"sqlite3" |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
pub enum HeaderLocation { |
| 333 |
FromEnvironment, |
| 334 |
Wrapper, |
| 335 |
FromPath(String), |
| 336 |
} |
| 337 |
|
| 338 |
impl From<HeaderLocation> for String { |
| 339 |
fn from(header: HeaderLocation) -> Self { |
| 340 |
match header { |
| 341 |
HeaderLocation::FromEnvironment => { |
| 342 |
let prefix = env_prefix(); |
| 343 |
let mut header = env::var(format!("{prefix}_INCLUDE_DIR")).unwrap_or_else(|_| { |
| 344 |
panic!("{prefix}_INCLUDE_DIR must be set if {prefix}_LIB_DIR is set") |
| 345 |
}); |
| 346 |
header.push_str(if cfg!(feature = "loadable_extension") { |
| 347 |
"/sqlite3ext.h" |
| 348 |
} else { |
| 349 |
"/sqlite3.h" |
| 350 |
}); |
| 351 |
header |
| 352 |
} |
| 353 |
HeaderLocation::Wrapper => if cfg!(feature = "loadable_extension") { |
| 354 |
"wrapper_ext.h" |
| 355 |
} else { |
| 356 |
"wrapper.h" |
| 357 |
} |
| 358 |
.into(), |
| 359 |
HeaderLocation::FromPath(path) => format!( |
| 360 |
"{}/{}", |
| 361 |
path, |
| 362 |
if cfg!(feature = "loadable_extension") { |
| 363 |
"sqlite3ext.h" |
| 364 |
} else { |
| 365 |
"sqlite3.h" |
| 366 |
} |
| 367 |
), |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
mod build_linked { |
| 373 |
#[cfg(feature = "vcpkg")] |
| 374 |
extern crate vcpkg; |
| 375 |
|
| 376 |
use super::{bindings, env_prefix, is_compiler, lib_name, win_target, HeaderLocation}; |
| 377 |
use std::env; |
| 378 |
use std::path::Path; |
| 379 |
|
| 380 |
pub fn main(_out_dir: &str, out_path: &Path) { |
| 381 |
let header = find_sqlite(); |
| 382 |
if (cfg!(any( |
| 383 |
feature = "bundled_bindings", |
| 384 |
feature = "bundled", |
| 385 |
feature = "bundled-sqlcipher" |
| 386 |
)) || (win_target() && cfg!(feature = "bundled-windows"))) |
| 387 |
&& !cfg!(feature = "buildtime_bindgen") |
| 388 |
{ |
| 389 |
// Generally means the `bundled_bindings` feature is enabled. |
| 390 |
// Most users are better off with turning |
| 391 |
// on buildtime_bindgen instead, but this is still supported as we |
| 392 |
// have runtime version checks and there are good reasons to not |
| 393 |
// want to run bindgen. |
| 394 |
super::copy_bindings(lib_name(), "bindgen_bundled_version", out_path); |
| 395 |
} else { |
| 396 |
bindings::write_to_out_dir(header, out_path); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
#[cfg(not(feature = "loadable_extension"))] |
| 401 |
fn find_link_mode() -> &'static str { |
| 402 |
// If the user specifies SQLITE3_STATIC (or SQLCIPHER_STATIC), do static |
| 403 |
// linking, unless it's explicitly set to 0. |
| 404 |
match &env::var(format!("{}_STATIC", env_prefix())) { |
| 405 |
Ok(v) if v != "0" => "static", |
| 406 |
_ => "dylib", |
| 407 |
} |
| 408 |
} |
| 409 |
// Prints the necessary cargo link commands and returns the path to the header. |
| 410 |
fn find_sqlite() -> HeaderLocation { |
| 411 |
let link_lib = lib_name(); |
| 412 |
|
| 413 |
println!("cargo:rerun-if-env-changed={}_INCLUDE_DIR", env_prefix()); |
| 414 |
println!("cargo:rerun-if-env-changed={}_LIB_DIR", env_prefix()); |
| 415 |
println!("cargo:rerun-if-env-changed={}_STATIC", env_prefix()); |
| 416 |
if cfg!(feature = "vcpkg") && is_compiler("msvc") { |
| 417 |
println!("cargo:rerun-if-env-changed=VCPKGRS_DYNAMIC"); |
| 418 |
} |
| 419 |
|
| 420 |
// dependents can access `DEP_SQLITE3_LINK_TARGET` (`sqlite3` being the |
| 421 |
// `links=` value in our Cargo.toml) to get this value. This might be |
| 422 |
// useful if you need to ensure whatever crypto library sqlcipher relies |
| 423 |
// on is available, for example. |
| 424 |
#[cfg(not(feature = "loadable_extension"))] |
| 425 |
println!("cargo:link-target={link_lib}"); |
| 426 |
|
| 427 |
// Allow users to specify where to find SQLite. |
| 428 |
if let Ok(dir) = env::var(format!("{}_LIB_DIR", env_prefix())) { |
| 429 |
// Try to use pkg-config to determine link commands |
| 430 |
let pkgconfig_path = Path::new(&dir).join("pkgconfig"); |
| 431 |
env::set_var("PKG_CONFIG_PATH", pkgconfig_path); |
| 432 |
#[cfg(not(feature = "loadable_extension"))] |
| 433 |
if pkg_config::Config::new().probe(link_lib).is_err() { |
| 434 |
// Otherwise just emit the bare minimum link commands. |
| 435 |
println!("cargo:rustc-link-lib={}={link_lib}", find_link_mode()); |
| 436 |
println!("cargo:rustc-link-search={dir}"); |
| 437 |
} |
| 438 |
return HeaderLocation::FromEnvironment; |
| 439 |
} |
| 440 |
|
| 441 |
if let Some(header) = try_vcpkg() { |
| 442 |
return header; |
| 443 |
} |
| 444 |
|
| 445 |
// See if pkg-config can do everything for us. |
| 446 |
if let Ok(mut lib) = pkg_config::Config::new() |
| 447 |
.print_system_libs(false) |
| 448 |
.probe(link_lib) |
| 449 |
{ |
| 450 |
if let Some(header) = lib.include_paths.pop() { |
| 451 |
HeaderLocation::FromPath(header.to_string_lossy().into()) |
| 452 |
} else { |
| 453 |
HeaderLocation::Wrapper |
| 454 |
} |
| 455 |
} else { |
| 456 |
// No env var set and pkg-config couldn't help; just output the link-lib |
| 457 |
// request and hope that the library exists on the system paths. We used to |
| 458 |
// output /usr/lib explicitly, but that can introduce other linking problems; |
| 459 |
// see https://github.com/rusqlite/rusqlite/issues/207. |
| 460 |
#[cfg(not(feature = "loadable_extension"))] |
| 461 |
println!("cargo:rustc-link-lib={}={link_lib}", find_link_mode()); |
| 462 |
HeaderLocation::Wrapper |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
fn try_vcpkg() -> Option<HeaderLocation> { |
| 467 |
if cfg!(feature = "vcpkg") && is_compiler("msvc") { |
| 468 |
// See if vcpkg can find it. |
| 469 |
if let Ok(mut lib) = vcpkg::Config::new().probe(lib_name()) { |
| 470 |
if let Some(header) = lib.include_paths.pop() { |
| 471 |
return Some(HeaderLocation::FromPath(header.to_string_lossy().into())); |
| 472 |
} |
| 473 |
} |
| 474 |
None |
| 475 |
} else { |
| 476 |
None |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
#[cfg(not(feature = "buildtime_bindgen"))] |
| 482 |
#[allow(dead_code)] |
| 483 |
mod bindings { |
| 484 |
use super::HeaderLocation; |
| 485 |
|
| 486 |
use std::path::Path; |
| 487 |
|
| 488 |
static PREBUILT_BINDGENS: &[&str] = &["bindgen_3.14.0"]; |
| 489 |
|
| 490 |
pub fn write_to_out_dir(_header: HeaderLocation, out_path: &Path) { |
| 491 |
let name = PREBUILT_BINDGENS[PREBUILT_BINDGENS.len() - 1]; |
| 492 |
super::copy_bindings("bindgen-bindings", name, out_path); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
#[cfg(feature = "buildtime_bindgen")] |
| 497 |
mod bindings { |
| 498 |
use super::HeaderLocation; |
| 499 |
use bindgen::callbacks::{IntKind, ParseCallbacks}; |
| 500 |
|
| 501 |
use std::path::Path; |
| 502 |
#[derive(Debug)] |
| 503 |
struct SqliteTypeChooser; |
| 504 |
|
| 505 |
impl ParseCallbacks for SqliteTypeChooser { |
| 506 |
fn int_macro(&self, name: &str, _value: i64) -> Option<IntKind> { |
| 507 |
if name == "SQLITE_SERIALIZE_NOCOPY" |
| 508 |
|| name.starts_with("SQLITE_DESERIALIZE_") |
| 509 |
|| name.starts_with("SQLITE_PREPARE_") |
| 510 |
|| name.starts_with("SQLITE_TRACE_") |
| 511 |
{ |
| 512 |
Some(IntKind::UInt) |
| 513 |
} else { |
| 514 |
None |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
// Are we generating the bundled bindings? Used to avoid emitting things |
| 520 |
// that would be problematic in bundled builds. This env var is set by |
| 521 |
// `upgrade.sh`. |
| 522 |
fn generating_bundled_bindings() -> bool { |
| 523 |
// Hacky way to know if we're generating the bundled bindings |
| 524 |
println!("cargo:rerun-if-env-changed=LIBSQLITE3_SYS_BUNDLING"); |
| 525 |
match std::env::var("LIBSQLITE3_SYS_BUNDLING") { |
| 526 |
Ok(v) => v != "0", |
| 527 |
Err(_) => false, |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
pub fn write_to_out_dir(header: HeaderLocation, out_path: &Path) { |
| 532 |
let header: String = header.into(); |
| 533 |
let mut bindings = bindgen::builder() |
| 534 |
.default_macro_constant_type(bindgen::MacroTypeVariation::Signed) |
| 535 |
.disable_nested_struct_naming() |
| 536 |
.generate_cstr(true) |
| 537 |
.trust_clang_mangling(false) |
| 538 |
.header(header.clone()) |
| 539 |
.parse_callbacks(Box::new(SqliteTypeChooser)); |
| 540 |
if cfg!(feature = "loadable_extension") { |
| 541 |
bindings = bindings.ignore_functions(); // see generate_functions |
| 542 |
} else { |
| 543 |
bindings = bindings |
| 544 |
.blocklist_function("sqlite3_auto_extension") |
| 545 |
.raw_line( |
| 546 |
r#"extern "C" { |
| 547 |
pub fn sqlite3_auto_extension( |
| 548 |
xEntryPoint: ::std::option::Option< |
| 549 |
unsafe extern "C" fn( |
| 550 |
db: *mut sqlite3, |
| 551 |
pzErrMsg: *mut *mut ::std::os::raw::c_char, |
| 552 |
_: *const sqlite3_api_routines, |
| 553 |
) -> ::std::os::raw::c_int, |
| 554 |
>, |
| 555 |
) -> ::std::os::raw::c_int; |
| 556 |
}"#, |
| 557 |
) |
| 558 |
.blocklist_function("sqlite3_cancel_auto_extension") |
| 559 |
.raw_line( |
| 560 |
r#"extern "C" { |
| 561 |
pub fn sqlite3_cancel_auto_extension( |
| 562 |
xEntryPoint: ::std::option::Option< |
| 563 |
unsafe extern "C" fn( |
| 564 |
db: *mut sqlite3, |
| 565 |
pzErrMsg: *mut *mut ::std::os::raw::c_char, |
| 566 |
_: *const sqlite3_api_routines, |
| 567 |
) -> ::std::os::raw::c_int, |
| 568 |
>, |
| 569 |
) -> ::std::os::raw::c_int; |
| 570 |
}"#, |
| 571 |
) |
| 572 |
.blocklist_function(".*16.*") |
| 573 |
.blocklist_function("sqlite3_close_v2") |
| 574 |
.blocklist_function("sqlite3_create_collation") |
| 575 |
.blocklist_function("sqlite3_create_function") |
| 576 |
.blocklist_function("sqlite3_create_module") |
| 577 |
.blocklist_function("sqlite3_prepare"); |
| 578 |
} |
| 579 |
|
| 580 |
if cfg!(any(feature = "sqlcipher", feature = "bundled-sqlcipher")) { |
| 581 |
bindings = bindings.clang_arg("-DSQLITE_HAS_CODEC"); |
| 582 |
} |
| 583 |
if cfg!(feature = "unlock_notify") { |
| 584 |
bindings = bindings.clang_arg("-DSQLITE_ENABLE_UNLOCK_NOTIFY"); |
| 585 |
} |
| 586 |
if cfg!(feature = "preupdate_hook") { |
| 587 |
bindings = bindings.clang_arg("-DSQLITE_ENABLE_PREUPDATE_HOOK"); |
| 588 |
} |
| 589 |
if cfg!(feature = "session") { |
| 590 |
bindings = bindings.clang_arg("-DSQLITE_ENABLE_SESSION"); |
| 591 |
} |
| 592 |
|
| 593 |
// When cross compiling unless effort is taken to fix the issue, bindgen |
| 594 |
// will find the wrong headers. There's only one header included by the |
| 595 |
// amalgamated `sqlite.h`: `stdarg.h`. |
| 596 |
// |
| 597 |
// Thankfully, there's almost no case where rust code needs to use |
| 598 |
// functions taking `va_list` (It's nearly impossible to get a `va_list` |
| 599 |
// in Rust unless you get passed it by C code for some reason). |
| 600 |
// |
| 601 |
// Arguably, we should never be including these, but we include them for |
| 602 |
// the cases where they aren't totally broken... |
| 603 |
let target_arch = std::env::var("TARGET").unwrap(); |
| 604 |
let host_arch = std::env::var("HOST").unwrap(); |
| 605 |
let is_cross_compiling = target_arch != host_arch; |
| 606 |
|
| 607 |
// Note that when generating the bundled file, we're essentially always |
| 608 |
// cross compiling. |
| 609 |
if generating_bundled_bindings() || is_cross_compiling { |
| 610 |
// Get rid of va_list, as it's not |
| 611 |
bindings = bindings |
| 612 |
.blocklist_function("sqlite3_vmprintf") |
| 613 |
.blocklist_function("sqlite3_vsnprintf") |
| 614 |
.blocklist_function("sqlite3_str_vappendf") |
| 615 |
.blocklist_type("va_list") |
| 616 |
.blocklist_item("__.*"); |
| 617 |
} |
| 618 |
|
| 619 |
let bindings = bindings |
| 620 |
.layout_tests(false) |
| 621 |
.generate() |
| 622 |
.unwrap_or_else(|_| panic!("could not run bindgen on header {}", header)); |
| 623 |
|
| 624 |
#[cfg(feature = "loadable_extension")] |
| 625 |
{ |
| 626 |
let mut output = Vec::new(); |
| 627 |
bindings |
| 628 |
.write(Box::new(&mut output)) |
| 629 |
.expect("could not write output of bindgen"); |
| 630 |
let mut output = String::from_utf8(output).expect("bindgen output was not UTF-8?!"); |
| 631 |
super::loadable_extension::generate_functions(&mut output); |
| 632 |
std::fs::write(out_path, output.as_bytes()) |
| 633 |
.unwrap_or_else(|_| panic!("Could not write to {:?}", out_path)); |
| 634 |
} |
| 635 |
#[cfg(not(feature = "loadable_extension"))] |
| 636 |
bindings |
| 637 |
.write_to_file(out_path) |
| 638 |
.unwrap_or_else(|_| panic!("Could not write to {:?}", out_path)); |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
#[cfg(all(feature = "buildtime_bindgen", feature = "loadable_extension"))] |
| 643 |
mod loadable_extension { |
| 644 |
/// try to generate similar rust code for all `#define sqlite3_xyz |
| 645 |
/// sqlite3_api->abc` macros` in sqlite3ext.h |
| 646 |
pub fn generate_functions(output: &mut String) { |
| 647 |
// (1) parse sqlite3_api_routines fields from bindgen output |
| 648 |
let ast: syn::File = syn::parse_str(output).expect("could not parse bindgen output"); |
| 649 |
let sqlite3_api_routines: syn::ItemStruct = ast |
| 650 |
.items |
| 651 |
.into_iter() |
| 652 |
.find_map(|i| { |
| 653 |
if let syn::Item::Struct(s) = i { |
| 654 |
if s.ident == "sqlite3_api_routines" { |
| 655 |
Some(s) |
| 656 |
} else { |
| 657 |
None |
| 658 |
} |
| 659 |
} else { |
| 660 |
None |
| 661 |
} |
| 662 |
}) |
| 663 |
.expect("could not find sqlite3_api_routines"); |
| 664 |
let sqlite3_api_routines_ident = sqlite3_api_routines.ident; |
| 665 |
let p_api = quote::format_ident!("p_api"); |
| 666 |
let mut stores = Vec::new(); |
| 667 |
let mut malloc = Vec::new(); |
| 668 |
// (2) `#define sqlite3_xyz sqlite3_api->abc` => `pub unsafe fn |
| 669 |
// sqlite3_xyz(args) -> ty {...}` for each `abc` field: |
| 670 |
for field in sqlite3_api_routines.fields { |
| 671 |
let ident = field.ident.expect("unnamed field"); |
| 672 |
let span = ident.span(); |
| 673 |
let name = ident.to_string(); |
| 674 |
if name.contains("16") { |
| 675 |
continue; // skip UTF-16 api as rust uses UTF-8 |
| 676 |
} else if name == "vmprintf" || name == "xvsnprintf" || name == "str_vappendf" { |
| 677 |
continue; // skip va_list |
| 678 |
} else if name == "aggregate_count" |
| 679 |
|| name == "expired" |
| 680 |
|| name == "global_recover" |
| 681 |
|| name == "thread_cleanup" |
| 682 |
|| name == "transfer_bindings" |
| 683 |
|| name == "create_collation" |
| 684 |
|| name == "create_function" |
| 685 |
|| name == "create_module" |
| 686 |
|| name == "prepare" |
| 687 |
|| name == "close_v2" |
| 688 |
{ |
| 689 |
continue; // omit deprecated |
| 690 |
} |
| 691 |
let sqlite3_name = match name.as_ref() { |
| 692 |
"xthreadsafe" => "sqlite3_threadsafe".to_owned(), |
| 693 |
"interruptx" => "sqlite3_interrupt".to_owned(), |
| 694 |
_ => { |
| 695 |
format!("sqlite3_{name}") |
| 696 |
} |
| 697 |
}; |
| 698 |
let ptr_name = |
| 699 |
syn::Ident::new(format!("__{}", sqlite3_name.to_uppercase()).as_ref(), span); |
| 700 |
let sqlite3_fn_name = syn::Ident::new(&sqlite3_name, span); |
| 701 |
let method = |
| 702 |
extract_method(&field.ty).unwrap_or_else(|| panic!("unexpected type for {name}")); |
| 703 |
let arg_names: syn::punctuated::Punctuated<&syn::Ident, syn::token::Comma> = method |
| 704 |
.inputs |
| 705 |
.iter() |
| 706 |
.map(|i| &i.name.as_ref().unwrap().0) |
| 707 |
.collect(); |
| 708 |
let args = &method.inputs; |
| 709 |
// vtab_config/sqlite3_vtab_config: ok |
| 710 |
let varargs = &method.variadic; |
| 711 |
if varargs.is_some() && "db_config" != name && "log" != name && "vtab_config" != name { |
| 712 |
continue; // skip ... |
| 713 |
} |
| 714 |
let ty = &method.output; |
| 715 |
let tokens = if "db_config" == name { |
| 716 |
quote::quote! { |
| 717 |
static #ptr_name: ::std::sync::atomic::AtomicPtr<()> = ::std::sync::atomic::AtomicPtr::new(::std::ptr::null_mut()); |
| 718 |
pub unsafe fn #sqlite3_fn_name(#args arg3: ::std::os::raw::c_int, arg4: *mut ::std::os::raw::c_int) #ty { |
| 719 |
let ptr = #ptr_name.load(::std::sync::atomic::Ordering::Acquire); |
| 720 |
assert!(!ptr.is_null(), "SQLite API not initialized"); |
| 721 |
let fun: unsafe extern "C" fn(#args #varargs) #ty = ::std::mem::transmute(ptr); |
| 722 |
(fun)(#arg_names, arg3, arg4) |
| 723 |
} |
| 724 |
} |
| 725 |
} else if "log" == name { |
| 726 |
quote::quote! { |
| 727 |
static #ptr_name: ::std::sync::atomic::AtomicPtr<()> = ::std::sync::atomic::AtomicPtr::new(::std::ptr::null_mut()); |
| 728 |
pub unsafe fn #sqlite3_fn_name(#args arg3: *const ::std::os::raw::c_char) #ty { |
| 729 |
let ptr = #ptr_name.load(::std::sync::atomic::Ordering::Acquire); |
| 730 |
assert!(!ptr.is_null(), "SQLite API not initialized"); |
| 731 |
let fun: unsafe extern "C" fn(#args #varargs) #ty = ::std::mem::transmute(ptr); |
| 732 |
(fun)(#arg_names, arg3) |
| 733 |
} |
| 734 |
} |
| 735 |
} else { |
| 736 |
quote::quote! { |
| 737 |
static #ptr_name: ::std::sync::atomic::AtomicPtr<()> = ::std::sync::atomic::AtomicPtr::new(::std::ptr::null_mut()); |
| 738 |
pub unsafe fn #sqlite3_fn_name(#args) #ty { |
| 739 |
let ptr = #ptr_name.load(::std::sync::atomic::Ordering::Acquire); |
| 740 |
assert!(!ptr.is_null(), "SQLite API not initialized or SQLite feature omitted"); |
| 741 |
let fun: unsafe extern "C" fn(#args #varargs) #ty = ::std::mem::transmute(ptr); |
| 742 |
(fun)(#arg_names) |
| 743 |
} |
| 744 |
} |
| 745 |
}; |
| 746 |
output.push_str(&prettyplease::unparse( |
| 747 |
&syn::parse2(tokens).expect("could not parse quote output"), |
| 748 |
)); |
| 749 |
output.push('\n'); |
| 750 |
if name == "malloc" { |
| 751 |
&mut malloc |
| 752 |
} else { |
| 753 |
&mut stores |
| 754 |
} |
| 755 |
.push(quote::quote! { |
| 756 |
if let Some(fun) = (*#p_api).#ident { |
| 757 |
#ptr_name.store( |
| 758 |
fun as usize as *mut (), |
| 759 |
::std::sync::atomic::Ordering::Release, |
| 760 |
); |
| 761 |
} |
| 762 |
}); |
| 763 |
} |
| 764 |
// (3) generate rust code similar to SQLITE_EXTENSION_INIT2 macro |
| 765 |
let tokens = quote::quote! { |
| 766 |
/// Like SQLITE_EXTENSION_INIT2 macro |
| 767 |
pub unsafe fn rusqlite_extension_init2(#p_api: *mut #sqlite3_api_routines_ident) -> ::std::result::Result<(),crate::InitError> { |
| 768 |
#(#malloc)* // sqlite3_malloc needed by to_sqlite_error |
| 769 |
if let Some(fun) = (*#p_api).libversion_number { |
| 770 |
let version = fun(); |
| 771 |
if SQLITE_VERSION_NUMBER > version { |
| 772 |
return Err(crate::InitError::VersionMismatch{compile_time: SQLITE_VERSION_NUMBER, runtime: version}); |
| 773 |
} |
| 774 |
} else { |
| 775 |
return Err(crate::InitError::NullFunctionPointer); |
| 776 |
} |
| 777 |
#(#stores)* |
| 778 |
Ok(()) |
| 779 |
} |
| 780 |
}; |
| 781 |
output.push_str(&prettyplease::unparse( |
| 782 |
&syn::parse2(tokens).expect("could not parse quote output"), |
| 783 |
)); |
| 784 |
output.push('\n'); |
| 785 |
} |
| 786 |
|
| 787 |
fn extract_method(ty: &syn::Type) -> Option<&syn::TypeBareFn> { |
| 788 |
match ty { |
| 789 |
syn::Type::Path(tp) => tp.path.segments.last(), |
| 790 |
_ => None, |
| 791 |
} |
| 792 |
.map(|seg| match &seg.arguments { |
| 793 |
syn::PathArguments::AngleBracketed(args) => args.args.first(), |
| 794 |
_ => None, |
| 795 |
})? |
| 796 |
.map(|arg| match arg { |
| 797 |
syn::GenericArgument::Type(t) => Some(t), |
| 798 |
_ => None, |
| 799 |
})? |
| 800 |
.map(|ty| match ty { |
| 801 |
syn::Type::BareFn(r) => Some(r), |
| 802 |
_ => None, |
| 803 |
})? |
| 804 |
} |
| 805 |
} |