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
win_target() -> bool10 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"))]
android_target() -> bool17 fn android_target() -> bool {
18     env::var("CARGO_CFG_TARGET_OS").map_or(false, |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`]
is_compiler(compiler_name: &str) -> bool25 fn is_compiler(compiler_name: &str) -> bool {
26     env::var("CARGO_CFG_TARGET_ENV").map_or(false, |v| v == compiler_name)
27 }
28 
29 /// Copy bindgen file from `dir` to `out_path`.
copy_bindings<T: AsRef<Path>>(dir: &str, bindgen_name: &str, out_path: T)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 
main()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").map_or(false, |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 
main(out_dir: &str, out_path: &Path)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.iter() {
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").map_or(false, |v| v == "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 {} in LIBSQLITE3_FLAGS", extra);
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 
env(name: &str) -> Option<OsString>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 
find_openssl_dir(_host: &str, _target: &str) -> Option<PathBuf>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 
env_prefix() -> &'static str316 fn env_prefix() -> &'static str {
317     if cfg!(any(feature = "sqlcipher", feature = "bundled-sqlcipher")) {
318         "SQLCIPHER"
319     } else {
320         "SQLITE3"
321     }
322 }
323 
lib_name() -> &'static str324 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 {
from(header: HeaderLocation) -> String339     fn from(header: HeaderLocation) -> String {
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 
main(_out_dir: &str, out_path: &Path)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"))]
find_link_mode() -> &'static str401     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.
find_sqlite() -> HeaderLocation410     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 
try_vcpkg() -> Option<HeaderLocation>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 
write_to_out_dir(_header: HeaderLocation, out_path: &Path)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 {
int_macro(&self, name: &str, _value: i64) -> Option<IntKind>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             {
511                 Some(IntKind::UInt)
512             } else {
513                 None
514             }
515         }
516     }
517 
518     // Are we generating the bundled bindings? Used to avoid emitting things
519     // that would be problematic in bundled builds. This env var is set by
520     // `upgrade.sh`.
generating_bundled_bindings() -> bool521     fn generating_bundled_bindings() -> bool {
522         // Hacky way to know if we're generating the bundled bindings
523         println!("cargo:rerun-if-env-changed=LIBSQLITE3_SYS_BUNDLING");
524         match std::env::var("LIBSQLITE3_SYS_BUNDLING") {
525             Ok(v) => v != "0",
526             Err(_) => false,
527         }
528     }
529 
write_to_out_dir(header: HeaderLocation, out_path: &Path)530     pub fn write_to_out_dir(header: HeaderLocation, out_path: &Path) {
531         let header: String = header.into();
532         let mut bindings = bindgen::builder()
533             .default_macro_constant_type(bindgen::MacroTypeVariation::Signed)
534             .disable_nested_struct_naming()
535             .trust_clang_mangling(false)
536             .header(header.clone())
537             .parse_callbacks(Box::new(SqliteTypeChooser));
538         if cfg!(feature = "loadable_extension") {
539             bindings = bindings.ignore_functions(); // see generate_functions
540         } else {
541             bindings = bindings
542                 .blocklist_function("sqlite3_auto_extension")
543                 .raw_line(
544                     r#"extern "C" {
545     pub fn sqlite3_auto_extension(
546         xEntryPoint: ::std::option::Option<
547             unsafe extern "C" fn(
548                 db: *mut sqlite3,
549                 pzErrMsg: *mut *mut ::std::os::raw::c_char,
550                 _: *const sqlite3_api_routines,
551             ) -> ::std::os::raw::c_int,
552         >,
553     ) -> ::std::os::raw::c_int;
554 }"#,
555                 )
556                 .blocklist_function("sqlite3_cancel_auto_extension")
557                 .raw_line(
558                     r#"extern "C" {
559     pub fn sqlite3_cancel_auto_extension(
560         xEntryPoint: ::std::option::Option<
561             unsafe extern "C" fn(
562                 db: *mut sqlite3,
563                 pzErrMsg: *mut *mut ::std::os::raw::c_char,
564                 _: *const sqlite3_api_routines,
565             ) -> ::std::os::raw::c_int,
566         >,
567     ) -> ::std::os::raw::c_int;
568 }"#,
569                 )
570                 .blocklist_function(".*16.*")
571                 .blocklist_function("sqlite3_close_v2")
572                 .blocklist_function("sqlite3_create_collation")
573                 .blocklist_function("sqlite3_create_function")
574                 .blocklist_function("sqlite3_create_module")
575                 .blocklist_function("sqlite3_prepare");
576         }
577 
578         if cfg!(any(feature = "sqlcipher", feature = "bundled-sqlcipher")) {
579             bindings = bindings.clang_arg("-DSQLITE_HAS_CODEC");
580         }
581         if cfg!(feature = "unlock_notify") {
582             bindings = bindings.clang_arg("-DSQLITE_ENABLE_UNLOCK_NOTIFY");
583         }
584         if cfg!(feature = "preupdate_hook") {
585             bindings = bindings.clang_arg("-DSQLITE_ENABLE_PREUPDATE_HOOK");
586         }
587         if cfg!(feature = "session") {
588             bindings = bindings.clang_arg("-DSQLITE_ENABLE_SESSION");
589         }
590 
591         // When cross compiling unless effort is taken to fix the issue, bindgen
592         // will find the wrong headers. There's only one header included by the
593         // amalgamated `sqlite.h`: `stdarg.h`.
594         //
595         // Thankfully, there's almost no case where rust code needs to use
596         // functions taking `va_list` (It's nearly impossible to get a `va_list`
597         // in Rust unless you get passed it by C code for some reason).
598         //
599         // Arguably, we should never be including these, but we include them for
600         // the cases where they aren't totally broken...
601         let target_arch = std::env::var("TARGET").unwrap();
602         let host_arch = std::env::var("HOST").unwrap();
603         let is_cross_compiling = target_arch != host_arch;
604 
605         // Note that when generating the bundled file, we're essentially always
606         // cross compiling.
607         if generating_bundled_bindings() || is_cross_compiling {
608             // Get rid of va_list, as it's not
609             bindings = bindings
610                 .blocklist_function("sqlite3_vmprintf")
611                 .blocklist_function("sqlite3_vsnprintf")
612                 .blocklist_function("sqlite3_str_vappendf")
613                 .blocklist_type("va_list")
614                 .blocklist_item("__.*");
615         }
616 
617         let bindings = bindings
618             .layout_tests(false)
619             .generate()
620             .unwrap_or_else(|_| panic!("could not run bindgen on header {}", header));
621 
622         #[cfg(feature = "loadable_extension")]
623         {
624             let mut output = Vec::new();
625             bindings
626                 .write(Box::new(&mut output))
627                 .expect("could not write output of bindgen");
628             let mut output = String::from_utf8(output).expect("bindgen output was not UTF-8?!");
629             super::loadable_extension::generate_functions(&mut output);
630             std::fs::write(out_path, output.as_bytes())
631                 .unwrap_or_else(|_| panic!("Could not write to {:?}", out_path));
632         }
633         #[cfg(not(feature = "loadable_extension"))]
634         bindings
635             .write_to_file(out_path)
636             .unwrap_or_else(|_| panic!("Could not write to {:?}", out_path));
637     }
638 }
639 
640 #[cfg(all(feature = "buildtime_bindgen", feature = "loadable_extension"))]
641 mod loadable_extension {
642     /// try to generate similar rust code for all `#define sqlite3_xyz
643     /// sqlite3_api->abc` macros` in sqlite3ext.h
generate_functions(output: &mut String)644     pub fn generate_functions(output: &mut String) {
645         // (1) parse sqlite3_api_routines fields from bindgen output
646         let ast: syn::File = syn::parse_str(output).expect("could not parse bindgen output");
647         let sqlite3_api_routines: syn::ItemStruct = ast
648             .items
649             .into_iter()
650             .find_map(|i| {
651                 if let syn::Item::Struct(s) = i {
652                     if s.ident == "sqlite3_api_routines" {
653                         Some(s)
654                     } else {
655                         None
656                     }
657                 } else {
658                     None
659                 }
660             })
661             .expect("could not find sqlite3_api_routines");
662         let sqlite3_api_routines_ident = sqlite3_api_routines.ident;
663         let p_api = quote::format_ident!("p_api");
664         let mut stores = Vec::new();
665         let mut malloc = Vec::new();
666         // (2) `#define sqlite3_xyz sqlite3_api->abc` => `pub unsafe fn
667         // sqlite3_xyz(args) -> ty {...}` for each `abc` field:
668         for field in sqlite3_api_routines.fields {
669             let ident = field.ident.expect("unnamed field");
670             let span = ident.span();
671             let name = ident.to_string();
672             if name.contains("16") {
673                 continue; // skip UTF-16 api as rust uses UTF-8
674             } else if name == "vmprintf" || name == "xvsnprintf" || name == "str_vappendf" {
675                 continue; // skip va_list
676             } else if name == "aggregate_count"
677                 || name == "expired"
678                 || name == "global_recover"
679                 || name == "thread_cleanup"
680                 || name == "transfer_bindings"
681                 || name == "create_collation"
682                 || name == "create_function"
683                 || name == "create_module"
684                 || name == "prepare"
685                 || name == "close_v2"
686             {
687                 continue; // omit deprecated
688             }
689             let sqlite3_name = match name.as_ref() {
690                 "xthreadsafe" => "sqlite3_threadsafe".to_owned(),
691                 "interruptx" => "sqlite3_interrupt".to_owned(),
692                 _ => {
693                     format!("sqlite3_{name}")
694                 }
695             };
696             let ptr_name =
697                 syn::Ident::new(format!("__{}", sqlite3_name.to_uppercase()).as_ref(), span);
698             let sqlite3_fn_name = syn::Ident::new(&sqlite3_name, span);
699             let method =
700                 extract_method(&field.ty).unwrap_or_else(|| panic!("unexpected type for {name}"));
701             let arg_names: syn::punctuated::Punctuated<&syn::Ident, syn::token::Comma> = method
702                 .inputs
703                 .iter()
704                 .map(|i| &i.name.as_ref().unwrap().0)
705                 .collect();
706             let args = &method.inputs;
707             // vtab_config/sqlite3_vtab_config: ok
708             let varargs = &method.variadic;
709             if varargs.is_some() && "db_config" != name && "log" != name && "vtab_config" != name {
710                 continue; // skip ...
711             }
712             let ty = &method.output;
713             let tokens = if "db_config" == name {
714                 quote::quote! {
715                     static #ptr_name: ::std::sync::atomic::AtomicPtr<()> = ::std::sync::atomic::AtomicPtr::new(::std::ptr::null_mut());
716                     pub unsafe fn #sqlite3_fn_name(#args arg3: ::std::os::raw::c_int, arg4: *mut ::std::os::raw::c_int) #ty {
717                         let ptr = #ptr_name.load(::std::sync::atomic::Ordering::Acquire);
718                         assert!(!ptr.is_null(), "SQLite API not initialized");
719                         let fun: unsafe extern "C" fn(#args #varargs) #ty = ::std::mem::transmute(ptr);
720                         (fun)(#arg_names, arg3, arg4)
721                     }
722                 }
723             } else if "log" == name {
724                 quote::quote! {
725                     static #ptr_name: ::std::sync::atomic::AtomicPtr<()> = ::std::sync::atomic::AtomicPtr::new(::std::ptr::null_mut());
726                     pub unsafe fn #sqlite3_fn_name(#args arg3: *const ::std::os::raw::c_char) #ty {
727                         let ptr = #ptr_name.load(::std::sync::atomic::Ordering::Acquire);
728                         assert!(!ptr.is_null(), "SQLite API not initialized");
729                         let fun: unsafe extern "C" fn(#args #varargs) #ty = ::std::mem::transmute(ptr);
730                         (fun)(#arg_names, arg3)
731                     }
732                 }
733             } else {
734                 quote::quote! {
735                     static #ptr_name: ::std::sync::atomic::AtomicPtr<()> = ::std::sync::atomic::AtomicPtr::new(::std::ptr::null_mut());
736                     pub unsafe fn #sqlite3_fn_name(#args) #ty {
737                         let ptr = #ptr_name.load(::std::sync::atomic::Ordering::Acquire);
738                         assert!(!ptr.is_null(), "SQLite API not initialized or SQLite feature omitted");
739                         let fun: unsafe extern "C" fn(#args #varargs) #ty = ::std::mem::transmute(ptr);
740                         (fun)(#arg_names)
741                     }
742                 }
743             };
744             output.push_str(&prettyplease::unparse(
745                 &syn::parse2(tokens).expect("could not parse quote output"),
746             ));
747             output.push('\n');
748             if name == "malloc" {
749                 &mut malloc
750             } else {
751                 &mut stores
752             }
753             .push(quote::quote! {
754                 if let Some(fun) = (*#p_api).#ident {
755                     #ptr_name.store(
756                         fun as usize as *mut (),
757                         ::std::sync::atomic::Ordering::Release,
758                     );
759                 }
760             });
761         }
762         // (3) generate rust code similar to SQLITE_EXTENSION_INIT2 macro
763         let tokens = quote::quote! {
764             /// Like SQLITE_EXTENSION_INIT2 macro
765             pub unsafe fn rusqlite_extension_init2(#p_api: *mut #sqlite3_api_routines_ident) -> ::std::result::Result<(),crate::InitError> {
766                 #(#malloc)* // sqlite3_malloc needed by to_sqlite_error
767                 if let Some(fun) = (*#p_api).libversion_number {
768                     let version = fun();
769                     if SQLITE_VERSION_NUMBER > version {
770                         return Err(crate::InitError::VersionMismatch{compile_time: SQLITE_VERSION_NUMBER, runtime: version});
771                     }
772                 } else {
773                     return Err(crate::InitError::NullFunctionPointer);
774                 }
775                 #(#stores)*
776                 Ok(())
777             }
778         };
779         output.push_str(&prettyplease::unparse(
780             &syn::parse2(tokens).expect("could not parse quote output"),
781         ));
782         output.push('\n');
783     }
784 
extract_method(ty: &syn::Type) -> Option<&syn::TypeBareFn>785     fn extract_method(ty: &syn::Type) -> Option<&syn::TypeBareFn> {
786         match ty {
787             syn::Type::Path(tp) => tp.path.segments.last(),
788             _ => None,
789         }
790         .map(|seg| match &seg.arguments {
791             syn::PathArguments::AngleBracketed(args) => args.args.first(),
792             _ => None,
793         })?
794         .map(|arg| match arg {
795             syn::GenericArgument::Type(t) => Some(t),
796             _ => None,
797         })?
798         .map(|ty| match ty {
799             syn::Type::BareFn(r) => Some(r),
800             _ => None,
801         })?
802     }
803 }
804