Skip to content

Commit

Permalink
Fix linking in windows
Browse files Browse the repository at this point in the history
Signed-off-by: FedericoBruzzone <federico.bruzzone.i@gmail.com>
Co-authored-by: Andrea Longoni <Andreal2000@users.noreply.github.com>
  • Loading branch information
FedericoBruzzone and Andreal2000 committed Aug 8, 2024
1 parent c0a04c7 commit d14368d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tdlib-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
41 changes: 38 additions & 3 deletions tdlib-rs/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#[allow(dead_code)]
#[cfg(not(any(feature = "docs", feature = "pkg-config")))]
/// The version of the TDLib library.
// The version of the TDLib library.
const TDLIB_VERSION: &str = "1.8.29";
#[cfg(feature = "download-tdlib")]
const TDLIB_CARGO_PKG_VERSION: &str = "1.0.4";
Expand Down Expand Up @@ -178,6 +178,38 @@ fn generic_build(lib_path: Option<String>) {
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);

let cargo_libcrypto3x64 = format!(r"{}\libcrypto-3-x64.dll", cargo_bin);
let cargo_libssl3x64 = format!(r"{}\libssl-3-x64.dll", cargo_bin);
let cargo_tdjson = format!(r"{}\tdjson.dll", cargo_bin);
let cargo_zlib1 = format!(r"{}\zlib1.dll", cargo_bin);

// Delete the files if they exist
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);

// Move all files to cargo_bin
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(all(target_os = "windows", target_arch = "x86_64"))]
{
let bin_dir = format!(r"{}\bin", prefix);
Expand All @@ -204,6 +236,7 @@ fn generic_build(lib_path: Option<String>) {
/// - `pkg-config` and `local-tdlib`
/// - `pkg-config` and `download-tdlib`
/// - `local-tdlib` and `download-tdlib`
///
/// If the features are not correctly set, the function will generate a compile error
pub fn check_features() {
// #[cfg(not(any(feature = "docs", feature = "local-tdlib", feature = "pkg-config", feature = "download-tdlib")))]
Expand Down Expand Up @@ -395,8 +428,8 @@ pub fn build_local_tdlib() {
///
/// # Arguments
/// - `dest_path`: The destination path where the tdlib library will be copied. If `None`, the path
/// will be the `OUT_DIR` environment variable. This argument is used only when the
/// `download-tdlib` feature is enabled.
/// will be the `OUT_DIR` environment variable. This argument is used only when the
/// `download-tdlib` feature is enabled.
///
/// The function will check if the features are correctly set.
/// The function will set the `rerun-if-changed` and `rerun-if-env-changed` flags for the build
Expand All @@ -409,6 +442,7 @@ pub fn build_local_tdlib() {
/// [dependencies]
/// tdlib = { version = "...", features = ["download-tdlib"] }
///

/// [build-dependencies]
/// tdlib = { version = "...", features = [ "download-tdlib" ] }
/// ```
Expand All @@ -424,6 +458,7 @@ pub fn build_local_tdlib() {
pub fn build(_dest_path: Option<String>) {
check_features();
set_rerun_if();

#[cfg(feature = "pkg-config")]
build_pkg_config();
#[cfg(feature = "download-tdlib")]
Expand Down

0 comments on commit d14368d

Please sign in to comment.