Skip to content

Commit

Permalink
Clean up builds.rs, separate ffi impls and stub tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prasannavl committed Apr 21, 2023
1 parent 4fb2e1a commit 464d3d8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 29 deletions.
41 changes: 22 additions & 19 deletions lib/ain-cpp-imports/build.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
use std::env;
use std::path::PathBuf;

fn main() {
let pkg_name = env::var("CARGO_PKG_NAME").unwrap();
let manifest_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let lib_path = &manifest_path
.parent()
.unwrap()
.parent()
.unwrap()
.join("src");
fn main() -> Result<(), Box<dyn std::error::Error>> {
let pkg_name = env::var("CARGO_PKG_NAME")?;
let manifest_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);

cxx_build::bridge("src/lib.rs")
.include(lib_path)
let cpp_src_path = &manifest_path.parent()
.and_then(|x| x.parent())
.and_then(|x| Some(x.join("src")));

let cpp_src_path = match cpp_src_path.as_ref() {
Some(r) => Ok(r),
None => Err("path err")
}?;

let ffi_rs_src_path = &manifest_path.join("src/bridge.rs");
let ffi_exports_h_path = &cpp_src_path.join("ffi/ffiexports.h");

cxx_build::bridge(ffi_rs_src_path)
.include(&cpp_src_path)
.cpp_link_stdlib("stdc++")
.flag_if_supported("-std=c++17")
.flag_if_supported("-Wno-unused-parameter")
.compile(pkg_name.as_str());

println!(
"cargo:rerun-if-changed={}/lib.rs",
lib_path.to_str().unwrap()
);
println!(
"cargo:rerun-if-changed={}/ffi/ffiexports.h",
lib_path.to_str().unwrap()
);
println!("cargo:rerun-if-changed={}", ffi_rs_src_path.to_string_lossy());
println!("cargo:rerun-if-changed={}", &ffi_exports_h_path.to_string_lossy());

Ok(())
}
12 changes: 12 additions & 0 deletions lib/ain-cpp-imports/src/bridge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[cxx::bridge]
pub mod ffi {
unsafe extern "C++" {
include!("ffi/ffiexports.h");

fn getChainId() -> u64;
fn isMining() -> bool;
fn publishEthTransaction(data: Vec<u8>) -> bool;
fn getAccounts() -> Vec<String>;
fn getDatadir() -> String;
}
}
30 changes: 20 additions & 10 deletions lib/ain-cpp-imports/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@

use std::error::Error;

#[cxx::bridge]
#[cfg(not(any(test, bench, example, doc)))]
mod bridge;

#[cfg(not(any(test, bench, example, doc)))]
use bridge::ffi;

#[cfg(any(test, bench, example, doc))]
#[allow(non_snake_case)]
mod ffi {
unsafe extern "C++" {
include!("ffi/ffiexports.h");

fn getChainId() -> u64;
fn isMining() -> bool;
fn publishEthTransaction(data: Vec<u8>) -> bool;
fn getAccounts() -> Vec<String>;
fn getDatadir() -> String;
}
const UNIMPL_MSG: &'static str = "This cannot be used on a test path";
pub fn getChainId() -> u64 { unimplemented!("{}", UNIMPL_MSG) }
pub fn isMining() -> bool { unimplemented!("{}", UNIMPL_MSG) }
pub fn publishEthTransaction(_data: Vec<u8>) -> bool { unimplemented!("{}", UNIMPL_MSG) }
pub fn getAccounts() -> Vec<String> { unimplemented!("{}", UNIMPL_MSG) }
pub fn getDatadir() -> String { unimplemented!("{}", UNIMPL_MSG) }
}

pub fn get_chain_id() -> Result<u64, Box<dyn Error>> {
Expand All @@ -37,3 +42,8 @@ pub fn get_datadir() -> Result<String, Box<dyn Error>> {
let datadir = ffi::getDatadir();
Ok(datadir)
}

#[cfg(test)]
mod tests {

}

0 comments on commit 464d3d8

Please sign in to comment.