mod boost;
mod extension;
mod leveldb;
mod linker;
mod snappy;
mod t3lru;
mod zstd;
use std::{env, path::PathBuf};
pub const LIB_DIR: &str = "lib";
pub fn is_lto_enabled() -> bool {
env::var("CARGO_FEATURE_ENABLE_LTO").is_ok()
}
pub fn apply_cmake_flags(config: &mut cmake::Config) {
#[cfg(target_env = "msvc")]
{
config
.define("CMAKE_POLICY_DEFAULT_CMP0091", "NEW")
.define("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreadedDLL");
}
if is_lto_enabled() {
println!("[build] LTO optimization enabled");
config
.cxxflag("-flto")
.cxxflag("-ffat-lto-objects")
.cflag("-flto");
} else {
println!("[build] LTO optimization disabled");
}
}
#[cfg(feature = "experimental-extension")]
pub const CXX_STANDARD: &str = "20";
#[cfg(not(feature = "experimental-extension"))]
pub const CXX_STANDARD: &str = "17";
pub const C_STANDARD: &str = "11";
fn get_install_dir() -> PathBuf {
let outdir = env::var("OUT_DIR").unwrap();
std::path::Path::new(&outdir).join(LIB_DIR)
}
fn set_cmake_env() {
unsafe {
env::set_var("CMAKE_BUILD_PARALLEL_LEVEL", num_cpus::get().to_string());
}
}
fn determine_build_type() -> &'static str {
match env::var("PROFILE")
.iter()
.find(|s| s.eq_ignore_ascii_case("release"))
{
Some(_) => "Release",
None => "Debug",
}
}
fn main() {
println!("[build] Started");
set_cmake_env();
let build_type = determine_build_type();
let install_dir = get_install_dir();
println!("cargo:rustc-link-search=native={}", &install_dir.display());
#[cfg(feature = "snappy")]
let snappy_prefix = Some(snappy::build(&install_dir, build_type));
#[cfg(not(feature = "snappy"))]
let snappy_prefix: Option<std::path::PathBuf> = None;
#[cfg(feature = "zstd")]
let zstd_prefix = Some(zstd::build(&install_dir, build_type));
#[cfg(not(feature = "zstd"))]
let zstd_prefix: Option<std::path::PathBuf> = None;
#[allow(unused)]
let leveldb_prefix = leveldb::build(&install_dir, build_type, snappy_prefix, zstd_prefix);
#[cfg(feature = "experimental-extension")]
let t3lru_build = Some(t3lru::build(build_type));
#[cfg(feature = "experimental-extension")]
let boost_build = Some(boost::build());
#[cfg(feature = "experimental-extension")]
extension::build(
&install_dir,
build_type,
&leveldb_prefix,
t3lru_build.as_ref(),
boost_build.as_ref(),
);
linker::configure();
println!("[build] Finished");
}