Skip to content

Commit

Permalink
Merge pull request #561 from tursodatabase/lucio/fix-sys-build
Browse files Browse the repository at this point in the history
Remove precompiled libs and make bindgen optional
  • Loading branch information
penberg committed Nov 3, 2023
2 parents 33f74a3 + 12a160f commit 88c3a2a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 42 deletions.
58 changes: 16 additions & 42 deletions libsql-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,36 @@
use std::env;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::Command;

const SQLITE_DIR: &str = "../libsql-sqlite3";
const LIB_NAME: &str = "libsql";

fn run_make() {
Command::new("./configure")
.current_dir(SQLITE_DIR)
.env("CFLAGS", "-DSQLITE_ENABLE_COLUMN_METADATA=1")
.output()
.unwrap();
Command::new("make")
.current_dir(SQLITE_DIR)
.output()
.unwrap();
}

fn precompiled() -> bool {
std::fs::metadata(Path::new(SQLITE_DIR).join(".libs").join("liblibsql.a")).is_ok()
}

fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let out_path = Path::new(&out_dir).join("bindgen.rs");

// Fast path: liblibsql.a exists and bindings are ready
println!("cargo:rerun-if-env-changed=LIBSQL_REGENERATE_BINDINGS");
if precompiled() && env::var("LIBSQL_REGENERATE_BINDINGS").is_err() {
println!("cargo:rerun-if-changed=bundled/src/");
build_bundled(&out_dir, &out_path);
}

pub fn build_bundled(out_dir: &str, out_path: &Path) {
let bindings_dir = if std::env::var("LIBSQL_REGENERATE_BINDINGS").is_ok() {
let header = HeaderLocation::FromPath(format!("bundled/src/sqlite3.h"));
bindings::write_to_out_dir(header, out_path);
out_path.display().to_string()
} else {
let bindgen_rs_path = if cfg!(feature = "session") {
"bundled/bindings/session_bindgen.rs"
} else {
"bundled/bindings/bindgen.rs"
};
std::fs::copy(Path::new(bindgen_rs_path), &out_path).unwrap();
std::fs::copy(
Path::new(SQLITE_DIR).join(".libs").join("liblibsql.a"),
Path::new(&out_dir).join("liblibsql.a"),
)
.unwrap();
println!("cargo:lib_dir={out_dir}");
println!("cargo:rustc-link-search={out_dir}");
println!("cargo:rustc-link-lib=static={LIB_NAME}");
return;
}
let dir = env!("CARGO_MANIFEST_DIR");
std::fs::copy(format!("{dir}/{bindgen_rs_path}"), out_path).unwrap();
"bundled/src".to_string()
};

println!("cargo:rerun-if-changed={SQLITE_DIR}/src/");
run_make();
build_bundled(&out_dir, &out_path);
}

pub fn build_bundled(out_dir: &str, out_path: &Path) {
let header = HeaderLocation::FromPath(format!("{SQLITE_DIR}/sqlite3.h"));
bindings::write_to_out_dir(header, out_path);
println!("cargo:rerun-if-changed={SQLITE_DIR}/sqlite3.c");
println!("cargo:rerun-if-changed={bindings_dir}/sqlite3.c");
let mut cfg = cc::Build::new();
cfg.file(format!("{SQLITE_DIR}/sqlite3.c"))
cfg.file(format!("{bindings_dir}/sqlite3.c"))
.flag("-DSQLITE_CORE")
.flag("-DSQLITE_DEFAULT_FOREIGN_KEYS=1")
.flag("-DSQLITE_ENABLE_API_ARMOR")
Expand Down
Binary file removed libsql-sys/bundled/aarch64/liblibsql.a
Binary file not shown.
Binary file removed libsql-sys/bundled/x86_64/liblibsql.a
Binary file not shown.

0 comments on commit 88c3a2a

Please sign in to comment.