summaryrefslogtreecommitdiff
path: root/impls/monero.rs/build.rs
diff options
context:
space:
mode:
Diffstat (limited to 'impls/monero.rs/build.rs')
-rw-r--r--impls/monero.rs/build.rs82
1 files changed, 77 insertions, 5 deletions
diff --git a/impls/monero.rs/build.rs b/impls/monero.rs/build.rs
index 357028b..19a4394 100644
--- a/impls/monero.rs/build.rs
+++ b/impls/monero.rs/build.rs
@@ -1,13 +1,86 @@
use std::env;
+use std::fs::{self, OpenOptions};
+use std::io::Write;
use std::path::PathBuf;
use bindgen::EnumVariation;
+#[cfg(unix)]
+use std::os::unix::fs as unix_fs;
+
+#[cfg(target_os = "windows")]
+use std::fs::copy;
+
fn main() {
let header_path = "../../monero_libwallet2_api_c/src/main/cpp/wallet2_api_c.h";
+ let lib_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("../../release");
+
+ // Set library names based on target OS.
+ //
+ // This rigamarole is currently required because the "monero_libwallet2_api_c" library is also
+ // required under the names "wallet2_api_c" and "libmonero_libwallet2_api_c" by various parts of
+ // the stack. This is a temporary workaround until the library is refactored to use a single
+ // name consistently.
+ let original_lib = if cfg!(target_os = "windows") {
+ lib_path.join("monero_libwallet2_api_c.dll")
+ } else if cfg!(target_os = "macos") {
+ lib_path.join("monero_libwallet2_api_c.dylib")
+ } else {
+ lib_path.join("monero_libwallet2_api_c.so")
+ };
+
+ let symlink_1 = if cfg!(target_os = "windows") {
+ lib_path.join("wallet2_api_c.dll")
+ } else if cfg!(target_os = "macos") {
+ lib_path.join("libwallet2_api_c.dylib")
+ } else {
+ lib_path.join("libwallet2_api_c.so")
+ };
+
+ let symlink_2 = if cfg!(target_os = "windows") {
+ lib_path.join("monero_wallet2_api_c.dll")
+ } else if cfg!(target_os = "macos") {
+ lib_path.join("libmonero_libwallet2_api_c.dylib")
+ } else {
+ lib_path.join("libmonero_libwallet2_api_c.so")
+ };
+
+ // On Unix-like systems, create symlinks.
+ #[cfg(unix)]
+ {
+ if original_lib.exists() && !symlink_1.exists() {
+ unix_fs::symlink(&original_lib, &symlink_1)
+ .expect("Failed to create symbolic link for libwallet2_api_c.so");
+ }
+
+ if original_lib.exists() && !symlink_2.exists() {
+ unix_fs::symlink(&original_lib, &symlink_2)
+ .expect("Failed to create symbolic link for libmonero_libwallet2_api_c.so");
+ }
+ }
+
+ // On Windows, copy the files instead of symlinking.
+ #[cfg(target_os = "windows")]
+ {
+ if original_lib.exists() && !symlink_1.exists() {
+ copy(&original_lib, &symlink_1).expect("Failed to copy DLL file to wallet2_api_c.dll");
+ }
+
+ if original_lib.exists() && !symlink_2.exists() {
+ copy(&original_lib, &symlink_2)
+ .expect("Failed to copy DLL file to monero_wallet2_api_c.dll");
+ }
+ }
println!("cargo:rerun-if-changed={}", header_path);
+ println!("cargo:rerun-if-changed=build.rs");
- // Configure bindgen
+ println!("cargo:rustc-link-search=native={}", lib_path.display());
+ println!("cargo:rustc-link-lib=dylib=monero_libwallet2_api_c");
+ println!("cargo:rustc-link-lib=dylib=stdc++");
+ println!("cargo:rustc-link-lib=dylib=hidapi-hidraw");
+ println!("cargo:rustc-link-arg=-Wl,-rpath,{}", lib_path.display());
+
+ // Generate bindings using bindgen.
let bindings = bindgen::Builder::default()
.header(header_path)
.allowlist_function("MONERO_.*")
@@ -28,18 +101,17 @@ fn main() {
.blocklist_type("_.*")
.blocklist_function("__.*")
.layout_tests(false)
- .default_enum_style(EnumVariation::Rust {
- non_exhaustive: false,
- })
+ .default_enum_style(EnumVariation::Rust { non_exhaustive: false })
.derive_default(false)
.conservative_inline_namespaces()
.generate_comments(false)
.generate()
.expect("Unable to generate bindings");
+
let out_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
.join("src")
.join("bindings.rs");
bindings
- .write_to_file(out_path)
+ .write_to_file(out_path.clone())
.expect("Couldn't write bindings!");
}