summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorJosh Ferrell <josh@vector35.com>2025-09-01 17:47:08 -0400
committerJosh Ferrell <josh@vector35.com>2025-09-01 17:47:08 -0400
commiteef93b932844110cf19a4b2741b528623b77998e (patch)
treeb8c89a56e603dc837d3fe48a33aa619baf2e7d4a /rust
parent42fd7435e2713181facfc6d4f54b42bdaa1d2825 (diff)
Add option to build rust api without linking to core
Diffstat (limited to 'rust')
-rw-r--r--rust/Cargo.toml6
-rw-r--r--rust/binaryninjacore-sys/Cargo.toml9
-rw-r--r--rust/binaryninjacore-sys/build.rs25
-rw-r--r--rust/build.rs6
4 files changed, 38 insertions, 8 deletions
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 0b9ac4f3..4856f97b 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -7,16 +7,20 @@ rust-version = "1.83.0"
license = "Apache-2.0"
[features]
+default = ["core"]
# This is used when statically linking to prevent exporting CorePluginABIVersion and UiPluginABIVersion.
no_exports = []
# Add this if you want to support the demo version of the product.
# This will disable certain functions that do not exist in the demo build.
demo = ["no_exports"]
+# If disabled, a stub binaryninjacore library will be generated and linked.
+# This feature should only be disabled for shared libraries that will later be loaded in the same process as the real binaryninjacore.
+core = ["binaryninjacore-sys/core"]
[dependencies]
log = { version = "0.4", features = ["std"] }
rayon = { version = "1.10", optional = true }
-binaryninjacore-sys = { path = "binaryninjacore-sys" }
+binaryninjacore-sys = { path = "binaryninjacore-sys", default-features = false}
thiserror = "2.0"
serde = "1.0"
serde_derive = "1.0"
diff --git a/rust/binaryninjacore-sys/Cargo.toml b/rust/binaryninjacore-sys/Cargo.toml
index bfd49a41..d4faaec8 100644
--- a/rust/binaryninjacore-sys/Cargo.toml
+++ b/rust/binaryninjacore-sys/Cargo.toml
@@ -7,8 +7,15 @@ edition = "2021"
links = "binaryninjacore"
license = "Apache-2.0"
+[features]
+default = ["core"]
+# If disabled, a stub binaryninjacore library will be generated and linked.
+# This feature should only be disabled for shared libraries that will later be loaded in the same process as the real binaryninjacore.
+core = []
+
[build-dependencies]
bindgen = "0.71.1"
# TODO: Remove this once bindgen correctly pins the version.
# proc-macro2 v1.0.79 does not have https://docs.rs/proc-macro2/1.0.80/proc_macro2/struct.Literal.html#method.c_string
-proc-macro2 = ">=1.0.80" \ No newline at end of file
+proc-macro2 = ">=1.0.80"
+cmake = "0.1"
diff --git a/rust/binaryninjacore-sys/build.rs b/rust/binaryninjacore-sys/build.rs
index 579276a6..672615da 100644
--- a/rust/binaryninjacore-sys/build.rs
+++ b/rust/binaryninjacore-sys/build.rs
@@ -41,6 +41,22 @@ fn link_path() -> PathBuf {
})
}
+// Generate and compile stub shared library and return the path to the folder containing the built stub library
+fn generate_stubs() -> PathBuf {
+ let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
+
+ let api_base_path = std::path::PathBuf::from(manifest_dir)
+ .join("../../")
+ .canonicalize()
+ .unwrap();
+
+ let stubs_path = api_base_path.join("stubs");
+ cmake::Config::new(stubs_path)
+ .generator("Ninja")
+ .build()
+ .join("build")
+}
+
fn main() {
println!("cargo:rerun-if-env-changed=BINARYNINJADIR");
println!("cargo:rerun-if-changed=../../binaryninjacore.h");
@@ -48,9 +64,12 @@ fn main() {
//Cargo's output directory
let out_dir = env::var("OUT_DIR").unwrap();
- let link_path = env::var("BINARYNINJADIR")
- .map(PathBuf::from)
- .unwrap_or_else(|_| link_path());
+ let link_path = match env::var("CARGO_FEATURE_CORE") {
+ Ok(_) => env::var("BINARYNINJADIR")
+ .map(PathBuf::from)
+ .unwrap_or_else(|_| link_path()),
+ Err(_) => generate_stubs(),
+ };
// Linux builds of binaryninja ship with libbinaryninjacore.so.1 in the
// application folder and no symlink. The linker will attempt to link with
diff --git a/rust/build.rs b/rust/build.rs
index c98bf81b..1e2fede0 100644
--- a/rust/build.rs
+++ b/rust/build.rs
@@ -1,8 +1,8 @@
use std::path::PathBuf;
fn main() {
- let link_path =
- std::env::var_os("DEP_BINARYNINJACORE_PATH").expect("DEP_BINARYNINJACORE_PATH specified");
+ let link_path = std::env::var_os("DEP_BINARYNINJACORE_PATH")
+ .expect("DEP_BINARYNINJACORE_PATH not specified");
println!("cargo::rustc-link-lib=dylib=binaryninjacore");
println!("cargo::rustc-link-search={}", link_path.to_str().unwrap());
@@ -15,7 +15,7 @@ fn main() {
);
}
- let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR specified");
+ let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR not specified");
let out_dir_path = PathBuf::from(out_dir);
// Copy all binaries to OUT_DIR for unit tests.