diff --git a/Cargo.lock b/Cargo.lock index fb91072..0a4ae0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/nebula-ffi/Cargo.toml b/nebula-ffi/Cargo.toml index f75d9a6..789274c 100644 --- a/nebula-ffi/Cargo.toml +++ b/nebula-ffi/Cargo.toml @@ -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" \ No newline at end of file diff --git a/nebula-ffi/build.rs b/nebula-ffi/build.rs index c95c6ff..bca0dad 100644 --- a/nebula-ffi/build.rs +++ b/nebula-ffi/build.rs @@ -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() +} \ No newline at end of file diff --git a/tfclient/Cargo.toml b/tfclient/Cargo.toml index 6dc18a1..51fd4c5 100644 --- a/tfclient/Cargo.toml +++ b/tfclient/Cargo.toml @@ -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 " copyright = "e3team "