mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
Update build process to use prebuilt bindings.
Adds buildtime_bindgen feature to run bindgen dynamically.
This commit is contained in:
parent
9510e25ef3
commit
06383c65cb
@ -19,11 +19,12 @@ name = "rusqlite"
|
|||||||
|
|
||||||
[features]
|
[features]
|
||||||
load_extension = []
|
load_extension = []
|
||||||
backup = []
|
backup = ["libsqlite3-sys/min_sqlite_version_3_6_11"]
|
||||||
blob = []
|
blob = ["libsqlite3-sys/min_sqlite_version_3_7_4"]
|
||||||
functions = []
|
functions = ["libsqlite3-sys/min_sqlite_version_3_7_3"]
|
||||||
trace = []
|
trace = ["libsqlite3-sys/min_sqlite_version_3_6_23"]
|
||||||
bundled = ["libsqlite3-sys/bundled"]
|
bundled = ["libsqlite3-sys/bundled"]
|
||||||
|
buildtime_bindgen = ["libsqlite3-sys/buildtime_bindgen"]
|
||||||
limits = []
|
limits = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
@ -11,9 +11,16 @@ keywords = ["sqlite", "database", "ffi"]
|
|||||||
categories = ["database", "external-ffi-bindings"]
|
categories = ["database", "external-ffi-bindings"]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
bundled = []
|
default = ["min_sqlite_version_3_6_8"]
|
||||||
|
bundled = ["gcc"]
|
||||||
|
buildtime_bindgen = ["bindgen", "pkg-config"]
|
||||||
|
min_sqlite_version_3_6_8 = ["pkg-config"]
|
||||||
|
min_sqlite_version_3_6_11 = ["pkg-config"]
|
||||||
|
min_sqlite_version_3_6_23 = ["pkg-config"]
|
||||||
|
min_sqlite_version_3_7_3 = ["pkg-config"]
|
||||||
|
min_sqlite_version_3_7_4 = ["pkg-config"]
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
bindgen = "0.21"
|
bindgen = { version = "0.21", optional = true }
|
||||||
pkg-config = "0.3"
|
pkg-config = { version = "0.3", optional = true }
|
||||||
gcc = "0.3"
|
gcc = { version = "0.3", optional = true }
|
||||||
|
@ -1,17 +1,133 @@
|
|||||||
extern crate bindgen;
|
fn main() {
|
||||||
extern crate gcc;
|
build::main();
|
||||||
extern crate pkg_config;
|
}
|
||||||
|
|
||||||
use std::env;
|
#[cfg(feature = "bundled")]
|
||||||
use std::io::Write;
|
mod build {
|
||||||
use std::fs::OpenOptions;
|
extern crate gcc;
|
||||||
use bindgen::chooser::{TypeChooser, IntKind};
|
use std::{env, fs};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
#[derive(Debug)]
|
pub fn main() {
|
||||||
struct SqliteTypeChooser;
|
let out_dir = env::var("OUT_DIR").unwrap();
|
||||||
|
let out_path = Path::new(&out_dir).join("bindgen.rs");
|
||||||
|
fs::copy("sqlite3/bindgen_bundled_version.rs", out_path)
|
||||||
|
.expect("Could not copy bindings to output directory");
|
||||||
|
|
||||||
impl TypeChooser for SqliteTypeChooser {
|
gcc::Config::new()
|
||||||
|
.file("sqlite3/sqlite3.c")
|
||||||
|
.flag("-DSQLITE_CORE")
|
||||||
|
.flag("-DSQLITE_DEFAULT_FOREIGN_KEYS=1")
|
||||||
|
.flag("-DSQLITE_ENABLE_API_ARMOR")
|
||||||
|
.flag("-DSQLITE_ENABLE_COLUMN_METADATA")
|
||||||
|
.flag("-DSQLITE_ENABLE_DBSTAT_VTAB")
|
||||||
|
.flag("-DSQLITE_ENABLE_FTS3")
|
||||||
|
.flag("-DSQLITE_ENABLE_FTS3_PARENTHESIS")
|
||||||
|
.flag("-DSQLITE_ENABLE_FTS5")
|
||||||
|
.flag("-DSQLITE_ENABLE_JSON1")
|
||||||
|
.flag("-DSQLITE_ENABLE_LOAD_EXTENSION=1")
|
||||||
|
.flag("-DSQLITE_ENABLE_MEMORY_MANAGEMENT")
|
||||||
|
.flag("-DSQLITE_ENABLE_RTREE")
|
||||||
|
.flag("-DSQLITE_ENABLE_STAT2")
|
||||||
|
.flag("-DSQLITE_ENABLE_STAT4")
|
||||||
|
.flag("-DSQLITE_HAVE_ISNAN")
|
||||||
|
.flag("-DSQLITE_SOUNDEX")
|
||||||
|
.flag("-DSQLITE_THREADSAFE=1")
|
||||||
|
.flag("-DSQLITE_USE_URI")
|
||||||
|
.compile("libsqlite3.a");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "bundled"))]
|
||||||
|
mod build {
|
||||||
|
extern crate pkg_config;
|
||||||
|
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let header = find_sqlite();
|
||||||
|
bindings::write_to_out_dir(header);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prints the necessary cargo link commands and returns the path to the header.
|
||||||
|
fn find_sqlite() -> String {
|
||||||
|
// Allow users to specify where to find SQLite.
|
||||||
|
if let Ok(dir) = env::var("SQLITE3_LIB_DIR") {
|
||||||
|
let mut header = env::var("SQLITE3_INCLUDE_DIR")
|
||||||
|
.expect("SQLITE3_INCLUDE_DIR must be set if SQLITE3_LIB_DIR is set");
|
||||||
|
header.push_str("/sqlite3.h");
|
||||||
|
//run_bindgen(header);
|
||||||
|
println!("cargo:rustc-link-lib=sqlite3");
|
||||||
|
println!("cargo:rustc-link-search={}", dir);
|
||||||
|
return header;
|
||||||
|
}
|
||||||
|
|
||||||
|
// See if pkg-config can do everything for us.
|
||||||
|
match pkg_config::Config::new().print_system_libs(false).probe("sqlite3") {
|
||||||
|
Ok(mut lib) => {
|
||||||
|
if let Some(mut header) = lib.include_paths.pop() {
|
||||||
|
header.push("sqlite3.h");
|
||||||
|
header.to_string_lossy().into()
|
||||||
|
} else {
|
||||||
|
"wrapper.h".into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
// 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/jgallagher/rusqlite/issues/207.
|
||||||
|
println!("cargo:rustc-link-lib=sqlite3");
|
||||||
|
"wrapper.h".into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "buildtime_bindgen"))]
|
||||||
|
mod bindings {
|
||||||
|
use std::{env, fs};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
#[cfg_attr(rustfmt, rustfmt_skip)]
|
||||||
|
static PREBUILT_BINDGEN_PATHS: &'static [&'static str] = &[
|
||||||
|
"bindgen-bindings/bindgen_3.6.8.rs",
|
||||||
|
|
||||||
|
#[cfg(feature = "min_sqlite_version_3_6_11")]
|
||||||
|
"bindgen-bindings/bindgen_3.6.11.rs",
|
||||||
|
|
||||||
|
#[cfg(feature = "min_sqlite_version_3_6_23")]
|
||||||
|
"bindgen-bindings/bindgen_3.6.23.rs",
|
||||||
|
|
||||||
|
#[cfg(feature = "min_sqlite_version_3_7_3")]
|
||||||
|
"bindgen-bindings/bindgen_3.7.3.rs",
|
||||||
|
|
||||||
|
#[cfg(feature = "min_sqlite_version_3_7_4")]
|
||||||
|
"bindgen-bindings/bindgen_3.7.4.rs",
|
||||||
|
];
|
||||||
|
|
||||||
|
pub fn write_to_out_dir(_header: String) {
|
||||||
|
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];
|
||||||
|
fs::copy(in_path, out_path).expect("Could not copy bindings to output directory");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "buildtime_bindgen")]
|
||||||
|
mod bindings {
|
||||||
|
extern crate bindgen;
|
||||||
|
|
||||||
|
use self::bindgen::chooser::{TypeChooser, IntKind};
|
||||||
|
|
||||||
|
use std::env;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::fs::OpenOptions;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct SqliteTypeChooser;
|
||||||
|
|
||||||
|
impl TypeChooser for SqliteTypeChooser {
|
||||||
fn int_macro(&self, _name: &str, value: i64) -> Option<IntKind> {
|
fn int_macro(&self, _name: &str, value: i64) -> Option<IntKind> {
|
||||||
if value >= i32::min_value() as i64 && value <= i32::max_value() as i64 {
|
if value >= i32::min_value() as i64 && value <= i32::max_value() as i64 {
|
||||||
Some(IntKind::I32)
|
Some(IntKind::I32)
|
||||||
@ -19,11 +135,10 @@ impl TypeChooser for SqliteTypeChooser {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_bindgen<T: Into<String>>(header: T) {
|
pub fn write_to_out_dir(header: String) {
|
||||||
let out_dir = env::var("OUT_DIR").unwrap();
|
let out_dir = env::var("OUT_DIR").unwrap();
|
||||||
let header = header.into();
|
|
||||||
let mut output = Vec::new();
|
let mut output = Vec::new();
|
||||||
bindgen::builder()
|
bindgen::builder()
|
||||||
.header(header.clone())
|
.header(header.clone())
|
||||||
@ -52,65 +167,6 @@ fn run_bindgen<T: Into<String>>(header: T) {
|
|||||||
.expect(&format!("Could not write to {:?}", path));
|
.expect(&format!("Could not write to {:?}", path));
|
||||||
|
|
||||||
file.write_all(output.as_bytes()).expect(&format!("Could not write to {:?}", path));
|
file.write_all(output.as_bytes()).expect(&format!("Could not write to {:?}", path));
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "bundled"))]
|
|
||||||
fn main() {
|
|
||||||
// Allow users to specify where to find SQLite.
|
|
||||||
if let Ok(dir) = env::var("SQLITE3_LIB_DIR") {
|
|
||||||
let mut header = env::var("SQLITE3_INCLUDE_DIR")
|
|
||||||
.expect("SQLITE3_INCLUDE_DIR must be set if SQLITE3_LIB_DIR is set");
|
|
||||||
header.push_str("/sqlite3.h");
|
|
||||||
run_bindgen(header);
|
|
||||||
println!("cargo:rustc-link-lib=sqlite3");
|
|
||||||
println!("cargo:rustc-link-search={}", dir);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// See if pkg-config can do everything for us.
|
|
||||||
match pkg_config::Config::new().print_system_libs(false).probe("sqlite3") {
|
|
||||||
Ok(mut lib) => {
|
|
||||||
if let Some(mut header) = lib.include_paths.pop() {
|
|
||||||
header.push("sqlite3.h");
|
|
||||||
run_bindgen(header.to_string_lossy());
|
|
||||||
} else {
|
|
||||||
run_bindgen("wrapper.h");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
// 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/jgallagher/rusqlite/issues/207.
|
|
||||||
println!("cargo:rustc-link-lib=sqlite3");
|
|
||||||
run_bindgen("wrapper.h");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "bundled")]
|
|
||||||
fn main() {
|
|
||||||
run_bindgen("sqlite3/sqlite3.h");
|
|
||||||
|
|
||||||
gcc::Config::new()
|
|
||||||
.file("sqlite3/sqlite3.c")
|
|
||||||
.flag("-DSQLITE_CORE")
|
|
||||||
.flag("-DSQLITE_DEFAULT_FOREIGN_KEYS=1")
|
|
||||||
.flag("-DSQLITE_ENABLE_API_ARMOR")
|
|
||||||
.flag("-DSQLITE_ENABLE_COLUMN_METADATA")
|
|
||||||
.flag("-DSQLITE_ENABLE_DBSTAT_VTAB")
|
|
||||||
.flag("-DSQLITE_ENABLE_FTS3")
|
|
||||||
.flag("-DSQLITE_ENABLE_FTS3_PARENTHESIS")
|
|
||||||
.flag("-DSQLITE_ENABLE_FTS5")
|
|
||||||
.flag("-DSQLITE_ENABLE_JSON1")
|
|
||||||
.flag("-DSQLITE_ENABLE_LOAD_EXTENSION=1")
|
|
||||||
.flag("-DSQLITE_ENABLE_MEMORY_MANAGEMENT")
|
|
||||||
.flag("-DSQLITE_ENABLE_RTREE")
|
|
||||||
.flag("-DSQLITE_ENABLE_STAT2")
|
|
||||||
.flag("-DSQLITE_ENABLE_STAT4")
|
|
||||||
.flag("-DSQLITE_HAVE_ISNAN")
|
|
||||||
.flag("-DSQLITE_SOUNDEX")
|
|
||||||
.flag("-DSQLITE_THREADSAFE=1")
|
|
||||||
.flag("-DSQLITE_USE_URI")
|
|
||||||
.compile("libsqlite3.a");
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user