change the nebula-ffi build process to be more compatible with other platforms [nebula-ffi/1.7.2, tfclient/0.2.6]
/ build (push) Successful in 1m59s Details
/ build_arm64 (push) Successful in 3m13s Details
/ build_x64 (push) Has been cancelled Details

This commit is contained in:
core 2023-10-09 15:06:02 -04:00
parent 27bf92d2cd
commit e519700530
Signed by: core
GPG Key ID: FDBF740DADDCEECF
4 changed files with 92 additions and 30 deletions

15
Cargo.lock generated
View File

@ -1568,15 +1568,6 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "gobuild"
version = "0.1.0-alpha.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71e156a4ddbf3deb5e8116946c111413bd9a5679bdc1536c78a60618a7a9ac9e"
dependencies = [
"cc",
]
[[package]]
name = "h2"
version = "0.3.21"
@ -2078,10 +2069,10 @@ dependencies = [
[[package]]
name = "nebula-ffi"
version = "0.2.0"
version = "1.7.2"
dependencies = [
"bindgen",
"gobuild",
"cc",
]
[[package]]
@ -3607,7 +3598,7 @@ dependencies = [
[[package]]
name = "tfclient"
version = "0.2.5"
version = "0.2.6"
dependencies = [
"base64 0.21.4",
"base64-serde",

View File

@ -1,6 +1,6 @@
[package]
name = "nebula-ffi"
version = "0.2.0"
version = "1.7.2"
edition = "2021"
description = "A Rust wrapper crate for communicating with Nebula via a CGO FFI."
license = "GPL-3.0-or-later"
@ -12,5 +12,5 @@ repository = "https://git.e3t.cc/~core/trifid"
[build-dependencies]
gobuild = "0.1.0-alpha.2"
bindgen = "0.68.1"
cc = "1"

View File

@ -1,27 +1,61 @@
use std::env;
use std::{env, process};
use std::path::PathBuf;
use bindgen::CargoCallbacks;
use gobuild::BuildMode;
use std::path::Path;
fn main() {
// Find compiler: if /usr/local/go/bin/go exists, use that
// otherwise, rely on PATH
let compiler = if Path::new("/usr/local/go/bin/go").exists() {
println!("Using /usr/local/go/bin/go");
"/usr/local/go/bin/go"
} else {
"go"
// Find compiler:
// 1. GOC
// 2. /usr/local/go/bin/go
// 3. system "go"
let compiler = match env::var("GOC") {
Ok(c) => c,
Err(_) => {
if Path::new("/usr/local/go/bin/go").exists() {
"/usr/local/go/bin/go".to_string()
} else {
"go".to_string()
}
}
};
gobuild::Build::new().compiler(compiler).buildmode(BuildMode::CArchive).file("main.go").compile("nebulaffi");
println!("using go compiler {}", compiler);
//gobuild::Build::new().compiler(compiler).buildmode(BuildMode::CArchive).file("main.go").compile("nebulaffi");
let c_compiler = cc::Build::new().try_get_compiler().unwrap();
let out_dir = env::var("OUT_DIR").unwrap();
let out_path = PathBuf::from(out_dir);
let out_file = LIBRARY_PREFIX.to_owned() + "nebula" + LIBRARY_EXTENSION;
let out = out_path.join(out_file);
let mut command = process::Command::new(compiler);
command.args(["build", "-buildmode", "c-archive", "-o", out.display().to_string().as_str(), "main.go"]);
command.env("CGO_ENABLED", "1");
command.env("CC", c_compiler.path());
command.env("GOARCH", goarch());
command.env("GOOS", goos());
println!("running go compile command: {:?}", command);
let mut child = command.spawn().unwrap();
let status = child.wait().unwrap();
println!("{}", status);
if !status.success() {
panic!("`{:?}` exited with status code {}", command, status);
}
println!("Go compile success");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
println!("cargo:rustc-link-lib=static=nebula");
println!("cargo:rustc-link-search=native={}", env::var("OUT_DIR").unwrap());
println!("cargo:rustc-link-search={}", out_path.display());
println!("cargo:rustc-link-lib=dylib=nebulaffi");
//let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
//println!("cargo:rustc-link-search={}", out_path.display());
println!("cargo:rerun-if-changed=go.mod");
println!("cargo:rerun-if-changed=go.sum");
println!("cargo:rerun-if-changed=main.go");
@ -29,7 +63,7 @@ fn main() {
println!("Generating bindings");
let bindings = bindgen::Builder::default()
.header(out_path.join("libnebulaffi.h").display().to_string())
.header(out_path.join("libnebula.h").display().to_string())
.parse_callbacks(Box::new(CargoCallbacks))
.generate()
.expect("Error generating CFFI bindings");
@ -39,3 +73,40 @@ fn main() {
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
#[cfg(target_family = "unix")]
const LIBRARY_EXTENSION: & str = ".a";
#[cfg(target_family = "unix")]
const LIBRARY_PREFIX: & str = "lib";
#[cfg(target_family = "windows")]
const LIBRARY_EXTENSION: &str = ".lib";
#[cfg(target_family = "windows")]
const LIBRARY_PREFIX: &str = "";
fn goarch() -> String {
match env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
"x86" => "386",
"x86_64" => "amd64",
"mips" => "mips",
"powerpc" => "ppc",
"powerpc64" => "ppc64",
"arm" => "arm",
"aarch64" => "arm64",
arch => panic!("unsupported architecture {arch}")
}.to_string()
}
fn goos() -> String {
match env::var("CARGO_CFG_TARGET_OS").unwrap().as_str() {
"windows" => "windows",
"macos" => "darwin",
"ios" => "darwin",
"linux" => "linux",
"android" => "android",
"freebsd" => "freebsd",
"dragonfly" => "dragonfly",
"openbsd" => "openbsd",
"netbsd" => "netbsd",
os => panic!("unsupported operating system {os}")
}.to_string()
}

View File

@ -1,6 +1,6 @@
[package]
name = "tfclient"
version = "0.2.5"
version = "0.2.6"
edition = "2021"
description = "An open-source reimplementation of a Defined Networking-compatible client"
license = "GPL-3.0-or-later"
@ -30,7 +30,7 @@ ipnet = "2.7"
base64-serde = "0.7"
dnapi-rs = { version = "0.2", path = "../dnapi-rs" }
serde_yaml = "0.9"
nebula-ffi = { version = "0.2", path = "../nebula-ffi", optional = true }
nebula-ffi = { version = "1.7.2", path = "../nebula-ffi", optional = true }
[package.metadata.deb]
maintainer = "c0repwn3r <core@e3t.cc>"
copyright = "e3team <admin@e3t.cc>"