Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: Allow running doctests and unit tests #5435

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ jobs:
working-directory: ./rust
run: cargo test --doc -- --show-output

- name: cargo unit test
working-directory: ./rust
run: cargo test --no-run

- name: cargo clippy
working-directory: ./rust
run: cargo clippy -- -D warnings
Expand Down
8 changes: 4 additions & 4 deletions rust/binaryninjacore-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn link_path() -> PathBuf {
}

fn main() {
println!("cargo:rerun-if-changed=../../binaryninjacore.h");
println!("cargo::rerun-if-changed=../../binaryninjacore.h");

//Cargo's output directory
let out_dir = env::var("OUT_DIR").unwrap();
Expand All @@ -69,11 +69,11 @@ fn main() {
)
.expect("failed to create required symlink");
}
println!("cargo:rustc-link-search={}", out_dir);
println!("cargo::rustc-link-search={}", out_dir);
}

println!("cargo:rustc-link-lib=binaryninjacore");
println!("cargo:rustc-link-search={}", link_path.to_str().unwrap());
println!("cargo::rustc-link-lib=binaryninjacore");
println!("cargo::rustc-link-search={}", link_path.to_str().unwrap());

let current_line = "#define BN_CURRENT_UI_ABI_VERSION ";
let minimum_line = "#define BN_MINIMUM_UI_ABI_VERSION ";
Expand Down
69 changes: 69 additions & 0 deletions rust/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
use std::env;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;

#[cfg(target_os = "macos")]
static LASTRUN_PATH: (&str, &str) = ("HOME", "Library/Application Support/Binary Ninja/lastrun");

#[cfg(target_os = "linux")]
static LASTRUN_PATH: (&str, &str) = ("HOME", ".binaryninja/lastrun");

#[cfg(windows)]
static LASTRUN_PATH: (&str, &str) = ("APPDATA", "Binary Ninja\\lastrun");

// Check last run location for path to BinaryNinja; Otherwise check the default install locations
fn link_path() -> PathBuf {
use std::io::prelude::*;

let home = PathBuf::from(env::var(LASTRUN_PATH.0).unwrap());
let lastrun = PathBuf::from(&home).join(LASTRUN_PATH.1);

File::open(lastrun)
.and_then(|f| {
let mut binja_path = String::new();
let mut reader = BufReader::new(f);

reader.read_line(&mut binja_path)?;
Ok(PathBuf::from(binja_path.trim()))
})
.unwrap_or_else(|_| {
#[cfg(target_os = "macos")]
return PathBuf::from("/Applications/Binary Ninja.app/Contents/MacOS");

#[cfg(target_os = "linux")]
return home.join("binaryninja");

#[cfg(windows)]
return PathBuf::from(env::var("PROGRAMFILES").unwrap())
.join("Vector35\\BinaryNinja\\");
})
}

fn main() {
// TODO : Enable the following when https://github.com/rust-lang/rust/issues/43781 stabilizes
// #[cfg(doc)]
Expand All @@ -9,4 +51,31 @@ fn main() {
"target/doc/under_construction.png",
);
let _ = std::fs::copy("../docs/img/logo.png", "target/doc/logo.png");

let install_path = env::var("BINARYNINJADIR")
.map(PathBuf::from)
.unwrap_or_else(|_| link_path());

#[cfg(target_os = "linux")]
println!(
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "macos")]
println!(
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "windows")]
{
println!("cargo::rustc-link-lib=binaryninjacore");
println!(
"cargo::rustc-link-search={}",
install_path.to_str().unwrap()
);
}
}
11 changes: 7 additions & 4 deletions rust/examples/basic_script/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,24 @@ fn main() {

#[cfg(target_os = "linux")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "macos")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "windows")]
{
println!("cargo:rustc-link-lib=binaryninjacore");
println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
println!("cargo::rustc-link-lib=binaryninjacore");
println!(
"cargo::rustc-link-search={}",
install_path.to_str().unwrap()
);
}
}
11 changes: 7 additions & 4 deletions rust/examples/decompile/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,24 @@ fn main() {

#[cfg(target_os = "linux")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "macos")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "windows")]
{
println!("cargo:rustc-link-lib=binaryninjacore");
println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
println!("cargo::rustc-link-lib=binaryninjacore");
println!(
"cargo::rustc-link-search={}",
install_path.to_str().unwrap()
);
}
}
11 changes: 7 additions & 4 deletions rust/examples/dwarf/dwarf_export/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,24 @@ fn main() {

#[cfg(target_os = "linux")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "macos")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "windows")]
{
println!("cargo:rustc-link-lib=binaryninjacore");
println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
println!("cargo::rustc-link-lib=binaryninjacore");
println!(
"cargo::rustc-link-search={}",
install_path.to_str().unwrap()
);
}
}
11 changes: 7 additions & 4 deletions rust/examples/hlil_lifter/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,24 @@ fn main() {

#[cfg(target_os = "linux")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "macos")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "windows")]
{
println!("cargo:rustc-link-lib=binaryninjacore");
println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
println!("cargo::rustc-link-lib=binaryninjacore");
println!(
"cargo::rustc-link-search={}",
install_path.to_str().unwrap()
);
}
}
11 changes: 7 additions & 4 deletions rust/examples/hlil_visitor/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,24 @@ fn main() {

#[cfg(target_os = "linux")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "macos")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "windows")]
{
println!("cargo:rustc-link-lib=binaryninjacore");
println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
println!("cargo::rustc-link-lib=binaryninjacore");
println!(
"cargo::rustc-link-search={}",
install_path.to_str().unwrap()
);
}
}
11 changes: 7 additions & 4 deletions rust/examples/minidump/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,24 @@ fn main() {

#[cfg(target_os = "linux")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "macos")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "windows")]
{
println!("cargo:rustc-link-lib=binaryninjacore");
println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
println!("cargo::rustc-link-lib=binaryninjacore");
println!(
"cargo::rustc-link-search={}",
install_path.to_str().unwrap()
);
}
}
11 changes: 7 additions & 4 deletions rust/examples/mlil_lifter/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,24 @@ fn main() {

#[cfg(target_os = "linux")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "macos")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "windows")]
{
println!("cargo:rustc-link-lib=binaryninjacore");
println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
println!("cargo::rustc-link-lib=binaryninjacore");
println!(
"cargo::rustc-link-search={}",
install_path.to_str().unwrap()
);
}
}
11 changes: 7 additions & 4 deletions rust/examples/mlil_visitor/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,24 @@ fn main() {

#[cfg(target_os = "linux")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "macos")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "windows")]
{
println!("cargo:rustc-link-lib=binaryninjacore");
println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
println!("cargo::rustc-link-lib=binaryninjacore");
println!(
"cargo::rustc-link-search={}",
install_path.to_str().unwrap()
);
}
}
11 changes: 7 additions & 4 deletions rust/examples/template/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,24 @@ fn main() {

#[cfg(target_os = "linux")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "macos")]
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
"cargo::rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
install_path.to_str().unwrap(),
install_path.to_str().unwrap(),
);

#[cfg(target_os = "windows")]
{
println!("cargo:rustc-link-lib=binaryninjacore");
println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
println!("cargo::rustc-link-lib=binaryninjacore");
println!(
"cargo::rustc-link-search={}",
install_path.to_str().unwrap()
);
}
}
Loading