Allow build time bindgen of bundled SQLite

Rewrite https://github.com/jgallagher/rusqlite/pull/320
This commit is contained in:
gwenn 2018-10-28 18:16:48 +01:00
parent ebf98b4241
commit e5b1af51ab

View File

@ -1,22 +1,34 @@
use std::env;
use std::path::Path;
fn main() { fn main() {
build::main(); let out_dir = env::var("OUT_DIR").unwrap();
let out_path = Path::new(&out_dir).join("bindgen.rs");
build::main(&out_dir, &out_path);
} }
#[cfg(feature = "bundled")] #[cfg(feature = "bundled")]
mod build { mod build {
extern crate cc; extern crate cc;
use std::path::Path; use std::path::Path;
use std::{env, fs};
pub fn main() { pub fn main(out_dir: &str, out_path: &Path) {
if cfg!(feature = "sqlcipher") { if cfg!(feature = "sqlcipher") {
panic!("Builds with bundled SQLCipher are not supported"); panic!("Builds with bundled SQLCipher are not supported");
} }
let out_dir = env::var("OUT_DIR").unwrap(); #[cfg(feature = "buildtime_bindgen")]
let out_path = Path::new(&out_dir).join("bindgen.rs"); {
use super::{bindings, HeaderLocation};
let header = HeaderLocation::FromPath("sqlite3/sqlite3.h".to_owned());
bindings::write_to_out_dir(header, out_path);
}
#[cfg(not(feature = "buildtime_bindgen"))]
{
use std::fs;
fs::copy("sqlite3/bindgen_bundled_version.rs", out_path) fs::copy("sqlite3/bindgen_bundled_version.rs", out_path)
.expect("Could not copy bindings to output directory"); .expect("Could not copy bindings to output directory");
}
let mut cfg = cc::Build::new(); let mut cfg = cc::Build::new();
cfg.file("sqlite3/sqlite3.c") cfg.file("sqlite3/sqlite3.c")
@ -48,14 +60,13 @@ mod build {
} }
} }
#[cfg(not(feature = "bundled"))] fn env_prefix() -> &'static str {
mod build { if cfg!(feature = "sqlcipher") {
extern crate pkg_config; "SQLCIPHER"
} else {
#[cfg(all(feature = "vcpkg", target_env = "msvc"))] "SQLITE3"
extern crate vcpkg; }
}
use std::env;
pub enum HeaderLocation { pub enum HeaderLocation {
FromEnvironment, FromEnvironment,
@ -81,9 +92,20 @@ mod build {
} }
} }
pub fn main() { #[cfg(not(feature = "bundled"))]
mod build {
extern crate pkg_config;
#[cfg(all(feature = "vcpkg", target_env = "msvc"))]
extern crate vcpkg;
use super::{bindings, env_prefix, HeaderLocation};
use std::env;
use std::path::Path;
pub fn main(_out_dir: &str, out_path: &Path) {
let header = find_sqlite(); let header = find_sqlite();
bindings::write_to_out_dir(header); bindings::write_to_out_dir(header, out_path);
} }
// Prints the necessary cargo link commands and returns the path to the header. // Prints the necessary cargo link commands and returns the path to the header.
@ -122,8 +144,8 @@ mod build {
Err(_) => { Err(_) => {
// No env var set and pkg-config couldn't help; just output the link-lib // 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 // 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 // output /usr/lib explicitly, but that can introduce other linking problems;
// https://github.com/jgallagher/rusqlite/issues/207. // see https://github.com/jgallagher/rusqlite/issues/207.
println!("cargo:rustc-link-lib={}", link_lib); println!("cargo:rustc-link-lib={}", link_lib);
HeaderLocation::Wrapper HeaderLocation::Wrapper
} }
@ -147,14 +169,6 @@ mod build {
None None
} }
fn env_prefix() -> &'static str {
if cfg!(feature = "sqlcipher") {
"SQLCIPHER"
} else {
"SQLITE3"
}
}
fn link_lib() -> &'static str { fn link_lib() -> &'static str {
if cfg!(feature = "sqlcipher") { if cfg!(feature = "sqlcipher") {
"sqlcipher" "sqlcipher"
@ -162,13 +176,14 @@ mod build {
"sqlite3" "sqlite3"
} }
} }
}
#[cfg(not(feature = "buildtime_bindgen"))] #[cfg(all(not(feature = "buildtime_bindgen"), not(feature = "bundled")))]
mod bindings { mod bindings {
use super::HeaderLocation; use super::HeaderLocation;
use std::fs;
use std::path::Path; use std::path::Path;
use std::{env, fs};
static PREBUILT_BINDGEN_PATHS: &'static [&'static str] = &[ static PREBUILT_BINDGEN_PATHS: &'static [&'static str] = &[
"bindgen-bindings/bindgen_3.6.8.rs", "bindgen-bindings/bindgen_3.6.8.rs",
@ -178,9 +193,7 @@ mod build {
"bindgen-bindings/bindgen_3.7.7.rs", "bindgen-bindings/bindgen_3.7.7.rs",
]; ];
pub fn write_to_out_dir(_header: HeaderLocation) { pub fn write_to_out_dir(_header: HeaderLocation, out_path: &Path) {
let out_dir = env::var("OUT_DIR").unwrap();
let out_path = Path::new(&out_dir).join("bindgen.rs");
let in_path = PREBUILT_BINDGEN_PATHS[PREBUILT_BINDGEN_PATHS.len() - 1]; let in_path = PREBUILT_BINDGEN_PATHS[PREBUILT_BINDGEN_PATHS.len() - 1];
fs::copy(in_path, out_path).expect("Could not copy bindings to output directory"); fs::copy(in_path, out_path).expect("Could not copy bindings to output directory");
} }
@ -193,7 +206,6 @@ mod build {
use self::bindgen::callbacks::{IntKind, ParseCallbacks}; use self::bindgen::callbacks::{IntKind, ParseCallbacks};
use super::HeaderLocation; use super::HeaderLocation;
use std::env;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
@ -211,9 +223,8 @@ mod build {
} }
} }
pub fn write_to_out_dir(header: HeaderLocation) { pub fn write_to_out_dir(header: HeaderLocation, out_path: &Path) {
let header: String = header.into(); let header: String = header.into();
let out_dir = env::var("OUT_DIR").unwrap();
let mut output = Vec::new(); let mut output = Vec::new();
bindgen::builder() bindgen::builder()
.header(header.clone()) .header(header.clone())
@ -225,25 +236,24 @@ mod build {
.expect("could not write output of bindgen"); .expect("could not write output of bindgen");
let mut output = String::from_utf8(output).expect("bindgen output was not UTF-8?!"); let mut output = String::from_utf8(output).expect("bindgen output was not UTF-8?!");
// rusqlite's functions feature ors in the SQLITE_DETERMINISTIC flag when it can. This flag // rusqlite's functions feature ors in the SQLITE_DETERMINISTIC flag when it
// was added in SQLite 3.8.3, but oring it in in prior versions of SQLite is harmless. We // can. This flag was added in SQLite 3.8.3, but oring it in in prior
// don't want to not build just because this flag is missing (e.g., if we're linking against // versions of SQLite is harmless. We don't want to not build just
// SQLite 3.7.x), so append the flag manually if it isn't present in bindgen's output. // because this flag is missing (e.g., if we're linking against
// SQLite 3.7.x), so append the flag manually if it isn't present in bindgen's
// output.
if !output.contains("pub const SQLITE_DETERMINISTIC") { if !output.contains("pub const SQLITE_DETERMINISTIC") {
output.push_str("\npub const SQLITE_DETERMINISTIC: i32 = 2048;\n"); output.push_str("\npub const SQLITE_DETERMINISTIC: i32 = 2048;\n");
} }
let path = Path::new(&out_dir).join("bindgen.rs");
let mut file = OpenOptions::new() let mut file = OpenOptions::new()
.write(true) .write(true)
.truncate(true) .truncate(true)
.create(true) .create(true)
.open(path.clone()) .open(out_path.clone())
.expect(&format!("Could not write to {:?}", path)); .expect(&format!("Could not write to {:?}", out_path));
file.write_all(output.as_bytes()) file.write_all(output.as_bytes())
.expect(&format!("Could not write to {:?}", path)); .expect(&format!("Could not write to {:?}", out_path));
}
} }
} }