|
|
|
@ -5,7 +5,7 @@ extern crate pkg_config;
|
|
|
|
|
extern crate bindgen;
|
|
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
@ -23,7 +23,7 @@ fn os_define() -> &'static str {
|
|
|
|
|
"LINUX"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn build_leechcore(target: &str) {
|
|
|
|
|
fn build() {
|
|
|
|
|
let mut files = vec![
|
|
|
|
|
"device_file.c",
|
|
|
|
|
"device_fpga.c",
|
|
|
|
@ -43,7 +43,7 @@ fn build_leechcore(target: &str) {
|
|
|
|
|
"ob/ob_map.c",
|
|
|
|
|
"ob/ob_set.c",
|
|
|
|
|
];
|
|
|
|
|
if target.contains("windows") {
|
|
|
|
|
if target().contains("windows") {
|
|
|
|
|
files.push("leechrpc_c.c");
|
|
|
|
|
files.push("leechrpcshared.c");
|
|
|
|
|
}
|
|
|
|
@ -61,7 +61,7 @@ fn build_leechcore(target: &str) {
|
|
|
|
|
.flag("-D_GNU_SOURCE");
|
|
|
|
|
// EXPORTED_FUNCTION= to not export any symbols
|
|
|
|
|
|
|
|
|
|
if !target.contains("windows") {
|
|
|
|
|
if !target().contains("windows") {
|
|
|
|
|
// setup additional flags
|
|
|
|
|
cfg.flag("-fPIC");
|
|
|
|
|
cfg.flag("-pthread");
|
|
|
|
@ -97,8 +97,10 @@ fn build_leechcore(target: &str) {
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// copy pre-compiled idl file into the leechcore folder
|
|
|
|
|
std::fs::copy("gen/leechrpc_c.c", "src/leechcore/leechcore/leechrpc_c.c").expect("Failed to copy leechrpc_c.c");
|
|
|
|
|
std::fs::copy("gen/leechrpc_h.h", "src/leechcore/leechcore/leechrpc_h.h").expect("Failed to copy leechrpc_h.h");
|
|
|
|
|
std::fs::copy("gen/leechrpc_c.c", "src/leechcore/leechcore/leechrpc_c.c")
|
|
|
|
|
.expect("Failed to copy leechrpc_c.c");
|
|
|
|
|
std::fs::copy("gen/leechrpc_h.h", "src/leechcore/leechcore/leechrpc_h.h")
|
|
|
|
|
.expect("Failed to copy leechrpc_h.h");
|
|
|
|
|
|
|
|
|
|
// link against required libraries
|
|
|
|
|
println!("cargo:rustc-link-lib=rpcrt4");
|
|
|
|
@ -112,24 +114,26 @@ fn build_leechcore(target: &str) {
|
|
|
|
|
|
|
|
|
|
cfg.compile("libleechcore.a");
|
|
|
|
|
|
|
|
|
|
if target.contains("windows") {
|
|
|
|
|
if target().contains("windows") {
|
|
|
|
|
// remove temporary generated files
|
|
|
|
|
std::fs::remove_file("src/leechcore/leechcore/leechrpc_c.c").expect("Failed to remove leechrpc_c.c");
|
|
|
|
|
std::fs::remove_file("src/leechcore/leechcore/leechrpc_h.h").expect("Failed to remove leechrpc_h.h");
|
|
|
|
|
std::fs::remove_file("src/leechcore/leechcore/leechrpc_c.c")
|
|
|
|
|
.expect("Failed to remove leechrpc_c.c");
|
|
|
|
|
std::fs::remove_file("src/leechcore/leechcore/leechrpc_h.h")
|
|
|
|
|
.expect("Failed to remove leechrpc_h.h");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println!("cargo:rustc-link-lib=static=leechcore");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "bindgen")]
|
|
|
|
|
fn gen_leechcore<P: AsRef<Path>>(target: &str, out_dir: P) {
|
|
|
|
|
fn generate_bindings() {
|
|
|
|
|
let mut builder = bindgen::builder()
|
|
|
|
|
.clang_arg(format!("-D{} -D_GNU_SOURCE", os_define()))
|
|
|
|
|
.header("./src/leechcore/leechcore/leechcore.h");
|
|
|
|
|
|
|
|
|
|
// workaround for windows.h
|
|
|
|
|
// see https://github.com/rust-lang/rust-bindgen/issues/1556
|
|
|
|
|
if target.contains("windows") {
|
|
|
|
|
if target().contains("windows") {
|
|
|
|
|
builder = builder.blocklist_type("_?P?IMAGE_TLS_DIRECTORY.*")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -137,34 +141,49 @@ fn gen_leechcore<P: AsRef<Path>>(target: &str, out_dir: P) {
|
|
|
|
|
.generate()
|
|
|
|
|
.unwrap_or_else(|err| panic!("Failed to generate bindings: {:?}", err));
|
|
|
|
|
|
|
|
|
|
let bindings_path = out_dir.as_ref().to_path_buf().join("leechcore.rs");
|
|
|
|
|
bindings
|
|
|
|
|
.write_to_file(&bindings_path)
|
|
|
|
|
.unwrap_or_else(|_| panic!("Failed to write {}", bindings_path.display()));
|
|
|
|
|
.write_to_file(&bindings_src_path())
|
|
|
|
|
.unwrap_or_else(|_| panic!("Failed to write {}", bindings_src_path().display()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(feature = "bindgen"))]
|
|
|
|
|
fn gen_leechcore<P: AsRef<Path>>(_target: &str, out_dir: P) {
|
|
|
|
|
fn copy_bindings() {
|
|
|
|
|
let bindings_src_path = bindings_src_path();
|
|
|
|
|
let bindings_dst_path = bindings_dst_path();
|
|
|
|
|
std::fs::copy(bindings_src_path, bindings_dst_path)
|
|
|
|
|
.expect("Failed to copy leechcore.rs bindings to OUT_DIR");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// path helper functions
|
|
|
|
|
fn target() -> String {
|
|
|
|
|
env::var("TARGET").unwrap()
|
|
|
|
|
}
|
|
|
|
|
fn out_dir() -> PathBuf {
|
|
|
|
|
PathBuf::from(env::var("OUT_DIR").unwrap())
|
|
|
|
|
}
|
|
|
|
|
fn bindings_src_path() -> PathBuf {
|
|
|
|
|
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
|
let bindings_src_path = manifest_dir.join("src").join("leechcore_windows.rs");
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
let bindings_src_path = manifest_dir.join("src").join("leechcore_linux.rs");
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
let bindings_src_path = manifest_dir.join("src").join("leechcore_mac.rs");
|
|
|
|
|
|
|
|
|
|
let bindings_dst_path = out_dir.as_ref().to_path_buf().join("leechcore.rs");
|
|
|
|
|
|
|
|
|
|
std::fs::copy(bindings_src_path, bindings_dst_path)
|
|
|
|
|
.expect("Failed to copy leechcore.rs bindings to OUT_DIR");
|
|
|
|
|
bindings_src_path
|
|
|
|
|
}
|
|
|
|
|
fn bindings_dst_path() -> PathBuf {
|
|
|
|
|
out_dir().to_path_buf().join("leechcore.rs")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
let target = env::var("TARGET").unwrap();
|
|
|
|
|
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
|
|
|
|
|
|
|
|
|
|
// build leechcore
|
|
|
|
|
build_leechcore(&target);
|
|
|
|
|
build();
|
|
|
|
|
|
|
|
|
|
// generate bindings from headers (optional)
|
|
|
|
|
#[cfg(feature = "bindgen")]
|
|
|
|
|
generate_bindings();
|
|
|
|
|
|
|
|
|
|
// generate or copy bindings
|
|
|
|
|
gen_leechcore(&target, out_dir);
|
|
|
|
|
// copy bindings to build directory
|
|
|
|
|
copy_bindings();
|
|
|
|
|
}
|
|
|
|
|