-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,41 @@ | ||
use std::env; | ||
use std::path::Path; | ||
|
||
const DEFAULT_CLANG_VERSION: &str = "14.0.7"; | ||
|
||
/// Adds a temporary workaround for an issue with the Rust compiler and Android | ||
/// in x86_64 devices: https://github.com/rust-lang/rust/issues/109717. | ||
/// The workaround comes from: https://github.com/smartvaults/smartvaults/blob/827805a989561b78c0ea5b41f2c1c9e9e59545e0/bindings/smartvaults-sdk-ffi/build.rs | ||
fn setup_x86_64_android_workaround() { | ||
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set"); | ||
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH not set"); | ||
if target_arch == "x86_64" && target_os == "android" { | ||
let android_ndk_home = env::var("ANDROID_NDK_HOME").expect("ANDROID_NDK_HOME not set"); | ||
let build_os = match env::consts::OS { | ||
"linux" => "linux", | ||
"macos" => "darwin", | ||
"windows" => "windows", | ||
_ => panic!( | ||
"Unsupported OS. You must use either Linux, MacOS or Windows to build the crate." | ||
), | ||
}; | ||
let clang_version = | ||
env::var("NDK_CLANG_VERSION").unwrap_or_else(|_| DEFAULT_CLANG_VERSION.to_owned()); | ||
let linux_x86_64_lib_dir = format!( | ||
"toolchains/llvm/prebuilt/{build_os}-x86_64/lib64/clang/{clang_version}/lib/linux/" | ||
); | ||
let linkpath = format!("{android_ndk_home}/{linux_x86_64_lib_dir}"); | ||
if Path::new(&linkpath).exists() { | ||
println!("cargo:rustc-link-search={android_ndk_home}/{linux_x86_64_lib_dir}"); | ||
println!("cargo:rustc-link-lib=static=clang_rt.builtins-x86_64-android"); | ||
} else { | ||
panic!("Path {linkpath} not exists"); | ||
} | ||
} | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
setup_x86_64_android_workaround(); | ||
tonic_build::compile_protos("src/grpc/proto/breez.proto")?; | ||
Ok(()) | ||
} |