forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves Vector35#2521; Rust API Linking Errors
- Loading branch information
1 parent
6d2b50a
commit 95f819e
Showing
11 changed files
with
551 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
cmake_minimum_required(VERSION 3.9 FATAL_ERROR) | ||
|
||
project(test_headless) | ||
|
||
file(GLOB PLUGIN_SOURCES | ||
${PROJECT_SOURCE_DIR}/Cargo.toml | ||
${PROJECT_SOURCE_DIR}/src/*.rs) | ||
|
||
file(GLOB API_SOURCES | ||
${PROJECT_SOURCE_DIR}/../../../binaryninjacore.h | ||
${PROJECT_SOURCE_DIR}/../../binaryninjacore-sys/build.rs | ||
${PROJECT_SOURCE_DIR}/../../binaryninjacore-sys/Cargo.toml | ||
${PROJECT_SOURCE_DIR}/../../binaryninjacore-sys/src/* | ||
${PROJECT_SOURCE_DIR}/../../Cargo.toml | ||
${PROJECT_SOURCE_DIR}/../../src/*.rs) | ||
|
||
if(CMAKE_BUILD_TYPE MATCHES Debug) | ||
set(TARGET_DIR ${PROJECT_BINARY_DIR}/target/debug) | ||
set(CARGO_OPTS --target-dir=${PROJECT_BINARY_DIR}/target) | ||
else() | ||
set(TARGET_DIR ${PROJECT_BINARY_DIR}/target/release) | ||
set(CARGO_OPTS --target-dir=${PROJECT_BINARY_DIR}/target --release) | ||
endif() | ||
|
||
set(OUTPUT_FILE basic_script${CMAKE_EXECUTABLE_SUFFIX}) | ||
set(OUTPUT_PATH ${CMAKE_BINARY_DIR}/out/bin/${OUTPUT_FILE}) | ||
|
||
add_custom_target(test_headless ALL DEPENDS ${OUTPUT_PATH}) | ||
add_dependencies(test_headless binaryninjaapi) | ||
|
||
find_program(RUSTUP_PATH rustup REQUIRED HINTS ~/.cargo/bin) | ||
set(INSTALL_UPDATE_NIGHTLY ${RUSTUP_PATH} install nightly) | ||
|
||
if(APPLE) | ||
if(UNIVERSAL) | ||
if(CMAKE_BUILD_TYPE MATCHES Debug) | ||
set(AARCH64_LIB_PATH ${PROJECT_BINARY_DIR}/target/aarch64-apple-darwin/debug/${OUTPUT_FILE}) | ||
set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/debug/${OUTPUT_FILE}) | ||
else() | ||
set(AARCH64_LIB_PATH ${PROJECT_BINARY_DIR}/target/aarch64-apple-darwin/release/${OUTPUT_FILE}) | ||
set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/release/${OUTPUT_FILE}) | ||
endif() | ||
|
||
add_custom_command( | ||
OUTPUT ${OUTPUT_PATH} | ||
COMMAND ${INSTALL_UPDATE_NIGHTLY} | ||
COMMAND ${CMAKE_COMMAND} -E env | ||
MACOSX_DEPLOYMENT_TARGET=10.14 LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR} | ||
${RUSTUP_PATH} run nightly cargo build --target=aarch64-apple-darwin ${CARGO_OPTS} | ||
COMMAND ${CMAKE_COMMAND} -E env | ||
MACOSX_DEPLOYMENT_TARGET=10.14 LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR} | ||
${RUSTUP_PATH} run nightly cargo build --target=x86_64-apple-darwin ${CARGO_OPTS} | ||
COMMAND cp ${AARCH64_LIB_PATH} ${OUTPUT_PATH}-aarch | ||
COMMAND cp ${X86_64_LIB_PATH} ${OUTPUT_PATH} | ||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} | ||
DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES}) | ||
else() | ||
if(CMAKE_BUILD_TYPE MATCHES Debug) | ||
set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/debug/${OUTPUT_FILE}) | ||
else() | ||
set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/release/${OUTPUT_FILE}) | ||
endif() | ||
|
||
add_custom_command( | ||
OUTPUT ${OUTPUT_PATH} | ||
COMMAND ${INSTALL_UPDATE_NIGHTLY} | ||
COMMAND ${CMAKE_COMMAND} -E env | ||
MACOSX_DEPLOYMENT_TARGET=10.14 LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR} | ||
${RUSTUP_PATH} run nightly cargo build --target=x86_64-apple-darwin ${CARGO_OPTS} | ||
COMMAND cp ${X86_64_LIB_PATH} ${OUTPUT_PATH} | ||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} | ||
DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES}) | ||
endif() | ||
elseif(WIN32) | ||
add_custom_command( | ||
OUTPUT ${OUTPUT_PATH} | ||
COMMAND ${INSTALL_UPDATE_NIGHTLY} | ||
COMMAND ${CMAKE_COMMAND} -E env | ||
LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR} | ||
${RUSTUP_PATH} run nightly cargo build ${CARGO_OPTS} | ||
COMMAND cp ${TARGET_DIR}/${OUTPUT_FILE} ${OUTPUT_PATH} | ||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} | ||
DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES}) | ||
else() | ||
add_custom_command( | ||
OUTPUT ${OUTPUT_PATH} | ||
COMMAND ${INSTALL_UPDATE_NIGHTLY} | ||
COMMAND ${CMAKE_COMMAND} -E env | ||
LIBCLANG_PATH=${LLVM_PATH}/lib LLVM_VERSION=${LLVM_VERSION} BINARYNINJADIR=${BN_CORE_OUTPUT_DIR} | ||
${RUSTUP_PATH} run nightly cargo build ${CARGO_OPTS} | ||
COMMAND cp ${TARGET_DIR}/${OUTPUT_FILE} ${OUTPUT_PATH} | ||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} | ||
DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES}) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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() { | ||
// Use BINARYNINJADIR first for custom BN builds/configurations (BN devs/build server), fallback on defaults | ||
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()); | ||
} | ||
} |
Oops, something went wrong.