use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
const CHDB_ENGINE_PIN: &str = "v26.11.0-rc.2";
fn main() {
println!("cargo::rustc-check-cfg=cfg(direct_arrow_insert)");
println!("cargo::rustc-check-cfg=cfg(has_chdb_version)");
println!("cargo::rustc-check-cfg=cfg(has_durable_abi)");
if env::var("DOCS_RS").is_ok() {
publish_engine_provenance(None, "none: docs.rs build", Path::new(""));
return;
}
emit_test_macos_floor();
let out_dir = env::var("OUT_DIR").unwrap();
let out_path = PathBuf::from(&out_dir);
let libchdb_info = find_libchdb_or_download(&out_path);
match libchdb_info {
Ok(engine) => {
publish_engine_provenance(engine.version.as_deref(), &engine.source, &engine.lib_path);
setup_link_paths(&engine.lib_path, &engine.header_path, &out_path);
generate_bindings(&engine.header_path, &out_path);
}
Err(e) => {
eprintln!("Failed to find or download libchdb: {e}");
println!("cargo:warning=Failed to find libchdb: {e}");
println!("cargo:warning=Install one with './update_libchdb.sh --local' or '--global', or set CHDB_LIB_DIR (plus CHDB_INCLUDE_DIR when the header is not next to the library)");
std::process::exit(1);
}
}
}
struct Engine {
lib_path: PathBuf,
header_path: PathBuf,
version: Option<String>,
source: String,
}
fn publish_engine_provenance(version: Option<&str>, source: &str, lib_path: &Path) {
println!(
"cargo:rustc-env=CHDB_EXPECTED_ENGINE_VERSION={}",
version.unwrap_or_default()
);
println!("cargo:rustc-env=CHDB_ENGINE_SOURCE={source}");
println!(
"cargo:rustc-env=CHDB_LINKED_ARTIFACT={}",
lib_path.display()
);
}
const TEST_MIN_MACOS_FLOOR: u32 = 12;
fn emit_test_macos_floor() {
println!("cargo:rerun-if-env-changed=CHDB_TEST_MIN_MACOS");
let Some(floor) = env_dir("CHDB_TEST_MIN_MACOS") else {
return;
};
let Some(floor) = floor.to_str() else {
println!("cargo:warning=CHDB_TEST_MIN_MACOS is not a version number");
std::process::exit(1);
};
if target_platform().0 != "macos" {
println!("cargo:warning=CHDB_TEST_MIN_MACOS={floor} ignored: the target is not macOS");
return;
}
let major = floor
.split('.')
.next()
.and_then(|major| major.parse::<u32>().ok());
match major {
Some(major) if major >= TEST_MIN_MACOS_FLOOR => {
println!("cargo:rustc-link-arg-tests=-mmacosx-version-min={floor}");
}
Some(_) => {
println!("cargo:warning=CHDB_TEST_MIN_MACOS={floor} is below {TEST_MIN_MACOS_FLOOR}: the linker emits no chained fixups under that, so the tests would pass without exercising the binding this floor exists to exercise");
std::process::exit(1);
}
None => {
println!("cargo:warning=CHDB_TEST_MIN_MACOS={floor} does not start with a major version number");
std::process::exit(1);
}
}
}
fn is_static() -> bool {
env::var_os("CARGO_FEATURE_STATIC").is_some()
}
fn lib_file_names() -> &'static [&'static str] {
if is_static() {
&["libchdb.a"]
} else if target_platform().0 == "macos" {
&["libchdb.so", "libchdb.dylib"]
} else {
&["libchdb.so"]
}
}
fn find_lib_in(dir: &Path) -> Option<PathBuf> {
lib_file_names()
.iter()
.map(|name| dir.join(name))
.find(|path| path.exists())
}
fn env_dir(var: &str) -> Option<PathBuf> {
env::var_os(var)
.filter(|value| !value.is_empty())
.map(PathBuf::from)
}
fn find_libchdb_or_download(out_dir: &Path) -> Result<Engine, Box<dyn std::error::Error>> {
println!("cargo:rerun-if-env-changed=CHDB_LIB_DIR");
println!("cargo:rerun-if-env-changed=CHDB_INCLUDE_DIR");
let header_override = header_from_env()?;
if let Some(lib_dir) = env_dir("CHDB_LIB_DIR") {
return libchdb_from_lib_dir(&lib_dir, header_override);
}
if let Some((lib_path, header_path)) = find_existing_libchdb() {
let source = format!("local: {}", lib_path.display());
return Ok(Engine {
lib_path,
header_path: header_override.unwrap_or(header_path),
version: None,
source,
});
}
println!("cargo:warning=libchdb not found locally, fetching it");
let (engine_dir, version) = fetch_engine(out_dir)?;
let lib_path = find_lib_in(&engine_dir).ok_or_else(|| {
format!(
"the archive contains none of {:?}, which the {} linkage needs",
lib_file_names(),
linkage_name()
)
})?;
let header_path = header_override.unwrap_or_else(|| engine_dir.join("chdb.h"));
if !header_path.exists() {
return Err(format!("no chdb.h in {}", engine_dir.display()).into());
}
Ok(Engine {
source: format!("download: chdb-core {version}"),
version: Some(version.trim_start_matches('v').to_string()),
lib_path,
header_path,
})
}
fn linkage_name() -> &'static str {
if is_static() {
"static"
} else {
"dynamic"
}
}
fn header_from_env() -> Result<Option<PathBuf>, Box<dyn std::error::Error>> {
let Some(include_dir) = env_dir("CHDB_INCLUDE_DIR") else {
return Ok(None);
};
let header_path = include_dir.join("chdb.h");
if !header_path.exists() {
return Err(format!(
"CHDB_INCLUDE_DIR is {} but there is no chdb.h in it",
include_dir.display()
)
.into());
}
Ok(Some(header_path))
}
fn libchdb_from_lib_dir(
lib_dir: &Path,
header_override: Option<PathBuf>,
) -> Result<Engine, Box<dyn std::error::Error>> {
let lib_path = find_lib_in(lib_dir).ok_or_else(|| {
format!(
"CHDB_LIB_DIR is {} but it contains none of {:?}, which the {} linkage needs",
lib_dir.display(),
lib_file_names(),
linkage_name()
)
})?;
let header_path = match header_override {
Some(header_path) => header_path,
None => {
let adjacent = lib_dir.join("chdb.h");
if !adjacent.exists() {
return Err(format!(
"found {} but no chdb.h next to it; set CHDB_INCLUDE_DIR to the directory \
holding the matching header (a chdb-core build tree keeps it in \
programs/local)",
lib_path.display()
)
.into());
}
adjacent
}
};
println!(
"cargo:warning=using libchdb from CHDB_LIB_DIR: {} (header {})",
lib_path.display(),
header_path.display()
);
Ok(Engine {
source: format!("CHDB_LIB_DIR: {}", lib_path.display()),
version: None,
lib_path,
header_path,
})
}
fn find_existing_libchdb() -> Option<(PathBuf, PathBuf)> {
if let Some(lib_path) = find_lib_in(Path::new(".")) {
if Path::new("./chdb.h").exists() {
return Some((lib_path, PathBuf::from("./chdb.h")));
}
}
let system_lib_path = Path::new("/usr/local/lib");
let system_header_path = Path::new("/usr/local/include/chdb.h");
if system_header_path.exists() {
if let Some(lib_path) = find_lib_in(system_lib_path) {
return Some((lib_path, system_header_path.to_path_buf()));
}
}
None
}
fn engine_tag_to_download() -> String {
println!("cargo:rerun-if-env-changed=CHDB_ENGINE_VERSION");
env::var("CHDB_ENGINE_VERSION")
.ok()
.filter(|v| !v.is_empty())
.unwrap_or_else(|| CHDB_ENGINE_PIN.to_string())
}
const CACHE_STAMP: &str = ".chdb-engine-complete";
fn engine_cache_root() -> Option<PathBuf> {
println!("cargo:rerun-if-env-changed=CHDB_ENGINE_CACHE_DIR");
if let Some(dir) = env_dir("CHDB_ENGINE_CACHE_DIR") {
return Some(dir);
}
let home = env_dir("HOME")?;
Some(if cfg!(target_os = "macos") {
home.join("Library/Caches/chdb-rust")
} else {
env_dir("XDG_CACHE_HOME")
.unwrap_or_else(|| home.join(".cache"))
.join("chdb-rust")
})
}
fn fetch_engine(out_dir: &Path) -> Result<(PathBuf, String), Box<dyn std::error::Error>> {
let tag = engine_tag_to_download();
let asset = get_platform_string()?;
if let Some(root) = engine_cache_root() {
match cached_engine(&tag, &asset, &root) {
Ok(dir) => return Ok((dir, tag)),
Err(e) => {
println!("cargo:warning=engine cache at {} unusable ({e}); unpacking into OUT_DIR instead", root.display());
}
}
}
download_and_unpack(&tag, &asset, out_dir)?;
Ok((out_dir.to_path_buf(), tag))
}
fn cached_engine(
tag: &str,
asset: &str,
cache_root: &Path,
) -> Result<PathBuf, Box<dyn std::error::Error>> {
let key = asset.trim_end_matches(".tar.gz");
let entry = cache_root.join(tag).join(key);
if entry.join(CACHE_STAMP).exists() {
println!(
"cargo:warning=using the cached engine at {}",
entry.display()
);
return Ok(entry);
}
let parent = cache_root.join(tag);
let staging = parent.join(format!("{key}.incomplete.{}", std::process::id()));
fs::create_dir_all(&parent)?;
let _ = fs::remove_dir_all(&staging);
fs::create_dir_all(&staging)?;
if let Err(e) = fill_cache_entry(tag, asset, &staging) {
let _ = fs::remove_dir_all(&staging);
let _ = fs::remove_dir(&parent);
return Err(e);
}
match fs::rename(&staging, &entry) {
Ok(()) => {}
Err(_) if entry.join(CACHE_STAMP).exists() => {
let _ = fs::remove_dir_all(&staging);
}
Err(e) => {
let _ = fs::remove_dir_all(&staging);
return Err(e.into());
}
}
println!("cargo:warning=cached the engine at {}", entry.display());
Ok(entry)
}
fn fill_cache_entry(
tag: &str,
asset: &str,
staging: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
download_and_unpack(tag, asset, staging)?;
fs::write(staging.join(CACHE_STAMP), tag)?;
Ok(())
}
fn download_and_unpack(
tag: &str,
asset: &str,
dest: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
let url = format!("https://github.com/chdb-io/chdb-core/releases/download/{tag}/{asset}");
println!("cargo:warning=Downloading libchdb from: {url}");
let client = reqwest::blocking::Client::builder()
.timeout(std::time::Duration::from_secs(600))
.build()?;
let mut response = client.get(&url).send()?;
if !response.status().is_success() {
return Err(format!("Download failed with status: {}", response.status()).into());
}
let temp_archive = dest.join("libchdb.tar.gz");
let mut archive_file = fs::File::create(&temp_archive)?;
response.copy_to(&mut archive_file)?;
println!("cargo:warning=Unpacking libchdb...");
let file = fs::File::open(&temp_archive)?;
let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(file));
archive.unpack(dest)?;
fs::remove_file(&temp_archive)?;
if cfg!(unix) {
if let Some(lib_path) = find_lib_in(dest) {
let _ = Command::new("chmod")
.args(["+x", lib_path.to_str().unwrap()])
.output();
}
}
Ok(())
}
fn target_platform() -> (String, String) {
let os = env::var("CARGO_CFG_TARGET_OS").expect("Failed to get target OS");
let arch = env::var("CARGO_CFG_TARGET_ARCH").expect("Failed to get target architecture");
(os, arch)
}
fn get_platform_string() -> Result<String, &'static str> {
let ext = if is_static() {
"-static.tar.gz"
} else {
".tar.gz"
};
let (os, arch) = target_platform();
match (os.as_str(), arch.as_str()) {
("linux", "x86_64") => Ok(format!("linux-x86_64-libchdb{}", ext)),
("linux", "aarch64") => Ok(format!("linux-aarch64-libchdb{}", ext)),
("macos", "x86_64") => Ok(format!("macos-x86_64-libchdb{}", ext)),
("macos", "aarch64") => Ok(format!("macos-arm64-libchdb{}", ext)),
_ => Err("Unsupported platform"),
}
}
fn lib_exports_symbol(lib_path: &Path, symbol: &str) -> bool {
if !lib_path.exists() {
return false;
}
let output = Command::new("nm")
.args(["-D", lib_path.to_str().unwrap_or_default()])
.output();
match output {
Ok(o) if o.status.success() => {
let stdout = String::from_utf8_lossy(&o.stdout);
stdout.contains(symbol)
}
_ => false,
}
}
fn header_declares(header_path: &Path, symbol: &str) -> bool {
fs::read_to_string(header_path)
.map(|contents| contents.contains(symbol))
.unwrap_or(false)
}
fn isolate_archive(lib_path: &Path, out_dir: &Path) -> Option<PathBuf> {
let dir = out_dir.join("static-link");
fs::create_dir_all(&dir).ok()?;
let link = dir.join("libchdb.a");
let target = lib_path.canonicalize().ok()?;
if fs::read_link(&link).is_ok_and(|existing| existing == target) {
return Some(dir);
}
let _ = fs::remove_file(&link);
#[cfg(unix)]
std::os::unix::fs::symlink(&target, &link).ok()?;
#[cfg(not(unix))]
fs::copy(&target, &link).ok()?;
Some(dir)
}
const DURABLE_ABI: [&str; 3] = [
"chdb_backup_database_n",
"chdb_restore_database_n",
"chdb_classify_query_n",
];
fn setup_link_paths(lib_path: &Path, header_path: &Path, out_dir: &Path) {
let lib_dir = lib_path.parent().unwrap_or(Path::new("."));
if is_static() {
let search_dir =
isolate_archive(lib_path, out_dir).unwrap_or_else(|| lib_dir.to_path_buf());
println!("cargo:rustc-link-search=native={}", search_dir.display());
println!("cargo:rustc-link-lib=static=chdb");
match target_platform().0.as_str() {
"linux" => {
println!("cargo:rustc-link-lib=stdc++");
}
"macos" => {
println!("cargo:rustc-link-lib=c++");
println!("cargo:rustc-link-lib=iconv");
println!("cargo:rustc-link-lib=framework=CoreFoundation");
println!("cargo:rustc-link-lib=framework=Security");
}
_ => {}
}
} else {
println!("cargo:rustc-link-search=native={}", lib_dir.display());
println!("cargo:rustc-link-search=native=./");
println!("cargo:rustc-link-search=native=/usr/local/lib");
println!("cargo:rustc-link-lib=chdb");
}
if header_declares(header_path, "chdb_insert_arrow_array")
&& lib_exports_symbol(lib_path, "chdb_insert_arrow_array")
{
println!("cargo:rustc-cfg=direct_arrow_insert");
}
if header_declares(header_path, "chdb_version") {
println!("cargo:rustc-cfg=has_chdb_version");
}
if DURABLE_ABI
.iter()
.all(|symbol| header_declares(header_path, symbol))
{
println!("cargo:rustc-cfg=has_durable_abi");
}
println!("cargo:rerun-if-changed=wrapper.h");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed={}", header_path.display());
println!("cargo:rerun-if-changed={}", lib_path.display());
}
fn generate_bindings(header_path: &Path, out_dir: &Path) {
let header_path = header_path
.canonicalize()
.unwrap_or_else(|_| header_path.to_path_buf());
let wrapper_content = format!("#include \"{}\"", header_path.display());
let temp_wrapper = out_dir.join("temp_wrapper.h");
if fs::read_to_string(&temp_wrapper)
.map(|s| s != wrapper_content)
.unwrap_or(true)
{
fs::write(&temp_wrapper, wrapper_content).expect("Failed to write temp wrapper");
}
let bindings = bindgen::Builder::default()
.header(temp_wrapper.to_str().unwrap())
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(out_dir.join("bindings.rs"))
.expect("Couldn't write bindings to OUT_DIR!");
}