use std::env;
use std::path::{Path, PathBuf};
fn main() {
if env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows") {
println!("cargo:rustc-link-lib=rstrtmgr");
}
if env::var("CARGO_FEATURE_BUNDLED_TEST").is_err() {
return;
}
let duckdb_include = find_duckdb_include();
cc::Build::new()
.cpp(true)
.file("src/testing/bundled_api_init.cpp")
.include(&duckdb_include)
.flag_if_supported("-std=c++11")
.flag_if_supported("-w")
.define("DUCKDB_STATIC_BUILD", None)
.compile("quack_rs_bundled_init");
println!("cargo:rerun-if-changed=src/testing/bundled_api_init.cpp");
}
fn find_duckdb_include() -> PathBuf {
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR not set"));
let build_dir = out_dir
.parent() .and_then(Path::parent) .expect("could not navigate to Cargo build directory from OUT_DIR");
for attempt in 0..120 {
if let Some(path) = scan_for_duckdb_headers(build_dir) {
return path;
}
if attempt < 119 {
std::thread::sleep(std::time::Duration::from_secs(5));
}
}
panic!(
"Could not find DuckDB headers from libduckdb-sys build output.\n\
Ensure that the `duckdb` dependency is resolved with `features = [\"bundled\"]`\n\
and that `libduckdb-sys` has been built before this crate."
);
}
fn scan_for_duckdb_headers(build_dir: &Path) -> Option<PathBuf> {
for entry in std::fs::read_dir(build_dir).ok()?.flatten() {
if !entry
.file_name()
.to_string_lossy()
.starts_with("libduckdb-sys-")
{
continue;
}
let candidate = entry.path().join("out/duckdb/src/include");
if candidate.is_dir() {
return Some(candidate);
}
}
None
}