2018-10-29 01:16:48 +08:00
|
|
|
use std::env;
|
|
|
|
use std::path::Path;
|
|
|
|
|
2021-05-28 15:00:39 +08:00
|
|
|
/// Tells whether we're building for Windows. This is more suitable than a plain
|
|
|
|
/// `cfg!(windows)`, since the latter does not properly handle cross-compilation
|
|
|
|
///
|
|
|
|
/// Note that there is no way to know at compile-time which system we'll be
|
|
|
|
/// targetting, and this test must be made at run-time (of the build script) See
|
|
|
|
/// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
|
|
|
|
fn win_target() -> bool {
|
2022-05-29 19:33:51 +08:00
|
|
|
env::var("CARGO_CFG_WINDOWS").is_ok()
|
2021-05-28 15:00:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tells whether we're building for Android.
|
|
|
|
/// See [`win_target`]
|
|
|
|
#[cfg(any(feature = "bundled", feature = "bundled-windows"))]
|
|
|
|
fn android_target() -> bool {
|
2022-05-29 19:33:51 +08:00
|
|
|
env::var("CARGO_CFG_TARGET_OS").map_or(false, |v| v == "android")
|
2021-05-28 15:00:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tells whether a given compiler will be used `compiler_name` is compared to
|
|
|
|
/// the content of `CARGO_CFG_TARGET_ENV` (and is always lowercase)
|
|
|
|
///
|
|
|
|
/// See [`win_target`]
|
|
|
|
fn is_compiler(compiler_name: &str) -> bool {
|
2022-05-29 19:33:51 +08:00
|
|
|
env::var("CARGO_CFG_TARGET_ENV").map_or(false, |v| v == compiler_name)
|
2021-05-28 15:00:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-09 15:21:40 +08:00
|
|
|
/// Copy bindgen file from `dir` to `out_path`.
|
|
|
|
fn copy_bindings<T: AsRef<Path>>(dir: &str, bindgen_name: &str, out_path: T) {
|
2023-07-09 16:39:21 +08:00
|
|
|
let from = if cfg!(feature = "loadable_extension") {
|
|
|
|
format!("{dir}/{bindgen_name}_ext.rs")
|
|
|
|
} else {
|
|
|
|
format!("{dir}/{bindgen_name}.rs")
|
|
|
|
};
|
|
|
|
std::fs::copy(from, out_path).expect("Could not copy bindings to output directory");
|
2023-07-09 15:21:40 +08:00
|
|
|
}
|
|
|
|
|
2017-03-04 03:57:40 +08:00
|
|
|
fn main() {
|
2018-10-29 01:16:48 +08:00
|
|
|
let out_dir = env::var("OUT_DIR").unwrap();
|
|
|
|
let out_path = Path::new(&out_dir).join("bindgen.rs");
|
2020-04-03 00:12:36 +08:00
|
|
|
if cfg!(feature = "in_gecko") {
|
|
|
|
// When inside mozilla-central, we are included into the build with
|
|
|
|
// sqlite3.o directly, so we don't want to provide any linker arguments.
|
2023-07-09 16:39:21 +08:00
|
|
|
copy_bindings("sqlite3", "bindgen_bundled_version", out_path);
|
2020-04-03 00:12:36 +08:00
|
|
|
return;
|
|
|
|
}
|
2023-04-17 03:53:40 +08:00
|
|
|
|
|
|
|
println!("cargo:rerun-if-env-changed=LIBSQLITE3_SYS_USE_PKG_CONFIG");
|
2023-07-09 16:39:21 +08:00
|
|
|
if env::var_os("LIBSQLITE3_SYS_USE_PKG_CONFIG").map_or(false, |s| s != "0")
|
|
|
|
|| cfg!(feature = "loadable_extension")
|
|
|
|
{
|
2023-04-17 03:53:40 +08:00
|
|
|
build_linked::main(&out_dir, &out_path);
|
|
|
|
} else if cfg!(all(
|
2021-06-03 03:07:56 +08:00
|
|
|
feature = "sqlcipher",
|
|
|
|
not(feature = "bundled-sqlcipher")
|
|
|
|
)) {
|
2021-05-28 15:00:39 +08:00
|
|
|
if cfg!(feature = "bundled") || (win_target() && cfg!(feature = "bundled-windows")) {
|
2019-04-20 01:22:03 +08:00
|
|
|
println!(
|
2021-06-03 03:07:56 +08:00
|
|
|
"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."
|
2022-01-06 02:53:49 +08:00
|
|
|
);
|
2019-04-20 01:22:03 +08:00
|
|
|
}
|
2022-01-06 02:53:49 +08:00
|
|
|
build_linked::main(&out_dir, &out_path);
|
2021-06-03 03:07:56 +08:00
|
|
|
} 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"
|
|
|
|
))]
|
2021-05-28 15:00:39 +08:00
|
|
|
build_bundled::main(&out_dir, &out_path);
|
2021-06-03 03:07:56 +08:00
|
|
|
#[cfg(not(any(
|
|
|
|
feature = "bundled",
|
|
|
|
feature = "bundled-windows",
|
|
|
|
feature = "bundled-sqlcipher"
|
|
|
|
)))]
|
2021-05-28 15:00:39 +08:00
|
|
|
panic!("The runtime test should not run this branch, which has not compiled any logic.")
|
2019-04-20 01:22:03 +08:00
|
|
|
} else {
|
2022-01-06 02:53:49 +08:00
|
|
|
build_linked::main(&out_dir, &out_path);
|
2019-04-20 01:22:03 +08:00
|
|
|
}
|
2017-02-08 09:37:52 +08:00
|
|
|
}
|
|
|
|
|
2021-06-03 03:07:56 +08:00
|
|
|
#[cfg(any(
|
|
|
|
feature = "bundled",
|
|
|
|
feature = "bundled-windows",
|
|
|
|
feature = "bundled-sqlcipher"
|
|
|
|
))]
|
2019-04-20 01:22:03 +08:00
|
|
|
mod build_bundled {
|
2019-05-16 00:41:23 +08:00
|
|
|
use std::env;
|
2021-06-03 03:07:56 +08:00
|
|
|
use std::ffi::OsString;
|
|
|
|
use std::path::{Path, PathBuf};
|
2017-02-09 10:41:34 +08:00
|
|
|
|
2021-05-28 15:00:39 +08:00
|
|
|
use super::{is_compiler, win_target};
|
|
|
|
|
2018-10-29 01:16:48 +08:00
|
|
|
pub fn main(out_dir: &str, out_path: &Path) {
|
2021-06-03 03:07:56 +08:00
|
|
|
let lib_name = super::lib_name();
|
2017-10-24 16:54:48 +08:00
|
|
|
|
2021-10-02 02:09:48 +08:00
|
|
|
// This is just a sanity check, the top level `main` should ensure this.
|
|
|
|
assert!(!(cfg!(feature = "bundled-windows") && !cfg!(feature = "bundled") && !win_target()),
|
|
|
|
"This module should not be used: we're not on Windows and the bundled feature has not been enabled");
|
2021-05-28 15:00:39 +08:00
|
|
|
|
2018-10-29 01:16:48 +08:00
|
|
|
#[cfg(feature = "buildtime_bindgen")]
|
|
|
|
{
|
|
|
|
use super::{bindings, HeaderLocation};
|
2023-07-09 15:21:40 +08:00
|
|
|
let header = HeaderLocation::FromPath(lib_name.to_owned());
|
2018-10-29 01:16:48 +08:00
|
|
|
bindings::write_to_out_dir(header, out_path);
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "buildtime_bindgen"))]
|
|
|
|
{
|
2023-07-09 16:39:21 +08:00
|
|
|
super::copy_bindings(lib_name, "bindgen_bundled_version", out_path);
|
2018-10-29 01:16:48 +08:00
|
|
|
}
|
2022-10-29 01:02:49 +08:00
|
|
|
println!("cargo:rerun-if-changed={lib_name}/sqlite3.c");
|
2020-07-17 17:09:56 +08:00
|
|
|
println!("cargo:rerun-if-changed=sqlite3/wasm32-wasi-vfs.c");
|
2017-09-21 03:28:19 +08:00
|
|
|
let mut cfg = cc::Build::new();
|
2022-10-29 01:02:49 +08:00
|
|
|
cfg.file(format!("{lib_name}/sqlite3.c"))
|
2017-03-04 03:57:40 +08:00
|
|
|
.flag("-DSQLITE_CORE")
|
|
|
|
.flag("-DSQLITE_DEFAULT_FOREIGN_KEYS=1")
|
|
|
|
.flag("-DSQLITE_ENABLE_API_ARMOR")
|
|
|
|
.flag("-DSQLITE_THREADSAFE=1")
|
2023-08-13 20:59:32 +08:00
|
|
|
.flag("-DSQLITE_USE_URI")
|
2020-06-27 19:37:26 +08:00
|
|
|
.flag("-D_POSIX_THREAD_SAFE_FUNCTIONS") // cross compile with MinGW
|
2023-07-31 04:40:36 +08:00
|
|
|
.flag("-DSQLITE_DEFAULT_MEMSTATUS=0")
|
|
|
|
.flag("-DSQLITE_MAX_EXPR_DEPTH=0")
|
|
|
|
.flag("-DSQLITE_OMIT_DECLTYPE")
|
|
|
|
.flag("-DSQLITE_OMIT_DEPRECATED")
|
|
|
|
.flag("-DSQLITE_OMIT_PROGRESS_CALLBACK")
|
|
|
|
.flag("-DSQLITE_LIKE_DOESNT_MATCH_BLOBS")
|
2020-04-07 01:43:43 +08:00
|
|
|
.warnings(false);
|
2020-04-16 17:38:40 +08:00
|
|
|
|
2021-06-03 03:07:56 +08:00
|
|
|
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"));
|
|
|
|
|
2021-10-02 02:09:48 +08:00
|
|
|
assert!(
|
|
|
|
Path::new(&lib_dir).exists(),
|
|
|
|
"OpenSSL library directory does not exist: {}",
|
|
|
|
lib_dir.to_string_lossy()
|
|
|
|
);
|
2021-06-03 03:07:56 +08:00
|
|
|
|
|
|
|
if !Path::new(&inc_dir).exists() {
|
|
|
|
panic!(
|
|
|
|
"OpenSSL include directory does not exist: {}",
|
|
|
|
inc_dir.to_string_lossy()
|
2022-01-06 02:53:49 +08:00
|
|
|
);
|
2021-06-03 03:07:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
use_openssl = true;
|
|
|
|
(lib_dir, inc_dir)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if cfg!(feature = "bundled-sqlcipher-vendored-openssl") {
|
2022-05-29 19:33:51 +08:00
|
|
|
cfg.include(env::var("DEP_OPENSSL_INCLUDE").unwrap());
|
2021-12-01 03:17:29 +08:00
|
|
|
// cargo will resolve downstream to the static lib in
|
|
|
|
// openssl-sys
|
2021-06-03 03:07:56 +08:00
|
|
|
} else if use_openssl {
|
|
|
|
cfg.include(inc_dir.to_string_lossy().as_ref());
|
2022-12-23 09:31:58 +08:00
|
|
|
let lib_name = if is_windows { "libcrypto" } else { "crypto" };
|
|
|
|
println!("cargo:rustc-link-lib=dylib={}", lib_name);
|
2021-10-06 13:04:04 +08:00
|
|
|
println!("cargo:rustc-link-search={}", lib_dir.to_string_lossy());
|
2021-06-03 03:07:56 +08:00
|
|
|
} else if is_apple {
|
|
|
|
cfg.flag("-DSQLCIPHER_CRYPTO_CC");
|
2021-11-16 01:40:49 +08:00
|
|
|
println!("cargo:rustc-link-lib=framework=Security");
|
2021-06-03 03:07:56 +08:00
|
|
|
println!("cargo:rustc-link-lib=framework=CoreFoundation");
|
|
|
|
} else {
|
2021-10-06 13:04:04 +08:00
|
|
|
// branch not taken on Windows, just `crypto` is fine.
|
2021-06-03 03:07:56 +08:00
|
|
|
println!("cargo:rustc-link-lib=dylib=crypto");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 01:27:59 +08:00
|
|
|
// 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
|
2021-05-28 15:00:39 +08:00
|
|
|
if super::android_target() {
|
2021-04-17 01:27:59 +08:00
|
|
|
cfg.flag("-DSQLITE_TEMP_STORE=3");
|
|
|
|
}
|
|
|
|
|
2020-04-16 17:38:40 +08:00
|
|
|
if cfg!(feature = "with-asan") {
|
|
|
|
cfg.flag("-fsanitize=address");
|
|
|
|
}
|
|
|
|
|
2021-10-02 02:09:48 +08:00
|
|
|
// If explicitly requested: enable static linking against the Microsoft Visual
|
|
|
|
// C++ Runtime to avoid dependencies on vcruntime140.dll and similar libraries.
|
2021-09-27 21:01:48 +08:00
|
|
|
if cfg!(target_feature = "crt-static") && is_compiler("msvc") {
|
|
|
|
cfg.static_crt(true);
|
|
|
|
}
|
|
|
|
|
2019-08-10 02:03:46 +08:00
|
|
|
// Older versions of visual studio don't support c99 (including isnan), which
|
|
|
|
// causes a build failure when the linker fails to find the `isnan`
|
2021-05-02 19:46:04 +08:00
|
|
|
// function. `sqlite` provides its own implementation, using the fact
|
2019-08-10 02:03:46 +08:00
|
|
|
// that x != x when x is NaN.
|
2019-05-10 20:42:02 +08:00
|
|
|
//
|
2019-08-10 02:03:46 +08:00
|
|
|
// There may be other platforms that don't support `isnan`, they should be
|
|
|
|
// tested for here.
|
2021-05-28 15:00:39 +08:00
|
|
|
if is_compiler("msvc") {
|
2019-08-17 14:18:37 +08:00
|
|
|
use cc::windows_registry::{find_vs_version, VsVers};
|
2019-05-10 20:42:02 +08:00
|
|
|
let vs_has_nan = match find_vs_version() {
|
|
|
|
Ok(ver) => ver != VsVers::Vs12,
|
|
|
|
Err(_msg) => false,
|
|
|
|
};
|
|
|
|
if vs_has_nan {
|
2020-06-11 05:06:16 +08:00
|
|
|
cfg.flag("-DHAVE_ISNAN");
|
2019-05-10 20:42:02 +08:00
|
|
|
}
|
2023-07-31 04:40:36 +08:00
|
|
|
} else if env::var("TARGET") != Ok("wasm32-unknown-unknown".to_string()) {
|
2020-06-11 05:06:16 +08:00
|
|
|
cfg.flag("-DHAVE_ISNAN");
|
2019-05-10 20:42:02 +08:00
|
|
|
}
|
2021-05-28 15:00:39 +08:00
|
|
|
if !win_target() {
|
2020-05-30 07:32:19 +08:00
|
|
|
cfg.flag("-DHAVE_LOCALTIME_R");
|
|
|
|
}
|
2023-05-03 00:32:56 +08:00
|
|
|
if env::var("TARGET").map_or(false, |v| v == "wasm32-wasi") {
|
2023-05-25 21:33:39 +08:00
|
|
|
cfg.flag("-USQLITE_THREADSAFE")
|
|
|
|
.flag("-DSQLITE_THREADSAFE=0")
|
2020-07-17 17:09:56 +08:00
|
|
|
// https://github.com/rust-lang/rust/issues/74393
|
2023-05-25 21:33:39 +08:00
|
|
|
.flag("-DLONGDOUBLE_TYPE=double")
|
|
|
|
.flag("-D_WASI_EMULATED_MMAN")
|
|
|
|
.flag("-D_WASI_EMULATED_GETPID")
|
|
|
|
.flag("-D_WASI_EMULATED_SIGNAL")
|
|
|
|
.flag("-D_WASI_EMULATED_PROCESS_CLOCKS");
|
|
|
|
|
2020-07-21 02:04:55 +08:00
|
|
|
if cfg!(feature = "wasm32-wasi-vfs") {
|
|
|
|
cfg.file("sqlite3/wasm32-wasi-vfs.c");
|
|
|
|
}
|
2020-07-17 17:09:56 +08:00
|
|
|
}
|
2023-07-31 04:40:36 +08:00
|
|
|
if env::var("TARGET") == Ok("wasm32-unknown-unknown".to_string()) {
|
|
|
|
// Apple clang doesn't support wasm32, so use Homebrew clang by default.
|
|
|
|
if env::var("HOST") == Ok("x86_64-apple-darwin".to_string()) {
|
|
|
|
if env::var("CC").is_err() {
|
|
|
|
std::env::set_var("CC", "/usr/local/opt/llvm/bin/clang");
|
|
|
|
}
|
|
|
|
if env::var("AR").is_err() {
|
|
|
|
std::env::set_var("AR", "/usr/local/opt/llvm/bin/llvm-ar");
|
|
|
|
}
|
|
|
|
} else if env::var("HOST") == Ok("aarch64-apple-darwin".to_string()) {
|
|
|
|
if env::var("CC").is_err() {
|
|
|
|
std::env::set_var("CC", "/opt/homebrew/opt/llvm/bin/clang");
|
|
|
|
}
|
|
|
|
if env::var("AR").is_err() {
|
|
|
|
std::env::set_var("AR", "/opt/homebrew/opt/llvm/bin/llvm-ar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.flag("-DSQLITE_OS_OTHER")
|
|
|
|
.flag("-DSQLITE_TEMP_STORE=3")
|
|
|
|
// https://github.com/rust-lang/rust/issues/74393
|
|
|
|
.flag("-DLONGDOUBLE_TYPE=double")
|
|
|
|
.flag("-DSQLITE_OMIT_LOCALTIME");
|
|
|
|
cfg.include("sqlite3/wasm32-unknown-unknown/include");
|
|
|
|
cfg.file("sqlite3/wasm32-unknown-unknown/libc/stdlib/qsort.c");
|
|
|
|
cfg.file("sqlite3/wasm32-unknown-unknown/libc/string/strcmp.c");
|
|
|
|
cfg.file("sqlite3/wasm32-unknown-unknown/libc/string/strcspn.c");
|
|
|
|
cfg.file("sqlite3/wasm32-unknown-unknown/libc/string/strlen.c");
|
|
|
|
cfg.file("sqlite3/wasm32-unknown-unknown/libc/string/strncmp.c");
|
|
|
|
cfg.file("sqlite3/wasm32-unknown-unknown/libc/string/strrchr.c");
|
|
|
|
}
|
2017-09-21 03:28:19 +08:00
|
|
|
if cfg!(feature = "unlock_notify") {
|
|
|
|
cfg.flag("-DSQLITE_ENABLE_UNLOCK_NOTIFY");
|
|
|
|
}
|
2019-01-13 19:46:19 +08:00
|
|
|
if cfg!(feature = "preupdate_hook") {
|
|
|
|
cfg.flag("-DSQLITE_ENABLE_PREUPDATE_HOOK");
|
|
|
|
}
|
|
|
|
if cfg!(feature = "session") {
|
|
|
|
cfg.flag("-DSQLITE_ENABLE_SESSION");
|
|
|
|
}
|
2019-05-16 02:23:20 +08:00
|
|
|
|
2019-05-16 00:41:23 +08:00
|
|
|
if let Ok(limit) = env::var("SQLITE_MAX_VARIABLE_NUMBER") {
|
2022-10-29 01:02:49 +08:00
|
|
|
cfg.flag(&format!("-DSQLITE_MAX_VARIABLE_NUMBER={limit}"));
|
2019-05-16 00:41:23 +08:00
|
|
|
}
|
2019-05-16 02:23:20 +08:00
|
|
|
println!("cargo:rerun-if-env-changed=SQLITE_MAX_VARIABLE_NUMBER");
|
|
|
|
|
2019-05-16 00:41:23 +08:00
|
|
|
if let Ok(limit) = env::var("SQLITE_MAX_EXPR_DEPTH") {
|
2022-10-29 01:02:49 +08:00
|
|
|
cfg.flag(&format!("-DSQLITE_MAX_EXPR_DEPTH={limit}"));
|
2019-05-16 00:41:23 +08:00
|
|
|
}
|
2019-05-16 02:23:20 +08:00
|
|
|
println!("cargo:rerun-if-env-changed=SQLITE_MAX_EXPR_DEPTH");
|
2019-05-16 00:41:23 +08:00
|
|
|
|
2023-05-10 04:19:25 +08:00
|
|
|
if let Ok(limit) = env::var("SQLITE_MAX_COLUMN") {
|
|
|
|
cfg.flag(&format!("-DSQLITE_MAX_COLUMN={limit}"));
|
|
|
|
}
|
|
|
|
println!("cargo:rerun-if-env-changed=SQLITE_MAX_COLUMN");
|
|
|
|
|
2020-06-11 05:06:47 +08:00
|
|
|
if let Ok(extras) = env::var("LIBSQLITE3_FLAGS") {
|
|
|
|
for extra in extras.split_whitespace() {
|
|
|
|
if extra.starts_with("-D") || extra.starts_with("-U") {
|
|
|
|
cfg.flag(extra);
|
|
|
|
} else if extra.starts_with("SQLITE_") {
|
2022-10-29 01:02:49 +08:00
|
|
|
cfg.flag(&format!("-D{extra}"));
|
2020-06-11 05:06:47 +08:00
|
|
|
} else {
|
|
|
|
panic!("Don't understand {} in LIBSQLITE3_FLAGS", extra);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("cargo:rerun-if-env-changed=LIBSQLITE3_FLAGS");
|
|
|
|
|
2021-06-03 03:07:56 +08:00
|
|
|
cfg.compile(lib_name);
|
2018-06-30 06:20:59 +08:00
|
|
|
|
2022-10-29 01:02:49 +08:00
|
|
|
println!("cargo:lib_dir={out_dir}");
|
2017-03-04 03:57:40 +08:00
|
|
|
}
|
2021-06-03 03:07:56 +08:00
|
|
|
|
|
|
|
fn env(name: &str) -> Option<OsString> {
|
2021-12-18 15:42:04 +08:00
|
|
|
let prefix = env::var("TARGET").unwrap().to_uppercase().replace('-', "_");
|
2022-10-29 01:02:49 +08:00
|
|
|
let prefixed = format!("{prefix}_{name}");
|
|
|
|
let var = env::var_os(prefixed);
|
2021-06-03 03:07:56 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2017-02-08 09:37:52 +08:00
|
|
|
}
|
|
|
|
|
2018-10-29 01:16:48 +08:00
|
|
|
fn env_prefix() -> &'static str {
|
2021-06-03 03:07:56 +08:00
|
|
|
if cfg!(any(feature = "sqlcipher", feature = "bundled-sqlcipher")) {
|
2018-10-29 01:16:48 +08:00
|
|
|
"SQLCIPHER"
|
|
|
|
} else {
|
|
|
|
"SQLITE3"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:07:56 +08:00
|
|
|
fn lib_name() -> &'static str {
|
|
|
|
if cfg!(any(feature = "sqlcipher", feature = "bundled-sqlcipher")) {
|
|
|
|
"sqlcipher"
|
|
|
|
} else if cfg!(all(windows, feature = "winsqlite3")) {
|
|
|
|
"winsqlite3"
|
|
|
|
} else {
|
|
|
|
"sqlite3"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 01:16:48 +08:00
|
|
|
pub enum HeaderLocation {
|
|
|
|
FromEnvironment,
|
|
|
|
Wrapper,
|
|
|
|
FromPath(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<HeaderLocation> for String {
|
|
|
|
fn from(header: HeaderLocation) -> String {
|
|
|
|
match header {
|
|
|
|
HeaderLocation::FromEnvironment => {
|
|
|
|
let prefix = env_prefix();
|
2022-10-29 01:02:49 +08:00
|
|
|
let mut header = env::var(format!("{prefix}_INCLUDE_DIR")).unwrap_or_else(|_| {
|
2023-12-23 21:21:23 +08:00
|
|
|
panic!("{prefix}_INCLUDE_DIR must be set if {prefix}_LIB_DIR is set")
|
2019-12-20 03:08:04 +08:00
|
|
|
});
|
2023-07-09 16:39:21 +08:00
|
|
|
header.push_str(if cfg!(feature = "loadable_extension") {
|
|
|
|
"/sqlite3ext.h"
|
|
|
|
} else {
|
|
|
|
"/sqlite3.h"
|
2019-12-20 03:08:04 +08:00
|
|
|
});
|
2018-10-29 01:16:48 +08:00
|
|
|
header
|
|
|
|
}
|
2023-07-09 16:39:21 +08:00
|
|
|
HeaderLocation::Wrapper => if cfg!(feature = "loadable_extension") {
|
|
|
|
"wrapper_ext.h"
|
|
|
|
} else {
|
|
|
|
"wrapper.h"
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
HeaderLocation::FromPath(path) => format!(
|
|
|
|
"{}/{}",
|
|
|
|
path,
|
|
|
|
if cfg!(feature = "loadable_extension") {
|
|
|
|
"sqlite3ext.h"
|
|
|
|
} else {
|
|
|
|
"sqlite3.h"
|
|
|
|
}
|
|
|
|
),
|
2018-10-29 01:16:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 01:22:03 +08:00
|
|
|
mod build_linked {
|
2021-05-28 15:00:39 +08:00
|
|
|
#[cfg(feature = "vcpkg")]
|
2017-05-28 11:35:46 +08:00
|
|
|
extern crate vcpkg;
|
|
|
|
|
2021-06-03 03:07:56 +08:00
|
|
|
use super::{bindings, env_prefix, is_compiler, lib_name, win_target, HeaderLocation};
|
2017-03-04 03:57:40 +08:00
|
|
|
use std::env;
|
2018-10-29 01:16:48 +08:00
|
|
|
use std::path::Path;
|
2017-03-04 03:57:40 +08:00
|
|
|
|
2018-10-29 01:16:48 +08:00
|
|
|
pub fn main(_out_dir: &str, out_path: &Path) {
|
2017-03-04 03:57:40 +08:00
|
|
|
let header = find_sqlite();
|
2021-06-03 03:07:56 +08:00
|
|
|
if (cfg!(any(
|
|
|
|
feature = "bundled_bindings",
|
|
|
|
feature = "bundled",
|
|
|
|
feature = "bundled-sqlcipher"
|
|
|
|
)) || (win_target() && cfg!(feature = "bundled-windows")))
|
2021-05-28 15:00:39 +08:00
|
|
|
&& !cfg!(feature = "buildtime_bindgen")
|
2019-06-26 02:40:28 +08:00
|
|
|
{
|
2021-06-03 03:07:56 +08:00
|
|
|
// Generally means the `bundled_bindings` feature is enabled.
|
|
|
|
// Most users are better off with turning
|
2020-01-15 00:11:36 +08:00
|
|
|
// 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.
|
2023-07-09 16:39:21 +08:00
|
|
|
super::copy_bindings(lib_name(), "bindgen_bundled_version", out_path);
|
2019-04-20 01:22:03 +08:00
|
|
|
} else {
|
|
|
|
bindings::write_to_out_dir(header, out_path);
|
|
|
|
}
|
2017-02-08 09:37:52 +08:00
|
|
|
}
|
|
|
|
|
2023-07-09 16:39:21 +08:00
|
|
|
#[cfg(not(feature = "loadable_extension"))]
|
2018-12-15 09:12:31 +08:00
|
|
|
fn find_link_mode() -> &'static str {
|
2020-06-25 01:53:45 +08:00
|
|
|
// If the user specifies SQLITE3_STATIC (or SQLCIPHER_STATIC), do static
|
2018-12-15 09:12:31 +08:00
|
|
|
// linking, unless it's explicitly set to 0.
|
|
|
|
match &env::var(format!("{}_STATIC", env_prefix())) {
|
|
|
|
Ok(v) if v != "0" => "static",
|
|
|
|
_ => "dylib",
|
|
|
|
}
|
|
|
|
}
|
2017-03-04 03:57:40 +08:00
|
|
|
// Prints the necessary cargo link commands and returns the path to the header.
|
2017-03-04 04:16:49 +08:00
|
|
|
fn find_sqlite() -> HeaderLocation {
|
2021-06-03 03:07:56 +08:00
|
|
|
let link_lib = lib_name();
|
2017-10-24 16:54:48 +08:00
|
|
|
|
2018-04-07 04:27:07 +08:00
|
|
|
println!("cargo:rerun-if-env-changed={}_INCLUDE_DIR", env_prefix());
|
|
|
|
println!("cargo:rerun-if-env-changed={}_LIB_DIR", env_prefix());
|
2018-12-15 09:12:31 +08:00
|
|
|
println!("cargo:rerun-if-env-changed={}_STATIC", env_prefix());
|
2021-05-28 15:00:39 +08:00
|
|
|
if cfg!(feature = "vcpkg") && is_compiler("msvc") {
|
2019-03-25 04:21:13 +08:00
|
|
|
println!("cargo:rerun-if-env-changed=VCPKGRS_DYNAMIC");
|
|
|
|
}
|
2020-01-08 15:48:19 +08:00
|
|
|
|
|
|
|
// dependents can access `DEP_SQLITE3_LINK_TARGET` (`sqlite3` being the
|
|
|
|
// `links=` value in our Cargo.toml) to get this value. This might be
|
|
|
|
// useful if you need to ensure whatever crypto library sqlcipher relies
|
|
|
|
// on is available, for example.
|
2023-07-09 16:39:21 +08:00
|
|
|
#[cfg(not(feature = "loadable_extension"))]
|
2022-10-29 01:02:49 +08:00
|
|
|
println!("cargo:link-target={link_lib}");
|
2020-01-08 15:48:19 +08:00
|
|
|
|
2021-05-28 15:00:39 +08:00
|
|
|
if win_target() && cfg!(feature = "winsqlite3") {
|
2023-07-09 16:39:21 +08:00
|
|
|
#[cfg(not(feature = "loadable_extension"))]
|
2022-10-29 01:02:49 +08:00
|
|
|
println!("cargo:rustc-link-lib=dylib={link_lib}");
|
2020-08-18 06:33:50 +08:00
|
|
|
return HeaderLocation::Wrapper;
|
|
|
|
}
|
|
|
|
|
2017-03-04 03:57:40 +08:00
|
|
|
// Allow users to specify where to find SQLite.
|
2017-10-24 16:54:48 +08:00
|
|
|
if let Ok(dir) = env::var(format!("{}_LIB_DIR", env_prefix())) {
|
2019-02-05 12:53:57 +08:00
|
|
|
// 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);
|
2023-07-09 16:39:21 +08:00
|
|
|
#[cfg(not(feature = "loadable_extension"))]
|
2019-12-20 03:08:04 +08:00
|
|
|
if pkg_config::Config::new().probe(link_lib).is_err() {
|
2019-02-05 12:53:57 +08:00
|
|
|
// Otherwise just emit the bare minimum link commands.
|
2022-10-29 01:02:49 +08:00
|
|
|
println!("cargo:rustc-link-lib={}={link_lib}", find_link_mode());
|
|
|
|
println!("cargo:rustc-link-search={dir}");
|
2019-02-05 12:53:57 +08:00
|
|
|
}
|
2017-03-04 04:16:49 +08:00
|
|
|
return HeaderLocation::FromEnvironment;
|
2017-03-04 03:57:40 +08:00
|
|
|
}
|
|
|
|
|
2017-05-28 11:35:46 +08:00
|
|
|
if let Some(header) = try_vcpkg() {
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
2017-03-04 03:57:40 +08:00
|
|
|
// See if pkg-config can do everything for us.
|
2022-01-06 02:59:54 +08:00
|
|
|
if let Ok(mut lib) = pkg_config::Config::new()
|
2018-08-11 18:48:21 +08:00
|
|
|
.print_system_libs(false)
|
|
|
|
.probe(link_lib)
|
|
|
|
{
|
2023-07-09 15:21:40 +08:00
|
|
|
if let Some(header) = lib.include_paths.pop() {
|
2022-01-06 02:59:54 +08:00
|
|
|
HeaderLocation::FromPath(header.to_string_lossy().into())
|
|
|
|
} else {
|
2017-03-04 04:16:49 +08:00
|
|
|
HeaderLocation::Wrapper
|
2017-02-08 09:37:52 +08:00
|
|
|
}
|
2022-01-06 02:59:54 +08:00
|
|
|
} else {
|
|
|
|
// No env var set and pkg-config couldn't help; just output the link-lib
|
|
|
|
// 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/rusqlite/rusqlite/issues/207.
|
2023-07-09 16:39:21 +08:00
|
|
|
#[cfg(not(feature = "loadable_extension"))]
|
2022-10-29 01:02:49 +08:00
|
|
|
println!("cargo:rustc-link-lib={}={link_lib}", find_link_mode());
|
2022-01-06 02:59:54 +08:00
|
|
|
HeaderLocation::Wrapper
|
2017-01-24 09:17:14 +08:00
|
|
|
}
|
2017-03-04 03:57:40 +08:00
|
|
|
}
|
|
|
|
|
2017-05-28 11:35:46 +08:00
|
|
|
fn try_vcpkg() -> Option<HeaderLocation> {
|
2021-05-28 15:00:39 +08:00
|
|
|
if cfg!(feature = "vcpkg") && is_compiler("msvc") {
|
|
|
|
// See if vcpkg can find it.
|
2021-06-03 03:07:56 +08:00
|
|
|
if let Ok(mut lib) = vcpkg::Config::new().probe(lib_name()) {
|
2023-07-09 15:21:40 +08:00
|
|
|
if let Some(header) = lib.include_paths.pop() {
|
2021-05-28 15:00:39 +08:00
|
|
|
return Some(HeaderLocation::FromPath(header.to_string_lossy().into()));
|
|
|
|
}
|
2017-05-28 11:35:46 +08:00
|
|
|
}
|
2021-05-28 15:00:39 +08:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
None
|
2017-05-28 11:35:46 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-29 01:16:48 +08:00
|
|
|
}
|
2017-10-24 16:54:48 +08:00
|
|
|
|
2019-04-20 01:22:03 +08:00
|
|
|
#[cfg(not(feature = "buildtime_bindgen"))]
|
2018-10-29 01:16:48 +08:00
|
|
|
mod bindings {
|
2021-06-03 03:07:56 +08:00
|
|
|
#![allow(dead_code)]
|
2018-10-29 01:16:48 +08:00
|
|
|
use super::HeaderLocation;
|
|
|
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
2023-07-09 16:39:21 +08:00
|
|
|
static PREBUILT_BINDGENS: &[&str] = &["bindgen_3.14.0"];
|
2018-10-29 01:16:48 +08:00
|
|
|
|
|
|
|
pub fn write_to_out_dir(_header: HeaderLocation, out_path: &Path) {
|
2023-07-09 15:21:40 +08:00
|
|
|
let name = PREBUILT_BINDGENS[PREBUILT_BINDGENS.len() - 1];
|
|
|
|
super::copy_bindings("bindgen-bindings", name, out_path);
|
2017-02-08 09:37:52 +08:00
|
|
|
}
|
2018-10-29 01:16:48 +08:00
|
|
|
}
|
2016-06-15 22:34:13 +08:00
|
|
|
|
2018-10-29 01:16:48 +08:00
|
|
|
#[cfg(feature = "buildtime_bindgen")]
|
|
|
|
mod bindings {
|
|
|
|
use super::HeaderLocation;
|
2019-08-17 14:18:37 +08:00
|
|
|
use bindgen::callbacks::{IntKind, ParseCallbacks};
|
2017-03-04 03:57:40 +08:00
|
|
|
|
2018-10-29 01:16:48 +08:00
|
|
|
use std::path::Path;
|
2017-03-04 03:57:40 +08:00
|
|
|
|
2021-05-28 15:00:39 +08:00
|
|
|
use super::win_target;
|
|
|
|
|
2018-10-29 01:16:48 +08:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct SqliteTypeChooser;
|
2017-03-04 03:57:40 +08:00
|
|
|
|
2018-10-29 01:16:48 +08:00
|
|
|
impl ParseCallbacks for SqliteTypeChooser {
|
2023-06-17 22:09:53 +08:00
|
|
|
fn int_macro(&self, name: &str, _value: i64) -> Option<IntKind> {
|
2023-06-04 18:31:44 +08:00
|
|
|
if name == "SQLITE_SERIALIZE_NOCOPY"
|
|
|
|
|| name.starts_with("SQLITE_DESERIALIZE_")
|
|
|
|
|| name.starts_with("SQLITE_PREPARE_")
|
|
|
|
{
|
|
|
|
Some(IntKind::UInt)
|
2018-10-29 01:16:48 +08:00
|
|
|
} else {
|
|
|
|
None
|
2017-03-04 03:57:40 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-29 01:16:48 +08:00
|
|
|
}
|
2017-03-04 03:57:40 +08:00
|
|
|
|
2020-04-10 16:38:55 +08:00
|
|
|
// Are we generating the bundled bindings? Used to avoid emitting things
|
|
|
|
// that would be problematic in bundled builds. This env var is set by
|
|
|
|
// `upgrade.sh`.
|
|
|
|
fn generating_bundled_bindings() -> bool {
|
|
|
|
// Hacky way to know if we're generating the bundled bindings
|
|
|
|
println!("cargo:rerun-if-env-changed=LIBSQLITE3_SYS_BUNDLING");
|
2021-05-06 00:29:16 +08:00
|
|
|
match std::env::var("LIBSQLITE3_SYS_BUNDLING") {
|
|
|
|
Ok(v) => v != "0",
|
|
|
|
Err(_) => false,
|
|
|
|
}
|
2020-04-10 16:38:55 +08:00
|
|
|
}
|
|
|
|
|
2018-10-29 01:16:48 +08:00
|
|
|
pub fn write_to_out_dir(header: HeaderLocation, out_path: &Path) {
|
|
|
|
let header: String = header.into();
|
2019-01-13 19:46:19 +08:00
|
|
|
let mut bindings = bindgen::builder()
|
2023-06-17 22:09:53 +08:00
|
|
|
.default_macro_constant_type(bindgen::MacroTypeVariation::Signed)
|
|
|
|
.disable_nested_struct_naming()
|
2021-06-03 03:07:56 +08:00
|
|
|
.trust_clang_mangling(false)
|
2018-10-29 01:16:48 +08:00
|
|
|
.header(header.clone())
|
2023-07-09 16:39:21 +08:00
|
|
|
.parse_callbacks(Box::new(SqliteTypeChooser));
|
|
|
|
if cfg!(feature = "loadable_extension") {
|
|
|
|
bindings = bindings.ignore_functions(); // see generate_functions
|
|
|
|
} else {
|
|
|
|
bindings = bindings
|
|
|
|
.blocklist_function("sqlite3_auto_extension")
|
|
|
|
.raw_line(
|
|
|
|
r#"extern "C" {
|
2023-03-25 18:33:33 +08:00
|
|
|
pub fn sqlite3_auto_extension(
|
2023-03-25 22:38:24 +08:00
|
|
|
xEntryPoint: ::std::option::Option<
|
|
|
|
unsafe extern "C" fn(
|
|
|
|
db: *mut sqlite3,
|
|
|
|
pzErrMsg: *mut *const ::std::os::raw::c_char,
|
|
|
|
pThunk: *const sqlite3_api_routines,
|
|
|
|
) -> ::std::os::raw::c_int,
|
|
|
|
>,
|
2023-03-25 18:33:33 +08:00
|
|
|
) -> ::std::os::raw::c_int;
|
|
|
|
}"#,
|
2023-07-09 16:39:21 +08:00
|
|
|
)
|
|
|
|
.blocklist_function("sqlite3_cancel_auto_extension")
|
|
|
|
.raw_line(
|
|
|
|
r#"extern "C" {
|
2023-03-25 22:38:24 +08:00
|
|
|
pub fn sqlite3_cancel_auto_extension(
|
|
|
|
xEntryPoint: ::std::option::Option<
|
|
|
|
unsafe extern "C" fn(
|
|
|
|
db: *mut sqlite3,
|
|
|
|
pzErrMsg: *mut *const ::std::os::raw::c_char,
|
|
|
|
pThunk: *const sqlite3_api_routines,
|
|
|
|
) -> ::std::os::raw::c_int,
|
|
|
|
>,
|
|
|
|
) -> ::std::os::raw::c_int;
|
2023-03-25 23:15:05 +08:00
|
|
|
}"#,
|
2023-07-09 16:39:21 +08:00
|
|
|
);
|
|
|
|
}
|
2019-01-13 19:46:19 +08:00
|
|
|
|
2021-06-03 03:07:56 +08:00
|
|
|
if cfg!(any(feature = "sqlcipher", feature = "bundled-sqlcipher")) {
|
|
|
|
bindings = bindings.clang_arg("-DSQLITE_HAS_CODEC");
|
|
|
|
}
|
2019-01-13 19:46:19 +08:00
|
|
|
if cfg!(feature = "unlock_notify") {
|
|
|
|
bindings = bindings.clang_arg("-DSQLITE_ENABLE_UNLOCK_NOTIFY");
|
|
|
|
}
|
|
|
|
if cfg!(feature = "preupdate_hook") {
|
|
|
|
bindings = bindings.clang_arg("-DSQLITE_ENABLE_PREUPDATE_HOOK");
|
|
|
|
}
|
|
|
|
if cfg!(feature = "session") {
|
|
|
|
bindings = bindings.clang_arg("-DSQLITE_ENABLE_SESSION");
|
|
|
|
}
|
2021-05-28 15:00:39 +08:00
|
|
|
if win_target() && cfg!(feature = "winsqlite3") {
|
2020-08-18 06:33:50 +08:00
|
|
|
bindings = bindings
|
|
|
|
.clang_arg("-DBINDGEN_USE_WINSQLITE3")
|
2021-04-05 23:35:23 +08:00
|
|
|
.blocklist_item("NTDDI_.+")
|
|
|
|
.blocklist_item("WINAPI_FAMILY.*")
|
|
|
|
.blocklist_item("_WIN32_.+")
|
|
|
|
.blocklist_item("_VCRT_COMPILER_PREPROCESSOR")
|
|
|
|
.blocklist_item("_SAL_VERSION")
|
|
|
|
.blocklist_item("__SAL_H_VERSION")
|
|
|
|
.blocklist_item("_USE_DECLSPECS_FOR_SAL")
|
|
|
|
.blocklist_item("_USE_ATTRIBUTES_FOR_SAL")
|
|
|
|
.blocklist_item("_CRT_PACKING")
|
|
|
|
.blocklist_item("_HAS_EXCEPTIONS")
|
|
|
|
.blocklist_item("_STL_LANG")
|
|
|
|
.blocklist_item("_HAS_CXX17")
|
|
|
|
.blocklist_item("_HAS_CXX20")
|
|
|
|
.blocklist_item("_HAS_NODISCARD")
|
|
|
|
.blocklist_item("WDK_NTDDI_VERSION")
|
|
|
|
.blocklist_item("OSVERSION_MASK")
|
|
|
|
.blocklist_item("SPVERSION_MASK")
|
|
|
|
.blocklist_item("SUBVERSION_MASK")
|
|
|
|
.blocklist_item("WINVER")
|
|
|
|
.blocklist_item("__security_cookie")
|
|
|
|
.blocklist_type("size_t")
|
|
|
|
.blocklist_type("__vcrt_bool")
|
|
|
|
.blocklist_type("wchar_t")
|
|
|
|
.blocklist_function("__security_init_cookie")
|
|
|
|
.blocklist_function("__report_gsfailure")
|
|
|
|
.blocklist_function("__va_start");
|
2020-08-18 06:33:50 +08:00
|
|
|
}
|
2019-01-13 19:46:19 +08:00
|
|
|
|
2020-04-10 16:38:55 +08:00
|
|
|
// When cross compiling unless effort is taken to fix the issue, bindgen
|
|
|
|
// will find the wrong headers. There's only one header included by the
|
|
|
|
// amalgamated `sqlite.h`: `stdarg.h`.
|
|
|
|
//
|
|
|
|
// Thankfully, there's almost no case where rust code needs to use
|
|
|
|
// functions taking `va_list` (It's nearly impossible to get a `va_list`
|
|
|
|
// in Rust unless you get passed it by C code for some reason).
|
|
|
|
//
|
|
|
|
// Arguably, we should never be including these, but we include them for
|
|
|
|
// the cases where they aren't totally broken...
|
|
|
|
let target_arch = std::env::var("TARGET").unwrap();
|
|
|
|
let host_arch = std::env::var("HOST").unwrap();
|
|
|
|
let is_cross_compiling = target_arch != host_arch;
|
|
|
|
|
|
|
|
// Note that when generating the bundled file, we're essentially always
|
|
|
|
// cross compiling.
|
|
|
|
if generating_bundled_bindings() || is_cross_compiling {
|
|
|
|
// Get rid of va_list, as it's not
|
|
|
|
bindings = bindings
|
2021-04-05 23:35:23 +08:00
|
|
|
.blocklist_function("sqlite3_vmprintf")
|
|
|
|
.blocklist_function("sqlite3_vsnprintf")
|
|
|
|
.blocklist_function("sqlite3_str_vappendf")
|
|
|
|
.blocklist_type("va_list")
|
2023-06-18 17:48:15 +08:00
|
|
|
.blocklist_item("__.*");
|
2020-04-10 16:38:55 +08:00
|
|
|
}
|
|
|
|
|
2023-07-09 16:39:21 +08:00
|
|
|
let bindings = bindings
|
2022-08-17 02:11:51 +08:00
|
|
|
.layout_tests(false)
|
2018-10-29 01:16:48 +08:00
|
|
|
.generate()
|
2023-07-09 16:39:21 +08:00
|
|
|
.unwrap_or_else(|_| panic!("could not run bindgen on header {}", header));
|
|
|
|
|
2023-07-09 20:17:19 +08:00
|
|
|
#[cfg(feature = "loadable_extension")]
|
|
|
|
{
|
2023-07-09 16:39:21 +08:00
|
|
|
let mut output = Vec::new();
|
|
|
|
bindings
|
|
|
|
.write(Box::new(&mut output))
|
|
|
|
.expect("could not write output of bindgen");
|
|
|
|
let mut output = String::from_utf8(output).expect("bindgen output was not UTF-8?!");
|
2023-07-09 20:17:19 +08:00
|
|
|
super::loadable_extension::generate_functions(&mut output);
|
2023-07-09 16:39:21 +08:00
|
|
|
std::fs::write(out_path, output.as_bytes())
|
2023-07-09 20:17:19 +08:00
|
|
|
.unwrap_or_else(|_| panic!("Could not write to {:?}", out_path));
|
2023-07-09 16:39:21 +08:00
|
|
|
}
|
2023-07-09 20:17:19 +08:00
|
|
|
#[cfg(not(feature = "loadable_extension"))]
|
|
|
|
bindings
|
2023-07-08 16:39:36 +08:00
|
|
|
.write_to_file(out_path)
|
2019-12-20 03:08:04 +08:00
|
|
|
.unwrap_or_else(|_| panic!("Could not write to {:?}", out_path));
|
2017-03-04 03:57:40 +08:00
|
|
|
}
|
2016-06-15 22:34:13 +08:00
|
|
|
}
|
2023-07-09 16:39:21 +08:00
|
|
|
|
|
|
|
#[cfg(all(feature = "buildtime_bindgen", feature = "loadable_extension"))]
|
|
|
|
mod loadable_extension {
|
|
|
|
/// try to generate similar rust code for all `#define sqlite3_xyz
|
|
|
|
/// sqlite3_api->abc` macros` in sqlite3ext.h
|
2023-07-09 20:17:19 +08:00
|
|
|
pub fn generate_functions(output: &mut String) {
|
|
|
|
// (1) parse sqlite3_api_routines fields from bindgen output
|
2023-07-09 16:39:21 +08:00
|
|
|
let ast: syn::File = syn::parse_str(output).expect("could not parse bindgen output");
|
|
|
|
let sqlite3_api_routines: syn::ItemStruct = ast
|
|
|
|
.items
|
|
|
|
.into_iter()
|
|
|
|
.find_map(|i| {
|
|
|
|
if let syn::Item::Struct(s) = i {
|
|
|
|
if s.ident == "sqlite3_api_routines" {
|
|
|
|
Some(s)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.expect("could not find sqlite3_api_routines");
|
|
|
|
let sqlite3_api_routines_ident = sqlite3_api_routines.ident;
|
|
|
|
let p_api = quote::format_ident!("p_api");
|
|
|
|
let mut stores = Vec::new();
|
2023-07-14 21:56:43 +08:00
|
|
|
let mut malloc = Vec::new();
|
2023-07-09 20:17:19 +08:00
|
|
|
// (2) `#define sqlite3_xyz sqlite3_api->abc` => `pub unsafe fn
|
2023-07-09 16:39:21 +08:00
|
|
|
// sqlite3_xyz(args) -> ty {...}` for each `abc` field:
|
|
|
|
for field in sqlite3_api_routines.fields {
|
|
|
|
let ident = field.ident.expect("unamed field");
|
|
|
|
let span = ident.span();
|
|
|
|
let name = ident.to_string();
|
|
|
|
if name == "vmprintf" || name == "xvsnprintf" || name == "str_vappendf" {
|
|
|
|
continue; // skip va_list
|
2023-07-15 16:24:51 +08:00
|
|
|
} else if name == "aggregate_count"
|
|
|
|
|| name == "expired"
|
|
|
|
|| name == "global_recover"
|
|
|
|
|| name == "thread_cleanup"
|
|
|
|
|| name == "transfer_bindings"
|
|
|
|
{
|
|
|
|
continue; // omit deprecated
|
2023-07-09 16:39:21 +08:00
|
|
|
}
|
2023-07-09 20:17:19 +08:00
|
|
|
let sqlite3_name = match name.as_ref() {
|
|
|
|
"xthreadsafe" => "sqlite3_threadsafe".to_owned(),
|
|
|
|
"interruptx" => "sqlite3_interrupt".to_owned(),
|
|
|
|
_ => {
|
|
|
|
format!("sqlite3_{name}")
|
|
|
|
}
|
|
|
|
};
|
2023-07-09 16:39:21 +08:00
|
|
|
let ptr_name =
|
|
|
|
syn::Ident::new(format!("__{}", sqlite3_name.to_uppercase()).as_ref(), span);
|
2023-07-09 20:17:19 +08:00
|
|
|
let sqlite3_fn_name = syn::Ident::new(&sqlite3_name, span);
|
2023-07-09 16:39:21 +08:00
|
|
|
let method =
|
|
|
|
extract_method(&field.ty).unwrap_or_else(|| panic!("unexpected type for {name}"));
|
|
|
|
let arg_names: syn::punctuated::Punctuated<&syn::Ident, syn::token::Comma> = method
|
|
|
|
.inputs
|
|
|
|
.iter()
|
|
|
|
.map(|i| &i.name.as_ref().unwrap().0)
|
|
|
|
.collect();
|
|
|
|
let args = &method.inputs;
|
|
|
|
// vtab_config/sqlite3_vtab_config: ok
|
|
|
|
let varargs = &method.variadic;
|
|
|
|
if varargs.is_some() && "db_config" != name && "log" != name && "vtab_config" != name {
|
|
|
|
continue; // skip ...
|
|
|
|
}
|
|
|
|
let ty = &method.output;
|
|
|
|
let tokens = if "db_config" == name {
|
|
|
|
quote::quote! {
|
2023-11-12 03:11:37 +08:00
|
|
|
static #ptr_name: ::std::sync::atomic::AtomicPtr<()> = ::std::sync::atomic::AtomicPtr::new(::std::ptr::null_mut());
|
2023-07-09 16:39:21 +08:00
|
|
|
pub unsafe fn #sqlite3_fn_name(#args arg3: ::std::os::raw::c_int, arg4: *mut ::std::os::raw::c_int) #ty {
|
2023-11-12 03:11:37 +08:00
|
|
|
let ptr = #ptr_name.load(::std::sync::atomic::Ordering::Acquire);
|
|
|
|
assert!(!ptr.is_null(), "SQLite API not initialized");
|
|
|
|
let fun: unsafe extern "C" fn(#args #varargs) #ty = ::std::mem::transmute(ptr);
|
2023-07-09 16:39:21 +08:00
|
|
|
(fun)(#arg_names, arg3, arg4)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if "log" == name {
|
|
|
|
quote::quote! {
|
2023-11-12 03:11:37 +08:00
|
|
|
static #ptr_name: ::std::sync::atomic::AtomicPtr<()> = ::std::sync::atomic::AtomicPtr::new(::std::ptr::null_mut());
|
2023-07-09 16:39:21 +08:00
|
|
|
pub unsafe fn #sqlite3_fn_name(#args arg3: *const ::std::os::raw::c_char) #ty {
|
2023-11-12 03:11:37 +08:00
|
|
|
let ptr = #ptr_name.load(::std::sync::atomic::Ordering::Acquire);
|
|
|
|
assert!(!ptr.is_null(), "SQLite API not initialized");
|
|
|
|
let fun: unsafe extern "C" fn(#args #varargs) #ty = ::std::mem::transmute(ptr);
|
2023-07-09 16:39:21 +08:00
|
|
|
(fun)(#arg_names, arg3)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quote::quote! {
|
2023-11-12 03:11:37 +08:00
|
|
|
static #ptr_name: ::std::sync::atomic::AtomicPtr<()> = ::std::sync::atomic::AtomicPtr::new(::std::ptr::null_mut());
|
2023-07-09 16:39:21 +08:00
|
|
|
pub unsafe fn #sqlite3_fn_name(#args) #ty {
|
2023-11-12 03:11:37 +08:00
|
|
|
let ptr = #ptr_name.load(::std::sync::atomic::Ordering::Acquire);
|
|
|
|
assert!(!ptr.is_null(), "SQLite API not initialized or SQLite feature omitted");
|
|
|
|
let fun: unsafe extern "C" fn(#args #varargs) #ty = ::std::mem::transmute(ptr);
|
2023-07-09 16:39:21 +08:00
|
|
|
(fun)(#arg_names)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
output.push_str(&prettyplease::unparse(
|
|
|
|
&syn::parse2(tokens).expect("could not parse quote output"),
|
|
|
|
));
|
|
|
|
output.push('\n');
|
2023-07-14 21:56:43 +08:00
|
|
|
if name == "malloc" {
|
|
|
|
&mut malloc
|
|
|
|
} else {
|
|
|
|
&mut stores
|
|
|
|
}
|
|
|
|
.push(quote::quote! {
|
2023-11-12 03:11:37 +08:00
|
|
|
if let Some(fun) = (*#p_api).#ident {
|
|
|
|
#ptr_name.store(
|
|
|
|
fun as usize as *mut (),
|
|
|
|
::std::sync::atomic::Ordering::Release,
|
|
|
|
);
|
|
|
|
}
|
2023-07-09 16:39:21 +08:00
|
|
|
});
|
|
|
|
}
|
2023-07-09 20:17:19 +08:00
|
|
|
// (3) generate rust code similar to SQLITE_EXTENSION_INIT2 macro
|
2023-07-09 16:39:21 +08:00
|
|
|
let tokens = quote::quote! {
|
|
|
|
/// Like SQLITE_EXTENSION_INIT2 macro
|
2023-07-09 21:53:03 +08:00
|
|
|
pub unsafe fn rusqlite_extension_init2(#p_api: *mut #sqlite3_api_routines_ident) -> ::std::result::Result<(),crate::InitError> {
|
2023-07-14 21:56:43 +08:00
|
|
|
#(#malloc)* // sqlite3_malloc needed by to_sqlite_error
|
2023-07-09 16:39:21 +08:00
|
|
|
if let Some(fun) = (*#p_api).libversion_number {
|
|
|
|
let version = fun();
|
|
|
|
if SQLITE_VERSION_NUMBER > version {
|
2023-07-09 21:53:03 +08:00
|
|
|
return Err(crate::InitError::VersionMismatch{compile_time: SQLITE_VERSION_NUMBER, runtime: version});
|
2023-07-09 16:39:21 +08:00
|
|
|
}
|
|
|
|
} else {
|
2023-07-09 21:53:03 +08:00
|
|
|
return Err(crate::InitError::NullFunctionPointer);
|
2023-07-09 16:39:21 +08:00
|
|
|
}
|
|
|
|
#(#stores)*
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
output.push_str(&prettyplease::unparse(
|
|
|
|
&syn::parse2(tokens).expect("could not parse quote output"),
|
|
|
|
));
|
|
|
|
output.push('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_method(ty: &syn::Type) -> Option<&syn::TypeBareFn> {
|
|
|
|
match ty {
|
|
|
|
syn::Type::Path(tp) => tp.path.segments.last(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
.map(|seg| match &seg.arguments {
|
|
|
|
syn::PathArguments::AngleBracketed(args) => args.args.first(),
|
|
|
|
_ => None,
|
|
|
|
})?
|
|
|
|
.map(|arg| match arg {
|
|
|
|
syn::GenericArgument::Type(t) => Some(t),
|
|
|
|
_ => None,
|
|
|
|
})?
|
|
|
|
.map(|ty| match ty {
|
|
|
|
syn::Type::BareFn(r) => Some(r),
|
|
|
|
_ => None,
|
|
|
|
})?
|
2017-03-04 03:57:40 +08:00
|
|
|
}
|
2016-06-15 22:34:13 +08:00
|
|
|
}
|