#[allow(dead_code)]
#[cfg(not(any(feature = "docs", feature = "pkg-config")))]
const TDLIB_VERSION: &str = "1.8.61";
#[cfg(feature = "download-tdlib")]
const TDLIB_CARGO_PKG_VERSION: &str = "1.3.0";
#[cfg(feature = "download-tdlib")]
fn copy_dir_all(
src: impl AsRef<std::path::Path>,
dst: impl AsRef<std::path::Path>,
) -> std::io::Result<()> {
std::fs::create_dir_all(&dst)?;
for entry in std::fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
std::fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
#[cfg(feature = "download-tdlib")]
fn download_tdlib() {
let base_url = "https://github.com/FedericoBruzzone/tdlib-rs/releases/download";
let url = format!(
"{}/v{}/tdlib-{}-{}-{}.zip",
base_url,
TDLIB_CARGO_PKG_VERSION,
TDLIB_VERSION,
std::env::var("CARGO_CFG_TARGET_OS").unwrap(),
std::env::var("CARGO_CFG_TARGET_ARCH").unwrap(),
);
let out_dir = std::env::var("OUT_DIR").unwrap();
let tdlib_dir = format!("{}/tdlib", &out_dir);
let zip_path = format!("{}.zip", &tdlib_dir);
let response = reqwest::blocking::get(&url).unwrap();
if response.status().is_success() {
let mut dest = std::fs::File::create(&zip_path).unwrap();
let content = response.bytes().unwrap();
std::io::copy(&mut content.as_ref(), &mut dest).unwrap();
} else {
panic!(
"[{}] Failed to download file: {}\n{}\n{}",
"Your OS or architecture may be unsupported.",
"Please try using the `pkg-config` or `local-tdlib` features.",
response.status(),
&url
)
}
let mut archive = zip::ZipArchive::new(std::fs::File::open(&zip_path).unwrap()).unwrap();
for i in 0..archive.len() {
let mut file = archive.by_index(i).unwrap();
let outpath = std::path::Path::new(&out_dir).join(file.name());
if (*file.name()).ends_with('/') {
std::fs::create_dir_all(&outpath).unwrap();
} else {
if let Some(p) = outpath.parent()
&& !p.exists()
{
std::fs::create_dir_all(p).unwrap();
}
let mut outfile = std::fs::File::create(&outpath).unwrap();
std::io::copy(&mut file, &mut outfile).unwrap();
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Some(mode) = file.unix_mode() {
std::fs::set_permissions(&outpath, std::fs::Permissions::from_mode(mode)).unwrap();
}
}
}
let _ = std::fs::remove_file(&zip_path);
}
#[cfg(any(feature = "download-tdlib", feature = "local-tdlib"))]
fn generic_build(lib_path: Option<String>) {
let correct_lib_path: String;
match lib_path {
Some(lib_path) => {
if lib_path.ends_with('/') || lib_path.ends_with('\\') {
correct_lib_path = lib_path[..lib_path.len() - 1].to_string();
} else {
correct_lib_path = lib_path.to_string();
}
}
None => {
correct_lib_path = format!("{}/tdlib", std::env::var("OUT_DIR").unwrap());
}
}
let prefix = correct_lib_path.to_string();
let include_dir = format!("{prefix}/include");
let lib_dir = format!("{prefix}/lib");
let mut_lib_path = {
#[cfg(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "linux", target_arch = "aarch64")
))]
{
format!("{lib_dir}/libtdjson.so.{TDLIB_VERSION}")
}
#[cfg(any(
all(target_os = "macos", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "aarch64")
))]
{
format!("{lib_dir}/libtdjson.{TDLIB_VERSION}.dylib")
}
#[cfg(any(
all(target_os = "windows", target_arch = "x86_64"),
all(target_os = "windows", target_arch = "aarch64")
))]
{
format!(r"{lib_dir}\tdjson.lib")
}
};
if !std::path::PathBuf::from(mut_lib_path.clone()).exists() {
panic!("tdjson shared library not found at {mut_lib_path}");
}
#[cfg(any(
all(target_os = "windows", target_arch = "x86_64"),
all(target_os = "windows", target_arch = "aarch64")
))]
{
let bin_dir = format!(r"{prefix}\bin");
let cargo_bin = format!("{}/.cargo/bin", dirs::home_dir().unwrap().to_str().unwrap());
let libcrypto3x64 = format!(r"{bin_dir}\libcrypto-3-x64.dll");
let libssl3x64 = format!(r"{bin_dir}\libssl-3-x64.dll");
let tdjson = format!(r"{bin_dir}\tdjson.dll");
let zlib1 = format!(r"{bin_dir}\zlib1.dll");
let cargo_libcrypto3x64 = format!(r"{cargo_bin}\libcrypto-3-x64.dll");
let cargo_libssl3x64 = format!(r"{cargo_bin}\libssl-3-x64.dll");
let cargo_tdjson = format!(r"{cargo_bin}\tdjson.dll");
let cargo_zlib1 = format!(r"{cargo_bin}\zlib1.dll");
let _ = std::fs::remove_file(&cargo_libcrypto3x64);
let _ = std::fs::remove_file(&cargo_libssl3x64);
let _ = std::fs::remove_file(&cargo_tdjson);
let _ = std::fs::remove_file(&cargo_zlib1);
let _ = std::fs::copy(libcrypto3x64.clone(), cargo_libcrypto3x64.clone());
let _ = std::fs::copy(libssl3x64.clone(), cargo_libssl3x64.clone());
let _ = std::fs::copy(tdjson.clone(), cargo_tdjson.clone());
let _ = std::fs::copy(zlib1.clone(), cargo_zlib1.clone());
}
#[cfg(any(
all(target_os = "windows", target_arch = "x86_64"),
all(target_os = "windows", target_arch = "aarch64")
))]
{
let bin_dir = format!(r"{prefix}\bin");
println!("cargo:rustc-link-search=native={bin_dir}");
}
println!("cargo:rustc-link-search=native={lib_dir}");
println!("cargo:include={include_dir}");
println!("cargo:rustc-link-lib=dylib=tdjson");
println!("cargo:rustc-link-arg=-Wl,-rpath,{lib_dir}");
}
pub fn check_features() {
#[cfg(all(feature = "docs", feature = "pkg-config"))]
compile_error!(
"feature \"docs\" and feature \"pkg-config\" cannot be enabled at the same time"
);
#[cfg(all(feature = "docs", feature = "download-tdlib"))]
compile_error!(
"feature \"docs\" and feature \"download-tdlib\" cannot be enabled at the same time"
);
#[cfg(all(feature = "docs", feature = "local-tdlib"))]
compile_error!(
"feature \"docs\" and feature \"local-tdlib\" cannot be enabled at the same time"
);
#[cfg(all(feature = "pkg-config", feature = "local-tdlib"))]
compile_error!(
"feature \"pkg-config\" and feature \"local-tdlib\" cannot be enabled at the same time"
);
#[cfg(all(feature = "pkg-config", feature = "download-tdlib"))]
compile_error!(
"feature \"pkg-config\" and feature \"download-tdlib\" cannot be enabled at the same time"
);
#[cfg(all(feature = "local-tdlib", feature = "download-tdlib"))]
compile_error!(
"feature \"local-tdlib\" and feature \"download-tdlib\" cannot be enabled at the same time"
);
}
pub fn set_rerun_if() {
#[cfg(feature = "local-tdlib")]
println!("cargo:rerun-if-env-changed=LOCAL_TDLIB_PATH");
println!("cargo:rerun-if-changed=build.rs");
}
#[cfg(any(feature = "pkg-config", feature = "docs"))]
#[allow(clippy::needless_doctest_main)]
pub fn build_pkg_config() {
#[cfg(not(feature = "docs"))]
{
system_deps::Config::new().probe().unwrap();
}
}
#[cfg(any(feature = "download-tdlib", feature = "docs"))]
#[allow(clippy::needless_doctest_main)]
#[allow(unused_variables)]
pub fn build_download_tdlib(dest_path: Option<String>) {
#[cfg(not(feature = "docs"))]
{
download_tdlib();
if let Some(dest_path) = &dest_path {
let out_dir = std::env::var("OUT_DIR").unwrap();
let tdlib_dir = format!("{}/tdlib", &out_dir);
copy_dir_all(
std::path::Path::new(&tdlib_dir),
std::path::Path::new(&dest_path),
)
.unwrap();
}
generic_build(dest_path);
}
}
#[cfg(any(feature = "local-tdlib", feature = "docs"))]
#[allow(clippy::needless_doctest_main)]
pub fn build_local_tdlib() {
#[cfg(not(feature = "docs"))]
{
let path = std::env::var("LOCAL_TDLIB_PATH").unwrap();
generic_build(Some(path));
}
}
#[allow(clippy::needless_doctest_main)]
pub fn build(_dest_path: Option<String>) {
check_features();
set_rerun_if();
#[cfg(feature = "pkg-config")]
build_pkg_config();
#[cfg(feature = "download-tdlib")]
build_download_tdlib(_dest_path);
#[cfg(feature = "local-tdlib")]
build_local_tdlib();
}