use std::env;
use std::fs::File;
use std::io::{BufWriter, Read, Write};
use std::path::Path;
use tdlib_rs_gen::generate_rust_code;
use tdlib_rs_parser::parse_tl_file;
use tdlib_rs_parser::tl::Definition;
#[allow(dead_code)]
#[cfg(not(any(feature = "docs", feature = "pkg-config")))]
const TDLIB_VERSION: &str = "1.8.61";
fn load_tl(file: &str) -> std::io::Result<Vec<Definition>> {
let mut file = File::open(file)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(parse_tl_file(contents)
.filter_map(|d| match d {
Ok(d) => Some(d),
Err(e) => {
eprintln!("TL: parse error: {e:?}");
None
}
})
.collect())
}
#[cfg(feature = "local-tdlib")]
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<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 = "local-tdlib")]
fn copy_local_tdlib() {
match env::var("LOCAL_TDLIB_PATH") {
Ok(tdlib_path) => {
let out_dir = env::var("OUT_DIR").unwrap();
let prefix = format!("{out_dir}/tdlib");
copy_dir_all(Path::new(&tdlib_path), Path::new(&prefix)).unwrap();
}
Err(_) => {
panic!("The LOCAL_TDLIB_PATH env variable must be set to the path of the tdlib folder");
}
};
}
#[cfg(any(feature = "download-tdlib", feature = "local-tdlib"))]
fn generic_build() {
let out_dir = env::var("OUT_DIR").unwrap();
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let prefix = format!("{out_dir}/tdlib");
let include_dir = format!("{prefix}/include");
let lib_dir = format!("{prefix}/lib");
#[cfg(not(feature = "static"))]
let dynamic_lib_path = match target_os.as_str() {
"android" => format!("{lib_dir}/libtdjson.so"),
"linux" => format!("{lib_dir}/libtdjson.so.{TDLIB_VERSION}"),
"macos" => format!("{lib_dir}/libtdjson.{TDLIB_VERSION}.dylib"),
"windows" => format!(r"{lib_dir}\tdjson.lib"),
_ => panic!("Unsupported target OS: {target_os}"),
};
#[cfg(feature = "static")]
let static_libs = [
"tdactor",
"tdapi",
"tdclient",
"tdcore",
"tddb",
"tde2e",
"tdjson_private",
"tdjson_static",
"tdmtproto",
"tdnet",
"tdsqlite",
"tdutils",
];
#[cfg(feature = "static")]
let static_libs_external = if target_os == "windows" {
["libssl", "libcrypto", "zlib"]
} else {
["ssl", "crypto", "z"]
};
#[cfg(feature = "static")]
let all_static_libs: Vec<String> = static_libs
.iter()
.map(|name| name.to_string())
.chain(static_libs_external.iter().map(|name| name.to_string()))
.collect();
#[cfg(feature = "static")]
let missing_static_libs: Vec<String> = all_static_libs
.iter()
.filter_map(|name| {
let path = if target_os == "windows" {
format!(r"{lib_dir}\{name}.lib")
} else {
format!("{lib_dir}/lib{name}.a")
};
if std::path::PathBuf::from(path.clone()).exists() {
None
} else {
Some(path)
}
})
.collect();
#[cfg(feature = "static")]
if !missing_static_libs.is_empty() {
panic!(
"required TDLib static libraries not found: {}",
missing_static_libs.join(", ")
);
}
#[cfg(not(feature = "static"))]
if !std::path::PathBuf::from(dynamic_lib_path.clone()).exists() {
panic!("tdjson shared library not found at {dynamic_lib_path}");
}
#[cfg(not(feature = "static"))]
if target_os == "windows" {
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}");
#[cfg(feature = "static")]
for link_name in &all_static_libs {
println!("cargo:rustc-link-lib=static={link_name}");
}
#[cfg(feature = "static")]
{
if target_os == "linux" || target_os == "macos" {
println!("cargo:rustc-link-lib=c++");
println!("cargo:rustc-link-lib=c++abi");
} else if target_os == "android" {
println!("cargo:rustc-link-lib=static=c++_static");
} else if target_os == "windows" {
println!("cargo:rustc-link-lib=psapi");
println!("cargo:rustc-link-lib=Normaliz");
println!("cargo:rustc-link-lib=Crypt32");
println!("cargo:rustc-link-lib=advapi32");
println!("cargo:rustc-link-lib=user32");
} else {
panic!("Unsupported target OS: {target_os}");
}
}
#[cfg(not(feature = "static"))]
println!("cargo:rustc-link-lib=dylib=tdjson");
#[cfg(not(feature = "static"))]
println!("cargo:rustc-link-arg=-Wl,-rpath,{lib_dir}");
}
#[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,
env!("CARGO_PKG_VERSION"),
TDLIB_VERSION,
std::env::var("CARGO_CFG_TARGET_OS").unwrap(),
std::env::var("CARGO_CFG_TARGET_ARCH").unwrap(),
);
let out_dir = env::var("OUT_DIR").unwrap();
let tdlib_dir = format!("{}/tdlib", &out_dir);
let zip_path = format!("{}.zip", &tdlib_dir);
let response = ureq::get(&url).call();
let mut response = match response {
Ok(response) => response,
Err(err) => {
panic!(
"[{}] Failed to download file: {}\n{}\n{}",
"Your OS or architecture may be unsupported.",
"Please try using the `pkg-config` or `local-tdlib` features.",
err,
&url
)
}
};
let mut dest = File::create(&zip_path).unwrap();
let mut response_reader = response.body_mut().as_reader();
std::io::copy(&mut response_reader, &mut dest).unwrap();
let mut archive = zip::ZipArchive::new(File::open(&zip_path).unwrap()).unwrap();
for i in 0..archive.len() {
let mut file = archive.by_index(i).unwrap();
let outpath = 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 = 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);
}
fn main() -> std::io::Result<()> {
#[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 = "pkg-config", feature = "download-tdlib"))]
compile_error!(
"feature \"pkg-config\" and feature \"download-tdlib\" cannot be enabled at the same time"
);
#[cfg(all(feature = "static", feature = "pkg-config"))]
compile_error!(
"feature \"static\" and feature \"pkg-config\" cannot be enabled at the same time"
);
println!("cargo:rerun-if-changed=build.rs");
#[cfg(feature = "local-tdlib")]
println!("cargo:rerun-if-env-changed=LOCAL_TDLIB_PATH");
#[cfg(not(feature = "docs"))]
{
#[cfg(feature = "pkg-config")]
system_deps::Config::new().probe().unwrap();
#[cfg(feature = "download-tdlib")]
download_tdlib();
#[cfg(feature = "local-tdlib")]
copy_local_tdlib();
#[cfg(any(feature = "download-tdlib", feature = "local-tdlib"))]
generic_build();
}
let out_dir = env::var("OUT_DIR").unwrap();
let definitions = load_tl("tl/api.tl")?;
let mut file = BufWriter::new(File::create(Path::new(&out_dir).join("generated.rs"))?);
generate_rust_code(&mut file, &definitions, cfg!(feature = "bots-only-api"))?;
file.flush()?;
Ok(())
}