From a1a3de3561577857d1f4987942c796b615071850 Mon Sep 17 00:00:00 2001 From: FedericoBruzzone Date: Thu, 8 Aug 2024 14:47:15 +0200 Subject: [PATCH] Fix linking in windows Signed-off-by: FedericoBruzzone Co-authored-by: Andrea Longoni --- Makefile | 5 +++-- tdlib-rs/Cargo.toml | 1 + tdlib-rs/src/build.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 5a8d2aa..a151c66 100644 --- a/Makefile +++ b/Makefile @@ -19,10 +19,11 @@ export RUST_BACKTRACE := 1 # Available bin_name: # get_me +# ARGS="--features download-tdlib" all: $(MAKE) fmt - $(MAKE) clippy # ARGS="--features download-tdlib" - $(MAKE) test # ARGS="--features download-tdlib" + $(MAKE) clippy + $(MAKE) test build_local: cargo build --no-default-features --features local-tdlib diff --git a/tdlib-rs/Cargo.toml b/tdlib-rs/Cargo.toml index fe83143..c6ca3fd 100644 --- a/tdlib-rs/Cargo.toml +++ b/tdlib-rs/Cargo.toml @@ -44,6 +44,7 @@ serde_with = "3.2" system-deps = { version = "7", optional = true } reqwest = { version = "0.12.4", features = ["blocking"], optional = true } zip = { version = "2.0.0", optional = true } +dirs = "5.0.1" [build-dependencies] tdlib-rs-gen = { path = "../tdlib-rs-gen", version = "1.0.4" } diff --git a/tdlib-rs/src/build.rs b/tdlib-rs/src/build.rs index 70c0226..2110f1e 100644 --- a/tdlib-rs/src/build.rs +++ b/tdlib-rs/src/build.rs @@ -140,6 +140,8 @@ fn download_tdlib() { /// If the tdlib library is not found at the specified path, the function will panic. /// The function will panic if the tdlib library is not found at the specified path. fn generic_build(lib_path: Option) { + use crate::tdjson; + let correct_lib_path: String; match lib_path { Some(lib_path) => { @@ -178,6 +180,33 @@ fn generic_build(lib_path: Option) { panic!("tdjson shared library not found at {}", mut_lib_path); } + // This should be not necessary, but it is a workaround because windows does not find the + // tdjson.dll using the commands below. + // TODO: investigate and if it is a bug in `cargo` or `rustc`, open an issue to `cargo` to fix + // this. + #[cfg(all(target_os = "windows", target_arch = "x86_64"))] + { + let bin_dir = format!(r"{}\bin", prefix); + let cargo_bin = format!("{}/.cargo/bin", dirs::home_dir().unwrap().to_str().unwrap()); + + let libcrypto3x64 = format!(r"{}\libcrypto-3-x64.dll", bin_dir); + let libssl3x64 = format!(r"{}\libssl-3-x64.dll", bin_dir); + let tdjson = format!(r"{}\tdjson.dll", bin_dir); + let zlib1 = format!(r"{}\zlib1.dll", bin_dir); + + // Delete the files if they exist + let _ = std::fs::remove_file(&libcrypto3x64); + let _ = std::fs::remove_file(&libssl3x64); + let _ = std::fs::remove_file(&tdjson); + let _ = std::fs::remove_file(&zlib1); + + // Move all files to cargo_bin + let _ = std::fs::copy(libcrypto3x64.clone(), cargo_bin.clone()); + let _ = std::fs::copy(libssl3x64.clone(), cargo_bin.clone()); + let _ = std::fs::copy(tdjson.clone(), cargo_bin.clone()); + let _ = std::fs::copy(zlib1.clone(), cargo_bin.clone()); + } + #[cfg(all(target_os = "windows", target_arch = "x86_64"))] { let bin_dir = format!(r"{}\bin", prefix); @@ -409,6 +438,7 @@ pub fn build_local_tdlib() { /// [dependencies] /// tdlib = { version = "...", features = ["download-tdlib"] } /// + /// [build-dependencies] /// tdlib = { version = "...", features = [ "download-tdlib" ] } /// ``` @@ -424,6 +454,7 @@ pub fn build_local_tdlib() { pub fn build(_dest_path: Option) { check_features(); set_rerun_if(); + #[cfg(feature = "pkg-config")] build_pkg_config(); #[cfg(feature = "download-tdlib")]