From b63a29ee34157eabf18285b16502234050b4840b Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Fri, 14 Apr 2023 14:19:51 +0800 Subject: [PATCH] evm: Enable C++ FFI (#1912) * Enable C++ FFI * Rename evm_ffi to ffi_exports * Update Makefile.am --- lib/ain-cpp-imports/build.rs | 27 +++++++++++++++++++ lib/ain-cpp-imports/build.rs.disabled | 13 --------- lib/ain-cpp-imports/src/lib.rs | 24 ++++++++--------- src/Makefile.am | 4 +-- .../{evm_ffi.cpp => ffi_exports.cpp} | 2 +- src/masternodes/{evm_ffi.h => ffi_exports.h} | 0 6 files changed, 42 insertions(+), 28 deletions(-) create mode 100644 lib/ain-cpp-imports/build.rs delete mode 100644 lib/ain-cpp-imports/build.rs.disabled rename src/masternodes/{evm_ffi.cpp => ffi_exports.cpp} (81%) rename src/masternodes/{evm_ffi.h => ffi_exports.h} (100%) diff --git a/lib/ain-cpp-imports/build.rs b/lib/ain-cpp-imports/build.rs new file mode 100644 index 00000000000..96065f60564 --- /dev/null +++ b/lib/ain-cpp-imports/build.rs @@ -0,0 +1,27 @@ +use std::env; +use std::path::PathBuf; + +fn main() { + let manifest_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + let lib_path = &manifest_path + .parent() + .unwrap() + .parent() + .unwrap() + .join("src"); + + cxx_build::bridge("src/lib.rs") + .include(lib_path) + .flag_if_supported("-std=c++17") + .flag_if_supported("-Wno-unused-parameter") + .compile("ain-evm-cpp-ffi"); + + println!( + "cargo:rerun-if-changed={}/lib.rs", + lib_path.to_str().unwrap() + ); + println!( + "cargo:rerun-if-changed={}/masternodes/ffi_exports.h", + lib_path.to_str().unwrap() + ); +} diff --git a/lib/ain-cpp-imports/build.rs.disabled b/lib/ain-cpp-imports/build.rs.disabled deleted file mode 100644 index 3f7c31f1dfd..00000000000 --- a/lib/ain-cpp-imports/build.rs.disabled +++ /dev/null @@ -1,13 +0,0 @@ -fn main() { - cxx_build::bridge("src/lib.rs") - .include("../../../../depends/x86_64-pc-linux-gnu/include/") - .include("../../../") - .include("../../../leveldb/include/") - .include("../../../univalue/include/") - .flag_if_supported("-std=c++17") - .flag_if_supported("-Wno-unused-parameter") - .compile("ain-evm-cpp-ffi"); - - println!("cargo:rerun-if-changed=src/lib.rs"); - println!("cargo:rerun-if-changed=../../../masternodes/evm_ffi.h"); -} diff --git a/lib/ain-cpp-imports/src/lib.rs b/lib/ain-cpp-imports/src/lib.rs index d7484dff746..408d1126ec7 100644 --- a/lib/ain-cpp-imports/src/lib.rs +++ b/lib/ain-cpp-imports/src/lib.rs @@ -1,21 +1,21 @@ use std::error::Error; -// #[cxx::bridge] -// mod ffi { -// unsafe extern "C++" { -// include!("masternodes/evm_ffi.h"); +#[cxx::bridge] +mod ffi { + unsafe extern "C++" { + include!("masternodes/ffi_exports.h"); -// fn getChainId() -> u64; -// fn isMining() -> bool; -// } -// } + fn getChainId() -> u64; + fn isMining() -> bool; + } +} pub fn get_chain_id() -> Result> { - // let chain_id = ffi::getChainId(); - Ok(0) + let chain_id = ffi::getChainId(); + Ok(chain_id) } pub fn is_mining() -> Result> { - // let is_mining = ffi::isMining(); - Ok(true) + let is_mining = ffi::isMining(); + Ok(is_mining) } diff --git a/src/Makefile.am b/src/Makefile.am index 82579416155..6ba1a65dae8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -164,7 +164,7 @@ DEFI_CORE_H = \ masternodes/communityaccounttypes.h \ masternodes/errors.h \ masternodes/evm.h \ - masternodes/evm_ffi.h \ + masternodes/ffi_exports.h \ masternodes/factory.h \ masternodes/govvariables/attributes.h \ masternodes/govvariables/icx_takerfee_per_btc.h \ @@ -396,7 +396,7 @@ libdefi_server_a_SOURCES = \ masternodes/accountshistory.cpp \ masternodes/anchors.cpp \ masternodes/auctionhistory.cpp \ - masternodes/evm_ffi.cpp \ + masternodes/ffi_exports.cpp \ masternodes/govvariables/attributes.cpp \ masternodes/govvariables/icx_takerfee_per_btc.cpp \ masternodes/govvariables/loan_daily_reward.cpp \ diff --git a/src/masternodes/evm_ffi.cpp b/src/masternodes/ffi_exports.cpp similarity index 81% rename from src/masternodes/evm_ffi.cpp rename to src/masternodes/ffi_exports.cpp index 27c57d417a3..72d84f308b0 100644 --- a/src/masternodes/evm_ffi.cpp +++ b/src/masternodes/ffi_exports.cpp @@ -1,4 +1,4 @@ -#include +#include #include uint64_t getChainId() { diff --git a/src/masternodes/evm_ffi.h b/src/masternodes/ffi_exports.h similarity index 100% rename from src/masternodes/evm_ffi.h rename to src/masternodes/ffi_exports.h