1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
use std::path::Path;
use std::{env, fs, path::PathBuf, process::Command};
fn link(name: &str, bundled: bool) {
use std::env::var;
let target = var("TARGET").unwrap();
let target: Vec<_> = target.split('-').collect();
if target.get(2) == Some(&"windows") {
println!("cargo:rustc-link-lib=dylib={name}");
if bundled && target.get(3) == Some(&"gnu") {
let dir = var("CARGO_MANIFEST_DIR").unwrap();
println!("cargo:rustc-link-search=native={}/{}", dir, target[0]);
}
}
}
fn fail_on_empty_directory(name: &str) {
if fs::read_dir(name).unwrap().count() == 0 {
println!("The `{name}` directory is empty, did you forget to pull the submodules?");
println!("Try `git submodule update --init --recursive`");
panic!();
}
}
fn rocksdb_include_dir() -> String {
env::var("ROCKSDB_INCLUDE_DIR").unwrap_or_else(|_| "rocksdb/include".to_string())
}
fn bindgen_rocksdb() {
let bindings = bindgen::Builder::default()
.header(rocksdb_include_dir() + "/rocksdb/c.h")
.derive_debug(false)
.blocklist_type("max_align_t") // https://github.com/rust-lang-nursery/rust-bindgen/issues/550
.ctypes_prefix("libc")
.size_t_is_usize(true)
.generate()
.expect("unable to generate rocksdb bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("unable to write rocksdb bindings");
}
fn build_rocksdb() {
let target = env::var("TARGET").unwrap();
let mut config = cc::Build::new();
config.include("rocksdb/include/");
config.include("rocksdb/");
config.include("rocksdb/third-party/gtest-1.8.1/fused-src/");
if cfg!(feature = "snappy") {
config.define("SNAPPY", Some("1"));
config.include("snappy/");
}
if cfg!(feature = "lz4") {
config.define("LZ4", Some("1"));
if let Some(path) = env::var_os("DEP_LZ4_INCLUDE") {
config.include(path);
}
}
if cfg!(feature = "zstd") {
config.define("ZSTD", Some("1"));
if let Some(path) = env::var_os("DEP_ZSTD_INCLUDE") {
config.include(path);
}
}
if cfg!(feature = "zlib") {
config.define("ZLIB", Some("1"));
if let Some(path) = env::var_os("DEP_Z_INCLUDE") {
config.include(path);
}
}
if cfg!(feature = "bzip2") {
config.define("BZIP2", Some("1"));
if let Some(path) = env::var_os("DEP_BZIP2_INCLUDE") {
config.include(path);
}
}
if cfg!(feature = "rtti") {
config.define("USE_RTTI", Some("1"));
}
// https://github.com/facebook/rocksdb/blob/be7703b27d9b3ac458641aaadf27042d86f6869c/Makefile#L195
if cfg!(feature = "lto") {
config.flag("-flto");
if !config.get_compiler().is_like_clang() {
panic!(
"LTO is only supported with clang. Either disable the `lto` feature\
or set `CC=/usr/bin/clang CXX=/usr/bin/clang++` environment variables."
);
}
}
config.include(".");
config.define("NDEBUG", Some("1"));
let mut lib_sources = include_str!("rocksdb_lib_sources.txt")
.trim()
.split('\n')
.map(str::trim)
// We have a pre-generated a version of build_version.cc in the local directory
.filter(|file| !matches!(*file, "util/build_version.cc"))
.collect::<Vec<&'static str>>();
if let (true, Ok(target_feature_value)) = (
target.contains("x86_64"),
env::var("CARGO_CFG_TARGET_FEATURE"),
) {
// This is needed to enable hardware CRC32C. Technically, SSE 4.2 is
// only available since Intel Nehalem (about 2010) and AMD Bulldozer
// (about 2011).
let target_features: Vec<_> = target_feature_value.split(',').collect();
if target_features.contains(&"sse2") {
config.flag_if_supported("-msse2");
}
if target_features.contains(&"sse4.1") {
config.flag_if_supported("-msse4.1");
}
if target_features.contains(&"sse4.2") {
config.flag_if_supported("-msse4.2");
}
// Pass along additional target features as defined in
// build_tools/build_detect_platform.
if target_features.contains(&"avx2") {
config.flag_if_supported("-mavx2");
}
if target_features.contains(&"bmi1") {
config.flag_if_supported("-mbmi");
}
if target_features.contains(&"lzcnt") {
config.flag_if_supported("-mlzcnt");
}
if !target.contains("android") && target_features.contains(&"pclmulqdq") {
config.flag_if_supported("-mpclmul");
}
}
if target.contains("apple-ios") {
config.define("OS_MACOSX", None);
config.define("IOS_CROSS_COMPILE", None);
config.define("PLATFORM", "IOS");
config.define("NIOSTATS_CONTEXT", None);
config.define("NPERF_CONTEXT", None);
config.define("ROCKSDB_PLATFORM_POSIX", None);
config.define("ROCKSDB_LIB_IO_POSIX", None);
env::set_var("IPHONEOS_DEPLOYMENT_TARGET", "12.0");
} else if target.contains("darwin") {
config.define("OS_MACOSX", None);
config.define("ROCKSDB_PLATFORM_POSIX", None);
config.define("ROCKSDB_LIB_IO_POSIX", None);
} else if target.contains("android") {
config.define("OS_ANDROID", None);
config.define("ROCKSDB_PLATFORM_POSIX", None);
config.define("ROCKSDB_LIB_IO_POSIX", None);
if &target == "armv7-linux-androideabi" {
config.define("_FILE_OFFSET_BITS", Some("32"));
}
} else if target.contains("linux") {
config.define("OS_LINUX", None);
config.define("ROCKSDB_PLATFORM_POSIX", None);
config.define("ROCKSDB_LIB_IO_POSIX", None);
config.define("ROCKSDB_SCHED_GETCPU_PRESENT", None);
} else if target.contains("dragonfly") {
config.define("OS_DRAGONFLYBSD", None);
config.define("ROCKSDB_PLATFORM_POSIX", None);
config.define("ROCKSDB_LIB_IO_POSIX", None);
} else if target.contains("freebsd") {
config.define("OS_FREEBSD", None);
config.define("ROCKSDB_PLATFORM_POSIX", None);
config.define("ROCKSDB_LIB_IO_POSIX", None);
} else if target.contains("netbsd") {
config.define("OS_NETBSD", None);
config.define("ROCKSDB_PLATFORM_POSIX", None);
config.define("ROCKSDB_LIB_IO_POSIX", None);
} else if target.contains("openbsd") {
config.define("OS_OPENBSD", None);
config.define("ROCKSDB_PLATFORM_POSIX", None);
config.define("ROCKSDB_LIB_IO_POSIX", None);
} else if target.contains("windows") {
link("rpcrt4", false);
link("shlwapi", false);
config.define("DWIN32", None);
config.define("OS_WIN", None);
config.define("_MBCS", None);
config.define("WIN64", None);
config.define("NOMINMAX", None);
config.define("ROCKSDB_WINDOWS_UTF8_FILENAMES", None);
if &target == "x86_64-pc-windows-gnu" {
// Tell MinGW to create localtime_r wrapper of localtime_s function.
config.define("_POSIX_C_SOURCE", Some("1"));
// Tell MinGW to use at least Windows Vista headers instead of the ones of Windows XP.
// (This is minimum supported version of rocksdb)
config.define("_WIN32_WINNT", Some("_WIN32_WINNT_VISTA"));
}
// Remove POSIX-specific sources
lib_sources = lib_sources
.iter()
.cloned()
.filter(|file| {
!matches!(
*file,
"port/port_posix.cc"
| "env/env_posix.cc"
| "env/fs_posix.cc"
| "env/io_posix.cc"
)
})
.collect::<Vec<&'static str>>();
// Add Windows-specific sources
lib_sources.extend([
"port/win/env_default.cc",
"port/win/port_win.cc",
"port/win/xpress_win.cc",
"port/win/io_win.cc",
"port/win/win_thread.cc",
"port/win/env_win.cc",
"port/win/win_logger.cc",
]);
if cfg!(feature = "jemalloc") {
lib_sources.push("port/win/win_jemalloc.cc");
}
}
config.define("ROCKSDB_SUPPORT_THREAD_LOCAL", None);
if cfg!(feature = "jemalloc") {
config.define("WITH_JEMALLOC", "ON");
}
#[cfg(feature = "io-uring")]
if target.contains("linux") {
pkg_config::probe_library("liburing")
.expect("The io-uring feature was requested but the library is not available");
config.define("ROCKSDB_IOURING_PRESENT", Some("1"));
}
if &target != "armv7-linux-androideabi"
&& env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap() != "64"
{
config.define("_FILE_OFFSET_BITS", Some("64"));
config.define("_LARGEFILE64_SOURCE", Some("1"));
}
if target.contains("msvc") {
if cfg!(feature = "mt_static") {
config.static_crt(true);
}
config.flag("-EHsc");
config.flag("-std:c++17");
} else {
config.flag(&cxx_standard());
// matches the flags in CMakeLists.txt from rocksdb
config.flag("-Wsign-compare");
config.flag("-Wshadow");
config.flag("-Wno-unused-parameter");
config.flag("-Wno-unused-variable");
config.flag("-Woverloaded-virtual");
config.flag("-Wnon-virtual-dtor");
config.flag("-Wno-missing-field-initializers");
config.flag("-Wno-strict-aliasing");
config.flag("-Wno-invalid-offsetof");
}
if target.contains("riscv64gc") {
// link libatomic required to build for riscv64gc
println!("cargo:rustc-link-lib=atomic");
}
for file in lib_sources {
config.file(format!("rocksdb/{file}"));
}
config.file("build_version.cc");
config.cpp(true);
config.flag_if_supported("-std=c++17");
config.compile("librocksdb.a");
}
fn build_snappy() {
let target = env::var("TARGET").unwrap();
let endianness = env::var("CARGO_CFG_TARGET_ENDIAN").unwrap();
let mut config = cc::Build::new();
config.include("snappy/");
config.include(".");
config.define("NDEBUG", Some("1"));
config.extra_warnings(false);
if target.contains("msvc") {
config.flag("-EHsc");
if cfg!(feature = "mt_static") {
config.static_crt(true);
}
} else {
// Snappy requires C++11.
// See: https://github.com/google/snappy/blob/master/CMakeLists.txt#L32-L38
config.flag("-std=c++11");
}
if endianness == "big" {
config.define("SNAPPY_IS_BIG_ENDIAN", Some("1"));
}
config.file("snappy/snappy.cc");
config.file("snappy/snappy-sinksource.cc");
config.file("snappy/snappy-c.cc");
config.cpp(true);
config.compile("libsnappy.a");
}
fn try_to_find_and_link_lib(lib_name: &str) -> bool {
println!("cargo:rerun-if-env-changed={lib_name}_COMPILE");
if let Ok(v) = env::var(format!("{lib_name}_COMPILE")) {
if v.to_lowercase() == "true" || v == "1" {
return false;
}
}
println!("cargo:rerun-if-env-changed={lib_name}_LIB_DIR");
println!("cargo:rerun-if-env-changed={lib_name}_STATIC");
if let Ok(lib_dir) = env::var(format!("{lib_name}_LIB_DIR")) {
println!("cargo:rustc-link-search=native={lib_dir}");
let mode = match env::var_os(format!("{lib_name}_STATIC")) {
Some(_) => "static",
None => "dylib",
};
println!("cargo:rustc-link-lib={}={}", mode, lib_name.to_lowercase());
return true;
}
false
}
fn cxx_standard() -> String {
env::var("ROCKSDB_CXX_STD").map_or("-std=c++17".to_owned(), |cxx_std| {
if !cxx_std.starts_with("-std=") {
format!("-std={cxx_std}")
} else {
cxx_std
}
})
}
fn update_submodules() {
let program = "git";
let dir = "../";
let args = ["submodule", "update", "--init"];
println!(
"Running command: \"{} {}\" in dir: {}",
program,
args.join(" "),
dir
);
let ret = Command::new(program).current_dir(dir).args(args).status();
match ret.map(|status| (status.success(), status.code())) {
Ok((true, _)) => (),
Ok((false, Some(c))) => panic!("Command failed with error code {}", c),
Ok((false, None)) => panic!("Command got killed"),
Err(e) => panic!("Command failed with error: {}", e),
}
}
macro_rules! pc {
($name:expr) => { pc!($name => $name) };
($name:expr => $probename:expr) => {
#[cfg(feature = $name)]
match pkg_config::Config::new().probe($probename) {
Ok(lib) => env::set_var(format!("{}_LIB_DIR", $name.to_ascii_uppercase()), &lib.link_paths[0]),
Err(e) => panic!("Debian: {} library not found: {e:?}", $name),
}
};
}
fn main() {
let Ok(rocksdb_lib) = pkg_config::Config::new().atleast_version("8.1.1").probe("rocksdb") else {
panic!("Debian: rocksdb library not found");
};
env::set_var("ROCKSDB_INCLUDE_DIR", &rocksdb_lib.include_paths[0]);
env::set_var("ROCKSDB_LIB_DIR", &rocksdb_lib.link_paths[0]);
pc!("snappy");
pc!("lz4" => "liblz4");
pc!("zstd" => "libzstd");
pc!("zlib");
pc!("bzip2");
#[cfg(feature = "bzip2")]
env::set_var("BZ2_LIB_DIR", env::var("BZIP2_LIB_DIR").unwrap());
println!("dh-cargo:deb-built-using=rocksdb=1={}", env::var("CARGO_MANIFEST_DIR").unwrap());
println!("dh-cargo:deb-built-using=snappy=1={}", env::var("CARGO_MANIFEST_DIR").unwrap());
if !Path::new("rocksdb/AUTHORS").exists() {
//update_submodules();
}
bindgen_rocksdb();
let target = env::var("TARGET").unwrap();
if !try_to_find_and_link_lib("ROCKSDB") {
// rocksdb only works with the prebuilt rocksdb system lib on freebsd.
// we don't need to rebuild rocksdb
if target.contains("freebsd") {
println!("cargo:rustc-link-search=native=/usr/local/lib");
let mode = match env::var_os("ROCKSDB_STATIC") {
Some(_) => "static",
None => "dylib",
};
println!("cargo:rustc-link-lib={}=rocksdb", mode);
return;
}
println!("cargo:rerun-if-changed=rocksdb/");
fail_on_empty_directory("rocksdb");
build_rocksdb();
} else {
// according to https://github.com/alexcrichton/cc-rs/blob/master/src/lib.rs#L2189
if target.contains("apple") || target.contains("freebsd") || target.contains("openbsd") {
println!("cargo:rustc-link-lib=dylib=c++");
} else if target.contains("linux") {
println!("cargo:rustc-link-lib=dylib=stdc++");
}
}
if cfg!(feature = "snappy") && !try_to_find_and_link_lib("SNAPPY") {
println!("cargo:rerun-if-changed=snappy/");
fail_on_empty_directory("snappy");
build_snappy();
}
// Allow dependent crates to locate the sources and output directory of
// this crate. Notably, this allows a dependent crate to locate the RocksDB
// sources and built archive artifacts provided by this crate.
println!(
"cargo:cargo_manifest_dir={}",
env::var("CARGO_MANIFEST_DIR").unwrap()
);
println!("cargo:out_dir={}", env::var("OUT_DIR").unwrap());
}
|