• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::env;
2 use std::path::Path;
3 
main()4 fn main() {
5     let out_dir = env::var("OUT_DIR").unwrap();
6     let out_path = Path::new(&out_dir).join("bindgen.rs");
7     if cfg!(feature = "in_gecko") {
8         // When inside mozilla-central, we are included into the build with
9         // sqlite3.o directly, so we don't want to provide any linker arguments.
10         std::fs::copy("sqlite3/bindgen_bundled_version.rs", out_path)
11             .expect("Could not copy bindings to output directory");
12         return;
13     }
14     if cfg!(feature = "sqlcipher") {
15         if cfg!(any(
16             feature = "bundled",
17             all(windows, feature = "bundled-windows")
18         )) {
19             println!(
20                 "cargo:warning=Builds with bundled SQLCipher are not supported. Searching for SQLCipher to link against. \
21                  This can lead to issues if your version of SQLCipher is not up to date!");
22         }
23         build_linked::main(&out_dir, &out_path)
24     } else {
25         // This can't be `cfg!` without always requiring our `mod build_bundled` (and
26         // thus `cc`)
27         #[cfg(any(feature = "bundled", all(windows, feature = "bundled-windows")))]
28         {
29             build_bundled::main(&out_dir, &out_path)
30         }
31         #[cfg(not(any(feature = "bundled", all(windows, feature = "bundled-windows"))))]
32         {
33             build_linked::main(&out_dir, &out_path)
34         }
35     }
36 }
37 
38 #[cfg(any(feature = "bundled", all(windows, feature = "bundled-windows")))]
39 mod build_bundled {
40     use std::env;
41     use std::path::Path;
42 
main(out_dir: &str, out_path: &Path)43     pub fn main(out_dir: &str, out_path: &Path) {
44         if cfg!(feature = "sqlcipher") {
45             // This is just a sanity check, the top level `main` should ensure this.
46             panic!("Builds with bundled SQLCipher are not supported");
47         }
48 
49         #[cfg(feature = "buildtime_bindgen")]
50         {
51             use super::{bindings, HeaderLocation};
52             let header = HeaderLocation::FromPath("sqlite3/sqlite3.h".to_owned());
53             bindings::write_to_out_dir(header, out_path);
54         }
55         #[cfg(not(feature = "buildtime_bindgen"))]
56         {
57             use std::fs;
58             fs::copy("sqlite3/bindgen_bundled_version.rs", out_path)
59                 .expect("Could not copy bindings to output directory");
60         }
61         println!("cargo:rerun-if-changed=sqlite3/sqlite3.c");
62         println!("cargo:rerun-if-changed=sqlite3/wasm32-wasi-vfs.c");
63         let mut cfg = cc::Build::new();
64         cfg.file("sqlite3/sqlite3.c")
65             .flag("-DSQLITE_CORE")
66             .flag("-DSQLITE_DEFAULT_FOREIGN_KEYS=1")
67             .flag("-DSQLITE_ENABLE_API_ARMOR")
68             .flag("-DSQLITE_ENABLE_COLUMN_METADATA")
69             .flag("-DSQLITE_ENABLE_DBSTAT_VTAB")
70             .flag("-DSQLITE_ENABLE_FTS3")
71             .flag("-DSQLITE_ENABLE_FTS3_PARENTHESIS")
72             .flag("-DSQLITE_ENABLE_FTS5")
73             .flag("-DSQLITE_ENABLE_JSON1")
74             .flag("-DSQLITE_ENABLE_LOAD_EXTENSION=1")
75             .flag("-DSQLITE_ENABLE_MEMORY_MANAGEMENT")
76             .flag("-DSQLITE_ENABLE_RTREE")
77             .flag("-DSQLITE_ENABLE_STAT2")
78             .flag("-DSQLITE_ENABLE_STAT4")
79             .flag("-DSQLITE_SOUNDEX")
80             .flag("-DSQLITE_THREADSAFE=1")
81             .flag("-DSQLITE_USE_URI")
82             .flag("-DHAVE_USLEEP=1")
83             .flag("-D_POSIX_THREAD_SAFE_FUNCTIONS") // cross compile with MinGW
84             .warnings(false);
85 
86         // on android sqlite can't figure out where to put the temp files.
87         // the bundled sqlite on android also uses `SQLITE_TEMP_STORE=3`.
88         // https://android.googlesource.com/platform/external/sqlite/+/2c8c9ae3b7e6f340a19a0001c2a889a211c9d8b2/dist/Android.mk
89         if cfg!(target_os = "android") {
90             cfg.flag("-DSQLITE_TEMP_STORE=3");
91         }
92 
93         if cfg!(feature = "with-asan") {
94             cfg.flag("-fsanitize=address");
95         }
96 
97         // Older versions of visual studio don't support c99 (including isnan), which
98         // causes a build failure when the linker fails to find the `isnan`
99         // function. `sqlite` provides its own implementation, using the fact
100         // that x != x when x is NaN.
101         //
102         // There may be other platforms that don't support `isnan`, they should be
103         // tested for here.
104         if cfg!(target_env = "msvc") {
105             use cc::windows_registry::{find_vs_version, VsVers};
106             let vs_has_nan = match find_vs_version() {
107                 Ok(ver) => ver != VsVers::Vs12,
108                 Err(_msg) => false,
109             };
110             if vs_has_nan {
111                 cfg.flag("-DHAVE_ISNAN");
112             }
113         } else {
114             cfg.flag("-DHAVE_ISNAN");
115         }
116         if cfg!(not(target_os = "windows")) {
117             cfg.flag("-DHAVE_LOCALTIME_R");
118         }
119         // Target wasm32-wasi can't compile the default VFS
120         if env::var("TARGET") == Ok("wasm32-wasi".to_string()) {
121             cfg.flag("-DSQLITE_OS_OTHER")
122                 // https://github.com/rust-lang/rust/issues/74393
123                 .flag("-DLONGDOUBLE_TYPE=double");
124             if cfg!(feature = "wasm32-wasi-vfs") {
125                 cfg.file("sqlite3/wasm32-wasi-vfs.c");
126             }
127         }
128         if cfg!(feature = "unlock_notify") {
129             cfg.flag("-DSQLITE_ENABLE_UNLOCK_NOTIFY");
130         }
131         if cfg!(feature = "preupdate_hook") {
132             cfg.flag("-DSQLITE_ENABLE_PREUPDATE_HOOK");
133         }
134         if cfg!(feature = "session") {
135             cfg.flag("-DSQLITE_ENABLE_SESSION");
136         }
137 
138         if let Ok(limit) = env::var("SQLITE_MAX_VARIABLE_NUMBER") {
139             cfg.flag(&format!("-DSQLITE_MAX_VARIABLE_NUMBER={}", limit));
140         }
141         println!("cargo:rerun-if-env-changed=SQLITE_MAX_VARIABLE_NUMBER");
142 
143         if let Ok(limit) = env::var("SQLITE_MAX_EXPR_DEPTH") {
144             cfg.flag(&format!("-DSQLITE_MAX_EXPR_DEPTH={}", limit));
145         }
146         println!("cargo:rerun-if-env-changed=SQLITE_MAX_EXPR_DEPTH");
147 
148         if let Ok(extras) = env::var("LIBSQLITE3_FLAGS") {
149             for extra in extras.split_whitespace() {
150                 if extra.starts_with("-D") || extra.starts_with("-U") {
151                     cfg.flag(extra);
152                 } else if extra.starts_with("SQLITE_") {
153                     cfg.flag(&format!("-D{}", extra));
154                 } else {
155                     panic!("Don't understand {} in LIBSQLITE3_FLAGS", extra);
156                 }
157             }
158         }
159         println!("cargo:rerun-if-env-changed=LIBSQLITE3_FLAGS");
160 
161         cfg.compile("libsqlite3.a");
162 
163         println!("cargo:lib_dir={}", out_dir);
164     }
165 }
166 
env_prefix() -> &'static str167 fn env_prefix() -> &'static str {
168     if cfg!(feature = "sqlcipher") {
169         "SQLCIPHER"
170     } else {
171         "SQLITE3"
172     }
173 }
174 
175 pub enum HeaderLocation {
176     FromEnvironment,
177     Wrapper,
178     FromPath(String),
179 }
180 
181 impl From<HeaderLocation> for String {
from(header: HeaderLocation) -> String182     fn from(header: HeaderLocation) -> String {
183         match header {
184             HeaderLocation::FromEnvironment => {
185                 let prefix = env_prefix();
186                 let mut header = env::var(format!("{}_INCLUDE_DIR", prefix)).unwrap_or_else(|_| {
187                     panic!(
188                         "{}_INCLUDE_DIR must be set if {}_LIB_DIR is set",
189                         prefix, prefix
190                     )
191                 });
192                 header.push_str("/sqlite3.h");
193                 header
194             }
195             HeaderLocation::Wrapper => "wrapper.h".into(),
196             HeaderLocation::FromPath(path) => path,
197         }
198     }
199 }
200 
201 mod build_linked {
202     #[cfg(all(feature = "vcpkg", target_env = "msvc"))]
203     extern crate vcpkg;
204 
205     use super::{bindings, env_prefix, HeaderLocation};
206     use std::env;
207     use std::path::Path;
208 
main(_out_dir: &str, out_path: &Path)209     pub fn main(_out_dir: &str, out_path: &Path) {
210         let header = find_sqlite();
211         if cfg!(any(
212             feature = "bundled_bindings",
213             feature = "bundled",
214             all(windows, feature = "bundled-windows")
215         )) && !cfg!(feature = "buildtime_bindgen")
216         {
217             // Generally means the `bundled_bindings` feature is enabled
218             // (there's also an edge case where we get here involving
219             // sqlcipher). In either case most users are better off with turning
220             // on buildtime_bindgen instead, but this is still supported as we
221             // have runtime version checks and there are good reasons to not
222             // want to run bindgen.
223             std::fs::copy("sqlite3/bindgen_bundled_version.rs", out_path)
224                 .expect("Could not copy bindings to output directory");
225         } else {
226             bindings::write_to_out_dir(header, out_path);
227         }
228     }
229 
find_link_mode() -> &'static str230     fn find_link_mode() -> &'static str {
231         // If the user specifies SQLITE3_STATIC (or SQLCIPHER_STATIC), do static
232         // linking, unless it's explicitly set to 0.
233         match &env::var(format!("{}_STATIC", env_prefix())) {
234             Ok(v) if v != "0" => "static",
235             _ => "dylib",
236         }
237     }
238     // Prints the necessary cargo link commands and returns the path to the header.
find_sqlite() -> HeaderLocation239     fn find_sqlite() -> HeaderLocation {
240         let link_lib = link_lib();
241 
242         println!("cargo:rerun-if-env-changed={}_INCLUDE_DIR", env_prefix());
243         println!("cargo:rerun-if-env-changed={}_LIB_DIR", env_prefix());
244         println!("cargo:rerun-if-env-changed={}_STATIC", env_prefix());
245         if cfg!(all(feature = "vcpkg", target_env = "msvc")) {
246             println!("cargo:rerun-if-env-changed=VCPKGRS_DYNAMIC");
247         }
248 
249         // dependents can access `DEP_SQLITE3_LINK_TARGET` (`sqlite3` being the
250         // `links=` value in our Cargo.toml) to get this value. This might be
251         // useful if you need to ensure whatever crypto library sqlcipher relies
252         // on is available, for example.
253         println!("cargo:link-target={}", link_lib);
254 
255         if cfg!(all(windows, feature = "winsqlite3")) {
256             println!("cargo:rustc-link-lib=dylib={}", link_lib);
257             return HeaderLocation::Wrapper;
258         }
259 
260         // Allow users to specify where to find SQLite.
261         if let Ok(dir) = env::var(format!("{}_LIB_DIR", env_prefix())) {
262             // Try to use pkg-config to determine link commands
263             let pkgconfig_path = Path::new(&dir).join("pkgconfig");
264             env::set_var("PKG_CONFIG_PATH", pkgconfig_path);
265             if pkg_config::Config::new().probe(link_lib).is_err() {
266                 // Otherwise just emit the bare minimum link commands.
267                 println!("cargo:rustc-link-lib={}={}", find_link_mode(), link_lib);
268                 println!("cargo:rustc-link-search={}", dir);
269             }
270             return HeaderLocation::FromEnvironment;
271         }
272 
273         if let Some(header) = try_vcpkg() {
274             return header;
275         }
276 
277         // See if pkg-config can do everything for us.
278         match pkg_config::Config::new()
279             .print_system_libs(false)
280             .probe(link_lib)
281         {
282             Ok(mut lib) => {
283                 if let Some(mut header) = lib.include_paths.pop() {
284                     header.push("sqlite3.h");
285                     HeaderLocation::FromPath(header.to_string_lossy().into())
286                 } else {
287                     HeaderLocation::Wrapper
288                 }
289             }
290             Err(_) => {
291                 // No env var set and pkg-config couldn't help; just output the link-lib
292                 // request and hope that the library exists on the system paths. We used to
293                 // output /usr/lib explicitly, but that can introduce other linking problems;
294                 // see https://github.com/rusqlite/rusqlite/issues/207.
295                 println!("cargo:rustc-link-lib={}={}", find_link_mode(), link_lib);
296                 HeaderLocation::Wrapper
297             }
298         }
299     }
300 
301     #[cfg(all(feature = "vcpkg", target_env = "msvc"))]
try_vcpkg() -> Option<HeaderLocation>302     fn try_vcpkg() -> Option<HeaderLocation> {
303         // See if vcpkg can find it.
304         if let Ok(mut lib) = vcpkg::Config::new().probe(link_lib()) {
305             if let Some(mut header) = lib.include_paths.pop() {
306                 header.push("sqlite3.h");
307                 return Some(HeaderLocation::FromPath(header.to_string_lossy().into()));
308             }
309         }
310         None
311     }
312 
313     #[cfg(not(all(feature = "vcpkg", target_env = "msvc")))]
try_vcpkg() -> Option<HeaderLocation>314     fn try_vcpkg() -> Option<HeaderLocation> {
315         None
316     }
317 
link_lib() -> &'static str318     fn link_lib() -> &'static str {
319         if cfg!(feature = "sqlcipher") {
320             "sqlcipher"
321         } else if cfg!(all(windows, feature = "winsqlite3")) {
322             "winsqlite3"
323         } else {
324             "sqlite3"
325         }
326     }
327 }
328 
329 #[cfg(not(feature = "buildtime_bindgen"))]
330 mod bindings {
331     use super::HeaderLocation;
332 
333     use std::fs;
334     use std::path::Path;
335 
336     static PREBUILT_BINDGEN_PATHS: &[&str] = &[
337         "bindgen-bindings/bindgen_3.6.8.rs",
338         #[cfg(feature = "min_sqlite_version_3_6_23")]
339         "bindgen-bindings/bindgen_3.6.23.rs",
340         #[cfg(feature = "min_sqlite_version_3_7_7")]
341         "bindgen-bindings/bindgen_3.7.7.rs",
342         #[cfg(feature = "min_sqlite_version_3_7_16")]
343         "bindgen-bindings/bindgen_3.7.16.rs",
344     ];
345 
write_to_out_dir(_header: HeaderLocation, out_path: &Path)346     pub fn write_to_out_dir(_header: HeaderLocation, out_path: &Path) {
347         let in_path = PREBUILT_BINDGEN_PATHS[PREBUILT_BINDGEN_PATHS.len() - 1];
348         fs::copy(in_path, out_path).expect("Could not copy bindings to output directory");
349     }
350 }
351 
352 #[cfg(feature = "buildtime_bindgen")]
353 mod bindings {
354     use super::HeaderLocation;
355     use bindgen::callbacks::{IntKind, ParseCallbacks};
356 
357     use std::fs::OpenOptions;
358     use std::io::Write;
359     use std::path::Path;
360 
361     #[derive(Debug)]
362     struct SqliteTypeChooser;
363 
364     impl ParseCallbacks for SqliteTypeChooser {
int_macro(&self, _name: &str, value: i64) -> Option<IntKind>365         fn int_macro(&self, _name: &str, value: i64) -> Option<IntKind> {
366             if value >= i32::min_value() as i64 && value <= i32::max_value() as i64 {
367                 Some(IntKind::I32)
368             } else {
369                 None
370             }
371         }
372     }
373 
374     // Are we generating the bundled bindings? Used to avoid emitting things
375     // that would be problematic in bundled builds. This env var is set by
376     // `upgrade.sh`.
generating_bundled_bindings() -> bool377     fn generating_bundled_bindings() -> bool {
378         // Hacky way to know if we're generating the bundled bindings
379         println!("cargo:rerun-if-env-changed=LIBSQLITE3_SYS_BUNDLING");
380         match std::env::var("LIBSQLITE3_SYS_BUNDLING") {
381             Ok(v) => v != "0",
382             Err(_) => false,
383         }
384     }
385 
write_to_out_dir(header: HeaderLocation, out_path: &Path)386     pub fn write_to_out_dir(header: HeaderLocation, out_path: &Path) {
387         let header: String = header.into();
388         let mut output = Vec::new();
389         let mut bindings = bindgen::builder()
390             .header(header.clone())
391             .parse_callbacks(Box::new(SqliteTypeChooser))
392             .rustfmt_bindings(true);
393 
394         if cfg!(feature = "unlock_notify") {
395             bindings = bindings.clang_arg("-DSQLITE_ENABLE_UNLOCK_NOTIFY");
396         }
397         if cfg!(feature = "preupdate_hook") {
398             bindings = bindings.clang_arg("-DSQLITE_ENABLE_PREUPDATE_HOOK");
399         }
400         if cfg!(feature = "session") {
401             bindings = bindings.clang_arg("-DSQLITE_ENABLE_SESSION");
402         }
403         if cfg!(all(windows, feature = "winsqlite3")) {
404             bindings = bindings
405                 .clang_arg("-DBINDGEN_USE_WINSQLITE3")
406                 .blocklist_item("NTDDI_.+")
407                 .blocklist_item("WINAPI_FAMILY.*")
408                 .blocklist_item("_WIN32_.+")
409                 .blocklist_item("_VCRT_COMPILER_PREPROCESSOR")
410                 .blocklist_item("_SAL_VERSION")
411                 .blocklist_item("__SAL_H_VERSION")
412                 .blocklist_item("_USE_DECLSPECS_FOR_SAL")
413                 .blocklist_item("_USE_ATTRIBUTES_FOR_SAL")
414                 .blocklist_item("_CRT_PACKING")
415                 .blocklist_item("_HAS_EXCEPTIONS")
416                 .blocklist_item("_STL_LANG")
417                 .blocklist_item("_HAS_CXX17")
418                 .blocklist_item("_HAS_CXX20")
419                 .blocklist_item("_HAS_NODISCARD")
420                 .blocklist_item("WDK_NTDDI_VERSION")
421                 .blocklist_item("OSVERSION_MASK")
422                 .blocklist_item("SPVERSION_MASK")
423                 .blocklist_item("SUBVERSION_MASK")
424                 .blocklist_item("WINVER")
425                 .blocklist_item("__security_cookie")
426                 .blocklist_type("size_t")
427                 .blocklist_type("__vcrt_bool")
428                 .blocklist_type("wchar_t")
429                 .blocklist_function("__security_init_cookie")
430                 .blocklist_function("__report_gsfailure")
431                 .blocklist_function("__va_start");
432         }
433 
434         // When cross compiling unless effort is taken to fix the issue, bindgen
435         // will find the wrong headers. There's only one header included by the
436         // amalgamated `sqlite.h`: `stdarg.h`.
437         //
438         // Thankfully, there's almost no case where rust code needs to use
439         // functions taking `va_list` (It's nearly impossible to get a `va_list`
440         // in Rust unless you get passed it by C code for some reason).
441         //
442         // Arguably, we should never be including these, but we include them for
443         // the cases where they aren't totally broken...
444         let target_arch = std::env::var("TARGET").unwrap();
445         let host_arch = std::env::var("HOST").unwrap();
446         let is_cross_compiling = target_arch != host_arch;
447 
448         // Note that when generating the bundled file, we're essentially always
449         // cross compiling.
450         if generating_bundled_bindings() || is_cross_compiling {
451             // Get rid of va_list, as it's not
452             bindings = bindings
453                 .blocklist_function("sqlite3_vmprintf")
454                 .blocklist_function("sqlite3_vsnprintf")
455                 .blocklist_function("sqlite3_str_vappendf")
456                 .blocklist_type("va_list")
457                 .blocklist_type("__builtin_va_list")
458                 .blocklist_type("__gnuc_va_list")
459                 .blocklist_type("__va_list_tag")
460                 .blocklist_item("__GNUC_VA_LIST");
461         }
462 
463         bindings
464             .generate()
465             .unwrap_or_else(|_| panic!("could not run bindgen on header {}", header))
466             .write(Box::new(&mut output))
467             .expect("could not write output of bindgen");
468         let mut output = String::from_utf8(output).expect("bindgen output was not UTF-8?!");
469 
470         // rusqlite's functions feature ors in the SQLITE_DETERMINISTIC flag when it
471         // can. This flag was added in SQLite 3.8.3, but oring it in in prior
472         // versions of SQLite is harmless. We don't want to not build just
473         // because this flag is missing (e.g., if we're linking against
474         // SQLite 3.7.x), so append the flag manually if it isn't present in bindgen's
475         // output.
476         if !output.contains("pub const SQLITE_DETERMINISTIC") {
477             output.push_str("\npub const SQLITE_DETERMINISTIC: i32 = 2048;\n");
478         }
479 
480         let mut file = OpenOptions::new()
481             .write(true)
482             .truncate(true)
483             .create(true)
484             .open(out_path)
485             .unwrap_or_else(|_| panic!("Could not write to {:?}", out_path));
486 
487         file.write_all(output.as_bytes())
488             .unwrap_or_else(|_| panic!("Could not write to {:?}", out_path));
489     }
490 }
491