From 6c7645806342bf737133a1ccda903ffca3bf3c31 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Thu, 7 Jul 2022 14:13:24 -0400 Subject: [PATCH] Statically link Monero Closes https://github.com/serai-dex/serai/issues/11. --- coins/monero/Cargo.toml | 3 ++ coins/monero/build.rs | 86 +++++++++++++++++++++++++---------------- 2 files changed, 55 insertions(+), 34 deletions(-) diff --git a/coins/monero/Cargo.toml b/coins/monero/Cargo.toml index d2f40f8c..f62145a7 100644 --- a/coins/monero/Cargo.toml +++ b/coins/monero/Cargo.toml @@ -6,6 +6,9 @@ license = "MIT" authors = ["Luke Parker "] edition = "2021" +[build-dependencies] +cc = "1.0" + [dependencies] hex-literal = "0.3" lazy_static = "1" diff --git a/coins/monero/build.rs b/coins/monero/build.rs index 807e80ef..d2b6f66d 100644 --- a/coins/monero/build.rs +++ b/coins/monero/build.rs @@ -1,6 +1,4 @@ -use std::process::Command; -use std::env; -use std::path::Path; +use std::{env, path::Path, process::Command}; fn main() { if !Command::new("git").args(&["submodule", "update", "--init", "--recursive"]).status().unwrap().success() { @@ -19,11 +17,6 @@ fn main() { // TODO: Move this signaling file into OUT_DIR once Monero is built statically successfully println!("cargo:rerun-if-changed=c/.build/monero"); if !Path::new("c/.build/monero").exists() { - if !Command::new("cmake").args(&["cmake", "-DCMAKE_BUILD_TYPE=Release", "-DBUILD_SHARED_LIBS=1", "."]) - .current_dir(&Path::new("c/monero")).status().unwrap().success() { - panic!("cmake failed to generate Monero's build scripts"); - } - if !Command::new("make").arg(format!("-j{}", &env::var("THREADS").unwrap_or("2".to_string()))) .current_dir(&Path::new("c/monero")).status().unwrap().success() { panic!("make failed to build Monero. Please check your dependencies"); @@ -44,47 +37,72 @@ fn main() { &env::consts::DLL_EXTENSION ) ).exists() { + let mut paths = vec![ + "c/monero/build/release/contrib/epee/src/libepee.a".to_string(), + "c/monero/build/release/external/easylogging++/libeasylogging.a".to_string(), + "c/monero/build/release/external/randomx/librandomx.a".to_string() + ]; + for (folder, lib) in [ + ("common", "common"), ("crypto", "cncrypto"), + ("crypto/wallet", "wallet-crypto"), + ("cryptonote_basic", "cryptonote_basic"), + ("cryptonote_basic", "cryptonote_format_utils_basic"), + ("", "version"), ("device", "device"), ("ringct", "ringct_basic"), ("ringct", "ringct") ] { - if !Command::new("cp").args(&[ - &format!( - "c/monero/src/{}/{}{}.{}", + paths.push( + format!( + "c/monero/build/release/src/{}/{}{}.a", folder, &env::consts::DLL_PREFIX, - lib, - &env::consts::DLL_EXTENSION - ), - out_dir - ]).status().unwrap().success() { - panic!("Failed to cp {}", lib); + lib + ) + ); + } + + for path in paths { + if !Command::new("cp").args(&[&path, out_dir]).status().unwrap().success() { + panic!("Failed to cp {}", path); } } } println!("cargo:rerun-if-changed=c/wrapper.cpp"); - if !Command::new("g++").args(&[ - "-O3", "-Wall", "-shared", "-std=c++14", "-fPIC", - "-Imonero/contrib/epee/include", "-Imonero/src", - "wrapper.cpp", "-o", &format!( - "{}/{}wrapper.{}", - out_dir, - &env::consts::DLL_PREFIX, - &env::consts::DLL_EXTENSION - ), - &format!("-L{}", out_dir), - "-ldevice", "-lringct_basic", "-lringct" - ]).current_dir(&Path::new("c")).status().unwrap().success() { - panic!("g++ failed to build the wrapper"); + if !Path::new(&format!("{}/{}wrapper.a", out_dir, &env::consts::DLL_PREFIX)).exists() { + cc::Build::new() + .file("c/wrapper.cpp") + .cpp(true) + .warnings(false) + .include("c/monero/contrib/epee/include") + .include("c/monero/src") + .compile("wrapper"); } println!("cargo:rustc-link-search={}", out_dir); - println!("cargo:rustc-link-lib=cncrypto"); - println!("cargo:rustc-link-lib=device"); - println!("cargo:rustc-link-lib=ringct_basic"); - println!("cargo:rustc-link-lib=ringct"); println!("cargo:rustc-link-lib=wrapper"); + println!("cargo:rustc-link-lib=ringct"); + println!("cargo:rustc-link-lib=ringct_basic"); + println!("cargo:rustc-link-lib=device"); + println!("cargo:rustc-link-lib=cryptonote_basic"); + println!("cargo:rustc-link-lib=cncrypto"); + println!("cargo:rustc-link-lib=cryptonote_format_utils_basic"); + println!("cargo:rustc-link-lib=version"); + println!("cargo:rustc-link-lib=wallet-crypto"); + println!("cargo:rustc-link-lib=easylogging"); + println!("cargo:rustc-link-lib=epee"); + println!("cargo:rustc-link-lib=common"); + println!("cargo:rustc-link-lib=randomx"); + println!("cargo:rustc-link-lib=unbound"); + println!("cargo:rustc-link-lib=sodium"); + println!("cargo:rustc-link-lib=boost_system"); + println!("cargo:rustc-link-lib=boost_thread"); + println!("cargo:rustc-link-lib=boost_filesystem"); + println!("cargo:rustc-link-lib=hidapi-hidraw"); + println!("cargo:rustc-link-lib=stdc++"); + + println!("cargo:rustc-link-arg=-zmuldefs"); }