Skip to content

Commit 08b5940

Browse files
committed
[Rust] Allow configuring API path for cargo builds
1 parent e1ebfcd commit 08b5940

1 file changed

Lines changed: 28 additions & 11 deletions

File tree

rust/binaryninjacore-sys/build.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,22 @@ fn link_path() -> PathBuf {
4141
})
4242
}
4343

44+
fn api_base_path() -> PathBuf {
45+
env::var_os("BINARYNINJA_API_DIR")
46+
.map(PathBuf::from)
47+
.unwrap_or_else(|| {
48+
let manifest_dir =
49+
std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
50+
std::path::PathBuf::from(manifest_dir)
51+
.join("../../")
52+
.canonicalize()
53+
.unwrap()
54+
})
55+
}
56+
4457
// Generate and compile stub shared library and return the path to the folder containing the built stub library
4558
fn generate_stubs() -> PathBuf {
46-
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
47-
48-
let api_base_path = std::path::PathBuf::from(manifest_dir)
49-
.join("../../")
50-
.canonicalize()
51-
.unwrap();
52-
53-
let stubs_path = api_base_path.join("stubs");
59+
let stubs_path = api_base_path().join("stubs");
5460

5561
// TODO: does visual studio add the config name as a subdirectory?
5662
cmake::Config::new(stubs_path)
@@ -61,7 +67,13 @@ fn generate_stubs() -> PathBuf {
6167

6268
fn main() {
6369
println!("cargo:rerun-if-env-changed=BINARYNINJADIR");
64-
println!("cargo:rerun-if-changed=../../binaryninjacore.h");
70+
println!("cargo:rerun-if-env-changed=BINARYNINJA_API_DIR");
71+
72+
let api_base_path = api_base_path();
73+
println!(
74+
"cargo:rerun-if-changed={}",
75+
api_base_path.join("binaryninjacore.h").display()
76+
);
6577

6678
//Cargo's output directory
6779
let out_dir = env::var("OUT_DIR").unwrap();
@@ -97,7 +109,7 @@ fn main() {
97109
let minimum_line = "#define BN_MINIMUM_UI_ABI_VERSION ";
98110
let mut current_version = "0".to_string();
99111
let mut minimum_version = "0".to_string();
100-
let file = File::open("../../ui/uitypes.h").expect("Couldn't open uitypes.h");
112+
let file = File::open(api_base_path.join("ui/uitypes.h")).expect("Couldn't open uitypes.h");
101113
for line in BufReader::new(file).lines() {
102114
let line = line.unwrap();
103115
if let Some(version) = line.strip_prefix(current_line) {
@@ -108,7 +120,12 @@ fn main() {
108120
}
109121

110122
bindgen::builder()
111-
.header("../../binaryninjacore.h")
123+
.header(
124+
api_base_path
125+
.join("binaryninjacore.h")
126+
.to_string_lossy()
127+
.into_owned(),
128+
)
112129
.clang_arg("-std=c++20")
113130
.clang_arg("-x")
114131
.clang_arg("c++")

0 commit comments

Comments
 (0)