Skip to content

Commit

Permalink
build: fix the compile error using a customized cmake-rs crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Chiichen committed Sep 6, 2024
1 parent b598a1b commit 758a857
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 87 deletions.
4 changes: 2 additions & 2 deletions libvnc-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ exclude = ["compile_commands.json"]
bindgen = "0.70.0"
cc = "1.0.95"
pkg-config = "0.3.30"
# cmake = "0.1.50"
# which = "6.0.3"
cmake = { git = "https://github.com/Chiichen/cmake-rs.git", rev = "b0fdc01b3bedd3cec8dfbbff660bec9f496734ed" }
which = "6.0.3"

[features]
default = []
Expand Down
99 changes: 14 additions & 85 deletions libvnc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,60 +42,10 @@ fn bindgen_vncserver() {
}
#[cfg(not(feature = "pkg"))]
fn bindgen_vncserver() {
use std::{
env::current_dir,
fs::{self, create_dir_all},
process::Command,
vec,
};

#[cfg(target_os = "android")]
compile_error!("Unsupported Target Android");

// https://github.com/LibVNC/libvncserver/issues/628 use cmake in plain command line to avoid compile error on windows
// let mut config = cmake::Config::new("libvncserver");
// let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
// let target_triple = env::var("TARGET").unwrap();
// if target_triple != env::var("HOST").unwrap() {
// if !cfg!(target_os = "linux") {
// //cfg!(target_os) in build.rs means the host os that build script is running
// panic!("Cross-compilation on platforms other than linux is not supported")
// }
// if target_os == "windows" {
// config.define(
// "CMAKE_TOOLCHAIN_FILE",
// "../cmake/Toolchain-cross-mingw32-linux.cmake",
// );
// }
// } else if target_os == "windows" {
// config.define(
// "CMAKE_TOOLCHAIN_FILE",
// which::which("vcpkg")
// .expect("Install vcpkg and make sure it can be found in current environment")
// .parent()
// .unwrap()
// .join("scripts/buildsystems/vcpkg.cmake"), //TODO BETTER toolchain path
// );
// config.define("WITH_OPENSSL", "OFF");
// config.define("WITH_GNUTLS", "OFF");
// config.define("WITH_GCRYPT", "OFF");
// } else if target_os == "android" {
// panic!("unsupported build target {}", target_os)
// }
// //TODO In WSL, if QT is installed in Windows system, then the build process might fail on Qt example.
// let dst = config.build();
// >>> Manually build libvncserver with cmake
let mut dst = PathBuf::from(env::var("OUT_DIR").unwrap());
dst.push("build");
let libvncserver_path = current_dir()
.unwrap()
.join("libvncserver")
.display()
.to_string();
let mut cmake_args = vec![];
cmake_args.push(libvncserver_path.as_str());
// let cmake_install_prefix_arg = format!(r#"-DCMAKE_INSTALL_PREFIX="{}""#, dst.display());
// cmake_args.push(&cmake_install_prefix_arg);
let mut config = cmake::Config::new("libvncserver");
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let target_triple = env::var("TARGET").unwrap();
if target_triple != env::var("HOST").unwrap() {
Expand All @@ -104,10 +54,13 @@ fn bindgen_vncserver() {
panic!("Cross-compilation on platforms other than linux is not supported")
}
if target_os == "windows" {
cmake_args
.push(r#"-DCMAKE_TOOLCHAIN_FILE="../cmake/Toolchain-cross-mingw32-linux.cmake""#);
config.define(
"CMAKE_TOOLCHAIN_FILE",
"../cmake/Toolchain-cross-mingw32-linux.cmake",
);
}
} else if target_os == "windows" {
// Is it necessary?
// config.define(
// "CMAKE_TOOLCHAIN_FILE",
// which::which("vcpkg")
Expand All @@ -116,40 +69,16 @@ fn bindgen_vncserver() {
// .unwrap()
// .join("scripts/buildsystems/vcpkg.cmake"),
// );
cmake_args.push("-DWITH_OPENSSL=OFF");
cmake_args.push("-DWITH_GNUTLS=OFF");
cmake_args.push("-DWITH_GCRYPT=OFF");
config.no_default_c_flags(true);
config.no_default_cxx_flags(true);
config.define("WITH_OPENSSL", "OFF");
config.define("WITH_GNUTLS", "OFF");
config.define("WITH_GCRYPT", "OFF");
} else if target_os == "android" {
panic!("unsupported build target {}", target_os)
}
//Make sure dst is cleaned up
if !dst.is_dir() {
let _ = fs::remove_dir_all(dst.as_path());
let _ = create_dir_all(dst.as_path());
}
let mut command = Command::new("cmake");
command
.current_dir(dst.as_path())
.args(cmake_args)
.env("CMAKE_INSTALL_PREFIX", dst.display().to_string());
let status = command.status().unwrap();
if !status.success() {
panic!("Failed to run {:?}", command)
}

let mut command = Command::new("cmake");
command
.current_dir(dst.as_path())
.arg("--build")
.arg(".")
.arg("--target")
.arg("install");
let status = command.status().unwrap();
println!("command : {:?} status:{:?}", command, status);
if !status.success() {
panic!("Failed to run {:?}", command)
}
// << Manually build libvncserver done
//TODO In WSL, if QT is installed in Windows system, then the build process might fail on Qt example.
let dst = config.build();
println!("cargo:rustc-link-lib=dylib=vncserver");
println!("cargo:rustc-link-lib=dylib=vncclient"); //There's no libvncclient , so we need to specify the vncclient manually
println!("cargo:rustc-link-search={}", dst.display());
Expand All @@ -162,7 +91,7 @@ fn bindgen_vncserver() {
.clang_args([
format!("-I{}/{}", dst.display(), "include"),
#[cfg(target_os = "windows")]
format!("-DWIN32"),
format!("-DWIN32"), //TODO I think it's a bug in libvncserver. It's expected to use _WIN32 macro to determine whether it's on Windows platform. However, it uses WIN32 instead.@see https://stackoverflow.com/questions/662084/whats-the-difference-between-the-win32-and-win32-defines-in-c
])
.generate()
.expect("unable to generate rfb bindings");
Expand Down

0 comments on commit 758a857

Please sign in to comment.