From 288aa961a75ac29f704bfe2f9794148e0a197e10 Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Fri, 10 Apr 2020 01:38:55 -0700 Subject: [PATCH] Exclude va_list and functions using it from the bundled bindings file --- libsqlite3-sys/build.rs | 41 +++++++++++++++++++ .../sqlite3/bindgen_bundled_version.rs | 25 ----------- libsqlite3-sys/upgrade.sh | 2 +- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/libsqlite3-sys/build.rs b/libsqlite3-sys/build.rs index 3f130ca..e539d2f 100644 --- a/libsqlite3-sys/build.rs +++ b/libsqlite3-sys/build.rs @@ -325,6 +325,18 @@ mod bindings { } } + // 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"); + match std::env::var("LIBSQLITE3_SYS_BUNDLING") { + Ok(v) if v != "0" => true, + _ => false, + } + } + pub fn write_to_out_dir(header: HeaderLocation, out_path: &Path) { let header: String = header.into(); let mut output = Vec::new(); @@ -343,6 +355,35 @@ mod bindings { bindings = bindings.clang_arg("-DSQLITE_ENABLE_SESSION"); } + // 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 + .blacklist_function("sqlite3_vmprintf") + .blacklist_function("sqlite3_vsnprintf") + .blacklist_function("sqlite3_str_vappendf") + .blacklist_type("va_list") + .blacklist_type("__builtin_va_list") + .blacklist_type("__gnuc_va_list") + .blacklist_type("__va_list_tag") + .blacklist_item("__GNUC_VA_LIST"); + } + bindings .generate() .unwrap_or_else(|_| panic!("could not run bindgen on header {}", header)) diff --git a/libsqlite3-sys/sqlite3/bindgen_bundled_version.rs b/libsqlite3-sys/sqlite3/bindgen_bundled_version.rs index c929655..6f6a56f 100644 --- a/libsqlite3-sys/sqlite3/bindgen_bundled_version.rs +++ b/libsqlite3-sys/sqlite3/bindgen_bundled_version.rs @@ -1,6 +1,5 @@ /* automatically generated by rust-bindgen */ -pub const __GNUC_VA_LIST: i32 = 1; pub const SQLITE_VERSION: &'static [u8; 7usize] = b"3.31.0\0"; pub const SQLITE_VERSION_NUMBER: i32 = 3031000; pub const SQLITE_SOURCE_ID: &'static [u8; 85usize] = @@ -437,8 +436,6 @@ pub const FTS5_TOKENIZE_PREFIX: i32 = 2; pub const FTS5_TOKENIZE_DOCUMENT: i32 = 4; pub const FTS5_TOKENIZE_AUX: i32 = 8; pub const FTS5_TOKEN_COLOCATED: i32 = 1; -pub type va_list = __builtin_va_list; -pub type __gnuc_va_list = __builtin_va_list; extern "C" { pub static mut sqlite3_version: [::std::os::raw::c_char; 0usize]; } @@ -1411,12 +1408,6 @@ extern "C" { pub fn sqlite3_mprintf(arg1: *const ::std::os::raw::c_char, ...) -> *mut ::std::os::raw::c_char; } -extern "C" { - pub fn sqlite3_vmprintf( - arg1: *const ::std::os::raw::c_char, - arg2: va_list, - ) -> *mut ::std::os::raw::c_char; -} extern "C" { pub fn sqlite3_snprintf( arg1: ::std::os::raw::c_int, @@ -1425,14 +1416,6 @@ extern "C" { ... ) -> *mut ::std::os::raw::c_char; } -extern "C" { - pub fn sqlite3_vsnprintf( - arg1: ::std::os::raw::c_int, - arg2: *mut ::std::os::raw::c_char, - arg3: *const ::std::os::raw::c_char, - arg4: va_list, - ) -> *mut ::std::os::raw::c_char; -} extern "C" { pub fn sqlite3_malloc(arg1: ::std::os::raw::c_int) -> *mut ::std::os::raw::c_void; } @@ -3617,13 +3600,6 @@ extern "C" { extern "C" { pub fn sqlite3_str_appendf(arg1: *mut sqlite3_str, zFormat: *const ::std::os::raw::c_char, ...); } -extern "C" { - pub fn sqlite3_str_vappendf( - arg1: *mut sqlite3_str, - zFormat: *const ::std::os::raw::c_char, - arg2: va_list, - ); -} extern "C" { pub fn sqlite3_str_append( arg1: *mut sqlite3_str, @@ -5217,4 +5193,3 @@ fn bindgen_test_layout_fts5_api() { ) ); } -pub type __builtin_va_list = *mut ::std::os::raw::c_char; diff --git a/libsqlite3-sys/upgrade.sh b/libsqlite3-sys/upgrade.sh index df35f88..0cbdd91 100755 --- a/libsqlite3-sys/upgrade.sh +++ b/libsqlite3-sys/upgrade.sh @@ -17,7 +17,7 @@ export SQLITE3_INCLUDE_DIR=$SQLITE3_LIB_DIR cargo update # Just to make sure there is only one bindgen.rs file in target dir find $SCRIPT_DIR/../target -type f -name bindgen.rs -exec rm {} \; -cargo build --features "buildtime_bindgen" --no-default-features +env LIBSQLITE3_SYS_BUNDLING=1 cargo build --features "buildtime_bindgen" --no-default-features find $SCRIPT_DIR/../target -type f -name bindgen.rs -exec cp {} $SQLITE3_LIB_DIR/bindgen_bundled_version.rs \; # Sanity check cd $SCRIPT_DIR/..